2026-05-18-21-20-13 改为逐帧生成结果视频

This commit is contained in:
2026-05-18 21:47:43 +08:00
parent 69444ced72
commit c818f0a663
6 changed files with 102 additions and 24 deletions

View File

@@ -12,7 +12,7 @@ from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, Response
from fastapi.staticfiles import StaticFiles
from backend.segmentation import METHOD_DESCRIPTIONS, compare_frame, overlay_mask, segment_frame
from backend.segmentation import METHOD_DESCRIPTIONS, compare_frame, segment_frame
ROOT = Path(__file__).resolve().parents[1]
@@ -213,7 +213,6 @@ def _process_video(
frame_index = 0
selected_count = 0
written_count = 0
active_mask = None
writer = None
raw_video_path = job_path / f"{method}_overlay.raw.mp4"
video_path = job_path / f"{method}_overlay.mp4"
@@ -234,9 +233,8 @@ def _process_video(
if selected_indices
else _selected_frame(frame_index, frame_stride, selected_count, max_frames)
)
video_frame = frame
if should_process and method == "compare":
if method == "compare" and should_process:
outputs = compare_frame(frame, previous, sensitivity)
for output in outputs:
frames.append(
@@ -252,34 +250,31 @@ def _process_video(
)
)
video_output = next(item for item in outputs if item.method == "fusion")
active_mask = video_output.mask
video_frame = video_output.overlay
selected_count += 1
elif should_process:
elif method == "compare":
video_output = segment_frame(frame, "fusion", previous, sensitivity)
else:
video_output = segment_frame(frame, method, previous, sensitivity)
frames.append(
_save_frame_outputs(
job_path,
frame_index,
method,
frame,
video_output,
frame_index / source_fps,
frame_index / source_fps,
frame_index,
if should_process:
frames.append(
_save_frame_outputs(
job_path,
frame_index,
method,
frame,
video_output,
frame_index / source_fps,
frame_index / source_fps,
frame_index,
)
)
)
active_mask = video_output.mask
video_frame = video_output.overlay
if should_process:
selected_count += 1
elif active_mask is not None:
video_frame = overlay_mask(frame, active_mask)
if writer is None:
height, width = frame.shape[:2]
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
writer = cv2.VideoWriter(str(raw_video_path), fourcc, result_fps, (width, height))
writer.write(video_frame)
writer.write(video_output.overlay)
written_count += 1
previous = frame