Created
December 22, 2024 11:13
-
-
Save rikkihamano/56d4403e206a022242059e89158dff7e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import matplotlib.pyplot as plt | |
import numpy as np | |
import easyocr | |
import requests | |
URL = 'https://authserver.nju.edu.cn/authserver/captcha.html' | |
webimg = requests.get(URL).content | |
# read image from web | |
nparr = np.frombuffer(webimg, np.uint8) | |
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR) | |
plt.imshow(img) | |
plt.show() | |
flt = cv2.pyrMeanShiftFiltering(img, sp=10, sr=60) | |
plt.imshow(flt) | |
plt.show() | |
gray = cv2.cvtColor(flt, cv2.COLOR_BGR2GRAY) | |
# show image in grayscale | |
plt.imshow(gray, cmap='gray') | |
plt.show() | |
ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU) | |
# show binary image | |
plt.imshow(binary, cmap='gray') | |
plt.show() | |
# reverse binary image to get text in black | |
binary = cv2.bitwise_not(binary) | |
# show reversed binary image | |
plt.imshow(binary, cmap='gray') | |
plt.show() | |
WHITELIST = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' | |
gray = cv2.cvtColor(binary, cv2.COLOR_GRAY2BGR) | |
reader = easyocr.Reader(['en'], detector=False) | |
result = reader.recognize(gray, detail=0, allowlist=WHITELIST) | |
print(result[0].upper()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment