Auth认证 http://ai.baidu.com/docs#/Auth/top
def fetch_access_token(api_key,secret_key):
api_key = "mAhDC1Z40ssn9rM0CcxiLOD9"
secret_key = "NLb94Zl4p2XKEjKkEaf1TSEygLmHSjyC"
url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id="+api_key+"&client_secret="+secret_key
request = urllib2.Request(url)
request.add_header('Content-Type', 'application/x-www-form-urlencoded')
response = urllib2.urlopen(request)
content = response.read()
if (content):
print(content)
return json.loads(content);
身份识别
def idcard_info(token_info):
token_info = fetch_access_token();
#print(token_info["access_token"])
access_token = '24.54a8827be3c6a5be65ed3d10cec81ad1.2592000.1538024838.282335-11737769'
access_token = token_info["access_token"]
url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=' + access_token
# 二进制方式打开图文件
f = open(r'/Users/sk/Desktop/WechatIMG2.jpeg', 'rb')
# 参数image:图像base64编码
img = base64.b64encode(f.read())
params = {"image": img, "id_card_side": "front"}
params = urllib.urlencode(params)
request = urllib2.Request(url, params)
request.add_header('Content-Type', 'application/x-www-form-urlencoded')
response = urllib2.urlopen(request)
content = response.read()
if (content):
print(content)
# print(json.dumps(content, sort_keys=True, indent=4, separators=(',', ': '))
最终调用
api_key
client_id = "mAhDC1Z40ssn9rM0CcxiLOD9"
secret_key
client_secret = "NLb94Zl4p2XKEjKkEaf1TSEygLmHSjyC"
token_info = fetch_access_token(client_id, client_secret);
card_info = idcard_info(token_info)
网友评论