温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2023-02-28 10:30:43 来源:亿速云 阅读:345 作者:iii 栏目:开发技术

这篇“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)

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI