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

1 篇文章 0 订阅
1 篇文章 0 订阅

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
性别:男
民族:
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LI4836

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值