简体   繁体   English

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

[英]python pytesseract.image_to_string can't read text in image

I am using python3.7 and Tesseract-OCR version 5 on my Windows 10 box.我在 Windows 10 机器上使用 python3.7 和 Tesseract-OCR 版本 5。 I have pictures containing the numbers.我有包含数字的图片。 However, despite that it is super clear to the human eyes, the Tesseract can't extract them correctly.然而,尽管人眼非常清楚,但 Tesseract 无法正确提取它们。 Some give me a couple of correct readings.有些人给了我一些正确的解读。 Some don't return anything at all.有些根本不返回任何东西。 The attached one is the extreme case that nothing is returned...附上一个是什么都不返回的极端情况...

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

I read that I must change the DPI to 300 for Tesseract to read it correctly.我读到我必须将 DPI 更改为 300,Tesseract 才能正确读取它。 Could you show me the best way to do it?你能告诉我最好的方法吗? I googled but I couldn't find a straight forward way to do it.我用谷歌搜索,但我找不到直接的方法来做到这一点。 Thanks!谢谢!

Input image输入图像

在此处输入图片说明


Hi Nathancy, here is the "unsupported image object" error I got when I run the pytesseract command嗨 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

Here's a quick example performing a bit of preprocessing using OpenCV:这是一个使用 OpenCV 执行一些预处理的快速示例:

在此处输入图片说明

Result from Pytesseract OCR: Pytesseract OCR 的结果:

55 58 6 25 41 1

Code代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM