1.文档提取接口说明
文档提取接口(API)定义:支持图片/PDF 文档信息提取,可根据提示词智能提取文档内容,返回结构化结果。
1.1主要功能
文档提取接口能够识别并提取图片/PDF中的文档信息。其主要功能包括:
- 智能提取:
- 支持根据提示词智能提取文档中的关键信息。
- 多格式支持:
- 支持图片和PDF格式的文档提取。
- 多页处理:
- 支持多页PDF文档的批量提取处理。
- 稳定高效:
- 提供稳定的服务,响应及时,处理速度快。
1.2API接入
支持各种程序和设备接入,包括小程序、APP、采集设备等,灵活适用于不同应用场景。
2.请求信息
2.1请求地址(URL)
POST https://ocr-api.shiliuai.com/api/doc_extract/v1
2.2请求方式
POST
2.3请求头(header)
| 参数 | 类型 | 说明 |
|---|---|---|
| Authorization | string | 'APPCODE ' + 您的AppCode(注意英文空格) |
| Content-Type | string | application/json |
| 参数 | 类型 | 说明 |
|---|---|---|
| Content-Type | string | application/json |
| x-ca-key | string | 您的AppKey |
| x-ca-timestamp | string | 时间戳(毫秒) |
| x-ca-signature | string | 签名sign |
str = app_key×tamp&app_secret sign = md5(str)
2.4请求体(body)
| 参数 | 是否必填 | 类型 | 说明 |
|---|---|---|---|
| file_base64 | 必填 | string | base64编码的图片文件或PDF文件 |
| prompt | 选填 | string | 关于文档信息提取的提示词,比如:"以json格式输出文档内容。",默认以text形式输出 |
3.返回信息
3.1返回类型
JSON
3.2返回码
| 参数名 | 类型 | 说明 |
|---|---|---|
| code | int | 返回码,200表示成功 |
| msg | string | 返回信息 |
3.3返回信息
| 参数名 | 类型 | 说明 |
|---|---|---|
| code | int | 错误码 |
| msg | string | 错误信息(英文) |
| msg_cn | string | 错误信息(中文) |
| success | bool | 识别是否成功 |
| file_id | string | 请求文件ID |
| request_id | string | 唯一请求ID |
| data | object | 具体见返回示例 |
3.4返回示例
成功返回示例
成功示例:
{
'code': 200,
'msg': 'OK',
'msg_cn': '成功',
'success': True,
'file_id': file id,
'request_id': request id,
'data': {
"page_count": 5, // int, 文件页面总数
"process_pages": 5, // int, 处理页面数
"content": content // 文档提取结果
}
}
错误返回示例
失败示例:
{
'code': error code,
'msg': error message,
'msg_cn': 中文错误信息,
'success': False,
'file_id': file id,
'request_id': request id,
'data': {}
}
3.5错误码
| 错误码 | 说明 |
|---|---|
| 200 | 成功 |
| 400 | 请求错误 |
| 401 | 未授权 |
| 500 | 服务错误 |
具体错误原因请看msg或msg_cn
4.示例代码
4.1 Python
同步调用
# -*- coding: utf-8 -*-
import requests
import base64
import json
# 请求接口
URL = "https://ocr-api.shiliuai.com/api/doc_extract/v1"
# 图片/pdf文件转base64
def get_base64(file_path):
with open(file_path, "rb") as f:
data = f.read()
return base64.b64encode(data).decode("utf8")
def demo(appcode, file_path):
# 请求头
headers = {
"Authorization": "APPCODE %s" % appcode,
"Content-Type": "application/json"
}
# 请求体
b64 = get_base64(file_path)
data = {
"file_base64": b64,
"prompt": ""
}
# 请求
response = requests.post(url=URL, headers=headers, json=data)
content = json.loads(response.content)
print(content)
if __name__ == "__main__":
appcode = "你的APPCODE"
file_path = "本地文件路径"
demo(appcode, file_path)
# -*- coding: utf-8 -*-
import requests
import base64
import json
import hashlib
import time
# 请求接口
URL = "https://ocr-api.shiliuai.com/api/doc_extract/v1"
# 图片/pdf文件转base64
def get_base64(file_path):
with open(file_path, "rb") as f:
data = f.read()
return base64.b64encode(data).decode("utf8")
# md5
def md5(s):
return hashlib.md5(s.encode("utf8")).hexdigest()
def demo(app_key, app_secret, file_path):
# 请求头(签名)
t = int(time.time() * 1000)
s = "%s&%s&%s" % (app_key, t, app_secret)
sign = md5(s)
headers = {
"x-ca-key": app_key,
"x-ca-timestamp": str(t),
"x-ca-signature": sign,
"Content-Type": "application/json"
}
# 请求体
b64 = get_base64(file_path)
data = {
"file_base64": b64,
"prompt": ""
}
# 请求
response = requests.post(url=URL, headers=headers, json=data)
content = json.loads(response.content)
print(content)
if __name__ == "__main__":
app_key = "你的APP_KEY"
app_secret = "你的APP_SECRET"
file_path = "本地文件路径"
demo(app_key, app_secret, file_path)