【问题标题】:Pytesseract can't read simple numbersPytesseract 无法读取简单的数字
【发布时间】:2020-12-10 19:21:51
【问题描述】:

我正在尝试使用 pytesseract 读取这个数字:,当我这样做时会打印出 IL

import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract'
text = pytesseract.image_to_string(Image.open("Number.jpg"))
print(text)

我也尝试将图像转换为黑色或白色:,但这也没有奏效。我做错了什么?

【问题讨论】:

    标签: image python-imaging-library python-tesseract


    【解决方案1】:

    pytesseract 效果最好,可以在白色背景上使用黑色文本提供准确的输出。预处理是获得准确结果的主要部分。但是在您的情况下,简单的逆二进制阈值处理足以获得正确的输出,因为您的图像根本不包含任何噪声。只有在光照不均匀的情况下才应使用自适应阈值。

    >>> image = cv2.imread("14.jpg",0)
    >>> thresh = cv2.threshold(image,0,255,cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
    >>> data = pytesseract.image_to_string(thresh,config= '--psm 6 digits')
    >>> data
    '14'
    

    我认为tesseract的版本不会造成任何问题。

    Tesseract 版本tesseract v5.0.0-alpha.20200223 pytesseract 版本pytesseract Version: 0.3.4

    【讨论】:

    • 非常感谢! @Tarun Chakitha
    【解决方案2】:

    我认为您错过了将 pytesseract'page-segmentation-mode (psm) 配置设置为 7 将图像视为单个文本行。 (source)

    我也应用了阈值,我的结果:

    当我将 psm 设置为 7 时

    txt = pytesseract.image_to_string(thr, config="--psm 7 digits")
    print(txt)
    

    结果:

    14
    

    代码:


    import cv2
    import pytesseract
    
    img = cv2.imread("d3njD.jpg")
    gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    thr = cv2.adaptiveThreshold(gry, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
                                cv2.THRESH_BINARY_INV, 11, 4)
    txt = pytesseract.image_to_string(thr, config="--psm 7 digits")
    print(txt)
    cv2.imshow("thr", thr)
    cv2.waitKey(0)
    

    请注意,对于其他图像,此解决方案可能不起作用。您可能需要额外的图像处理方法,或者您需要更改参数。

    • pytesseract 版本:4.1.1

    【讨论】:

    • 嗯,我尝试了与您完全相同的代码,现在我得到了具有相同代码的“ER”,现在我想知道它是否与我的安装有关。
    • 在同一张图片还是不同的图片上?
    • 如果您在同一张图片上收到ER,请用txt = pytesseract.image_to_string(thr, config="--psm 7 digits") 替换代码中的txt = pytesseract 部分
    • 所以我尝试这样做,但这次我没有输出(我相信)它在控制台中打印了 ,你认为这可能是我的 pytesseract 灌输吗?
    • 你使用的是和我一样的代码吗?或者,如果您使用不同的代码,请将其发布到您的问题中
    猜你喜欢
    • 2020-10-23
    • 2020-04-01
    • 2021-04-14
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 2022-08-21
    • 1970-01-01
    • 2022-09-28
    相关资源
    最近更新 更多