IDcard身份证自动识别
身份证识别
百度发布很多智能识别功能具体参见:
https://cloud.baidu.com/doc/OCR/s/zk3h7xw5e 技术文档`
1.接口描述
支持对二代居民身份证正反面所有8个字段进行结构化识别,包括姓名、性别、民族、出生日期、住址、身份证号、签发机关、有效期限,识别准确率超过99%;同时支持身份证正面头像检测,并返回头像切片的base64编码及位置信息。
同时,支持对用户上传的身份证图片进行图像风险和质量检测,可识别图片是否为复印件或临时身份证,是否被翻拍或编辑,是否存在正反颠倒、模糊、欠曝、过曝等质量问题。
请求说明
请求示例
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中放置请求参数,参数详情如下:
2.请求参数
参数 | 是否必选 | 类型 | 说明 |
---|---|---|---|
image | 是 | string | 图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/jpeg/png/bmp格式 |
id_card_side | 是 | string | front/back front:身份证含照片的一面;back:身份证带国徽的一面 |
detect_direction | 否 | string | true/false 是否检测图像旋转角度,默认检测,即:true。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括: true:检测旋转角度;false:不检测旋转角度。 |
detect_risk | 否 | string | true/false 是否开启身份证风险类型(身份证复印件、临时身份证、身份证翻拍、修改过的身份证)功能,默认不开启,即:false。可选值:true-开启;false-不开启 |
detect_photo | 否 | string | true/false是否检测头像内容,默认不检测。可选值:true-检测头像并返回头像的 base64 编码及位置信息 |
detect_rectify | 否 | string | true/false 是否进行完整性校验,默认为true,需上传各字段内容完善的图片方可识别;如果设置为false,则对于身份证切片(如仅身份证号区域)也可识别 |
请求代码示例
提示一:使用示例代码前,请记得替换其中的示例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'
3返回参数说明
字段 | 是否必选 | 类型 | 说明 |
---|---|---|---|
direction | 否 | int32图像方向,当 detect_direction = true 时,返回该参数。 | - -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),如果没有被编辑过则返回值无此参数 |
log_id | 是 | int | uint64 唯一的log id,用于问题定位 |
photo | 否 | string | 当请求参数 detect_photo = true时返回,头像切图的 base64 编码(无编码头,需自行处理) |
photo_location | 否 | lString | array() 当请求参数 detect_photo = true时返回,头像的位置信息(坐标0点为左上角) |
idcard_number_type | 是 | string | 用于校验身份证号码、性别、出生是否一致,输出结果及其对应关系如下:-1: 身份证正面所有字段全为空0: 身份证证号识别错误1: 身份证证号和性别、出生信息一致2: 身份证证号和性别、出生信息都不一致3: 身份证证号和出生信息不一致4: 身份证证号和性别信息不一致 |
words_result | 是 | array() | 定位和识别结果数组 |
words_result_num | 是 | uint32 | 识别结果数,表示words_result的元素个数 |
+location | 是 | array() | 位置数组(坐标0点为左上角) |
++left | 是 | uint32 | 表示定位位置的长方形左上顶点的水平坐标 |
++top | 是 | uint32 | 表示定位位置的长方形左上顶点的垂直坐标 |
++width | 是 | uint32 | 表示定位位置的长方形的宽度 |
++height | 是 | uint32 | 表示定位位置的长方形的高度 |
+words | 否 | string | 识别结果字符串 |
4.返回示例
{
"log_id": 2648325511,
"direction": 0,
"image_status": "normal",
"idcard_type": "normal",
"edit_tool": "Adobe Photoshop CS3 Windows",
"photo": "/9j/4AAQSkZJRgABA......",
"photo_location": {
"width": 1189,
"top": 638,
"left": 2248,
"height": 1483
},
"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
}
示例Demopom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>IDCards</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.12.0</version>
</dependency>
</dependencies>
</project>
主程序Sample
package com.baidu.ai;
import com.baidu.aip.ocr.AipOcr;
import org.json.JSONObject;
import java.util.HashMap;
/**
* @Author:xuchang
* @Date 2020-02-29 08:35
* @description
*/
public class Sample {
//设置APPID/AK/SK
public static final String APP_ID = "注册的appID";
public static final String API_KEY = "你注册的百度apiKey";
public static final String SECRET_KEY = "你注册的百度秘钥";
public static void main(String[] args) {
// 初始化一个AipOcr
AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
// 可选:设置网络连接参数
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);
// 可选:设置代理服务器地址, http和socket二选一,或者均不设置
// client.setHttpProxy("proxy_host", proxy_port); // 设置http代理
//client.setSocketProxy("proxy_host", proxy_port); // 设置socket代理
// 可选:设置log4j日志输出格式,若不设置,则使用默认配置
// 也可以直接通过jvm启动参数设置此环境变量
//System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");
// 调用接口
String path = "F:\\韦小宝.jpg"; //身份证图片路径
JSONObject res = client.basicGeneral(path, new HashMap<String, String>());
System.out.println(res.toString(2));
}
}
运行结果:
{
"words_result": [
{"words": "姓名韦小宝"},
{"words": "性别男民族汉"},
{"words": "出生1654年12月20日"},
{"words": "住址北京市东城区景山前街4号"},
{"words": "紫禁城敬事房"},
{"words": "公民身份证号码112日4416541228243X"}
],
"log_id": 1299889884391014400,
"words_result_num": 6
}