首先呢,祝大家冬至快乐,吃好喝好准备过新年~也感谢一年来大家的支持和鼓励。
回归正题。
前阵子用python弄个登录器,需要填写简单验证码的,想通过ocr的方式进行识别,所以搜索了一番,发现了个比较有用的库——ddddocr,戏称带带弟弟ocr。
文章标题还提到pip下载换源了,主要是因为一开始下载这个库的时候着实有点累人,要么下载太慢要么报错,但更换个下载源,很快就搞定了。因此,顺带写写下载源的问题。
国内下载源
pypi 清华大学源:https://pypi.tuna.tsinghua.edu.cn/simple
pypi 腾讯源:http://mirrors.cloud.tencent.com/pypi/simple
pypi 阿里源:https://mirrors.aliyun.com/pypi/simple/
pypi 豆瓣源:http://pypi.douban.com/simple/
以上是常用的4个国内下载源,我比较有文化,一般选清华大学的:)
pip下载换源
有了下载源,就要懂得怎么更换。一般换下载源的方法有更改配置文件和命令行的方式,我比较喜欢用命令行的,方法如下:
命令行临时修改
直接使用pip install packagename -i参数,指定下载源的URL。
pip install packagename -i https://mirrors.aliyun.com/pypi/simple/
2. 命令行永久修改
使用pip config set global.index-url来直接指定下载源的URL,这样就不用手动修改配置文件了。
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
ddddocr测试
懂了如何换源,现在就可以去下载ddddocr了。我使用清华源,命令行输入命令:
pip install ddddocr -i https://pypi.tuna.tsinghua.edu.cn/simple
等待下载完成就可以了。
我找了两个网站登录页面练练手,抓取了验证码的接口后,写了以下测试代码:
import ddddocr,time
from urllib import request
def test1():
url = "https://puser.zjzwfw.gov.cn/sso/usp.do?action=verifyimg&rd=0.14098955719708983"
for i in range(10):
f = open(f'./test1/{i}.jpg', 'wb+')
req = request.urlopen(url)
buf = req.read()
f.write(buf)
f.close()
with open(f'./test1/{i}.jpg','rb') as img:
img_bytes = img.read()
res = ocr.classification(img_bytes)
print(f"识别结果[{i}]:",res)
time.sleep(2)
def test2():#效果不好
url = "https://tysfrz.isdapp.shandong.gov.cn/api-gateway/common-captcha-server/interface/code/imageCode"
for i in range(10):
f = open(f'./test2/{i}.jpg', 'wb+')
req = request.urlopen(url)
buf = req.read()
f.write(buf)
f.close()
with open(f'./test2/{i}.jpg','rb') as img:
img_bytes = img.read()
res = ocr.classification(img_bytes)
print(f"识别结果[{i}]:",res)
time.sleep(2)
ocr = ddddocr.DdddOcr(show_ad=False)
test1()
test2()
总结
代码很简单,测试也很简单,过程我也不再贴图了。
总的来说,对于简单的验证码,识别准确率挺高,问题不大,如下面这种:
对于有干扰图形的验证码识别结果不怎么好,如下面这种。
如需要测试文件的可以回复:OCR
-END-
更多精彩文章
点击下方名片关注【偶尔敲代码】