python pytesseract.image_to_string 无法读取图片中的文字

python pytesseract.image_to_string can't read text in image

我在 Windows 10 盒子上使用 python3.7 和 Tesseract-OCR 版本 5。我有包含数字的图片。然而,尽管人眼非常清晰,但 Tesseract 无法正确提取它们。有些人给了我几个正确的读数。有些根本 return 什么都没有。附件是极端情况,什么都没有 returned...

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

我读到必须将 DPI 更改为 300 才能让 Tesseract 正确读取它。你能告诉我最好的方法吗?我用谷歌搜索,但找不到直接的方法来做到这一点。谢谢!

输入图片


嗨 Nathancy,这是我在 运行 pytesseract 命令

时遇到的 "unsupported image object" 错误
>>> 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

这是一个使用 OpenCV 执行一些预处理的简单示例:

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()