Files
ocr-server/scripts/download_models.py
T
zimoyin 5ab4bef80e build: CPU/GPU 双环境、产物命名与打包修复
交付:
  · 产物按变体命名:CPU -> dist/ocr-server/,GPU -> dist/ocr-server-gpu/。
    变体由构建环境实际装的 paddle 自动判断,不需要人工声明
  · GPU 包额外加 --collect-binaries nvidia。漏收的后果实测是最难查的那种:
    引擎能正常加载、设备也识别成 GPU,但第一次推理才报
    cudnn64_9.dll error 126
  · --copy-metadata 的发行包名随变体变化(paddlepaddle / paddlepaddle-gpu),
    改由 scripts/paddle_env.py 动态取;写死任何一个在另一种环境下都打不出包
  · 新增 scripts/prepare_release_config.py 按变体覆盖发布配置(用 Python 改写
    以保持 UTF-8,PowerShell 的 Set-Content 会把中文注释写坏)

环境:
  · paddle 从 pyproject 依赖里移除,改用 scripts/install_paddle.py 安装。
    它没法写死:CPU/GPU 互斥,GPU 还得按显卡选 CUDA 通道(RTX 50 系必须 cu129)。
    钉死 CPU 版时,uv sync / uv run / PyCharm 自动同步都会把装好的
    paddlepaddle-gpu 静默挤掉、服务悄悄退回 CPU(实测踩过三次)
  · 新增 scripts/switch_env.py:.venv(CPU)与 .venv-gpu(GPU)两套环境共存,
    切换零成本;所有 uv run 补 --no-sync,打包改用 uv sync --inexact
  · 预置模型扩到 8 套(v4/v5 × 服务端/移动端 × 检测/识别,约 398MB)

其它:
  · scripts/start.* 先找发布包再退回源码环境;入口脚本名固定是 ocr-server
  · benchmark_ocr.py 输出 segments 与 chars,换设备/模型时可直接比对召回
  · .gitignore 覆盖 .venv-gpu/ 与 dist*/

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

43 lines
1.6 KiB
Python

"""Download the staged PP-OCR models into the project's configured model directory.
交付包会预置多套检测/识别模型,方便在客户机器上只改 config.yaml 就能离线切换。
已经完整的模型目录会被跳过(见 OCRService._is_complete_model),不会重复下载。
"""
import shutil
from ocr_server.config import Settings
from ocr_server.ocr import STAGED_DET_MODELS, STAGED_REC_MODELS, OCRService
def drop_download_stubs(settings: Settings) -> None:
"""清理只含 .cache 的残壳目录(上次下载中断留下的)。
只删这种明确的失败残留,不碰任何带真实权重文件的目录。
"""
official_dir = settings.model_dir.resolve() / "official_models"
if not official_dir.is_dir():
return
for path in sorted(official_dir.iterdir()):
if not path.is_dir():
continue
entries = {item.name for item in path.iterdir()}
if entries and entries <= {".cache"}:
print(f"清理下载残留目录: {path.name}")
shutil.rmtree(path)
if __name__ == "__main__":
settings = Settings()
drop_download_stubs(settings)
combos = [(det, settings.rec_model_name) for det in STAGED_DET_MODELS]
combos += [(settings.det_model_name, rec) for rec in STAGED_REC_MODELS]
for det_model, rec_model in combos:
print(f"检查模型 det={det_model} rec={rec_model}")
OCRService.from_settings(
settings, det_model_name=det_model, rec_model_name=rec_model
).prepare()
print(f"Models are ready in: {settings.model_dir.resolve()}")