Spaces:
Running
on
Zero
Running
on
Zero
initial commit
Browse files- .gitattributes +4 -0
- app.py +971 -0
- deers.mp4 +3 -0
- foot.mp4 +3 -0
- penguins.mp4 +3 -0
- requirements.txt +8 -0
- transformers-5.0.0.dev0-py3-none-any.whl +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,7 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
deers.mp4 filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
foot.mp4 filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
penguins.mp4 filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
transformers-5.0.0.dev0-py3-none-any.whl filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,971 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
os.system("pip install --upgrade ./transformers-5.0.0.dev0-py3-none-any.whl")
|
| 4 |
+
import colorsys
|
| 5 |
+
import gc
|
| 6 |
+
import os
|
| 7 |
+
from typing import Optional
|
| 8 |
+
|
| 9 |
+
import cv2
|
| 10 |
+
import gradio as gr
|
| 11 |
+
import numpy as np
|
| 12 |
+
import torch
|
| 13 |
+
from gradio.themes import Soft
|
| 14 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 15 |
+
from transformers import Sam3TrackerVideoModel, Sam3TrackerVideoProcessor, Sam3VideoModel, Sam3VideoProcessor
|
| 16 |
+
|
| 17 |
+
def get_device_and_dtype() -> tuple[str, torch.dtype]:
|
| 18 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 19 |
+
dtype = torch.bfloat16
|
| 20 |
+
return device, dtype
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
_GLOBAL_DEVICE, _GLOBAL_DTYPE = get_device_and_dtype()
|
| 24 |
+
_GLOBAL_MODEL_REPO_ID = "facebook/sam3"
|
| 25 |
+
_GLOBAL_TOKEN = os.getenv("HF_TOKEN")
|
| 26 |
+
|
| 27 |
+
_GLOBAL_TRACKER_MODEL = Sam3TrackerVideoModel.from_pretrained(
|
| 28 |
+
_GLOBAL_MODEL_REPO_ID, torch_dtype=_GLOBAL_DTYPE, device_map=_GLOBAL_DEVICE
|
| 29 |
+
).eval()
|
| 30 |
+
_GLOBAL_TRACKER_PROCESSOR = Sam3TrackerVideoProcessor.from_pretrained(_GLOBAL_MODEL_REPO_ID, token=_GLOBAL_TOKEN)
|
| 31 |
+
|
| 32 |
+
_GLOBAL_TEXT_VIDEO_MODEL = Sam3VideoModel.from_pretrained(_GLOBAL_MODEL_REPO_ID, token=_GLOBAL_TOKEN)
|
| 33 |
+
_GLOBAL_TEXT_VIDEO_MODEL = _GLOBAL_TEXT_VIDEO_MODEL.to(_GLOBAL_DEVICE, dtype=_GLOBAL_DTYPE).eval()
|
| 34 |
+
_GLOBAL_TEXT_VIDEO_PROCESSOR = Sam3VideoProcessor.from_pretrained(_GLOBAL_MODEL_REPO_ID, token=_GLOBAL_TOKEN)
|
| 35 |
+
print("Models loaded successfully!")
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def try_load_video_frames(video_path_or_url: str) -> tuple[list[Image.Image], dict]:
|
| 39 |
+
cap = cv2.VideoCapture(video_path_or_url)
|
| 40 |
+
frames = []
|
| 41 |
+
while cap.isOpened():
|
| 42 |
+
ret, frame = cap.read()
|
| 43 |
+
if not ret:
|
| 44 |
+
break
|
| 45 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 46 |
+
frames.append(Image.fromarray(frame_rgb))
|
| 47 |
+
fps_val = cap.get(cv2.CAP_PROP_FPS)
|
| 48 |
+
cap.release()
|
| 49 |
+
info = {
|
| 50 |
+
"num_frames": len(frames),
|
| 51 |
+
"fps": float(fps_val) if fps_val and fps_val > 0 else None,
|
| 52 |
+
}
|
| 53 |
+
return frames, info
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def overlay_masks_on_frame(
|
| 57 |
+
frame: Image.Image,
|
| 58 |
+
masks_per_object: dict[int, np.ndarray],
|
| 59 |
+
color_by_obj: dict[int, tuple[int, int, int]],
|
| 60 |
+
alpha: float = 0.5,
|
| 61 |
+
) -> Image.Image:
|
| 62 |
+
base = np.array(frame).astype(np.float32) / 255.0
|
| 63 |
+
height, width = base.shape[:2]
|
| 64 |
+
overlay = base.copy()
|
| 65 |
+
|
| 66 |
+
for obj_id, mask in masks_per_object.items():
|
| 67 |
+
if mask is None:
|
| 68 |
+
continue
|
| 69 |
+
if mask.dtype != np.float32:
|
| 70 |
+
mask = mask.astype(np.float32)
|
| 71 |
+
if mask.ndim == 3:
|
| 72 |
+
mask = mask.squeeze()
|
| 73 |
+
mask = np.clip(mask, 0.0, 1.0)
|
| 74 |
+
color = np.array(color_by_obj.get(obj_id, (255, 0, 0)), dtype=np.float32) / 255.0
|
| 75 |
+
a = alpha
|
| 76 |
+
m = mask[..., None]
|
| 77 |
+
overlay = (1.0 - a * m) * overlay + (a * m) * color
|
| 78 |
+
|
| 79 |
+
out = np.clip(overlay * 255.0, 0, 255).astype(np.uint8)
|
| 80 |
+
return Image.fromarray(out)
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def pastel_color_for_object(obj_id: int) -> tuple[int, int, int]:
|
| 84 |
+
golden_ratio_conjugate = 0.61
|
| 85 |
+
hue = (obj_id * golden_ratio_conjugate) % 1.0
|
| 86 |
+
saturation = 0.45
|
| 87 |
+
value = 1.0
|
| 88 |
+
r_f, g_f, b_f = colorsys.hsv_to_rgb(hue, saturation, value)
|
| 89 |
+
return int(r_f * 255), int(g_f * 255), int(b_f * 255)
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
class AppState:
|
| 93 |
+
def __init__(self):
|
| 94 |
+
self.reset()
|
| 95 |
+
|
| 96 |
+
def reset(self):
|
| 97 |
+
self.video_frames: list[Image.Image] = []
|
| 98 |
+
self.inference_session = None
|
| 99 |
+
self.video_fps: float | None = None
|
| 100 |
+
self.masks_by_frame: dict[int, dict[int, np.ndarray]] = {}
|
| 101 |
+
self.color_by_obj: dict[int, tuple[int, int, int]] = {}
|
| 102 |
+
self.clicks_by_frame_obj: dict[int, dict[int, list[tuple[int, int, int]]]] = {}
|
| 103 |
+
self.boxes_by_frame_obj: dict[int, dict[int, list[tuple[int, int, int, int]]]] = {}
|
| 104 |
+
self.text_prompts_by_frame_obj: dict[int, dict[int, str]] = {}
|
| 105 |
+
self.composited_frames: dict[int, Image.Image] = {}
|
| 106 |
+
self.current_frame_idx: int = 0
|
| 107 |
+
self.current_obj_id: int = 1
|
| 108 |
+
self.current_label: str = "positive"
|
| 109 |
+
self.current_clear_old: bool = True
|
| 110 |
+
self.current_prompt_type: str = "Points"
|
| 111 |
+
self.pending_box_start: tuple[int, int] | None = None
|
| 112 |
+
self.pending_box_start_frame_idx: int | None = None
|
| 113 |
+
self.pending_box_start_obj_id: int | None = None
|
| 114 |
+
self.active_tab: str = "point_box"
|
| 115 |
+
|
| 116 |
+
def __repr__(self):
|
| 117 |
+
return f"AppState(video_frames={len(self.video_frames)}, video_fps={self.video_fps}, masks_by_frame={len(self.masks_by_frame)}, color_by_obj={len(self.color_by_obj)})"
|
| 118 |
+
|
| 119 |
+
@property
|
| 120 |
+
def num_frames(self) -> int:
|
| 121 |
+
return len(self.video_frames)
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
def init_video_session(
|
| 127 |
+
GLOBAL_STATE: gr.State, video: str | dict, active_tab: str = "point_box"
|
| 128 |
+
) -> tuple[AppState, int, int, Image.Image, str]:
|
| 129 |
+
GLOBAL_STATE.video_frames = []
|
| 130 |
+
GLOBAL_STATE.masks_by_frame = {}
|
| 131 |
+
GLOBAL_STATE.color_by_obj = {}
|
| 132 |
+
GLOBAL_STATE.text_prompts_by_frame_obj = {}
|
| 133 |
+
GLOBAL_STATE.clicks_by_frame_obj = {}
|
| 134 |
+
GLOBAL_STATE.boxes_by_frame_obj = {}
|
| 135 |
+
GLOBAL_STATE.composited_frames = {}
|
| 136 |
+
GLOBAL_STATE.inference_session = None
|
| 137 |
+
GLOBAL_STATE.active_tab = active_tab
|
| 138 |
+
|
| 139 |
+
device = _GLOBAL_DEVICE
|
| 140 |
+
dtype = _GLOBAL_DTYPE
|
| 141 |
+
|
| 142 |
+
video_path: Optional[str] = None
|
| 143 |
+
if isinstance(video, dict):
|
| 144 |
+
video_path = video.get("name") or video.get("path") or video.get("data")
|
| 145 |
+
elif isinstance(video, str):
|
| 146 |
+
video_path = video
|
| 147 |
+
else:
|
| 148 |
+
video_path = None
|
| 149 |
+
|
| 150 |
+
if not video_path:
|
| 151 |
+
raise gr.Error("Invalid video input.")
|
| 152 |
+
|
| 153 |
+
frames, info = try_load_video_frames(video_path)
|
| 154 |
+
if len(frames) == 0:
|
| 155 |
+
raise gr.Error("No frames could be loaded from the video.")
|
| 156 |
+
|
| 157 |
+
MAX_SECONDS = 8.0
|
| 158 |
+
trimmed_note = ""
|
| 159 |
+
fps_in = info.get("fps")
|
| 160 |
+
max_frames_allowed = int(MAX_SECONDS * fps_in) if fps_in else len(frames)
|
| 161 |
+
if len(frames) > max_frames_allowed:
|
| 162 |
+
frames = frames[:max_frames_allowed]
|
| 163 |
+
trimmed_note = f" (trimmed to {int(MAX_SECONDS)}s = {len(frames)} frames)"
|
| 164 |
+
if isinstance(info, dict):
|
| 165 |
+
info["num_frames"] = len(frames)
|
| 166 |
+
GLOBAL_STATE.video_frames = frames
|
| 167 |
+
GLOBAL_STATE.video_fps = float(fps_in) if fps_in else None
|
| 168 |
+
|
| 169 |
+
raw_video = [np.array(frame) for frame in frames]
|
| 170 |
+
|
| 171 |
+
if active_tab == "text":
|
| 172 |
+
processor = _GLOBAL_TEXT_VIDEO_PROCESSOR
|
| 173 |
+
GLOBAL_STATE.inference_session = processor.init_video_session(
|
| 174 |
+
video=frames,
|
| 175 |
+
inference_device=device,
|
| 176 |
+
processing_device="cpu",
|
| 177 |
+
video_storage_device="cpu",
|
| 178 |
+
dtype=dtype,
|
| 179 |
+
)
|
| 180 |
+
else:
|
| 181 |
+
processor = _GLOBAL_TRACKER_PROCESSOR
|
| 182 |
+
GLOBAL_STATE.inference_session = processor.init_video_session(
|
| 183 |
+
video=raw_video,
|
| 184 |
+
inference_device=device,
|
| 185 |
+
video_storage_device=device,
|
| 186 |
+
processing_device=device,
|
| 187 |
+
inference_state_device=device,
|
| 188 |
+
dtype=dtype,
|
| 189 |
+
max_vision_features_cache_size=1,
|
| 190 |
+
)
|
| 191 |
+
|
| 192 |
+
first_frame = frames[0]
|
| 193 |
+
max_idx = len(frames) - 1
|
| 194 |
+
if active_tab == "text":
|
| 195 |
+
status = (
|
| 196 |
+
f"Loaded {len(frames)} frames @ {GLOBAL_STATE.video_fps or 'unknown'} fps{trimmed_note}. "
|
| 197 |
+
f"Device: {device}, dtype: bfloat16. Ready for text prompting."
|
| 198 |
+
)
|
| 199 |
+
else:
|
| 200 |
+
status = (
|
| 201 |
+
f"Loaded {len(frames)} frames @ {GLOBAL_STATE.video_fps or 'unknown'} fps{trimmed_note}. "
|
| 202 |
+
f"Device: {device}, dtype: bfloat16. Video session initialized."
|
| 203 |
+
)
|
| 204 |
+
return GLOBAL_STATE, 0, max_idx, first_frame, status
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
def compose_frame(state: AppState, frame_idx: int) -> Image.Image:
|
| 208 |
+
if state is None or state.video_frames is None or len(state.video_frames) == 0:
|
| 209 |
+
return None
|
| 210 |
+
frame_idx = int(np.clip(frame_idx, 0, len(state.video_frames) - 1))
|
| 211 |
+
frame = state.video_frames[frame_idx]
|
| 212 |
+
masks = state.masks_by_frame.get(frame_idx, {})
|
| 213 |
+
out_img = frame
|
| 214 |
+
if len(masks) != 0:
|
| 215 |
+
out_img = overlay_masks_on_frame(out_img, masks, state.color_by_obj, alpha=0.65)
|
| 216 |
+
|
| 217 |
+
clicks_map = state.clicks_by_frame_obj.get(frame_idx)
|
| 218 |
+
if clicks_map:
|
| 219 |
+
draw = ImageDraw.Draw(out_img)
|
| 220 |
+
cross_half = 6
|
| 221 |
+
for obj_id, pts in clicks_map.items():
|
| 222 |
+
for x, y, lbl in pts:
|
| 223 |
+
color = (0, 255, 0) if int(lbl) == 1 else (255, 0, 0)
|
| 224 |
+
draw.line([(x - cross_half, y), (x + cross_half, y)], fill=color, width=2)
|
| 225 |
+
draw.line([(x, y - cross_half), (x, y + cross_half)], fill=color, width=2)
|
| 226 |
+
if (
|
| 227 |
+
state.pending_box_start is not None
|
| 228 |
+
and state.pending_box_start_frame_idx == frame_idx
|
| 229 |
+
and state.pending_box_start_obj_id is not None
|
| 230 |
+
):
|
| 231 |
+
draw = ImageDraw.Draw(out_img)
|
| 232 |
+
x, y = state.pending_box_start
|
| 233 |
+
cross_half = 6
|
| 234 |
+
color = state.color_by_obj.get(state.pending_box_start_obj_id, (255, 255, 255))
|
| 235 |
+
draw.line([(x - cross_half, y), (x + cross_half, y)], fill=color, width=2)
|
| 236 |
+
draw.line([(x, y - cross_half), (x, y + cross_half)], fill=color, width=2)
|
| 237 |
+
box_map = state.boxes_by_frame_obj.get(frame_idx)
|
| 238 |
+
if box_map:
|
| 239 |
+
draw = ImageDraw.Draw(out_img)
|
| 240 |
+
for obj_id, boxes in box_map.items():
|
| 241 |
+
color = state.color_by_obj.get(obj_id, (255, 255, 255))
|
| 242 |
+
for x1, y1, x2, y2 in boxes:
|
| 243 |
+
draw.rectangle([(x1, y1), (x2, y2)], outline=color, width=2)
|
| 244 |
+
|
| 245 |
+
text_prompts_by_obj = {}
|
| 246 |
+
for frame_texts in state.text_prompts_by_frame_obj.values():
|
| 247 |
+
for obj_id, text_prompt in frame_texts.items():
|
| 248 |
+
if obj_id not in text_prompts_by_obj:
|
| 249 |
+
text_prompts_by_obj[obj_id] = text_prompt
|
| 250 |
+
|
| 251 |
+
if text_prompts_by_obj and len(masks) > 0:
|
| 252 |
+
draw = ImageDraw.Draw(out_img)
|
| 253 |
+
font = ImageFont.load_default()
|
| 254 |
+
|
| 255 |
+
for obj_id, text_prompt in text_prompts_by_obj.items():
|
| 256 |
+
obj_mask = masks.get(obj_id)
|
| 257 |
+
if obj_mask is not None:
|
| 258 |
+
mask_array = np.array(obj_mask)
|
| 259 |
+
if mask_array.size > 0 and np.any(mask_array):
|
| 260 |
+
rows = np.any(mask_array, axis=1)
|
| 261 |
+
cols = np.any(mask_array, axis=0)
|
| 262 |
+
if np.any(rows) and np.any(cols):
|
| 263 |
+
y_min, y_max = np.where(rows)[0][[0, -1]]
|
| 264 |
+
x_min, x_max = np.where(cols)[0][[0, -1]]
|
| 265 |
+
label_x = int(x_min)
|
| 266 |
+
label_y = int(y_min) - 20
|
| 267 |
+
label_y = max(5, label_y)
|
| 268 |
+
|
| 269 |
+
obj_color = state.color_by_obj.get(obj_id, (255, 255, 255))
|
| 270 |
+
|
| 271 |
+
# Include object ID in the label
|
| 272 |
+
label_text = f"{text_prompt} (ID: {obj_id})"
|
| 273 |
+
bbox = draw.textbbox((label_x, label_y), label_text, font=font)
|
| 274 |
+
padding = 4
|
| 275 |
+
draw.rectangle(
|
| 276 |
+
[(bbox[0] - padding, bbox[1] - padding), (bbox[2] + padding, bbox[3] + padding)],
|
| 277 |
+
fill=obj_color,
|
| 278 |
+
outline=None,
|
| 279 |
+
width=0,
|
| 280 |
+
)
|
| 281 |
+
draw.text((label_x, label_y), label_text, fill=(255, 255, 255), font=font)
|
| 282 |
+
|
| 283 |
+
state.composited_frames[frame_idx] = out_img
|
| 284 |
+
return out_img
|
| 285 |
+
|
| 286 |
+
|
| 287 |
+
def update_frame_display(state: AppState, frame_idx: int) -> Image.Image:
|
| 288 |
+
if state is None or state.video_frames is None or len(state.video_frames) == 0:
|
| 289 |
+
return None
|
| 290 |
+
frame_idx = int(np.clip(frame_idx, 0, len(state.video_frames) - 1))
|
| 291 |
+
cached = state.composited_frames.get(frame_idx)
|
| 292 |
+
if cached is not None:
|
| 293 |
+
return cached
|
| 294 |
+
return compose_frame(state, frame_idx)
|
| 295 |
+
|
| 296 |
+
|
| 297 |
+
def _ensure_color_for_obj(state: AppState, obj_id: int):
|
| 298 |
+
if obj_id not in state.color_by_obj:
|
| 299 |
+
state.color_by_obj[obj_id] = pastel_color_for_object(obj_id)
|
| 300 |
+
|
| 301 |
+
|
| 302 |
+
def on_image_click(
|
| 303 |
+
img: Image.Image | np.ndarray,
|
| 304 |
+
state: AppState,
|
| 305 |
+
frame_idx: int,
|
| 306 |
+
obj_id: int,
|
| 307 |
+
label: str,
|
| 308 |
+
clear_old: bool,
|
| 309 |
+
evt: gr.SelectData,
|
| 310 |
+
) -> Image.Image:
|
| 311 |
+
if state is None or state.inference_session is None:
|
| 312 |
+
return img
|
| 313 |
+
|
| 314 |
+
model = _GLOBAL_TRACKER_MODEL
|
| 315 |
+
processor = _GLOBAL_TRACKER_PROCESSOR
|
| 316 |
+
|
| 317 |
+
x = y = None
|
| 318 |
+
if evt is not None:
|
| 319 |
+
try:
|
| 320 |
+
if hasattr(evt, "index") and isinstance(evt.index, (list, tuple)) and len(evt.index) == 2:
|
| 321 |
+
x, y = int(evt.index[0]), int(evt.index[1])
|
| 322 |
+
elif hasattr(evt, "value") and isinstance(evt.value, dict) and "x" in evt.value and "y" in evt.value:
|
| 323 |
+
x, y = int(evt.value["x"]), int(evt.value["y"])
|
| 324 |
+
except Exception:
|
| 325 |
+
x = y = None
|
| 326 |
+
|
| 327 |
+
if x is None or y is None:
|
| 328 |
+
raise gr.Error("Could not read click coordinates.")
|
| 329 |
+
|
| 330 |
+
_ensure_color_for_obj(state, int(obj_id))
|
| 331 |
+
ann_frame_idx = int(frame_idx)
|
| 332 |
+
ann_obj_id = int(obj_id)
|
| 333 |
+
|
| 334 |
+
if state.current_prompt_type == "Boxes":
|
| 335 |
+
if state.pending_box_start is None:
|
| 336 |
+
frame_clicks = state.clicks_by_frame_obj.setdefault(ann_frame_idx, {})
|
| 337 |
+
frame_clicks[ann_obj_id] = []
|
| 338 |
+
state.composited_frames.pop(ann_frame_idx, None)
|
| 339 |
+
state.pending_box_start = (int(x), int(y))
|
| 340 |
+
state.pending_box_start_frame_idx = ann_frame_idx
|
| 341 |
+
state.pending_box_start_obj_id = ann_obj_id
|
| 342 |
+
state.composited_frames.pop(ann_frame_idx, None)
|
| 343 |
+
return update_frame_display(state, ann_frame_idx)
|
| 344 |
+
else:
|
| 345 |
+
x1, y1 = state.pending_box_start
|
| 346 |
+
x2, y2 = int(x), int(y)
|
| 347 |
+
state.pending_box_start = None
|
| 348 |
+
state.pending_box_start_frame_idx = None
|
| 349 |
+
state.pending_box_start_obj_id = None
|
| 350 |
+
state.composited_frames.pop(ann_frame_idx, None)
|
| 351 |
+
x_min, y_min = min(x1, x2), min(y1, y2)
|
| 352 |
+
x_max, y_max = max(x1, x2), max(y1, y2)
|
| 353 |
+
|
| 354 |
+
box = [[[x_min, y_min, x_max, y_max]]]
|
| 355 |
+
processor.add_inputs_to_inference_session(
|
| 356 |
+
inference_session=state.inference_session,
|
| 357 |
+
frame_idx=ann_frame_idx,
|
| 358 |
+
obj_ids=ann_obj_id,
|
| 359 |
+
input_boxes=box,
|
| 360 |
+
)
|
| 361 |
+
|
| 362 |
+
frame_boxes = state.boxes_by_frame_obj.setdefault(ann_frame_idx, {})
|
| 363 |
+
obj_boxes = frame_boxes.setdefault(ann_obj_id, [])
|
| 364 |
+
obj_boxes.clear()
|
| 365 |
+
obj_boxes.append((x_min, y_min, x_max, y_max))
|
| 366 |
+
state.composited_frames.pop(ann_frame_idx, None)
|
| 367 |
+
else:
|
| 368 |
+
label_int = 1 if str(label).lower().startswith("pos") else 0
|
| 369 |
+
|
| 370 |
+
frame_clicks = state.clicks_by_frame_obj.setdefault(ann_frame_idx, {})
|
| 371 |
+
obj_clicks = frame_clicks.setdefault(ann_obj_id, [])
|
| 372 |
+
|
| 373 |
+
if bool(clear_old):
|
| 374 |
+
obj_clicks.clear()
|
| 375 |
+
frame_boxes = state.boxes_by_frame_obj.setdefault(ann_frame_idx, {})
|
| 376 |
+
frame_boxes[ann_obj_id] = []
|
| 377 |
+
if hasattr(state.inference_session, "reset_inference_session"):
|
| 378 |
+
pass
|
| 379 |
+
|
| 380 |
+
obj_clicks.append((int(x), int(y), int(label_int)))
|
| 381 |
+
|
| 382 |
+
points = [[[[click[0], click[1]] for click in obj_clicks]]]
|
| 383 |
+
labels = [[[click[2] for click in obj_clicks]]]
|
| 384 |
+
|
| 385 |
+
processor.add_inputs_to_inference_session(
|
| 386 |
+
inference_session=state.inference_session,
|
| 387 |
+
frame_idx=ann_frame_idx,
|
| 388 |
+
obj_ids=ann_obj_id,
|
| 389 |
+
input_points=points,
|
| 390 |
+
input_labels=labels,
|
| 391 |
+
)
|
| 392 |
+
state.composited_frames.pop(ann_frame_idx, None)
|
| 393 |
+
|
| 394 |
+
with torch.no_grad():
|
| 395 |
+
outputs = model(
|
| 396 |
+
inference_session=state.inference_session,
|
| 397 |
+
frame_idx=ann_frame_idx,
|
| 398 |
+
)
|
| 399 |
+
|
| 400 |
+
out_mask_logits = processor.post_process_masks(
|
| 401 |
+
[outputs.pred_masks],
|
| 402 |
+
[[state.inference_session.video_height, state.inference_session.video_width]],
|
| 403 |
+
binarize=False,
|
| 404 |
+
)[0]
|
| 405 |
+
|
| 406 |
+
mask_2d = (out_mask_logits[0] > 0.0).cpu().numpy()
|
| 407 |
+
masks_for_frame = state.masks_by_frame.setdefault(ann_frame_idx, {})
|
| 408 |
+
masks_for_frame[ann_obj_id] = mask_2d
|
| 409 |
+
|
| 410 |
+
state.composited_frames.pop(ann_frame_idx, None)
|
| 411 |
+
|
| 412 |
+
return update_frame_display(state, ann_frame_idx)
|
| 413 |
+
|
| 414 |
+
|
| 415 |
+
def on_text_prompt(
|
| 416 |
+
state: AppState,
|
| 417 |
+
frame_idx: int,
|
| 418 |
+
text_prompt: str,
|
| 419 |
+
) -> tuple[Image.Image, str]:
|
| 420 |
+
if state is None or state.inference_session is None:
|
| 421 |
+
return None, "Upload a video and enter text prompt."
|
| 422 |
+
|
| 423 |
+
model = _GLOBAL_TEXT_VIDEO_MODEL
|
| 424 |
+
processor = _GLOBAL_TEXT_VIDEO_PROCESSOR
|
| 425 |
+
|
| 426 |
+
if not text_prompt or not text_prompt.strip():
|
| 427 |
+
return update_frame_display(state, int(frame_idx)), "Please enter a text prompt."
|
| 428 |
+
|
| 429 |
+
frame_idx = int(np.clip(frame_idx, 0, len(state.video_frames) - 1))
|
| 430 |
+
|
| 431 |
+
state.inference_session = processor.add_text_prompt(
|
| 432 |
+
inference_session=state.inference_session,
|
| 433 |
+
text=text_prompt.strip(),
|
| 434 |
+
)
|
| 435 |
+
|
| 436 |
+
masks_for_frame = state.masks_by_frame.setdefault(frame_idx, {})
|
| 437 |
+
frame_texts = state.text_prompts_by_frame_obj.setdefault(int(frame_idx), {})
|
| 438 |
+
|
| 439 |
+
num_objects = 0
|
| 440 |
+
detected_obj_ids = []
|
| 441 |
+
with torch.no_grad():
|
| 442 |
+
for model_outputs in model.propagate_in_video_iterator(
|
| 443 |
+
inference_session=state.inference_session,
|
| 444 |
+
start_frame_idx=frame_idx,
|
| 445 |
+
max_frame_num_to_track=1,
|
| 446 |
+
):
|
| 447 |
+
processed_outputs = processor.postprocess_outputs(
|
| 448 |
+
state.inference_session,
|
| 449 |
+
model_outputs,
|
| 450 |
+
)
|
| 451 |
+
|
| 452 |
+
current_frame_idx = model_outputs.frame_idx
|
| 453 |
+
if current_frame_idx == frame_idx:
|
| 454 |
+
object_ids = processed_outputs["object_ids"]
|
| 455 |
+
masks = processed_outputs["masks"]
|
| 456 |
+
scores = processed_outputs["scores"]
|
| 457 |
+
|
| 458 |
+
num_objects = len(object_ids)
|
| 459 |
+
if num_objects > 0:
|
| 460 |
+
if len(scores) > 0:
|
| 461 |
+
sorted_indices = torch.argsort(scores, descending=True).cpu().tolist()
|
| 462 |
+
else:
|
| 463 |
+
sorted_indices = list(range(num_objects))
|
| 464 |
+
|
| 465 |
+
for mask_idx in sorted_indices:
|
| 466 |
+
current_obj_id = int(object_ids[mask_idx].item())
|
| 467 |
+
detected_obj_ids.append(current_obj_id)
|
| 468 |
+
_ensure_color_for_obj(state, current_obj_id)
|
| 469 |
+
mask_2d = masks[mask_idx].float().cpu().numpy()
|
| 470 |
+
if mask_2d.ndim == 3:
|
| 471 |
+
mask_2d = mask_2d.squeeze()
|
| 472 |
+
mask_2d = (mask_2d > 0.0).astype(np.float32)
|
| 473 |
+
masks_for_frame[current_obj_id] = mask_2d
|
| 474 |
+
frame_texts[current_obj_id] = text_prompt.strip()
|
| 475 |
+
|
| 476 |
+
state.composited_frames.pop(frame_idx, None)
|
| 477 |
+
|
| 478 |
+
if detected_obj_ids:
|
| 479 |
+
obj_ids_str = ", ".join(map(str, detected_obj_ids))
|
| 480 |
+
status = f"Processed text prompt '{text_prompt.strip()}' on frame {frame_idx}. Found {num_objects} object(s) with IDs: {obj_ids_str}."
|
| 481 |
+
else:
|
| 482 |
+
status = f"Processed text prompt '{text_prompt.strip()}' on frame {frame_idx}. No objects detected."
|
| 483 |
+
return update_frame_display(state, int(frame_idx)), status
|
| 484 |
+
|
| 485 |
+
|
| 486 |
+
def propagate_masks(GLOBAL_STATE: gr.State):
|
| 487 |
+
if GLOBAL_STATE is None:
|
| 488 |
+
return GLOBAL_STATE, "Load a video first.", gr.update()
|
| 489 |
+
|
| 490 |
+
if GLOBAL_STATE.active_tab != "text" and GLOBAL_STATE.inference_session is None:
|
| 491 |
+
return GLOBAL_STATE, "Load a video first.", gr.update()
|
| 492 |
+
|
| 493 |
+
total = max(1, GLOBAL_STATE.num_frames)
|
| 494 |
+
processed = 0
|
| 495 |
+
|
| 496 |
+
yield GLOBAL_STATE, f"Propagating masks: {processed}/{total}", gr.update()
|
| 497 |
+
|
| 498 |
+
last_frame_idx = 0
|
| 499 |
+
|
| 500 |
+
with torch.no_grad():
|
| 501 |
+
if GLOBAL_STATE.active_tab == "text":
|
| 502 |
+
if GLOBAL_STATE.inference_session is None:
|
| 503 |
+
yield GLOBAL_STATE, "Text video model not loaded.", gr.update()
|
| 504 |
+
return
|
| 505 |
+
|
| 506 |
+
model = _GLOBAL_TEXT_VIDEO_MODEL
|
| 507 |
+
processor = _GLOBAL_TEXT_VIDEO_PROCESSOR
|
| 508 |
+
|
| 509 |
+
text_prompt_to_obj_ids = {}
|
| 510 |
+
for frame_idx, frame_texts in GLOBAL_STATE.text_prompts_by_frame_obj.items():
|
| 511 |
+
for obj_id, text_prompt in frame_texts.items():
|
| 512 |
+
if text_prompt not in text_prompt_to_obj_ids:
|
| 513 |
+
text_prompt_to_obj_ids[text_prompt] = []
|
| 514 |
+
if obj_id not in text_prompt_to_obj_ids[text_prompt]:
|
| 515 |
+
text_prompt_to_obj_ids[text_prompt].append(obj_id)
|
| 516 |
+
|
| 517 |
+
for text_prompt in text_prompt_to_obj_ids:
|
| 518 |
+
text_prompt_to_obj_ids[text_prompt].sort()
|
| 519 |
+
|
| 520 |
+
if not text_prompt_to_obj_ids:
|
| 521 |
+
yield GLOBAL_STATE, "No text prompts found. Please add a text prompt first.", gr.update()
|
| 522 |
+
return
|
| 523 |
+
|
| 524 |
+
for text_prompt in text_prompt_to_obj_ids.keys():
|
| 525 |
+
GLOBAL_STATE.inference_session = processor.add_text_prompt(
|
| 526 |
+
inference_session=GLOBAL_STATE.inference_session,
|
| 527 |
+
text=text_prompt,
|
| 528 |
+
)
|
| 529 |
+
|
| 530 |
+
earliest_frame = (
|
| 531 |
+
min(GLOBAL_STATE.text_prompts_by_frame_obj.keys()) if GLOBAL_STATE.text_prompts_by_frame_obj else 0
|
| 532 |
+
)
|
| 533 |
+
|
| 534 |
+
frames_to_track = GLOBAL_STATE.num_frames - earliest_frame
|
| 535 |
+
|
| 536 |
+
outputs_per_frame = {}
|
| 537 |
+
|
| 538 |
+
for model_outputs in model.propagate_in_video_iterator(
|
| 539 |
+
inference_session=GLOBAL_STATE.inference_session,
|
| 540 |
+
start_frame_idx=earliest_frame,
|
| 541 |
+
max_frame_num_to_track=frames_to_track,
|
| 542 |
+
):
|
| 543 |
+
processed_outputs = processor.postprocess_outputs(
|
| 544 |
+
GLOBAL_STATE.inference_session,
|
| 545 |
+
model_outputs,
|
| 546 |
+
)
|
| 547 |
+
frame_idx = model_outputs.frame_idx
|
| 548 |
+
outputs_per_frame[frame_idx] = processed_outputs
|
| 549 |
+
|
| 550 |
+
object_ids = processed_outputs["object_ids"]
|
| 551 |
+
masks = processed_outputs["masks"]
|
| 552 |
+
scores = processed_outputs["scores"]
|
| 553 |
+
|
| 554 |
+
masks_for_frame = GLOBAL_STATE.masks_by_frame.setdefault(frame_idx, {})
|
| 555 |
+
frame_texts = GLOBAL_STATE.text_prompts_by_frame_obj.setdefault(frame_idx, {})
|
| 556 |
+
|
| 557 |
+
num_objects = len(object_ids)
|
| 558 |
+
if num_objects > 0:
|
| 559 |
+
if len(scores) > 0:
|
| 560 |
+
sorted_indices = torch.argsort(scores, descending=True).cpu().tolist()
|
| 561 |
+
else:
|
| 562 |
+
sorted_indices = list(range(num_objects))
|
| 563 |
+
|
| 564 |
+
for mask_idx in sorted_indices:
|
| 565 |
+
current_obj_id = int(object_ids[mask_idx].item())
|
| 566 |
+
_ensure_color_for_obj(GLOBAL_STATE, current_obj_id)
|
| 567 |
+
mask_2d = masks[mask_idx].float().cpu().numpy()
|
| 568 |
+
if mask_2d.ndim == 3:
|
| 569 |
+
mask_2d = mask_2d.squeeze()
|
| 570 |
+
mask_2d = (mask_2d > 0.0).astype(np.float32)
|
| 571 |
+
masks_for_frame[current_obj_id] = mask_2d
|
| 572 |
+
|
| 573 |
+
found_prompt = None
|
| 574 |
+
for existing_frame_idx, existing_frame_texts in GLOBAL_STATE.text_prompts_by_frame_obj.items():
|
| 575 |
+
if current_obj_id in existing_frame_texts:
|
| 576 |
+
found_prompt = existing_frame_texts[current_obj_id]
|
| 577 |
+
break
|
| 578 |
+
|
| 579 |
+
if found_prompt is None and text_prompt_to_obj_ids:
|
| 580 |
+
found_prompt = list(text_prompt_to_obj_ids.keys())[0]
|
| 581 |
+
|
| 582 |
+
if found_prompt:
|
| 583 |
+
frame_texts[current_obj_id] = found_prompt
|
| 584 |
+
|
| 585 |
+
GLOBAL_STATE.composited_frames.pop(frame_idx, None)
|
| 586 |
+
last_frame_idx = frame_idx
|
| 587 |
+
processed += 1
|
| 588 |
+
if processed % 30 == 0 or processed == total:
|
| 589 |
+
yield GLOBAL_STATE, f"Propagating masks: {processed}/{total}", gr.update(value=frame_idx)
|
| 590 |
+
else:
|
| 591 |
+
if GLOBAL_STATE.inference_session is None:
|
| 592 |
+
yield GLOBAL_STATE, "Tracker model not loaded.", gr.update()
|
| 593 |
+
return
|
| 594 |
+
|
| 595 |
+
model = _GLOBAL_TRACKER_MODEL
|
| 596 |
+
processor = _GLOBAL_TRACKER_PROCESSOR
|
| 597 |
+
|
| 598 |
+
for sam2_video_output in model.propagate_in_video_iterator(
|
| 599 |
+
inference_session=GLOBAL_STATE.inference_session
|
| 600 |
+
):
|
| 601 |
+
video_res_masks = processor.post_process_masks(
|
| 602 |
+
[sam2_video_output.pred_masks],
|
| 603 |
+
original_sizes=[
|
| 604 |
+
[GLOBAL_STATE.inference_session.video_height, GLOBAL_STATE.inference_session.video_width]
|
| 605 |
+
],
|
| 606 |
+
)[0]
|
| 607 |
+
|
| 608 |
+
frame_idx = sam2_video_output.frame_idx
|
| 609 |
+
for i, out_obj_id in enumerate(GLOBAL_STATE.inference_session.obj_ids):
|
| 610 |
+
_ensure_color_for_obj(GLOBAL_STATE, int(out_obj_id))
|
| 611 |
+
mask_2d = video_res_masks[i].cpu().numpy()
|
| 612 |
+
masks_for_frame = GLOBAL_STATE.masks_by_frame.setdefault(frame_idx, {})
|
| 613 |
+
masks_for_frame[int(out_obj_id)] = mask_2d
|
| 614 |
+
GLOBAL_STATE.composited_frames.pop(frame_idx, None)
|
| 615 |
+
|
| 616 |
+
last_frame_idx = frame_idx
|
| 617 |
+
processed += 1
|
| 618 |
+
if processed % 30 == 0 or processed == total:
|
| 619 |
+
yield GLOBAL_STATE, f"Propagating masks: {processed}/{total}", gr.update(value=frame_idx)
|
| 620 |
+
|
| 621 |
+
text = f"Propagated masks across {processed} frames."
|
| 622 |
+
yield GLOBAL_STATE, text, gr.update(value=last_frame_idx)
|
| 623 |
+
|
| 624 |
+
|
| 625 |
+
def reset_session(GLOBAL_STATE: gr.State) -> tuple[AppState, Image.Image, int, int, str]:
|
| 626 |
+
if not GLOBAL_STATE.video_frames:
|
| 627 |
+
return GLOBAL_STATE, None, 0, 0, "Session reset. Load a new video."
|
| 628 |
+
|
| 629 |
+
if GLOBAL_STATE.active_tab == "text":
|
| 630 |
+
if GLOBAL_STATE.video_frames:
|
| 631 |
+
processor = _GLOBAL_TEXT_VIDEO_PROCESSOR
|
| 632 |
+
GLOBAL_STATE.inference_session = processor.init_video_session(
|
| 633 |
+
video=GLOBAL_STATE.video_frames,
|
| 634 |
+
inference_device=_GLOBAL_DEVICE,
|
| 635 |
+
processing_device="cpu",
|
| 636 |
+
video_storage_device="cpu",
|
| 637 |
+
dtype=_GLOBAL_DTYPE,
|
| 638 |
+
)
|
| 639 |
+
elif GLOBAL_STATE.inference_session is not None and hasattr(
|
| 640 |
+
GLOBAL_STATE.inference_session, "reset_inference_session"
|
| 641 |
+
):
|
| 642 |
+
GLOBAL_STATE.inference_session.reset_inference_session()
|
| 643 |
+
else:
|
| 644 |
+
if GLOBAL_STATE.video_frames:
|
| 645 |
+
processor = _GLOBAL_TRACKER_PROCESSOR
|
| 646 |
+
raw_video = [np.array(frame) for frame in GLOBAL_STATE.video_frames]
|
| 647 |
+
GLOBAL_STATE.inference_session = processor.init_video_session(
|
| 648 |
+
video=raw_video,
|
| 649 |
+
inference_device=_GLOBAL_DEVICE,
|
| 650 |
+
video_storage_device=_GLOBAL_DEVICE,
|
| 651 |
+
processing_device=_GLOBAL_DEVICE,
|
| 652 |
+
inference_state_device=_GLOBAL_DEVICE,
|
| 653 |
+
dtype=_GLOBAL_DTYPE,
|
| 654 |
+
max_vision_features_cache_size=1,
|
| 655 |
+
)
|
| 656 |
+
|
| 657 |
+
GLOBAL_STATE.masks_by_frame.clear()
|
| 658 |
+
GLOBAL_STATE.clicks_by_frame_obj.clear()
|
| 659 |
+
GLOBAL_STATE.boxes_by_frame_obj.clear()
|
| 660 |
+
GLOBAL_STATE.text_prompts_by_frame_obj.clear()
|
| 661 |
+
GLOBAL_STATE.composited_frames.clear()
|
| 662 |
+
GLOBAL_STATE.pending_box_start = None
|
| 663 |
+
GLOBAL_STATE.pending_box_start_frame_idx = None
|
| 664 |
+
GLOBAL_STATE.pending_box_start_obj_id = None
|
| 665 |
+
|
| 666 |
+
gc.collect()
|
| 667 |
+
|
| 668 |
+
current_idx = int(getattr(GLOBAL_STATE, "current_frame_idx", 0))
|
| 669 |
+
current_idx = max(0, min(current_idx, GLOBAL_STATE.num_frames - 1))
|
| 670 |
+
preview_img = update_frame_display(GLOBAL_STATE, current_idx)
|
| 671 |
+
slider_minmax = gr.update(minimum=0, maximum=max(GLOBAL_STATE.num_frames - 1, 0), interactive=True)
|
| 672 |
+
slider_value = gr.update(value=current_idx)
|
| 673 |
+
status = "Session reset. Prompts cleared; video preserved."
|
| 674 |
+
return GLOBAL_STATE, preview_img, slider_minmax, slider_value, status
|
| 675 |
+
|
| 676 |
+
|
| 677 |
+
def _on_video_change_pointbox(GLOBAL_STATE: gr.State, video):
|
| 678 |
+
GLOBAL_STATE, min_idx, max_idx, first_frame, status = init_video_session(GLOBAL_STATE, video, "point_box")
|
| 679 |
+
return (
|
| 680 |
+
GLOBAL_STATE,
|
| 681 |
+
gr.update(minimum=min_idx, maximum=max_idx, value=min_idx, interactive=True),
|
| 682 |
+
first_frame,
|
| 683 |
+
status,
|
| 684 |
+
)
|
| 685 |
+
|
| 686 |
+
|
| 687 |
+
def _on_video_change_text(GLOBAL_STATE: gr.State, video):
|
| 688 |
+
GLOBAL_STATE, min_idx, max_idx, first_frame, status = init_video_session(GLOBAL_STATE, video, "text")
|
| 689 |
+
return (
|
| 690 |
+
GLOBAL_STATE,
|
| 691 |
+
gr.update(minimum=min_idx, maximum=max_idx, value=min_idx, interactive=True),
|
| 692 |
+
first_frame,
|
| 693 |
+
status,
|
| 694 |
+
)
|
| 695 |
+
|
| 696 |
+
|
| 697 |
+
theme = Soft(primary_hue="blue", secondary_hue="rose", neutral_hue="slate")
|
| 698 |
+
|
| 699 |
+
with gr.Blocks(title="SAM3", theme=theme) as demo:
|
| 700 |
+
GLOBAL_STATE = gr.State(AppState())
|
| 701 |
+
|
| 702 |
+
gr.Markdown(
|
| 703 |
+
"""
|
| 704 |
+
### SAM3 Video Tracking · powered by Hugging Face 🤗 Transformers
|
| 705 |
+
Segment and track objects across a video with SAM3 (Segment Anything 3). This demo runs the official implementation from the Hugging Face Transformers library for interactive, promptable video segmentation with point, box, and text prompts.
|
| 706 |
+
"""
|
| 707 |
+
)
|
| 708 |
+
|
| 709 |
+
with gr.Tabs() as main_tabs:
|
| 710 |
+
with gr.Tab("Text Prompting"):
|
| 711 |
+
with gr.Row():
|
| 712 |
+
with gr.Column():
|
| 713 |
+
gr.Markdown(
|
| 714 |
+
"""
|
| 715 |
+
**Quick start**
|
| 716 |
+
- **Load a video**: Upload your own or pick an example below.
|
| 717 |
+
- Select a frame and enter a text description to segment objects (e.g., "red car", "penguin"). The text prompt will return all the instances of the object in the frame and not specific ones (e.g. not "penguin on the left" but "penguin").
|
| 718 |
+
"""
|
| 719 |
+
)
|
| 720 |
+
with gr.Column():
|
| 721 |
+
gr.Markdown(
|
| 722 |
+
"""
|
| 723 |
+
**Working with results**
|
| 724 |
+
- **Preview**: Use the slider to navigate frames and see the current masks.
|
| 725 |
+
- **Propagate**: Click "Propagate across video" to track all defined objects through the entire video.
|
| 726 |
+
- **Export**: Render an MP4 for smooth playback using the original video FPS.
|
| 727 |
+
"""
|
| 728 |
+
)
|
| 729 |
+
|
| 730 |
+
with gr.Row():
|
| 731 |
+
with gr.Column(scale=1):
|
| 732 |
+
video_in_text = gr.Video(label="Upload video", sources=["upload", "webcam"], interactive=True)
|
| 733 |
+
load_status_text = gr.Markdown(visible=True)
|
| 734 |
+
reset_btn_text = gr.Button("Reset Session", variant="secondary")
|
| 735 |
+
with gr.Column(scale=2):
|
| 736 |
+
preview_text = gr.Image(label="Preview", interactive=True)
|
| 737 |
+
with gr.Row():
|
| 738 |
+
frame_slider_text = gr.Slider(
|
| 739 |
+
label="Frame", minimum=0, maximum=0, step=1, value=0, interactive=True
|
| 740 |
+
)
|
| 741 |
+
with gr.Column(scale=0):
|
| 742 |
+
propagate_btn_text = gr.Button("Propagate across video", variant="primary")
|
| 743 |
+
propagate_status_text = gr.Markdown(visible=True)
|
| 744 |
+
with gr.Row():
|
| 745 |
+
text_prompt_input = gr.Textbox(
|
| 746 |
+
label="Text Prompt",
|
| 747 |
+
placeholder="Enter a text description (e.g., 'person', 'red car', 'short hair')",
|
| 748 |
+
lines=2,
|
| 749 |
+
)
|
| 750 |
+
text_apply_btn = gr.Button("Apply Text Prompt", variant="primary")
|
| 751 |
+
text_status = gr.Markdown(visible=True)
|
| 752 |
+
|
| 753 |
+
with gr.Row():
|
| 754 |
+
render_btn_text = gr.Button("Render MP4 for smooth playback", variant="primary")
|
| 755 |
+
playback_video_text = gr.Video(label="Rendered Playback", interactive=False)
|
| 756 |
+
|
| 757 |
+
examples_list_text = [
|
| 758 |
+
[None, "./deers.mp4"],
|
| 759 |
+
[None, "./penguins.mp4"],
|
| 760 |
+
[None, "./foot.mp4"],
|
| 761 |
+
]
|
| 762 |
+
with gr.Row():
|
| 763 |
+
gr.Examples(
|
| 764 |
+
examples=examples_list_text,
|
| 765 |
+
inputs=[GLOBAL_STATE, video_in_text],
|
| 766 |
+
fn=_on_video_change_text,
|
| 767 |
+
outputs=[GLOBAL_STATE, frame_slider_text, preview_text, load_status_text],
|
| 768 |
+
label="Examples",
|
| 769 |
+
cache_examples=False,
|
| 770 |
+
examples_per_page=5,
|
| 771 |
+
)
|
| 772 |
+
|
| 773 |
+
with gr.Tab("Point/Box Prompting"):
|
| 774 |
+
with gr.Row():
|
| 775 |
+
with gr.Column():
|
| 776 |
+
gr.Markdown(
|
| 777 |
+
"""
|
| 778 |
+
**Quick start**
|
| 779 |
+
- **Load a video**: Upload your own or pick an example below.
|
| 780 |
+
- Select an Object ID and point label (positive/negative), then click the frame to add guidance. You can add **multiple points per object** and define **multiple objects** across frames.
|
| 781 |
+
"""
|
| 782 |
+
)
|
| 783 |
+
with gr.Column():
|
| 784 |
+
gr.Markdown(
|
| 785 |
+
"""
|
| 786 |
+
**Working with results**
|
| 787 |
+
- **Preview**: Use the slider to navigate frames and see the current masks.
|
| 788 |
+
- **Propagate**: Click "Propagate across video" to track all defined objects through the entire video.
|
| 789 |
+
- **Export**: Render an MP4 for smooth playback using the original video FPS.
|
| 790 |
+
"""
|
| 791 |
+
)
|
| 792 |
+
|
| 793 |
+
with gr.Row():
|
| 794 |
+
with gr.Column(scale=1):
|
| 795 |
+
video_in_pointbox = gr.Video(label="Upload video", sources=["upload", "webcam"], interactive=True)
|
| 796 |
+
load_status_pointbox = gr.Markdown(visible=True)
|
| 797 |
+
reset_btn_pointbox = gr.Button("Reset Session", variant="secondary")
|
| 798 |
+
with gr.Column(scale=2):
|
| 799 |
+
preview_pointbox = gr.Image(label="Preview", interactive=True)
|
| 800 |
+
with gr.Row():
|
| 801 |
+
frame_slider_pointbox = gr.Slider(
|
| 802 |
+
label="Frame", minimum=0, maximum=0, step=1, value=0, interactive=True
|
| 803 |
+
)
|
| 804 |
+
with gr.Column(scale=0):
|
| 805 |
+
propagate_btn_pointbox = gr.Button("Propagate across video", variant="primary")
|
| 806 |
+
propagate_status_pointbox = gr.Markdown(visible=True)
|
| 807 |
+
|
| 808 |
+
with gr.Row():
|
| 809 |
+
obj_id_inp = gr.Number(value=1, precision=0, label="Object ID", scale=0)
|
| 810 |
+
label_radio = gr.Radio(choices=["positive", "negative"], value="positive", label="Point label")
|
| 811 |
+
clear_old_chk = gr.Checkbox(value=False, label="Clear old inputs for this object")
|
| 812 |
+
prompt_type = gr.Radio(choices=["Points", "Boxes"], value="Points", label="Prompt type")
|
| 813 |
+
|
| 814 |
+
with gr.Row():
|
| 815 |
+
render_btn_pointbox = gr.Button("Render MP4 for smooth playback", variant="primary")
|
| 816 |
+
playback_video_pointbox = gr.Video(label="Rendered Playback", interactive=False)
|
| 817 |
+
|
| 818 |
+
examples_list_pointbox = [
|
| 819 |
+
[None, "./deers.mp4"],
|
| 820 |
+
[None, "./penguins.mp4"],
|
| 821 |
+
[None, "./foot.mp4"],
|
| 822 |
+
]
|
| 823 |
+
with gr.Row():
|
| 824 |
+
gr.Examples(
|
| 825 |
+
examples=examples_list_pointbox,
|
| 826 |
+
inputs=[GLOBAL_STATE, video_in_pointbox],
|
| 827 |
+
fn=_on_video_change_pointbox,
|
| 828 |
+
outputs=[GLOBAL_STATE, frame_slider_pointbox, preview_pointbox, load_status_pointbox],
|
| 829 |
+
label="Examples",
|
| 830 |
+
cache_examples=False,
|
| 831 |
+
examples_per_page=5,
|
| 832 |
+
)
|
| 833 |
+
|
| 834 |
+
video_in_pointbox.change(
|
| 835 |
+
_on_video_change_pointbox,
|
| 836 |
+
inputs=[GLOBAL_STATE, video_in_pointbox],
|
| 837 |
+
outputs=[GLOBAL_STATE, frame_slider_pointbox, preview_pointbox, load_status_pointbox],
|
| 838 |
+
show_progress=True,
|
| 839 |
+
)
|
| 840 |
+
|
| 841 |
+
def _sync_frame_idx_pointbox(state_in: AppState, idx: int):
|
| 842 |
+
if state_in is not None:
|
| 843 |
+
state_in.current_frame_idx = int(idx)
|
| 844 |
+
return update_frame_display(state_in, int(idx))
|
| 845 |
+
|
| 846 |
+
frame_slider_pointbox.change(
|
| 847 |
+
_sync_frame_idx_pointbox,
|
| 848 |
+
inputs=[GLOBAL_STATE, frame_slider_pointbox],
|
| 849 |
+
outputs=preview_pointbox,
|
| 850 |
+
)
|
| 851 |
+
|
| 852 |
+
video_in_text.change(
|
| 853 |
+
_on_video_change_text,
|
| 854 |
+
inputs=[GLOBAL_STATE, video_in_text],
|
| 855 |
+
outputs=[GLOBAL_STATE, frame_slider_text, preview_text, load_status_text],
|
| 856 |
+
show_progress=True,
|
| 857 |
+
)
|
| 858 |
+
|
| 859 |
+
def _sync_frame_idx_text(state_in: AppState, idx: int):
|
| 860 |
+
if state_in is not None:
|
| 861 |
+
state_in.current_frame_idx = int(idx)
|
| 862 |
+
return update_frame_display(state_in, int(idx))
|
| 863 |
+
|
| 864 |
+
frame_slider_text.change(
|
| 865 |
+
_sync_frame_idx_text,
|
| 866 |
+
inputs=[GLOBAL_STATE, frame_slider_text],
|
| 867 |
+
outputs=preview_text,
|
| 868 |
+
)
|
| 869 |
+
|
| 870 |
+
def _sync_obj_id(s: AppState, oid):
|
| 871 |
+
if s is not None and oid is not None:
|
| 872 |
+
s.current_obj_id = int(oid)
|
| 873 |
+
return gr.update()
|
| 874 |
+
|
| 875 |
+
obj_id_inp.change(_sync_obj_id, inputs=[GLOBAL_STATE, obj_id_inp], outputs=[])
|
| 876 |
+
|
| 877 |
+
def _sync_label(s: AppState, lab: str):
|
| 878 |
+
if s is not None and lab is not None:
|
| 879 |
+
s.current_label = str(lab)
|
| 880 |
+
return gr.update()
|
| 881 |
+
|
| 882 |
+
label_radio.change(_sync_label, inputs=[GLOBAL_STATE, label_radio], outputs=[])
|
| 883 |
+
|
| 884 |
+
def _sync_prompt_type(s: AppState, val: str):
|
| 885 |
+
if s is not None and val is not None:
|
| 886 |
+
s.current_prompt_type = str(val)
|
| 887 |
+
s.pending_box_start = None
|
| 888 |
+
is_points = str(val).lower() == "points"
|
| 889 |
+
updates = [
|
| 890 |
+
gr.update(visible=is_points),
|
| 891 |
+
gr.update(interactive=is_points) if is_points else gr.update(value=True, interactive=False),
|
| 892 |
+
]
|
| 893 |
+
return updates
|
| 894 |
+
|
| 895 |
+
prompt_type.change(
|
| 896 |
+
_sync_prompt_type,
|
| 897 |
+
inputs=[GLOBAL_STATE, prompt_type],
|
| 898 |
+
outputs=[label_radio, clear_old_chk],
|
| 899 |
+
)
|
| 900 |
+
|
| 901 |
+
preview_pointbox.select(
|
| 902 |
+
on_image_click,
|
| 903 |
+
[preview_pointbox, GLOBAL_STATE, frame_slider_pointbox, obj_id_inp, label_radio, clear_old_chk],
|
| 904 |
+
preview_pointbox,
|
| 905 |
+
)
|
| 906 |
+
|
| 907 |
+
def _on_text_apply(state: AppState, frame_idx: int, text: str):
|
| 908 |
+
img, status = on_text_prompt(state, frame_idx, text)
|
| 909 |
+
return img, status
|
| 910 |
+
|
| 911 |
+
text_apply_btn.click(
|
| 912 |
+
_on_text_apply,
|
| 913 |
+
inputs=[GLOBAL_STATE, frame_slider_text, text_prompt_input],
|
| 914 |
+
outputs=[preview_text, text_status],
|
| 915 |
+
)
|
| 916 |
+
|
| 917 |
+
def _render_video(s: AppState):
|
| 918 |
+
if s is None or s.num_frames == 0:
|
| 919 |
+
raise gr.Error("Load a video first.")
|
| 920 |
+
fps = s.video_fps if s.video_fps and s.video_fps > 0 else 12
|
| 921 |
+
frames_np = []
|
| 922 |
+
first = compose_frame(s, 0)
|
| 923 |
+
h, w = first.size[1], first.size[0]
|
| 924 |
+
for idx in range(s.num_frames):
|
| 925 |
+
img = s.composited_frames.get(idx)
|
| 926 |
+
if img is None:
|
| 927 |
+
img = compose_frame(s, idx)
|
| 928 |
+
frames_np.append(np.array(img)[:, :, ::-1])
|
| 929 |
+
if (idx + 1) % 60 == 0:
|
| 930 |
+
gc.collect()
|
| 931 |
+
out_path = "/tmp/sam3_playback.mp4"
|
| 932 |
+
try:
|
| 933 |
+
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
| 934 |
+
writer = cv2.VideoWriter(out_path, fourcc, fps, (w, h))
|
| 935 |
+
for fr_bgr in frames_np:
|
| 936 |
+
writer.write(fr_bgr)
|
| 937 |
+
writer.release()
|
| 938 |
+
return out_path
|
| 939 |
+
except Exception as e:
|
| 940 |
+
print(f"Failed to render video with cv2: {e}")
|
| 941 |
+
raise gr.Error(f"Failed to render video: {e}")
|
| 942 |
+
|
| 943 |
+
render_btn_pointbox.click(_render_video, inputs=[GLOBAL_STATE], outputs=[playback_video_pointbox])
|
| 944 |
+
render_btn_text.click(_render_video, inputs=[GLOBAL_STATE], outputs=[playback_video_text])
|
| 945 |
+
|
| 946 |
+
propagate_btn_pointbox.click(
|
| 947 |
+
propagate_masks,
|
| 948 |
+
inputs=[GLOBAL_STATE],
|
| 949 |
+
outputs=[GLOBAL_STATE, propagate_status_pointbox, frame_slider_pointbox],
|
| 950 |
+
)
|
| 951 |
+
|
| 952 |
+
propagate_btn_text.click(
|
| 953 |
+
propagate_masks,
|
| 954 |
+
inputs=[GLOBAL_STATE],
|
| 955 |
+
outputs=[GLOBAL_STATE, propagate_status_text, frame_slider_text],
|
| 956 |
+
)
|
| 957 |
+
|
| 958 |
+
reset_btn_pointbox.click(
|
| 959 |
+
reset_session,
|
| 960 |
+
inputs=GLOBAL_STATE,
|
| 961 |
+
outputs=[GLOBAL_STATE, preview_pointbox, frame_slider_pointbox, frame_slider_pointbox, load_status_pointbox],
|
| 962 |
+
)
|
| 963 |
+
|
| 964 |
+
reset_btn_text.click(
|
| 965 |
+
reset_session,
|
| 966 |
+
inputs=GLOBAL_STATE,
|
| 967 |
+
outputs=[GLOBAL_STATE, preview_text, frame_slider_text, frame_slider_text, load_status_text],
|
| 968 |
+
)
|
| 969 |
+
|
| 970 |
+
|
| 971 |
+
demo.queue(api_open=False).launch()
|
deers.mp4
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1e60c4974bbfff98d16e8f264a54d9f84084c5591fdb8455d64449561eb74714
|
| 3 |
+
size 3401495
|
foot.mp4
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0e7f86a74b9fa12322024ce4e60c27a2c86acf65abfa32b0a3e3dc44163de96b
|
| 3 |
+
size 2359941
|
penguins.mp4
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7a7776418857bd05405fa055cce364f122eafd418be489e88ff7955b4dfd427a
|
| 3 |
+
size 4573098
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
torch
|
| 3 |
+
torchvision
|
| 4 |
+
pillow
|
| 5 |
+
opencv-python
|
| 6 |
+
imageio[pyav]
|
| 7 |
+
accelerate
|
| 8 |
+
|
transformers-5.0.0.dev0-py3-none-any.whl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f3fe5e3a03f2b46d47b6d99c458175fc8caea263fc89be1ae4f5cd5500375ecc
|
| 3 |
+
size 11545319
|