Issue
I am trying to identify the numbers representing the time that are in the left corner at the bottom of the following image.
Specifically this is the piece of image that I need to recognize:
The digits are in a seven bars format.
I am using opencv and tesseract, but I am not obtaining good results with any of the filters that I am applying.
This is the code developed (I am assuming that the section where the numbers are, is already identified, so that's my starting point):
image = cv2.imread("C:\example2.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(blurred, 50, 200, 255)
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
text = pytesseract.image_to_string(edged)
Have tried with another set of filters but none of them gives me a result that is near to the image.
Thanks in advance.
Solution
Tesseract likes black text on white background. Also, it likes characters to be a minimum height.
I passed cv2.THRESH_OTSU
to do Otsu thresholding and cv2.THRESH_BINARY_INV
to invert image (change black to white and white to black). At the original size, the "9" was recognized as "q". I resized by factor of 2, and recognition was better.
import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
img = cv2.imread('example2.jpg', cv2.IMREAD_GRAYSCALE)
thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)[1]
thresh = cv2.resize(thresh, (0,0), fx=2, fy=2) # scale image 2X
detected_text = pytesseract.image_to_string(thresh, config = '--psm 6')
print(detected_text)
which gives
9:47
Answered By - bfris
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.