百度API之身份证识别
今天的任务是调用百度的API,实现身份证识别,然后我糟了,感觉有点难,开启百度模式,百度了半天,站到了别人写的代码,复制then粘贴→运行,成功了,可是不是自己的,于是在老师的指导下,阅读了百度给出的官方文档,决定自己写一下。
步骤如下:
浏览器搜索百度AI智能开放平台,进入官网注册登录
2.找到如下图,点击身份证识别
3.点击立即使用
4.创建应用
5.填写信息。点击立即创建
6.返回应用列表
7.创建成功,注意图中标出的Appid, API Key ,secret Key
8.下面进入代码部分
环境依赖 requests,base65 库
下载requests库
pip install requests
推荐使用清华源,下载速度快
pip install requests -i https://pypi.tuna.tsinghua.edu.cn/simple
使用python代码如下,其他语言请参考https://ai.baidu.com/ai-doc/OCR/rk3h7xzck
import requests import base64 \'\'\' 身份证识别 \'\'\' request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard" # 二进制方式打开图片文件 f = open(\'[本地文件]\', \'rb\') img = base64.b64encode(f.read()) params = {"id_card_side":"front","image":img} access_token = \'[调用鉴权接口获取的token]\' request_url = request_url + "?access_token=" + access_token headers = {\'content-type\': \'application/x-www-form-urlencoded\'} response = requests.post(request_url, data=params, headers=headers) if response: print (response.json())
注意文件路径f = open(\'[本地文件]\', \'rb\')
params = {"id_card_side":"front","image":img} id_card_side的参数front为身份证正面,背面为back
获取Access Token
代码中三个参数
-
grant_type: 必须参数,固定为
client_credentials
; -
client_id: 必须参数,应用的
API Key
; -
client_secret: 必须参数,应用的
Secret Key
;
import requests # client_id 为官网获取的AK, client_secret 为官网获取的SK host = \'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【官网获取的AK】&client_secret=【官网获取的SK】\' response = requests.get(host) if response: print(response.json())
例如
import requests import base64 host = \'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=B5zZqwl2cO3b4lcWlPKoUuRO&client_secret=v4ZDzz8uT1pFaZkbOGLj5hSYupV7njKR\' response = requests.get(host) if response: taken = response.json()
代码运行结果为
{ "refresh_token": "25.b55fe1d287227ca97aab219bb249b8ab.315360000.1798284651.282335-8574074", "expires_in": 2592000, "scope": "public wise_adapt", "session_key": "9mzdDZXu3dENdFZQurfg0Vz8slgSgvvOAUebNFzyzcpQ5EnbxbF+hfG9DQkpUVQdh4p6HbQcAiz5RmuBAja1JJGgIdJI", "access_token": "24.6c5e1ff107f0e8bcef8c46d3424a0e78.2592000.1485516651.282335-8574074", "session_secret": "dfac94a3489fe9fca7c3221cbf7525ff" }
获取access_token:taken[\'access_token\']
最后,展示完整代码
import requests import base64 # client_id 为官网获取的AK, client_secret 为官网获取的SK host = \'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=B5zZqwl2cO3b4lcWlPKoUuRO&client_secret=v4ZDzz8uT1pFaZkbOGLj5hSYupV7njKR\' response = requests.get(host) if response: taken = response.json() print(taken) print(taken[\'access_token\']) \'\'\' 身份证识别 \'\'\' request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard" f = open(\'E:\plus班上课\模拟教务系统\db\card\张笑辉-正面.png\', \'rb\') img = base64.b64encode(f.read()) params = {"id_card_side": "front", "image": img} access_token = taken[\'access_token\'] request_url = request_url + "?access_token=" + access_token headers = {\'content-type\': \'application/x-www-form-urlencoded\'} response = requests.post(request_url, data=params, headers=headers) if response: res = response.json() print(res)