Tesseract can't read all numbers accurately

I am using Tesseract to recognize numbers from screen images captured with a phone camera. I've done some image preprocessing: a processed image , and using Tesseract I can get some mixed results. Using the following code in the above images, I get the following output: "EOE". However, with this image processed image , I get an exact match: "39: 45.8"

import cv2
import pytesseract
from PIL import Image, ImageEnhance
from matplotlib import pyplot as plt

orig_name  = "time3.jpg";
image_name = "time3_.jpg";

img = cv2.imread(orig_name, 0)
img = cv2.medianBlur(img, 5)

img_th = cv2.adaptiveThreshold(img, 255,\
    cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY, 11, 2)

cv2.imshow('image', img_th)
cv2.waitKey(0)
cv2.imwrite(image_name, img_th)

im = Image.open(image_name)

time = pytesseract.image_to_string(im, config = "-psm 7")
print(time)

      

Is there anything I can do to get more consistent results?

+3


source to share


1 answer


I did three additional things to get the correct value for the first image.

  • You can whitelist Tesseract. In your case, we know that only charachters will be on this list . This greatly improves accuracy. 01234567890.:

  • I resized the image to make tesseract easier.

  • I switched from psm 7 to 11 mode (Recoginze as much as possible)

Code:



import cv2
import pytesseract
from PIL import Image, ImageEnhance

orig_name  = "./time1.jpg";
img = cv2.imread(orig_name)

height, width, channels = img.shape
imgResized = cv2.resize(img, ( width*3, height*3))
cv2.imshow("img",imgResized)
cv2.waitKey()
im = Image.fromarray(imgResized)
time = pytesseract.image_to_string(im, config ='--tessdata-dir "/home/rvq/github/tesseract/tessdata/" -c tessedit_char_whitelist=01234567890.: -psm 11 -oem 0')
print(time)

      


Note: You can use Image.fromarray(imgResized)

to convert opencv image to PIL image. You don't have to write to disk and read it again.

+1


source







All Articles