实现效果:
步骤一:接入接口
进入上述网站申请账号,然后运行相关代码,获取 access_token 即算完成(由于百度json每30天更新一次,故代码中进行日期更新了的,如何获取accss_token也可见代码)
步骤二:功能介绍:用户输入的图片路径可为网络上的url,也可为本机上的地址,为图省事,图片名称为 ValidateCode.jpg ,由于本人接入的的百度AI接口的手写文字识别,所以一般的验证码应该都可以通过,如果想加入其它功能,那么返回json数据就会有所改变,具体可以见API接口,本人是为了简化理解百度文档介绍
1 request.urlretrieve(imagepath, 'ValidateCode.jpg') # 下载图片
更新access_json:因为百度API规定:30天更新一次,所以我就把时间提前了。(别乱搞我的密钥呀,我也是为了分享呀QAQ)
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"> 2 def accesjson():
3 flag = 0
4 fromtime = 1546061002 #起始时间
5 nowtime = int(time.time())
6
7 #2592000恰好为30天,故提前
8 if nowtime - fromtime > 2000000:
9 flag = 1
10 gcontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
11 # client_id 为官网获取的AK, client_secret 为官网获取的SK
12 host = 'https://aip.baidubce.com/oauth/2.0/token?grant_'
13 'type=client_credentials&client_id=Ooj730ZD0Rm7E1dmcPwoZX9s&client_secret=dr5T1icZGqK8ZFyTr4wi2AWbtNKMIsNd'
14 req = request.Request(host)
15 response = request.urlopen(req, context=gcontext).read().decode('UTF-8')
16 result = json.loads(response)
17 if flag == 1:
18 return result
19 else:
20 return None
</pre>
图片正式识别:注意,接入功能不一样,放回json数据不一样,具体看返回json就明白了
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"> 1 #返回图片验证码
2 def vercode():
3 f = open('ValidateCode.jpg', 'rb')
4 img = base64.b64encode(f.read())
5 #不同百度API接口不一样,传递参数不一样,返回json也不一样
6 host = 'https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting'
7 headers = {
8 'Content-Type': 'application/x-www-form-urlencoded'
9 }
10 #更换json
11 if accesjson() == None:
12 access_token = '24.18591b2e4c97956e0f830db9f66e5373.2592000.1548646630.282335-15301065'
13 else:
14 access_token = accesjson()
15 print('已更换最新json,欢迎继续使用!')
16 host = host + '?access_token=' + access_token
17
18 data = {}
19 data['access_token'] = access_token
20 data['image'] = img
21 res = requests.post(url=host, headers=headers, data=data)
22 req = res.json()
23 return req['words_result'][0]['words']
</pre>
完整代码:目前可实现的功能就是网络上面的文字图片识别,或本机图片识别(和之前的抖音图片加载类似。)
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">百度AI接口(手写文字识别):https://ai.baidu.com/docs#/OCR-API/9ef46660</pre>
网友评论