在 pytesseract 中使用 image_to_osd 方法时出现错误

问题描述 投票:0回答:4

这是我的代码:

import pytesseract
import cv2
from PIL import Image

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files (x86)\Tesseract-OCR\tesseract.exe"


def main():
    original = cv2.imread('D_Testing.png', 0)
    # binary thresh it at value 100. It is now a black and white image
    ret, original = cv2.threshold(original, 100, 255, cv2.THRESH_BINARY)
    text = pytesseract.image_to_string(original, config='--psm 10')
    print(text)
    print(pytesseract.image_to_osd(Image.open('D_Testing.png')))


if __name__ == "__main__":
    main()

对于第一个输出,我得到了我需要的东西,即字母 D

D

这是有意的,但是当它尝试执行第二个打印语句时,它会吐出这个。

Traceback (most recent call last):
  File "C:/Users/Me/Documents/Python/OpenCV/OpenCV_WokringTest/PytesseractAttempt.py", line 18, in <module>
    main()
  File "C:/Users/Me/Documents/Python/OpenCV/OpenCV_WokringTest/PytesseractAttempt.py", line 14, in main
    print(pytesseract.image_to_osd(Image.open('D_Testing.png')))
  File "C:\Users\Me\Documents\Python\OpenCV\OpenCV_WokringTest\venv\lib\site-packages\pytesseract\pytesseract.py", line 402, in image_to_osd
    }[output_type]()
  File "C:\Users\Me\Documents\Python\OpenCV\OpenCV_WokringTest\venv\lib\site-packages\pytesseract\pytesseract.py", line 401, in <lambda>
    Output.STRING: lambda: run_and_get_output(*args),
  File "C:\Users\Me\Documents\Python\OpenCV\OpenCV_WokringTest\venv\lib\site-packages\pytesseract\pytesseract.py", line 218, in run_and_get_output
    run_tesseract(**kwargs)
  File "C:\Users\Me\Documents\Python\OpenCV\OpenCV_WokringTest\venv\lib\site-packages\pytesseract\pytesseract.py", line 194, in run_tesseract
    raise TesseractError(status_code, get_errors(error_string))
pytesseract.pytesseract.TesseractError: (1, 'Tesseract Open Source OCR Engine v4.0.0.20181030 with Leptonica Warning: Invalid resolution 0 dpi. Using 70 instead. Too few characters. Skipping this page Warning. Invalid resolution 0 dpi. Using 70 instead. Too few characters. Skipping this page Error during processing.'). 

我不知道该怎么办。我在网上找不到太多关于此错误的信息。我也不知道该怎么办。目的只是让它说出我信的方向。感谢您提前提供所有有用的评论!

python-3.x ocr tesseract python-tesseract pytesser
4个回答
6
投票

面对这个问题,尝试了不同的方法,终于解决了!!

只需直接传递图像位置,而不是通过 @Esraa Abdelmaksoud 提到的 Pillow 或 OpenCV

text = pytesseract.image_to_osd(r'Report 2.jpeg')

5
投票

Tesseract OSD 的工作原理是使用图像中的“识别”字符来检测方向和旋转。使其工作的最小字符数称为 min_characters_to_try。如果引擎找不到足够的字符或引擎无法识别此类字体,OSD 将给出该错误消息。还有其他情况会导致失败,例如旋转不接近 90,180 或 270 度。 另外,像这样直接传递裁剪后的图像文件,不要使用 OpenCV 或 Pillow 的输出。

osd = pytesseract.image_to_osd(r'D:\image.jpg',config='--psm 0 -c min_characters_to_try=5')

在你的情况下,一个字符是不够的,引擎无法清楚地读取它。


1
投票
min_characters_to_try

控制截止。默认为 50。因此您的图像应包含至少 50 个字符,OSD 才能正常工作


> $ tesseract --print-parameters | fgrep characters ... > min_characters_to_try 50 Specify minimum characters to try during OSD



0
投票
https://stackoverflow.com/a/78870807/3147027

© www.soinside.com 2019 - 2024. All rights reserved.