Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tesseract image_to_string is empty

Tags:

I have a simple text in an image image_ball.png. Usually OCR of Tesseract works well, but for this certain image it returns always an empty string. image_ball.png

In [1]: from PIL import Image

In [2]: from pytesseract import image_to_string

In [3]: img = Image.open("image_ball.png")

In [4]: image_to_string(img)
Out[5]: u''

I could not find a workaround up-to-now. How could I figure out what is going wrong with this image?

The versions are:

In [6]: import PIL

In [7]: PIL.__version__
Out[7]: '4.0.0'


$ tesseract -v
tesseract 4.0.0
 leptonica-1.77.0
  libgif 5.1.4 : libjpeg 9c : libpng 1.6.36 : libtiff 4.0.10 : zlib 1.2.11 : libwebp 1.0.2 : libopenjp2 2.3.0
 Found AVX2
 Found AVX
 Found SSE

EDIT

I tried also to convert the image to black/white. But it is still not recognized.

In [6]: image = img.convert('L') 

In [7]: image_to_string(image)
Out[8]: u''

EDIT 2

Single characters seem also to be a problem to Tesseract. Dilating or eroding the image seems not to help: image_1.png image_1.png

like image 395
Andy Avatar asked Feb 06 '19 20:02

Andy


1 Answers

Dilating image gives you the desired output.

image = cv2.imread("Ball.png", cv2.IMREAD_GRAYSCALE) 
cv2.dilate(image, (5, 5), image)
print(pytesseract.image_to_string(image), config='--psm 7')

Ball

like image 57
Dmitrii Z. Avatar answered Sep 25 '22 23:09

Dmitrii Z.