问题
由于最近想搞深度学习,和AI这块,计算机算力不够。而且最近经常会碰到一些要用很棘手的算法求解复杂模型的问题。
由于我只对问题的结果有兴趣,而对具体的实现过程不在意,我注意到百度智能云有许多的应用程序的接口API产品,而且很便宜(有很多是免费的)这极大的吸引了我的注意。
下面将展示如何借助百度智能云提取身份证信息的过程。
代码部分
# encoding:utf-8
# 利用百度云智能的资源进行身份证的信息提取
import requests
import base64
def Get_acess_token(API_Key,Secret_Key):#获取acess_token
host = r'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + API_Key + '&client_secret=' + Secret_Key
response = requests.get(host)
if response:
text = response.json()
print('access_token:' + text['access_token'])
print('Access Token的有效期(秒为单位,有效期30天):' + str(text['expires_in']))
return text['access_token']
else:
print(r'检查到鉴权认证失败,请参见文档:https://ai.baidu.com/ai-doc/REFERENCE/Ck3dwjhhu')
def Get_ID_Cardinformation(IDcardAdress,acess_token):#获取身份证信息
#IDcardAdress:图片地址
#acess_token:acess_token号码
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard"
#IDcardAdress = r'C:\Users\pc\Desktop\my-IDCard.jpg'
# 二进制方式打开图片文件
f = open(IDcardAdress,'rb')
img = base64.b64encode(f.read())
params = {'image':img,'id_card_side':'front'}
#acess_token = '24.45d8d63a239c9ec7bc3d74468e935f69.2592000.1632471652.282335-24751172'
headers = {'Content-Type':'application/x-www-form-urlencoded'}
request_url = request_url + '?access_token='+acess_token
response = requests.post(request_url,data=params,headers = headers)
if response:
text = response.json()
result = text['words_result']
print('姓名:'+result['姓名']['words'])
print('民族:'+result['民族']['words'])
print('性别:' + result['性别']['words'])
print('出生年月:' + result['出生']['words'][0:4]+'年'+result['出生']['words'][4:6]+'月'+result['出生']['words'][6:]+'日')
print('住址:'+result['住址']['words'])
print('身份证号:'+result['公民身份号码']['words'])
else:
print('请检查浏览器网络情况!')
if __name__ == '__main__':
# 以下是基本信息
# IDcardAdress = r'C:\Users\pc\Desktop\my-IDCard.jpg'
API_Key = input('请输入百度云智能的API_Key:\n')
Secret_Key = input('请输入百度云智能的Secret_Key:\n')
IDcardAdress = input('身份证的正面照片的完整路径:\n')
try:
access_token = Get_acess_token(API_Key, Secret_Key)#对access_token的获取
Get_ID_Cardinformation(IDcardAdress, access_token) #提取身份证信息
except:
print('错误:没有注册百度云智能及获得access_token!')
结果:
可见识别准确率还是很高的!