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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import colorsys
|
| 2 |
+
import gc
|
| 3 |
+
from typing import Optional
|
| 4 |
+
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import numpy as np
|
| 7 |
+
import spaces
|
| 8 |
+
import torch
|
| 9 |
+
from PIL import Image, ImageDraw
|
| 10 |
+
|
| 11 |
+
from transformers import Sam2VideoModel, Sam2VideoProcessor
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def pastel_color_for_object(obj_id: int) -> tuple[int, int, int]:
|
| 15 |
+
golden_ratio_conjugate = 0.61803398875
|
| 16 |
+
hue = (obj_id * golden_ratio_conjugate) % 1.0
|
| 17 |
+
saturation = 0.45
|
| 18 |
+
value = 1.0
|
| 19 |
+
r_f, g_f, b_f = colorsys.hsv_to_rgb(hue, saturation, value)
|
| 20 |
+
return int(r_f * 255), int(g_f * 255), int(b_f * 255)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def try_load_video_frames(video_path_or_url: str) -> tuple[list[Image.Image], dict]:
|
| 24 |
+
try:
|
| 25 |
+
from transformers.video_utils import load_video # type: ignore
|
| 26 |
+
|
| 27 |
+
frames, info = load_video(video_path_or_url)
|
| 28 |
+
pil_frames = []
|
| 29 |
+
for fr in frames:
|
| 30 |
+
if isinstance(fr, Image.Image):
|
| 31 |
+
pil_frames.append(fr.convert("RGB"))
|
| 32 |
+
else:
|
| 33 |
+
pil_frames.append(Image.fromarray(fr).convert("RGB"))
|
| 34 |
+
info = info if info is not None else {}
|
| 35 |
+
if "fps" not in info or not info.get("fps"):
|
| 36 |
+
try:
|
| 37 |
+
import cv2 # type: ignore
|
| 38 |
+
|
| 39 |
+
cap = cv2.VideoCapture(video_path_or_url)
|
| 40 |
+
fps_val = cap.get(cv2.CAP_PROP_FPS)
|
| 41 |
+
cap.release()
|
| 42 |
+
if fps_val and fps_val > 0:
|
| 43 |
+
info["fps"] = float(fps_val)
|
| 44 |
+
except Exception:
|
| 45 |
+
pass
|
| 46 |
+
return pil_frames, info
|
| 47 |
+
except Exception:
|
| 48 |
+
try:
|
| 49 |
+
import cv2 # type: ignore
|
| 50 |
+
|
| 51 |
+
cap = cv2.VideoCapture(video_path_or_url)
|
| 52 |
+
frames = []
|
| 53 |
+
while cap.isOpened():
|
| 54 |
+
ret, frame = cap.read()
|
| 55 |
+
if not ret:
|
| 56 |
+
break
|
| 57 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 58 |
+
frames.append(Image.fromarray(frame_rgb))
|
| 59 |
+
fps_val = cap.get(cv2.CAP_PROP_FPS)
|
| 60 |
+
cap.release()
|
| 61 |
+
info = {
|
| 62 |
+
"num_frames": len(frames),
|
| 63 |
+
"fps": float(fps_val) if fps_val and fps_val > 0 else None,
|
| 64 |
+
}
|
| 65 |
+
return frames, info
|
| 66 |
+
except Exception as e:
|
| 67 |
+
raise RuntimeError(f"Failed to load video: {e}")
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def overlay_masks_on_frame(
|
| 71 |
+
frame: Image.Image,
|
| 72 |
+
masks_per_object: dict[int, np.ndarray],
|
| 73 |
+
color_by_obj: dict[int, tuple[int, int, int]],
|
| 74 |
+
alpha: float = 0.65,
|
| 75 |
+
) -> Image.Image:
|
| 76 |
+
base = np.array(frame).astype(np.float32) / 255.0
|
| 77 |
+
overlay = base.copy()
|
| 78 |
+
for obj_id, mask in masks_per_object.items():
|
| 79 |
+
if mask is None:
|
| 80 |
+
continue
|
| 81 |
+
if mask.dtype != np.float32:
|
| 82 |
+
mask = mask.astype(np.float32)
|
| 83 |
+
if mask.ndim == 3:
|
| 84 |
+
mask = mask.squeeze()
|
| 85 |
+
mask = np.clip(mask, 0.0, 1.0)
|
| 86 |
+
color = np.array(color_by_obj.get(obj_id, (255, 0, 0)), dtype=np.float32) / 255.0
|
| 87 |
+
m = mask[..., None]
|
| 88 |
+
overlay = (1.0 - alpha * m) * overlay + (alpha * m) * color
|
| 89 |
+
out = np.clip(overlay * 255.0, 0, 255).astype(np.uint8)
|
| 90 |
+
return Image.fromarray(out)
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def get_device_and_dtype() -> tuple[str, torch.dtype]:
|
| 94 |
+
# Force CPU-only on Spaces with zero GPU
|
| 95 |
+
return "cpu", torch.float32
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
class AppState:
|
| 99 |
+
def __init__(self):
|
| 100 |
+
self.reset()
|
| 101 |
+
|
| 102 |
+
def reset(self):
|
| 103 |
+
self.video_frames: list[Image.Image] = []
|
| 104 |
+
self.inference_session = None
|
| 105 |
+
self.model: Optional[Sam2VideoModel] = None
|
| 106 |
+
self.processor: Optional[Sam2VideoProcessor] = None
|
| 107 |
+
self.device: str = "cpu"
|
| 108 |
+
self.dtype: torch.dtype = torch.float32
|
| 109 |
+
self.video_fps: float | None = None
|
| 110 |
+
self.masks_by_frame: dict[int, dict[int, np.ndarray]] = {}
|
| 111 |
+
self.color_by_obj: dict[int, tuple[int, int, int]] = {}
|
| 112 |
+
self.clicks_by_frame_obj: dict[int, dict[int, list[tuple[int, int, int]]]] = {}
|
| 113 |
+
self.boxes_by_frame_obj: dict[int, dict[int, list[tuple[int, int, int, int]]]] = {}
|
| 114 |
+
self.composited_frames: dict[int, Image.Image] = {}
|
| 115 |
+
self.current_frame_idx: int = 0
|
| 116 |
+
self.current_obj_id: int = 1
|
| 117 |
+
self.current_label: str = "positive"
|
| 118 |
+
self.current_clear_old: bool = True
|
| 119 |
+
self.current_prompt_type: str = "Points"
|
| 120 |
+
self.pending_box_start: tuple[int, int] | None = None
|
| 121 |
+
self.pending_box_start_frame_idx: int | None = None
|
| 122 |
+
self.pending_box_start_obj_id: int | None = None
|
| 123 |
+
self.is_switching_model: bool = False
|
| 124 |
+
self.model_repo_key: str = "tiny"
|
| 125 |
+
self.model_repo_id: str | None = None
|
| 126 |
+
self.session_repo_id: str | None = None
|
| 127 |
+
|
| 128 |
+
@property
|
| 129 |
+
def num_frames(self) -> int:
|
| 130 |
+
return len(self.video_frames)
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
GLOBAL_STATE = AppState()
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
def _model_repo_from_key(key: str) -> str:
|
| 137 |
+
mapping = {
|
| 138 |
+
"tiny": "yonigozlan/sam2.1_hiera_tiny_hf",
|
| 139 |
+
"small": "yonigozlan/sam2.1_hiera_small_hf",
|
| 140 |
+
"base_plus": "yonigozlan/sam2.1_hiera_base_plus_hf",
|
| 141 |
+
"large": "yonigozlan/sam2.1_hiera_large_hf",
|
| 142 |
+
}
|
| 143 |
+
return mapping.get(key, mapping["base_plus"])
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
@spaces.GPU()
|
| 147 |
+
def load_model_if_needed() -> tuple[Sam2VideoModel, Sam2VideoProcessor, str, torch.dtype]:
|
| 148 |
+
desired_repo = _model_repo_from_key(GLOBAL_STATE.model_repo_key)
|
| 149 |
+
if GLOBAL_STATE.model is not None and GLOBAL_STATE.processor is not None:
|
| 150 |
+
if GLOBAL_STATE.model_repo_id == desired_repo:
|
| 151 |
+
return GLOBAL_STATE.model, GLOBAL_STATE.processor, GLOBAL_STATE.device, GLOBAL_STATE.dtype
|
| 152 |
+
try:
|
| 153 |
+
del GLOBAL_STATE.model
|
| 154 |
+
except Exception:
|
| 155 |
+
pass
|
| 156 |
+
try:
|
| 157 |
+
del GLOBAL_STATE.processor
|
| 158 |
+
except Exception:
|
| 159 |
+
pass
|
| 160 |
+
GLOBAL_STATE.model = None
|
| 161 |
+
GLOBAL_STATE.processor = None
|
| 162 |
+
|
| 163 |
+
device, dtype = get_device_and_dtype()
|
| 164 |
+
model = Sam2VideoModel.from_pretrained(desired_repo, torch_dtype=dtype)
|
| 165 |
+
processor = Sam2VideoProcessor.from_pretrained(desired_repo)
|
| 166 |
+
model.to(device)
|
| 167 |
+
GLOBAL_STATE.model = model
|
| 168 |
+
GLOBAL_STATE.processor = processor
|
| 169 |
+
GLOBAL_STATE.device = device
|
| 170 |
+
GLOBAL_STATE.dtype = dtype
|
| 171 |
+
GLOBAL_STATE.model_repo_id = desired_repo
|
| 172 |
+
return model, processor, device, dtype
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
def ensure_session_for_current_model() -> None:
|
| 176 |
+
model, processor, device, dtype = load_model_if_needed()
|
| 177 |
+
desired_repo = _model_repo_from_key(GLOBAL_STATE.model_repo_key)
|
| 178 |
+
if GLOBAL_STATE.inference_session is None or GLOBAL_STATE.session_repo_id != desired_repo:
|
| 179 |
+
if GLOBAL_STATE.video_frames:
|
| 180 |
+
GLOBAL_STATE.masks_by_frame.clear()
|
| 181 |
+
GLOBAL_STATE.clicks_by_frame_obj.clear()
|
| 182 |
+
GLOBAL_STATE.boxes_by_frame_obj.clear()
|
| 183 |
+
GLOBAL_STATE.composited_frames.clear()
|
| 184 |
+
try:
|
| 185 |
+
if GLOBAL_STATE.inference_session is not None:
|
| 186 |
+
GLOBAL_STATE.inference_session.reset_inference_session()
|
| 187 |
+
except Exception:
|
| 188 |
+
pass
|
| 189 |
+
GLOBAL_STATE.inference_session = None
|
| 190 |
+
gc.collect()
|
| 191 |
+
GLOBAL_STATE.inference_session = processor.init_video_session(
|
| 192 |
+
video=GLOBAL_STATE.video_frames,
|
| 193 |
+
inference_device=device,
|
| 194 |
+
video_storage_device="cpu",
|
| 195 |
+
)
|
| 196 |
+
GLOBAL_STATE.session_repo_id = desired_repo
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
def init_video_session(video: str | dict):
|
| 200 |
+
GLOBAL_STATE.video_frames = []
|
| 201 |
+
GLOBAL_STATE.inference_session = None
|
| 202 |
+
GLOBAL_STATE.masks_by_frame = {}
|
| 203 |
+
GLOBAL_STATE.color_by_obj = {}
|
| 204 |
+
|
| 205 |
+
load_model_if_needed()
|
| 206 |
+
|
| 207 |
+
video_path: Optional[str] = None
|
| 208 |
+
if isinstance(video, dict):
|
| 209 |
+
video_path = video.get("name") or video.get("path") or video.get("data")
|
| 210 |
+
elif isinstance(video, str):
|
| 211 |
+
video_path = video
|
| 212 |
+
else:
|
| 213 |
+
video_path = None
|
| 214 |
+
if not video_path:
|
| 215 |
+
raise gr.Error("Invalid video input.")
|
| 216 |
+
|
| 217 |
+
frames, info = try_load_video_frames(video_path)
|
| 218 |
+
if len(frames) == 0:
|
| 219 |
+
raise gr.Error("No frames could be loaded from the video.")
|
| 220 |
+
|
| 221 |
+
GLOBAL_STATE.video_frames = frames
|
| 222 |
+
GLOBAL_STATE.video_fps = None
|
| 223 |
+
if isinstance(info, dict) and info.get("fps"):
|
| 224 |
+
try:
|
| 225 |
+
GLOBAL_STATE.video_fps = float(info["fps"]) or None
|
| 226 |
+
except Exception:
|
| 227 |
+
GLOBAL_STATE.video_fps = None
|
| 228 |
+
|
| 229 |
+
processor = GLOBAL_STATE.processor
|
| 230 |
+
device = GLOBAL_STATE.device
|
| 231 |
+
inference_session = processor.init_video_session(
|
| 232 |
+
video=frames,
|
| 233 |
+
inference_device=device,
|
| 234 |
+
video_storage_device="cpu",
|
| 235 |
+
)
|
| 236 |
+
GLOBAL_STATE.inference_session = inference_session
|
| 237 |
+
|
| 238 |
+
first_frame = frames[0]
|
| 239 |
+
max_idx = len(frames) - 1
|
| 240 |
+
status = f"Loaded {len(frames)} frames @ {GLOBAL_STATE.video_fps or 'unknown'} fps. Device: {device}, dtype: {GLOBAL_STATE.dtype}"
|
| 241 |
+
return GLOBAL_STATE, 0, max_idx, first_frame, status
|
| 242 |
+
|
| 243 |
+
|
| 244 |
+
def compose_frame(state: AppState, frame_idx: int) -> Image.Image:
|
| 245 |
+
if state is None or state.video_frames is None or len(state.video_frames) == 0:
|
| 246 |
+
return None
|
| 247 |
+
frame_idx = int(np.clip(frame_idx, 0, len(state.video_frames) - 1))
|
| 248 |
+
frame = state.video_frames[frame_idx]
|
| 249 |
+
masks = state.masks_by_frame.get(frame_idx, {})
|
| 250 |
+
out_img = frame
|
| 251 |
+
if len(masks) != 0:
|
| 252 |
+
out_img = overlay_masks_on_frame(out_img, masks, state.color_by_obj, alpha=0.65)
|
| 253 |
+
|
| 254 |
+
clicks_map = state.clicks_by_frame_obj.get(frame_idx)
|
| 255 |
+
if clicks_map:
|
| 256 |
+
draw = ImageDraw.Draw(out_img)
|
| 257 |
+
cross_half = 6
|
| 258 |
+
for obj_id, pts in clicks_map.items():
|
| 259 |
+
for x, y, lbl in pts:
|
| 260 |
+
color = (0, 255, 0) if int(lbl) == 1 else (255, 0, 0)
|
| 261 |
+
draw.line([(x - cross_half, y), (x + cross_half, y)], fill=color, width=2)
|
| 262 |
+
draw.line([(x, y - cross_half), (x, y + cross_half)], fill=color, width=2)
|
| 263 |
+
|
| 264 |
+
box_map = state.boxes_by_frame_obj.get(frame_idx)
|
| 265 |
+
if box_map:
|
| 266 |
+
draw = ImageDraw.Draw(out_img)
|
| 267 |
+
for obj_id, boxes in box_map.items():
|
| 268 |
+
color = state.color_by_obj.get(obj_id, (255, 255, 255))
|
| 269 |
+
for x1, y1, x2, y2 in boxes:
|
| 270 |
+
draw.rectangle([(x1, y1), (x2, y2)], outline=color, width=2)
|
| 271 |
+
|
| 272 |
+
if (
|
| 273 |
+
state.pending_box_start is not None
|
| 274 |
+
and state.pending_box_start_frame_idx == frame_idx
|
| 275 |
+
and state.pending_box_start_obj_id is not None
|
| 276 |
+
):
|
| 277 |
+
draw = ImageDraw.Draw(out_img)
|
| 278 |
+
x, y = state.pending_box_start
|
| 279 |
+
cross_half = 6
|
| 280 |
+
color = state.color_by_obj.get(state.pending_box_start_obj_id, (255, 255, 255))
|
| 281 |
+
draw.line([(x - cross_half, y), (x + cross_half, y)], fill=color, width=2)
|
| 282 |
+
draw.line([(x, y - cross_half), (x, y + cross_half)], fill=color, width=2)
|
| 283 |
+
|
| 284 |
+
state.composited_frames[frame_idx] = out_img
|
| 285 |
+
return out_img
|
| 286 |
+
|
| 287 |
+
|
| 288 |
+
def update_frame_display(state: AppState, frame_idx: int) -> Image.Image:
|
| 289 |
+
if state is None or state.video_frames is None or len(state.video_frames) == 0:
|
| 290 |
+
return None
|
| 291 |
+
frame_idx = int(np.clip(frame_idx, 0, len(state.video_frames) - 1))
|
| 292 |
+
cached = state.composited_frames.get(frame_idx)
|
| 293 |
+
if cached is not None:
|
| 294 |
+
return cached
|
| 295 |
+
return compose_frame(state, frame_idx)
|
| 296 |
+
|
| 297 |
+
|
| 298 |
+
def _ensure_color_for_obj(obj_id: int):
|
| 299 |
+
if obj_id not in GLOBAL_STATE.color_by_obj:
|
| 300 |
+
GLOBAL_STATE.color_by_obj[obj_id] = pastel_color_for_object(obj_id)
|
| 301 |
+
|
| 302 |
+
|
| 303 |
+
@spaces.GPU()
|
| 304 |
+
def on_image_click(
|
| 305 |
+
img: Image.Image | np.ndarray,
|
| 306 |
+
state: AppState,
|
| 307 |
+
frame_idx: int,
|
| 308 |
+
obj_id: int,
|
| 309 |
+
label: str,
|
| 310 |
+
clear_old: bool,
|
| 311 |
+
evt: gr.SelectData,
|
| 312 |
+
):
|
| 313 |
+
if state is None or state.inference_session is None:
|
| 314 |
+
return img
|
| 315 |
+
if state.is_switching_model:
|
| 316 |
+
return update_frame_display(state, int(frame_idx))
|
| 317 |
+
|
| 318 |
+
x = y = None
|
| 319 |
+
if evt is not None:
|
| 320 |
+
try:
|
| 321 |
+
if hasattr(evt, "index") and isinstance(evt.index, (list, tuple)) and len(evt.index) == 2:
|
| 322 |
+
x, y = int(evt.index[0]), int(evt.index[1])
|
| 323 |
+
elif hasattr(evt, "value") and isinstance(evt.value, dict) and "x" in evt.value and "y" in evt.value:
|
| 324 |
+
x, y = int(evt.value["x"]), int(evt.value["y"])
|
| 325 |
+
except Exception:
|
| 326 |
+
x = y = None
|
| 327 |
+
if x is None or y is None:
|
| 328 |
+
return update_frame_display(state, int(frame_idx))
|
| 329 |
+
|
| 330 |
+
_ensure_color_for_obj(int(obj_id))
|
| 331 |
+
processor = GLOBAL_STATE.processor
|
| 332 |
+
model = GLOBAL_STATE.model
|
| 333 |
+
inference_session = GLOBAL_STATE.inference_session
|
| 334 |
+
|
| 335 |
+
if state.current_prompt_type == "Boxes":
|
| 336 |
+
if state.pending_box_start is None:
|
| 337 |
+
if bool(clear_old):
|
| 338 |
+
frame_clicks = state.clicks_by_frame_obj.setdefault(int(frame_idx), {})
|
| 339 |
+
frame_clicks[int(obj_id)] = []
|
| 340 |
+
state.composited_frames.pop(int(frame_idx), None)
|
| 341 |
+
state.pending_box_start = (int(x), int(y))
|
| 342 |
+
state.pending_box_start_frame_idx = int(frame_idx)
|
| 343 |
+
state.pending_box_start_obj_id = int(obj_id)
|
| 344 |
+
state.composited_frames.pop(int(frame_idx), None)
|
| 345 |
+
return update_frame_display(state, int(frame_idx))
|
| 346 |
+
else:
|
| 347 |
+
x1, y1 = state.pending_box_start
|
| 348 |
+
x2, y2 = int(x), int(y)
|
| 349 |
+
state.pending_box_start = None
|
| 350 |
+
state.pending_box_start_frame_idx = None
|
| 351 |
+
state.pending_box_start_obj_id = None
|
| 352 |
+
state.composited_frames.pop(int(frame_idx), None)
|
| 353 |
+
x_min, y_min = min(x1, x2), min(y1, y2)
|
| 354 |
+
x_max, y_max = max(x1, x2), max(y1, y2)
|
| 355 |
+
|
| 356 |
+
processor.add_inputs_to_inference_session(
|
| 357 |
+
inference_session=inference_session,
|
| 358 |
+
frame_idx=int(frame_idx),
|
| 359 |
+
obj_ids=int(obj_id),
|
| 360 |
+
input_boxes=[[[x_min, y_min, x_max, y_max]]],
|
| 361 |
+
clear_old_inputs=bool(clear_old),
|
| 362 |
+
)
|
| 363 |
+
|
| 364 |
+
frame_boxes = state.boxes_by_frame_obj.setdefault(int(frame_idx), {})
|
| 365 |
+
obj_boxes = frame_boxes.setdefault(int(obj_id), [])
|
| 366 |
+
if bool(clear_old):
|
| 367 |
+
obj_boxes.clear()
|
| 368 |
+
obj_boxes.append((x_min, y_min, x_max, y_max))
|
| 369 |
+
state.composited_frames.pop(int(frame_idx), None)
|
| 370 |
+
else:
|
| 371 |
+
label_int = 1 if str(label).lower().startswith("pos") else 0
|
| 372 |
+
if bool(clear_old):
|
| 373 |
+
frame_boxes = state.boxes_by_frame_obj.setdefault(int(frame_idx), {})
|
| 374 |
+
frame_boxes[int(obj_id)] = []
|
| 375 |
+
state.composited_frames.pop(int(frame_idx), None)
|
| 376 |
+
processor.add_inputs_to_inference_session(
|
| 377 |
+
inference_session=inference_session,
|
| 378 |
+
frame_idx=int(frame_idx),
|
| 379 |
+
obj_ids=int(obj_id),
|
| 380 |
+
input_points=[[[[int(x), int(y)]]]],
|
| 381 |
+
input_labels=[[[int(label_int)]]],
|
| 382 |
+
clear_old_inputs=bool(clear_old),
|
| 383 |
+
)
|
| 384 |
+
frame_clicks = state.clicks_by_frame_obj.setdefault(int(frame_idx), {})
|
| 385 |
+
obj_clicks = frame_clicks.setdefault(int(obj_id), [])
|
| 386 |
+
if bool(clear_old):
|
| 387 |
+
obj_clicks.clear()
|
| 388 |
+
obj_clicks.append((int(x), int(y), int(label_int)))
|
| 389 |
+
state.composited_frames.pop(int(frame_idx), None)
|
| 390 |
+
|
| 391 |
+
with torch.inference_mode():
|
| 392 |
+
outputs = model(inference_session=inference_session, frame_idx=int(frame_idx))
|
| 393 |
+
|
| 394 |
+
H = inference_session.video_height
|
| 395 |
+
W = inference_session.video_width
|
| 396 |
+
pred_masks = outputs.pred_masks.detach().cpu()
|
| 397 |
+
video_res_masks = processor.post_process_masks([pred_masks], original_sizes=[[H, W]])[0]
|
| 398 |
+
masks_for_frame: dict[int, np.ndarray] = {}
|
| 399 |
+
obj_ids_order = list(inference_session.obj_ids)
|
| 400 |
+
for i, oid in enumerate(obj_ids_order):
|
| 401 |
+
mask_i = video_res_masks[i]
|
| 402 |
+
mask_2d = mask_i.cpu().numpy().squeeze()
|
| 403 |
+
masks_for_frame[int(oid)] = mask_2d
|
| 404 |
+
GLOBAL_STATE.masks_by_frame[int(frame_idx)] = masks_for_frame
|
| 405 |
+
GLOBAL_STATE.composited_frames.pop(int(frame_idx), None)
|
| 406 |
+
return update_frame_display(GLOBAL_STATE, int(frame_idx))
|
| 407 |
+
|
| 408 |
+
|
| 409 |
+
@spaces.GPU()
|
| 410 |
+
def propagate_masks(state: AppState, progress=gr.Progress()):
|
| 411 |
+
if state is None or state.inference_session is None:
|
| 412 |
+
yield "Load a video first."
|
| 413 |
+
return
|
| 414 |
+
processor = GLOBAL_STATE.processor
|
| 415 |
+
model = GLOBAL_STATE.model
|
| 416 |
+
inference_session = GLOBAL_STATE.inference_session
|
| 417 |
+
total = max(1, GLOBAL_STATE.num_frames)
|
| 418 |
+
processed = 0
|
| 419 |
+
yield f"Propagating masks: {processed}/{total}"
|
| 420 |
+
with torch.inference_mode():
|
| 421 |
+
for sam2_video_output in model.propagate_in_video_iterator(inference_session):
|
| 422 |
+
H = inference_session.video_height
|
| 423 |
+
W = inference_session.video_width
|
| 424 |
+
pred_masks = sam2_video_output.pred_masks.detach().cpu()
|
| 425 |
+
video_res_masks = processor.post_process_masks([pred_masks], original_sizes=[[H, W]])[0]
|
| 426 |
+
frame_idx = int(sam2_video_output.frame_idx)
|
| 427 |
+
masks_for_frame: dict[int, np.ndarray] = {}
|
| 428 |
+
obj_ids_order = list(inference_session.obj_ids)
|
| 429 |
+
for i, oid in enumerate(obj_ids_order):
|
| 430 |
+
mask_2d = video_res_masks[i].cpu().numpy().squeeze()
|
| 431 |
+
masks_for_frame[int(oid)] = mask_2d
|
| 432 |
+
GLOBAL_STATE.masks_by_frame[frame_idx] = masks_for_frame
|
| 433 |
+
GLOBAL_STATE.composited_frames.pop(frame_idx, None)
|
| 434 |
+
processed += 1
|
| 435 |
+
progress((processed, total), f"Propagating masks: {processed}/{total}")
|
| 436 |
+
yield f"Propagating masks: {processed}/{total}"
|
| 437 |
+
yield f"Propagated masks across {processed} frames for {len(inference_session.obj_ids)} objects."
|
| 438 |
+
|
| 439 |
+
|
| 440 |
+
def reset_session():
|
| 441 |
+
if not GLOBAL_STATE.video_frames:
|
| 442 |
+
return GLOBAL_STATE, None, 0, 0, "Session reset. Load a new video."
|
| 443 |
+
GLOBAL_STATE.masks_by_frame.clear()
|
| 444 |
+
GLOBAL_STATE.clicks_by_frame_obj.clear()
|
| 445 |
+
GLOBAL_STATE.boxes_by_frame_obj.clear()
|
| 446 |
+
GLOBAL_STATE.composited_frames.clear()
|
| 447 |
+
GLOBAL_STATE.pending_box_start = None
|
| 448 |
+
GLOBAL_STATE.pending_box_start_frame_idx = None
|
| 449 |
+
GLOBAL_STATE.pending_box_start_obj_id = None
|
| 450 |
+
try:
|
| 451 |
+
if GLOBAL_STATE.inference_session is not None:
|
| 452 |
+
GLOBAL_STATE.inference_session.reset_inference_session()
|
| 453 |
+
except Exception:
|
| 454 |
+
pass
|
| 455 |
+
GLOBAL_STATE.inference_session = None
|
| 456 |
+
gc.collect()
|
| 457 |
+
ensure_session_for_current_model()
|
| 458 |
+
current_idx = int(getattr(GLOBAL_STATE, "current_frame_idx", 0))
|
| 459 |
+
current_idx = max(0, min(current_idx, GLOBAL_STATE.num_frames - 1))
|
| 460 |
+
preview_img = update_frame_display(GLOBAL_STATE, current_idx)
|
| 461 |
+
slider_minmax = gr.update(minimum=0, maximum=max(GLOBAL_STATE.num_frames - 1, 0), interactive=True)
|
| 462 |
+
slider_value = gr.update(value=current_idx)
|
| 463 |
+
status = "Session reset. Prompts cleared; video preserved."
|
| 464 |
+
return GLOBAL_STATE, preview_img, slider_minmax, slider_value, status
|
| 465 |
+
|
| 466 |
+
|
| 467 |
+
with gr.Blocks(title="SAM2 Video (Transformers) - Interactive Segmentation (CPU)") as demo:
|
| 468 |
+
state = gr.State(GLOBAL_STATE)
|
| 469 |
+
|
| 470 |
+
gr.Markdown(
|
| 471 |
+
"""
|
| 472 |
+
**SAM2 Video (Transformers)** — CPU-only Space. Upload a video, click to add positive/negative points per object or draw two-click boxes, preview masks, then propagate across the video. Use the slider to scrub frames.
|
| 473 |
+
"""
|
| 474 |
+
)
|
| 475 |
+
|
| 476 |
+
with gr.Row():
|
| 477 |
+
with gr.Column(scale=1):
|
| 478 |
+
video_in = gr.Video(label="Upload video", sources=["upload", "webcam"], interactive=True)
|
| 479 |
+
ckpt_radio = gr.Radio(
|
| 480 |
+
choices=["tiny", "small", "base_plus", "large"],
|
| 481 |
+
value="tiny",
|
| 482 |
+
label="SAM2 checkpoint",
|
| 483 |
+
)
|
| 484 |
+
ckpt_progress = gr.Markdown(visible=False)
|
| 485 |
+
load_status = gr.Markdown(visible=True)
|
| 486 |
+
reset_btn = gr.Button("Reset Session", variant="secondary")
|
| 487 |
+
with gr.Column(scale=2):
|
| 488 |
+
preview = gr.Image(label="Preview", interactive=True)
|
| 489 |
+
frame_slider = gr.Slider(label="Frame", minimum=0, maximum=0, step=1, value=0, interactive=True)
|
| 490 |
+
|
| 491 |
+
with gr.Row():
|
| 492 |
+
obj_id_inp = gr.Number(value=1, precision=0, label="Object ID")
|
| 493 |
+
label_radio = gr.Radio(choices=["positive", "negative"], value="positive", label="Point label")
|
| 494 |
+
clear_old_chk = gr.Checkbox(value=True, label="Clear old inputs for this object")
|
| 495 |
+
prompt_type = gr.Radio(choices=["Points", "Boxes"], value="Points", label="Prompt type")
|
| 496 |
+
with gr.Column():
|
| 497 |
+
propagate_btn = gr.Button("Propagate across video", variant="primary")
|
| 498 |
+
propagate_status = gr.Markdown(visible=True)
|
| 499 |
+
|
| 500 |
+
with gr.Row():
|
| 501 |
+
render_btn = gr.Button("Render MP4 for smooth playback")
|
| 502 |
+
playback_video = gr.Video(label="Rendered Playback", interactive=False)
|
| 503 |
+
|
| 504 |
+
def _on_video_change(video):
|
| 505 |
+
s, min_idx, max_idx, first_frame, status = init_video_session(video)
|
| 506 |
+
return s, gr.update(minimum=min_idx, maximum=max_idx, value=min_idx, interactive=True), first_frame, status
|
| 507 |
+
|
| 508 |
+
video_in.change(
|
| 509 |
+
_on_video_change, inputs=[video_in], outputs=[state, frame_slider, preview, load_status], show_progress=True
|
| 510 |
+
)
|
| 511 |
+
|
| 512 |
+
def _on_ckpt_change(s: AppState, key: str):
|
| 513 |
+
if s is not None and key:
|
| 514 |
+
key = str(key)
|
| 515 |
+
if key != s.model_repo_key:
|
| 516 |
+
s.is_switching_model = True
|
| 517 |
+
s.model_repo_key = key
|
| 518 |
+
s.model_repo_id = None
|
| 519 |
+
s.model = None
|
| 520 |
+
s.processor = None
|
| 521 |
+
yield gr.update(visible=True, value=f"Loading checkpoint: {key}...")
|
| 522 |
+
ensure_session_for_current_model()
|
| 523 |
+
if s is not None:
|
| 524 |
+
s.is_switching_model = False
|
| 525 |
+
yield gr.update(visible=False, value="")
|
| 526 |
+
|
| 527 |
+
ckpt_radio.change(_on_ckpt_change, inputs=[state, ckpt_radio], outputs=[ckpt_progress])
|
| 528 |
+
|
| 529 |
+
def _rebind_session_after_ckpt(s: AppState):
|
| 530 |
+
ensure_session_for_current_model()
|
| 531 |
+
if s is not None:
|
| 532 |
+
s.pending_box_start = None
|
| 533 |
+
return gr.update()
|
| 534 |
+
|
| 535 |
+
ckpt_radio.change(_rebind_session_after_ckpt, inputs=[state], outputs=[])
|
| 536 |
+
|
| 537 |
+
def _sync_frame_idx(state_in: AppState, idx: int):
|
| 538 |
+
if state_in is not None:
|
| 539 |
+
state_in.current_frame_idx = int(idx)
|
| 540 |
+
return update_frame_display(state_in, int(idx))
|
| 541 |
+
|
| 542 |
+
frame_slider.change(_sync_frame_idx, inputs=[state, frame_slider], outputs=preview)
|
| 543 |
+
|
| 544 |
+
def _sync_obj_id(s: AppState, oid):
|
| 545 |
+
if s is not None and oid is not None:
|
| 546 |
+
s.current_obj_id = int(oid)
|
| 547 |
+
return gr.update()
|
| 548 |
+
|
| 549 |
+
obj_id_inp.change(_sync_obj_id, inputs=[state, obj_id_inp], outputs=[])
|
| 550 |
+
|
| 551 |
+
def _sync_label(s: AppState, lab: str):
|
| 552 |
+
if s is not None and lab is not None:
|
| 553 |
+
s.current_label = str(lab)
|
| 554 |
+
return gr.update()
|
| 555 |
+
|
| 556 |
+
label_radio.change(_sync_label, inputs=[state, label_radio], outputs=[])
|
| 557 |
+
|
| 558 |
+
def _sync_prompt_type(s: AppState, val: str):
|
| 559 |
+
if s is not None and val is not None:
|
| 560 |
+
s.current_prompt_type = str(val)
|
| 561 |
+
s.pending_box_start = None
|
| 562 |
+
show_labels = str(val).lower() == "points"
|
| 563 |
+
return gr.update(visible=show_labels)
|
| 564 |
+
|
| 565 |
+
prompt_type.change(_sync_prompt_type, inputs=[state, prompt_type], outputs=[label_radio])
|
| 566 |
+
|
| 567 |
+
preview.select(on_image_click, [preview, state, frame_slider, obj_id_inp, label_radio, clear_old_chk], preview)
|
| 568 |
+
|
| 569 |
+
def _render_video(s: AppState):
|
| 570 |
+
if s is None or s.num_frames == 0:
|
| 571 |
+
raise gr.Error("Load a video first.")
|
| 572 |
+
fps = s.video_fps if s.video_fps and s.video_fps > 0 else 12
|
| 573 |
+
frames_np = []
|
| 574 |
+
for idx in range(s.num_frames):
|
| 575 |
+
img = s.composited_frames.get(idx)
|
| 576 |
+
if img is None:
|
| 577 |
+
img = compose_frame(s, idx)
|
| 578 |
+
frames_np.append(np.array(img)[:, :, ::-1])
|
| 579 |
+
if (idx + 1) % 60 == 0:
|
| 580 |
+
gc.collect()
|
| 581 |
+
out_path = "/tmp/sam2_playback.mp4"
|
| 582 |
+
try:
|
| 583 |
+
import imageio.v3 as iio # type: ignore
|
| 584 |
+
|
| 585 |
+
iio.imwrite(out_path, [fr[:, :, ::-1] for fr in frames_np], plugin="pyav", fps=fps)
|
| 586 |
+
return out_path
|
| 587 |
+
except Exception:
|
| 588 |
+
try:
|
| 589 |
+
import imageio.v2 as imageio # type: ignore
|
| 590 |
+
|
| 591 |
+
imageio.mimsave(out_path, [fr[:, :, ::-1] for fr in frames_np], fps=fps)
|
| 592 |
+
return out_path
|
| 593 |
+
except Exception as e:
|
| 594 |
+
raise gr.Error(f"Failed to render video: {e}")
|
| 595 |
+
|
| 596 |
+
render_btn.click(_render_video, inputs=[state], outputs=[playback_video])
|
| 597 |
+
|
| 598 |
+
propagate_btn.click(propagate_masks, inputs=[state], outputs=[propagate_status], show_progress=True)
|
| 599 |
+
|
| 600 |
+
reset_btn.click(
|
| 601 |
+
reset_session,
|
| 602 |
+
inputs=None,
|
| 603 |
+
outputs=[state, preview, frame_slider, frame_slider, load_status],
|
| 604 |
+
)
|
| 605 |
+
|
| 606 |
+
|
| 607 |
+
demo.queue(api_open=False).launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
git+https://github.com/SangbumChoi/transformers.git@sam2
|
| 3 |
+
torch
|
| 4 |
+
pillow
|
| 5 |
+
opencv-python
|
| 6 |
+
imageio[pyav]
|
| 7 |
+
spaces
|
| 8 |
+
|