简体   繁体   中英

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

I have the following clear binary image, and I want to read the digits using tesseract . 图片

My problem is that tesseract is reading only the first digit ( 5 )!

How can I make tesseract read the full sequence?

import cv2
import pytesseract

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

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

print(text)

Output: 5 < oxo c >

You will have to do some amount of preprocessing before you push the image directly to pytesseract for extraction of text. One thing that comes to mind is using binary_fill_holes to fill the area inside edges. Here is an example of what you can do.

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

EDIT: No idea why pytesseract is predicting the last digit as 3 (weird!!)

You will have to find your own preprocessing pipeline that suits the other images. I would recommend looking at image segmentation and edge filling methods .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM