Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -222,13 +222,6 @@ def propagate_in_video(video_predictor, session_id):
|
|
| 222 |
outputs_per_frame[idx] = frame_output["outputs"]
|
| 223 |
return outputs_per_frame
|
| 224 |
|
| 225 |
-
|
| 226 |
-
# ----------------------------------------------------------------------
|
| 227 |
-
# Main inference entrypoint
|
| 228 |
-
# ----------------------------------------------------------------------
|
| 229 |
-
# ----------------------------------------------------------------------
|
| 230 |
-
# Main inference entrypoint
|
| 231 |
-
# ----------------------------------------------------------------------
|
| 232 |
# ----------------------------------------------------------------------
|
| 233 |
# Main inference entrypoint
|
| 234 |
# ----------------------------------------------------------------------
|
|
@@ -295,13 +288,11 @@ def run_sam3(file_path, prompt, progress=gr.Progress()):
|
|
| 295 |
with torch.inference_mode(), torch.autocast(device_type=DEVICE, dtype=torch.bfloat16):
|
| 296 |
|
| 297 |
for obj_idx, class_name in enumerate(class_names):
|
| 298 |
-
# We start a NEW session for every class
|
| 299 |
response = video_predictor.handle_request(
|
| 300 |
request=dict(type="start_session", resource_path=file_path)
|
| 301 |
)
|
| 302 |
session_id = response["session_id"]
|
| 303 |
|
| 304 |
-
# We assign this class the obj_id of `obj_idx`
|
| 305 |
video_predictor.handle_request(
|
| 306 |
request=dict(
|
| 307 |
type="add_prompt",
|
|
@@ -312,30 +303,42 @@ def run_sam3(file_path, prompt, progress=gr.Progress()):
|
|
| 312 |
)
|
| 313 |
)
|
| 314 |
|
| 315 |
-
# Stream the masks
|
| 316 |
for frame_output in video_predictor.handle_stream_request(
|
| 317 |
request=dict(type="propagate_in_video", session_id=session_id, propagation_direction="both")
|
| 318 |
):
|
| 319 |
idx = frame_output["frame_index"]
|
| 320 |
out_data = frame_output["outputs"]
|
| 321 |
|
| 322 |
-
# FORCE the data to be explicitly keyed by our unique obj_idx in the master dictionary
|
| 323 |
-
# This prevents the final class from blindly overwriting previous classes
|
| 324 |
if isinstance(out_data, dict):
|
| 325 |
for k, v in out_data.items():
|
| 326 |
-
master_outputs_per_frame[idx]
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 331 |
|
| 332 |
video_predictor.handle_request(request=dict(type="close_session", session_id=session_id))
|
| 333 |
|
| 334 |
progress(0.85, desc="Rendering combined overlay video...")
|
| 335 |
out_path = f"/tmp/output/sam3_overlay_{timestamp}.mp4"
|
| 336 |
|
| 337 |
-
#
|
| 338 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 339 |
save_masklet_video(video_frames, master_outputs_per_frame, out_path=out_path, alpha=0.5, fps=video_fps)
|
| 340 |
|
| 341 |
progress(1.0, desc="Done!")
|
|
|
|
| 222 |
outputs_per_frame[idx] = frame_output["outputs"]
|
| 223 |
return outputs_per_frame
|
| 224 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
# ----------------------------------------------------------------------
|
| 226 |
# Main inference entrypoint
|
| 227 |
# ----------------------------------------------------------------------
|
|
|
|
| 288 |
with torch.inference_mode(), torch.autocast(device_type=DEVICE, dtype=torch.bfloat16):
|
| 289 |
|
| 290 |
for obj_idx, class_name in enumerate(class_names):
|
|
|
|
| 291 |
response = video_predictor.handle_request(
|
| 292 |
request=dict(type="start_session", resource_path=file_path)
|
| 293 |
)
|
| 294 |
session_id = response["session_id"]
|
| 295 |
|
|
|
|
| 296 |
video_predictor.handle_request(
|
| 297 |
request=dict(
|
| 298 |
type="add_prompt",
|
|
|
|
| 303 |
)
|
| 304 |
)
|
| 305 |
|
| 306 |
+
# Stream the masks and cleanly accumulate the arrays/tensors
|
| 307 |
for frame_output in video_predictor.handle_stream_request(
|
| 308 |
request=dict(type="propagate_in_video", session_id=session_id, propagation_direction="both")
|
| 309 |
):
|
| 310 |
idx = frame_output["frame_index"]
|
| 311 |
out_data = frame_output["outputs"]
|
| 312 |
|
|
|
|
|
|
|
| 313 |
if isinstance(out_data, dict):
|
| 314 |
for k, v in out_data.items():
|
| 315 |
+
if k not in master_outputs_per_frame[idx]:
|
| 316 |
+
master_outputs_per_frame[idx][k] = []
|
| 317 |
+
|
| 318 |
+
# If it's a list (like out_obj_ids), extend it.
|
| 319 |
+
# If it's a tensor (like out_masks or out_probs), append it to be concatenated later.
|
| 320 |
+
if isinstance(v, list):
|
| 321 |
+
master_outputs_per_frame[idx][k].extend(v)
|
| 322 |
+
else:
|
| 323 |
+
master_outputs_per_frame[idx][k].append(v)
|
| 324 |
|
| 325 |
video_predictor.handle_request(request=dict(type="close_session", session_id=session_id))
|
| 326 |
|
| 327 |
progress(0.85, desc="Rendering combined overlay video...")
|
| 328 |
out_path = f"/tmp/output/sam3_overlay_{timestamp}.mp4"
|
| 329 |
|
| 330 |
+
# --- CONSOLIDATION PASS ---
|
| 331 |
+
# The visualizer requires single tensors for masks and probs, not lists of tensors.
|
| 332 |
+
# We loop through our accumulated master dictionary and concatenate everything perfectly.
|
| 333 |
+
for idx in master_outputs_per_frame:
|
| 334 |
+
for k, v_list in master_outputs_per_frame[idx].items():
|
| 335 |
+
if isinstance(v_list, list) and len(v_list) > 0:
|
| 336 |
+
if torch.is_tensor(v_list[0]):
|
| 337 |
+
master_outputs_per_frame[idx][k] = torch.cat(v_list, dim=0)
|
| 338 |
+
elif isinstance(v_list[0], np.ndarray):
|
| 339 |
+
master_outputs_per_frame[idx][k] = np.concatenate(v_list, axis=0)
|
| 340 |
+
|
| 341 |
+
# Feed the fully unified master dictionary to the visualizer
|
| 342 |
save_masklet_video(video_frames, master_outputs_per_frame, out_path=out_path, alpha=0.5, fps=video_fps)
|
| 343 |
|
| 344 |
progress(1.0, desc="Done!")
|