import requests
import base64
import os
from 清查工作.lib import info
# path = '身份证/张三,身份证.jpg'
def get_id_card_info(path):
print(path)
# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = f'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={info.AK}&client_secret={info.SK}'
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(path, '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()
# dic = {res['words_result']['姓名']['words']: res['words_result']['公民身份号码']['words']}
name = res['words_result']['姓名']['words']
id = res['words_result']['公民身份号码']['words']
# print(res)
with open('id_card_log.txt', 'a') as f:
# f = open('log.txt', 'a')
f.write(f'{name}-{id}!\n')
f.close()
print(f'{name}-{id}-信息提取成功!')
# 提取身份证照片目录
jpgs_ls = os.listdir('身份证/')
for jpg_name in jpgs_ls:
path = '身份证/' + jpg_name
get_id_card_info(path)
网友评论