首页 > 解决方案 > 如何使用 Tesseract 识别图像上两个数字之间的斜线?

问题描述

我有一些图像,其中两个数字/非常接近。Tesseract 根本无法识别该破折号,或者1在大多数情况下都可以识别它(对于少数图像它有效)。

输入 1

我的正方体代码:

pytesseract.image_to_string(img,lang='eng',config='--psm 7 --oem 3 -c tessedit_char_whitelist=/0123456789').strip()

我已经尝试过其他psmoem配置。我一直在玩图像很多,例如cv2.threshold调整cv2.cvtColor大小。

编辑:

img = cv2.threshold(img, 200, 255, cv2.THRESH_BINARY_INV)[1]` <br>
img = cv2.resize(img,(0,0), fx=1.5, fy=1.5)`

大多数图像返回良好的值,但其中一些5随机添加(转换后的图像):

输入 2

输入 3

很少有案例仍然无法识别斜线。

标签: pythonimage-processingocrtesseractpython-tesseract

解决方案


灰度,甚至更大,不同的阈值在我的机器上完成了这项工作:

import cv2
import pytesseract


def extract_stats(img_filepath):
    img = cv2.imread(img_filepath, cv2.IMREAD_GRAYSCALE)
    img = cv2.resize(img, (0, 0), None, 4.0, 4.0)
    img = cv2.threshold(img, 160, 255, cv2.THRESH_BINARY)[1]
    config = '--psm 6 -c tessedit_char_whitelist="0123456789/"'
    text = pytesseract.image_to_string(img, config=config)
    print(text.replace('\n', '').replace('\f', ''))


for filepath in ['Bzh3j.png', 't9gAh.png', 'BBy2P.png']:
    extract_stats(filepath)
# 4319/6149
# 943/7114
# 103/6149
----------------------------------------
System information
----------------------------------------
Platform:      Windows-10-10.0.19042-SP0
Python:        3.9.6
PyCharm:       2021.2
OpenCV:        4.5.3
pytesseract:   5.0.0-alpha.20201127
----------------------------------------

推荐阅读