通用卡证票据识别
更新时间:2026-11-12
接口描述
支持对全品类卡证、票据的通用识别与结构化信息抽取。基于多模态文档理解技术,自动识别各类身份证件、企业资质证照、财务报销票据、行业凭证等图像内容,完成关键字段的精准提取与结构化输出,并输出卡证/票据类型。支持KV对定位信息、图片中印章信息的输出。
- 通用卡证数据识别:覆盖身份证、银行卡、驾驶证、行驶证、营业执照、护照、港澳台通行证、社保卡、户口本等常见个人及企业证照,同时支持许可证类、资质证书类等各种长尾卡证场景
- 全品类票据识别:包括但不限于增值税发票(专票、普票、电子票)、定额发票、机打发票、火车票、机票行程单、出租车票、过路过桥费发票等财务报销类票据,实现卡证、票据的一站式通用识别能力。
在线调试
您可以在 示例代码中心 中调试该接口,可进行签名验证、查看在线调用的请求内容和返回结果、示例代码的自动生成。
请求说明
请求示例
HTTP 方法: POST
请求URL: https://aip.baidubce.com/rest/2.0/ocr/v1/general_ocr
内容类型:application/x-www-form-urlencoded
URL参数:
| 参数 | 值 |
|---|---|
| access_token | 通过API Key和Secret Key获取的access_token,参考“Access Token获取” |
Header如下:
| 参数 | 值 |
|---|---|
| Content-Type | application/x-www-form-urlencoded |
Body中放置请求参数,参数详情如下:
请求参数
| 参数 | 是否必须 | 类型 | 可选值范围 | 说明 |
|---|---|---|---|---|
| image | 和 url/pdf_file/ofd_file 四选一 | string | - | 图片完整URL,URL长度不超过1024字节,URL对应的图片base64编码和urlencode后大小不超过10M,最短边至少5px,支持jpg/jpeg/png/bmp格式,当image字段存在时url字段失效优先级:image > url > pdf_file > ofd_file ,当image字段存在时,url、pdf_file、ofd_file 字段失效 |
| url | 和 image/pdf_file/ofd_file 四选一 | string | - | 图片完整url,url长度不超过1024字节,url对应的图片base64编码后大小不超过10MB,最短边至少5px,支持jpg/jpeg/png格式优先级:image > url > pdf_file > ofd_file,当image字段存在时,url字段失效请注意关闭URL防盗链 |
| pdf_file | 和 image/url/ofd_file 四选一 | string | - | PDF文件,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过10MB,最短边至少5px优先级:image > url > pdf_file > ofd_file,当image、url字段存在时,pdf_file字段失效 |
| pdf_file_num | 否 | string | - | 需要识别的PDF文件的对应页码,当 pdf_file 参数有效时,识别传入页码的对应页面内容,若不传入,则默认识别第 1 页 |
| ofd_file | 和 image/url/pdf_file 四选一 | string | - | OFD文件,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过10MB,最短边至少5px优先级:image > url > pdf_file > ofd_file,当image、url、pdf_file字段存在时,ofd_file字段失效 |
| ofd_file_num | 否 | string | - | 需要识别的OFD文件的对应页码,当 ofd_file 参数有效时,识别传入页码的对应页面内容,若不传入,则默认识别第 1 页 |
请求代码示例
提示:使用示例代码前,请记得替换其中的示例Token、task_id。
1curl -i -k 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_ocr?access_token=【调用鉴权接口获取的token】'
2 -H 'Content-Type: application/x-www-form-urlencoded'
3--data-urlencode 'image=【图片Base64编码,需UrlEncode】'
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/general_ocr"
11# 二进制方式打开图片文件
12f = open('[本地文件]', 'rb')
13img = base64.b64encode(f.read())
14
15params = {"image":img}
16access_token = '[调用鉴权接口获取的token]'
17request_url = request_url + "?access_token=" + access_token
18headers = {'content-type': 'application/x-www-form-urlencoded'}
19response = requests.post(request_url, data=params, headers=headers)
20if response:
21 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 GeneralOcr {
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 generalOcr() {
24 // 请求url
25 String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_ocr";
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;
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 GeneralOcr.generalOcr();
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/general_ocr";
7static std::string generalocr_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 generalocr_result = std::string((char *) ptr, size * nmemb);
16 return size * nmemb;
17}
18/**
19* 通用卡证票据识别
20* @return 调用成功返回0,发生错误返回其他错误码
21*/
22int generalocr(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
35 curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
36 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
37 result_code = curl_easy_perform(curl);
38 if (result_code != CURLE_OK) {
39 fprintf(stderr, "curl_easy_perform() failed: %s\n",
40 curl_easy_strerror(result_code));
41 is_success = 1;
42 return is_success;
43 }
44 json_result = generalocr_result;
45 curl_easy_cleanup(curl);
46 is_success = 0;
47 } else {
48 fprintf(stderr, "curl_easy_init() failed.");
49 is_success = 1;
50 }
51 return is_success;
52}
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/general_ocr?access_token=' . $token;
35$img = file_get_contents('[本地文件路径]');
36$img = base64_encode($img);
37$bodys = array(
38 'image' => $img
39);
40$res = request_post($url, $bodys);
41
42var_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 GeneralOcr
10 {
11 // 通用卡证票据识别
12 public static string generalOcr()
13 {
14 string token = "[调用鉴权接口获取的token]";
15 string host = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_ocr?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);
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}
返回说明
返回参数
| 参数 | 是否必须 | 类型 | 说明 |
|---|---|---|---|
| log_id | 是 | uint64 | 唯一的log id,用于问题定位 |
| pdf_file_size | 否 | string | 传入PDF文件的总页数,当 pdf_file 参数有效时返回该字段 |
| results | 是 | object{} | 识别结果 |
| + subimageid | 是 | integer | 图片 ID(编号从 0 开始) |
| ++ key | 是 | array[] | key为字段名称,当key存在多级时用“>>>”表示,示例“一级标题>>>二级标题” |
| +++ words | 是 | array[] | 值为key字段对应的识别结果 |
| +++ k_location | 是 | object | 字段名称key的位置信息,当字段只有值无名称时无该定位信息(坐标0点为左上角)++++ left 表示定位位置的长方形左上顶点的水平坐标++++ top表示定位位置的长方形左上顶点的垂直坐标++++ width表示定位位置的长方形的宽度++++ height表示定位位置的长方形的高度 |
| +++ v_location | 是 | object | 字段值words的位置信息(坐标0点为左上角)++++ left 表示定位位置的长方形左上顶点的水平坐标++++ top表示定位位置的长方形左上顶点的垂直坐标++++ width表示定位位置的长方形的宽度++++ height表示定位位置的长方形的高度 |
| ++ table_kv_results | 否 | string | 图片中表格信息解析结果,html格式表示(注:表格区域无定位信息) |
| ++ class | 是 | string | 卡证票据类别,根据图片信息模型自动匹配类别 |
| ++ seal_result | 是 | array[] | 印章结果 |
| +++ type | 是 | string | 印章的类别,共有圆章、椭圆章、方章、骑缝章四种 |
| +++ color | 是 | string | 印章的颜色,常规有红黑蓝三种,少数情况会有其他颜色 |
| +++ seal_info | 是 | array[] | 展示印章中所有的文字及编号信息 |
| +++ location | 是 | object | 印章位置信息(坐标0点为左上角)++++ left 表示定位位置的长方形左上顶点的水平坐标++++ top表示定位位置的长方形左上顶点的垂直坐标++++ width表示定位位置的长方形的宽度++++ height表示定位位置的长方形的高度 |
返回示例
成功:
1{
2 "results": {
3 "0": {
4 "标题": [
5 {
6 "words": [
7 "低压电工证"
8 ],
9 "v_location": {
10 "top": 96,
11 "left": 394,
12 "width": 108,
13 "height": 23
14 }
15 }
16 ],
17 "证号": [
18 {
19 "words": [
20 "T3204811998063035170"
21 ],
22 "v_location": {
23 "top": 216,
24 "left": 328,
25 "width": 340,
26 "height": 28
27 },
28 "k_location": {
29 "top": 195,
30 "left": 324,
31 "width": 28,
32 "height": 16
33 }
34 }
35 ],
36 "姓名": [
37 {
38 "words": [
39 "张敬"
40 ],
41 "k_location": {
42 "top": 253,
43 "left": 325,
44 "width": 29,
45 "height": 16
46 }
47 }
48 ],
49 "性别": [
50 {
51 "words": [
52 "男"
53 ],
54 "v_location": {
55 "top": 354,
56 "left": 326,
57 "width": 22,
58 "height": 23
59 },
60 "k_location": {
61 "top": 331,
62 "left": 326,
63 "width": 29,
64 "height": 16
65 }
66 }
67 ],
68 "作业类别": [
69 {
70 "words": [
71 "电工作业"
72 ],
73 "v_location": {
74 "top": 274,
75 "left": 541,
76 "width": 82,
77 "height": 21
78 },
79 "k_location": {
80 "top": 251,
81 "left": 542,
82 "width": 59,
83 "height": 16
84 }
85 }
86 ],
87 "操作项目": [
88 {
89 "words": [
90 "低压电工作业"
91 ],
92 "v_location": {
93 "top": 352,
94 "left": 541,
95 "width": 125,
96 "height": 22
97 },
98 "k_location": {
99 "top": 329,
100 "left": 542,
101 "width": 59,
102 "height": 17
103 }
104 }
105 ],
106 "初领日期": [
107 {
108 "words": [
109 "2021-10-08"
110 ],
111 "v_location": {
112 "top": 471,
113 "left": 138,
114 "width": 121,
115 "height": 19
116 },
117 "k_location": {
118 "top": 448,
119 "left": 138,
120 "width": 59,
121 "height": 16
122 }
123 }
124 ],
125 "有效期限": [
126 {
127 "words": [
128 "2021-10-08至2027-10-07"
129 ],
130 "v_location": {
131 "top": 466,
132 "left": 311,
133 "width": 273,
134 "height": 22
135 },
136 "k_location": {
137 "top": 446,
138 "left": 312,
139 "width": 58,
140 "height": 17
141 }
142 }
143 ],
144 "应复审日期": [
145 {
146 "words": [
147 "2024-10-07前"
148 ],
149 "v_location": {
150 "top": 527,
151 "left": 139,
152 "width": 139,
153 "height": 21
154 },
155 "k_location": {
156 "top": 507,
157 "left": 139,
158 "width": 73,
159 "height": 16
160 }
161 }
162 ],
163 "签发机关": [
164 {
165 "words": [
166 "江苏省应急管理厅"
167 ],
168 "v_location": {
169 "top": 524,
170 "left": 312,
171 "width": 165,
172 "height": 22
173 },
174 "k_location": {
175 "top": 505,
176 "left": 312,
177 "width": 59,
178 "height": 17
179 }
180 }
181 ],
182 "class": "低压电工证",
183 "table_kv_results": [],
184 "seal_result": []
185 },
186 "1": {
187 "证号": [
188 {
189 "words": [
190 "T32048119940824381X"
191 ],
192 "v_location": {
193 "top": 685,
194 "left": 329,
195 "width": 327,
196 "height": 25
197 },
198 "k_location": {
199 "top": 662,
200 "left": 327,
201 "width": 29,
202 "height": 16
203 }
204 }
205 ],
206 "姓名": [
207 {
208 "words": [
209 "常鹏"
210 ],
211 "v_location": {
212 "top": 744,
213 "left": 327,
214 "width": 44,
215 "height": 22
216 },
217 "k_location": {
218 "top": 720,
219 "left": 326,
220 "width": 30,
221 "height": 16
222 }
223 }
224 ],
225 "性别": [
226 {
227 "words": [
228 "男"
229 ],
230 "v_location": {
231 "top": 354,
232 "left": 326,
233 "width": 22,
234 "height": 23
235 },
236 "k_location": {
237 "top": 331,
238 "left": 326,
239 "width": 29,
240 "height": 16
241 }
242 }
243 ],
244 "作业类别": [
245 {
246 "words": [
247 "电工作业"
248 ],
249 "v_location": {
250 "top": 274,
251 "left": 541,
252 "width": 82,
253 "height": 21
254 },
255 "k_location": {
256 "top": 251,
257 "left": 542,
258 "width": 59,
259 "height": 16
260 }
261 }
262 ],
263 "操作项目": [
264 {
265 "words": [
266 "低压电工作业"
267 ],
268 "v_location": {
269 "top": 352,
270 "left": 541,
271 "width": 125,
272 "height": 22
273 },
274 "k_location": {
275 "top": 329,
276 "left": 542,
277 "width": 59,
278 "height": 17
279 }
280 }
281 ],
282 "初领日期": [
283 {
284 "words": [
285 "2021-10-08"
286 ],
287 "v_location": {
288 "top": 471,
289 "left": 138,
290 "width": 121,
291 "height": 19
292 },
293 "k_location": {
294 "top": 448,
295 "left": 138,
296 "width": 59,
297 "height": 16
298 }
299 }
300 ],
301 "有效期限": [
302 {
303 "words": [
304 "2021-10-08至2027-10-07"
305 ],
306 "v_location": {
307 "top": 466,
308 "left": 311,
309 "width": 273,
310 "height": 22
311 },
312 "k_location": {
313 "top": 446,
314 "left": 312,
315 "width": 58,
316 "height": 17
317 }
318 }
319 ],
320 "应复审日期": [
321 {
322 "words": [
323 "2024-10-07前"
324 ],
325 "v_location": {
326 "top": 527,
327 "left": 139,
328 "width": 139,
329 "height": 21
330 },
331 "k_location": {
332 "top": 507,
333 "left": 139,
334 "width": 73,
335 "height": 16
336 }
337 }
338 ],
339 "签发机关": [
340 {
341 "words": [
342 "江苏省应急管理厅"
343 ],
344 "v_location": {
345 "top": 524,
346 "left": 312,
347 "width": 165,
348 "height": 22
349 },
350 "k_location": {
351 "top": 505,
352 "left": 312,
353 "width": 59,
354 "height": 17
355 }
356 }
357 ],
358 "class": "低压电工证",
359 "table_kv_results": [],
360 "seal_result": []
361 }
362 },
363 "log_id": 2120796905211276616
364}
评价此篇文章
