Python pytesseract.image_to_string无法识别图像中的文本

4

我正在使用 Windows 10 中的 Python3.7 和 Tesseract-OCR 版本5。 我有一些包含数字的图片。 然而,尽管对人眼来说非常清晰,但 Tesseract 无法正确提取它们。 有些给我几个正确的读数。 有些根本没有返回任何内容。 附加的一个是极端情况,什么也没有返回...

text = pytesseract.image_to_string(n)
print(text) -> returns nothing

我看到了一个建议,说如果要让Tesseract正确读取图像,必须将DPI更改为300。请问您能告诉我最好的做法吗?我已经搜索过了,但没有找到一个简单明了的方法。谢谢!

输入图像

enter image description here


您好Nathancy,当我运行pytesseract命令时,出现了“不支持的图像对象”错误。

>>> data = pytesseract.image_to_string(thresh, lang='eng', config='--psm 6')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python37\lib\site-packages\pytesseract\pytesseract.py", line 309, in image_to_string
}[output_type]()
  File "C:\Python37\lib\site-packages\pytesseract\pytesseract.py", line 308, in <lambda>
Output.STRING: lambda: run_and_get_output(*args),
  File "C:\Python37\lib\site-packages\pytesseract\pytesseract.py", line 208, in run_and_get_output
temp_name, input_filename = save_image(image)
  File "C:\Python37\lib\site-packages\pytesseract\pytesseract.py", line 121, in save_image
image = prepare(image)
  File "C:\Python37\lib\site-packages\pytesseract\pytesseract.py", line 113, in prepare
raise TypeError('Unsupported image object')
TypeError: Unsupported image object
1个回答

3

这里是一个使用OpenCV进行预处理的快速示例:

enter image description here

通过Pytesseract OCR的结果:

55 58 6 25 41 1

代码

import cv2
import pytesseract

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

# Load image, grayscale, Otsu's threshold
image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = 255 - cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Blur and perform text extraction
thresh = cv2.GaussianBlur(thresh, (3,3), 0)
data = pytesseract.image_to_string(thresh, lang='eng', config='--psm 6')
print(data)

cv2.imshow('thresh', thresh)
cv2.waitKey()

今晚我会试一下。谢谢您的回复! - Difan Zhao
1
嗨,Nathancy,当我运行pytesseract命令时,出现了“不支持的图像对象”错误... - Difan Zhao
1
我想我已经弄清楚了。 "thresh" 是一种数组格式。我必须使用这个命令将其转换回图像格式 "image_new = Image.fromarray(thresh)"。然后这个命令就会起作用 "data = pytesseract.image_to_string(image_new, lang='eng', config='--psm 6')"。现在它给我正确的结果。谢谢! - Difan Zhao
当然可以!谢谢。 - Difan Zhao

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接