sdcb/simdpaddleocr 是基于 examples/SimdCodecs.AspNetCore 构建的官方容器镜像:
- 纯 C# 零原生依赖(
Sdcb.SimdPaddleOCR+Sdcb.SimdCodecs),基础镜像为noble-chiseled - 内置 PP-OCRv6 中文
tiny/small/medium三个模型(含文本行方向分类 CLS)与整页四向文档方向分类模型(PP-LCNet_x1_0_doc_ori),开箱即用 - 提供完整 OCR 流水线、整页 0°/90°/180°/270° 文档方向校正,以及共享已加载模型权重的独立检测 / 文本行分类 / 单行识别接口
- 3.0 扩展模型(首次调用时加载,也可用
OCR__PreloadExtensions预加载):版面分析(PP-DocLayoutV3)、表格识别(PP-LCNet_x1_0_table_cls+SLANet_plus+ OCR 合并成 HTML)、文档展平(UVDoc)、印章文字识别(PP-OCRv4_mobile_seal_det),以及韩文、西里尔、阿拉伯、泰文等 11 个 PP-OCRv5 语种的单行识别。 - 同时提供
linux/amd64与linux/arm64多架构镜像 - 自带 Web 体验界面(支持一键旋转 90° 测试与自动扶正预览)、REST API 与 Scalar 交互式 OpenAPI 文档
docker run --rm -p 8080:8080 sdcb/simdpaddleocr:latest启动后访问:
- Web 体验界面:
http://localhost:8080/ - 交互式 API 文档(Scalar):
http://localhost:8080/scalar - OpenAPI 规范:
http://localhost:8080/openapi/v1.json - 健康检查:
http://localhost:8080/health
services:
ocr:
image: sdcb/simdpaddleocr:latest
ports:
- "8080:8080"
environment:
OCR__DefaultModel: tiny
OCR__PreloadModels: tiny
OCR__MaxUploadBytes: 20971520
OCR__MaxImagePixels: 40000000
# OCR__ApiKey: your-secret-key
restart: unless-stopped| 标签 | 说明 |
|---|---|
latest |
最新正式版(打正式 v* Git Tag 时更新) |
3.0.0 / 3.0 |
对应正式版本号(不含预发布后缀) |
r-<run_number> |
每次 main 构建或发布产出的不可变构建号镜像,适合固定版本验证 |
所有参数均通过标准 ASP.NET Core 配置键 OCR__* 传入:
| 环境变量 | 默认值 | 说明 |
|---|---|---|
OCR__DefaultModel |
tiny |
请求未指定 model 时的默认模型(tiny / small / medium) |
OCR__PreloadModels |
空(按需懒加载) | 启动时预加载的模型列表,逗号分隔,如 tiny、tiny,small 或 all |
OCR__MaxUploadBytes |
20971520 (20 MB) |
单次上传图片最大字节数 |
OCR__MaxImagePixels |
40000000 (4000 万) |
单张图片最大像素数(width * height) |
OCR__ApiKey |
空(不鉴权) | 设置后,/api/* 接口必须携带 X-API-Key: <key> 或 Authorization: Bearer <key>;/health 不受影响 |
OCR__Backend |
Auto |
推理后端(Auto / Cpu / Vulkan) |
OCR__LineWorkerCount |
0(自动) |
文本行 CLS/REC 并发 worker 路数(0 表示 min(ProcessorCount, 4)) |
OCR__DetIntraOpThreads |
0(自动) |
检测模型图内算子线程数 |
OCR__UseDirectionClassification |
true |
是否启用 180° 文本行方向分类 |
OCR__DefaultDocumentOrientation |
false |
POST /api/ocr 未传 documentOrientation 时是否默认执行整页方向检测与旋转校正 |
OCR__PreloadDocumentOrientation |
false |
启动时是否预加载整页方向分类模型(PP-LCNet_x1_0_doc_ori) |
OCR__PreloadExtensions |
空(按需懒加载) | 启动时预加载的 3.0 扩展模型,逗号分隔:layout、table、unwarp、seal、语种代码(如 korean),或 all。layout 常驻约 1 GB 内存 |
OCR__DefaultLanguage |
空(中文模型) | POST /api/ocr/recognize 未传 language 时使用的 PP-OCRv5 语种(korean / eslav / cyrillic / arabic / th / el / devanagari / ta / te / latin / en) |
返回服务健康状态与当前已加载到内存的模型列表:
{
"status": "ok",
"defaultModel": "tiny",
"loadedModels": ["tiny"],
"documentOrientationLoaded": false,
"buildConfiguration": "Release",
"loadedExtensions": ["layout", "korean"]
}loadedExtensions 列出已加载的 3.0 扩展模型(layout / table / unwarp / seal / 语种代码)。
列出可用模型与默认模型:
{
"models": ["tiny", "small", "medium"],
"default": "tiny",
"languages": ["korean", "eslav", "cyrillic", "arabic", "th", "el", "devanagari", "ta", "te", "latin", "en"]
}languages 是 POST /api/ocr/recognize 的 language 字段可用的 PP-OCRv5 语种。
multipart/form-data 上传图片并返回完整 OCR 识别结果:
| 表单字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
file |
file | 是 | 图片文件(JPEG / PNG / WebP / BMP / GIF / TIFF 等,自动应用 EXIF 方向) |
model |
string | 否 | tiny / small / medium,省略则使用 OCR__DefaultModel |
characters |
boolean | 否 | 为 true 时每行额外返回逐字坐标框 characters,默认 false |
documentOrientation |
boolean | 否 | 为 true 时先检测 0°/90°/180°/270° 整页方向并自动扶正后再 OCR,返回 documentOrientation 字段且检测框自动映射回原图坐标;省略则使用 OCR__DefaultDocumentOrientation(默认 false) |
curl -X POST http://localhost:8080/api/ocr \
-F "file=@sample.jpg" \
-F "model=tiny" \
-F "documentOrientation=true" \
-F "characters=true"启用 OCR__ApiKey 时附带请求头:
curl -X POST http://localhost:8080/api/ocr \
-H "X-API-Key: your-secret-key" \
-F "file=@sample.jpg"独立执行整页文档方向分类(PP-LCNet_x1_0_doc_ori,0° / 90° / 180° / 270°):
| 表单字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
file |
file | 是 | 图片文件(自动应用 EXIF 方向) |
curl -X POST http://localhost:8080/api/ocr/orientation \
-F "file=@rotated.jpg"响应示例:
{
"label": 1,
"orientationDegrees": 90,
"score": 0.9998,
"width": 669,
"height": 841,
"buildConfiguration": "Release",
"elapsedMs": { "decode": 3.2, "inference": 9.8, "total": 13.1 }
}
orientationDegrees表示页面相对正向的顺时针偏转角度,同时也是调用PaddleOcrDocumentRotation.Rotate将页面扶正所需的逆时针旋转角度。
仅执行文本检测(DET),与 POST /api/ocr 共享同一个已加载的 PaddleOcrAll 检测模型权重(零额外内存开销):
| 表单字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
file |
file | 是 | 图片文件 |
model |
string | 否 | tiny / small / medium,省略则使用 OCR__DefaultModel |
curl -X POST http://localhost:8080/api/ocr/detect \
-F "file=@sample.jpg" \
-F "model=tiny"仅执行文本行 0° / 180° 方向分类(CLS),输入为单行裁剪图片;默认直接复用已加载 PaddleOcrAll 的分类器权重(若 OCR__UseDirectionClassification=false 则自动按需加载独立分类器):
| 表单字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
file |
file | 是 | 单行文本裁剪图 |
curl -X POST http://localhost:8080/api/ocr/classify \
-F "file=@line-crop.png"仅执行单行文本识别(REC),输入为单行裁剪图片,与 POST /api/ocr 共享同一个已加载的 PaddleOcrAll 识别模型权重(零额外内存开销):
| 表单字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
file |
file | 是 | 单行文本裁剪图 |
model |
string | 否 | tiny / small / medium,省略则使用 OCR__DefaultModel |
characters |
boolean | 否 | 为 true 时额外返回逐字坐标框 characters,默认 false |
language |
string | 否 | PP-OCRv5 语种:korean、eslav、cyrillic、arabic、th、el、devanagari、ta、te、latin、en。传入时忽略 model,响应的 model 为 v5-<语种>;省略则使用 OCR__DefaultLanguage(空则用中文 model)。阿拉伯文等从右往左的文字按逻辑顺序返回 |
curl -X POST http://localhost:8080/api/ocr/recognize \
-F "file=@line-crop.png" \
-F "model=tiny" \
-F "characters=true"
# 韩文单行
curl -X POST http://localhost:8080/api/ocr/recognize \
-F "file=@korean-line.png" \
-F "language=korean"版面分析(PP-DocLayoutV3):把整页文档切成 25 类区域(text、paragraph_title、doc_title、table、image、display_formula、seal、header、footer 等),并给出阅读顺序;列表顺序与 PaddleX 相同(以下为 test/assets/v3/layout.jpg 的前 4 个区域)。首次调用加载约 125 MB 的模型,常驻约 1 GB 内存。
| 表单字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
file |
file | 是 | 整页文档图片 |
curl -X POST http://localhost:8080/api/ocr/layout -F "file=@page.jpg"{
"width": 760,
"height": 865,
"regions": [
{ "label": "figure_title", "classId": 7, "score": 0.928, "box": [34, 19, 363, 81], "polygon": [[34, 19], [363, 19], [363, 81], [34, 81]], "order": null },
{ "label": "table", "classId": 21, "score": 0.965, "box": [75, 105, 323, 300], "polygon": [[75, 105], [323, 105], [323, 300], [75, 300]], "order": null },
{ "label": "paragraph_title", "classId": 17, "score": 0.852, "box": [35, 330, 144, 347], "polygon": [[35, 330], [143, 330], [143, 346], [35, 346]], "order": 1 },
{ "label": "text", "classId": 22, "score": 0.966, "box": [34, 349, 363, 615], "polygon": [[34, 349], [363, 349], [363, 615], [34, 615]], "order": 2 }
],
"buildConfiguration": "Release",
"elapsedMs": { "decode": 6.2, "inference": 607.0, "total": 613.5 }
}order 是从 1 开始的阅读顺序;标题、表格、图片、页眉页脚等不参与排序的区域为 null(与 PaddleX 一致)。polygon 由每个区域的掩码求得。
表格识别:输入一张表格裁剪图(可先用 /api/ocr/layout 找到 table 区域再裁剪),依次执行有线 / 无线表分类(PP-LCNet_x1_0_table_cls)、表格结构识别(SLANet_plus)和中文 OCR,再把文字填进单元格,返回完整 HTML。
| 表单字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
file |
file | 是 | 表格裁剪图 |
model |
string | 否 | 读取单元格文字用的中文模型 tiny / small / medium,省略则使用 OCR__DefaultModel |
curl -X POST http://localhost:8080/api/ocr/table -F "file=@table.png" -F "model=small"{
"type": "Wireless",
"typeScore": 0.80,
"structureScore": 0.99,
"html": "<html><body><table><tr><td>Item</td><td>Qty</td>…</tr>…</table></body></html>",
"structureHtml": "<html><body><table><tr><td></td><td></td>…",
"cells": [ { "box": [x1, y1, x2, y2] }, … ],
"ocrLines": 22,
"model": "small",
"buildConfiguration": "Release",
"elapsedMs": { "decode": 15.1, "inference": 52.5, "total": 67.6 }
}文档展平(UVDoc):把拍照时弯曲、褶皱、透视变形的文档展平,直接返回图片(与输入同尺寸)。各阶段耗时写在响应头 X-Elapsed-Ms(如 decode=11.4;inference=116.4;total=276.2)。上限 1600 万像素。
| 表单字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
file |
file | 是 | 文档照片 |
format |
string | 否 | png(默认)或 jpeg |
curl -X POST http://localhost:8080/api/ocr/unwarp -F "file=@photo.jpg" -o unwarped.png展平后的图片可以再交给 POST /api/ocr 识别。
印章文字识别:检测圆形 / 椭圆形印章上的文字区域(PP-OCRv4_mobile_seal_det),把弧形文字沿轮廓展开成水平长条,再用中文识别模型识别。
| 表单字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
file |
file | 是 | 含印章的图片 |
model |
string | 否 | 识别用的中文模型 tiny / small / medium,省略则使用 OCR__DefaultModel;印章文字建议用 medium |
curl -X POST http://localhost:8080/api/ocr/seal -F "file=@seal.png" -F "model=medium"{
"lines": [
{ "text": "天津君和缘商贸有限公司", "score": 0.99, "detectionScore": 0.97, "polygon": [[x, y], …] },
{ "text": "发票专用章", "score": 0.99, "detectionScore": 0.98, "polygon": [[x, y], …] }
],
"model": "medium",
"buildConfiguration": "Release",
"elapsedMs": { "decode": 5.2, "inference": 86.4, "total": 92.1 }
}import requests
with open("sample.jpg", "rb") as f:
resp = requests.post(
"http://localhost:8080/api/ocr",
files={"file": f},
data={"model": "tiny", "characters": "false"},
timeout=30,
)
resp.raise_for_status()
data = resp.json()
print(data["text"])using var http = new HttpClient();
using var form = new MultipartFormDataContent();
form.Add(new ByteArrayContent(await File.ReadAllBytesAsync("sample.jpg")), "file", "sample.jpg");
form.Add(new StringContent("tiny"), "model");
using var resp = await http.PostAsync("http://localhost:8080/api/ocr", form);
resp.EnsureSuccessStatusCode();
Console.WriteLine(await resp.Content.ReadAsStringAsync());本项目直接通过 .NET 10 SDK 的 PublishContainer 构建镜像:
# 构建并加载到本地 Docker daemon
dotnet publish examples/SimdCodecs.AspNetCore/SimdCodecs.AspNetCore.csproj \
-c Release \
/t:PublishContainer \
-p:ContainerImageTag=local