Spaces:
Running
on
Zero
Running
on
Zero
xiaoyuxi
commited on
Commit
·
852c062
1
Parent(s):
baffcce
backend
Browse files
app.py
CHANGED
|
@@ -1,412 +1,161 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
-
import sys
|
| 4 |
-
import logging
|
| 5 |
-
import time
|
| 6 |
-
import uuid
|
| 7 |
-
import atexit
|
| 8 |
-
from concurrent.futures import ThreadPoolExecutor
|
| 9 |
-
from typing import Union, List, Tuple, Dict, Any
|
| 10 |
-
|
| 11 |
-
# Configure logging
|
| 12 |
-
logging.basicConfig(level=logging.INFO)
|
| 13 |
-
logger = logging.getLogger(__name__)
|
| 14 |
-
|
| 15 |
-
# Import spaces for ZeroGPU support
|
| 16 |
-
try:
|
| 17 |
-
import spaces
|
| 18 |
-
except ImportError:
|
| 19 |
-
# Fallback for local development
|
| 20 |
-
def spaces(func):
|
| 21 |
-
return func
|
| 22 |
-
|
| 23 |
-
# Import other dependencies
|
| 24 |
-
import subprocess
|
| 25 |
-
import cv2
|
| 26 |
-
import numpy as np
|
| 27 |
-
import threading
|
| 28 |
-
import tempfile
|
| 29 |
-
import shutil
|
| 30 |
-
import glob
|
| 31 |
import json
|
|
|
|
|
|
|
| 32 |
import base64
|
| 33 |
-
import
|
| 34 |
-
import
|
| 35 |
-
import
|
| 36 |
-
import socket
|
| 37 |
-
import gc
|
| 38 |
-
from pathlib import Path
|
| 39 |
-
from einops import rearrange
|
| 40 |
-
from tempfile import TemporaryDirectory
|
| 41 |
-
from http.server import SimpleHTTPRequestHandler
|
| 42 |
-
from socketserver import ThreadingTCPServer
|
| 43 |
-
import socketserver
|
| 44 |
-
import http.server
|
| 45 |
-
import torch
|
| 46 |
-
from huggingface_hub import hf_hub_download
|
| 47 |
-
|
| 48 |
-
# Import custom modules with error handling
|
| 49 |
-
try:
|
| 50 |
-
from app_3rd.sam_utils.inference import SamPredictor, get_sam_predictor, run_inference
|
| 51 |
-
from app_3rd.spatrack_utils.infer_track import get_tracker_predictor, run_tracker, get_points_on_a_grid
|
| 52 |
-
except ImportError as e:
|
| 53 |
-
logger.error(f"Failed to import custom modules: {e}")
|
| 54 |
-
raise
|
| 55 |
-
|
| 56 |
-
MAX_FRAMES = 80
|
| 57 |
-
|
| 58 |
-
try:
|
| 59 |
-
import vggt
|
| 60 |
-
except:
|
| 61 |
-
subprocess.run(["pip", "install", "-e", "./models/vggt"], check=True)
|
| 62 |
-
sys.path.append("/home/user/app/models/vggt")
|
| 63 |
|
| 64 |
-
#
|
| 65 |
-
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
vggt_model.load_state_dict(torch.load(os.environ.get("VGGT_DIR")))
|
| 73 |
-
vggt_model.eval()
|
| 74 |
-
vggt_model = vggt_model.to("cuda")
|
| 75 |
|
| 76 |
-
#
|
| 77 |
-
|
|
|
|
| 78 |
|
| 79 |
-
def
|
| 80 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
try:
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
print("✅ SAM predictor loaded successfully")
|
| 85 |
-
# Keep on CPU for ZeroGPU - will be moved to GPU in the decorated function
|
| 86 |
-
|
| 87 |
-
print("🔧 Loading tracker models...")
|
| 88 |
-
out_dir = os.path.join("temp_init", "results")
|
| 89 |
-
os.makedirs(out_dir, exist_ok=True)
|
| 90 |
-
tracker_model, tracker_viser = get_tracker_predictor(out_dir, vo_points=756)
|
| 91 |
-
print("✅ Tracker models loaded successfully")
|
| 92 |
-
# Keep on CPU for ZeroGPU - will be moved to GPU in the decorated function
|
| 93 |
-
|
| 94 |
-
print("✅ All models initialized successfully!")
|
| 95 |
-
return True
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
except Exception as e:
|
| 98 |
-
print(f"❌ Error
|
| 99 |
-
import traceback
|
| 100 |
-
traceback.print_exc()
|
| 101 |
return False
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
# Thread pool for delayed deletion
|
| 106 |
-
thread_pool_executor = ThreadPoolExecutor(max_workers=2)
|
| 107 |
-
|
| 108 |
-
def delete_later(path: Union[str, os.PathLike], delay: int = 600):
|
| 109 |
-
"""Delete file or directory after specified delay (default 10 minutes)"""
|
| 110 |
-
def _delete():
|
| 111 |
-
try:
|
| 112 |
-
if os.path.isfile(path):
|
| 113 |
-
os.remove(path)
|
| 114 |
-
elif os.path.isdir(path):
|
| 115 |
-
shutil.rmtree(path)
|
| 116 |
-
except Exception as e:
|
| 117 |
-
logger.warning(f"Failed to delete {path}: {e}")
|
| 118 |
-
|
| 119 |
-
def _wait_and_delete():
|
| 120 |
-
time.sleep(delay)
|
| 121 |
-
_delete()
|
| 122 |
-
|
| 123 |
-
thread_pool_executor.submit(_wait_and_delete)
|
| 124 |
-
atexit.register(_delete)
|
| 125 |
-
|
| 126 |
-
def create_user_temp_dir():
|
| 127 |
-
"""Create a unique temporary directory for each user session"""
|
| 128 |
-
session_id = str(uuid.uuid4())[:8] # Short unique ID
|
| 129 |
-
temp_dir = os.path.join("temp", f"session_{session_id}")
|
| 130 |
-
os.makedirs(temp_dir, exist_ok=True)
|
| 131 |
-
|
| 132 |
-
# Schedule deletion after 10 minutes
|
| 133 |
-
delete_later(temp_dir, delay=600)
|
| 134 |
-
|
| 135 |
-
return temp_dir
|
| 136 |
-
|
| 137 |
-
# Wrap the core GPU functions with @spaces.GPU
|
| 138 |
-
@spaces.GPU
|
| 139 |
-
def gpu_run_sam(image, points, boxes):
|
| 140 |
-
"""GPU-accelerated SAM inference"""
|
| 141 |
-
# Initialize SAM predictor inside GPU function
|
| 142 |
-
predictor = get_sam_predictor()
|
| 143 |
-
|
| 144 |
-
# Ensure predictor is on GPU - handle different SAM predictor types
|
| 145 |
try:
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
|
|
|
| 159 |
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
video_reader = decord.VideoReader(video_path)
|
| 184 |
-
video_tensor = torch.from_numpy(video_reader.get_batch(range(len(video_reader))).asnumpy()).permute(0, 3, 1, 2) # Convert to tensor and permute to (N, C, H, W)
|
| 185 |
-
# resize make sure the shortest side is 336
|
| 186 |
-
h, w = video_tensor.shape[2:]
|
| 187 |
-
scale = max(224 / h, 224 / w)
|
| 188 |
-
if scale < 1:
|
| 189 |
-
new_h, new_w = int(h * scale), int(w * scale)
|
| 190 |
-
video_tensor = T.Resize((new_h, new_w))(video_tensor)
|
| 191 |
-
video_tensor = video_tensor[::fps].float()[:MAX_FRAMES]
|
| 192 |
-
|
| 193 |
-
# Move video tensor to GPU
|
| 194 |
-
video_tensor = video_tensor.cuda()
|
| 195 |
-
print(f"Video tensor shape: {video_tensor.shape}, device: {video_tensor.device}")
|
| 196 |
-
|
| 197 |
-
depth_tensor = None
|
| 198 |
-
intrs = None
|
| 199 |
-
extrs = None
|
| 200 |
-
data_npz_load = {}
|
| 201 |
-
|
| 202 |
-
# run vggt
|
| 203 |
-
if os.environ.get("VGGT_DIR", None) is not None:
|
| 204 |
-
# process the image tensor
|
| 205 |
-
video_tensor = preprocess_image(video_tensor)[None]
|
| 206 |
-
with torch.no_grad():
|
| 207 |
-
with torch.cuda.amp.autocast(dtype=torch.bfloat16):
|
| 208 |
-
# Predict attributes including cameras, depth maps, and point maps.
|
| 209 |
-
aggregated_tokens_list, ps_idx = vggt_model.aggregator(video_tensor.cuda()/255)
|
| 210 |
-
pose_enc = vggt_model.camera_head(aggregated_tokens_list)[-1]
|
| 211 |
-
# Extrinsic and intrinsic matrices, following OpenCV convention (camera from world)
|
| 212 |
-
extrinsic, intrinsic = pose_encoding_to_extri_intri(pose_enc, video_tensor.shape[-2:])
|
| 213 |
-
# Predict Depth Maps
|
| 214 |
-
depth_map, depth_conf = vggt_model.depth_head(aggregated_tokens_list, video_tensor.cuda()/255, ps_idx)
|
| 215 |
-
depth_tensor = depth_map.squeeze().cpu().numpy()
|
| 216 |
-
extrs = np.eye(4)[None].repeat(len(depth_tensor), axis=0)
|
| 217 |
-
extrs[:, :3, :4] = extrinsic.squeeze().cpu().numpy()
|
| 218 |
-
intrs = intrinsic.squeeze().cpu().numpy()
|
| 219 |
-
video_tensor = video_tensor.squeeze()
|
| 220 |
-
#NOTE: 20% of the depth is not reliable
|
| 221 |
-
threshold = depth_conf.squeeze()[0].view(-1).quantile(0.6).item()
|
| 222 |
-
unc_metric = depth_conf.squeeze().cpu().numpy() > threshold
|
| 223 |
-
|
| 224 |
-
# Load and process mask
|
| 225 |
-
if os.path.exists(mask_path):
|
| 226 |
-
mask = cv2.imread(mask_path)
|
| 227 |
-
mask = cv2.resize(mask, (video_tensor.shape[3], video_tensor.shape[2]))
|
| 228 |
-
mask = mask.sum(axis=-1)>0
|
| 229 |
-
else:
|
| 230 |
-
mask = np.ones_like(video_tensor[0,0].cpu().numpy())>0
|
| 231 |
-
grid_size = 10
|
| 232 |
-
|
| 233 |
-
# Get frame dimensions and create grid points
|
| 234 |
-
frame_H, frame_W = video_tensor.shape[2:]
|
| 235 |
-
grid_pts = get_points_on_a_grid(grid_size, (frame_H, frame_W), device="cuda") # Create on GPU
|
| 236 |
-
|
| 237 |
-
# Sample mask values at grid points and filter out points where mask=0
|
| 238 |
-
if os.path.exists(mask_path):
|
| 239 |
-
grid_pts_int = grid_pts[0].long()
|
| 240 |
-
mask_values = mask[grid_pts_int.cpu()[...,1], grid_pts_int.cpu()[...,0]]
|
| 241 |
-
grid_pts = grid_pts[:, mask_values]
|
| 242 |
-
|
| 243 |
-
query_xyt = torch.cat([torch.zeros_like(grid_pts[:, :, :1]), grid_pts], dim=2)[0].cpu().numpy()
|
| 244 |
-
|
| 245 |
-
print(f"Query points shape: {query_xyt.shape}")
|
| 246 |
-
# Run model inference
|
| 247 |
-
with torch.amp.autocast(device_type="cuda", dtype=torch.bfloat16):
|
| 248 |
-
(
|
| 249 |
-
c2w_traj, intrs, point_map, conf_depth,
|
| 250 |
-
track3d_pred, track2d_pred, vis_pred, conf_pred, video
|
| 251 |
-
) = tracker_model.forward(video_tensor, depth=depth_tensor,
|
| 252 |
-
intrs=intrs, extrs=extrs,
|
| 253 |
-
queries=query_xyt,
|
| 254 |
-
fps=1, full_point=False, iters_track=4,
|
| 255 |
-
query_no_BA=True, fixed_cam=False, stage=1,
|
| 256 |
-
support_frame=len(video_tensor)-1, replace_ratio=0.2)
|
| 257 |
-
|
| 258 |
-
# Resize results to avoid too large I/O Burden
|
| 259 |
-
max_size = 224
|
| 260 |
-
h, w = video.shape[2:]
|
| 261 |
-
scale = min(max_size / h, max_size / w)
|
| 262 |
-
if scale < 1:
|
| 263 |
-
new_h, new_w = int(h * scale), int(w * scale)
|
| 264 |
-
video = T.Resize((new_h, new_w))(video)
|
| 265 |
-
video_tensor = T.Resize((new_h, new_w))(video_tensor)
|
| 266 |
-
point_map = T.Resize((new_h, new_w))(point_map)
|
| 267 |
-
track2d_pred[...,:2] = track2d_pred[...,:2] * scale
|
| 268 |
-
intrs[:,:2,:] = intrs[:,:2,:] * scale
|
| 269 |
-
conf_depth = T.Resize((new_h, new_w))(conf_depth)
|
| 270 |
-
|
| 271 |
-
# Visualize tracks
|
| 272 |
-
tracker_viser.visualize(video=video[None],
|
| 273 |
-
tracks=track2d_pred[None][...,:2],
|
| 274 |
-
visibility=vis_pred[None],filename="test")
|
| 275 |
-
|
| 276 |
-
# Save in tapip3d format
|
| 277 |
-
data_npz_load["coords"] = (torch.einsum("tij,tnj->tni", c2w_traj[:,:3,:3], track3d_pred[:,:,:3].cpu()) + c2w_traj[:,:3,3][:,None,:]).numpy()
|
| 278 |
-
data_npz_load["extrinsics"] = torch.inverse(c2w_traj).cpu().numpy()
|
| 279 |
-
data_npz_load["intrinsics"] = intrs.cpu().numpy()
|
| 280 |
-
data_npz_load["depths"] = point_map[:,2,...].cpu().numpy()
|
| 281 |
-
data_npz_load["video"] = (video_tensor).cpu().numpy()/255
|
| 282 |
-
data_npz_load["visibs"] = vis_pred.cpu().numpy()
|
| 283 |
-
data_npz_load["confs"] = conf_pred.cpu().numpy()
|
| 284 |
-
data_npz_load["confs_depth"] = conf_depth.cpu().numpy()
|
| 285 |
-
np.savez(os.path.join(out_dir, f'result.npz'), **data_npz_load)
|
| 286 |
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
|
| 297 |
-
def
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
data = np.load(npz_file)
|
| 301 |
-
extrinsics = data["extrinsics"]
|
| 302 |
-
intrinsics = data["intrinsics"]
|
| 303 |
-
trajs = data["coords"]
|
| 304 |
-
T, C, H, W = data["video"].shape
|
| 305 |
-
|
| 306 |
-
fx = intrinsics[0, 0, 0]
|
| 307 |
-
fy = intrinsics[0, 1, 1]
|
| 308 |
-
fov_y = 2 * np.arctan(H / (2 * fy)) * (180 / np.pi)
|
| 309 |
-
fov_x = 2 * np.arctan(W / (2 * fx)) * (180 / np.pi)
|
| 310 |
-
original_aspect_ratio = (W / fx) / (H / fy)
|
| 311 |
-
|
| 312 |
-
rgb_video = (rearrange(data["video"], "T C H W -> T H W C") * 255).astype(np.uint8)
|
| 313 |
-
rgb_video = np.stack([cv2.resize(frame, fixed_size, interpolation=cv2.INTER_AREA)
|
| 314 |
-
for frame in rgb_video])
|
| 315 |
-
|
| 316 |
-
depth_video = data["depths"].astype(np.float32)
|
| 317 |
-
if "confs_depth" in data.keys():
|
| 318 |
-
confs = (data["confs_depth"].astype(np.float32) > 0.5).astype(np.float32)
|
| 319 |
-
depth_video = depth_video * confs
|
| 320 |
-
depth_video = np.stack([cv2.resize(frame, fixed_size, interpolation=cv2.INTER_NEAREST)
|
| 321 |
-
for frame in depth_video])
|
| 322 |
-
|
| 323 |
-
scale_x = fixed_size[0] / W
|
| 324 |
-
scale_y = fixed_size[1] / H
|
| 325 |
-
intrinsics = intrinsics.copy()
|
| 326 |
-
intrinsics[:, 0, :] *= scale_x
|
| 327 |
-
intrinsics[:, 1, :] *= scale_y
|
| 328 |
-
|
| 329 |
-
min_depth = float(depth_video.min()) * 0.8
|
| 330 |
-
max_depth = float(depth_video.max()) * 1.5
|
| 331 |
-
|
| 332 |
-
depth_normalized = (depth_video - min_depth) / (max_depth - min_depth)
|
| 333 |
-
depth_int = (depth_normalized * ((1 << 16) - 1)).astype(np.uint16)
|
| 334 |
-
|
| 335 |
-
depths_rgb = np.zeros((T, fixed_size[1], fixed_size[0], 3), dtype=np.uint8)
|
| 336 |
-
depths_rgb[:, :, :, 0] = (depth_int & 0xFF).astype(np.uint8)
|
| 337 |
-
depths_rgb[:, :, :, 1] = ((depth_int >> 8) & 0xFF).astype(np.uint8)
|
| 338 |
-
|
| 339 |
-
first_frame_inv = np.linalg.inv(extrinsics[0])
|
| 340 |
-
normalized_extrinsics = np.array([first_frame_inv @ ext for ext in extrinsics])
|
| 341 |
-
|
| 342 |
-
normalized_trajs = np.zeros_like(trajs)
|
| 343 |
-
for t in range(T):
|
| 344 |
-
homogeneous_trajs = np.concatenate([trajs[t], np.ones((trajs.shape[1], 1))], axis=1)
|
| 345 |
-
transformed_trajs = (first_frame_inv @ homogeneous_trajs.T).T
|
| 346 |
-
normalized_trajs[t] = transformed_trajs[:, :3]
|
| 347 |
-
|
| 348 |
-
arrays = {
|
| 349 |
-
"rgb_video": rgb_video,
|
| 350 |
-
"depths_rgb": depths_rgb,
|
| 351 |
-
"intrinsics": intrinsics,
|
| 352 |
-
"extrinsics": normalized_extrinsics,
|
| 353 |
-
"inv_extrinsics": np.linalg.inv(normalized_extrinsics),
|
| 354 |
-
"trajectories": normalized_trajs.astype(np.float32),
|
| 355 |
-
"cameraZ": 0.0
|
| 356 |
-
}
|
| 357 |
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
header["meta"] = {
|
| 377 |
-
"depthRange": [min_depth, max_depth],
|
| 378 |
-
"totalFrames": int(T),
|
| 379 |
-
"resolution": fixed_size,
|
| 380 |
-
"baseFrameRate": fps,
|
| 381 |
-
"numTrajectoryPoints": normalized_trajs.shape[1],
|
| 382 |
-
"fov": float(fov_y),
|
| 383 |
-
"fov_x": float(fov_x),
|
| 384 |
-
"original_aspect_ratio": float(original_aspect_ratio),
|
| 385 |
-
"fixed_aspect_ratio": float(fixed_size[0]/fixed_size[1])
|
| 386 |
-
}
|
| 387 |
-
|
| 388 |
-
# Create temporary file for compression
|
| 389 |
-
temp_data_file = f'./temp_data_{int(time.time())}.bin'
|
| 390 |
-
compress_and_write(temp_data_file, header, compressed_blob)
|
| 391 |
-
|
| 392 |
-
# Read the compressed data and encode to base64
|
| 393 |
-
with open(temp_data_file, "rb") as f:
|
| 394 |
-
encoded_blob = base64.b64encode(f.read()).decode("ascii")
|
| 395 |
-
|
| 396 |
-
# Clean up temporary file
|
| 397 |
-
os.unlink(temp_data_file)
|
| 398 |
-
|
| 399 |
-
# Read the HTML template and inject the base64 data
|
| 400 |
-
with open('./_viz/viz_template.html') as f:
|
| 401 |
-
html_template = f.read()
|
| 402 |
-
|
| 403 |
-
# Inject the base64 data into the HTML
|
| 404 |
-
html_content = html_template.replace(
|
| 405 |
-
"<head>",
|
| 406 |
-
f"<head>\n<script>window.embeddedBase64 = `{encoded_blob}`;</script>"
|
| 407 |
-
)
|
| 408 |
-
|
| 409 |
-
return html_content
|
| 410 |
|
| 411 |
def numpy_to_base64(arr):
|
| 412 |
"""Convert numpy array to base64 string"""
|
|
@@ -416,367 +165,934 @@ def base64_to_numpy(b64_str, shape, dtype):
|
|
| 416 |
"""Convert base64 string back to numpy array"""
|
| 417 |
return np.frombuffer(base64.b64decode(b64_str), dtype=dtype).reshape(shape)
|
| 418 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 419 |
def get_video_name(video_path):
|
| 420 |
"""Extract video name without extension"""
|
| 421 |
return os.path.splitext(os.path.basename(video_path))[0]
|
| 422 |
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
"""Backend API for video upload"""
|
| 426 |
try:
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
# Get original video name
|
| 431 |
-
video_name = get_video_name(video_path)
|
| 432 |
-
temp_video_path = os.path.join(user_temp_dir, f"{video_name}.mp4")
|
| 433 |
-
shutil.copy(video_path, temp_video_path)
|
| 434 |
-
|
| 435 |
-
print(f"Video saved to: {temp_video_path}")
|
| 436 |
-
|
| 437 |
-
# Extract first frame
|
| 438 |
-
cap = cv2.VideoCapture(temp_video_path)
|
| 439 |
-
success, frame = cap.read()
|
| 440 |
cap.release()
|
| 441 |
|
| 442 |
-
if
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
new_h, new_w = int(h * scale)//2*2, int(w * scale)//2*2
|
| 449 |
-
frame = cv2.resize(frame, (new_w, new_h), interpolation=cv2.INTER_LINEAR)
|
| 450 |
-
|
| 451 |
-
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 452 |
-
|
| 453 |
-
# Convert frame to base64 string for storage, include temp_dir info
|
| 454 |
-
frame_data = {
|
| 455 |
-
'data': numpy_to_base64(frame),
|
| 456 |
-
'shape': frame.shape,
|
| 457 |
-
'dtype': str(frame.dtype),
|
| 458 |
-
'temp_dir': user_temp_dir # Store temp directory path
|
| 459 |
-
}
|
| 460 |
-
|
| 461 |
-
return {
|
| 462 |
-
"success": True,
|
| 463 |
-
"original_image_state": json.dumps(frame_data),
|
| 464 |
-
"display_image": frame,
|
| 465 |
-
"selected_points": [],
|
| 466 |
-
"temp_dir": user_temp_dir
|
| 467 |
-
}
|
| 468 |
-
|
| 469 |
except Exception as e:
|
| 470 |
-
|
| 471 |
-
return
|
| 472 |
|
| 473 |
-
def
|
| 474 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 475 |
try:
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 480 |
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
#
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
#
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
for mask, _ in o_masks:
|
| 515 |
-
o_mask = np.uint8(mask.squeeze() * 255)
|
| 516 |
-
o_file = os.path.join(temp_dir, f"{video_name}.png")
|
| 517 |
-
cv2.imwrite(o_file, o_mask)
|
| 518 |
-
|
| 519 |
-
return {
|
| 520 |
-
"success": True,
|
| 521 |
-
"display_image": display_img,
|
| 522 |
-
"selected_points": new_sel_pix
|
| 523 |
-
}
|
| 524 |
|
| 525 |
except Exception as e:
|
| 526 |
-
|
| 527 |
-
return
|
| 528 |
|
| 529 |
-
def
|
| 530 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 531 |
try:
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
original_img = base64_to_numpy(frame_data['data'], frame_data['shape'], frame_data['dtype'])
|
| 535 |
-
temp_dir = frame_data.get('temp_dir', 'temp') # Get user-specific temp dir
|
| 536 |
-
|
| 537 |
-
# Create a display image for visualization (just the original image)
|
| 538 |
-
display_img = original_img.copy()
|
| 539 |
-
|
| 540 |
-
# Clear all points
|
| 541 |
-
new_sel_pix = []
|
| 542 |
-
|
| 543 |
-
# Clear any existing masks in user's temp directory
|
| 544 |
-
for mask_file in glob.glob(os.path.join(temp_dir, "*.png")):
|
| 545 |
try:
|
| 546 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 547 |
except Exception as e:
|
| 548 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 549 |
|
| 550 |
-
return
|
| 551 |
-
"success": True,
|
| 552 |
-
"display_image": display_img,
|
| 553 |
-
"selected_points": new_sel_pix
|
| 554 |
-
}
|
| 555 |
|
| 556 |
except Exception as e:
|
| 557 |
-
|
| 558 |
-
return
|
| 559 |
|
| 560 |
-
def
|
| 561 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 562 |
try:
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 566 |
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
return {"success": False, "error": "No video file found"}
|
| 570 |
|
| 571 |
-
|
| 572 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 573 |
|
| 574 |
-
|
| 575 |
-
|
|
|
|
|
|
|
| 576 |
|
| 577 |
-
|
| 578 |
-
html_content = process_point_cloud_data(npz_path)
|
| 579 |
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 585 |
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 591 |
|
| 592 |
except Exception as e:
|
| 593 |
-
|
| 594 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 595 |
|
| 596 |
-
|
| 597 |
-
|
| 598 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 599 |
try:
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
return
|
| 606 |
-
elif function_type == "reset_points":
|
| 607 |
-
# args: original_img, sel_pix
|
| 608 |
-
return backend_reset_points(args[0], args[1])
|
| 609 |
-
elif function_type == "run_tracker":
|
| 610 |
-
# args: grid_size, vo_points, fps, original_image_state
|
| 611 |
-
return backend_run_tracker(args[0], args[1], args[2], args[3])
|
| 612 |
else:
|
| 613 |
-
|
|
|
|
| 614 |
except Exception as e:
|
| 615 |
-
|
| 616 |
-
return
|
| 617 |
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
description="Unified Backend API for SpaTrackV2. This is a private Space that provides core functionality.",
|
| 642 |
-
api_name="unified_api"
|
| 643 |
-
)
|
| 644 |
|
| 645 |
-
#
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
inputs=[
|
| 649 |
-
gr.Textbox(label="Original Image State"),
|
| 650 |
-
gr.JSON(label="Selected Points"),
|
| 651 |
-
gr.Radio(choices=['positive_point', 'negative_point'], label="Point Type"),
|
| 652 |
-
gr.Number(label="Point X"),
|
| 653 |
-
gr.Number(label="Point Y")
|
| 654 |
-
],
|
| 655 |
-
outputs=[
|
| 656 |
-
gr.JSON(label="Result")
|
| 657 |
-
],
|
| 658 |
-
title="Select Point API",
|
| 659 |
-
description="API for selecting points on video frames"
|
| 660 |
-
)
|
| 661 |
|
| 662 |
-
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
],
|
| 668 |
-
outputs=[
|
| 669 |
-
gr.JSON(label="Result")
|
| 670 |
-
],
|
| 671 |
-
title="Reset Points API",
|
| 672 |
-
description="API for resetting points"
|
| 673 |
-
)
|
| 674 |
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
inputs=[
|
| 678 |
-
gr.Number(label="Grid Size", value=50),
|
| 679 |
-
gr.Number(label="VO Points", value=756),
|
| 680 |
-
gr.Number(label="FPS", value=3),
|
| 681 |
-
gr.Textbox(label="Original Image State")
|
| 682 |
-
],
|
| 683 |
-
outputs=[
|
| 684 |
-
gr.JSON(label="Result")
|
| 685 |
-
],
|
| 686 |
-
title="Run Tracker API",
|
| 687 |
-
description="API for running the tracking algorithm"
|
| 688 |
-
)
|
| 689 |
|
| 690 |
-
|
| 691 |
-
|
| 692 |
-
|
| 693 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 694 |
|
| 695 |
-
|
| 696 |
-
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
|
| 700 |
-
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
|
| 704 |
-
|
| 705 |
-
|
| 706 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 707 |
|
| 708 |
-
|
| 709 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 710 |
|
| 711 |
-
|
| 712 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 713 |
|
| 714 |
-
|
| 715 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 716 |
|
| 717 |
-
with gr.
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
|
| 723 |
-
|
| 724 |
-
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
|
| 730 |
-
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
|
| 734 |
-
|
| 735 |
-
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
|
| 739 |
-
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
|
| 749 |
-
|
| 750 |
-
|
| 751 |
-
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
|
| 760 |
-
|
| 761 |
-
|
|
|
|
| 762 |
|
| 763 |
-
|
|
|
|
|
|
|
| 764 |
|
| 765 |
-
#
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 771 |
|
| 772 |
-
|
| 773 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 774 |
|
| 775 |
-
|
| 776 |
-
backend_app.launch(
|
| 777 |
server_name="0.0.0.0",
|
| 778 |
server_port=7860,
|
| 779 |
-
share=
|
| 780 |
debug=True,
|
| 781 |
show_error=True
|
| 782 |
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import json
|
| 4 |
+
import numpy as np
|
| 5 |
+
import cv2
|
| 6 |
import base64
|
| 7 |
+
import requests
|
| 8 |
+
import time
|
| 9 |
+
from typing import List, Tuple
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Backend Space URL - replace with your actual backend space URL
|
| 12 |
+
BACKEND_SPACE_URL = "Yuxihenry/SpatialTrackerV2_Backend" # Replace with actual backend space URL
|
| 13 |
+
hf_token = os.getenv("HF_TOKEN") # Replace with your actual Hugging Face token
|
| 14 |
|
| 15 |
+
# Debug information
|
| 16 |
+
print(f"🔧 Environment Debug Info:")
|
| 17 |
+
print(f" - Backend URL: {BACKEND_SPACE_URL}")
|
| 18 |
+
print(f" - HF Token available: {'Yes' if hf_token else 'No'}")
|
| 19 |
+
print(f" - HF Token length: {len(hf_token) if hf_token else 0}")
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# Flag to track if backend is available
|
| 22 |
+
BACKEND_AVAILABLE = False
|
| 23 |
+
backend_client = None
|
| 24 |
|
| 25 |
+
def check_user_permissions():
|
| 26 |
+
"""Check if user has necessary permissions"""
|
| 27 |
+
print("🔐 Checking user permissions...")
|
| 28 |
+
|
| 29 |
+
if not hf_token:
|
| 30 |
+
print("❌ No HF Token found")
|
| 31 |
+
print("🔧 To get a token:")
|
| 32 |
+
print(" 1. Go to https://huggingface.co/settings/tokens")
|
| 33 |
+
print(" 2. Create a new token with 'read' permissions")
|
| 34 |
+
print(" 3. Set it as environment variable: export HF_TOKEN='your_token'")
|
| 35 |
+
return False
|
| 36 |
+
|
| 37 |
+
# Try to access user info
|
| 38 |
try:
|
| 39 |
+
headers = {'Authorization': f'Bearer {hf_token}'}
|
| 40 |
+
response = requests.get('https://huggingface.co/api/whoami', headers=headers, timeout=5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
if response.status_code == 200:
|
| 43 |
+
user_info = response.json()
|
| 44 |
+
username = user_info.get('name', 'Unknown')
|
| 45 |
+
print(f"✅ Authenticated as: {username}")
|
| 46 |
+
|
| 47 |
+
# Check if user has access to the specific space
|
| 48 |
+
space_url = f"https://huggingface.co/api/spaces/{BACKEND_SPACE_URL}"
|
| 49 |
+
space_response = requests.get(space_url, headers=headers, timeout=5)
|
| 50 |
+
|
| 51 |
+
if space_response.status_code == 200:
|
| 52 |
+
print("✅ You have access to the backend Space")
|
| 53 |
+
return True
|
| 54 |
+
elif space_response.status_code == 401:
|
| 55 |
+
print("❌ You don't have access to the backend Space")
|
| 56 |
+
print("🔧 Solutions:")
|
| 57 |
+
print(" 1. Contact the Space owner to add you as collaborator")
|
| 58 |
+
print(" 2. Ask the owner to make the Space public")
|
| 59 |
+
return False
|
| 60 |
+
elif space_response.status_code == 404:
|
| 61 |
+
print("❌ Backend Space not found")
|
| 62 |
+
print("🔧 Please check if the Space URL is correct")
|
| 63 |
+
return False
|
| 64 |
+
else:
|
| 65 |
+
print(f"⚠️ Unexpected response checking Space access: {space_response.status_code}")
|
| 66 |
+
return False
|
| 67 |
+
|
| 68 |
+
else:
|
| 69 |
+
print(f"❌ Token validation failed: {response.status_code}")
|
| 70 |
+
print("🔧 Your token might be invalid or expired")
|
| 71 |
+
return False
|
| 72 |
+
|
| 73 |
except Exception as e:
|
| 74 |
+
print(f"❌ Error checking permissions: {e}")
|
|
|
|
|
|
|
| 75 |
return False
|
| 76 |
|
| 77 |
+
def check_backend_space_status():
|
| 78 |
+
"""Check if backend space is running via HTTP request"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
try:
|
| 80 |
+
backend_url = f"https://huggingface.co/spaces/{BACKEND_SPACE_URL}"
|
| 81 |
+
print(f"🔍 Checking backend space status: {backend_url}")
|
| 82 |
+
|
| 83 |
+
# Prepare headers with authentication if token is available
|
| 84 |
+
headers = {}
|
| 85 |
+
if hf_token:
|
| 86 |
+
headers['Authorization'] = f'Bearer {hf_token}'
|
| 87 |
+
print(f"🔐 Using HF Token for authentication")
|
| 88 |
+
|
| 89 |
+
# Try to access the space page
|
| 90 |
+
response = requests.get(backend_url, headers=headers, timeout=10)
|
| 91 |
+
|
| 92 |
+
if response.status_code == 200:
|
| 93 |
+
print("✅ Backend space page is accessible")
|
| 94 |
|
| 95 |
+
# Check if space is running (look for common indicators)
|
| 96 |
+
page_content = response.text.lower()
|
| 97 |
+
if "runtime error" in page_content:
|
| 98 |
+
print("❌ Backend space has runtime error")
|
| 99 |
+
return False
|
| 100 |
+
elif "building" in page_content:
|
| 101 |
+
print("🔄 Backend space is building...")
|
| 102 |
+
return False
|
| 103 |
+
elif "sleeping" in page_content:
|
| 104 |
+
print("😴 Backend space is sleeping")
|
| 105 |
+
return False
|
| 106 |
+
else:
|
| 107 |
+
print("✅ Backend space appears to be running")
|
| 108 |
+
return True
|
| 109 |
+
|
| 110 |
+
elif response.status_code == 401:
|
| 111 |
+
print("❌ Authentication failed (HTTP 401)")
|
| 112 |
+
print("🔧 This means:")
|
| 113 |
+
print(" - The backend Space is private")
|
| 114 |
+
print(" - Your HF Token doesn't have access to this Space")
|
| 115 |
+
print(" - You need to be added as a collaborator to the Space")
|
| 116 |
+
print(" - Or the Space owner needs to make it public")
|
| 117 |
+
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
+
elif response.status_code == 404:
|
| 120 |
+
print("❌ Backend space not found (HTTP 404)")
|
| 121 |
+
print("🔧 Please check if the Space URL is correct:")
|
| 122 |
+
print(f" Current URL: {BACKEND_SPACE_URL}")
|
| 123 |
+
return False
|
| 124 |
+
|
| 125 |
+
else:
|
| 126 |
+
print(f"❌ Backend space not accessible (HTTP {response.status_code})")
|
| 127 |
+
print(f"🔧 Response: {response.text[:200]}...")
|
| 128 |
+
return False
|
| 129 |
+
|
| 130 |
+
except requests.RequestException as e:
|
| 131 |
+
print(f"❌ Failed to check backend space status: {e}")
|
| 132 |
+
return False
|
| 133 |
+
except Exception as e:
|
| 134 |
+
print(f"❌ Unexpected error checking backend: {e}")
|
| 135 |
+
return False
|
| 136 |
|
| 137 |
+
def initialize_backend():
|
| 138 |
+
"""Initialize backend connection using gradio_client"""
|
| 139 |
+
global backend_client, BACKEND_AVAILABLE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
+
try:
|
| 142 |
+
from gradio_client import Client
|
| 143 |
+
|
| 144 |
+
# Connect to HF Space
|
| 145 |
+
if hf_token:
|
| 146 |
+
backend_client = Client(BACKEND_SPACE_URL, hf_token=hf_token)
|
| 147 |
+
else:
|
| 148 |
+
backend_client = Client(BACKEND_SPACE_URL)
|
| 149 |
+
|
| 150 |
+
# Test the connection
|
| 151 |
+
backend_client.view_api()
|
| 152 |
+
BACKEND_AVAILABLE = True
|
| 153 |
+
return True
|
| 154 |
+
|
| 155 |
+
except Exception as e:
|
| 156 |
+
print(f"❌ Backend connection failed: {e}")
|
| 157 |
+
BACKEND_AVAILABLE = False
|
| 158 |
+
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
def numpy_to_base64(arr):
|
| 161 |
"""Convert numpy array to base64 string"""
|
|
|
|
| 165 |
"""Convert base64 string back to numpy array"""
|
| 166 |
return np.frombuffer(base64.b64decode(b64_str), dtype=dtype).reshape(shape)
|
| 167 |
|
| 168 |
+
def base64_to_image(b64_str):
|
| 169 |
+
"""Convert base64 string to numpy image array"""
|
| 170 |
+
if not b64_str:
|
| 171 |
+
return None
|
| 172 |
+
try:
|
| 173 |
+
# Decode base64 to bytes
|
| 174 |
+
img_bytes = base64.b64decode(b64_str)
|
| 175 |
+
# Convert bytes to numpy array
|
| 176 |
+
nparr = np.frombuffer(img_bytes, np.uint8)
|
| 177 |
+
# Decode image
|
| 178 |
+
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 179 |
+
# Convert BGR to RGB
|
| 180 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 181 |
+
return img
|
| 182 |
+
except Exception as e:
|
| 183 |
+
print(f"Error converting base64 to image: {e}")
|
| 184 |
+
return None
|
| 185 |
+
|
| 186 |
def get_video_name(video_path):
|
| 187 |
"""Extract video name without extension"""
|
| 188 |
return os.path.splitext(os.path.basename(video_path))[0]
|
| 189 |
|
| 190 |
+
def extract_first_frame(video_path):
|
| 191 |
+
"""Extract first frame from video file"""
|
|
|
|
| 192 |
try:
|
| 193 |
+
cap = cv2.VideoCapture(video_path)
|
| 194 |
+
ret, frame = cap.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
cap.release()
|
| 196 |
|
| 197 |
+
if ret:
|
| 198 |
+
# Convert BGR to RGB
|
| 199 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 200 |
+
return frame_rgb
|
| 201 |
+
else:
|
| 202 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
except Exception as e:
|
| 204 |
+
print(f"Error extracting first frame: {e}")
|
| 205 |
+
return None
|
| 206 |
|
| 207 |
+
def handle_video_upload(video):
|
| 208 |
+
"""Handle video upload and extract first frame"""
|
| 209 |
+
if video is None:
|
| 210 |
+
return None, None, [], 50, 756, 3
|
| 211 |
+
|
| 212 |
try:
|
| 213 |
+
if BACKEND_AVAILABLE and backend_client:
|
| 214 |
+
# Try to use backend API
|
| 215 |
+
try:
|
| 216 |
+
print("🔧 Calling backend API for video upload...")
|
| 217 |
+
|
| 218 |
+
# Call the unified API with upload_video function type
|
| 219 |
+
result = backend_client.predict(
|
| 220 |
+
"upload_video", # function_type
|
| 221 |
+
video, # video file
|
| 222 |
+
"", # original_image_state (not used for upload)
|
| 223 |
+
[], # selected_points (not used for upload)
|
| 224 |
+
"positive_point", # point_type (not used for upload)
|
| 225 |
+
0, # point_x (not used for upload)
|
| 226 |
+
0, # point_y (not used for upload)
|
| 227 |
+
50, # grid_size (not used for upload)
|
| 228 |
+
756, # vo_points (not used for upload)
|
| 229 |
+
3, # fps (not used for upload)
|
| 230 |
+
api_name="/unified_api"
|
| 231 |
+
)
|
| 232 |
+
|
| 233 |
+
print(f"✅ Backend video upload API call successful!")
|
| 234 |
+
print(f"🔧 Result type: {type(result)}")
|
| 235 |
+
print(f"🔧 Result: {result}")
|
| 236 |
+
|
| 237 |
+
# Parse the result - expect a dict with success status
|
| 238 |
+
if isinstance(result, dict) and result.get("success"):
|
| 239 |
+
# Extract data from backend response
|
| 240 |
+
original_image_state = result.get("original_image_state", "")
|
| 241 |
+
display_image = result.get("display_image", None)
|
| 242 |
+
selected_points = result.get("selected_points", [])
|
| 243 |
+
|
| 244 |
+
# Get video settings based on video name
|
| 245 |
+
video_name = get_video_name(video)
|
| 246 |
+
grid_size_val, vo_points_val, fps_val = get_video_settings(video_name)
|
| 247 |
+
|
| 248 |
+
return original_image_state, display_image, selected_points, grid_size_val, vo_points_val, fps_val
|
| 249 |
+
else:
|
| 250 |
+
print("Backend processing failed, using local fallback")
|
| 251 |
+
# Fallback to local processing
|
| 252 |
+
pass
|
| 253 |
+
except Exception as e:
|
| 254 |
+
print(f"Backend API call failed: {e}")
|
| 255 |
+
# Fallback to local processing
|
| 256 |
+
pass
|
| 257 |
+
|
| 258 |
+
# Fallback: local processing
|
| 259 |
+
print("Using local video processing...")
|
| 260 |
+
display_image = extract_first_frame(video)
|
| 261 |
+
|
| 262 |
+
if display_image is not None:
|
| 263 |
+
# Create a state format compatible with backend
|
| 264 |
+
import tempfile
|
| 265 |
+
import shutil
|
| 266 |
|
| 267 |
+
# Create a temporary directory for this session
|
| 268 |
+
session_id = str(int(time.time() * 1000)) # Use timestamp as session ID
|
| 269 |
+
temp_dir = os.path.join("temp_frontend", f"session_{session_id}")
|
| 270 |
+
os.makedirs(temp_dir, exist_ok=True)
|
| 271 |
+
|
| 272 |
+
# Copy video to temp directory with standardized name
|
| 273 |
+
video_name = get_video_name(video)
|
| 274 |
+
temp_video_path = os.path.join(temp_dir, f"{video_name}.mp4")
|
| 275 |
+
shutil.copy(video, temp_video_path)
|
| 276 |
+
|
| 277 |
+
# Create state format compatible with backend
|
| 278 |
+
frame_data = {
|
| 279 |
+
'data': numpy_to_base64(display_image),
|
| 280 |
+
'shape': display_image.shape,
|
| 281 |
+
'dtype': str(display_image.dtype),
|
| 282 |
+
'temp_dir': temp_dir,
|
| 283 |
+
'video_name': video_name,
|
| 284 |
+
'video_path': temp_video_path # Keep for backward compatibility
|
| 285 |
+
}
|
| 286 |
+
|
| 287 |
+
original_image_state = json.dumps(frame_data)
|
| 288 |
+
else:
|
| 289 |
+
# Fallback to simple state if frame extraction fails
|
| 290 |
+
original_image_state = json.dumps({
|
| 291 |
+
"video_path": video,
|
| 292 |
+
"frame": "local_processing_failed"
|
| 293 |
+
})
|
| 294 |
+
|
| 295 |
+
# Get video settings
|
| 296 |
+
video_name = get_video_name(video)
|
| 297 |
+
grid_size_val, vo_points_val, fps_val = get_video_settings(video_name)
|
| 298 |
+
|
| 299 |
+
return original_image_state, display_image, [], grid_size_val, vo_points_val, fps_val
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
|
| 301 |
except Exception as e:
|
| 302 |
+
print(f"Error in handle_video_upload: {e}")
|
| 303 |
+
return None, None, [], 50, 756, 3
|
| 304 |
|
| 305 |
+
def select_point(original_img: str, sel_pix: list, point_type: str, evt: gr.SelectData):
|
| 306 |
+
"""Handle point selection for SAM"""
|
| 307 |
+
if original_img is None:
|
| 308 |
+
return None, []
|
| 309 |
+
|
| 310 |
try:
|
| 311 |
+
if BACKEND_AVAILABLE and backend_client:
|
| 312 |
+
# Try to use backend API
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
try:
|
| 314 |
+
print(f"🔧 Calling backend select point API: x={evt.index[0]}, y={evt.index[1]}, type={point_type}")
|
| 315 |
+
|
| 316 |
+
# Call the unified API with select_point function type
|
| 317 |
+
result = backend_client.predict(
|
| 318 |
+
"select_point", # function_type
|
| 319 |
+
None, # video file (not used for select_point)
|
| 320 |
+
original_img, # original_image_state
|
| 321 |
+
sel_pix, # selected_points
|
| 322 |
+
point_type, # point_type
|
| 323 |
+
evt.index[0], # point_x
|
| 324 |
+
evt.index[1], # point_y
|
| 325 |
+
50, # grid_size (not used for select_point)
|
| 326 |
+
756, # vo_points (not used for select_point)
|
| 327 |
+
3, # fps (not used for select_point)
|
| 328 |
+
api_name="/unified_api"
|
| 329 |
+
)
|
| 330 |
+
|
| 331 |
+
print(f"✅ Backend select point API call successful!")
|
| 332 |
+
print(f"🔧 Result type: {type(result)}")
|
| 333 |
+
print(f"🔧 Result: {result}")
|
| 334 |
+
|
| 335 |
+
# Parse the result - expect a dict with success status
|
| 336 |
+
if isinstance(result, dict) and result.get("success"):
|
| 337 |
+
display_image = result.get("display_image", None)
|
| 338 |
+
new_sel_pix = result.get("selected_points", sel_pix)
|
| 339 |
+
return display_image, new_sel_pix
|
| 340 |
+
else:
|
| 341 |
+
print("Backend processing failed, using local fallback")
|
| 342 |
+
# Fallback to local processing
|
| 343 |
+
pass
|
| 344 |
except Exception as e:
|
| 345 |
+
print(f"Backend API call failed: {e}")
|
| 346 |
+
|
| 347 |
+
# Check for specific gradio_client errors
|
| 348 |
+
if "AppError" in str(type(e)):
|
| 349 |
+
print("🔧 Backend Space has internal errors (AppError)")
|
| 350 |
+
print("🔧 The backend Space code has bugs or configuration issues")
|
| 351 |
+
print("🔧 Contact the Space owner to fix the backend implementation")
|
| 352 |
+
elif "Could not fetch config" in str(e):
|
| 353 |
+
print("🔧 Config fetch failed - possible Gradio version mismatch")
|
| 354 |
+
print("🔧 Frontend and backend may be using incompatible Gradio versions")
|
| 355 |
+
elif "timeout" in str(e).lower():
|
| 356 |
+
print("🔧 Backend request timed out - Space might be overloaded")
|
| 357 |
+
else:
|
| 358 |
+
print(f"🔧 Unexpected error type: {type(e).__name__}")
|
| 359 |
+
|
| 360 |
+
print("🔄 Showing error message instead of visualization...")
|
| 361 |
+
# Fallback to local processing
|
| 362 |
+
pass
|
| 363 |
+
|
| 364 |
+
# Fallback: local processing with improved visualization
|
| 365 |
+
print("Using local point selection with enhanced visualization...")
|
| 366 |
+
|
| 367 |
+
# Parse original image state
|
| 368 |
+
try:
|
| 369 |
+
state_data = json.loads(original_img)
|
| 370 |
+
video_path = state_data.get("video_path")
|
| 371 |
+
except:
|
| 372 |
+
video_path = None
|
| 373 |
+
|
| 374 |
+
if video_path:
|
| 375 |
+
# Re-extract frame and add point with mask visualization
|
| 376 |
+
display_image = extract_first_frame(video_path)
|
| 377 |
+
if display_image is not None:
|
| 378 |
+
# Add point to the image with enhanced visualization
|
| 379 |
+
x, y = evt.index[0], evt.index[1]
|
| 380 |
+
color = (0, 255, 0) if point_type == 'positive_point' else (255, 0, 0)
|
| 381 |
+
|
| 382 |
+
# Draw a larger, more visible point
|
| 383 |
+
cv2.circle(display_image, (x, y), 8, color, -1)
|
| 384 |
+
cv2.circle(display_image, (x, y), 12, (255, 255, 255), 2)
|
| 385 |
+
|
| 386 |
+
# Add point to selected points list
|
| 387 |
+
new_sel_pix = sel_pix.copy()
|
| 388 |
+
new_sel_pix.append([x, y, point_type])
|
| 389 |
+
|
| 390 |
+
return display_image, new_sel_pix
|
| 391 |
|
| 392 |
+
return None, []
|
|
|
|
|
|
|
|
|
|
|
|
|
| 393 |
|
| 394 |
except Exception as e:
|
| 395 |
+
print(f"Error in select_point: {e}")
|
| 396 |
+
return None, []
|
| 397 |
|
| 398 |
+
def reset_points(original_img: str, sel_pix):
|
| 399 |
+
"""Reset points and restore original image"""
|
| 400 |
+
if original_img is None:
|
| 401 |
+
return None, []
|
| 402 |
+
|
| 403 |
try:
|
| 404 |
+
if BACKEND_AVAILABLE and backend_client:
|
| 405 |
+
# Try to use backend API
|
| 406 |
+
try:
|
| 407 |
+
print("🔧 Calling backend reset points API...")
|
| 408 |
+
|
| 409 |
+
# Call the unified API with reset_points function type
|
| 410 |
+
result = backend_client.predict(
|
| 411 |
+
"reset_points", # function_type
|
| 412 |
+
None, # video file (not used for reset_points)
|
| 413 |
+
original_img, # original_image_state
|
| 414 |
+
sel_pix, # selected_points
|
| 415 |
+
"positive_point", # point_type (not used for reset_points)
|
| 416 |
+
0, # point_x (not used for reset_points)
|
| 417 |
+
0, # point_y (not used for reset_points)
|
| 418 |
+
50, # grid_size (not used for reset_points)
|
| 419 |
+
756, # vo_points (not used for reset_points)
|
| 420 |
+
3, # fps (not used for reset_points)
|
| 421 |
+
api_name="/unified_api"
|
| 422 |
+
)
|
| 423 |
+
|
| 424 |
+
print(f"✅ Backend reset points API call successful!")
|
| 425 |
+
print(f"🔧 Result: {result}")
|
| 426 |
+
|
| 427 |
+
# Parse the result
|
| 428 |
+
if isinstance(result, dict) and result.get("success"):
|
| 429 |
+
display_image = result.get("display_image", None)
|
| 430 |
+
new_sel_pix = result.get("selected_points", [])
|
| 431 |
+
return display_image, new_sel_pix
|
| 432 |
+
else:
|
| 433 |
+
print("Backend processing failed, using local fallback")
|
| 434 |
+
# Fallback to local processing
|
| 435 |
+
pass
|
| 436 |
+
except Exception as e:
|
| 437 |
+
print(f"Backend API call failed: {e}")
|
| 438 |
+
# Fallback to local processing
|
| 439 |
+
pass
|
| 440 |
|
| 441 |
+
# Fallback: local processing
|
| 442 |
+
print("Using local reset points...")
|
|
|
|
| 443 |
|
| 444 |
+
# Parse original image state
|
| 445 |
+
try:
|
| 446 |
+
state_data = json.loads(original_img)
|
| 447 |
+
video_path = state_data.get("video_path")
|
| 448 |
+
except:
|
| 449 |
+
video_path = None
|
| 450 |
|
| 451 |
+
if video_path:
|
| 452 |
+
# Re-extract original frame
|
| 453 |
+
display_image = extract_first_frame(video_path)
|
| 454 |
+
return display_image, []
|
| 455 |
|
| 456 |
+
return None, []
|
|
|
|
| 457 |
|
| 458 |
+
except Exception as e:
|
| 459 |
+
print(f"Error in reset_points: {e}")
|
| 460 |
+
return None, []
|
| 461 |
+
|
| 462 |
+
def launch_viz(grid_size, vo_points, fps, original_image_state):
|
| 463 |
+
"""Launch visualization with user-specific temp directory"""
|
| 464 |
+
if original_image_state is None:
|
| 465 |
+
return None, None
|
| 466 |
+
|
| 467 |
+
try:
|
| 468 |
+
if BACKEND_AVAILABLE and backend_client:
|
| 469 |
+
# Try to use backend API
|
| 470 |
+
try:
|
| 471 |
+
print(f"🔧 Calling backend API with parameters: grid_size={grid_size}, vo_points={vo_points}, fps={fps}")
|
| 472 |
+
print(f"🔧 Original image state type: {type(original_image_state)}")
|
| 473 |
+
print(f"🔧 Original image state preview: {str(original_image_state)[:100]}...")
|
| 474 |
+
|
| 475 |
+
# Validate and potentially fix the original_image_state format
|
| 476 |
+
state_to_send = original_image_state
|
| 477 |
+
|
| 478 |
+
# Check if this is a local processing state that needs to be converted
|
| 479 |
+
try:
|
| 480 |
+
if isinstance(original_image_state, str):
|
| 481 |
+
parsed_state = json.loads(original_image_state)
|
| 482 |
+
if "video_path" in parsed_state and "frame" in parsed_state:
|
| 483 |
+
# This is a local processing state, we need to handle differently
|
| 484 |
+
print("🔧 Detected local processing state, cannot use backend for tracking")
|
| 485 |
+
print("🔧 Backend requires proper video upload state from backend API")
|
| 486 |
+
# Fall through to local processing
|
| 487 |
+
raise ValueError("Local state cannot be processed by backend")
|
| 488 |
+
except json.JSONDecodeError:
|
| 489 |
+
print("🔧 Invalid JSON state, cannot send to backend")
|
| 490 |
+
raise ValueError("Invalid state format")
|
| 491 |
+
|
| 492 |
+
# Call the unified API with run_tracker function type
|
| 493 |
+
result = backend_client.predict(
|
| 494 |
+
"run_tracker", # function_type
|
| 495 |
+
None, # video file (not used for run_tracker)
|
| 496 |
+
state_to_send, # original_image_state
|
| 497 |
+
[], # selected_points (not used for run_tracker)
|
| 498 |
+
"positive_point", # point_type (not used for run_tracker)
|
| 499 |
+
0, # point_x (not used for run_tracker)
|
| 500 |
+
0, # point_y (not used for run_tracker)
|
| 501 |
+
grid_size, # grid_size
|
| 502 |
+
vo_points, # vo_points
|
| 503 |
+
fps, # fps
|
| 504 |
+
api_name="/unified_api"
|
| 505 |
+
)
|
| 506 |
+
|
| 507 |
+
print(f"✅ Backend API call successful!")
|
| 508 |
+
print(f"🔧 Result type: {type(result)}")
|
| 509 |
+
print(f"🔧 Result: {result}")
|
| 510 |
+
|
| 511 |
+
# Parse the result
|
| 512 |
+
if isinstance(result, dict) and result.get("success"):
|
| 513 |
+
viz_html = result.get("viz_html", "")
|
| 514 |
+
track_video_path = result.get("track_video_path", "")
|
| 515 |
+
return viz_html, track_video_path
|
| 516 |
+
else:
|
| 517 |
+
error_msg = result.get("error", "Unknown error") if isinstance(result, dict) else "Backend processing failed"
|
| 518 |
+
print(f"❌ Backend processing failed: {error_msg}")
|
| 519 |
+
# Fall through to error message
|
| 520 |
+
pass
|
| 521 |
+
except Exception as e:
|
| 522 |
+
print(f"❌ Backend API call failed: {e}")
|
| 523 |
+
print(f"🔧 Error type: {type(e)}")
|
| 524 |
+
print(f"🔧 Error details: {str(e)}")
|
| 525 |
+
|
| 526 |
+
# Check for specific gradio_client errors
|
| 527 |
+
if "AppError" in str(type(e)):
|
| 528 |
+
print("🔧 Backend Space has internal errors (AppError)")
|
| 529 |
+
print("🔧 The backend Space code has bugs or configuration issues")
|
| 530 |
+
print("🔧 Contact the Space owner to fix the backend implementation")
|
| 531 |
+
elif "Could not fetch config" in str(e):
|
| 532 |
+
print("🔧 Config fetch failed - possible Gradio version mismatch")
|
| 533 |
+
print("🔧 Frontend and backend may be using incompatible Gradio versions")
|
| 534 |
+
elif "timeout" in str(e).lower():
|
| 535 |
+
print("🔧 Backend request timed out - Space might be overloaded")
|
| 536 |
+
elif "Expecting value" in str(e):
|
| 537 |
+
print("🔧 JSON parsing error in backend - state format mismatch")
|
| 538 |
+
print("🔧 This happens when using local processing state with backend API")
|
| 539 |
+
print("🔧 Please upload video again to use backend processing")
|
| 540 |
+
else:
|
| 541 |
+
print(f"🔧 Unexpected error type: {type(e).__name__}")
|
| 542 |
+
|
| 543 |
+
print("🔄 Showing error message instead of visualization...")
|
| 544 |
+
# Fall through to error message
|
| 545 |
+
pass
|
| 546 |
|
| 547 |
+
# Create an informative error message based on the state
|
| 548 |
+
state_info = ""
|
| 549 |
+
try:
|
| 550 |
+
if isinstance(original_image_state, str):
|
| 551 |
+
parsed_state = json.loads(original_image_state)
|
| 552 |
+
if "video_path" in parsed_state:
|
| 553 |
+
video_name = os.path.basename(parsed_state["video_path"])
|
| 554 |
+
state_info = f"Video: {video_name}"
|
| 555 |
+
except:
|
| 556 |
+
state_info = "State format unknown"
|
| 557 |
+
|
| 558 |
+
# Fallback: show message that backend is required
|
| 559 |
+
error_message = f"""
|
| 560 |
+
<div style='border: 3px solid #ff6b6b; border-radius: 10px; padding: 20px; background-color: #fff5f5;'>
|
| 561 |
+
<h3 style='color: #d63031; margin-bottom: 15px;'>⚠️ Backend Processing Required</h3>
|
| 562 |
+
<p style='color: #2d3436; line-height: 1.6;'>
|
| 563 |
+
The tracking and visualization features require backend processing. The current setup is using local processing which is incompatible with the backend API.
|
| 564 |
+
</p>
|
| 565 |
+
<h4 style='color: #d63031; margin: 15px 0 10px 0;'>Solutions:</h4>
|
| 566 |
+
<ul style='color: #2d3436; line-height: 1.6;'>
|
| 567 |
+
<li><strong>Upload video again:</strong> This will properly initialize the backend state</li>
|
| 568 |
+
<li><strong>Select points on the frame:</strong> Ensure you've clicked on the object to track</li>
|
| 569 |
+
<li><strong>Check backend connection:</strong> Ensure the backend Space is running</li>
|
| 570 |
+
<li><strong>Use compatible state:</strong> Avoid local processing mode</li>
|
| 571 |
+
</ul>
|
| 572 |
+
<div style='background-color: #f8f9fa; border-radius: 5px; padding: 10px; margin-top: 15px;'>
|
| 573 |
+
<p style='color: #2d3436; font-weight: bold; margin: 0 0 5px 0;'>Debug Information:</p>
|
| 574 |
+
<p style='color: #666; font-size: 12px; margin: 0;'>Backend Available: {BACKEND_AVAILABLE}</p>
|
| 575 |
+
<p style='color: #666; font-size: 12px; margin: 0;'>Backend Client: {backend_client is not None}</p>
|
| 576 |
+
<p style='color: #666; font-size: 12px; margin: 0;'>Backend URL: {BACKEND_SPACE_URL}</p>
|
| 577 |
+
<p style='color: #666; font-size: 12px; margin: 0;'>State Info: {state_info}</p>
|
| 578 |
+
<p style='color: #666; font-size: 12px; margin: 0;'>Processing Mode: {"Backend" if BACKEND_AVAILABLE else "Local (Limited)"}</p>
|
| 579 |
+
</div>
|
| 580 |
+
<div style='background-color: #e3f2fd; border-radius: 5px; padding: 10px; margin-top: 10px; border-left: 4px solid #2196f3;'>
|
| 581 |
+
<p style='color: #1976d2; font-weight: bold; margin: 0 0 5px 0;'>💡 Quick Fix:</p>
|
| 582 |
+
<p style='color: #1976d2; font-size: 13px; margin: 0;'>
|
| 583 |
+
Try uploading your video again - this should properly initialize the backend state for tracking.
|
| 584 |
+
</p>
|
| 585 |
+
</div>
|
| 586 |
+
</div>
|
| 587 |
+
"""
|
| 588 |
+
return error_message, None
|
| 589 |
|
| 590 |
except Exception as e:
|
| 591 |
+
print(f"Error in launch_viz: {e}")
|
| 592 |
+
return None, None
|
| 593 |
+
|
| 594 |
+
def clear_all():
|
| 595 |
+
"""Clear all buffers and temporary files"""
|
| 596 |
+
return None, None, [], 50, 756, 3
|
| 597 |
+
|
| 598 |
+
def update_tracker_model(model_name):
|
| 599 |
+
"""Update tracker model (placeholder function)"""
|
| 600 |
+
return
|
| 601 |
|
| 602 |
+
def get_video_settings(video_name):
|
| 603 |
+
"""Get video-specific settings based on video name"""
|
| 604 |
+
video_settings = {
|
| 605 |
+
"kiss": (45, 700, 10),
|
| 606 |
+
"backpack": (40, 600, 2),
|
| 607 |
+
"kitchen": (60, 800, 3),
|
| 608 |
+
"pillow": (35, 500, 2),
|
| 609 |
+
"hockey": (45, 700, 2),
|
| 610 |
+
"drifting": (35, 1000, 6),
|
| 611 |
+
"ball": (45, 256, 6),
|
| 612 |
+
"ken_block_0": (45, 700, 2),
|
| 613 |
+
"ego_kc1": (45, 500, 4),
|
| 614 |
+
"vertical_place": (45, 500, 3),
|
| 615 |
+
"ego_teaser": (45, 1200, 10),
|
| 616 |
+
"robot_unitree": (45, 500, 4),
|
| 617 |
+
"droid_robot": (35, 400, 5),
|
| 618 |
+
"robot_2": (45, 256, 5),
|
| 619 |
+
"cinema_0": (45, 356, 5),
|
| 620 |
+
"cinema_1": (45, 756, 3),
|
| 621 |
+
}
|
| 622 |
+
|
| 623 |
+
return video_settings.get(video_name, (50, 756, 3))
|
| 624 |
+
|
| 625 |
+
def test_backend_connection():
|
| 626 |
+
"""Test if backend is actually working"""
|
| 627 |
+
global BACKEND_AVAILABLE
|
| 628 |
+
if not backend_client:
|
| 629 |
+
return False
|
| 630 |
+
|
| 631 |
try:
|
| 632 |
+
print("Testing backend connection with a simple call...")
|
| 633 |
+
# Check if we have fns available
|
| 634 |
+
if hasattr(backend_client, 'fns') and backend_client.fns:
|
| 635 |
+
print("✅ Backend API functions are available")
|
| 636 |
+
print(f"🔧 Available function indices: {list(backend_client.fns.keys())}")
|
| 637 |
+
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 638 |
else:
|
| 639 |
+
print("❌ Backend API functions not found")
|
| 640 |
+
return False
|
| 641 |
except Exception as e:
|
| 642 |
+
print(f"❌ Backend connection test failed: {e}")
|
| 643 |
+
return False
|
| 644 |
|
| 645 |
+
def test_backend_api():
|
| 646 |
+
"""Test specific backend API functions"""
|
| 647 |
+
if not BACKEND_AVAILABLE or not backend_client:
|
| 648 |
+
print("❌ Backend not available for testing")
|
| 649 |
+
return False
|
| 650 |
+
|
| 651 |
+
try:
|
| 652 |
+
print("🧪 Testing backend API functions...")
|
| 653 |
+
|
| 654 |
+
# Test if fns exist and show available indices
|
| 655 |
+
if hasattr(backend_client, 'fns') and backend_client.fns:
|
| 656 |
+
print(f"✅ Backend has {len(backend_client.fns)} functions available")
|
| 657 |
+
for idx in backend_client.fns.keys():
|
| 658 |
+
print(f"✅ Function {idx} is available")
|
| 659 |
+
else:
|
| 660 |
+
print("❌ No functions found in backend API")
|
| 661 |
+
return False
|
| 662 |
+
|
| 663 |
+
return True
|
| 664 |
+
|
| 665 |
+
except Exception as e:
|
| 666 |
+
print(f"❌ Backend API test failed: {e}")
|
| 667 |
+
return False
|
|
|
|
|
|
|
|
|
|
| 668 |
|
| 669 |
+
# Initialize the backend connection
|
| 670 |
+
print("🚀 Initializing frontend application...")
|
| 671 |
+
result = initialize_backend()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 672 |
|
| 673 |
+
# Test backend connection if available
|
| 674 |
+
if result and BACKEND_AVAILABLE:
|
| 675 |
+
print("✅ Backend connection successful!")
|
| 676 |
+
else:
|
| 677 |
+
print("❌ Backend connection failed!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 678 |
|
| 679 |
+
# Create the Gradio interface
|
| 680 |
+
print("🎨 Creating Gradio interface...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 681 |
|
| 682 |
+
with gr.Blocks(
|
| 683 |
+
theme=gr.themes.Soft(),
|
| 684 |
+
title="SpatialTracker V2 - Frontend",
|
| 685 |
+
css="""
|
| 686 |
+
.gradio-container {
|
| 687 |
+
max-width: 1200px !important;
|
| 688 |
+
margin: auto !important;
|
| 689 |
+
}
|
| 690 |
+
.gr-button {
|
| 691 |
+
margin: 5px;
|
| 692 |
+
}
|
| 693 |
+
.gr-form {
|
| 694 |
+
background: white;
|
| 695 |
+
border-radius: 10px;
|
| 696 |
+
padding: 20px;
|
| 697 |
+
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
| 698 |
+
}
|
| 699 |
+
/* 固定视频上传组件高度 */
|
| 700 |
+
.gr-video {
|
| 701 |
+
height: 300px !important;
|
| 702 |
+
min-height: 300px !important;
|
| 703 |
+
max-height: 300px !important;
|
| 704 |
+
}
|
| 705 |
+
.gr-video video {
|
| 706 |
+
height: 260px !important;
|
| 707 |
+
max-height: 260px !important;
|
| 708 |
+
object-fit: contain !important;
|
| 709 |
+
background: #f8f9fa;
|
| 710 |
+
}
|
| 711 |
+
.gr-video .gr-video-player {
|
| 712 |
+
height: 260px !important;
|
| 713 |
+
max-height: 260px !important;
|
| 714 |
+
}
|
| 715 |
+
/* 水平滚动的示例视频样式 */
|
| 716 |
+
.example-videos .gr-examples {
|
| 717 |
+
overflow: visible !important;
|
| 718 |
+
}
|
| 719 |
+
.example-videos .gr-examples .gr-table-wrapper {
|
| 720 |
+
overflow-x: auto !important;
|
| 721 |
+
overflow-y: hidden !important;
|
| 722 |
+
scrollbar-width: thin;
|
| 723 |
+
scrollbar-color: #667eea #f1f1f1;
|
| 724 |
+
}
|
| 725 |
+
.example-videos .gr-examples .gr-table-wrapper::-webkit-scrollbar {
|
| 726 |
+
height: 8px;
|
| 727 |
+
}
|
| 728 |
+
.example-videos .gr-examples .gr-table-wrapper::-webkit-scrollbar-track {
|
| 729 |
+
background: #f1f1f1;
|
| 730 |
+
border-radius: 4px;
|
| 731 |
+
}
|
| 732 |
+
.example-videos .gr-examples .gr-table-wrapper::-webkit-scrollbar-thumb {
|
| 733 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 734 |
+
border-radius: 4px;
|
| 735 |
+
}
|
| 736 |
+
.example-videos .gr-examples .gr-table-wrapper::-webkit-scrollbar-thumb:hover {
|
| 737 |
+
background: linear-gradient(135deg, #5a6fd8 0%, #6a4190 100%);
|
| 738 |
+
}
|
| 739 |
+
.example-videos .gr-examples .gr-table {
|
| 740 |
+
display: flex !important;
|
| 741 |
+
flex-wrap: nowrap !important;
|
| 742 |
+
min-width: max-content !important;
|
| 743 |
+
gap: 10px !important;
|
| 744 |
+
}
|
| 745 |
+
.example-videos .gr-examples .gr-table tbody {
|
| 746 |
+
display: flex !important;
|
| 747 |
+
flex-direction: row !important;
|
| 748 |
+
flex-wrap: nowrap !important;
|
| 749 |
+
gap: 10px !important;
|
| 750 |
+
}
|
| 751 |
+
.example-videos .gr-examples .gr-table tbody tr {
|
| 752 |
+
display: flex !important;
|
| 753 |
+
flex-direction: column !important;
|
| 754 |
+
min-width: 120px !important;
|
| 755 |
+
max-width: 120px !important;
|
| 756 |
+
margin: 0 !important;
|
| 757 |
+
background: white;
|
| 758 |
+
border-radius: 8px;
|
| 759 |
+
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
|
| 760 |
+
transition: all 0.3s ease;
|
| 761 |
+
cursor: pointer;
|
| 762 |
+
}
|
| 763 |
+
.example-videos .gr-examples .gr-table tbody tr:hover {
|
| 764 |
+
transform: translateY(-2px);
|
| 765 |
+
box-shadow: 0 4px 12px rgba(102, 126, 234, 0.2);
|
| 766 |
+
}
|
| 767 |
+
.example-videos .gr-examples .gr-table tbody tr td {
|
| 768 |
+
text-align: center !important;
|
| 769 |
+
padding: 8px !important;
|
| 770 |
+
border: none !important;
|
| 771 |
+
}
|
| 772 |
+
.example-videos .gr-examples .gr-table tbody tr td video {
|
| 773 |
+
border-radius: 6px !important;
|
| 774 |
+
width: 100% !important;
|
| 775 |
+
height: auto !important;
|
| 776 |
+
}
|
| 777 |
+
.example-videos .gr-examples .gr-table tbody tr td:last-child {
|
| 778 |
+
font-size: 12px !important;
|
| 779 |
+
font-weight: 500 !important;
|
| 780 |
+
color: #333 !important;
|
| 781 |
+
padding-top: 4px !important;
|
| 782 |
+
}
|
| 783 |
|
| 784 |
+
/* 新的水平滚动示例视频样式 */
|
| 785 |
+
.horizontal-examples .gr-examples {
|
| 786 |
+
overflow: visible !important;
|
| 787 |
+
}
|
| 788 |
+
.horizontal-examples .gr-examples .gr-table-wrapper {
|
| 789 |
+
overflow-x: auto !important;
|
| 790 |
+
overflow-y: hidden !important;
|
| 791 |
+
scrollbar-width: thin;
|
| 792 |
+
scrollbar-color: #667eea #f1f1f1;
|
| 793 |
+
padding: 10px 0;
|
| 794 |
+
}
|
| 795 |
+
.horizontal-examples .gr-examples .gr-table-wrapper::-webkit-scrollbar {
|
| 796 |
+
height: 8px;
|
| 797 |
+
}
|
| 798 |
+
.horizontal-examples .gr-examples .gr-table-wrapper::-webkit-scrollbar-track {
|
| 799 |
+
background: #f1f1f1;
|
| 800 |
+
border-radius: 4px;
|
| 801 |
+
}
|
| 802 |
+
.horizontal-examples .gr-examples .gr-table-wrapper::-webkit-scrollbar-thumb {
|
| 803 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 804 |
+
border-radius: 4px;
|
| 805 |
+
}
|
| 806 |
+
.horizontal-examples .gr-examples .gr-table-wrapper::-webkit-scrollbar-thumb:hover {
|
| 807 |
+
background: linear-gradient(135deg, #5a6fd8 0%, #6a4190 100%);
|
| 808 |
+
}
|
| 809 |
+
.horizontal-examples .gr-examples .gr-table {
|
| 810 |
+
display: flex !important;
|
| 811 |
+
flex-wrap: nowrap !important;
|
| 812 |
+
min-width: max-content !important;
|
| 813 |
+
gap: 15px !important;
|
| 814 |
+
padding-bottom: 10px;
|
| 815 |
+
}
|
| 816 |
+
.horizontal-examples .gr-examples .gr-table tbody {
|
| 817 |
+
display: flex !important;
|
| 818 |
+
flex-direction: row !important;
|
| 819 |
+
flex-wrap: nowrap !important;
|
| 820 |
+
gap: 15px !important;
|
| 821 |
+
}
|
| 822 |
+
.horizontal-examples .gr-examples .gr-table tbody tr {
|
| 823 |
+
display: flex !important;
|
| 824 |
+
flex-direction: column !important;
|
| 825 |
+
min-width: 160px !important;
|
| 826 |
+
max-width: 160px !important;
|
| 827 |
+
margin: 0 !important;
|
| 828 |
+
background: white;
|
| 829 |
+
border-radius: 12px;
|
| 830 |
+
box-shadow: 0 3px 12px rgba(0,0,0,0.12);
|
| 831 |
+
transition: all 0.3s ease;
|
| 832 |
+
cursor: pointer;
|
| 833 |
+
overflow: hidden;
|
| 834 |
+
}
|
| 835 |
+
.horizontal-examples .gr-examples .gr-table tbody tr:hover {
|
| 836 |
+
transform: translateY(-4px);
|
| 837 |
+
box-shadow: 0 8px 20px rgba(102, 126, 234, 0.25);
|
| 838 |
+
}
|
| 839 |
+
.horizontal-examples .gr-examples .gr-table tbody tr td {
|
| 840 |
+
text-align: center !important;
|
| 841 |
+
padding: 0 !important;
|
| 842 |
+
border: none !important;
|
| 843 |
+
}
|
| 844 |
+
.horizontal-examples .gr-examples .gr-table tbody tr td:first-child {
|
| 845 |
+
padding: 0 !important;
|
| 846 |
+
}
|
| 847 |
+
.horizontal-examples .gr-examples .gr-table tbody tr td video {
|
| 848 |
+
border-radius: 8px 8px 0 0 !important;
|
| 849 |
+
width: 100% !important;
|
| 850 |
+
height: 90px !important;
|
| 851 |
+
object-fit: cover !important;
|
| 852 |
+
}
|
| 853 |
+
.horizontal-examples .gr-examples .gr-table tbody tr td:last-child {
|
| 854 |
+
font-size: 11px !important;
|
| 855 |
+
font-weight: 600 !important;
|
| 856 |
+
color: #333 !important;
|
| 857 |
+
padding: 8px 12px !important;
|
| 858 |
+
background: linear-gradient(135deg, #f8f9ff 0%, #e6f3ff 100%);
|
| 859 |
+
border-radius: 0 0 8px 8px;
|
| 860 |
+
}
|
| 861 |
+
"""
|
| 862 |
+
) as demo:
|
| 863 |
+
|
| 864 |
+
gr.Markdown("""
|
| 865 |
+
# 🎯 SpatialTracker V2 - Frontend Interface
|
| 866 |
+
|
| 867 |
+
Welcome to SpatialTracker V2! This interface allows you to track objects in videos using advanced computer vision techniques.
|
| 868 |
+
|
| 869 |
+
**Instructions:**
|
| 870 |
+
1. Upload a video file or select from examples below
|
| 871 |
+
2. Click on the object you want to track in the first frame
|
| 872 |
+
3. Adjust tracking parameters if needed
|
| 873 |
+
4. Click "Launch Visualization" to start tracking
|
| 874 |
+
|
| 875 |
+
""")
|
| 876 |
+
|
| 877 |
+
# Status indicator with more detailed information
|
| 878 |
+
if BACKEND_AVAILABLE:
|
| 879 |
+
status_text = "🟢 Backend Connected"
|
| 880 |
+
status_details = f"Connected to: {BACKEND_SPACE_URL}"
|
| 881 |
+
else:
|
| 882 |
+
status_text = "🟡 Running in Standalone Mode"
|
| 883 |
+
status_details = f"Backend unavailable: {BACKEND_SPACE_URL}"
|
| 884 |
+
|
| 885 |
+
gr.Markdown(f"**Status:** {status_text}")
|
| 886 |
+
gr.Markdown(f"<small style='color: #666;'>{status_details}</small>", elem_id="status-details")
|
| 887 |
+
|
| 888 |
+
# GitHub Star Reminder - Added back!
|
| 889 |
+
gr.HTML("""
|
| 890 |
+
<div style='background: linear-gradient(135deg, #e8eaff 0%, #f0f2ff 100%);
|
| 891 |
+
border-radius: 10px;
|
| 892 |
+
padding: 15px;
|
| 893 |
+
margin: 15px 0;
|
| 894 |
+
box-shadow: 0 2px 8px rgba(102, 126, 234, 0.1);
|
| 895 |
+
border: 1px solid rgba(102, 126, 234, 0.15);'>
|
| 896 |
+
<div style='text-align: center; color: #4a5568;'>
|
| 897 |
+
<h3 style='margin: 0 0 10px 0; font-size: 18px; text-shadow: none; color: #2d3748;'>
|
| 898 |
+
⭐ Love SpatialTracker? Give us a Star! ⭐
|
| 899 |
+
</h3>
|
| 900 |
+
<p style='margin: 0 0 12px 0; font-size: 14px; opacity: 0.8; color: #4a5568;'>
|
| 901 |
+
Help us grow by starring our repository on GitHub! 🚀
|
| 902 |
+
</p>
|
| 903 |
+
<div style='display: flex; justify-content: center;'>
|
| 904 |
+
<a href="https://github.com/henry123-boy/SpaTrackerV2"
|
| 905 |
+
target="_blank"
|
| 906 |
+
style='display: inline-flex;
|
| 907 |
+
align-items: center;
|
| 908 |
+
gap: 6px;
|
| 909 |
+
background: rgba(102, 126, 234, 0.1);
|
| 910 |
+
color: #4a5568;
|
| 911 |
+
padding: 8px 16px;
|
| 912 |
+
border-radius: 20px;
|
| 913 |
+
text-decoration: none;
|
| 914 |
+
font-weight: bold;
|
| 915 |
+
font-size: 14px;
|
| 916 |
+
backdrop-filter: blur(5px);
|
| 917 |
+
border: 1px solid rgba(102, 126, 234, 0.2);
|
| 918 |
+
transition: all 0.3s ease;'
|
| 919 |
+
onmouseover="this.style.background='rgba(102, 126, 234, 0.15)'; this.style.transform='translateY(-1px)'"
|
| 920 |
+
onmouseout="this.style.background='rgba(102, 126, 234, 0.1)'; this.style.transform='translateY(0)'">
|
| 921 |
+
<span style='font-size: 16px;'>⭐</span>
|
| 922 |
+
Star on GitHub
|
| 923 |
+
</a>
|
| 924 |
+
</div>
|
| 925 |
+
</div>
|
| 926 |
+
</div>
|
| 927 |
+
""")
|
| 928 |
+
|
| 929 |
+
# Example videos section - moved to top
|
| 930 |
+
with gr.Group(elem_classes=["example-videos"]):
|
| 931 |
+
gr.Markdown("### 📂 Example Videos")
|
| 932 |
+
gr.Markdown("Try these example videos to get started quickly:")
|
| 933 |
|
| 934 |
+
# Custom horizontal scrolling video gallery
|
| 935 |
+
gr.HTML("""
|
| 936 |
+
<div style='background-color: #f8f9ff; border-radius: 8px; padding: 10px; margin: 10px 0; border-left: 4px solid #667eea;'>
|
| 937 |
+
<p style='margin: 0; font-size: 13px; color: #666; display: flex; align-items: center; gap: 8px;'>
|
| 938 |
+
<span style='font-size: 16px;'>💡</span>
|
| 939 |
+
<strong>Tip:</strong> Scroll horizontally below to see all example videos
|
| 940 |
+
</p>
|
| 941 |
+
</div>
|
| 942 |
+
""")
|
| 943 |
|
| 944 |
+
# Define video_input here so it can be referenced in examples
|
| 945 |
+
video_input = gr.Video(
|
| 946 |
+
label="Upload Video or Select Example",
|
| 947 |
+
format="mp4",
|
| 948 |
+
height=300
|
| 949 |
+
)
|
| 950 |
|
| 951 |
+
# Create a horizontal scrolling container for the examples
|
| 952 |
+
with gr.Group(elem_classes=["horizontal-examples"]):
|
| 953 |
+
gr.Examples(
|
| 954 |
+
examples=[
|
| 955 |
+
["examples/kiss.mp4"],
|
| 956 |
+
["examples/backpack.mp4"],
|
| 957 |
+
["examples/kitchen.mp4"],
|
| 958 |
+
["examples/pillow.mp4"],
|
| 959 |
+
["examples/hockey.mp4"],
|
| 960 |
+
["examples/drifting.mp4"],
|
| 961 |
+
["examples/ball.mp4"],
|
| 962 |
+
["examples/ken_block_0.mp4"],
|
| 963 |
+
["examples/ego_kc1.mp4"],
|
| 964 |
+
["examples/vertical_place.mp4"],
|
| 965 |
+
["examples/ego_teaser.mp4"],
|
| 966 |
+
["examples/robot_unitree.mp4"],
|
| 967 |
+
["examples/droid_robot.mp4"],
|
| 968 |
+
["examples/robot_2.mp4"],
|
| 969 |
+
["examples/cinema_0.mp4"],
|
| 970 |
+
["examples/cinema_1.mp4"],
|
| 971 |
+
],
|
| 972 |
+
inputs=video_input,
|
| 973 |
+
label="🎬 Click on any example to load it",
|
| 974 |
+
examples_per_page=16 # Show all examples on one page
|
| 975 |
+
)
|
| 976 |
+
|
| 977 |
+
with gr.Row():
|
| 978 |
+
with gr.Column(scale=1):
|
| 979 |
+
# Interactive frame display
|
| 980 |
+
with gr.Group():
|
| 981 |
+
gr.Markdown("### 🎯 Point Selection")
|
| 982 |
+
gr.Markdown("Click on the object you want to track in the frame below:")
|
| 983 |
+
|
| 984 |
+
interactive_frame = gr.Image(
|
| 985 |
+
label="Click to select tracking points",
|
| 986 |
+
type="numpy",
|
| 987 |
+
interactive=True
|
| 988 |
+
)
|
| 989 |
+
|
| 990 |
+
with gr.Row():
|
| 991 |
+
point_type = gr.Radio(
|
| 992 |
+
choices=["positive_point", "negative_point"],
|
| 993 |
+
value="positive_point",
|
| 994 |
+
label="Point Type",
|
| 995 |
+
info="Positive points indicate the object to track, negative points indicate areas to avoid"
|
| 996 |
+
)
|
| 997 |
+
|
| 998 |
+
with gr.Row():
|
| 999 |
+
reset_points_btn = gr.Button("🔄 Reset Points", variant="secondary")
|
| 1000 |
+
clear_all_btn = gr.Button("🗑️ Clear All", variant="stop")
|
| 1001 |
|
| 1002 |
+
with gr.Column(scale=1):
|
| 1003 |
+
# Tracking results
|
| 1004 |
+
with gr.Group():
|
| 1005 |
+
gr.Markdown("### 🎬 Tracking Results")
|
| 1006 |
+
tracking_result_video = gr.Video(
|
| 1007 |
+
label="Tracking Result Video",
|
| 1008 |
+
interactive=False,
|
| 1009 |
+
height=300
|
| 1010 |
+
)
|
| 1011 |
+
|
| 1012 |
+
# 3D Visualization
|
| 1013 |
+
with gr.Group():
|
| 1014 |
+
gr.Markdown("### 🌐 3D Visualization")
|
| 1015 |
+
viz_html = gr.HTML(
|
| 1016 |
+
label="3D Trajectory Visualization",
|
| 1017 |
+
value="<p>Upload a video and select points to see 3D visualization here.</p>"
|
| 1018 |
+
)
|
| 1019 |
+
|
| 1020 |
+
# Advanced settings section - changed to open=True
|
| 1021 |
+
with gr.Accordion("⚙️ Advanced Settings", open=True):
|
| 1022 |
+
gr.Markdown("Adjust these parameters to optimize tracking performance:")
|
| 1023 |
+
with gr.Row():
|
| 1024 |
+
grid_size = gr.Slider(
|
| 1025 |
+
minimum=10,
|
| 1026 |
+
maximum=100,
|
| 1027 |
+
step=10,
|
| 1028 |
+
value=50,
|
| 1029 |
+
label="Grid Size",
|
| 1030 |
+
info="Size of the tracking grid (larger = more detailed)"
|
| 1031 |
+
)
|
| 1032 |
+
vo_points = gr.Slider(
|
| 1033 |
+
minimum=100,
|
| 1034 |
+
maximum=2000,
|
| 1035 |
+
step=50,
|
| 1036 |
+
value=756,
|
| 1037 |
+
label="VO Points",
|
| 1038 |
+
info="Number of visual odometry points (more = better accuracy)"
|
| 1039 |
+
)
|
| 1040 |
+
fps = gr.Slider(
|
| 1041 |
+
minimum=1,
|
| 1042 |
+
maximum=30,
|
| 1043 |
+
step=1,
|
| 1044 |
+
value=3,
|
| 1045 |
+
label="FPS",
|
| 1046 |
+
info="Frames per second for processing (higher = smoother but slower)"
|
| 1047 |
+
)
|
| 1048 |
|
| 1049 |
+
# Launch button
|
| 1050 |
+
with gr.Row():
|
| 1051 |
+
launch_btn = gr.Button("🚀 Launch Visualization", variant="primary", size="lg")
|
| 1052 |
|
| 1053 |
+
# Hidden state variables
|
| 1054 |
+
original_image_state = gr.State(None)
|
| 1055 |
+
selected_points = gr.State([])
|
| 1056 |
+
|
| 1057 |
+
# Event handlers
|
| 1058 |
+
video_input.change(
|
| 1059 |
+
fn=handle_video_upload,
|
| 1060 |
+
inputs=[video_input],
|
| 1061 |
+
outputs=[original_image_state, interactive_frame, selected_points, grid_size, vo_points, fps]
|
| 1062 |
+
)
|
| 1063 |
|
| 1064 |
+
interactive_frame.select(
|
| 1065 |
+
fn=select_point,
|
| 1066 |
+
inputs=[original_image_state, selected_points, point_type],
|
| 1067 |
+
outputs=[interactive_frame, selected_points]
|
| 1068 |
+
)
|
| 1069 |
+
|
| 1070 |
+
reset_points_btn.click(
|
| 1071 |
+
fn=reset_points,
|
| 1072 |
+
inputs=[original_image_state, selected_points],
|
| 1073 |
+
outputs=[interactive_frame, selected_points]
|
| 1074 |
+
)
|
| 1075 |
+
|
| 1076 |
+
clear_all_btn.click(
|
| 1077 |
+
fn=clear_all,
|
| 1078 |
+
outputs=[video_input, interactive_frame, selected_points, grid_size, vo_points, fps]
|
| 1079 |
+
)
|
| 1080 |
+
|
| 1081 |
+
launch_btn.click(
|
| 1082 |
+
fn=launch_viz,
|
| 1083 |
+
inputs=[grid_size, vo_points, fps, original_image_state],
|
| 1084 |
+
outputs=[viz_html, tracking_result_video]
|
| 1085 |
+
)
|
| 1086 |
+
|
| 1087 |
+
# Launch the interface
|
| 1088 |
+
if __name__ == "__main__":
|
| 1089 |
+
print("🌟 Launching SpatialTracker V2 Frontend...")
|
| 1090 |
+
print(f"🔗 Backend Status: {'Connected' if BACKEND_AVAILABLE else 'Disconnected'}")
|
| 1091 |
|
| 1092 |
+
demo.launch(
|
|
|
|
| 1093 |
server_name="0.0.0.0",
|
| 1094 |
server_port=7860,
|
| 1095 |
+
share=True,
|
| 1096 |
debug=True,
|
| 1097 |
show_error=True
|
| 1098 |
)
|