【问题标题】:PyTesseract not seeing some single-digit numbers in tablePyTesseract 没有在表中看到一些个位数
【发布时间】:2020-08-25 04:56:23
【问题描述】:

我有这张桌子的图片

我正在尝试使用 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))

这似乎非常准确,只是倒数第二列中的个位数为空白。我需要做一些不同的事情来获取这些数字吗?

【问题讨论】:

  • 也有同样的问题。就我而言,如果所有数字都是个位数,它就可以工作。这不是一个理想的解决方案,但如果您可以删除所有多位数字并重新处理图像,它可能会起作用。 (注意:我不知道为什么。)

标签: python python-imaging-library ocr tesseract python-tesseract


【解决方案1】:

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

【讨论】:

  • 您能否解释一下有关调整大小的更多信息?例如,尝试运行此图像gist.github.com/gumdropsteve/… 我发现裁剪出 0 并将其调整为 100x100 是可行的,但我正在努力找出一个同时适用于所有数字的调整大小。谢谢。
  • 不幸的是,没有找到合适尺寸的规则。我知道这与训练 tesseract 的数据有关,所以最好你的字体纵横比和字符大小应该接近训练中的数据。它真的不是很健壮。即使你在上面的例子中改变了一个像素的垂直尺寸,你也会得到一些新的错误
  • tesseract documenation看来,最佳字母垂直尺寸应该在20-25像素左右。
  • 太棒了,这对我有用!我之前尝试过配置,但我认为我没有正确设置它。
猜你喜欢
  • 2021-12-12
  • 2016-09-02
  • 1970-01-01
  • 1970-01-01
  • 2015-10-19
  • 2019-12-09
  • 1970-01-01
  • 1970-01-01
  • 2011-12-03
相关资源
最近更新 更多