使用 pytesseract 难以阅读文本
Difficulty reading text with pytesseract
我需要读取热像图上的最高温度,如下图:
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, and 85.2 here).
我的代码需要从这些图像中找到始终检测此温度并生成一个从最高到最低指示的列表。
在这些图像的情况下,您对我有什么建议可以提高 pytesseract 的自信度?
注意 1:当我分析整个图像(没有裁剪)时,returns 数据甚至不存在。
注2:在某些图像中,即使是二进制数,pytesseract (image_to_string) 也不会return任何数据。
谢谢大家,抱歉打字错误,用英文写作对我来说仍然是一个挑战。
因为你有相同的图像,你可以裁剪你想要的区域,然后在那里进行处理。处理也很简单。更改为灰色,获取阈值,反转,调整大小,然后进行 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()
我需要读取热像图上的最高温度,如下图:
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, and 85.2 here).
我的代码需要从这些图像中找到始终检测此温度并生成一个从最高到最低指示的列表。
在这些图像的情况下,您对我有什么建议可以提高 pytesseract 的自信度?
注意 1:当我分析整个图像(没有裁剪)时,returns 数据甚至不存在。
注2:在某些图像中,即使是二进制数,pytesseract (image_to_string) 也不会return任何数据。
谢谢大家,抱歉打字错误,用英文写作对我来说仍然是一个挑战。
因为你有相同的图像,你可以裁剪你想要的区域,然后在那里进行处理。处理也很简单。更改为灰色,获取阈值,反转,调整大小,然后进行 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()