使用的百度开放api .
import requests
import base64
import json
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard"
# 二进制方式打开图片文件
f = open('本地图片地址', 'rb')
image = base64.b64encode(f.read())
params = {"id_card_side": "front", "image": image}
access_token = '改成你自己的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:
name = json.loads(response.text)['words_result']['姓名']['words']
address = json.loads(response.text)['words_result']['住址']['words']
birth = json.loads(response.text)['words_result']['出生']['words']
id = json.loads(response.text)['words_result']['公民身份号码']['words']
sex = json.loads(response.text)['words_result']['性别']['words']
minzu = json.loads(response.text)['words_result']['民族']['words']
print('正在识别中')
print('姓名:', name, '性别:', sex, '住址:', address, '身份证号:', id, '出生年月:', birth, '民族:', minzu)
if response:
print('已经识别成功')
然后是access_token的获取
import requests
import json
url = "https://aip.baidubce.com/oauth/2.0/token"
data = {
'grant_type': 'client_credentials',
'client_id': ''改成你自己的id',
'client_secret': '改成你自己的secret',
}
response = requests.post(url=url, data=data)
data2 = json.loads(response.text)
accesstoken = data2['access_token']
api在百度智能云-产品-人工智能-图像识别就可以申请
然后创建应用就行了