Issue
I've been experimenting with pytesseract and I have searched some improvements for accuracy but it didn't work for me. So here's my img:
This is the output:
Code:
img = cv2.imread("temp.png")
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thr = cv2.adaptiveThreshold(gry, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 4)
txt = pytesseract.image_to_string(thr, config='--psm 13')
print(txt) # "@)"
I don't know everything since I just started, can someone give me tips how can this be done?
Edit: Ahx
solved my question but there's something buggy in the code. It's reading 6 as é
. For example d6
, it will read it as dé
.
I added some thresholds and blurs because I think it will improve it but it didn't.
Here's my updated code:
img = cv2.imread('temp.png')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lwr = np.array([0, 0, 0])
upr = np.array([179, 255, 180])
msk = cv2.inRange(hsv, lwr, upr)
msk = cv2.resize(msk, (0, 0), fx=3, fy=3, interpolation=cv2.INTER_CUBIC)
msk = cv2.adaptiveThreshold(cv2.bilateralFilter(msk, 9, 75, 75), 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 2)
msk = cv2.adaptiveThreshold(cv2.medianBlur(msk, 3), 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 2)
txt = pytesseract.image_to_string(msk, lang='eng', config=r'--psm 6 --oem 3')
The e6
actually worked but 6
in others like d6
, Nf6
is always é
.
Here's an example if ever you wanna try it out:
Output:
result: Nf6é
Solution
You can easily get the result by performing color-segmentation. First, you need to load the image, convert it to the HSV format. Next, define the upper and lower boundaries to get the binary-mask. The binary mask will contain the required features for recognizing the characters. Then we will upsample the binary-mask and give input to the tesseract.
Code:
import cv2
import numpy as np
import pytesseract
# Load the image
img = cv2.imread("iTO9h.png")
# Convert to grayscale
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# Get binary-mask
lwr = np.array([0, 0, 0])
upr = np.array([179, 255, 180])
msk = cv2.inRange(hsv, lwr, upr)
# Up-sample
msk = cv2.resize(msk, (0, 0), fx=2, fy=2)
# OCR
txt = pytesseract.image_to_string(msk)
print(txt)
Answered By - Ahx
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.