4

I am using python3.7 and Tesseract-OCR version 5 on my Windows 10 box. I have pictures containing the numbers. However, despite that it is super clear to the human eyes, the Tesseract can't extract them correctly. 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. 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

enter image description here


Hi Nathancy, here is the "unsupported image object" error I got when I run the pytesseract command

>>> 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
Difan Zhao
  • 379
  • 6
  • 20

1 Answers1

3

Here's a quick example performing a bit of preprocessing using OpenCV:

enter image description here

Result from 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()
nathancy
  • 42,661
  • 14
  • 115
  • 137
  • I will give it a try tonight. Thanks for the response! – Difan Zhao Jan 07 '20 at 17:37
  • 1
    Hi Nathancy, I got this "unsupported image object" error when I run the pytesseract command... – Difan Zhao Jan 09 '20 at 05:02
  • 1
    I think that I got it figured out. "thresh" is kind of an array format. I have to convert back to the image format with this "image_new = Image.fromarray(thresh)". Then this command will work "data = pytesseract.image_to_string(image_new, lang='eng', config='--psm 6')". It gives me the correct result now. Thank you! – Difan Zhao Jan 10 '20 at 04:59
  • Absolutely! Thank you – Difan Zhao Jan 11 '20 at 04:37