所以我目前正在开发一个项目,我使用 pyautogui 和 pytesseract 在我正在使用的视频游戏模拟器中截取时间的屏幕截图,然后尝试读取图像并确定我得到的时间。这是当我使用 pyautogui 获取我想要的区域的屏幕截图时图像的样子:
只需使用 pytesseract.image_to_string()
当我测试它以确保它安装正确时,使用文本图像,但是当我使用游戏计时器图片时,它不会输出任何内容。这是否与图像质量或 pytesseract 的某些模仿有关?
最佳答案
在使用 Pytesseract 执行 OCR 之前,您需要对图像进行预处理。这是使用 OpenCV 和 Pytesseract OCR 的简单方法。这个想法是获得处理后的图像,其中要提取的文本为黑色,背景为白色。为此,我们可以转换为 grayscale ,稍微涂抹Gaussian blur ,然后 Otsu's threshold以获得二值图像。我们使用 --psm 6
配置选项执行文本提取,以假定单个统一的文本 block 。看看here了解更多选项。
输入图片
Otsu 获取二值图像的阈值
Pytesseract OCR 结果
0’ 12”92
代码
import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
# Grayscale, Gaussian blur, Otsu's threshold
image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Perform text extraction
data = pytesseract.image_to_string(thresh, lang='eng', config='--psm 6')
print(data)
cv2.imshow('thresh', thresh)
cv2.waitKey()
关于python - Pytesseract image_to_data 无法读取我的图像中的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72194510/