python - PyTesseract OCR 无法从简单图像中读取数字

标签 python image ocr tesseract python-tesseract

我试图让 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')


Image I'm trying to read

输出 :
[]

最佳答案

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

这是 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()

关于python - PyTesseract OCR 无法从简单图像中读取数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59237973/

相关文章:

python - Django Selenium 单击 ajax 模式弹出窗口上的按钮

ios - 使用 UICollectionView iOS 创建图库

opencv - SWT基于投票的色彩还原

python - 是否可以使用pytesseract从图像的特定部分提取文本

java - BlackBerry drawTexturedPath 旋转 将 anchor 移动到图像中心

python - pyOCR 没有可用的工具

python - 在 Python 3 中使用套接字传输文件

Python while 循环测试两个真/假元素

python - 在 Pandas 中管理数据清理数据的最佳方式

java - 如何使用 java 将图像插入到 openoffice writer 文档中?