如何使用PyTesseract OCR从图像中读取数字?

3

我正在尝试让PyTesseract OCR从这张简单且裁剪得很好的图片中读取数字,但由于某些原因它无法做到。

from PIL import Image
import pytesseract as p

def obtain_balance(a):
    im = Image.open(a)
    width,height = im.size
    a = 300*5 - 120
    # print(width,height)
    left = 155+a
    top = 5
    right = 360+a 
    bottom = 120
    m1 = im.crop((left, top, right, bottom)) 
    text = p.image_to_string(m1,lang='eng',config='--psm 13 --oem 3 -c tessedit_char_whitelist=0123456789').split()
    print(text)
    m1.show()
    return text

obtain_balance('cur.jpg')

我正在尝试阅读的图片

输出:

[]
1个回答

2
在执行OCR时,重要的是对图像进行预处理,使得期望的前景文本为黑色,而背景为白色。为了做到这一点,我们可以使用OpenCV对图像进行Otsu二值化处理,得到一个二进制图像。然后,在将其输入Pytesseract之前,我们对图像进行轻微的高斯模糊以平滑图像。我们使用--psm 6配置将图像视为单个统一的文本块。有关更多配置选项,请参见此处
这是预处理后的图像和Pytesseract的结果。

enter image description here

PRACTICE ACCOUNT
$9,047.26~ i

代码

import cv2
import pytesseract

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

image = cv2.imread('1.png', 0)
thresh = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
thresh = cv2.GaussianBlur(thresh, (3,3), 0)
data = pytesseract.image_to_string(thresh, lang='eng',config='--psm 6')
print(data)

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

请阅读 https://github.com/tesseract-ocr/tesseract/wiki/ImproveQuality。 - user898678

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接