身份证识别系统搭建
获取视觉智能开放平台提供的 SDK
进入到视觉智能开放平台的官网 https://vision.aliyun.com/ 后,我们可以发现平台已经开放了包括人脸识别、文字识别、商品理解等在内的多项视觉 AI 能力,点击文字识别菜单,可以找到身份证识别项,然后我们点击进入。
身份证识别包括识别二代身份证关键字段内容,关键字段包括:姓名、性别、民族、身份证号、出生日期、地址信息、有效起始时间、签发机关,同时可输出身份证区域位置和人脸位置信息。
体验调试
在这里我们可以先上传图片进行试用
上传成功后点击发起调用查看结果
识别结果
{
"success": true,
"data": {
"data": {
"RequestId": "23C9D339-63FD-43E9-814B-BC19EE1EECD1",
"Data": {
"FrontResult": {
"Address": "华盛顿特区宜宾法尼亚大道1600号白宫",
"FaceRectVertices": [
{
"X": 0,
"Y": 0
},
{
"X": 0,
"Y": 0
},
{
"X": 0,
"Y": 0
},
{
"X": 0,
"Y": 0
}
],
"FaceRectangle": {
"Center": {
"X": 0,
"Y": 0
},
"Angle": 0,
"Size": {
"Height": 0,
"Width": 0
}
},
"CardAreas": [
{
"X": 7,
"Y": 40
},
{
"X": 460,
"Y": 36
},
{
"X": 463,
"Y": 311
},
{
"X": 10,
"Y": 316
}
],
"Gender": "",
"Nationality": "肯尼亚",
"Name": "奥巴马",
"IDNumber": "",
"BirthDate": ""
}
}
},
"url": "https://ocr.cn-shanghai.aliyuncs.com/",
"during": 560,
"headers": {
"response": {
"date": "Fri, 30 Oct 2020 12:07:13 GMT",
"content-type": "application/json;charset=utf-8",
"content-length": "464",
"connection": "keep-alive",
"access-control-allow-origin": "*",
"access-control-allow-methods": "POST, GET, OPTIONS",
"access-control-allow-headers": "X-Requested-With, X-Sequence, _aop_secret, _aop_signature",
"access-control-max-age": "172800",
"x-acs-request-id": "23C9D339-63FD-43E9-814B-BC19EE1EECD1"
}
}
},
"requestId": "303f05d1-e59a-477a-8641-e13dab8b0a12"
}
示例代码,这个代码有错误,后文有改正的代码
#!/usr/bin/env python
#coding=utf-8
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkocr.request.v20191230.RecognizeIdentityCardRequest import RecognizeIdentityCardRequest
client = AcsClient('<accessKeyId>', '<accessSecret>', 'cn-shanghai')
request = RecognizeIdentityCardRequest()
request.set_accept_format('json')
request.set_Side("face")
request.set_ImageURL("http://explorer-image.oss-cn-shanghai.aliyuncs.com/HiQN85YxMTAEMDfD8fGruxyU/s1.jpg?OSSAccessKeyId=LTAI4Fk9FstqSEYnqKJ5Dpeo&Expires=1604061374&Signature=RC9fZ%2BeORAjfli09HRG4xEYh2CM%3D")
response = client.do_action_with_exception(request)
# python2: print(response)
print(str(response, encoding='utf-8'))
编写程序并尝试
安装aliyunsdkcore
安装SDK核心库。
使用 pip 命令安装阿里云SDK核心库:pip install aliyun-python-sdk-core
安装云产品的SDK。pip install aliyun-python-sdk-ecs
安装文字识别,阿里云OCR服务pip install aliyun-python-sdk-ocr
开通身份证识别
点击立即开通
创建AccessKey
在使用阿里云SDK和调用视觉智能开放平台的API之前,确保您已经生成访问密钥(AccessKey)。您可以在个人中心AcessKey管理中创建并管理属于您自己的AccessKey,
主账号AccessKey
登录视觉智能开放平台控制台。
鼠标悬停至右上角的用户头像,单击AccessKey管理。
创建AccessKey
在安全提示对话框中单击继续使用AccessKey。
在安全信息管理页面,单击左上角的创建AccessKey。
在手机验证对话框,单击点击获取。
输入校验码,单击确定。
启动开发
下载安装SDK包。
目前,SDK支持Java、Python、Node.js、PHP四种常用语言,您可以选择对应语言类目下的视觉技术能力SDK进行接入。
之前已安装PythonSDK包
RAM授权。
为账户进行RAM授权,以访问相应的阿里云资源。
依照样例和说明文档编写程序并运行
开始本地调用。
参考身份证识别接口文档,进行开发。
程序
#!/usr/bin/env python
#coding=utf-8
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkocr.request.v20191230 import RecognizeIdentityCardRequest
client = AcsClient('xxxxxxxxx', 'xxxxxxxxxx', 'cn-shanghai')
request = RecognizeIdentityCardRequest.RecognizeIdentityCardRequest();
request.set_accept_format('json')
request.set_Side("face")
request.set_ImageURL("http://viapi-test.oss-cn-shanghai.aliyuncs.com/sanjiye-meizi/5.15/shenfz.jpg")
response = client.do_action_with_exception(request)
# python2: print(response)
print(str(response, encoding='utf-8'))
得到结果
C:\Users\xxx\.conda\envs\aliproject\python.exe C:/Users/xxx/Desktop/aliyunidcardocr/main.py
{"RequestId":"9E6AA8E5-75E5-4A47-856D-1509DAA203A4","Data":{"FrontResult":{"Address":"河北省邯郸市肥乡县肥乡镇","FaceRectVertices":[{"X":521.0860595703125,"Y":280.8114013671875},{"X":424.39859008789062,"Y":277.23809814453125},{"X":428.482666015625,"Y":166.73095703125},{"X":525.170166015625,"Y":170.30426025390625}],"FaceRectangle":{"Center":{"X":474.78436279296875,"Y":223.77117919921875},"Angle":-87.883460998535156,"Size":{"Height":96.7535400390625,"Width":110.58258056640625}},"CardAreas":[{"X":38,"Y":76},{"X":589,"Y":73},{"X":591,"Y":399},{"X":40,"Y":403}],"Gender":"男","Nationality":"汉","Name":"常涛","IDNumber":"411526199706013217","BirthDate":"19970601"}}}
Process finished with exit code 0
运行结果
结语
RecognizeIdentityCard可以识别二代身份证关键字段内容,关键字段包括:姓名、性别、民族、身份证号、出生日期、地址信息、有效起始时间、签发机关,同时可输出身份证区域位置和人脸位置信息。
参考
https://help.aliyun.com/document_detail/145009.html?spm=a2c4g.11186623.6.565.786d3ee7YEbR4a
https://help.aliyun.com/document_detail/151899.html?spm=a2c4g.11186623.2.33.7cb72b14PwDPyX#doc-api-ocr-RecognizeIdentityCard
https://vision.aliyun.com/experience/detail?spm=a211p3.14471183.J_7240705510.9.46dc1aa1ILcbp5&tagName=ocr&children=RecognizeIdentityCard#techSupport