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>
This commit is contained in:
zimoyin
2026-10-12 14:37:03 +08:00
parent 44d42f6ae0
commit e32ad664f1
19 changed files with 1646 additions and 65 deletions
+61
View File
@@ -60,6 +60,67 @@ def test_queue_completes_task() -> None:
assert queue.snapshot(file_id)[3] is not None
def test_resolve_cpu_threads_supports_max_and_int() -> None:
import os
from ocr_server.config import CPU_THREADS_MAX, resolve_cpu_threads
assert resolve_cpu_threads(4) == 4
assert resolve_cpu_threads("max") == max(1, os.cpu_count() or 1)
assert resolve_cpu_threads("MAX") == max(1, os.cpu_count() or 1)
assert resolve_cpu_threads(CPU_THREADS_MAX) == max(1, os.cpu_count() or 1)
def test_settings_validate_cpu_threads_and_expose_mkldnn() -> None:
import os
import pytest
from ocr_server.config import Settings
assert Settings(cpu_threads=8).cpu_threads_resolved == 8
assert Settings(cpu_threads="max").cpu_threads_resolved == max(1, os.cpu_count() or 1)
assert Settings(enable_mkldnn=True).enable_mkldnn is True
assert Settings(enable_mkldnn=False).enable_mkldnn is False
with pytest.raises(Exception):
Settings(cpu_threads="lots")
with pytest.raises(Exception):
Settings(cpu_threads=0)
def test_ocr_service_resolves_threads_and_stores_mkldnn() -> None:
from ocr_server.ocr import OCRService
service = OCRService(cpu_threads="max", enable_mkldnn=True)
assert service._cpu_threads == max(1, __import__("os").cpu_count() or 1)
assert service._enable_mkldnn is True
plain = OCRService(cpu_threads=3, enable_mkldnn=False)
assert plain._cpu_threads == 3
assert plain._enable_mkldnn is False
def test_document_rows_groups_by_coordinates() -> None:
"""行文本按坐标(bbox 纵向重叠)归并,供字段抽取的 row 选项使用。"""
recognized = [
{"text": "检查号", "confidence": 0.9, "polygon": [[0, 0], [40, 0], [40, 10], [0, 10]]},
{"text": "26100830064", "confidence": 0.9, "polygon": [[45, 0], [120, 0], [120, 10], [45, 10]]},
{"text": "姓名", "confidence": 0.9, "polygon": [[0, 20], [40, 20], [40, 30], [0, 30]]},
{"text": "万镭", "confidence": 0.9, "polygon": [[45, 20], [90, 20], [90, 30], [45, 30]]},
]
document = OCRTaskQueue.build_document(recognized)
rows = OCRTaskQueue.document_rows(document)
assert rows == ["检查号26100830064", "姓名万镭"]
def test_document_rows_empty_document() -> None:
document = OCRTaskQueue.build_document([])
assert OCRTaskQueue.document_rows(document) == []
def test_progress_is_monotonic_and_includes_checkpoints() -> None:
from ocr_server.ocr import OCRService