文档简介:
接口描述
支持对二代居民身份证正反面所有8个字段进行结构化识别,包括姓名、性别、民族、出生日期、住址、身份证号、签发机关、有效期限,识别准确率超过99%;同时支持身份证正面头像检测,并返回头像切片的base64编码及位置信息。
同时,支持对用户上传的身份证图片进行图像质量和风险检测,是否存在正反颠倒、模糊、欠曝、过曝等质量问题,可识别图片是否为复印件或临时身份证,是否被翻拍或编辑,是否存在四角不完整、头像或关键字段被遮挡。
增值能力 | 详情 |
---|---|
裁剪能力 | 头像检测与切片:返回头像切片的base64编码及位置信息 |
身份证检测与切片:返回身份证的base64编码及位置信息(去掉证件外围多余背景、自动矫正拍摄角度) | |
质量检测 | 身份证图片模糊检测 |
身份证关键字段反光或过曝光 | |
身份证图片较暗或欠曝光 | |
身份证边框/四角不完整告警 | |
身份证头像或关键字段被遮挡/马赛克告警 | |
风险检测 | 身份证复印件告警 |
临时身份证告警 | |
身份证翻拍告警 | |
身份证PS编辑告警 | |
身份证证号不合法告警 | |
身份证证号和出生日期、性别信息不一致告警 |
视频教程请参见 身份证识别API调用教程(视频版)
在线调试
您可以在 示例代码中心 中调试该接口,可进行签名验证、查看在线调用的请求内容和返回结果、示例代码的自动生成。
请求说明
请求示例
HTTP 方法:POST
请求URL: https://aip.baidubce.com/rest/2.0/ocr/v1/idcard
URL参数:
参数 | 值 |
---|---|
access_token | 通过API Key和Secret Key获取的access_token,参考“Access Token获取” |
Header如下:
参数 | 值 |
---|---|
Content-Type | application/x-www-form-urlencoded |
Body中放置请求参数,参数详情如下:
请求参数
参数 | 是否必选 | 类型 | 可选值范围 | 说明 |
---|---|---|---|---|
image | 和url二选一 | string | - | 图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/jpeg/png/bmp格式 |
url | 和image二选一 | string | - |
图片完整URL,URL长度不超过1024字节,URL对应的图片base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/jpeg/png/bmp格式,当image字段存在时url字段失效 请注意关闭URL防盗链 |
id_card_side | 是 | string | front/back |
-front:身份证含照片的一面 -back:身份证带国徽的一面 自动检测身份证正反面,如果传参指定方向与图片相反,支持正常识别,返回参数image_status字段为"reversed_side" |
detect_risk | 否 | string | true/false |
是否开启身份证风险类型(身份证复印件、临时身份证、身份证翻拍、修改过的身份证)检测功能,默认不开启,即:false。 - true:开启,请查看返回参数risk_type; - false:不开启 |
detect_quality | 否 | string | true/false |
是否开启身份证质量类型(边框/四角不完整、头像或关键字段被遮挡/马赛克)检测功能,默认不开启,即:false。 - true:开启,请查看返回参数card_quality; - false:不开启 |
detect_photo | 否 | string | true/false | 是否检测头像内容,默认不检测。可选值:true-检测头像并返回头像的 base64 编码及位置信息 |
detect_card | 否 | string | true/false | 是否检测身份证进行裁剪,默认不检测。可选值:true-检测身份证并返回证照的 base64 编码及位置信息 |
请求代码示例
提示一:使用示例代码前,请记得替换其中的示例Token、图片地址或Base64信息。
提示二:部分语言依赖的类或库,请在代码注释中查看下载地址。
curl -i -k 'https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=
【调用鉴权接口获取的token】' --data 'id_card_side=front&image=【图片Base64编码,
需UrlEncode】' -H 'Content-Type:application/x-www-form-urlencoded'
返回说明
返回参数
字段 | 是否必选 | 类型 | 说明 |
---|---|---|---|
log_id | 是 | uint64 | 唯一的log id,用于问题定位 |
words_result | 是 | array[] | 定位和识别结果数组 |
words_result_num | 是 | uint32 | 识别结果数,表示words_result的元素个数 |
direction | 是 | int32 |
图像方向。 - - 1:未定义, - 0:正向, - 1:逆时针90度, - 2:逆时针180度, - 3:逆时针270度 |
image_status | 是 | string |
normal-识别正常 reversed_side-身份证正反面颠倒 non_idcard-上传的图片中不包含身份证 blurred-身份证模糊 other_type_card-其他类型证照 over_exposure-身份证关键字段反光或过曝 over_dark-身份证欠曝(亮度过低) unknown-未知状态 |
risk_type | 否 | string |
输入参数 detect_risk = true 时,则返回该字段识别身份证风险类型: normal-正常身份证; copy-复印件; temporary-临时身份证; screen-翻拍; unknown-其他未知情况 |
edit_tool | 否 | string | 如果参数 detect_risk = true 时,则返回此字段。如果检测身份证被编辑过,该字段指定编辑软件名称,如:Adobe Photoshop CC 2014 (Macintosh),如果没有被编辑过则返回值无此参数 |
card_quality | 否 | object |
输入参数 detect_quality = true 时,则返回该字段识别身份证质量类型: IsClear - 是否清晰; IsComplete - 是否边框/四角完整; IsNoCover - 是否头像、关键字段无遮挡/马赛克。 及对应的概率:IsComplete_propobility、IsClear_propobility、IsNoCover_propobility,值在0-1之间,值越大表示图像质量越好。 默认阈值(仅为推荐值,建议按照实际业务场景,基于图片返回的具体概率值,自定义设置判断阈值):当 IsClear_propobility、IsComplete_propobility 超过0.5时,对应IsClear、IsComplete 返回1,低于0.5,则返回0; 当 IsNoCover_propobility 超过0.3时,对应IsNoCover 返回1,低于0.3,则返回0 |
photo | 否 | string | 当请求参数 detect_photo = true时返回,头像切图的 base64 编码(无编码头,需自行处理) |
photo_location | 否 | object | 当请求参数 detect_photo = true时返回,头像的位置信息(坐标0点为左上角) |
card_image | 否 | string | 当请求参数 detect_card = true时返回,身份证裁剪切图的 base64 编码(无编码头,需自行处理) |
card_location | 否 | object | 当请求参数 detect_card = true时返回,身份证裁剪切图的位置信息(坐标0点为左上角) |
idcard_number_type | 是 | int |
用于校验身份证号码、性别、出生是否一致,输出结果及其对应关系如下: - 1: 身份证正面所有字段全为空 0: 身份证证号不合法,此情况下不返回身份证证号 1: 身份证证号和性别、出生信息一致 2: 身份证证号和性别、出生信息都不一致 3: 身份证证号和出生信息不一致 4: 身份证证号和性别信息不一致 |
+ location | 是 | array[] | 位置数组(坐标0点为左上角) |
++ left | 是 | uint32 | 表示定位位置的长方形左上顶点的水平坐标 |
++ top | 是 | uint32 | 表示定位位置的长方形左上顶点的垂直坐标 |
++ width | 是 | uint32 | 表示定位位置的长方形的宽度 |
++ height | 是 | uint32 | 表示定位位置的长方形的高度 |
+ words | 否 | string | 识别结果字符串 |
返回示例(身份证头像面)
{ "log_id": 2648325511, "direction": 0, "image_status": "normal", "photo":
"/9j/4AAQSkZJRgABA......", "photo_location": { "width": 1189, "top": 638, "left":
2248, "height": 1483 }, "card_image": "/9j/4AAQSkZJRgABA......", "card_location":
{ "top": 328, "left": 275, "width": 1329, "height": 571 }, "words_result": { "住址":
{ "location": { "left": 267, "top": 453, "width": 459, "height": 99 }, "words":
"南京市江宁区弘景大道3889号" }, "公民身份号码": { "location": { "left": 443, "top":
681, "width": 589, "height": 45 }, "words": "330881199904173914" }, "出生": {
"location": { "left": 270, "top": 355, "width": 357, "height": 45 }, "words":
"19990417" }, "姓名": { "location": { "left": 267, "top": 176, "width": 152,
"height": 50 }, "words": "伍云龙" }, "性别": { "location": { "left": 269, "top": 262,
"width": 33, "height": 52 }, "words": "男" }, "民族": { "location": { "left": 492,
"top": 279, "width": 30, "height": 37 }, "words": "汉" } }, "words_result_num": 6 }
返回示例(身份证国徽面)
{ "words_result": { "失效日期": { "words": "20390711", "location": { "top": 445,
"left": 523, "width": 153, "height": 38 } }, "签发机关": { "words": "陆丰市公安局",
"location": { "top": 377, "left": 339, "width": 195, "height": 38 } }, "签发日期":
{ "words": "20190606", "location": { "top": 445, "left": 343, "width": 152, "height":
38 } } }, "log_id": "1559208562721579328", "words_result_num": 3,
"error_code": 0, "image_status": "normal" }
AES加密
您可以选择使用AES加密来请求本身份证识别接口,支持对身份证图片及识别结果进行加密后传输,示意图如下:
- 在百度云控制台「文字识别-应用列表-管理」中获取您的AES Key
- 使用AES Key对将要识别的图片进行加密
- 将加密后的图片传入接口,请求参数AESEncry设置为true
-
接口返回加密后的识别结果,使用AES Key进行解密,得到明文识别结果
使用AES加密不影响身份证识别接口支持的质量检测、风险检测等其他能力,也不影响识别效果。
请求说明
请求示例
HTTP 方法:POST
请求URL: https://aip.baidubce.com/rest/2.0/ocr/v1/idcard
URL参数:
参数 | 值 |
---|---|
access_token | 通过API Key和Secret Key获取的access_token,参考“Access Token获取” |
Header如下:
参数 | 值 |
---|---|
Content-Type | application/x-www-form-urlencoded |
Body中放置请求参数,参数详情如下:
请求参数
参数 | 是否必选 | 类型 | 可选值范围 | 说明 |
---|---|---|---|---|
image | 是 | string | - | AES加密后的图像数据,请对加密图片进行base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/jpeg/png/bmp格式 |
AESEncry | 是 | string | true/false | 使用AES加密请传true |
id_card_side | 是 | string | front/back |
-front:身份证含照片的一面 -back:身份证带国徽的一面 自动检测身份证正反面,如果传参指定方向与图片相反,支持正常识别,返回参数image_status字段为"reversed_side" |
detect_direction | 否 | string | - | 此参数新版本无需传,支持自动检测图像旋转角度 |
detect_risk | 否 | string | true/false |
是否开启身份证风险类型(身份证复印件、临时身份证、身份证翻拍、修改过的身份证)功能,默认不开启,即:false。 - true:开启,请查看返回参数risk_type; - false:不开启 |
detect_photo | 否 | string | true/false | 是否检测头像内容,默认不检测。可选值:true-检测头像并返回头像的 base64 编码及位置信息 |
请求代码示例
提示一:使用示例代码前,请记得替换其中的示例Token、图片地址或Base64信息。
提示二:部分语言依赖的类或库,请在代码注释中查看下载地址。
# -*- coding: utf-8 -*- # from urllib import urlencode import base64 from urllib import
unquote import requests import json from Crypto.Cipher import AES import binascii ak = "ak" # 文字识别应用的API Key sk = "sk" # 文字识别应用的Secret Key aes = 'res'
# aes key 从控制台 文字识别-应用列表-应用管理 获取 class AESCipher: def __init__
(self, key): self.key = self.get_key(key) def get_key(self, key): st = bytearray()
for s in key: st.append(int(s, 16)) str = st.decode('utf-8') return str def pad(self,
text): # 填充方法,加密内容必须为16字节的倍数 text_length = len(text) amount_to_pad =
AES.block_size - (text_length % AES.block_size) if amount_to_pad == 0: amount_to_pad = AES.block_size pad = chr(amount_to_pad) return text + pad * amount_to_pad def __unpad(self, text):
# 截取填充的字符 pad = ord(text[-1]) return text[:-pad] # 加密函数 def encrypt(self, raw):
raw = self.pad(raw) cipher = AES.new(self.key, AES.MODE_ECB) return base64.b64encode
(cipher.encrypt(raw)) def decrypt(self, enc): """ 解密方法 :param enc: base64编码的密文 str :return: 解密后的明文 str """ cipher = AES.new(self.key, AES.MODE_ECB) return self.__unpad(cipher.decrypt(enc))
def get_accessToken(): url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client
_credentials&client_id={ak}&client_secret={sk}".format( ak=ak, sk=sk) response = requests.
get(url) return response.json()['access_token'] encryptor = AESCipher(aes) token =
get_accessToken() # 改成接口文档里的url nameurl = "https://aip.baidubce.com/rest/2.0/
ocr/v1/idcard" url = nameurl + "?access_token={token}".format(token=token) print url f = open('resources/my.jpg', 'rb') d = f.read() r_data = encryptor.encrypt(d)
# 构造请求数据格式 data = {} data['image'] = r_data data['id_card_side'] = 'front' data['AESEncry'] = 'true' headers = {'content-type':
'application/x-www-form-urlencoded'} res = requests.post(url=url, data=data,
headers=headers) #res= json.dumps(res.content, ensure_ascii=False) data=json.loads
(res.content) result=encryptor.decrypt(base64.b64decode(data['result'])) print result
返回说明
返回示例
{ "log_id":"2648325511", "result":"密文" }
请对result密文进行base64解码后得到byte流,再进行AES解密,得到识别结果的明文。