【问题标题】:extracting numbers from image with tesseract + cv2使用 tesseract + cv2 从图像中提取数字
【发布时间】:2021-10-24 03:30:07
【问题描述】:

我尝试了几天但没有成功,我决定寻求帮助:)

我对 cv2 和 tesseract 很陌生,我正在尝试做一些我认为很容易的事情,但由于某种原因并不像我预期的那么容易。

这个image 是一个包含多个值的打印屏幕,我必须阅读并转换为 text/int,this one 是原始值。 我可以将所有人与我拥有的多个图像隔离开来,但是当我尝试转换它们时,我不能。有时,它给了我正确的价值,但他错过了 90% 的时间。

这就是我的工作:

#open the image
image = cv2.imread('image.png')

#resize it to give some help (this way i was able to get some good results)
image2 = cv2.resize(image, None, fx=1.2, fy=1.2, interpolation=cv2.INTER_CUBIC)
image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)

#getting the parts of the images with the text that i want to convert
name = image2[0:100, 100:500]
stats1 = image2[110:160, 460:580]
stats2 = image2[160:210, 460:580]
stats3 = image2[220:270, 460:580]

#using pytesseract to convert from image to string
name_str = pytesseract.image_to_string(name, lang='eng',config='--psm 6')
stats1_str = pytesseract.image_to_string(ally_stats_grass, lang='eng',config='--psm 13 --oem 3 -c tessedit_char_whitelist=0123456789.%')

#print the values
print('name', name_str)
print('grass', stats1_str)

同时,我也尝试了不同的阈值和反转图像颜色的方法,也有一些扩张和侵蚀但没有成功

image2 = cv2.threshold(image2, 1, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
image2 = 255 - cv2.morphologyEx(image2, cv2.MORPH_CLOSE, kernel, iterations=1)
karnel = np.ones((1, 1), np.uint8)
image2 = cv2.dilate(image2, kernel, iterations=1)
image2 = cv2.erode(image2, kernel, iterations=1)

我只是在祈祷,希望有人能帮助我:) 感谢您的宝贵时间

【问题讨论】:

    标签: python-3.x cv2 python-tesseract


    【解决方案1】:

    对我来说,百分比看起来最难识别,所以这个答案集中在那些上。对于这张图片,我认为可能需要针对图片的不同区域使用不同的解决方案。

    百分比很困难,因为它们周围的黑色边框使阈值后的数字太粗。我使用cv2.inRange 只保留整个图像中的绿色区域。如果您有其他颜色百分比不同的图像,此方法将不起作用。并且值得指出的是,默认情况下,OpenCV 图像是 BGR,所以第二个数字是绿色分量。

    import cv2
    import pytesseract
    
    pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
    
    image = cv2.imread('plagued_void.png')
    
    image = cv2.resize(image, None, fx=5, fy=5, interpolation=cv2.INTER_CUBIC)
    
    mask = cv2.inRange(image, (0, 219, 0), (5, 255, 5))
    image = cv2.bitwise_and(image, image, mask=mask)
    
    cv2.imshow('green', image)
    cv2.waitKey(0)
    
    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    
    image = cv2.threshold(image, 1, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
    cv2.imshow('threshold', image)
    cv2.waitKey(0)
    
    config = '--oem 3 --psm 6'
    
    txt = pytesseract.image_to_string(image, config = config, lang='eng')
    print(txt)
    txt = txt.replace('\u201D', '%').replace('Y', '%')
    print(txt)
    

    这是我在第一个打印语句后得到的:

    +1.20”.
    +1.18Y
    +1.28Y.
    

    Tesseract 很难使用 % 字符,将其列入白名单似乎无济于事。所以我对 Y 进行了直接替换,第二个打印语句变为:

    +1.20%.
    +1.18%
    +1.28%.
    

    末尾仍有尾随句点,您必须将其删除。

    【讨论】:

    • 哦,伙计!!!非常感谢您!!这样可行!我以前试过这个面具,但看起来我在某些时候失败了。但现在我明白了:)谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多