Issue
I have a rather small set of images which contains dates. The size might be a problem, but I'd say that the quality is OK. I have followed the guidelines to provide the clearest image I can to the engine. After resizing, apply filters, lots of trial and error, etc. I came up with an image that is almost properly read. I put an example below:
Now, this is read as “9 MAR 2021\n\x0c
. Not bad, but the first 2
is read as "
. At this point I think I'm misusing part of the power of Tesseract. After all, I know what it should expect, i.e. something as "%d %b %Y"
.
Is there a way to tell Tesseract that it should try to find the best match given this strong constraint? Providing this metadata to the engine should heavily facilitate the task. I have been reading the documentation, but I can't find the way to do this.
I'm using pytesseract
on Tesseract 4.1. with Pytyon 3.9.
Solution
You need to know the followings:
Now if we center the image (by adding borders):
Second, we need to make the characters in the image bold to make the OCR result accurate.
Now OCR:
29 MAR 2021
Code:
import cv2
import pytesseract
# Load the image
img = cv2.imread("xsGBK.jpg")
# Center the image
img = cv2.copyMakeBorder(img, 50, 50, 50, 50, cv2.BORDER_CONSTANT, value=[0, 0, 0])
# Convert to the gray-scale
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Dilate
gry = cv2.dilate(gry, None, iterations=1)
# OCR
print(pytesseract.image_to_string(gry))
# Display
cv2.imshow("", gry)
cv2.waitKey(0)
Answered By - Ahx
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.