使用 PyTesseract 读取数字

Reading numbers using PyTesseract

我正在尝试从图像中读取数字,但找不到使它始终如一地工作的方法(并非所有图像都有数字)。这些是图像:

(这里是相册的 link 以防图片不工作)

这是我用来 运行 对图像进行正方体分析的命令:pytesseract.image_to_string(image, timeout=2, config='--psm 13 -- OEM 3 -c tessedit_char_whitelist=0123456789')。我尝试了多种配置,但这似乎效果最好。

就预处理而言,这个效果最好:

    gray = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2GRAY)
    gray = cv2.bilateralFilter(gray, 11, 17, 17)
    im_bw = cv2.threshold(gray, thresh, 255, cv2.THRESH_BINARY_INV)[1]

这适用于除第 3 个图像之外的所有图像。为了解决第三张图片中的线条问题,我尝试使用 cv2.Canny 和一个相当大的阈值来获取边缘,但是当将它们拉回来时,即使它得到每个数字的边缘超过 95%,tesseract 无法正确读取它们。

我也尝试过调整图像大小,使用 cv2.morphologyEx、模糊处理等。我找不到一种方法让它适用于每种情况。

谢谢。

cv2.resize 一直为我工作 INTER_CUBIC 插值。

将这最后一步添加到预处理很可能会解决您的问题。

im_bw_scaled = cv2.resize(im_bw, (0, 0), fx=4, fy=4, interpolation=cv2.INTER_CUBIC)

你可以玩玩秤。我在上面使用了'4'。

编辑:

以下代码可以很好地处理您的图像,甚至是特殊字符。请尝试使用您的其余数据集。缩放、OTSU 和侵蚀是最佳组合。

import cv2
import numpy
import pytesseract

pytesseract.pytesseract.tesseract_cmd = "<path to tesseract.exe>"

# Page segmentation mode, PSM was changed to 6 since each page is a single uniform text block.
custom_config = r'--psm 6 --oem 3 -c tessedit_char_whitelist=0123456789'

# load the image as grayscale
img = cv2.imread("5.png",cv2.IMREAD_GRAYSCALE)

# Change all pixels to black, if they aren't white already (since all characters were white)
img[img != 255] = 0

# Scale it 10x
scaled = cv2.resize(img, (0,0), fx=10, fy=10, interpolation = cv2.INTER_CUBIC)

# Retained your bilateral filter
filtered = cv2.bilateralFilter(scaled, 11, 17, 17)

# Thresholded OTSU method
thresh = cv2.threshold(filtered, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]

# Erode the image to bulk it up for tesseract
kernel = numpy.ones((5,5),numpy.uint8)
eroded = cv2.erode(thresh, kernel, iterations = 2)

pre_processed = eroded

# Feed the pre-processed image to tesseract and print the output.
ocr_text = pytesseract.image_to_string(pre_processed, config=custom_config)
if len(ocr_text) != 0:
    print(ocr_text)
else: print("No string detected")