Python中识别图片/滑块验证码准确率极高的ddddocr库详解
作者:上海-悠悠 时间:2021-10-05 05:20:17
前言
验证码的种类有很多,它是常用的一种反爬手段,包括:图片验证码,滑块验证码,等一些常见的验证码场景。
识别验证码的python 库有很多,用起来也并不简单,这里推荐一个简单实用的识别验证码的库 ddddocr (带带弟弟ocr)库.
环境准备
python 版本要求小于等于python3.9 版本
pip 安装
pip install ddddocr
下载的安装包比较大,一般用国内的下载源可以加快下载速度
pip install ddddocr -i https://pypi.douban.com/simple
github地址 https://github.com/sml2h3/ddddocr
快速开始
先随便找个纯英文的验证码,保持为a1.png
代码示例
import ddddocr # 导入 ddddocr
ocr = ddddocr.DdddOcr() # 实例化
with open('a1.png', 'rb') as f: # 打开图片
img_bytes = f.read() # 读取图片
res = ocr.classification(img_bytes) # 识别
print(res)
运行结果
已经能识别到 xnen ,但是会出现"欢迎使用ddddocr,本项目专注带动行业内卷…"提示语, 可以加一个参数show_ad=False
import ddddocr # 导入 ddddocr
ocr = ddddocr.DdddOcr(show_ad=False) # 实例化
with open('a1.png', 'rb') as f: # 打开图片
img_bytes = f.read() # 读取图片
res = ocr.classification(img_bytes) # 识别
print(res)
图片验证码
识别一下三种验证码
代码示例
import ddddocr # 导入 ddddocr
ocr = ddddocr.DdddOcr(show_ad=False) # 实例化
with open('a2.png', 'rb') as f: # 打开图片
img_bytes = f.read() # 读取图片
res2 = ocr.classification(img_bytes) # 识别
print(res2)
with open('a3.png', 'rb') as f: # 打开图片
img_bytes = f.read() # 读取图片
res3 = ocr.classification(img_bytes) # 识别
print(res3)
with open('a4.png', 'rb') as f: # 打开图片
img_bytes = f.read() # 读取图片
res4 = ocr.classification(img_bytes) # 识别
print(res4)
运行结果
giv6j
zppk
4Tskh
滑块验证码
滑块验证码场景如下场景示例
先抠出2张图片,分别为background.png 和 target.png
解决问题的重点是计算缺口的位置
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)
运行结果
{'target_y': 0, 'target': [184, 58, 246, 120]}
target 的四个值就是缺口位置的左上角和右下角的左边位置
识别中文
识别图片上的文字
import ddddocr
import cv2
det = ddddocr.DdddOcr(det=True)
with open("test.png", 'rb') as f:
image = f.read()
poses = det.detection(image)
im = cv2.imread("test.png")
for box in poses:
x1, y1, x2, y2 = box
im = cv2.rectangle(im, (x1, y1), (x2, y2), color=(0, 0, 255), thickness=2)
cv2.imwrite("result.jpg", im)
保存后的图片
来源:https://blog.csdn.net/qq_27371025/article/details/129147852
标签:Python,ddddocr,识别,验证码
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python实战之画哆啦A梦(超详细步骤)
2021-05-16 14:36:47
![](https://img.aspxhome.com/file/2023/0/80480_0s.jpg)
PyCharm在新窗口打开项目的方法
2023-06-14 23:00:29
![](https://img.aspxhome.com/file/2023/9/86219_0s.jpg)
ASP.NET(AJAX+JSON)实现对象调用
2023-07-19 12:29:33
谈谈FACEBOOK的一处产品细节
2008-03-11 11:05:00
解决缩小图标变样问题
2007-10-08 19:13:00
![](https://img.aspxhome.com/file/UploadPic/200710/8/2007108191659246s.png)
实例详解Matlab 与 Python 的区别
2021-07-20 22:14:15
解决pip install xxx报错SyntaxError: invalid syntax的问题
2023-02-13 23:53:07
![](https://img.aspxhome.com/file/2023/0/90760_0s.jpg)
Python爬虫爬取ts碎片视频+验证码登录功能
2023-03-23 01:39:23
![](https://img.aspxhome.com/file/2023/7/70207_0s.png)
python 统计list中各个元素出现的次数的几种方法
2022-12-09 10:04:01
Java正则表达式API边界匹配
2023-07-03 19:36:18
python自动提取文本中的时间(包含中文日期)
2023-08-22 21:32:11
黑客谈 MSSQL SA权限入侵的感悟
2008-03-20 10:18:00
Python实现删除重复文件的示例代码
2022-02-26 03:50:22
详解如何利用Python绘制迷宫小游戏
2021-02-28 05:31:32
![](https://img.aspxhome.com/file/2023/5/66885_0s.gif)
JavaScript 在各个浏览器中执行的耐性
2009-02-06 15:26:00
Docker构建python Flask+ nginx+uwsgi容器
2023-07-25 06:36:43
![](https://img.aspxhome.com/file/2023/5/61685_0s.png)
php将ppt转jpg图片的具体步骤代码
2023-06-12 21:53:33
用于统计项目中代码总行数的Python脚本分享
2022-05-11 17:37:11
Python运行DLL文件的方法
2021-12-25 15:23:21
简单的淡入淡出图片轮换效果
2009-05-22 18:38:00