Batch処理

This commit is contained in:
2026-02-22 04:36:28 +09:00
parent d67265aa39
commit 0fdff5423e
9 changed files with 733 additions and 137 deletions
+37 -2
View File
@@ -85,6 +85,10 @@ class GenerateRequest(BaseModel):
iou_threshold: float = 0.45
class VideoInfoRequest(BaseModel):
video_path: str
class BakeRequest(BaseModel):
video_path: str
detections_path: str
@@ -397,6 +401,9 @@ def process_bake_task(task_id: str, req: BakeRequest):
tasks[task_id].message = "Invalid detections format: 'frames' is missing"
return
# 検出キャッシュの開始フレーム(ソース動画のフレームインデックス)
det_start_frame = int(payload.get("start_frame", 0))
# Get video info
temp_cap = cv2.VideoCapture(req.video_path)
if not temp_cap.isOpened():
@@ -415,7 +422,8 @@ def process_bake_task(task_id: str, req: BakeRequest):
tasks[task_id].message = "Invalid source video dimensions"
return
total = min(src_frames, len(frames_detections)) if src_frames > 0 else len(frames_detections)
# ソース動画の全フレームを出力(スワップ後もトリム設定が正しく機能するよう)
total = src_frames if src_frames > 0 else (det_start_frame + len(frames_detections))
if total <= 0:
tasks[task_id].status = TaskStatus.FAILED
tasks[task_id].message = "Source/detections frame count is zero"
@@ -504,7 +512,8 @@ def process_bake_task(task_id: str, req: BakeRequest):
break
idx, frame = item
frame_boxes = frames_detections[idx] if idx < len(frames_detections) else []
det_idx = idx - det_start_frame
frame_boxes = frames_detections[det_idx] if 0 <= det_idx < len(frames_detections) else []
if not frame_boxes:
process_queue.put((idx, frame))
@@ -839,6 +848,32 @@ def get_status():
"rocm_version": gpu_info["rocm_version"]
}
@app.post("/video_info")
def get_video_info(req: VideoInfoRequest):
if not os.path.exists(req.video_path):
raise HTTPException(status_code=404, detail=f"Video not found: {req.video_path}")
cap = cv2.VideoCapture(req.video_path)
if not cap.isOpened():
raise HTTPException(status_code=400, detail="Failed to open video")
try:
fps = float(cap.get(cv2.CAP_PROP_FPS) or 0.0)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) or 0)
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) or 0)
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT) or 0)
finally:
cap.release()
return {
"video_path": req.video_path,
"fps": fps,
"width": width,
"height": height,
"frame_count": frame_count,
}
@app.post("/generate", response_model=Task)
def generate_mask_endpoint(req: GenerateRequest, background_tasks: BackgroundTasks):
task_id = str(uuid.uuid4())