- 新增基于 JWT 当前用户的登录恢复、角色权限、用户管理、审计日志和演示出厂重置后台接口与前端管理页。 - 重串 GT_label 导出和 GT Mask 导入逻辑:导出保留类别真实 maskid,导入仅接受灰度或 RGB 等通道 maskid 图,支持未知 maskid 策略、尺寸最近邻拉伸和导入预览。 - 统一分割结果导出体验:默认当前帧,按项目抽帧顺序和 XhXXmXXsXXXms 时间戳命名 ZIP 与图片,补齐 GT/Pro/Mix/分开 Mask 输出和映射 JSON。 - 调整工作区左侧工具栏:移除创建点/线段入口,新增画笔、橡皮擦及尺寸控制,并按绘制、布尔、导入/AI 工具分组分隔。 - 扩展 Canvas 编辑能力:画笔按语义分类绘制并可自动并入连通选中 mask,橡皮擦对选中区域扣除,优化布尔操作、选区、撤销重做和保存状态联动。 - 优化自动传播时间轴显示:同一蓝色系按传播新旧递进变暗,老传播记录达到阈值后统一旧记录色,并维护范围选择与清空后的历史显示。 - 将 AI 智能分割入口替换为更明确的 AI 元素图标,并同步侧栏、工作区和 AI 页面入口表现。 - 完善模板分类、maskid 工具函数、分类树联动、遮罩透明度、边缘平滑和传播链同步相关前端状态。 - 扩展后端项目、媒体、任务、Dashboard、模板和传播 runner 的用户隔离、任务控制、进度事件与兼容处理。 - 补充前后端测试,覆盖用户管理、GT_label 往返导入导出、GT Mask 校验和预览、画笔/橡皮擦、时间轴传播历史、导出范围、WebSocket 与 API 封装。 - 更新 AGENTS、README 和 doc 文档,记录当前接口契约、实现状态、测试计划、安装说明和 maskid/GT_label 规则。
230 lines
6.4 KiB
Python
230 lines
6.4 KiB
Python
"""Project and Frame CRUD endpoints."""
|
|
|
|
import logging
|
|
from typing import List
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from database import get_db
|
|
from models import Project, Frame, User
|
|
from routers.auth import get_current_user, require_editor
|
|
from schemas import ProjectCreate, ProjectOut, ProjectUpdate, FrameCreate, FrameOut
|
|
from minio_client import get_presigned_url
|
|
|
|
logger = logging.getLogger(__name__)
|
|
router = APIRouter(prefix="/api/projects", tags=["Projects"])
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Projects
|
|
# ---------------------------------------------------------------------------
|
|
@router.post(
|
|
"",
|
|
response_model=ProjectOut,
|
|
status_code=status.HTTP_201_CREATED,
|
|
summary="Create a new project",
|
|
)
|
|
def create_project(
|
|
payload: ProjectCreate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(require_editor),
|
|
) -> Project:
|
|
"""Create a new segmentation project."""
|
|
project = Project(**payload.model_dump(), owner_user_id=current_user.id)
|
|
db.add(project)
|
|
db.commit()
|
|
db.refresh(project)
|
|
logger.info("Created project id=%s name=%s", project.id, project.name)
|
|
return project
|
|
|
|
|
|
@router.get(
|
|
"",
|
|
response_model=List[ProjectOut],
|
|
summary="List all projects",
|
|
)
|
|
def list_projects(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
) -> List[Project]:
|
|
"""Retrieve a paginated list of projects."""
|
|
projects = (
|
|
db.query(Project)
|
|
.filter(Project.owner_user_id == current_user.id)
|
|
.offset(skip)
|
|
.limit(limit)
|
|
.all()
|
|
)
|
|
for p in projects:
|
|
p.frame_count = len(p.frames)
|
|
if p.thumbnail_url:
|
|
p.thumbnail_url = get_presigned_url(p.thumbnail_url, expires=3600)
|
|
return projects
|
|
|
|
|
|
@router.get(
|
|
"/{project_id}",
|
|
response_model=ProjectOut,
|
|
summary="Get a single project",
|
|
)
|
|
def get_project(
|
|
project_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
) -> Project:
|
|
"""Retrieve a project by its ID."""
|
|
project = db.query(Project).filter(
|
|
Project.id == project_id,
|
|
Project.owner_user_id == current_user.id,
|
|
).first()
|
|
if not project:
|
|
raise HTTPException(status_code=404, detail="Project not found")
|
|
project.frame_count = len(project.frames)
|
|
if project.thumbnail_url:
|
|
project.thumbnail_url = get_presigned_url(project.thumbnail_url, expires=3600)
|
|
return project
|
|
|
|
|
|
@router.patch(
|
|
"/{project_id}",
|
|
response_model=ProjectOut,
|
|
summary="Update a project",
|
|
)
|
|
def update_project(
|
|
project_id: int,
|
|
payload: ProjectUpdate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(require_editor),
|
|
) -> Project:
|
|
"""Update project fields partially."""
|
|
project = db.query(Project).filter(
|
|
Project.id == project_id,
|
|
Project.owner_user_id == current_user.id,
|
|
).first()
|
|
if not project:
|
|
raise HTTPException(status_code=404, detail="Project not found")
|
|
|
|
for key, value in payload.model_dump(exclude_unset=True).items():
|
|
setattr(project, key, value)
|
|
|
|
db.commit()
|
|
db.refresh(project)
|
|
logger.info("Updated project id=%s", project_id)
|
|
return project
|
|
|
|
|
|
@router.delete(
|
|
"/{project_id}",
|
|
status_code=status.HTTP_204_NO_CONTENT,
|
|
summary="Delete a project",
|
|
)
|
|
def delete_project(
|
|
project_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(require_editor),
|
|
) -> None:
|
|
"""Delete a project and all related frames and annotations."""
|
|
project = db.query(Project).filter(
|
|
Project.id == project_id,
|
|
Project.owner_user_id == current_user.id,
|
|
).first()
|
|
if not project:
|
|
raise HTTPException(status_code=404, detail="Project not found")
|
|
|
|
db.delete(project)
|
|
db.commit()
|
|
logger.info("Deleted project id=%s", project_id)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Frames
|
|
# ---------------------------------------------------------------------------
|
|
@router.post(
|
|
"/{project_id}/frames",
|
|
response_model=FrameOut,
|
|
status_code=status.HTTP_201_CREATED,
|
|
summary="Add a frame to a project",
|
|
)
|
|
def create_frame(
|
|
project_id: int,
|
|
payload: FrameCreate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(require_editor),
|
|
) -> Frame:
|
|
"""Register a new frame under a project."""
|
|
project = db.query(Project).filter(
|
|
Project.id == project_id,
|
|
Project.owner_user_id == current_user.id,
|
|
).first()
|
|
if not project:
|
|
raise HTTPException(status_code=404, detail="Project not found")
|
|
|
|
frame = Frame(project_id=project_id, **payload.model_dump(exclude={"project_id"}))
|
|
db.add(frame)
|
|
db.commit()
|
|
db.refresh(frame)
|
|
return frame
|
|
|
|
|
|
@router.get(
|
|
"/{project_id}/frames",
|
|
response_model=List[FrameOut],
|
|
summary="List frames for a project",
|
|
)
|
|
def list_frames(
|
|
project_id: int,
|
|
skip: int = 0,
|
|
limit: int = 1000,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
) -> List[Frame]:
|
|
"""Retrieve all frames belonging to a project."""
|
|
project = db.query(Project).filter(
|
|
Project.id == project_id,
|
|
Project.owner_user_id == current_user.id,
|
|
).first()
|
|
if not project:
|
|
raise HTTPException(status_code=404, detail="Project not found")
|
|
|
|
frames = (
|
|
db.query(Frame)
|
|
.filter(Frame.project_id == project_id)
|
|
.order_by(Frame.frame_index)
|
|
.offset(skip)
|
|
.limit(limit)
|
|
.all()
|
|
)
|
|
for frame in frames:
|
|
frame.image_url = get_presigned_url(frame.image_url, expires=3600)
|
|
return frames
|
|
|
|
|
|
@router.get(
|
|
"/{project_id}/frames/{frame_id}",
|
|
response_model=FrameOut,
|
|
summary="Get a single frame",
|
|
)
|
|
def get_frame(
|
|
project_id: int,
|
|
frame_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
) -> Frame:
|
|
"""Retrieve a specific frame by ID."""
|
|
frame = (
|
|
db.query(Frame)
|
|
.join(Project, Project.id == Frame.project_id)
|
|
.filter(
|
|
Frame.project_id == project_id,
|
|
Frame.id == frame_id,
|
|
Project.owner_user_id == current_user.id,
|
|
)
|
|
.first()
|
|
)
|
|
if not frame:
|
|
raise HTTPException(status_code=404, detail="Frame not found")
|
|
return frame
|