当前位置 : 主页 > 编程语言 > python >

项目实战-RuoYi后台管理系统-用Python基于图像识别技术处理登录页面的验证码

来源:互联网 收集:自由互联 发布时间:2022-06-15
之前在群里咨询,做自动化的时候,接口怎么去处理验证码的,接下来介绍一下如何通过图像识别技术去实现。 这里要用到ddddocr的库,暂不支持python最新版本,因此,建议使用python3



之前在群里咨询,做自动化的时候,接口怎么去处理验证码的,接下来介绍一下如何通过图像识别技术去实现。

这里要用到ddddocr的库,暂不支持python最新版本,因此,建议使用python3.7去使用。

步骤一、调用获取验证码的接口,拿到返回的验证码的相关信息

def get_code():
res = requests.get('http://8.129.162.225:8080/captchaImage')
return res.json()["img"]

步骤二、将接口返回的验证码的信息保存为验证码的图片

def save_images(img_str):
imgdata = base64.b64decode(img_str)
file = open('验证码.jpg', 'wb')
file.write(imgdata)
file.close()

步骤三、基于图片识别,用dddocr库识别验证码

def ocr():
ocr = ddddocr.DdddOcr()
with open('验证码.jpg', 'rb') as f:
img_bytes = f.read()
res = ocr.classification(img_bytes)
print(res)

最终运行效果:

项目实战-RuoYi后台管理系统-用Python基于图像识别技术处理登录页面的验证码_python

扩展:

可以使用opencv将图片内容之间显示在页面,方便对比查看:

def show_code(img_str):
img_b64decode = base64.b64decode(img_str) # base64解码
img_array = numpy.fromstring(img_b64decode, numpy.uint8) # 转换numpy序列
img = cv2.imdecode(img_array, cv2.COLOR_BGR2RGB) # 转换Opencv格式
cv2.imshow("img", img)
cv2.waitKey()

效果如下:

项目实战-RuoYi后台管理系统-用Python基于图像识别技术处理登录页面的验证码_图像识别_02

最终整体代码如下,运行前记得先安装相关插件:

"""
pip install numpy
pip install opencv-python
pip install ddddocr
"""
import requests
import ddddocr
import base64
import numpy
import cv2




# 调接口获取验证码
def get_code():
res = requests.get('http://8.129.162.225:8080/captchaImage')
return res.json()["img"]




def save_images(img_str):
imgdata = base64.b64decode(img_str)
file = open('验证码.jpg', 'wb')
file.write(imgdata)
file.close()




def ocr():
ocr = ddddocr.DdddOcr()
with open('验证码.jpg', 'rb') as f:
img_bytes = f.read()
res = ocr.classification(img_bytes)
print(res)




def show_code(img_str):
img_b64decode = base64.b64decode(img_str) # base64解码
img_array = numpy.fromstring(img_b64decode, numpy.uint8) # 转换numpy序列
img = cv2.imdecode(img_array, cv2.COLOR_BGR2RGB) # 转换Opencv格式
cv2.imshow("img", img)
cv2.waitKey()




def run():
code = get_code()
save_images(code)
ocr()
show_code(code)




run()

当然,这个图片识别的具体准确率怎么样 ,在后续做自动化的过程中的话可能要做一个容错处理 ,不能完全依赖这个 。查redis获取验证码才是比较好的方案。



上一篇:时序预测之nbeats源码和训练代码
下一篇:没有了
网友评论