"""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()}")