easyocr+opencv识别输入式验证码

easyocr+opencv识别输入式验证码

import easyocr
import cv2

reader = easyocr.Reader(['ch_sim', 'en'])
pan = cv2.imread(r'C:\Users\yun\Desktop\h.jpg')
# img=cv2.imshow('sd',img)

img = cv2.cvtColor(pan, cv2.COLOR_BGR2GRAY)

# th2 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 3, 3)
ret, thresh1 = cv2.threshold(img, 15, 255, cv2.THRESH_BINARY_INV)

ret2, thresh2 = cv2.threshold(img, 30, 255, cv2.THRESH_BINARY)

# cv2.imshow('th', th2)
# cv2.waitKey(0)
cv2.imshow('ret', thresh1)
cv2.waitKey(0)

img = cv2.imshow('sd', thresh2)
cv2.waitKey(0)

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 4))  # 块操作
dilated = cv2.dilate(thresh1, kernel)  # 膨胀
# eroded = cv2.erode(img,kernel) #腐蚀
# 显示膨胀后的图像
cv2.imshow("Dilated Image", dilated)
cv2.waitKey(0)
thresh1 = reader.readtext(thresh1)
thresh2 = reader.readtext(thresh2)
dilated = reader.readtext(dilated)
# print(result)

for i in thresh1:
    for j in i:
        print(j)

for i in thresh2:  # 成功
    for j in i:
        print(j)

for i in dilated:  # thresh1膨胀后也行
    for j in i:
        print(j)

你可能感兴趣的:(笔记)