Harshil07 commited on
Commit
66fd774
·
verified ·
1 Parent(s): 62ffad8

Update app.py

Browse files

video multi class added

Files changed (1) hide show
  1. app.py +20 -11
app.py CHANGED
@@ -20,10 +20,11 @@ import gradio as gr
20
  from PIL import Image, ImageDraw
21
  from huggingface_hub import login
22
  import spaces
 
23
  # ----------------------------------------------------------------------
24
  # Auth (Space secret) -- gated checkpoint download needs this at startup.
25
  # ----------------------------------------------------------------------
26
- HF_TOKEN = os.environ.get("hf_token")
27
  if HF_TOKEN:
28
  login(token=HF_TOKEN)
29
  print("Logged in to Hugging Face using HF_TOKEN secret.")
@@ -266,10 +267,8 @@ def run_sam3(file_path, prompt, progress=gr.Progress()):
266
  progress(0.1, desc="Loading video predictor...")
267
  from sam3.visualization_utils import save_masklet_video
268
  video_predictor = get_video_predictor()
269
- video_prompt = class_names[0]
270
- note = ""
271
- if len(class_names) > 1:
272
- note = f" (only first class '{video_prompt}' is tracked for video)"
273
 
274
  progress(0.2, desc="Reading video frames...")
275
  cap = cv2.VideoCapture(file_path)
@@ -282,15 +281,25 @@ def run_sam3(file_path, prompt, progress=gr.Progress()):
282
  video_frames.append(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
283
  cap.release()
284
 
285
- progress(0.4, desc="Tracking object through video...")
286
  with torch.inference_mode(), torch.autocast(device_type=DEVICE, dtype=torch.bfloat16):
287
  response = video_predictor.handle_request(
288
  request=dict(type="start_session", resource_path=file_path)
289
  )
290
  session_id = response["session_id"]
291
- video_predictor.handle_request(
292
- request=dict(type="add_prompt", session_id=session_id, frame_index=0, text=video_prompt)
293
- )
 
 
 
 
 
 
 
 
 
 
294
  outputs_per_frame = propagate_in_video(video_predictor, session_id)
295
 
296
  progress(0.85, desc="Rendering overlay video...")
@@ -300,7 +309,7 @@ def run_sam3(file_path, prompt, progress=gr.Progress()):
300
  video_predictor.handle_request(request=dict(type="close_session", session_id=session_id))
301
  progress(1.0, desc="Done!")
302
 
303
- status = f"Tracked {len(outputs_per_frame)} frames for '{video_prompt}'.{note}"
304
  return (
305
  gr.update(visible=False),
306
  gr.update(value=out_path, visible=True),
@@ -344,4 +353,4 @@ with gr.Blocks(title="SAM 3 Segmentation") as demo:
344
  )
345
 
346
  if __name__ == "__main__":
347
- demo.launch()
 
20
  from PIL import Image, ImageDraw
21
  from huggingface_hub import login
22
  import spaces
23
+
24
  # ----------------------------------------------------------------------
25
  # Auth (Space secret) -- gated checkpoint download needs this at startup.
26
  # ----------------------------------------------------------------------
27
+ HF_TOKEN = os.environ.get("HF_TOKEN")
28
  if HF_TOKEN:
29
  login(token=HF_TOKEN)
30
  print("Logged in to Hugging Face using HF_TOKEN secret.")
 
267
  progress(0.1, desc="Loading video predictor...")
268
  from sam3.visualization_utils import save_masklet_video
269
  video_predictor = get_video_predictor()
270
+
271
+ tracked_classes_str = ", ".join(class_names)
 
 
272
 
273
  progress(0.2, desc="Reading video frames...")
274
  cap = cv2.VideoCapture(file_path)
 
281
  video_frames.append(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
282
  cap.release()
283
 
284
+ progress(0.4, desc="Tracking objects through video...")
285
  with torch.inference_mode(), torch.autocast(device_type=DEVICE, dtype=torch.bfloat16):
286
  response = video_predictor.handle_request(
287
  request=dict(type="start_session", resource_path=file_path)
288
  )
289
  session_id = response["session_id"]
290
+
291
+ # Loop through ALL classes and add them to the session with unique obj_ids
292
+ for obj_idx, class_name in enumerate(class_names):
293
+ video_predictor.handle_request(
294
+ request=dict(
295
+ type="add_prompt",
296
+ session_id=session_id,
297
+ frame_index=0,
298
+ obj_id=obj_idx,
299
+ text=class_name
300
+ )
301
+ )
302
+
303
  outputs_per_frame = propagate_in_video(video_predictor, session_id)
304
 
305
  progress(0.85, desc="Rendering overlay video...")
 
309
  video_predictor.handle_request(request=dict(type="close_session", session_id=session_id))
310
  progress(1.0, desc="Done!")
311
 
312
+ status = f"Tracked {len(outputs_per_frame)} frames for: {tracked_classes_str}"
313
  return (
314
  gr.update(visible=False),
315
  gr.update(value=out_path, visible=True),
 
353
  )
354
 
355
  if __name__ == "__main__":
356
+ demo.launch()