Files
ocr-server/scripts/benchmark_ocr.py
T
zimoyin e32ad664f1 feat(fields): 字段抽取支持按行锁定与多项匹配开关,修复进度日志被吞
字段抽取 src/ocr_server/fields.py
- 新增 row:只从第 N 行取值,行号由 bbox 纵向重叠的坐标分组得出,-1 表示不限定;
  用于排除正文/页眉里同名标签造成的误取
- 新增 separator_optional(标签后无冒号也抽,默认关闭)
  与 label_linebreak(标签内部跨行,如 姓 + 名:,默认关闭)
- 新增 occurrence(first/last,正副本、页眉页脚重复打印时取最后一个)
  与全局 extract_include_empty(未命中的字段返回空串,便于固定 schema)
- 修复:跨行命中的标签文本包含换行,归一化时未去换行导致匹配不到字段规则、
  取到的值被丢弃
- build_specs 在服务启动时校验:字段名重复、同一标签被多个字段共用、
  正则语法、max_length / occurrence / row 取值合法性

日志 src/ocr_server/logging_setup.py
- handler 改挂到 ocr_server logger 并关闭向 root 传播:Paddle 初始化会把 root
  级别改成 WARNING,依赖 root 会导致其后所有 INFO 日志(含 10/20/50/80/100
  进度行)被静默过滤,看起来像任务卡死

引擎配置
- enable_mkldnn 由写死的 False 改为可配置;cpu_threads 支持 max(吃满逻辑核心)
- pyproject 固定 paddlepaddle>=3.2,<3.3:3.3.x 的 PIR + oneDNN 组合会直接推理失败

打包
- 移除多余的 scripts 目录打包;新增 -DistPath 便于 dist 被占用时改址构建;
  构建结束输出 exe SHA256(避免再拿到旧产物)
- Ubuntu 脚本同步更新,并输出二进制 SHA256

文档
- README / config.yaml 注释按实测修订:行结构、模型档位对比、oneDNN 与版本差异、
  汉字字符类的范围与缺口、防止取错的机制与建议

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-10-12 14:37:03 +08:00

47 lines
1.7 KiB
Python

"""Benchmark the configured OCR pipeline without modifying model files."""
from pathlib import Path
from time import perf_counter
from ocr_server.config import Settings
from ocr_server.ocr import OCRService
if __name__ == "__main__":
image_path = Path("http/fixtures/general_ocr_002.png")
settings = Settings()
service = OCRService(
lang=settings.lang,
device=settings.device,
model_dir=settings.model_dir,
det_model_name=settings.det_model_name,
rec_model_name=settings.rec_model_name,
text_det_limit_side_len=settings.text_det_limit_side_len,
text_det_limit_type=settings.text_det_limit_type,
text_det_thresh=settings.text_det_thresh,
text_det_box_thresh=settings.text_det_box_thresh,
text_det_unclip_ratio=settings.text_det_unclip_ratio,
text_rec_score_thresh=settings.text_rec_score_thresh,
text_recognition_batch_size=settings.text_recognition_batch_size,
cpu_threads=settings.cpu_threads_resolved,
enable_mkldnn=settings.enable_mkldnn,
)
service.prepare()
stages: list[str] = []
def on_progress(percent: int, message: str) -> None:
stages.append(f"{percent}% {message}")
started = perf_counter()
lines = service.recognize(image_path, on_progress=on_progress)
elapsed_ms = (perf_counter() - started) * 1000
print(f"image={image_path}")
print(f"cpu_threads={settings.cpu_threads_resolved} (config={settings.cpu_threads})")
print(f"enable_mkldnn={settings.enable_mkldnn}")
print(f"det={settings.det_model_name} rec={settings.rec_model_name}")
print(f"elapsed_ms={elapsed_ms:.2f}")
print(f"segments={len(lines)}")
for stage in stages:
print(f"stage={stage}")