- 打通工作区真实标注闭环:支持手工多边形、矩形、圆形、点区域和线段生成 mask,并可保存、回显、更新和删除后端 annotation。 - 增强 polygon 编辑器:支持顶点拖动、顶点删除、边中点插入、多 polygon 子区域选择编辑,以及区域合并和区域去除。 - 接入 GT mask 导入:后端支持二值/多类别 mask 拆分、contour 转 polygon、distance transform seed point,前端支持导入、回显和 seed point 拖动编辑。 - 完善导出能力:COCO JSON 导出对齐前端,PNG mask ZIP 同时包含单标注 mask、按 zIndex 融合的 semantic_frame 和 semantic_classes.json。 - 打通异步任务管理:新增任务取消、重试、失败详情接口与 Dashboard 控件,worker 支持取消状态检查并通过 Redis/WebSocket 推送 cancelled 事件。 - 对接 Dashboard 后端数据:概览统计、解析队列和实时流转记录从 FastAPI 聚合接口与 WebSocket 更新。 - 增强 AI 推理参数:前端发送 crop_to_prompt、auto_filter_background 和 min_score,后端支持点/框 prompt 局部裁剪推理、结果回映射和负向点/低分过滤。 - 接入 SAM3 基础设施:新增独立 Python 3.12 sam3 环境安装脚本、外部 worker helper、后端桥接和真实 Python/CUDA/包/HF checkpoint access 状态检测。 - 保留 SAM3 授权边界:当前官方 facebook/sam3 gated 权重未授权时状态接口会返回不可用,不伪装成可推理。 - 增强前端状态管理:新增 mask undo/redo 历史栈、AI 模型选择状态、保存状态 dirty/draft/saved 流转和项目状态归一化。 - 更新前端 API 封装:补充 annotation CRUD、GT mask import、mask ZIP export、task cancel/retry/detail、AI runtime status 和 prediction options。 - 更新 UI 控件:ToolsPalette、AISegmentation、VideoWorkspace 和 CanvasArea 接入真实操作、导入导出、撤销重做、任务控制和模型状态。 - 新增 polygon-clipping 依赖,用于前端区域 union/difference 几何运算。 - 完善后端 schemas/status/progress:补充 AI 模型外部状态字段、任务 cancelled 状态和进度事件 payload。 - 补充测试覆盖:新增后端任务控制、SAM3 桥接、GT mask、导出融合、AI options 测试;补充前端 Canvas、Dashboard、VideoWorkspace、ToolsPalette、API 和 store 测试。 - 更新 README、AGENTS 和 doc 文档:冻结当前需求/设计/测试计划,标注真实功能、剩余 Mock、SAM3 授权边界和后续实施顺序。
119 lines
4.2 KiB
Python
119 lines
4.2 KiB
Python
import zipfile
|
|
import json
|
|
from io import BytesIO
|
|
|
|
import cv2
|
|
import numpy as np
|
|
|
|
|
|
def _seed_export_data(client):
|
|
project = client.post("/api/projects", json={"name": "Export Project"}).json()
|
|
frame = client.post(f"/api/projects/{project['id']}/frames", json={
|
|
"project_id": project["id"],
|
|
"frame_index": 0,
|
|
"image_url": "frames/0.jpg",
|
|
"width": 100,
|
|
"height": 50,
|
|
}).json()
|
|
template = client.post("/api/templates", json={
|
|
"name": "Category",
|
|
"color": "#06b6d4",
|
|
"z_index": 0,
|
|
"classes": [],
|
|
"rules": [],
|
|
}).json()
|
|
annotation = client.post("/api/ai/annotate", json={
|
|
"project_id": project["id"],
|
|
"frame_id": frame["id"],
|
|
"template_id": template["id"],
|
|
"mask_data": {"polygons": [[[0.1, 0.2], [0.9, 0.2], [0.9, 0.8], [0.1, 0.8]]]},
|
|
"points": [[0.5, 0.5]],
|
|
"bbox": [0.1, 0.2, 0.8, 0.6],
|
|
}).json()
|
|
return project, frame, template, annotation
|
|
|
|
|
|
def test_export_coco_json_structure(client):
|
|
project, frame, _, _ = _seed_export_data(client)
|
|
|
|
response = client.get(f"/api/export/{project['id']}/coco")
|
|
|
|
assert response.status_code == 200
|
|
assert response.headers["content-type"].startswith("application/json")
|
|
data = response.json()
|
|
assert data["info"]["description"] == "Annotations for Export Project"
|
|
assert data["images"][0] == {
|
|
"id": frame["id"],
|
|
"file_name": "frames/0.jpg",
|
|
"width": 100,
|
|
"height": 50,
|
|
"frame_index": 0,
|
|
}
|
|
assert data["annotations"][0]["segmentation"] == [[10.0, 10.0, 90.0, 10.0, 90.0, 40.0, 10.0, 40.0]]
|
|
assert data["annotations"][0]["bbox"] == [10.0, 10.0, 80.0, 30.000000000000004]
|
|
assert data["categories"][0]["name"] == "Category"
|
|
|
|
|
|
def test_export_masks_zip(client):
|
|
project, _, _, annotation = _seed_export_data(client)
|
|
|
|
response = client.get(f"/api/export/{project['id']}/masks")
|
|
|
|
assert response.status_code == 200
|
|
assert response.headers["content-type"].startswith("application/zip")
|
|
with zipfile.ZipFile(BytesIO(response.content)) as archive:
|
|
assert archive.namelist() == [
|
|
f"mask_{annotation['id']:06d}.png",
|
|
"semantic_frame_000000.png",
|
|
"semantic_classes.json",
|
|
]
|
|
|
|
|
|
def test_export_masks_uses_z_index_for_semantic_fusion(client):
|
|
project = client.post("/api/projects", json={"name": "Fusion Project"}).json()
|
|
frame = client.post(f"/api/projects/{project['id']}/frames", json={
|
|
"project_id": project["id"],
|
|
"frame_index": 0,
|
|
"image_url": "frames/0.jpg",
|
|
"width": 20,
|
|
"height": 20,
|
|
}).json()
|
|
low = client.post("/api/ai/annotate", json={
|
|
"project_id": project["id"],
|
|
"frame_id": frame["id"],
|
|
"mask_data": {
|
|
"polygons": [[[0.1, 0.1], [0.8, 0.1], [0.8, 0.8], [0.1, 0.8]]],
|
|
"label": "Low",
|
|
"color": "#00ff00",
|
|
"class": {"id": "low", "name": "Low", "color": "#00ff00", "zIndex": 10},
|
|
},
|
|
}).json()
|
|
high = client.post("/api/ai/annotate", json={
|
|
"project_id": project["id"],
|
|
"frame_id": frame["id"],
|
|
"mask_data": {
|
|
"polygons": [[[0.4, 0.4], [0.9, 0.4], [0.9, 0.9], [0.4, 0.9]]],
|
|
"label": "High",
|
|
"color": "#ff0000",
|
|
"class": {"id": "high", "name": "High", "color": "#ff0000", "zIndex": 20},
|
|
},
|
|
}).json()
|
|
|
|
response = client.get(f"/api/export/{project['id']}/masks")
|
|
|
|
assert response.status_code == 200
|
|
with zipfile.ZipFile(BytesIO(response.content)) as archive:
|
|
assert f"mask_{low['id']:06d}.png" in archive.namelist()
|
|
assert f"mask_{high['id']:06d}.png" in archive.namelist()
|
|
legend = json.loads(archive.read("semantic_classes.json"))
|
|
high_value = next(item["value"] for item in legend["classes"] if item["key"] == "class:high")
|
|
semantic_bytes = np.frombuffer(archive.read("semantic_frame_000000.png"), dtype=np.uint8)
|
|
semantic = cv2.imdecode(semantic_bytes, cv2.IMREAD_GRAYSCALE)
|
|
|
|
assert semantic[10, 10] == high_value
|
|
|
|
|
|
def test_export_missing_project_returns_404(client):
|
|
assert client.get("/api/export/999/coco").status_code == 404
|
|
assert client.get("/api/export/999/masks").status_code == 404
|