微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python中怎么使用ddddocr库识别图片与滑块验证码

@H_502_0@这篇“Python中怎么使用ddddocr库识别图片与滑块验证码”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Python中怎么使用ddddocr库识别图片与滑块验证码”文章吧。

环境准备

python 版本要求小于等于python3.9 版本

pip 安装

pip install ddddocr

下载的安装包比较大,一般用国内的下载源可以加快下载速度

pip install ddddocr -i https://pypi.douban.com/simple

快速开始

先随便找个纯英文的验证码,保持为a1.png

Python中怎么使用ddddocr库识别图片与滑块验证码

代码示例

import ddddocr                       # 导入 ddddocr
ocr = ddddocr.ddddOcr()              # 实例化
with open('a1.png', 'rb') as f:     # 打开图片
    img_bytes = f.read()             # 读取图片
res = ocr.classification(img_bytes)  # 识别
print(res)

运行结果

Python中怎么使用ddddocr库识别图片与滑块验证码

已经能识别到 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)

图片验证

识别一下三种验证码

Python中怎么使用ddddocr库识别图片与滑块验证码

Python中怎么使用ddddocr库识别图片与滑块验证码

Python中怎么使用ddddocr库识别图片与滑块验证码

代码示例

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

滑块验证码

滑块验证码场景如下场景示例

Python中怎么使用ddddocr库识别图片与滑块验证码

先抠出2张图片,分别为background.png 和 target.png

Python中怎么使用ddddocr库识别图片与滑块验证码

Python中怎么使用ddddocr库识别图片与滑块验证码

解决问题的重点是计算缺口的位置

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)
@H_502_0@以上就是关于“Python中怎么使用ddddocr库识别图片与滑块验证码”这篇文章内容,相信大家都有了一定的了解,希望小编分享内容对大家有帮助,若想了解更多相关的知识内容,请关注编程之家行业资讯频道。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐