带有自定义字体的 Pytesseract 错误地对数字进行分类

Pytesseract with custom font incorrectly classifying numbers

我正在尝试使用 pytesseract 检测价格。

然而我的结果很糟糕。

我有一张大图片,在不同位置有多个价格。 这些位置是不变的,所以我裁剪图像并将每个区域保存为新图像,然后尝试检测文本。

我知道文本只会包含 0123456789$¢。

我使用 trainyourtesseract.com 训练了我的新字体。

例如,我拍了这张照片。

将它的大小加倍,并对其设置阈值。

运行 它通过 tesseract 得到 8.

的输出

如有任何帮助,我们将不胜感激。

def getnumber(self, img):
   grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
   thresh, grey = cv2.threshold(grey, 50, 255, cv2.THRESH_BINARY_INV)

   filename = "{}.png".format(os.getpid())
   cv2.imwrite(filename, grey)

   text = pytesseract.image_to_string(Image.open(filename), lang='Droid',
                                      config='--psm 13 --oem 3 -c tessedit_char_whitelist=0123456789.$¢')
   os.remove(filename)
   return(text)

你走在正确的轨道上。在为 OCR 预处理图像时,您希望文本为黑色,背景为白色。思路是将图像放大,Otsu的阈值得到二值图像,然后进行OCR。我们使用 --psm 6 告诉 Pytesseract 假设一个单一的统一文本块。查看 以获得更多配置选项。这是处理后的图像:

OCR 结果:

代码

import cv2
import pytesseract
import imutils

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"

# Resize, grayscale, Otsu's threshold
image = cv2.imread('1.png')
image = imutils.resize(image, width=500)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Perform text extraction
data = pytesseract.image_to_string(thresh, lang='eng',config='--psm 6')
print(data)

cv2.imshow('thresh', thresh)
cv2.imwrite('thresh.png', thresh)
cv2.waitKey()

机器规格:

Windows 10
opencv-python==4.2.0.32
pytesseract==0.2.7
numpy==1.14.5