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:
@@ -23,7 +23,8 @@ if __name__ == "__main__":
|
||||
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,
|
||||
)
|
||||
service.prepare()
|
||||
|
||||
@@ -36,7 +37,8 @@ if __name__ == "__main__":
|
||||
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}")
|
||||
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)}")
|
||||
|
||||
@@ -19,6 +19,7 @@ if __name__ == "__main__":
|
||||
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,
|
||||
).prepare()
|
||||
print(f"Models are ready in: {settings.model_dir.resolve()}")
|
||||
|
||||
@@ -1,4 +1,16 @@
|
||||
#!/usr/bin/env bash
|
||||
# 打包 Ubuntu/Linux 发布包(--onedir 目录形式)。
|
||||
#
|
||||
# 关键参数说明,改动前请先读:
|
||||
# --collect-all paddle/paddlex/paddleocr Paddle 的原生库与配置必须整体收集,缺一个就跑不起来
|
||||
# --collect-all pyclipper/pypdfium2/shapely/bidi
|
||||
# 检测后处理与识别的必需依赖,漏了会在推理时报错
|
||||
# --copy-metadata * PaddleX 通过 importlib.metadata 检查依赖,
|
||||
# 缺元数据会误判「依赖未安装」
|
||||
# --additional-hooks-dir scripts 启用 scripts/hook-imagesize.py
|
||||
# --hidden-import imagesize 源码环境下按需导入,打包时必须显式声明
|
||||
#
|
||||
# 必须在 Linux 上执行:PyInstaller 不能交叉编译,Windows 上无法生成 ELF 二进制。
|
||||
set -euo pipefail
|
||||
|
||||
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
@@ -11,6 +23,7 @@ fi
|
||||
|
||||
uv sync --dev --python 3.12
|
||||
rm -rf build dist
|
||||
|
||||
uv run pyinstaller --noconfirm --clean --onedir --name ocr-server \
|
||||
--paths src \
|
||||
--collect-all paddleocr \
|
||||
@@ -29,7 +42,6 @@ uv run pyinstaller --noconfirm --clean --onedir --name ocr-server \
|
||||
--copy-metadata imagesize \
|
||||
--add-data 'config.yaml:.' \
|
||||
--add-data 'http:http' \
|
||||
--add-data 'scripts:_packaging_scripts' \
|
||||
--additional-hooks-dir scripts \
|
||||
--hidden-import imagesize \
|
||||
--hidden-import imagesize.imagesize \
|
||||
@@ -41,5 +53,13 @@ cp -f config.yaml dist/ocr-server/config.yaml
|
||||
cp -a models dist/ocr-server/models
|
||||
chmod +x dist/ocr-server/ocr-server
|
||||
|
||||
printf 'Ubuntu 发布目录:%s\n' "$PROJECT_ROOT/dist/ocr-server"
|
||||
BIN="$PROJECT_ROOT/dist/ocr-server/ocr-server"
|
||||
if [[ ! -x "$BIN" ]]; then
|
||||
printf '打包失败:未生成 %s\n' "$BIN" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
printf '\nUbuntu 发布目录:%s\n' "$PROJECT_ROOT/dist/ocr-server"
|
||||
printf '%s\n' '启动:dist/ocr-server/ocr-server'
|
||||
printf '二进制 SHA256:%s\n' "$(sha256sum "$BIN" | cut -d' ' -f1)"
|
||||
printf '%s\n' '(发布时要连 _internal 和 models 一起复制,不能只替换二进制)'
|
||||
|
||||
@@ -1,10 +1,27 @@
|
||||
# 打包 Windows 发布包(--onedir 目录形式)。
|
||||
#
|
||||
# 关键参数说明,改动前请先读:
|
||||
# --collect-all paddle/paddlex/paddleocr Paddle 的原生库与配置必须整体收集,缺一个就跑不起来
|
||||
# --collect-all pyclipper/pypdfium2/shapely/bidi
|
||||
# 检测后处理与识别的必需依赖,漏了会在推理时报错
|
||||
# --copy-metadata * PaddleX 通过 importlib.metadata 检查依赖,
|
||||
# 缺元数据会误判「依赖未安装」
|
||||
# --additional-hooks-dir scripts 启用 scripts/hook-imagesize.py
|
||||
# --hidden-import imagesize 源码环境下按需导入,打包时必须显式声明
|
||||
#
|
||||
# 发布包为 --onedir:模型与 config.yaml 外置,便于替换;不要只替换 exe,_internal 也要一起更新。
|
||||
param(
|
||||
[switch]$Clean
|
||||
[switch]$Clean,
|
||||
# 输出目录,默认 dist。若 dist 被其它进程当作工作目录占用(Windows 会锁住该目录),
|
||||
# 可指定别处构建,再把内容搬回去,例如:
|
||||
# .\scripts\package-windows.ps1 -DistPath build\pyi-dist
|
||||
[string]$DistPath = "dist"
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
$ProjectRoot = Split-Path -Parent $PSScriptRoot
|
||||
Set-Location $ProjectRoot
|
||||
$ReleaseDir = Join-Path $ProjectRoot "$DistPath\ocr-server"
|
||||
|
||||
if ($Clean) {
|
||||
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue build, dist
|
||||
@@ -12,6 +29,7 @@ if ($Clean) {
|
||||
|
||||
uv sync --dev
|
||||
uv run pyinstaller --noconfirm --clean --onedir --name ocr-server `
|
||||
--distpath $DistPath `
|
||||
--paths src `
|
||||
--collect-all paddleocr `
|
||||
--collect-all paddlex `
|
||||
@@ -29,7 +47,6 @@ uv run pyinstaller --noconfirm --clean --onedir --name ocr-server `
|
||||
--copy-metadata imagesize `
|
||||
--add-data "config.yaml;." `
|
||||
--add-data "http;http" `
|
||||
--add-data "scripts;_packaging_scripts" `
|
||||
--additional-hooks-dir scripts `
|
||||
--hidden-import imagesize `
|
||||
--hidden-import imagesize.imagesize `
|
||||
@@ -37,10 +54,20 @@ uv run pyinstaller --noconfirm --clean --onedir --name ocr-server `
|
||||
--hidden-import ocr_server.config `
|
||||
scripts/packaged_entry.py
|
||||
|
||||
Copy-Item -Force config.yaml dist/ocr-server/config.yaml
|
||||
Copy-Item -Force config.yaml (Join-Path $ReleaseDir "config.yaml")
|
||||
if (Test-Path models) {
|
||||
Copy-Item -Recurse -Force models dist/ocr-server/models
|
||||
Copy-Item -Recurse -Force models (Join-Path $ReleaseDir "models")
|
||||
}
|
||||
|
||||
Write-Host "Windows 发布目录:$ProjectRoot\dist\ocr-server"
|
||||
Write-Host "启动:dist\ocr-server\ocr-server.exe"
|
||||
$Exe = Join-Path $ReleaseDir "ocr-server.exe"
|
||||
if (-not (Test-Path $Exe)) {
|
||||
throw "打包失败:未生成 $Exe"
|
||||
}
|
||||
$Hash = (Get-FileHash -Algorithm SHA256 $Exe).Hash.ToLower()
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "Windows 发布目录:$ReleaseDir"
|
||||
Write-Host "启动:$DistPath\ocr-server\ocr-server.exe"
|
||||
Write-Host ("exe 体积:{0:N1} MB" -f ((Get-Item $Exe).Length / 1MB))
|
||||
Write-Host "exe SHA256:$Hash"
|
||||
Write-Host "(发布时要连 _internal 和 models 一起复制,不能只替换 exe)"
|
||||
|
||||
Reference in New Issue
Block a user