python pytesseract.image_to_string无法读取图像中的文本

问题描述 投票:2回答:1

我正在Windows 10机器上使用python3.7和Tesseract-OCR版本5。我有包含数字的图片。但是,尽管人眼非常清楚,但Tesseract无法正确提取它们。有些给了我一些正确的读数。有些根本不返回任何东西。所附的是极端情况,什么也没有返回...

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

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

输入图像

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9GR1kwWi5qcGcifQ==” alt =“在此处输入图像说明”>


嗨,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
python image image-processing ocr python-tesseract
1个回答
0
投票

这是一个使用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()
© www.soinside.com 2019 - 2024. All rights reserved.