PyTesseract 无法识别图像

PyTesseract not recognising images

我目前遇到 pytesseract 问题,软件无法在此图像中检测到数字:

https://i.stack.imgur.com/kmH2R.png

这是从应用了阈值过滤器的更大图像中截取的。

出于某种原因,pytesseract 不想识别这张图片中的 6。有什么建议么?这是我的代码:

image = #Insert raw image here. My code takes a screenshot.
image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
image = cv2.medianBlur(image, 3)
rel, gray = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
# If you want to use the image from above, start here.
image = Image.fromarray(image)
string = pytesseract.image_to_string(image)
print(string)

编辑:经过进一步调查,我的代码可以很好地处理包含 2 位数字的数字。但不是那些带有单数的数字。

pytesseract 默认为查找大块文本的模式(PSM_SINGLE_BLOCK 或 --psm 6),为了让它检测单个字符,您需要 运行 它与选项 --psm 10 (PSM_SINGLE_CHAR)。但是,由于您提供的图像角落中的黑点,它会将它们检测为随机破折号并且 returns 在这种模式下什么也没有,因为它有多个字符,所以在这种情况下您需要使用 --psm 8 (PSM_SINGLE_WORD):

string = pytesseract.image_to_string(image, config='--psm 8')

此输出将包括那些随机字符,因此您需要在 pytesseract 运行s 之后去除它们或改进数字周围的边界框以消除任何噪音。此外,如果检测到的所有字符都是数字,您可以在“--psm 8”之后添加“-c tessedit_char_whitelist=0123456789”以改进检测。


其他一些简化代码的小技巧是 cv2.imread 有一个选项可以将图像读取为黑白图像,因此您之后不需要 运行 cvtColor,只需执行以下操作:

image = cv2.imread('/path/to/image/6.png', 0)

您还可以在对 pytesseract 的调用中创建 PIL 图像对象,以便该行简化为:

string = pytesseract.image_to_string(Image.fromarray(img), config='--psm 8')

只要您的脚本顶部有 'from PIL import Image'。