如何使用 tesseract 而不是仅使用第一个数字来读取完整的数字序列

How can I read a full sequence of digits using tesseract instead of first digit only

我有如下清晰的二进制图像,我想使用tesseract读取数字。

我的问题tesseract只读取第一个数字(5)!

如何让 tesseract 读取完整序列?

import cv2
import pytesseract

gray = cv2.imread('input.jpg', 0)

text = pytesseract.image_to_string(gray, config='outputbase digits')

print(text)

Output: 5 < o x o c >

在将图像直接推送到 pytesseract 以提取文本之前,您必须进行一些预处理。我想到的一件事是使用 binary_fill_holes 填充边缘内的区域。这是您可以执行的操作的示例。

from skimage import io, util, feature
from scipy import ndimage as ndi
import matplotlib.pyplot as plt
import pytesseract
import numpy as np

#Import image
img = io.imread('jbAsM.jpg', as_gray=True)

#Preprocessing
imginv = util.invert(img)  #Invert image

#Loop and fill holes iteratively
for i in range(2):
    edges = feature.canny(imginv) #find edges
    imginv = ndi.binary_fill_holes(edges) #fill holes in edges

fill_inv = util.invert(imginv)  #invert again
plt.imshow(fill_inv, cmap='gray') 

#Image to text
text = pytesseract.image_to_string(fill_inv, config='outputbase digits')
print('Extracted Text ->',text)

Extracted Text -> 5113

编辑: 不知道为什么 pytesseract 将最后一位数字预测为 3(很奇怪!!)

您必须找到适合其他图像的预处理管道。我建议查看 image segmentationedge filling methods.