【问题标题】:Can't get numbers from image with python, Tesseract and opencv无法使用 python、Tesseract 和 opencv 从图像中获取数字
【发布时间】:2021-03-08 01:55:59
【问题描述】:

我必须从使用 python tesseract 和 opencv 的水表图像中获取数字。 我试图更改 --psm 但它不起作用。

这里是未经修改的图像:

enter image description here

这里是输出图像:

enter image description here

我需要你们的帮助,我正在启动 python,但我已经被阻止了:'(

我的代码:

from PIL import Image
import pytesseract
import cv2
import numpy as np
import urllib
import requests
pytesseract.pytesseract.tesseract_cmd = r'C:\Users\Hymed\AppData\Local\Tesseract-OCR\tesseract.exe'

col = Image.open("pts.jpg")
gray = col.convert('L')
bw = gray.point(lambda x: 0 if x<128 else 255, '1')
bw.save("cp19.png")


image = cv2.imread('cp19.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = 255 - cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Blur and perform text extraction
thresh = cv2.GaussianBlur(thresh, (3,3), 0)
img1 = np.array(thresh)
data = pytesseract.image_to_string(img1, config='--psm 11 digits')
print(data)

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

【问题讨论】:

    标签: python opencv tesseract python-tesseract


    【解决方案1】:

    你几乎完成了任务。

    我在GaussianBlur之后使用除法运算。

    div = cv2.divide(gray, thresh, scale=192)
    

    结果:

    当我从图像中读取时:

    data = pytesseract.image_to_string(div, config='--psm 11 digits')
    print(data)
    

    结果:

    00000161
    

    代码:(刚刚添加 div = cv2.divide(gray, thresh, scale=192) 其余是您的代码)

    from PIL import Image
    import pytesseract
    import cv2
    import numpy as np
    
    col = Image.open("TOaEW.jpg")
    gray = col.convert('L')
    bw = gray.point(lambda x: 0 if x < 128 else 255, '1')
    bw.save("cp19.png")
    
    image = cv2.imread('cp19.png')
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    thresh = 255 - cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
    
    # Blur and perform text extraction
    thresh = cv2.GaussianBlur(thresh, (3, 3), 0)
    
    div = cv2.divide(gray, thresh, scale=192)  # added
    
    data = pytesseract.image_to_string(div, config='--psm 11 digits')
    print(data)
    

    【讨论】:

    • 感谢它的神奇。我可以在不同的水表上使用它吗?
    • 我认为是,但我不能保证
    • 我想对于相似的图像你可以使用这个,但是对于具有不同强度的图像你必须做更多的预处理。
    【解决方案2】:

    我尝试使用 Tesseract 从图像中读取数字。除了第一行显示的数字外,它还在第二行返回了一个无法识别的符号。我不明白我做错了什么。这是代码和结果 code and output

    这是我从中提取数字的图像: Image used for number extraction

    【讨论】:

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