首页 > 技术文章 > 绝了,dddd带带弟弟OCR识别验证码

wwho 2022-01-17 17:04 原文

前言

上一篇介绍了通过 python 的 pytesserract 模块进行识别验证码,但是他只能识别一些简单的验证码,比如像这种。

遇到稍微复杂一点的验证码,就会识别不了。


那咋办?

网上找了一圈,介绍了不同的第三方平台识别验证码,像百度 ocr 、打码兔、超级鹰等,其中百度 ocr 呼声最高。

链接: https://cloud.baidu.com/product/ocr_others/webimage

奈何去试了一下,上面 3T9Q 这个验证码都不能识别出来...

而且收费还不低。

算了,后面在 github 上找到一个开源库,而且还很强大。这个库叫 ddddocr ,
带带弟弟,
哲哥的博客地址:https://wenanzhe.com/

不单单可以学到技术,还可以学到不少的人生道理。左边技术,右边人生道理。

网友们对这个库高度评价,


ddddorc 安装使用

环境要求:

python <= 3.9
Windows/Linux/Macos..

以下是在windows上安装

更新 pip

python -m pip install --upgrade pip -i https://pypi.douban.com/simple

不更新 pip 有可能会安装失败。

安装 ddddocr

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


使用方法

# -*- coding:utf-8 -*-
import ddddocr                       # 导入 ddddocr
ocr = ddddocr.DdddOcr()              # 实例化
with open('002.png', 'rb') as f:     # 打开图片
    img_bytes = f.read()             # 读取图片
res = ocr.classification(img_bytes)  # 识别
print(res)

单个图片的识别:


多个图片识别:

# -*- coding:utf-8 -*-
import ddddocr                       # 导入 ddddocr
ocr = ddddocr.DdddOcr()
for i in range(1, 4):
    with open(str(i) + '.png', 'rb') as f:
        img_bytes = f.read()
    res = ocr.classification(img_bytes)
    print(res)



有些大小写还是不能识别出来。

封装一下:

# -*- coding:utf-8 -*-
import ddddocr
ocr = ddddocr.DdddOcr()

def ddocr(file):
    try:
        with open(file, 'rb') as f:
            img_bytes = f.read()
        res = ocr.classification(img_bytes)
        return res
    except:
        print("获取验证码失败,请继续!")

r = ddocr('3.png')
print(r)




结合抠图一起使用,即获取验证码图片,然后用dddr 识别验证码。

from selenium import webdriver
import time
from PIL import Image
import ddddocr
ocr = ddddocr.DdddOcr()


# 抠图
def matting():
    # 打开谷歌浏览器
    browser = webdriver.Chrome()
    # 打开网站首页
    # browser.get("https://v3pro.houjiemeishi.com/PC/pages/login/login.html")
    browser.get("http://192.168.139.129:8081/jpress/admin/login")
    # 网页最大化
    browser.maximize_window()
    # 登录页图片
    picture_name1 = 'login'+'.png'
    # 保存第一张截图
    browser.save_screenshot(picture_name1)
    # 定位元素
    ce = browser.find_element_by_id("captchaImg")
    # ce = browser.find_element_by_xpath('//*[@class="codeImg"]')
    # 打印元素位置、元素尺寸
    print(ce.location, ce.size)
    # 要抠验证码的图,先获取元素参数
    left = ce.location.get('x')
    top = ce.location.get('y')
    right = ce.size.get('width') + left
    height = ce.size.get('height') + top
    # 读取刚才截的第一张图
    im = Image.open(picture_name1)
    # 抠图
    img = im.crop((left, top, right, height))
    # 验证码块的图片
    picture_name2 = 'code'+'.png'
    # 保存图片
    img.save(picture_name2)
    time.sleep(5)
    browser.close()


# 通过 ddddocr 模块识别验证码
def ddocr(file):
    try:
        with open(file, 'rb') as f:
            img_bytes = f.read()
        res = ocr.classification(img_bytes)
        return res
    except:
        print("获取验证码失败,请继续!")


if __name__ == '__main__':
    print("抠图")
    matting()
    print("识别")
    code = ddocr('code.png')
    print(code)

分界线----------------------------------------------

运行过程中,有可能会遇到这个问题。

ddddocr模块的项目使用pyinstaller 打包后报错 ImportError: Microsoft Visual C++ Redistributable for Visual Studio 2019 not installed on the machine.


解决办法:
安装Microsoft Visual C++ Redistributable 2019

https://aka.ms/vs/16/release/VC_redist.x64.exe

直接点击就可以下载了,下载后直接安装即可。

推荐阅读