Empty string with Tesseract

╄→尐↘猪︶ㄣ 提交于 2019-12-04 05:20:02

问题


I'm trying to read different cropped images from a big file and I manage to read most of them but there are some of them which return an empty string when I try to read them with tesseract.

The code is just this line:

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

Is there anything I can try to be able to read these kind of images?

Thanks in advance

Edit:


回答1:


Thresholding the image before passing it to pytesseract increases the accuracy.

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

This printed out

5.78 / C02

Edit: Doing just thresholding on the second image returns 11.1. Another step that can help is to set the page segmentation mode to "Treat the image as a single text line." with the config --psm 7. Doing this on the second image returns 11.1 "202 ', with the quotation marks coming from the partial text at the top. To ignore those, you can also set what characters to search for with a whitelist by the config -c tessedit_char_whitelist=0123456789.%. Everything together:

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

This returns 11.1 202. Clearly pytesseract is having a hard time with that percent symbol, which I'm not sure how to improve on that with image processing or config changes.



来源:https://stackoverflow.com/questions/53797130/empty-string-with-tesseract

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!