港澳台证件识别
更新时间:2024-05-14
接口描述
支持识别4类港澳台出入境证件,包含港澳通行证正/反面、台湾通行证正/反面、台胞证(台湾居民来往大陆通行证)正/反面、返乡证(港澳居民来往内地通行证)正/反面,支持识别以上4类证件的全部字段信息。
在线调试
您可以在 示例代码中心 中调试该接口,可进行签名验证、查看在线调用的请求内容和返回结果、示例代码的自动生成。
请求说明
请求示例
HTTP 方法:POST
请求URL: https://aip.baidubce.com/rest/2.0/ocr/v1/hk_macau_taiwan_exitentrypermit
URL参数:
参数 | 值 |
---|---|
access_token | 通过API Key和Secret Key获取的access_token,参考“Access Token获取” |
Header如下:
参数 | 值 |
---|---|
Content-Type | application/x-www-form-urlencoded |
Body中放置请求参数,参数详情如下:
请求参数
参数 | 是否必选 | 类型 | 可选值范围 | 说明 |
---|---|---|---|---|
image | 和 url/pdf_file 三选一 | string | - | 图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/jpeg/png/bmp格式 优先级:image > url > pdf_file,当image字段存在时,url、pdf_file字段失效 |
url | 和 image/pdf_file 三选一 | string | - | 图片完整url,url长度不超过1024字节,url对应的图片base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/jpeg/png/bmp格式优先级:image > url > pdf_file,当image字段存在时,url字段失效请注意关闭URL防盗链 |
pdf_file | 和 image/url 三选一 | string | - | PDF文件,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过4M,最短边至少15px,最长边最大4096px优先级:image > url > pdf_file,当image、url字段存在时,pdf_file字段失效 |
pdf_file_num | 否 | string | - | 需要识别的PDF文件的对应页码,当 pdf_file 参数有效时,识别传入页码的对应页面内容,若不传入,则默认识别第 1 页 |
exitentrypermit_type | 是 | string | hk_mc_passport_front:港澳通行证正面hk_mc_passport_back:港澳通行证反面tw_passport_front:台湾通行证正面tw_passport_back:台湾通行证反面tw_return_passport_front:台胞证正面tw_return_passport_back:台胞证反面hk_mc_return_passport_front:返乡证正面hk_mc_return_passport_back:返乡证反面 | 出入境许可证件的具体类型 |
probability | 否 | string | true/false | 是否返回字段置信度,默认为 false ,即不返回 |
location | 否 | string | true/false | 是否返回字段位置坐标,默认为 false,即不返回 |
请求代码示例
提示一:使用示例代码前,请记得替换其中的示例Token、图片地址或Base64信息。
提示二:部分语言依赖的类或库,请在代码注释中查看下载地址。
1curl -i -k 'https://aip.baidubce.com/rest/2.0/ocr/v1/hk_macau_taiwan_exitentrypermit?access_token=【调用鉴权接口获取的token】' --data 'image=【图片Base64编码,需UrlEncode】&exitentrypermit_type=tw_return_passport_front' -H 'Content-Type:application/x-www-form-urlencoded'
1# encoding:utf-8
2
3import requests
4import base64
5
6'''
7港澳台证件识别
8'''
9
10request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/hk_macau_taiwan_exitentrypermit"
11# 二进制方式打开图片文件
12f = open('[本地文件]', 'rb')
13img = base64.b64encode(f.read())
14
15params = {"image":img,"exitentrypermit_type":"tw_return_passport_front"
16}
17access_token = '[调用鉴权接口获取的token]'
18request_url = request_url + "?access_token=" + access_token
19headers = {'content-type': 'application/x-www-form-urlencoded'}
20response = requests.post(request_url, data=params, headers=headers)
21if response:
22 print (response.json())
1package com.baidu.ai.aip;
2
3import com.baidu.ai.aip.utils.Base64Util;
4import com.baidu.ai.aip.utils.FileUtil;
5import com.baidu.ai.aip.utils.HttpUtil;
6
7import java.net.URLEncoder;
8
9/**
10* 港澳台证件识别
11*/
12public class SocialSecurityCard {
13
14 /**
15 * 重要提示代码中所需工具类
16 * FileUtil,Base64Util,HttpUtil,GsonUtils请从
17 * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
18 * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
19 * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
20 * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
21 * 下载
22 */
23 public static String securityCard() {
24 // 请求url
25 String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/social_security_card";
26 try {
27 // 本地文件路径
28 String filePath = "[本地文件路径]";
29 byte[] imgData = FileUtil.readFileByBytes(filePath);
30 String imgStr = Base64Util.encode(imgData);
31 String imgParam = URLEncoder.encode(imgStr, "UTF-8");
32
33 String param = "image=" + imgParam + "&exitentrypermit_type=tw_return_passport_front";
34
35 // 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
36 String accessToken = "[调用鉴权接口获取的token]";
37
38 String result = HttpUtil.post(url, accessToken, param);
39 System.out.println(result);
40 return result;
41 } catch (Exception e) {
42 e.printStackTrace();
43 }
44 return null;
45 }
46
47 public static void main(String[] args) {
48 BankCard.bankCard();
49 }
50}
1#include <iostream>
2#include <curl/curl.h>
3
4// libcurl库下载链接:https://curl.haxx.se/download.html
5// jsoncpp库下载链接:https://github.com/open-source-parsers/jsoncpp/
6const static std::string request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/social_security_card";
7static std::string securityCard_result;
8/**
9 * curl发送http请求调用的回调函数,回调函数中对返回的json格式的body进行了解析,解析结果储存在全局的静态变量当中
10 * @param 参数定义见libcurl文档
11 * @return 返回值定义见libcurl文档
12 */
13static size_t callback(void *ptr, size_t size, size_t nmemb, void *stream) {
14 // 获取到的body存放在ptr中,先将其转换为string格式
15 bankCard_result = std::string((char *) ptr, size * nmemb);
16 return size * nmemb;
17}
18/**
19 * 港澳台证件识别
20 * @return 调用成功返回0,发生错误返回其他错误码
21 */
22int bankCard(std::string &json_result, const std::string &access_token) {
23 std::string url = request_url + "?access_token=" + access_token;
24 CURL *curl = NULL;
25 CURLcode result_code;
26 int is_success;
27 curl = curl_easy_init();
28 if (curl) {
29 curl_easy_setopt(curl, CURLOPT_URL, url.data());
30 curl_easy_setopt(curl, CURLOPT_POST, 1);
31 curl_httppost *post = NULL;
32 curl_httppost *last = NULL;
33 curl_formadd(&post, &last, CURLFORM_COPYNAME, "image", CURLFORM_COPYCONTENTS, "【base64_img】", CURLFORM_END);
34 curl_formadd(&post, &last, CURLFORM_COPYNAME, "exitentrypermit_type", CURLFORM_COPYCONTENTS, "tw_return_passport_front", CURLFORM_END);
35
36
37 curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
38 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
39 result_code = curl_easy_perform(curl);
40 if (result_code != CURLE_OK) {
41 fprintf(stderr, "curl_easy_perform() failed: %s\n",
42 curl_easy_strerror(result_code));
43 is_success = 1;
44 return is_success;
45 }
46 json_result = securityCard_result;
47 curl_easy_cleanup(curl);
48 is_success = 0;
49 } else {
50 fprintf(stderr, "curl_easy_init() failed.");
51 is_success = 1;
52 }
53 return is_success;
54}
1<?php
2/**
3 * 发起http post请求(REST API), 并获取REST请求的结果
4 * @param string $url
5 * @param string $param
6 * @return - http response body if succeeds, else false.
7 */
8function request_post($url = '', $param = '')
9{
10 if (empty($url) || empty($param)) {
11 return false;
12 }
13
14 $postUrl = $url;
15 $curlPost = $param;
16 // 初始化curl
17 $curl = curl_init();
18 curl_setopt($curl, CURLOPT_URL, $postUrl);
19 curl_setopt($curl, CURLOPT_HEADER, 0);
20 // 要求结果为字符串且输出到屏幕上
21 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
22 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
23 // post提交方式
24 curl_setopt($curl, CURLOPT_POST, 1);
25 curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
26 // 运行curl
27 $data = curl_exec($curl);
28 curl_close($curl);
29
30 return $data;
31}
32
33$token = '[调用鉴权接口获取的token]';
34$url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/social_security_card?access_token=' . $token;
35$img = file_get_contents('[本地文件路径]');
36$img = base64_encode($img);
37$bodys = array(
38 'image' => $img,
39 'exitentrypermit_type' => "tw_return_passport_front"
40
41);
42$res = request_post($url, $bodys);
43
44var_dump($res);
1using System;
2using System.IO;
3using System.Net;
4using System.Text;
5using System.Web;
6
7namespace com.baidu.ai
8{
9 public class SocialSecurityCard
10 {
11 // 港澳台证件识别
12 public static string bankCard()
13 {
14 string token = "[调用鉴权接口获取的token]";
15 string host = "https://aip.baidubce.com/rest/2.0/ocr/v1/social_security_card?access_token=" + token;
16 Encoding encoding = Encoding.Default;
17 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
18 request.Method = "post";
19 request.KeepAlive = true;
20 // 图片的base64编码
21 string base64 = getFileBase64("[本地图片文件]");
22 String str = "image=" + HttpUtility.UrlEncode(base64) + "&exitentrypermit_type="tw_return_passport_front";
23 byte[] buffer = encoding.GetBytes(str);
24 request.ContentLength = buffer.Length;
25 request.GetRequestStream().Write(buffer, 0, buffer.Length);
26 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
27 StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
28 string result = reader.ReadToEnd();
29 Console.WriteLine("港澳台证件识别:");
30 Console.WriteLine(result);
31 return result;
32 }
33
34 public static String getFileBase64(String fileName) {
35 FileStream filestream = new FileStream(fileName, FileMode.Open);
36 byte[] arr = new byte[filestream.Length];
37 filestream.Read(arr, 0, (int)filestream.Length);
38 string baser64 = Convert.ToBase64String(arr);
39 filestream.Close();
40 return baser64;
41 }
42 }
43}
返回说明
返回参数 1、港澳通行证正面
字段 | 是否必选 | 类型 | 说明 |
---|---|---|---|
log_id | 是 | uint64 | 唯一的log id,用于问题定位 |
pdf_file_size | 否 | string | 传入PDF文件的总页数,当 pdf_file 参数有效时返回该字段 |
words_result_num | 是 | uint32 | 识别结果数,表示words_result的元素个数 |
words_result | 是 | object{} | 识别结果 |
+ card_number | 是 | array[] | 证件号码 |
+ name_chn | 是 | array[] | 姓名 |
+ name_eng | 是 | array[] | 姓名(英文) |
+ birthday | 是 | array[] | 出生日期 |
+ sex | 是 | array[] | 性别 |
+ valid_date | 是 | array[] | 有效期限 |
+ issue_authority | 是 | array[] | 签发机关 |
+ issue_place | 是 | array[] | 签发地点 |
+ MRZCode | 是 | array[] | 证件下方第一行 |
++ word | 是 | string | 字段识别结果,以上字段均包含 |
++ location | 否 | object{} | 字段位置信息,当请求参数 location=true 时返回该字段,以上字段均包含 |
+++ top | 否 | uint32 | 字段的上边距 |
+++ left | 否 | uint32 | 字段的左边距 |
+++ height | 否 | uint32 | 字段的高度 |
+++ width | 否 | uint32 | 字段的宽度 |
++ probability | 否 | object{} | 字段识别结果置信度,当请求参数 probability=true 时返回该字段,以上字段均包含 |
+++ average | 否 | float | 字段识别结果中各字符的置信度平均值 |
+++ min | 否 | float | 字段识别结果中各字符的置信度最小值 |
2、港澳通行证反面
字段 | 是否必选 | 类型 | 说明 |
---|---|---|---|
log_id | 是 | uint64 | 唯一的log id,用于问题定位 |
pdf_file_size | 否 | string | 传入PDF文件的总页数,当 pdf_file 参数有效时返回该字段 |
words_result_num | 是 | uint32 | 识别结果数,表示words_result的元素个数 |
words_result | 是 | object{} | 识别结果 |
+ hk_type | 是 | array[] | 来往香港签注-种类 |
+ hk_valid_date | 是 | array[] | 来往香港签注-有效期 |
+ hk_remarks | 是 | array[] | 来往香港签注-备注 |
+ hk_round_trip_number | 是 | array[] | 来往香港签注-往返有效 |
+ mc_type | 是 | array[] | 来往澳门签注-种类 |
+ mc_valid_date | 是 | array[] | 来往澳门签注-有效期 |
+ mc_remarks | 是 | array[] | 来往澳门签注-备注 |
+ mc_round_trip_number | 是 | array[] | 来往澳门签注-往返有效 |
++ word | 是 | string | 字段识别结果,以上字段均包含 |
++ location | 否 | object{} | 字段位置信息,当请求参数 location=true 时返回该字段,以上字段均包含 |
+++ top | 否 | uint32 | 字段的上边距 |
+++ left | 否 | uint32 | 字段的左边距 |
+++ height | 否 | uint32 | 字段的高度 |
+++ width | 否 | uint32 | 字段的宽度 |
++ probability | 否 | object{} | 字段识别结果置信度,当请求参数 probability=true 时返回该字段,以上字段均包含 |
+++ average | 否 | float | 字段识别结果中各字符的置信度平均值 |
+++ min | 否 | float | 字段识别结果中各字符的置信度最小值 |
3、台湾通行证正面
字段 | 是否必选 | 类型 | 说明 |
---|---|---|---|
log_id | 是 | uint64 | 唯一的log id,用于问题定位 |
pdf_file_size | 否 | string | 传入PDF文件的总页数,当 pdf_file 参数有效时返回该字段 |
words_result_num | 是 | uint32 | 识别结果数,表示words_result的元素个数 |
words_result | 是 | object{} | 识别结果 |
+ card_number | 是 | array[] | 证件号码 |
+ name_chn | 是 | array[] | 姓名 |
+ name_eng | 是 | array[] | 姓名(英文) |
+ birthday | 是 | array[] | 出生日期 |
+ sex | 是 | array[] | 性别 |
+ valid_date | 是 | array[] | 有效期限 |
+ issue_authority | 是 | array[] | 签发机关 |
+ issue_place | 是 | array[] | 签发地点 |
+ MRZCode | 是 | array[] | 证件下方第一行 |
++ word | 是 | string | 字段识别结果,以上字段均包含 |
++ location | 否 | object{} | 字段位置信息,当请求参数 location=true 时返回该字段,以上字段均包含 |
+++ top | 否 | uint32 | 字段的上边距 |
+++ left | 否 | uint32 | 字段的左边距 |
+++ height | 否 | uint32 | 字段的高度 |
+++ width | 否 | uint32 | 字段的宽度 |
++ probability | 否 | object{} | 字段识别结果置信度,当请求参数 probability=true 时返回该字段,以上字段均包含 |
+++ average | 否 | float | 字段识别结果中各字符的置信度平均值 |
+++ min | 否 | float | 字段识别结果中各字符的置信度最小值 |
4、台湾通行证反面
字段 | 是否必选 | 类型 | 说明 |
---|---|---|---|
log_id | 是 | uint64 | 唯一的log id,用于问题定位 |
pdf_file_size | 否 | string | 传入PDF文件的总页数,当 pdf_file 参数有效时返回该字段 |
words_result_num | 是 | uint32 | 识别结果数,表示words_result的元素个数 |
words_result | 是 | object{} | 识别结果 |
+ type | 是 | array[] | 种类 |
+ valid_date | 是 | array[] | 有效期 |
+ remarks | 是 | array[] | 备注 |
+ round_trip_number | 是 | array[] | 往返有效 |
++ word | 是 | string | 字段识别结果,以上字段均包含 |
++ location | 否 | object{} | 字段位置信息,当请求参数 location=true 时返回该字段,以上字段均包含 |
+++ top | 否 | uint32 | 字段的上边距 |
+++ left | 否 | uint32 | 字段的左边距 |
+++ height | 否 | uint32 | 字段的高度 |
+++ width | 否 | uint32 | 字段的宽度 |
++ probability | 否 | object{} | 字段识别结果置信度,当请求参数 probability=true 时返回该字段,以上字段均包含 |
+++ average | 否 | float | 字段识别结果中各字符的置信度平均值 |
+++ min | 否 | float | 字段识别结果中各字符的置信度最小值 |
5、返乡证正面
字段 | 是否必选 | 类型 | 说明 |
---|---|---|---|
log_id | 是 | uint64 | 唯一的log id,用于问题定位 |
pdf_file_size | 否 | string | 传入PDF文件的总页数,当 pdf_file 参数有效时返回该字段 |
words_result_num | 是 | uint32 | 识别结果数,表示words_result的元素个数 |
words_result | 是 | object{} | 识别结果 |
+ name_chn | 是 | array[] | 姓名 |
+ name_eng | 是 | array[] | 姓名(英文) |
+ birthday | 是 | array[] | 出生日期 |
+ sex | 是 | array[] | 性别 |
+ valid_date | 是 | array[] | 有效期限 |
+ issue_authority | 是 | array[] | 签发机关 |
+ issue_times | 是 | array[] | 签发次数 |
+ card_number | 是 | array[] | 证件号码 |
++ word | 是 | string | 字段识别结果,以上字段均包含 |
++ location | 否 | object{} | 字段位置信息,当请求参数 location=true 时返回该字段,以上字段均包含 |
+++ top | 否 | uint32 | 字段的上边距 |
+++ left | 否 | uint32 | 字段的左边距 |
+++ height | 否 | uint32 | 字段的高度 |
+++ width | 否 | uint32 | 字段的宽度 |
++ probability | 否 | object{} | 字段识别结果置信度,当请求参数 probability=true 时返回该字段,以上字段均包含 |
+++ average | 否 | float | 字段识别结果中各字符的置信度平均值 |
+++ min | 否 | float | 字段识别结果中各字符的置信度最小值 |
6、返乡证反面
字段 | 是否必选 | 类型 | 说明 |
---|---|---|---|
log_id | 是 | uint64 | 唯一的log id,用于问题定位 |
pdf_file_size | 否 | string | 传入PDF文件的总页数,当 pdf_file 参数有效时返回该字段 |
words_result_num | 是 | uint32 | 识别结果数,表示words_result的元素个数 |
words_result | 是 | object{} | 识别结果 |
+ idcard_name | 是 | array[] | 身份证姓名 |
+ idcard_number | 是 | array[] | 身份证号码 |
+ MRZCode1 | 是 | array[] | 证件下方第一行 |
+ MRZCode2 | 是 | array[] | 证件下方第二行 |
++ word | 是 | string | 字段识别结果,以上字段均包含 |
++ location | 否 | object{} | 字段位置信息,当请求参数 location=true 时返回该字段,以上字段均包含 |
+++ top | 否 | uint32 | 字段的上边距 |
+++ left | 否 | uint32 | 字段的左边距 |
+++ height | 否 | uint32 | 字段的高度 |
+++ width | 否 | uint32 | 字段的宽度 |
++ probability | 否 | object{} | 字段识别结果置信度,当请求参数 probability=true 时返回该字段,以上字段均包含 |
+++ average | 否 | float | 字段识别结果中各字符的置信度平均值 |
+++ min | 否 | float | 字段识别结果中各字符的置信度最小值 |
7、台胞证正面
字段 | 是否必选 | 类型 | 说明 |
---|---|---|---|
log_id | 是 | uint64 | 唯一的log id,用于问题定位 |
pdf_file_size | 否 | string | 传入PDF文件的总页数,当 pdf_file 参数有效时返回该字段 |
words_result_num | 是 | uint32 | 识别结果数,表示words_result的元素个数 |
words_result | 是 | object{} | 识别结果 |
+ name_chn | 是 | array[] | 姓名 |
+ name_eng | 是 | array[] | 姓名(英文) |
+ birthday | 是 | array[] | 出生日期 |
+ sex | 是 | array[] | 性别 |
+ valid_date | 是 | array[] | 有效期限 |
+ issue_authority | 是 | array[] | 签发机关 |
+ issue_place | 是 | array[] | 签发地点 |
+ card_number | 是 | array[] | 证件号码 |
+ issue_times | 是 | array[] | 签发次数 |
++ word | 是 | string | 字段识别结果,以上字段均包含 |
++ location | 否 | object{} | 字段位置信息,当请求参数 location=true 时返回该字段,以上字段均包含 |
+++ top | 否 | uint32 | 字段的上边距 |
+++ left | 否 | uint32 | 字段的左边距 |
+++ height | 否 | uint32 | 字段的高度 |
+++ width | 否 | uint32 | 字段的宽度 |
++ probability | 否 | object{} | 字段识别结果置信度,当请求参数 probability=true 时返回该字段,以上字段均包含 |
+++ average | 否 | float | 字段识别结果中各字符的置信度平均值 |
+++ min | 否 | float | 字段识别结果中各字符的置信度最小值 |
8、台胞证反面
字段 | 是否必选 | 类型 | 说明 |
---|---|---|---|
log_id | 是 | uint64 | 唯一的log id,用于问题定位 |
pdf_file_size | 否 | string | 传入PDF文件的总页数,当 pdf_file 参数有效时返回该字段 |
words_result_num | 是 | uint32 | 识别结果数,表示words_result的元素个数 |
words_result | 是 | object{} | 识别结果 |
+ idcard_name | 是 | array[] | 身份证姓名 |
+ idcard_number | 是 | array[] | 身份证号码 |
+ MRZCode1 | 是 | array[] | 证件下方第一行 |
+ MRZCode2 | 是 | array[] | 证件下方第二行 |
++ word | 是 | string | 字段识别结果,以上字段均包含 |
++ location | 否 | object{} | 字段位置信息,当请求参数 location=true 时返回该字段,以上字段均包含 |
+++ top | 否 | uint32 | 字段的上边距 |
+++ left | 否 | uint32 | 字段的左边距 |
+++ height | 否 | uint32 | 字段的高度 |
+++ width | 否 | uint32 | 字段的宽度 |
++ probability | 否 | object{} | 字段识别结果置信度,当请求参数 probability=true 时返回该字段,以上字段均包含 |
+++ average | 否 | float | 字段识别结果中各字符的置信度平均值 |
+++ min | 否 | float | 字段识别结果中各字符的置信度最小值 |
返回示例 示例一:港澳通行证正面
JSON
1{
2 "words_result_num": 9,
3 "words_result": {
4 "card_number": [
5 {
6 "word": "C00000000"
7 }
8 ],
9 "name_chn": [
10 {
11 "word": "证件样本"
12 }
13 ],
14 "name_eng": [
15 {
16 "word": "ZHENGJIAN,YANGBEN"
17 }
18 ],
19 "birthday": [
20 {
21 "word": "1981.08.03"
22 }
23 ],
24 "sex": [
25 {
26 "word": "女"
27 }
28 ],
29 "valid_date": [
30 {
31 "word": "2014.04.21-2024.04.20"
32 }
33 ],
34 "issue_authority": [
35 {
36 "word": "公安部出入境管理局"
37 }
38 ],
39 "issue_place": [
40 {
41 "word": "广东"
42 }
43 ],
44 "MRZCode": [
45 {
46 "word": "CSC000000004<2404200<8108038<6"
47 }
48 ]
49 },
50 "direction": 0,
51 "log_id": 1645241609425709013
52}