空字符串与Tesseract

时间:2018-12-15 20:47:42

标签: python opencv ocr tesseract python-tesseract

我试图从一个大文件中读取不同的裁剪图像,并且设法读取其中的大多数图像,但是当我尝试使用tesseract读取它们时,有一些图像返回一个空字符串。

String to read with tesseract

代码就是这一行:

pytesseract.image_to_string(cv2.imread("img.png"), lang="eng")

我能尝试读取这些图像吗?

预先感谢

编辑: enter image description here

1 个答案:

答案 0 :(得分:1)

在将图像传递到pytesseract之前对图像进行阈值处理可以提高准确性。

import cv2
import numpy as np

# Grayscale image
img = Image.open('num.png').convert('L')
ret,img = cv2.threshold(np.array(img), 125, 255, cv2.THRESH_BINARY)

# Older versions of pytesseract need a pillow image
# Convert back if needed
img = Image.fromarray(img.astype(np.uint8))

print(pytesseract.image_to_string(img))

此打印输出

5.78 / C02

编辑: 仅对第二张图像进行阈值处理将返回11.1。另一个有用的步骤是将page segmentation mode设置为“将图像作为单个文本行处理”。使用配置--psm 7。在第二张图像上执行此操作将返回11.1 "202 ',并且引号来自顶部的部分文本。要忽略这些字符,您还可以通过配置-c tessedit_char_whitelist=0123456789.%设置要使用白名单搜索的字符。一切都在一起:

pytesseract.image_to_string(img, config='--psm 7 -c tessedit_char_whitelist=0123456789.%')

这将返回11.1 202。显然,pytesseract在使用该百分比符号时遇到了困难,我不确定如何通过图像处理或配置更改来改善这一点。