通用卡证票据识别
更新时间: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。
curl -i -k 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_ocr?access_token=【调用鉴权接口获取的token】'
-H 'Content-Type: application/x-www-form-urlencoded'
--data-urlencode 'image=【图片Base64编码,需UrlEncode】'# encoding:utf-8
import requests
import base64
'''
通用卡证票据识别
'''
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_ocr"
# 二进制方式打开图片文件
f = open('[本地文件]', 'rb')
img = base64.b64encode(f.read())
params = {"image":img}
access_token = '[调用鉴权接口获取的token]'
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
print (response.json())package com.baidu.ai.aip;
import com.baidu.ai.aip.utils.Base64Util;
import com.baidu.ai.aip.utils.FileUtil;
import com.baidu.ai.aip.utils.HttpUtil;
import java.net.URLEncoder;
/**
* 通用卡证票据识别
*/
public class GeneralOcr {
/**
* 重要提示代码中所需工具类
* FileUtil,Base64Util,HttpUtil,GsonUtils请从
* https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
* https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
* https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
* https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
* 下载
*/
public static String generalOcr() {
// 请求url
String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_ocr";
try {
// 本地文件路径
String filePath = "[本地文件路径]";
byte[] imgData = FileUtil.readFileByBytes(filePath);
String imgStr = Base64Util.encode(imgData);
String imgParam = URLEncoder.encode(imgStr, "UTF-8");
String param = "image=" + imgParam;
// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
String accessToken = "[调用鉴权接口获取的token]";
String result = HttpUtil.post(url, accessToken, param);
System.out.println(result);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
GeneralOcr.generalOcr();
}
}#include <iostream>
#include <curl/curl.h>
// libcurl库下载链接:https://curl.haxx.se/download.html
// jsoncpp库下载链接:https://github.com/open-source-parsers/jsoncpp/
const static std::string request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_ocr";
static std::string generalocr_result;
/**
* curl发送http请求调用的回调函数,回调函数中对返回的json格式的body进行了解析,解析结果储存在全局的静态变量当中
* @param 参数定义见libcurl文档
* @return 返回值定义见libcurl文档
*/
static size_t callback(void *ptr, size_t size, size_t nmemb, void *stream) {
// 获取到的body存放在ptr中,先将其转换为string格式
generalocr_result = std::string((char *) ptr, size * nmemb);
return size * nmemb;
}
/**
* 通用卡证票据识别
* @return 调用成功返回0,发生错误返回其他错误码
*/
int generalocr(std::string &json_result, const std::string &access_token) {
std::string url = request_url + "?access_token=" + access_token;
CURL *curl = NULL;
CURLcode result_code;
int is_success;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.data());
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_httppost *post = NULL;
curl_httppost *last = NULL;
curl_formadd(&post, &last, CURLFORM_COPYNAME, "image", CURLFORM_COPYCONTENTS, "【base64_img】", CURLFORM_END);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
result_code = curl_easy_perform(curl);
if (result_code != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(result_code));
is_success = 1;
return is_success;
}
json_result = generalocr_result;
curl_easy_cleanup(curl);
is_success = 0;
} else {
fprintf(stderr, "curl_easy_init() failed.");
is_success = 1;
}
return is_success;
}<?php
/**
* 发起http post请求(REST API), 并获取REST请求的结果
* @param string $url
* @param string $param
* @return - http response body if succeeds, else false.
*/
function request_post($url = '', $param = '')
{
if (empty($url) || empty($param)) {
return false;
}
$postUrl = $url;
$curlPost = $param;
// 初始化curl
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $postUrl);
curl_setopt($curl, CURLOPT_HEADER, 0);
// 要求结果为字符串且输出到屏幕上
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
// post提交方式
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
// 运行curl
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
$token = '[调用鉴权接口获取的token]';
$url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_ocr?access_token=' . $token;
$img = file_get_contents('[本地文件路径]');
$img = base64_encode($img);
$bodys = array(
'image' => $img
);
$res = request_post($url, $bodys);
var_dump($res);using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
namespace com.baidu.ai
{
public class GeneralOcr
{
// 通用卡证票据识别
public static string generalOcr()
{
string token = "[调用鉴权接口获取的token]";
string host = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_ocr?access_token=" + token;
Encoding encoding = Encoding.Default;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
request.Method = "post";
request.KeepAlive = true;
// 图片的base64编码
string base64 = getFileBase64("[本地图片文件]");
String str = "image=" + HttpUtility.UrlEncode(base64);
byte[] buffer = encoding.GetBytes(str);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
string result = reader.ReadToEnd();
Console.WriteLine("通用卡证票据识别:");
Console.WriteLine(result);
return result;
}
public static String getFileBase64(String fileName) {
FileStream filestream = new FileStream(fileName, FileMode.Open);
byte[] arr = new byte[filestream.Length];
filestream.Read(arr, 0, (int)filestream.Length);
string baser64 = Convert.ToBase64String(arr);
filestream.Close();
return baser64;
}
}
}返回说明
返回参数
| 参数 | 是否必须 | 类型 | 说明 |
|---|---|---|---|
| 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表示定位位置的长方形的高度 |
返回示例
{
"results": {
"0": {
"标题": [
{
"words": [
"低压电工证"
],
"v_location": {
"top": 96,
"left": 394,
"width": 108,
"height": 23
}
}
],
"证号": [
{
"words": [
"T3204811998063035170"
],
"v_location": {
"top": 216,
"left": 328,
"width": 340,
"height": 28
},
"k_location": {
"top": 195,
"left": 324,
"width": 28,
"height": 16
}
}
],
"姓名": [
{
"words": [
"张敬"
],
"k_location": {
"top": 253,
"left": 325,
"width": 29,
"height": 16
}
}
],
"性别": [
{
"words": [
"男"
],
"v_location": {
"top": 354,
"left": 326,
"width": 22,
"height": 23
},
"k_location": {
"top": 331,
"left": 326,
"width": 29,
"height": 16
}
}
],
"作业类别": [
{
"words": [
"电工作业"
],
"v_location": {
"top": 274,
"left": 541,
"width": 82,
"height": 21
},
"k_location": {
"top": 251,
"left": 542,
"width": 59,
"height": 16
}
}
],
"操作项目": [
{
"words": [
"低压电工作业"
],
"v_location": {
"top": 352,
"left": 541,
"width": 125,
"height": 22
},
"k_location": {
"top": 329,
"left": 542,
"width": 59,
"height": 17
}
}
],
"初领日期": [
{
"words": [
"2021-10-08"
],
"v_location": {
"top": 471,
"left": 138,
"width": 121,
"height": 19
},
"k_location": {
"top": 448,
"left": 138,
"width": 59,
"height": 16
}
}
],
"有效期限": [
{
"words": [
"2021-10-08至2027-10-07"
],
"v_location": {
"top": 466,
"left": 311,
"width": 273,
"height": 22
},
"k_location": {
"top": 446,
"left": 312,
"width": 58,
"height": 17
}
}
],
"应复审日期": [
{
"words": [
"2024-10-07前"
],
"v_location": {
"top": 527,
"left": 139,
"width": 139,
"height": 21
},
"k_location": {
"top": 507,
"left": 139,
"width": 73,
"height": 16
}
}
],
"签发机关": [
{
"words": [
"江苏省应急管理厅"
],
"v_location": {
"top": 524,
"left": 312,
"width": 165,
"height": 22
},
"k_location": {
"top": 505,
"left": 312,
"width": 59,
"height": 17
}
}
],
"class": "低压电工证",
"table_kv_results": [],
"seal_result": []
},
"1": {
"证号": [
{
"words": [
"T32048119940824381X"
],
"v_location": {
"top": 685,
"left": 329,
"width": 327,
"height": 25
},
"k_location": {
"top": 662,
"left": 327,
"width": 29,
"height": 16
}
}
],
"姓名": [
{
"words": [
"常鹏"
],
"v_location": {
"top": 744,
"left": 327,
"width": 44,
"height": 22
},
"k_location": {
"top": 720,
"left": 326,
"width": 30,
"height": 16
}
}
],
"性别": [
{
"words": [
"男"
],
"v_location": {
"top": 354,
"left": 326,
"width": 22,
"height": 23
},
"k_location": {
"top": 331,
"left": 326,
"width": 29,
"height": 16
}
}
],
"作业类别": [
{
"words": [
"电工作业"
],
"v_location": {
"top": 274,
"left": 541,
"width": 82,
"height": 21
},
"k_location": {
"top": 251,
"left": 542,
"width": 59,
"height": 16
}
}
],
"操作项目": [
{
"words": [
"低压电工作业"
],
"v_location": {
"top": 352,
"left": 541,
"width": 125,
"height": 22
},
"k_location": {
"top": 329,
"left": 542,
"width": 59,
"height": 17
}
}
],
"初领日期": [
{
"words": [
"2021-10-08"
],
"v_location": {
"top": 471,
"left": 138,
"width": 121,
"height": 19
},
"k_location": {
"top": 448,
"left": 138,
"width": 59,
"height": 16
}
}
],
"有效期限": [
{
"words": [
"2021-10-08至2027-10-07"
],
"v_location": {
"top": 466,
"left": 311,
"width": 273,
"height": 22
},
"k_location": {
"top": 446,
"left": 312,
"width": 58,
"height": 17
}
}
],
"应复审日期": [
{
"words": [
"2024-10-07前"
],
"v_location": {
"top": 527,
"left": 139,
"width": 139,
"height": 21
},
"k_location": {
"top": 507,
"left": 139,
"width": 73,
"height": 16
}
}
],
"签发机关": [
{
"words": [
"江苏省应急管理厅"
],
"v_location": {
"top": 524,
"left": 312,
"width": 165,
"height": 22
},
"k_location": {
"top": 505,
"left": 312,
"width": 59,
"height": 17
}
}
],
"class": "低压电工证",
"table_kv_results": [],
"seal_result": []
}
},
"log_id": 2120796905211276616
}