API Path
/aipaas/ocr/v1/identify/complexDOcr
请求协议
HTTPS
请求方法
POST
请求头部:
| 头部标签 | 必填 | 说明 | 类型 | 数据字典 | 限制 | 头部内容 | 示例 |
|---|---|---|---|---|---|---|---|
| Content-Type | 是 | application/json | [string] | application/json | application/json | ||
| X-APP-ID | 是 | 买家中心-已购能力-【X-APP-ID】 | [string] | ||||
| Device-Uuid | 否 | 设备管理-设备uuid | [string] | ||||
| Authorization | 是 | 鉴权信息 | [string] |
请求参数 Json
Object
| 参数名 | 说明 | 必填 | 类型 | 数据字典 | 限制 | 示例 |
|---|---|---|---|---|---|---|
| seqid | 请求序列号,请求的唯一标识,不能为空字符 | 是 | [string] | |||
| image | (公网用户目前只能使用base64数据)图片base64 或者图片url 或者pdf base64编码数据或者pdf url ,编码后大小不超过10M,最短边至少15px,图片分辨率不超过3840*3840,图片类型支持jpg/jpeg/png。pdf支持多页,最多支持24页,超过了设置直接返回报错,不处理。pdf文件大小不超过10M,对于图片分辨率和文件大小内部没有做强制限制,只是为了防止因分辨率过大或文件过大出现未知错误无法跟用户解释 | 是 | [string] | |||
| html_escape | 默认值为True,用于解决xss漏洞,值为True时对识别出的文本做html转义,上层调用不用担心xss漏洞问题,值为False时识别出的文本原样输出,上层调用方如果需要把识别结果展示在web页面上需要自己修复xss漏洞问题 | 否 | [boolean] | |||
| generate_image | 默认值为False,对于图片中的插图不做输出。输出图片的条件:generate_image:True output_format:1或3 图片输出的形式为png格式的base64编码,跟识别出的文本在同一个字符串中,主要用来做版式还原用 | 否 | [boolean] | |||
| output_format | 当前版本只能填0,1,3,默认值为0,输出纯文本。0:text;1:markdown;3:json,json输出时的格式说明见返回参数说明中“output_format为json时的输出文本说明”(3.0版本新加) | 否 | [int] | |||
| image_type | (公网用户目前只能使用base64数据)(3.0版本新加)对于pdf文件只能处理一页,多于一页的只处理第一页 只能填默认值0,1,2,3,默认为0 0:图片base64 1:图片url 2:pdf文件base64 3:pdf文件url | 否 | [int] | |||
| first_page_idx | (3.1版本新加),支持传入第一页的页码,返回时页码从这一页开始,默认为1,必须大于等于1,只有output_format 为3时有效(其他情况不输出页码) | 否 | [int] |
响应内容:
返回结果
> 成功 (200)
> Json
> Object
| 参数名 | 说明 | 必填 | 类型 | 数据字典 | 限制 | 示例 |
|---|---|---|---|---|---|---|
| seqid | 请求序列号,usid,请求的唯一标识,透传 | 是 | [string] | |||
| code | 状态响应码。参考状态码说明 | 是 | [string] | |||
| message | 调用结果描述。参考错误编码规范 | 是 | [string] | |||
| flag | 业务上成功或失败:1成功,0失败 | 是 | [int] | |||
| data | 是 | [object] | ||||
| data>>timeSecond | 分析耗时,单位:秒 | 是 | [double] | |||
| data>>words_result | list[string];一次输出所有结果,如果识别失败,或者图片中本身就没有文字会返回[] 返回的数组的长度为1 或0,如果output_format 选的是3,则输出的是一个json的字符串,json的格式说明见详细说明 | 是 | [array] |
成功示例[Mock API]:
{
"seqid": "zhongpingjushi",
"code": "10000",
"message": "成功",
"flag": "1",
"data": {
"timeSecond": 1,
"words_result": ["测试"]
}
}
详细说明:
复杂文档解析支持传入图片和pdf格式,对图片或者pdf中的文字进行解析,输出md、json、text三种格式。
若传入pdf,业务方需要将pdf切片成页,算法对每页的内容进行解析。
| 日期 | 版本 | 修订内容 |
|---|---|---|
| 2025-11-18 | 算法版本:V2.1.0算法接口文档版本:v3.1 | 入参image支持多页pdf、generate_image输出图片条件output_format改为1或3、新增first_page_idx字段;详细说明中output_format为json时的输出样例与字段修改; |
| 2025-09-12 | 无 | |
| 2025-08-15 | v3.0.1 | 当输出为json格式时,新增page_size和total_page_num参数返回;详细说明中增加bbox绘制示例代码 |
| 2025-08-01 | v3.0 | 支持传入图片和pdf格式、指定输出格式。入参新增generate_image、output_format和image_type,修改image字段描述;出参中增加对output_format为json时的字段说明 |
| 2025-05-20 | v1.6.5.6 | 初始化API文档 |
服务接口调用时需要严格遵循服务鉴权规则,服务调用鉴权规则请参见:开发指南 - 签名认证方式。
| 返回编码 | 返回信息 | 说明 |
|---|---|---|
| 10000 | 服务执行成功 | 正确执行并返回结果 |
| 10301 | 服务必填参数缺失 | 服务必填参数缺失或未填写 |
| 10304 | 请求参数格式或类型错误 | 服务入参格式错误,输入图片格式需为 base64 位编码的数据 |
| 10305 | 请求参数范围错误 | 上传的图片大小错误,现阶段我们支持的图片大小为:base64 编码后小于 5M,请重新上传图片 |
| 10503 | 服务连接超时 | 连接第三方服务发生超时,返回服务连接超时错误 |
| 10903 | 服务执行失败 | 服务执行失败 |
通用状态码请参考【状态码】中的【网关认证】
{
"seqid": "test_seq0",
"image": "/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgK===",
"html_escape": true,
"generate_image": false,
"output_format": 0,
"image_type": 0,
"first_page_idx":1
}
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import java.util.HashMap;
import java.util.Map;
/** 主类,用于发起HTTP请求并处理响应 */
public class Example {
public static void main(String[] args) {
example();
}
/**
* 方法中使用到的 JSONUtil、HttpRequest、HttpResponse均来自Hutool工具类。
* 具体maven依赖为:
*
* cn.hutool
* hutool-all
* 5.8.29
*
*/
public static void example() {
try {
String url = "算法调用地址";
// 设置请求头
Map headers = new HashMap();
//调用鉴权
headers.put("Content-Type", "application/json");
headers.put("X-APP-ID", "yourAppId");
headers.put("Authorization", "yourAuthorization");
// 创建请求对象
Map request = new HashMap();
request.put("seqid", "123");
request.put("image", "图片base64");
request.put("html_escape", true);
// 将请求对象转换为JsonNode
String requestString = JSONUtil.toJsonStr(request);
// 发起HTTP请求
HttpResponse response =
HttpRequest.post(url)
.headerMap(headers, true)
.body(requestString)
.execute();
// 输出响应结果
System.out.println(response.body());
} catch (Exception e) {
e.printStackTrace();
}
}
}
import json
import hashlib
import hmac
import time
import re
import urllib.parse
import requests
import warnings
from datetime import datetime
import logging
# 配置日志,设置日志级别为INFO,并指定日志格式
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# 配置环境变量
X_APP_ID = "yourAppId"
AUTHORIZATION = "yourAuthorization"
# URL 和请求数据
url = "算法调用地址" # 请求的URL
request_data = {
"seqid": "123",
"image": "图片base64",
"html_escape": true
}
# 构建请求头
headers = {
"Content-Type": "application/json",
"X-APP-ID": X_APP_ID,
"Authorization": AUTHORIZATION
}
def timeSimple(timestamp):
# 将时间戳转换为HH:MM:SS格式的时间字符串
dt_object = datetime.fromtimestamp(timestamp)
formatted_time = dt_object.strftime("%H:%M:%S")
return formatted_time
def send_request(url):
try:
start_time = time.time()
logging.info(f"请求路径: {url}")
logging.info(f"开始发送: {timeSimple(start_time)}")
with requests.post(
url, json=request_data, headers=headers, stream=True, verify=False
) as response:
first_packet_time = None
if response.status_code == 200:
logging.info(f"接受到返回: {timeSimple(time.time())}")
for chunk in response.iter_content(chunk_size=1024):
if chunk:
if first_packet_time is None:
first_packet_time = time.time()
logging.info(
f"Received chunk: {timeSimple(time.time())} {chunk.decode('utf-8')}"
)
end_time = time.time()
logging.info(f"Time to first byte (TTFB): {first_packet_time - start_time:.3f} seconds")
logging.info(f"Request completed in {end_time - start_time:.3f} seconds")
else:
logging.error(f"Request failed with status code {response.status_code}")
except Exception as e:
logging.error(f"An error occurred: {e}")
# 发送请求
send_request(url)
logging.info(f"headers = {headers}")
output_format 选的是3,则输出的是一个json的字符串,解析成json示例如下:
{
"result": {
"text": "# 四年级需要准备的美术用具: ",
"markdown": "# 四年级需要准备的美术用具:
1.",
"success_count": 1,
"pages": [
{
"page_id": 1,
"content": [
{
"pos": [
89,
73,
221,
86
],
"type": "title",
"text": "四年级需要准备的美术用具:"
},
{
"pos": [
106,
433,
435,
520
],
"type": "image",
"text": "",
"image_url": "/e35183140eddf973aabe95b2c41263952c6b29a156f00b8aa05467aaf6fa4e0b.jpg"
}
],
"status": "success",
"height": 841.92,
"structured": [],
"width":595.2
}
],
"valid_page_number": 1,
"total_page_number": 1,
"total_count": 1,
"detail": [
]
},
"metrics": [
{
"status": "success",
"dpi": 72,
"page_id": 1,
"page_image_width": 595.2,
"page_image_height": 841.92
}
],
"duration": 42393,
"message": "success",
"version": "v2.1.0"
}
| 属性名称 | 类型 | 说明 | 备注 |
|---|---|---|---|
| result | object | ||
| +text | string | 正文字符串,不包含图片,对于多页的,所有页面里边的文字都包含在此字符串中 | |
| +markdown | string | markdown格式正文字符串,包含文本和图片,图片已上传S3,这里只有图片的url,以格式呈现,对于多页的,所有页面里边的文字都包含在此字符串中,当generate_image为否时,跟text字段内容一,如果原文本有表格内容,表格内容以html格式呈现,公式是latex格式呈现 |
|
| +success_count | int | 解析成功的页数 | |
| +pages | list | ⽂档按照⻚为单位展开时, 存储每⼀⻚的详情和状态 (适⽤于PDF),数组长度跟文档总页数一致 | 部分信息跟metrics字段重复。 为兼容旧接⼝,保留metrics信息。 |
| ++page_id | int | 当前段落所在⻚码 | |
| ++content | list | 基础数据文字内容信息 | |
| +++pos | list[int] | 当前基础数据模块所在框的坐标,左上右下的坐标点 | 1. 如果输入是pdf(image_type为2或3),可直接使用 bbox 在 PDF 上绘制标注框,并输出 PDF 文件。(bbox的使用方式如下示例代码:pdf 文件使用返回的bbox绘制区域示例) 2. 如果输入是图片(image_type为0或1),可直接使用 bbox 在图像上绘制标注框,并输出图像文件。(bbox的使用方法参考如下示例代码:图片文件使用返回的bbox绘制区域示例代码) |
| +++image_url | string | 当type为image时才有此字段,存放图片上传s3后的url,跟markdown中的url一致,generate_image为否时没有此类型的字段 | |
| +++type | string | 数据类型,可能取值为:title、text、image、image_caption、table、table_caption、interline_equation | |
| +++text | string | 文本行文字内容,type为image时为空字符串 | |
| ++status | string | 表⽰当前⻚的引擎输出状态,只会输出success | |
| ++height | int | 文档页高度 | |
| ++width | int | 文档页宽度 | |
| ++structured | list | 当前返回空列表 | |
| +valid_page_number | int | 记录本次解析成功的总页数 | |
| +total_page_number | int | 输入为PDF时, 文档的总页数 | |
| +total_count | int | 解析的总页数,就是一共解析了多少页 | |
| +detail | list | 当前返回空列表 | |
| metrics | list | 每一页的信息 包括长,宽,页码信息,当前pdf页转成图片所用的dpi,长度跟页码数一致 | |
| +status | string | 当前页状态,success表示成功,其他表示错误信息 | |
| +dpi | int | 当前pdf⻚转成图⽚所⽤的dpi | |
| +page_id | int | 当前页码 | |
| +page_image_width | float | 当前段落所在⻚的图⽚宽或者pdf转成的图⽚宽 | |
| +page_image_height | float | 当前段落所在⻚的图⽚⾼或者pdf转成的图⽚⾼ | |
| duration | int | 当前文档处理总耗时,单位毫秒 | |
| message | string | 只有当算法成功输出时才有message字段,值为success | |
| version | string | 当前算法版本,返回v2.1.0 |
##pdf 文件使用返回的bbox绘制区域示例
import fitz # pip install pymupdf
# 打开 PDF
pdf_path = "example.pdf"
doc = fitz.open(pdf_path)
# 假设在第1页画框
page = doc[0]
# 需要画的框的坐标 (x0, y0, x1, y1)这个坐标就是返回的bbox
# 左上角 (x0, y0),右下角 (x1, y1),单位是 PDF 点(1点 = 1/72 英寸)
#例如bbox=[100,150,300,200]则绘制方法如下
bbox = [100,150,300,200]
rect = fitz.Rect(bbox[0], bbox[1], bbox[2], bbox[3])
# 在 PDF 上画一个红色矩形
shape = page.new_shape()
shape.draw_rect(rect)
shape.finish(color=(1, 0, 0), width=2) # RGB颜色(1,0,0) = 红色,线宽 2
shape.commit()
# 保存到新文件
doc.save("example_boxed.pdf")
doc.close()
# 坐标说明:PDF 的坐标原点在 左上角(x 向右增加,y 向下增加)
##图片文件使用返回的bbox绘制区域示例代码
from PIL import Image, ImageDraw
# 打开图像
img = Image.open("example.jpg")
draw = ImageDraw.Draw(img)
# 例如返回的bbox=[50,60,200,150]
bbox = [50, 60, 200, 150]
# 画红色矩形框
draw.rectangle(bbox, outline="red", width=3)
# 保存结果
img.save("boxed.jpg")