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
+31 -1
View File
@@ -1,18 +1,25 @@
import logging
from fastapi import FastAPI, File, HTTPException, UploadFile, status
from fastapi.responses import JSONResponse
from ocr_server.config import Settings
from ocr_server.fields import build_specs, extract_fields
from ocr_server.logging_setup import configure_logging
from ocr_server.ocr import OCRService
from ocr_server.schemas import ApiResponse, I2TData, I2TRequest
from ocr_server.storage import ImageStore, InvalidImage
from ocr_server.task_queue import OCRTaskQueue
logger = logging.getLogger("ocr_server.app")
def create_app(settings: Settings | None = None) -> FastAPI:
settings = settings or Settings()
configure_logging(settings.log_level)
store = ImageStore(settings)
field_specs = build_specs(settings.extract_fields)
field_separators = tuple(settings.extract_separators or (":", ":"))
def create_ocr() -> OCRService:
return OCRService(
@@ -28,7 +35,8 @@ def create_app(settings: Settings | None = None) -> FastAPI:
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,
cpu_threads=settings.cpu_threads_resolved,
enable_mkldnn=settings.enable_mkldnn,
)
tasks = OCRTaskQueue(create_ocr, worker_count=settings.ocr_workers)
@@ -116,6 +124,27 @@ def create_app(settings: Settings | None = None) -> FastAPI:
if result is None:
raise HTTPException(status_code=500, detail="OCR 结果为空")
filtered = OCRTaskQueue.filter_document(result, request.min_confidence)
extracted = (
extract_fields(
filtered.text,
field_specs,
field_separators,
rows=OCRTaskQueue.document_rows(filtered),
include_empty=settings.extract_include_empty,
)
if field_specs
else None
)
if field_specs:
# 默认不打 INFO 级日志:字段值里可能含姓名等敏感信息。
# 调 log_level: DEBUG 可以看到抽取明细,便于排查取值错乱。
logger.debug(
"字段抽取 file_id=%s min_confidence=%.2f 命中=%s 未命中=%s",
request.file_id,
request.min_confidence,
extracted,
[spec.name for spec in field_specs if spec.name not in (extracted or {})],
)
return ApiResponse(
data=I2TData(
file_id=request.file_id,
@@ -125,6 +154,7 @@ def create_app(settings: Settings | None = None) -> FastAPI:
ocr_finished_at=ocr_finished_at,
ocr_elapsed_ms=ocr_elapsed_ms,
result=filtered,
fields=extracted,
)
)