效果示例
下面的例子分解一张海报并获取文字图层的 OCR 结果。请求设置max_images: 15,返回 13 张图片:1 张底图 + 12 个图层,其中 9 个文字图层带 OCR 结果。
| 输入 | 文字图层 | OCR 结果 |
|---|---|---|
![]() | ![]() | 文字:第 1 行 Presented By :,第 2 行 Anna Katrina Marchesi位置(绝对像素坐标):第 1 行 [1196, 629, 1358, 658],第 2 行 [1195, 651, 1427, 681]相似字体:两行均为 Helvetica Neue 颜色:第 1 行 #193366,第 2 行 #46597E加粗:第 1 行 true,第 2 行 false |
可以下载官方的完整示例包(ZIP,约 6.7 MB),包含输入海报、底图、全部图层、OCR 数据和一个可交互页面:保持目录结构解压后,用浏览器打开
seedream-55-ocr-preview.html,选择文字图层即可查看每行的文字、坐标和样式,并实时预览修改文字、颜色和加粗的效果。开启 OCR 识别
在图层分解请求里同时设置:layer_decomposition: true:开启图层分解;layer_decomposition_options.text_recognition: true:请求返回常规文字图层的 OCR 结果。
curl -X POST "https://api.dreamto.ai/api/v3/images/generations" \
-H "Authorization: Bearer $DREAMTO_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "seedream-5.5",
"prompt": "把图片中的元素拆分成独立的图层。",
"image": "https://arkdocs-en.tos-ap-southeast-1.volces.com/images/image-generation/ocr-test2.png",
"size": "auto",
"layer_decomposition": true,
"layer_decomposition_options": {
"max_images": 15,
"text_recognition": true
},
"output_format": "png",
"response_format": "url",
"watermark": true
}'
import os
import requests
URL = "https://api.dreamto.ai/api/v3/images/generations"
HEADERS = {"Authorization": f"Bearer {os.environ['DREAMTO_API_KEY']}"}
payload = {
"model": "seedream-5.5",
"prompt": "把图片中的元素拆分成独立的图层。",
"image": "https://arkdocs-en.tos-ap-southeast-1.volces.com/images/image-generation/ocr-test2.png",
"size": "auto",
"layer_decomposition": True,
"layer_decomposition_options": {
"max_images": 15,
"text_recognition": True
},
"output_format": "png",
"response_format": "url",
"watermark": True
}
result = requests.post(URL, headers=HEADERS, json=payload, timeout=600).json()
for image in result["data"]:
print(image["url"])
import os
from openai import OpenAI
client = OpenAI(base_url="https://api.dreamto.ai/api/v3", api_key=os.environ["DREAMTO_API_KEY"])
response = client.images.generate(
model="seedream-5.5",
prompt="把图片中的元素拆分成独立的图层。",
size="auto",
output_format="png",
response_format="url",
extra_body={
"image": "https://arkdocs-en.tos-ap-southeast-1.volces.com/images/image-generation/ocr-test2.png",
"layer_decomposition": True,
"layer_decomposition_options": {
"max_images": 15,
"text_recognition": True
},
"watermark": True,
},
)
for item in response.data:
print(item)
OCR 响应字段
当前文字图层的 OCR 结果在data[].text_recognition 中返回,包含完整纯文本,以及按文字行和样式片段组织的位置、文字和样式信息。
| 字段 | 说明 |
|---|---|
text_recognition | 当前文字图层的富文本 OCR 结果。 |
text_recognition.text | 完整纯文本,按从上到下、从左到右排列,行之间用 \n 分隔。 |
text_recognition.lines[] | 按顺序排列的文字行,每行包含位置和样式片段。 |
lines[].bounding_box | 该行在输出底图坐标系中的边界框,包含绝对坐标 absolute 和归一化坐标 normalized,用于把文字还原到底图或目标画布上的对应位置。 |
bounding_box.absolute | 边界框的绝对像素坐标 [左, 上, 右, 下],输出底图左上角为 (0, 0)。 |
bounding_box.normalized | 按输出底图宽高换算成 0 ~ 1000 的整数坐标 [左, 上, 右, 下],用于计算文字行在目标画布上的位置。 |
lines[].runs[] | 该行的样式片段,每个片段内样式一致:整行样式相同时只有一个片段,行内字体、颜色、加粗等有变化时拆成多个。 |
runs[].text | 该片段的文字,不含换行符。 |
runs[].style | 该片段的近似视觉样式,包含下面八个字段。样式由模型自动识别,可能与原设计稿的精确属性不同。 |
style.similar_font_family | 根据外观匹配到的最相近字体名称;无法识别或置信度不足时返回 unknown。 |
style.foreground_color | 文字颜色,大写 #RRGGBB 格式,不含透明通道。 |
style.bold | 是否识别为加粗。 |
style.italic | 是否识别为斜体:只有字形本身向右倾斜时为 true,旋转或透视造成的倾斜不算。 |
style.underline | 是否识别到文字下划线;独立的分隔线、表格线或装饰线不算。 |
style.strikethrough | 是否识别到删除线。 |
style.shadow | 是否识别到明显的投影、阴影或发光偏移;正常的抗锯齿过渡不算。 |
style.translucent | 文字本身是否明显半透明、能透出背景的纹理或颜色;浅灰、浅蓝等不透明的浅色文字不算。 |
文字图层响应示例
下面是上例中包含Presented By : 和 Anna Katrina Marchesi 的文字图层(z_index: 6)在 data[] 中的一项。
{
"url": "https://files.dreamto.ai/images/seedream_layer_6_9b21.png",
"size": "1166x899",
"output_format": "png",
"background": "transparent",
"z_index": 6,
"name": "Presenter text",
"description": "Two lines of dark blue presenter text",
"bounding_box": {
"absolute": [
1142,
528,
1474,
784
],
"normalized": [
643,
529,
830,
786
]
},
"text_recognition": {
"text": "Presented By : \nAnna Katrina Marchesi",
"lines": [
{
"bounding_box": {
"absolute": [
1196,
629,
1358,
658
],
"normalized": [
673,
630,
765,
659
]
},
"runs": [
{
"text": "Presented By : ",
"style": {
"similar_font_family": "Helvetica Neue",
"foreground_color": "#193366",
"bold": true,
"italic": false,
"underline": false,
"strikethrough": false,
"shadow": false,
"translucent": false
}
}
]
},
{
"bounding_box": {
"absolute": [
1195,
651,
1427,
681
],
"normalized": [
673,
652,
803,
682
]
},
"runs": [
{
"text": "Anna Katrina Marchesi",
"style": {
"similar_font_family": "Helvetica Neue",
"foreground_color": "#46597E",
"bold": false,
"italic": false,
"underline": false,
"strikethrough": false,
"shadow": false,
"translucent": false
}
}
]
}
]
}
}



