【问题标题】:Difficulty reading text with pytesseractpytesseract 难以阅读文本
【发布时间】:2020-04-28 13:23:07
【问题描述】:

我需要读取热像图上的最高温度,如下图:

IR_1544_INFRA.jpg

IR_1546_INFRA.jpg

IR_1560_INFRA.jpg

IR_1564_INFRA.jpg

我使用了以下代码,这是最好的结果。 我还尝试了其他几种方法,例如:模糊、灰度、二值化等,但都失败了。

import cv2
import pytesseract

pytesseract.pytesseract.tesseract_cmd = r"C:\Users\User\AppData\Local\Tesseract-OCR\tesseract.exe"

# Load image, grayscale, Otsu's threshold
entrada = cv2.imread('IR_1546_INFRA.jpg')

image = entrada[40:65, 277:319]

#image = cv2.imread('IR_1546_INFRA.jpg')
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)
data = pytesseract.image_to_string(thresh, lang='eng', config='--psm 6')
print(data)

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

在第一张图片中,我发现 this

在第二张图片中,我找到了this

imagem 的布局总是一样的,也就是温度总是在同一个地方,所以我裁剪了图片,只隔离了数字。我想要(97.7 here 和 85.2 here)。

我的代码需要从这些图像中查找以始终检测此温度并生成一个从最高到最低指示的列表。

在这些图像的情况下,你对我有什么建议可以提高 pytesseract 的自信度?

注意 1:当我分析整个图像(不裁剪)时,它返回的数据甚至不存在。

注2:有些图片即使是二进制数,pytesseract(image_to_string)也不返回任何数据。

谢谢大家,对错别字深表歉意,用英文写作对我来说仍然是一个挑战。

【问题讨论】:

    标签: python-3.x text ocr python-tesseract string-decoding


    【解决方案1】:

    因为你有相同的图像,你可以裁剪你想要的区域,然后在那里进行处理。处理也很简单。更改为灰色,获取阈值,反转,调整大小,然后进行 OCR。您可以在下面的代码中看到它。它适用于您所有附加的图像。

    import cv2
    import pytesseract
    import os
    
    image_path = "temperature"
    
    for nama_file in sorted(os.listdir(image_path)):
        print(nama_file)
    
        img = cv2.imread(os.path.join(image_path, nama_file))
        crop = img[43:62, 278:319]
        gray = cv2.cvtColor(crop, cv2.COLOR_BGR2GRAY)
        thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)[1]
        thresh = cv2.bitwise_not(thresh)
        double = cv2.resize(thresh, None, fx=2, fy=2)
    
        custom_config = r'-l eng --oem 3 --psm 7 -c tessedit_char_whitelist="1234567890." '
        text = pytesseract.image_to_string(double, config=custom_config)
        print("detected: " + text)
    
        cv2.imshow("img", img)
        cv2.imshow("double", double)
    
        cv2.waitKey(0)
    
    cv2.destroyAllWindows()
    

    【讨论】:

    • 谢谢!仅在一个图像中不成功,因为该算法无法获得小数点,但在所有其他图像中都有效。你能指出我可以在图像处理方面学习的地方吗?
    • 您可以在这里找到大量信息pyimagesearch.com
    • OpenCV 有 fantastic tutorials 用于 Python。我强烈推荐。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-24
    • 1970-01-01
    相关资源
    最近更新 更多