【问题标题】:How to read digits from an image with PyTesseract OCR?PyTesseract OCR 无法从简单图像中读取数字
【发布时间】:2020-04-01 21:54:30
【问题描述】:

我正在尝试让 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')

输出:

[]

【问题讨论】:

    标签: python image ocr tesseract python-tesseract


    【解决方案1】:

    在执行 OCR 时,重要的是要预先设置图像,以便所需的前景文本为黑色,而背景为白色。为此,我们可以使用 OpenCV 对 Otsu 的图像进行阈值处理并获得二值图像。然后我们应用轻微的高斯模糊来平滑图像,然后将其放入 Pytesseract。我们使用--psm 6 配置将图像视为单个统一的文本块。有关更多配置选项,请参阅here。


    这是预处理后的图像和 Pytesseract 的结果

    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()
    
    猜你喜欢
    • 2020-10-23
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 2021-07-07
    • 2018-11-06
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    相关资源
    最近更新 更多