首页 > 解决方案 > 正方体 image_to_string 为空

问题描述

我在图像中有一个简单的文本image_ball.png。通常 Tesseract 的 OCR 效果很好,但是对于这个特定的图像,它总是返回一个空字符串。 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''

到目前为止,我找不到解决方法。我怎么能弄清楚这张图片出了什么问题?

版本是:

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

编辑

我还尝试将图像转换为黑白。但它仍然没有被识别。

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

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

编辑 2

单个字符似乎也是 Tesseract 的问题。扩张或侵蚀图像似乎无济于事:image_1.png image_1.png

标签: image-processingpython-imaging-libraryocrtesseract

解决方案


膨胀图像为您提供所需的输出。

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


推荐阅读