【问题标题】:Converting image to text using pytesseracct使用 tesseract 将图像转换为文本
【发布时间】:2021-03-25 06:57:21
【问题描述】:

我想从这张图片中提取数字。

我已经使用 opencv 使用此代码 sn-p 预处理图像

def inverte(imagem):
    imagem = (255-imagem)
    return imagem

import cv2
image = cv2.imread('5.jpg', 0)
print(image)
thresh = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
thresh=inverte(thresh)

这段代码的输出是这样的

之后,我使用 tesseract 使用此代码从该图像中获取文本

import pytesseract
image=cv2.imread("output.png")
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
custom_config = r'--psm 13 --oem 1 -c tessedit_char_whitelist=0123456789'
results = pytesseract.image_to_string(rgb,lang='eng',config=custom_config)
print(results)

我已经尝试了所有有效的 psm 值和 oem 值,但没有给出正确的结果

【问题讨论】:

    标签: opencv computer-vision tesseract python-tesseract text-recognition


    【解决方案1】:

    你需要知道以下几点:

    你已经知道page-segmentation-modes,所以我不需要推荐它。

    识别文本的一种方法是对图像应用阈值。你有三个选择

    您已经应用了简单阈值并且无法获得所需的结果。接下来你可以申请adaptive-threshold,这将是:

    现在,如果您使用当前配置阅读它:

    607
    

    很遗憾,数字 9 无法识别。

    如果我们尝试inRange 阈值:

    现在,如果您使用当前配置阅读它:

    607
    

    代码:

    import cv2
    import numpy as np
    import pytesseract
    
    # Load the image
    img = cv2.imread("EkXoW.jpg")
    
    # Convert to the gray-scale
    gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    
    # Convert to the HSV color
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    
    # Adaptive-threshold
    thr = cv2.adaptiveThreshold(gry, 255,
                                cv2.ADAPTIVE_THRESH_MEAN_C,
                                cv2.THRESH_BINARY, 13, 10)
    
    # Threshold-in-range
    thr_in_range = cv2.inRange(hsv,
                               np.array([0, 0, 0]),
                               np.array([179, 255, 185]))
    
    # OCR
    print("Threshold-in-range result")
    print(pytesseract.image_to_string(thr_in_range,
                                      config="--psm 13 --oem 1 -c tessedit_char_whitelist=0123456789"))
    
    print("Adaptive threshold result")
    print(pytesseract.image_to_string(thr,
                                      config="--psm 13 --oem 1 -c tessedit_char_whitelist=0123456789"))
    
    # Display
    cv2.imshow("Threshold-in-range result", thr_in_range)
    cv2.imshow("Adaptive threshold result", thr)
    cv2.waitKey(0)
    

    你可以在0.3.7得到同样的结果

    这个答案更像是一个关于文本识别中图像处理的小教程,而不是一个完整的答案。原因是无法识别数字九。不过,我认为解释中包含简短的信息和有用的链接,可能会鼓励您识别数字九。

    【讨论】:

    • 您好,我已经尝试了所有可能的图像处理技术,但是您提到的 tesseract 即使那样它也没有给出正确的结果
    猜你喜欢
    • 2015-09-04
    • 1970-01-01
    • 2010-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    相关资源
    最近更新 更多