python - ddddocr验证码识别

1. ddddocr安装

建议使用国内镜像安装

pip3 install ddddocr -i https://pypi.tuna.tsinghua.edu.cn/simple

2. 图片验证码

import ddddocr
ocr = ddddocr.DdddOcr(show_ad=False) # 关闭广告
with open('captcha.png', 'rb') as f:
    img_bytes = f.read()
res = ocr.classification(img_bytes)
print(res)

image
运行结果

hqymw

3. 滑块验证码

import ddddocr
det = ddddocr.DdddOcr(det=False,ocr=False,show_ad=False)
with open('target.png', 'rb') as f:
    target_bytes = f.read()
with open('background.png', 'rb') as f:
    background_bytes = f.read()
res = det.slide_match(target_bytes, background_bytes, simple_target=True)
print(res)

image
image
运行结果

{'target_y': 0, 'target': [130, 0, 193, 155]}

4. 中文识别

需要额外安装一个库

pip3 install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple
import ddddocr
import cv2

# 截图
img1 = cv2.imread('img1.jpg')
height = len(img1)
width = len(img1[0])
img2 = img1[0:min(width,height),0:min(width,height)]
cv2.imwrite('img2.jpg',img2)

det = ddddocr.DdddOcr(det=True,show_ad=False)
with open('img2.jpg', 'rb') as f:
    img2_bytes = f.read()
poses = det.detection(img2_bytes)
img2 = cv2.imread('img2.jpg')

for box in poses:
    x1, y1, x2, y2 = box
    img2 = cv2.rectangle(img2, (x1, y1), (x2, y2), color=(0,0,255), thickness=2)

cv2.imwrite('result.jpg',img2)

运行结果
image
image
image

posted @ 2023-03-13 21:12  wstong  阅读(2195)  评论(0编辑  收藏  举报