接口描述
针对图片中的身份证,进行OCR检测,返回检测到的姓名、身份证号码等信息
接口要求
图片格式:
1. 图片单张大小不超过 10MB,批量请求单次不超过 50张
2. 图片像素尺寸大于 32x32 ,小于 5000x5000
3. 图片格式支持 png、jpg、jpeg、bmp格式
请求说明
-
请求示例
HTTP方法:POST
请求URL:https://ai.ctyun.cn/v1/aiop/api/[系统生成的服务码]/IdentityCard/ocr/v1/idcard.json
-
请求头说明
参数 |
值(说明) |
Content-Type |
application/json |
Sign |
MD5(AppKey + AppSecret + Timestamp + Path + Http Body),32位小写的MD5加密串 |
Timestamp |
13位时间戳 |
AppKey |
平台分配给应用独立的AppKey |
请求参数
参数 |
是否必选 |
类型 |
可选值范围 |
说明 |
data |
true |
list |
- |
请求参数放置于http body中,一张或多张图像Base64编码数据构成的list。注意:图片需要使用常规base64编码方式 |
-
请求代码示例
Bash:
curl -X POST "https://ai.ctyun.cn/v1/aiop/api/[系统生成的服务码]/IdentityCard/ocr/v1/idcard.json " -H "Content-Type: application/json" -H 'Sign: XXXXX' -H 'Timestamp: XXXXX' -H 'AppKey: XXXXX' --data '{"data": ["9j_4AAQSkZJRgABAQAAAQABAAD_4QAwRXhpZgAATU0AKgAAA..."]}'
Python:
# -*- coding: utf-8 -*-
import requests
import base64
import hashlib
import time
import json
'''
身份证识别
'''
def md5(key):
input_name = hashlib.md5()
input_name.update(key.encode("utf-8"))
return input_name.hexdigest()
request_url = "身份证识别的API URL"
# 二进制方式打开图片文件
f = open('[本地文件]', 'rb')
img = "{}".format(base64.b64encode(f.read()).decode())
params = {"data": [img]}
app_key = '平台分配的APP Key'
app_secret = '平台分配的APP Secret '
time_stamp = str(int(round(time.time() * 1000)))
headers = {
'content-type': 'application/json',
'Sign': md5(app_key + app_secret + time_stamp + '/v1/aiop/api/[系统生成的服务码]/IdentityCard/ocr/v1/idcard.json' + json.dumps(params)),
'Timestamp': time_stamp,
'AppKey': app_key
}
response = requests.post(request_url, data=json.dumps(params), headers=headers)
if response:
print(response.json())
返回说明
-
返回参数
返回字段 |
是否必选 |
类型 |
字段说明 |
code |
true |
int |
请求响应状态码 |
message |
true |
object |
请求信息结构体,message["success"]代表请求list中的成功数量,message["fail"]代表请求list中的失败数量。 |
result |
true |
json array |
识别的结果,按照列表形式排列,每个元素为图片对应的鉴定结果键值对,元素排序按照传入顺序排列,元素中包括身份证正反面信息,以及姓名、性别、身份证号码等信息 |
result[].note |
true |
string |
身份的正面或反面信息,国徽面为正面:front,人像面为反面:back |
result[].detail |
true |
map |
返回结果键值集合,包含姓名、性别、身份证号码等信息 |
result[].detail.name |
true |
string |
姓名 |
result[].detail.sex |
true |
string |
性别 |
result[].detail.nation |
true |
string |
民族 |
result[].detail.birthday |
true |
string |
出生日期 |
result[].detail.idn |
true |
string |
身份证号 |
result[].detail.addr |
true |
string |
住址 |
result[].detail.org |
true |
string |
签发机关 |
result[].detail.validperiod |
true |
string |
有效期限 |
result[].idcard_face |
true |
string |
身份照片的人脸图片Base64字符串 |
result[].demo_image |
true |
string |
无效字段(可忽略) |
result[].risk |
true |
string |
判定身份证是原件还是复印件 |
result[].err_code |
true |
int |
若该张图片处理异常,则返回该图片的错误代码,错误代码参见错误码说明 |
result[].err_msg |
true |
int |
若该张图片处理异常,则返回该图片的错误消息,错误消息参见错误码说明 |
-
API调用失败返回值
返回字段 |
是否必选 |
类型 |
字段说明 |
code |
true |
int |
错误码,参见本文档错误码说明部分 |
message |
true |
string |
返回对应的可读消息,值为error |
details |
true |
string |
错误消息 |
-
返回示例
HTTP/1.1 200 OK
Content-Type: application/json
{
'code': 0,
'message': {'success' : 1, 'fail' : 0},
'result': [
{
'note': 'front',
'detail':
{
'name': '',
'sex': '',
'nation': '',
'birthday': '',
'idn': '',
'addr': '',
'org': '兰州市公安局城关分局',
'validperiod': '2007.07.27-2027.07.27'
},
'idcard_face': 'no face',
'demo_image': 'demo_image',
'risk': 'normal'
}
]
}
错误码说明
错误码 |
错误信息 |
4001 |
请求JSON处理异常 |
4002 |
body传入的不是字典 |
4003 |
请求中未包data字段 |
4004 |
data字段图片数据不是list格式 |
4005 |
图片list内图片数量为0 |
4006 |
图片list内图片数量超过50张 |
4007 |
文件处理错误,请联系管理员 |
4008 |
base64数据处理异常 |
4009 |
请求文件格式不合法,仅支持 jpeg/png/jpg/bmp 格式 |
4010 |
单张图片大小超过10M |
4011 |
图片尺寸不符合要求,分辨率长宽尺寸应低于5000,高于32 |
4012 |
请求方法错误,请使用POST请求 |
4013 |
无效的请求路径,请确保请求路径正确 |
5000 |
OCR服务接口异常,请联系管理员 |