Reputation: 59
Hello i am new to python and i am trying to get numbers from an image using pytesseract and cv2, whenever i run my code its always missing a number or works well, for instance, it will miss the number 8 in the below image, also please note the numbers in the input could be random or empty as its a date input but a full date length i expect is 8, any help on getting accuracy would be really helpful.
15082022 there are 8 digits.
Thanks.
Code
img1 = cv2.imread("image.tiff")
gry1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
(h, w) = gry1.shape[:2]
gry1 = cv2.resize(gry1, (w*3, h*3))
thr1 = cv2.threshold(gry1, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
thr1 = cv2.GaussianBlur(thr1, (3, 3), 0)
txt1 = pytesseract.image_to_string(
thr1, config=f"--oem 3 --psm 11 outputbase digits tessedit_char_whitelist=0123456789")
print("".join(t for t in txt1 if t.isalnum()))
cv2.imshow("thr1", thr1)
cv2.waitKey(0)
Output
1502022
Expected Output
15082022
Image example The image could contain text and might not, this depends on how the screenshot system did its work.
Upvotes: 0
Views: 401
Reputation: 3865
After playing around with tesseract and OpenCV I found several ways of how I could improve accuracy. However without proper benchmarking with a large dataset, I am not sure any answer would be helpful.
My approach was to:
cv2.erode()
with a suitable kernel size.I've also tried with several kernel sizes, and several scaling, and several iteration for the erode process, and there are many combinations that gives the wanted results.
If you find a way to remove the borders of the boxes the task would be easier
Upvotes: 1