使用百度ai识别身份证信息

Python :3.5

首先在百度AI中注册,并开通身份证识别应用。
https://ai.baidu.com/tech/ocr_cards/idcard

安装Python SDK:pip install baidu-ai

from aip import AipOcr

appId = '你的 App ID'
appkey = '你的 Api Key'
secretkey = '你的 Secret Key'

client = AipOcr(appId, appkey, secretkey)
#在上面代码中,常量appId 在百度智能云控制台中创建,常量appkey 与secretkey 是在创建完毕应用后,
#系统分配给用户的,均为字符串,用于标识用户,为访问做签名验证,可在AI服务控制台中的应用列表中查看。

'''读取图片'''
def get_file_content(filePath):
    with open(filePath,'rb') as fp:
        return fp.read()

image = get_file_content('334.png') #替换为自己的图片 
idCardSide = 'front'    # front 反面,  back 正面

'''调用身份证识别'''
# a = client.idcard(image, idCardSide)

'''如果有可选参数'''
options = {
     }
options['detect_direction'] = 'true'      # 是否检测图像朝向
options['detect_risk'] = 'true'            # 是否开启身份证风险类型

'''带参数调用身份证识别'''
result = client.idcard(image, idCardSide, options)
# print(result)

directions = {
     0:'正向',1:'逆时针90度',2:'逆时针180度',3:'逆时针270度'}

imageStatus = {
     'normal':'识别正常','reversed_side':'未摆正身份证',
				'non_idcard':'上传的图片中不包含身份证',
                'blurred':'身份证模糊','over_exposure':'身份证关键字段反光或过曝',
               	'unknown':'未知状态'}

riskType = {
     'normal':'正常身份证','copy':'复印件','temporary':'临时身份证',
			'screen':'翻拍','unknow':'其他未知情况'}

image_status = result['image_status']
print('身份证识别状态'+':'+imageStatus[image_status])


if options['detect_direction'] == 'true':
    detect_direction = result['direction']
    print('身份证朝向'+':'+directions[detect_direction])
if options['detect_risk'] == 'true':
    risk_type = result['risk_type']
    print('身份证类型'+':'+riskType[risk_type])

words_result = result['words_result']
for wordResult in words_result:
    words = words_result[wordResult]['words']
    print(wordResult+':'+words)

运行结果:

身份证识别状态:识别正常
身份证朝向:正向
身份证类型:正常身份证
住址:湖南省永兴县复和乡梓木村五组
出生:19920529
姓名:胡沙
公民身份号码:431023199205297212
性别:男
民族:

你可能感兴趣的:(Python,身份证,百度API,python)