Issue
I am trying to solve basic captchas that have a little bit of noise but it is proving to be difficult.
This is a sample image of one of the captchas:
This is the code I am using:
import cv2
from pytesseract import image_to_string
img = cv2.imread("sample.png")
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
(h, w) = gry.shape[:2]
gry = cv2.resize(gry, (w*2, h*2))
cls = cv2.morphologyEx(gry, cv2.MORPH_CLOSE, None)
thr = cv2.threshold(cls, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
txt = image_to_string(thr)
print(txt)
The output that I get using this code is: "_JHB9TPR
This is obviously not correct. I think more work needs to be done with making the image clearer so that the letters stick out, but it doesn't help that the letters are the same colours as some of the background noise so that leads it to incorrectly recognise some letters.
Is there any other technique (with sample code) that I should be doing?
Solution
For your specific sample:
img = cv2.imread("./sample.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), sigmaX=1, sigmaY=1)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2,2))
morph = cv2.morphologyEx(blur, cv2.MORPH_CLOSE, kernel)
txt = image_to_string(morph)
print(txt)
Outputs:
JH69TPR
Which performs OCR on this image;
Edit; as Christoph Rackwitz suggested in the comments, I removed thresholding.
Answered By - doneforaiur
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.