资讯 文档
技术能力
语音技术
文字识别
人脸与人体
图像技术
语言与知识
视频技术

API文档

申请试用

该接口正在邀测中,请您先提交 合作咨询提交工单,提供公司名称、appid、应用场景等信息,工作人员协助开通权限后方可使用。

接口描述

iOCR 全场景识别采用 MoE 混合专家架构,无需分类调用、无需定制训练,一个接口实现全场景结构化识别。基于意图识别模型,自动将同一任务中的不同类型文件分发至对应专家模型,已预置 10 余种 OCR 识别模型与通用抽取大模型,同时支持自定义文档抽取模型,不限类型与版式,轻松提取复杂文档关键字段。

登录 iOCR 全场景识别平台,可在示例项目空间快速体验,也可创建您专属的项目空间与抽取模型,平台和接口服务的额度共享互通。

本服务为异步接口,包含提交请求、获取结果两个子接口,详情如下:

  • iOCR 全场景识别-提交请求:传入图片/PDF、项目空间ID等参数,创建识别任务并获取任务ID,该接口支持2QPS;
  • iOCR 全场景识别-获取结果:成功创建任务后,传入任务ID获取识别结果,该接口支持10QPS。可调用获取结果接口进行结果轮询,建议提交请求后30秒开始轮询。

iOCR 全场景识别-提交请求

请求说明

请求示例

HTTP 方法:POST

请求URL: https://aip.baidubce.com/rest/2.0/solution/v1/iocr/recognise/universal/request

URL参数:

参数
access_token 通过API Key和Secret Key获取的access_token,参考“Access Token获取

Header如下:

参数
Content-Type application/x-www-form-urlencoded

Body中放置请求参数,参数详情如下:

请求参数

参数 是否必选 类型 可选值范围 说明
file_name string - 文件名称,例如:通过pdf_file传入一份名为test的文件,该字段需传入test.pdf。
注意: 文件名称后缀需与文件实际类型一致
image 和url/pdf_file/ofd_file四选一 string - 图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过10M,最短边至少15px,最长边最大8192px,支持jpg/jpeg/png/bmp格式
优先级: image>url>pdf_file>ofd_file,当image字段存在时,url、pdf_file、ofd_file字段失效
url 和image/pdf_file/ofd_file 四选一 string - 图片完整URL,URL长度不超过1024字节,URL对应的图片要求base64编码和urlencode后大小不超过10M,最短边至少15px,最长边最大8192px
优先级: image>url>pdf_file>ofd_file,当image字段存在时,url字段失效
注意: 请关闭URL防盗链
pdf_file 和image/url/ofd_file四选一 string - PDF文件,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过10M,最短边至少15px,最长边最大8192px。若PDF存在多页,默认识别全部页
优先级: image>url>pdf_file>ofd_file,当image、url字段存在时,pdf_file字段失效
ofd_file 和image/url/pdf_file四选一 string - OFD文件,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过10M,最短边至少15px,最长边最大8192px。若OFD存在多页,默认识别全部页
优先级: image>url>pdf_file>ofd_file,当image、url、pdf_file字段存在时,ofd_file字段失效
templateSign 和 classifierId 二选一 string - 文件类型 ID,自定义模型或预置模型的唯一标示,可用于调用指定的识别模型进行结构化识别。
自定义模型 ID: 可在「模型管理」页查看并复制使用
预置模型 ID: 可在「预置模型清单」查看并复制使用
classifierId 和 templateSign 二选一 string - 项目空间 ID,项目空间识别规则的唯一标示,可用于调用指定项目空间的识别规则,对传入的图片自动分类及识别,可在「项目空间」查看并复制使用。
优先级: templateSign>classfierId,当templateSign字段存在时,classfierId字段失效

请求代码示例

提示一:使用示例代码前,请记得替换其中的示例Token、文件地址等信息

提示二:目前仅提供Python语言

import base64
import requests
import os
'''
iOCR 全场景识别-提交请求
'''
file_path = '[本地文件]'
request_host = "https://aip.baidubce.com/rest/2.0/solution/v1/iocr/recognise/universal/request"
encoded_string = ''
with open(file_path, 'rb') as file:
    file_base64 = base64.b64encode(file.read()).decode('utf-8')
# 优先级:templateSign>classifierId,当templateSign字段存在时,classifierId字段失效
data = {
    'image': file_base64,
    'fileName': os.path.basename(file_path),
    "classifierId":"project-yht3ia44fyixcwm2",
    "templateSign":"id_card_font"    
    
}
access_token = '[调用鉴权接口获取的token]'
request_url = request_host + "?access_token=" + access_token
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, headers=headers, data=data)
if response:
    print(response.json())

返回说明

返回参数

字段 类型 说明
log_id string 唯一的 log_id,用于问题定位
error_code int 错误码
error_msg string 错误码描述信息
error_explain string 错误码详细中文描述
result dict 返回的结果列表
+ task_id string 该请求生成的task_id,可使用该task_id获取识别结果

返回示例

成功返回示例

{
    "error_code": 0,
    "error_msg": "",
    "log_id": "3599798266",
    "task_result": {
        "task_id": "task-2m8g3dhqmrv9cxfe"
    }
}

失败返回示例(错误码说明详见API文档-错误码):

{
    "error_code": 283016,
    "error_msg": "parameters value error",
    "error_explain": "classifier_id和template_sign不能同时为空",
    "log_id": "3599798268",
    "data": null
}

iOCR 全场景识别-获取结果

请求说明

请求示例

HTTP 方法:POST 请求URL:https://aip.baidubce.com/rest/2.0/solution/v1/iocr/recognise/universal/get_result

URL参数:

参数
access_token 通过API Key和Secret Key获取的access_token,参考“Access Token获取

Header如下:

参数
Content-Type application/x-www-form-urlencoded

Body中放置请求参数,参数详情如下:

请求参数

参数 是否必选 类型 说明
task_id string 发送提交请求时返回的 task_id

请求代码示例

提示一:使用示例代码前,请记得替换其中的示例Token、task_id信息

提示二:目前仅提供Python语言

import requests
'''
iOCR 全场景识别-获取结果
'''

request_host = "https://aip.baidubce.com/rest/2.0/solution/v1/iocr/recognise/universal/get_result"
task_id = "task-xxxx"
access_token = "[调用鉴权接口获取的token]"
request_url = request_host + "?access_token=" + access_token
data={
   "task_id":"task-111"
}
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=data, headers=headers)
if response:
    print(response.json())

返回说明

返回参数

字段 类型 说明
log_id uint64 唯一的log id,用于问题定位
error_code int 错误码
error_msg string 错误描述信息
task_result dict 任务详情列表
+ task_id string 任务id
+ status string 任务状态,Success:成功;Running:处理中;Failed:失败
+ created int 创建时间,格式为:YYYY-MM-DD nn:nn:nn(UTC/GMT+08:00 东八区)
+ started int 开始时间,格式为:YYYY-MM-DD nn:nn:nn(UTC/GMT+08:00 东八区)
+ finished int 完成时间,格式为:YYYY-MM-DD nn:nn:nn(UTC/GMT+08:00 东八区)
+ duration int 处理时长,单位为秒(s)
words_result_num int 识别结果数量,表示word_result结果数量
words_result list 识别结果列表
+ doc_type string 文件类型
+ page_num int 页码信息,页码从1开始
+ doc_location dict 文件位置信息
++ box [4][2]int 四角点坐标框,[[x1,y1],[x2,y2],[,x3,y3],[x4,y4]],分别对应坐标框四个点的坐标
++ cbox array 坐标框,「x, y, w, h」(x, y)为坐标点坐标,w为box宽度,h为box高度(以页面坐标为原点)
+ kv_result dict 结构化识别结果,包含抽取字段和结果信息
++ single_key dict 单个字段抽取结果
+++ {key} []dict 单个字段名称
++++ words string 单个字段抽取结果
++++ location []dict 抽取内容信息
+++++ box [4][2]int 四角点坐标框,[[x1,y1],[x2,y2],[,x3,y3],[x4,y4]],分别对应坐标框四个点的坐标
+++++ cbox [4]int 坐标框,「x, y, w, h」(x, y)为坐标点坐标,w为box宽度,h为box高度(以页面坐标为原点)
++ combo_key dict 组合字段抽取结果
+++ {root_key} []dict 父字段名称
++++ {leaf_key} []dict 子字段名称
+++++ words string 子字段抽取结果
+++++ location []dict 抽取内容信息
++++++ box [4][2]int 四角点坐标框,[[x1,y1],[x2,y2],[,x3,y3],[x4,y4]],分别对应坐标框四个点的坐标
++++++ cbox [4]int 坐标框,「x, y, w, h」(x, y)为坐标点坐标,w为box宽度,h为box高度(以页面坐标为原点)
++ full_result dict 全文识别结果,仅在doc_type为"其他",且识别规则兜底策略选择“全文识别”时返回
++ other_result dict 其他识别结果,如卡证质量、风险检测结果

返回示例

识别成功示例

{
    "error_code": 0,
    "error_msg": "",
    "log_id": "2761737701",
    "task_result": {
        "task_id": "task-2m8g3dhqmrv9cxfe",
        "status": "Success",
        "created": "2025-03-17 15:37:55",
        "started": "2025-03-17 15:37:55",
        "finished": "2025-03-17 15:38:02",
        "duration": 7
    },
    "words_result_num": 1,
    "words_result": [
        {
            "page_num": 1,
            "doc_type": "其他",
            "doc_location": null,
            "kv_result": {
                "single_key": {
                    "价税合计(大写)": [
                        {
                            "words": "叁佰陆拾圆整",
                            "location": {
                                "box": null,
                                "cbox": null
                            }
                        }
                    ],
                    "价税合计(小写)": [
                        {
                            "words": "¥360.00",
                            "location": {
                                "box": null,
                                "cbox": null
                            }
                        }
                    ],
                    "发票号码": [
                        {
                            "words": "3321192130",
                            "location": {
                                "box": null,
                                "cbox": null
                            }
                        },
                        {
                            "words": "No 07285251",
                            "location": {
                                "box": null,
                                "cbox": null
                            }
                        }
                    ],
                    "名称": [
                        {
                            "words": "百度在线网络技术(北京)有限公司",
                            "location": {
                                "box": null,
                                "cbox": null
                            }
                        },
                        {
                            "words": "阿里云计算有限公司",
                            "location": {
                                "box": null,
                                "cbox": null
                            }
                        }
                    ],
                    "地址、电话": [
                        {
                            "words": "北京市海淀区上地十街10号百度大厦三层",
                            "location": {
                                "box": null,
                                "cbox": null
                            }
                        },
                        {
                            "words": "杭州市转塘科技经济区块16号8幢 0571-85022088",
                            "location": {
                                "box": null,
                                "cbox": null
                            }
                        }
                    ],
                    "开户行及账号": [
                        {
                            "words": "招商银行北京分行大屯路支行866180100210002",
                            "location": {
                                "box": null,
                                "cbox": null
                            }
                        },
                        {
                            "words": "招商银行杭州高新支行502905023610702",
                            "location": {
                                "box": null,
                                "cbox": null
                            }
                        }
                    ],
                    "开票日期": [
                        {
                            "words": "2019年08月28日",
                            "location": {
                                "box": null,
                                "cbox": null
                            }
                        }
                    ],
                    "纳税人识别号": [
                        {
                            "words": "91110911717743469K",
                            "location": {
                                "box": null,
                                "cbox": null
                            }
                        },
                        {
                            "words": "91330106673959654P",
                            "location": {
                                "box": null,
                                "cbox": null
                            }
                        }
                    ]
                },
                "combo_key": {
                    "货物或应税劳务、服务信息": [
                        {
                            "单价": {
                                "words": "339. 62",
                                "location": {
                                    "box": null,
                                    "cbox": null
                                }
                            },
                            "单位": {
                                "words": "",
                                "location": {
                                    "box": null,
                                    "cbox": null
                                }
                            },
                            "数量": {
                                "words": "1",
                                "location": {
                                    "box": null,
                                    "cbox": null
                                }
                            },
                            "税率": {
                                "words": "",
                                "location": {
                                    "box": null,
                                    "cbox": null
                                }
                            },
                            "税额": {
                                "words": "20. 38",
                                "location": {
                                    "box": null,
                                    "cbox": null
                                }
                            },
                            "规格型号": {
                                "words": "",
                                "location": {
                                    "box": null,
                                    "cbox": null
                                }
                            },
                            "货物或应税劳务、服务名称": {
                                "words": "*信息技术服务*软件服务费",
                                "location": {
                                    "box": null,
                                    "cbox": null
                                }
                            },
                            "金额": {
                                "words": "339. 62",
                                "location": {
                                    "box": null,
                                    "cbox": null
                                }
                            }
                        }
                    ],
                    "购买方扣税凭证信息": [
                        {
                            "单价": {
                                "words": "2.30",
                                "location": {
                                    "box": null,
                                    "cbox": null
                                }
                            },
                            "合计": {
                                "words": "¥339.62",
                                "location": {
                                    "box": null,
                                    "cbox": null
                                }
                            },
                            "时间": {
                                "words": "18:50-17:06",
                                "location": {
                                    "box": null,
                                    "cbox": null
                                }
                            },
                            "油附加费": {
                                "words": "¥1.00",
                                "location": {
                                    "box": null,
                                    "cbox": null
                                }
                            },
                            "税额": {
                                "words": "Y20.38",
                                "location": {
                                    "box": null,
                                    "cbox": null
                                }
                            },
                            "证号": {
                                "words": "244926",
                                "location": {
                                    "box": null,
                                    "cbox": null
                                }
                            },
                            "车号": {
                                "words": "京B.26353",
                                "location": {
                                    "box": null,
                                    "cbox": null
                                }
                            },
                            "里程": {
                                "words": "6.0",
                                "location": {
                                    "box": null,
                                    "cbox": null
                                }
                            }
                        }
                    ]
                }
            }
        }
    ]
}

任务运行中示例:

{
    "error_code": 0,
    "error_msg": "",
    "log_id": "3739485273",
    "task_result": {
        "task_id": "task-q0zaegy82ujqe6qc",
        "status": "Running",
        "reason": "",
        "created": "2025-03-17 17:29:38",
        "started": "2025-03-17 17:29:38",
        "finished": "",
        "duration": 5
    },
    "words_result": null
}

预置模型清单

序号 文件类型 文件类型 ID
(通过templateSign传入)
默认请求参数 备注
1 身份证正面 id_card_front id_card_side = front
detect_ps = true
detect_risk = true
detect_quality = true
detect_photo = true
detect_card = false
detect_direction = true
即人像面,返参含义详见 API文档-身份证识别
2 身份证反面 id_card_back id_card_side = back
detect_ps = true
detect_risk = true
detect_quality = true
detect_photo = true
detect_card = false
detect_direction = true
即国徽面,返参含义详见 API文档-身份证识别
3 行驶证正页 vehicle_license detect_direction = true
vehicle_license_side = front
unified = false
quality_warn = true
risk_warn = true
返参含义详见 API文档-行驶证识别
4 行驶证副页 vehicle_license_back detect_direction = true
vehicle_license_side = back
unified = false
quality_warn = true
risk_warn = true
返参含义详见 API文档-行驶证识别
5 驾驶证正页 driver_license detect_direction = true
driving_license_side = front
unified_valid_period = false
quality_warn = true
risk_warn = true
支持电子驾驶证正页,返参含义详见 API文档-驾驶证识别
6 驾驶证副页 driver_license_back detect_direction = true
driving_license_side = back
unified_valid_period = false
quality_warn = true
risk_warn = true
返参含义详见 API文档-驾驶证识别
7 银行卡 bank_card location = true
detect_quality = true
返参含义详见 API文档-银行卡识别
8 大陆护照 chinese_passport / 返参含义详见 API文档-护照识别
9 户口本登记页 household_register_subpage household_register_side = subpage 返参含义详见 API文档-户口本识别
10 户口本户主页 household_register_homepage household_register_side = homepage 返参含义详见 API文档-户口本识别
11 社保卡 social_security_card / 返参含义详见 API文档-社保卡识别
12 房产证 real_estate_certificate probability = true
location = true
返参含义详见 API文档-房产证识别
13 财务票据 multiple_invoice probability = true
location = true
verify_parameter = false
支持增值税发票、卷票、机打发票等13类票据,返参含义详见 API文档-智能财务票据识别
14 营业执照 biz_license risk_warn= true
detect_quality = true
返参含义详见 API文档-营业执照
15 港澳居民来往内地通行证正面 hk_mc_return_passport_front exitentrypermit_type = hk_mc_return_passport_front
probability = true
location = true
即人像面,返参含义详见 API文档-港澳台证件识别
16 港澳居民来往内地通行证反面 hk_mc_return_passport_back exitentrypermit_type = hk_mc_return_passport_back
probability = true
location = true
返参含义详见 API文档-港澳台证件识别
17 港澳通行证正面 hk_mc_passport_front exitentrypermit_type = hk_mc_passport_front
probability = true
location = true
即人像面,返参含义详见 API文档-港澳台证件识别
18 港澳通行证反面 hk_mc_passport_back exitentrypermit_type = hk_mc_passport_back
probability = true
location = true
返参含义详见 API文档-港澳台证件识别
19 台湾居民来往大陆通行证正面 tw_return_passport_front exitentrypermit_type = tw_return_passport_front
probability = true
location = true
即人像面,返参含义详见 API文档-港澳台证件识别
20 台湾居民来往大陆通行证反面 tw_return_passport_back exitentrypermit_type = tw_return_passport_back
probability = true
location = true
返参含义详见 API文档-港澳台证件识别
21 台湾通行证正面 tw_passport_front exitentrypermit_type = tw_passport_front
probability = true
location = true
即人像面,返参含义详见 API文档-港澳台证件识别
22 台湾通行证反面 tw_passport_back exitentrypermit_type = tw_passport_back
probability = true
location = true
返参含义详见 API文档-港澳台证件识别
23 车辆合格证 vehicle_certificate / 返参详见 API文档-车辆合格证识别
24 快递面单 waybill is_identify_virtual_waybill = true 返参详见 API文档-快递面单识别
25 车牌 license_plate multi_detect = true
multi_scale = false
detect_complete = true
detect_risk = true
返参详见 API文档-车牌识别
上一篇
简介
下一篇
iOCR通用版