PyTesseract在表中看不到一些个位数

问题描述

我有这张桌子的图片

image_of_table

我正在尝试使用PyTesseract解析它。使用此代码,我的工作真是不可思议:

from PIL import Image,ImageOps
import PyTesseract

og_image = Image.open('og_image.png')
grayscale = ImageOps.grayscale(og_image)
inverted = ImageOps.invert(grayscale.convert('RGB'))
print(PyTesseract.image_to_string(inverted))

这似乎非常准确,除了倒数第二列的一位数字为空。我需要采取其他措施来获取这些数字吗?

解决方法

Tesseract有几种页面细分模式,选择正确的页面细分是帮助其获得最佳结果的必要条件。参见documentation

在这种情况下,您也可以将tesseract限制为某个字符集。

另一件事,tesseract对字体和图像大小敏感。简单的调整大小可以大大改变结果。在这里,我将图像大小水平更改为2倍,然后垂直更改为最佳效果;)

结合以上所有内容,您将获得:

custom_config = r'--psm 6  -c tessedit_char_whitelist=0123456789.'
print(pytesseract.image_to_string(inverted.resize((1506,412),Image.ANTIALIAS),config=custom_config))
      
1525 .199 303 82 161 162 7 .241
1464 .290 424 70 139 198 25 .352
1456 .292 425 116 224 224 0 .345
1433 .240 346 81 130 187 15 .275
1390 .273 373 108 217 216 3 .345
1386 .276 383 54 181 154 18 .315
1225 .208 255 68 148 129 1 .242
1218 .238 230 46 128 127 18 .273
1117 .240 268 43 113 1193 1 .308