0
votes

I'm trying to solve captchas by OCR them with Tesseract on C#. I'm grabbing the captcha with Selenium and I apply some effects to clean the image and I end up with this that is not clean enough to read it properly.

I'm asking here if someone could help me writing an OpenCV code that could remove the lines and dots as they have different width. Thanks!

Image: https://i.imgur.com/Md5XC6G.png

Image after OpenCv changes: https://i.imgur.com/pxcxPfQ.png

var image = Resize(img, 800, 600);
            image.Save("captcha.png", System.Drawing.Imaging.ImageFormat.Png);
            Mat imageIn = Cv2.ImRead("captcha.png", ImreadModes.Grayscale);
            Mat edges = new Mat();

            Cv2.Canny(imageIn, edges, 100, 100);

            edges.SaveImage("captcha1.png");

Anyway to improve it a bit more? Tesseract doesn't recognize it yet.

1

1 Answers

1
votes

I do not know how to do it in C#, but in python, this code works

import cv2
import numpy as np
import pytesseract

img = cv2.imread("Md5XC6G.png", cv2.IMREAD_GRAYSCALE)
cv2.imshow("original", img)

img = cv2.bitwise_not(img)

kernel = np.ones((14, 14), np.uint8)
close = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
newkernel = np.ones((5, 5), np.uint8)
inv = cv2.erode(close, newkernel, iterations=1)

inv = cv2.bitwise_not(inv)

cv2.imshow("inverted", inv)
cv2.imwrite("inverted.jpg", inv)

custom_config = r'-l eng --oem 3 --psm 7 -c tessedit_char_whitelist="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"'
text = pytesseract.image_to_string(inv, config=custom_config)
print("Detected captcha is:", text)

cv2.waitKey(0)
cv2.destroyAllWindows()

The result when run

Detected captcha is: wjr4s2