使用ddddocr自动化测试验证码识别

原打算使用tesseract进行验证码识别的但后面发现实在太辣鸡了

不知道tesseract以及没安装的可以看这篇文章:
tesseract安装以及联调python

使用tesseract的代码:

import pytesseract
from PIL import Image, ImageEnhance
"""
步骤①:定位图片的元素,并且截取当前浏览器的页面图片
步骤②:获取验证码坐标点,以及验证码图片、浏览器、截图的长和宽
步骤③:截取截图里的验证码图片,获得的验证码图片并保存
步骤④:获得验证码code
"""
# imagePng = "../img/test.png"
# 原图路径
imagePng = "../img/test.png"
# 处理之后图片的路径
savePngPath = "../img/savePng.png"

# 原图转对象
resource_img = Image.open(imagePng)

# 转换模式:L | RGB
resource_img = resource_img.convert('L')

# 提高识别率(提高个毛)
enhancer = ImageEnhance.Color(resource_img)
enhancer = enhancer.enhance(0)
enhancer = ImageEnhance.Brightness(enhancer)
enhancer = enhancer.enhance(2)
enhancer = ImageEnhance.Contrast(enhancer)      # 增强对比度
enhancer = enhancer.enhance(8)
enhancer = ImageEnhance.Sharpness(enhancer)
resource_img = enhancer.enhance(20)

resource_img = ImageEnhance.Contrast(resource_img)  # 增强对比度
resource_img = resource_img.enhance(2.0)
resource_img.save(savePngPath)

# 识别图片
code = pytesseract.image_to_string(Image.open(savePngPath)).strip()
#code = pytesseract.image_to_string(Image.open('../img/xin.png')).strip()
print(f"提取的验证码为:【{code}】")







实际验证码图片
在这里插入图片描述

得到的效果
在这里插入图片描述
这还识别个毛啊

使用ddddocr进行识别

  • ddddocr安装
pip install ddddocr

代码:

import ddddocr

ocr = ddddocr.DdddOcr()
with open('../img/test.png', 'rb') as f:
	img_bytes = f.read()
res = ocr.classification(img_bytes)
print('识别出的验证码为:' + res)

运行结果

E:\code\venv\Scripts\python.exe E:\code\SeleniumTest\ddddocr验证码识别.py 
欢迎使用ddddocr,本项目专注带动行业内卷,个人博客:wenanzhe.com
训练数据支持来源于:http://146.56.204.113:19199/preview
爬虫框架feapder可快速一键接入,快速开启爬虫之旅:https://github.com/Boris-code/feapder
谷歌reCaptcha验证码 / hCaptcha验证码 / funCaptcha验证码商业级识别接口:https://yescaptcha.com/i/NSwk7i
识别出的验证码为:2177

Process finished with exit code 0

运行的时候可能会出现:
ddddocr找不到onnxruntime_pybind11_state模块的异常

...
    from .onnxruntime_pybind11_state import *  # noqa
ImportError: DLL load failed: 找不到指定的模块。

找对应的你电脑的对应位数直接下载就好了

Microsoft Visual C++ Redistributable 2019x86 官方下载地址:32位
Microsoft Visual C++ Redistributable 2019x64 官方下载地址:64位

参考解决链接

  • 8
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值