【问题标题】:Unable to get the correct OCR text Python无法获取正确的 OCR 文本 Python
【发布时间】:2022-01-10 23:34:35
【问题描述】:

我正在尝试从 .png 文件中读取文本,但无法获得正确的输出。

这是我尝试过的代码:

from PIL import Image
from pytesseract import pytesseract

path_to_tesseract = r"Path_to Tesseract-OCR.exe"

image_path = r"Path to png file"

img = Image.open(image_path)

pytesseract.tesseract_cmd = path_to_tesseract

text = pytesseract.image_to_string(img)

print(text)

我得到的输出是这样的:m _ an I: umonfé ‘

输入.png文件:

预期的输出是LG485169046

【问题讨论】:

    标签: python-3.x image-processing ocr python-tesseract


    【解决方案1】:

    Step1- 尝试将“Path_to Tesseract-OCR.exe”添加到您的环境变量中。

    Step2- 如果 step1 不起作用,请尝试使用 passporteye 包。

    【讨论】:

    • tesseract 只是无法获得正确的输出。
    • 您是否尝试过 passporteye,它对可读区域非常有用。尝试一下并告诉我有关输出的信息。
    • 尝试使用 pip install Passporteye 安装它,但它给出了错误ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
    • 您可以使用以下方式安装:
    • 点安装 PassportEye
    【解决方案2】:

    我想建议你使用inRange-thresholding

    步骤是:

    1. 将图像转换为hsv色彩空间
    2. 设置上下边界
    3. 使用蒙版去除背景

    结果将是:

    输出将是:

    LABGROWN IGI LG485169046
    

    如果你只想要最后一部分,你可以这样做:

    >>> print(text.split(" ")[3])
    LG485169046
    

    代码:

    import cv2
    import pytesseract
    
    
    # Load the image
    img = cv2.imread("KfzeJ.png")
    
    # Resize the image
    img = cv2.resize(img, None, fx=2, fy=2)
    
    # Convert to the hsv color-space
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    
    # Get binary-mask
    msk = cv2.inRange(hsv, array([0, 0, 0]), array([179, 255, 80]))
    krn = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 3))
    dlt = cv2.dilate(msk, krn, iterations=1)
    thr = 255 - cv2.bitwise_and(dlt, msk)
    
    # OCR
    txt = pytesseract.image_to_string(thr)
    print(txt.split(" ")[3])
    

    附: Pytesseract 版本:0.3.8

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-11
      • 1970-01-01
      • 2021-07-07
      相关资源
      最近更新 更多