【问题标题】:Why pytesseract does not recognise single digits?为什么 pytesseract 不能识别个位数?
【发布时间】:2014-09-28 23:16:01
【问题描述】:

我正在一个网站上执行 ocr,特别是在这两个图像上:

我是 OCR 的新手,我使用以下内容:

from PIL import Image
import pytesseract

my_image = '....png'
text = pytesseract.image_to_string(Image.open(my_image))

在第二张图片中,它可以识别除单个数字 3、4、5、6 之外的所有内容。

在第一张图片中,它也无法识别单个数字。

我通过调整图像大小、反转图像和使用阈值来预处理图像。

这是一种标准字体,所以我知道还有其他方法可以做到这一点,但在一定程度上它对我有用,所以我想在进入更高级的东西之前保持简单。

【问题讨论】:

  • 澄清一下,它不能识别 only 第二张图片中的 3、4、5、6 - 第一张,它不能识别 any个位数对吗?
  • 是的,任何图像中都没有单个数字。
  • 数字似乎使用 psm 6 显示。
  • 个位数也出现了?
  • 尝试使用 ImageMagick 将图像转换为 tiff,然后在终端上测试:tesseract yourimag.tiff output.txt

标签: python ocr tesseract python-tesseract


【解决方案1】:

对于这两个图像,你可以

  1. 对图像进行上采样:为了准确识别。
  2. 申请simple-thresholding:展示功能。

对于第一张图片,可以取部分图片选择范围:

结果将是:

62001
33000

代码:


import cv2
import pytesseract

img1 = cv2.imread("lNKH4.png")  # "FX2in.png"
gry1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
(h, w) = gry1.shape[:2]
gry1 = cv2.resize(gry1, (w*2, h*2))
gry1 = gry1[30:(h*2), w+50:w*2]
thr1 = cv2.threshold(gry1, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
txt1 = pytesseract.image_to_string(thr1, config="--psm 6 digits")
print(txt1)
cv2.imshow("thr1", thr1)
cv2.waitKey(0)

第二张图片:

结果将是:

2
3 1.28 4.50 9.00
4 2.00 3.75 3.00
5 3.50 4.33 1.72
6 7.00 6.00 1.28

相同的代码,只需删除以下行:

gry1 = gry1[30:(h*2), w+50:w*2]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多