feat: 按算法生成模板并支持从 Git LFS 下载模型

- init 新增 yolo26/resnet/arcface/lprnet/face-recognition 模板(多模型人脸检测+识别)
- 新增 embedding/text 输出类型与模板资源(COCO、字符表等)
- 新增 algpkg download-models:从 Gitea LFS 下载 models/BM1688 并做 SHA256 校验
- 补充单元测试与 README
This commit is contained in:
linaro
2026-10-17 20:33:00 +08:00
parent 897aa73bbf
commit 211341cbbb
7 changed files with 2220 additions and 110 deletions
+67 -2
View File
@@ -35,8 +35,12 @@ worker 侧 `workers/video/pyproject.toml` 以**路径依赖**指向本项目(`
# ① 生成脚手架(目录骨架 + manifest 模板 + 可运行示例 + 自测说明)
algpkg init /tmp/smoke-detect --id demo.smoke-detect --name 抽烟检测 --version 1.0.0
# 按算法生成(推荐):YOLO26 模板已内置完整 SAIL 前后处理与 COCO 80 类标签
algpkg init /tmp/yolo26-demo --id demo.yolo26 --name YOLO26检测 --algorithm yolo26
# ② 实现 /tmp/smoke-detect/<包内代码目录>/__init__.py 的 load() / infer()
# 模型放 /tmp/smoke-detect/models/,或打包时用 --model 指定
# yolo26 模板已实现,无需再写;模型放 /tmp/yolo26-demo/models/
# 或打包时用 --model 指定
# ③ 打包 + 自检(不需要设备)
algpkg build /tmp/smoke-detect --out demo.smoke-detect-1.0.0.mbank
@@ -44,6 +48,64 @@ algpkg inspect demo.smoke-detect-1.0.0.mbank # 清单/模型/快照/
algpkg verify demo.smoke-detect-1.0.0.mbank # 结构与模型校验
```
### 2.1 算法模板(`algpkg init --algorithm/--template`)
| 模板 | 生成内容 |
| ---- | -------- |
| `generic`(默认) | 目录骨架 + 占位 `load()/infer()`,自己按模型补前后处理 |
| `yolo26` | **可直接运行的 YOLO26 检测包**:`load()` 构造 SAIL 引擎;`infer()` 完成 letterbox 前处理 → 推理 → 置信度过滤/坐标反变换;manifest 预置 `yolo26s_fp16_1b` 模型、`conf/interval/draw` 参数与 COCO 80 类 labels |
| `resnet` | **可直接运行的 ResNet 图像分类包**:resize + ImageNet mean/std 归一化 → 推理 → softmax/top-1;分类结果用整帧框承载(score/类别),绘制与告警链路无需改动;manifest 预置 `resnet50_fp32_1b` 模型、`conf/interval/draw` 参数与 1000 类占位 labels |
| `arcface` | **ArcFace 人脸特征提取包**:RGB → resize 112×112 → 推理 → L2 归一化;输出 512 维 embedding 放在 `AlgoResult.meta["embedding"]`,`output.type=embedding`;manifest 预置 `arcface_resnet50_fp32_1b` 模型与 `interval` 参数 |
| `lprnet` | **LPRNet 车牌识别包**:BGR → resize 到模型输入 → `(x-127.5)/128` → CTC 贪心解码;车牌字符串放在 `AlgoResult.meta["text"]`,`output.type=text`;manifest 预置 `lprnet_fp32_1b` 模型、`interval` 参数与字符表 `assets/chars.txt` |
| `face-recognition` | **人脸检测 + 识别双模型包**:RetinaFace 检测人脸 → 按框外扩裁脸 → ArcFace 提取 512 维特征;`boxes` 为人脸框,`meta["embeddings"]` 与框逐项对应;manifest 同时声明 `face_detector` 与 `arcface` 两份模型 |
`--algorithm` 也接受 `yolov26`、`yolo26-detect`、`resnet50`、`classifier`、`arc-face`、
`face-embedding`、`lpr`、`license-plate`、`face`、`retinaface-arcface` 等别名;
`--kind` 缺省沿用模板(yolo26/face-recognition → `detector`,其余 → `classifier`),
显式给出可覆盖。模型放到模板提示的路径即可打包,不在源码目录时用
`--model <模型名>=<磁盘路径>` 指定实际文件:
```bash
algpkg init /tmp/yolo26-demo --id demo.yolo26 --name YOLO26检测 --algorithm yolo26
algpkg build /tmp/yolo26-demo --out demo.yolo26-1.0.0.mbank \
--model yolo26s_fp16_1b=/path/to/yolo26s_fp16_1b.bmodel
algpkg init /tmp/resnet-demo --id demo.resnet --name ResNet分类 --algorithm resnet
algpkg build /tmp/resnet-demo --out demo.resnet-1.0.0.mbank \
--model resnet50_fp32_1b=/path/to/resnet50_fp32_1b.bmodel
algpkg init /tmp/arcface-demo --id demo.arcface --name ArcFace特征 --algorithm arcface
algpkg build /tmp/arcface-demo --out demo.arcface-1.0.0.mbank \
--model arcface_resnet50_fp32_1b=/path/to/arcface_resnet50_fp32_1b.bmodel
algpkg init /tmp/lprnet-demo --id demo.lprnet --name 车牌识别 --algorithm lprnet
algpkg build /tmp/lprnet-demo --out demo.lprnet-1.0.0.mbank \
--model lprnet_fp32_1b=/path/to/lprnet_fp32_1b.bmodel
algpkg init /tmp/face-demo --id demo.face --name 人脸检测识别 --algorithm face-recognition
algpkg build /tmp/face-demo --out demo.face-1.0.0.mbank \
--model face_detector=/path/to/retinaface_mobilenet0.25_fp32_1b.bmodel \
--model arcface=/path/to/arcface_resnet50_fp32_1b.bmodel
```
### 2.2 下载演示模型(`algpkg download-models`)
仓库里的 5 个 BM1688 演示模型(YOLO26 / ResNet50 / ArcFace / LPRNet / RetinaFace,
共约 299 MB)通过 **Git LFS** 存放在 `models/BM1688/`。没有 `git-lfs`、或不想拉取
整个仓库时,可直接从 Gitea 的 LFS 端点下载(自动 SHA256 校验;已存在且正确的文件跳过):
```bash
algpkg download-models # 全部 5 个 → models/BM1688
algpkg download-models --model yolo26 --model resnet
algpkg download-models --dest /tmp/models --force # 自定义目录 / 强制重下
# 私有仓库或自定义地址:
algpkg download-models --url http://<gitea>/<owner>/<repo>.git --token <TOKEN>
```
默认仓库地址可用 `ALGPKG_REPO_URL` 覆盖,token 也可用 `GITEA_TOKEN` 环境变量提供。
模型名支持 `yolo26`、`resnet`、`arcface`、`lprnet`、`retinaface` 及常用别名
(`yolov26`、`resnet50`、`lpr`、`face-detector` 等)。模型需已 push 到远端 LFS。
## 3. 本地测试(不需要 worker)
```bash
@@ -144,10 +206,13 @@ algpkg-sdk/
├── src/algpkg_sdk/
│ ├── api.py 接口契约(Algorithm / AlgoContext / AlgoResult)+ 别名注册
│ ├── manifest.py 清单字段与校验(models / params_schema / alarm / legacy…)
│ ├── models.py download-models:从 Git LFS 拉取 models/BM1688 演示模型
│ ├── loader.py AlgoPackage:读 svfs、挂代码(试加载)、构造实例
│ ├── builder.py 打包(module_bank 打代码 + svfs 放模型/资源 + 快照=版本 + 签名)
│ ├── testkit.py 本地测试台:check / run / compare(无需 worker)
│ └── cli.py algpkg build|inspect|verify|init|list|check|run|compare
│ ├── templates.py init 算法模板(generic / yolo26 / resnet / arcface / lprnet / face-recognition)
│ └── cli.py algpkg build|inspect|verify|init|list|check|run|compare|download-models
├── models/BM1688/ Git LFS:5 个 BM1688 演示模型(可 download-models 拉取)
├── examples/person-count/ 完整示例:人员计数(自带实现,只依赖本 SDK)
│ └── …
└── tests/ pytest(无设备跑静态,有设备跑推理,自动跳过)