Spaces:
Running on Zero
Running on Zero
xiaoyuxi commited on
Commit ·
09a6aa8
1
Parent(s): dc6fad3
backend
Browse files
app.py
CHANGED
|
@@ -1,161 +1,412 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
-
import
|
| 4 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import cv2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import base64
|
| 7 |
-
import
|
| 8 |
-
import
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 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 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
try:
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
|
|
|
|
|
|
| 75 |
return False
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
try:
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 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"
|
| 135 |
-
|
|
|
|
| 136 |
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
def numpy_to_base64(arr):
|
| 161 |
"""Convert numpy array to base64 string"""
|
|
@@ -165,907 +416,367 @@ def base64_to_numpy(b64_str, shape, dtype):
|
|
| 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 |
-
|
| 191 |
-
|
|
|
|
| 192 |
try:
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
cap.release()
|
| 196 |
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
else:
|
| 202 |
-
return None
|
| 203 |
-
except Exception as e:
|
| 204 |
-
print(f"Error extracting first frame: {e}")
|
| 205 |
-
return None
|
| 206 |
|
| 207 |
-
|
| 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 |
-
#
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
})
|
| 267 |
-
|
| 268 |
-
# Get video settings
|
| 269 |
-
video_name = get_video_name(video)
|
| 270 |
-
grid_size_val, vo_points_val, fps_val = get_video_settings(video_name)
|
| 271 |
|
| 272 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
|
| 274 |
except Exception as e:
|
| 275 |
-
|
| 276 |
-
return
|
| 277 |
|
| 278 |
-
def
|
| 279 |
-
"""
|
| 280 |
-
if original_img is None:
|
| 281 |
-
return None, []
|
| 282 |
-
|
| 283 |
try:
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
print("🔄 Showing error message instead of visualization...")
|
| 334 |
-
# Fallback to local processing
|
| 335 |
-
pass
|
| 336 |
|
| 337 |
-
|
| 338 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 339 |
|
| 340 |
-
#
|
| 341 |
-
|
| 342 |
-
state_data = json.loads(original_img)
|
| 343 |
-
video_path = state_data.get("video_path")
|
| 344 |
-
except:
|
| 345 |
-
video_path = None
|
| 346 |
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
color = (0, 255, 0) if point_type == 'positive_point' else (255, 0, 0)
|
| 354 |
-
|
| 355 |
-
# Draw a larger, more visible point
|
| 356 |
-
cv2.circle(display_image, (x, y), 8, color, -1)
|
| 357 |
-
cv2.circle(display_image, (x, y), 12, (255, 255, 255), 2)
|
| 358 |
-
|
| 359 |
-
# Add point to selected points list
|
| 360 |
-
new_sel_pix = sel_pix.copy()
|
| 361 |
-
new_sel_pix.append([x, y, point_type])
|
| 362 |
-
|
| 363 |
-
return display_image, new_sel_pix
|
| 364 |
|
| 365 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 366 |
|
| 367 |
except Exception as e:
|
| 368 |
-
|
| 369 |
-
return
|
| 370 |
|
| 371 |
-
def
|
| 372 |
-
"""
|
| 373 |
-
if original_img is None:
|
| 374 |
-
return None, []
|
| 375 |
-
|
| 376 |
try:
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
print("🔧 Calling backend reset points API...")
|
| 381 |
-
|
| 382 |
-
# Call the unified API with reset_points function type
|
| 383 |
-
result = backend_client.predict(
|
| 384 |
-
"reset_points", # function_type
|
| 385 |
-
None, # video file (not used for reset_points)
|
| 386 |
-
original_img, # original_image_state
|
| 387 |
-
sel_pix, # selected_points
|
| 388 |
-
"positive_point", # point_type (not used for reset_points)
|
| 389 |
-
0, # point_x (not used for reset_points)
|
| 390 |
-
0, # point_y (not used for reset_points)
|
| 391 |
-
50, # grid_size (not used for reset_points)
|
| 392 |
-
756, # vo_points (not used for reset_points)
|
| 393 |
-
3, # fps (not used for reset_points)
|
| 394 |
-
api_name="/unified_api"
|
| 395 |
-
)
|
| 396 |
-
|
| 397 |
-
print(f"✅ Backend reset points API call successful!")
|
| 398 |
-
print(f"🔧 Result: {result}")
|
| 399 |
-
|
| 400 |
-
# Parse the result
|
| 401 |
-
if isinstance(result, dict) and result.get("success"):
|
| 402 |
-
display_image = result.get("display_image", None)
|
| 403 |
-
new_sel_pix = result.get("selected_points", [])
|
| 404 |
-
return display_image, new_sel_pix
|
| 405 |
-
else:
|
| 406 |
-
print("Backend processing failed, using local fallback")
|
| 407 |
-
# Fallback to local processing
|
| 408 |
-
pass
|
| 409 |
-
except Exception as e:
|
| 410 |
-
print(f"Backend API call failed: {e}")
|
| 411 |
-
# Fallback to local processing
|
| 412 |
-
pass
|
| 413 |
|
| 414 |
-
|
| 415 |
-
|
|
|
|
| 416 |
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
state_data = json.loads(original_img)
|
| 420 |
-
video_path = state_data.get("video_path")
|
| 421 |
-
except:
|
| 422 |
-
video_path = None
|
| 423 |
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
display_image = extract_first_frame(video_path)
|
| 427 |
-
return display_image, []
|
| 428 |
|
| 429 |
-
|
|
|
|
| 430 |
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
"""Launch visualization with user-specific temp directory"""
|
| 437 |
-
if original_image_state is None:
|
| 438 |
-
return None, None
|
| 439 |
-
|
| 440 |
-
try:
|
| 441 |
-
if BACKEND_AVAILABLE and backend_client:
|
| 442 |
-
# Try to use backend API
|
| 443 |
-
try:
|
| 444 |
-
print(f"🔧 Calling backend API with parameters: grid_size={grid_size}, vo_points={vo_points}, fps={fps}")
|
| 445 |
-
print(f"🔧 Original image state type: {type(original_image_state)}")
|
| 446 |
-
print(f"🔧 Original image state preview: {str(original_image_state)[:100]}...")
|
| 447 |
-
|
| 448 |
-
# Validate and potentially fix the original_image_state format
|
| 449 |
-
state_to_send = original_image_state
|
| 450 |
-
|
| 451 |
-
# Check if this is a local processing state that needs to be converted
|
| 452 |
-
try:
|
| 453 |
-
if isinstance(original_image_state, str):
|
| 454 |
-
parsed_state = json.loads(original_image_state)
|
| 455 |
-
if "video_path" in parsed_state and "frame" in parsed_state:
|
| 456 |
-
# This is a local processing state, we need to handle differently
|
| 457 |
-
print("🔧 Detected local processing state, cannot use backend for tracking")
|
| 458 |
-
print("🔧 Backend requires proper video upload state from backend API")
|
| 459 |
-
# Fall through to local processing
|
| 460 |
-
raise ValueError("Local state cannot be processed by backend")
|
| 461 |
-
except json.JSONDecodeError:
|
| 462 |
-
print("🔧 Invalid JSON state, cannot send to backend")
|
| 463 |
-
raise ValueError("Invalid state format")
|
| 464 |
-
|
| 465 |
-
# Call the unified API with run_tracker function type
|
| 466 |
-
result = backend_client.predict(
|
| 467 |
-
"run_tracker", # function_type
|
| 468 |
-
None, # video file (not used for run_tracker)
|
| 469 |
-
state_to_send, # original_image_state
|
| 470 |
-
[], # selected_points (not used for run_tracker)
|
| 471 |
-
"positive_point", # point_type (not used for run_tracker)
|
| 472 |
-
0, # point_x (not used for run_tracker)
|
| 473 |
-
0, # point_y (not used for run_tracker)
|
| 474 |
-
grid_size, # grid_size
|
| 475 |
-
vo_points, # vo_points
|
| 476 |
-
fps, # fps
|
| 477 |
-
api_name="/unified_api"
|
| 478 |
-
)
|
| 479 |
-
|
| 480 |
-
print(f"✅ Backend API call successful!")
|
| 481 |
-
print(f"🔧 Result type: {type(result)}")
|
| 482 |
-
print(f"🔧 Result: {result}")
|
| 483 |
-
|
| 484 |
-
# Parse the result
|
| 485 |
-
if isinstance(result, dict) and result.get("success"):
|
| 486 |
-
viz_html = result.get("viz_html", "")
|
| 487 |
-
track_video_path = result.get("track_video_path", "")
|
| 488 |
-
return viz_html, track_video_path
|
| 489 |
-
else:
|
| 490 |
-
error_msg = result.get("error", "Unknown error") if isinstance(result, dict) else "Backend processing failed"
|
| 491 |
-
print(f"❌ Backend processing failed: {error_msg}")
|
| 492 |
-
# Fall through to error message
|
| 493 |
-
pass
|
| 494 |
-
except Exception as e:
|
| 495 |
-
print(f"❌ Backend API call failed: {e}")
|
| 496 |
-
print(f"🔧 Error type: {type(e)}")
|
| 497 |
-
print(f"🔧 Error details: {str(e)}")
|
| 498 |
-
|
| 499 |
-
# Check for specific gradio_client errors
|
| 500 |
-
if "AppError" in str(type(e)):
|
| 501 |
-
print("🔧 Backend Space has internal errors (AppError)")
|
| 502 |
-
print("🔧 The backend Space code has bugs or configuration issues")
|
| 503 |
-
print("🔧 Contact the Space owner to fix the backend implementation")
|
| 504 |
-
elif "Could not fetch config" in str(e):
|
| 505 |
-
print("🔧 Config fetch failed - possible Gradio version mismatch")
|
| 506 |
-
print("🔧 Frontend and backend may be using incompatible Gradio versions")
|
| 507 |
-
elif "timeout" in str(e).lower():
|
| 508 |
-
print("🔧 Backend request timed out - Space might be overloaded")
|
| 509 |
-
elif "Expecting value" in str(e):
|
| 510 |
-
print("🔧 JSON parsing error in backend - state format mismatch")
|
| 511 |
-
print("🔧 This happens when using local processing state with backend API")
|
| 512 |
-
print("🔧 Please upload video again to use backend processing")
|
| 513 |
-
else:
|
| 514 |
-
print(f"🔧 Unexpected error type: {type(e).__name__}")
|
| 515 |
-
|
| 516 |
-
print("🔄 Showing error message instead of visualization...")
|
| 517 |
-
# Fall through to error message
|
| 518 |
-
pass
|
| 519 |
-
|
| 520 |
-
# Create an informative error message based on the state
|
| 521 |
-
state_info = ""
|
| 522 |
-
try:
|
| 523 |
-
if isinstance(original_image_state, str):
|
| 524 |
-
parsed_state = json.loads(original_image_state)
|
| 525 |
-
if "video_path" in parsed_state:
|
| 526 |
-
video_name = os.path.basename(parsed_state["video_path"])
|
| 527 |
-
state_info = f"Video: {video_name}"
|
| 528 |
-
except:
|
| 529 |
-
state_info = "State format unknown"
|
| 530 |
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
The tracking and visualization features require backend processing. The current setup is using local processing which is incompatible with the backend API.
|
| 537 |
-
</p>
|
| 538 |
-
<h4 style='color: #d63031; margin: 15px 0 10px 0;'>Solutions:</h4>
|
| 539 |
-
<ul style='color: #2d3436; line-height: 1.6;'>
|
| 540 |
-
<li><strong>Upload video again:</strong> This will properly initialize the backend state</li>
|
| 541 |
-
<li><strong>Select points on the frame:</strong> Ensure you've clicked on the object to track</li>
|
| 542 |
-
<li><strong>Check backend connection:</strong> Ensure the backend Space is running</li>
|
| 543 |
-
<li><strong>Use compatible state:</strong> Avoid local processing mode</li>
|
| 544 |
-
</ul>
|
| 545 |
-
<div style='background-color: #f8f9fa; border-radius: 5px; padding: 10px; margin-top: 15px;'>
|
| 546 |
-
<p style='color: #2d3436; font-weight: bold; margin: 0 0 5px 0;'>Debug Information:</p>
|
| 547 |
-
<p style='color: #666; font-size: 12px; margin: 0;'>Backend Available: {BACKEND_AVAILABLE}</p>
|
| 548 |
-
<p style='color: #666; font-size: 12px; margin: 0;'>Backend Client: {backend_client is not None}</p>
|
| 549 |
-
<p style='color: #666; font-size: 12px; margin: 0;'>Backend URL: {BACKEND_SPACE_URL}</p>
|
| 550 |
-
<p style='color: #666; font-size: 12px; margin: 0;'>State Info: {state_info}</p>
|
| 551 |
-
<p style='color: #666; font-size: 12px; margin: 0;'>Processing Mode: {"Backend" if BACKEND_AVAILABLE else "Local (Limited)"}</p>
|
| 552 |
-
</div>
|
| 553 |
-
<div style='background-color: #e3f2fd; border-radius: 5px; padding: 10px; margin-top: 10px; border-left: 4px solid #2196f3;'>
|
| 554 |
-
<p style='color: #1976d2; font-weight: bold; margin: 0 0 5px 0;'>💡 Quick Fix:</p>
|
| 555 |
-
<p style='color: #1976d2; font-size: 13px; margin: 0;'>
|
| 556 |
-
Try uploading your video again - this should properly initialize the backend state for tracking.
|
| 557 |
-
</p>
|
| 558 |
-
</div>
|
| 559 |
-
</div>
|
| 560 |
-
"""
|
| 561 |
-
return error_message, None
|
| 562 |
|
| 563 |
except Exception as e:
|
| 564 |
-
|
| 565 |
-
return
|
| 566 |
-
|
| 567 |
-
def clear_all():
|
| 568 |
-
"""Clear all buffers and temporary files"""
|
| 569 |
-
return None, None, [], 50, 756, 3
|
| 570 |
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
def get_video_settings(video_name):
|
| 576 |
-
"""Get video-specific settings based on video name"""
|
| 577 |
-
video_settings = {
|
| 578 |
-
"kiss": (45, 700, 10),
|
| 579 |
-
"backpack": (40, 600, 2),
|
| 580 |
-
"kitchen": (60, 800, 3),
|
| 581 |
-
"pillow": (35, 500, 2),
|
| 582 |
-
"hockey": (45, 700, 2),
|
| 583 |
-
"drifting": (35, 1000, 6),
|
| 584 |
-
"ball": (45, 256, 6),
|
| 585 |
-
"ken_block_0": (45, 700, 2),
|
| 586 |
-
"ego_kc1": (45, 500, 4),
|
| 587 |
-
"vertical_place": (45, 500, 3),
|
| 588 |
-
"ego_teaser": (45, 1200, 10),
|
| 589 |
-
"robot_unitree": (45, 500, 4),
|
| 590 |
-
"droid_robot": (35, 400, 5),
|
| 591 |
-
"robot_2": (45, 256, 5),
|
| 592 |
-
"cinema_0": (45, 356, 5),
|
| 593 |
-
"cinema_1": (45, 756, 3),
|
| 594 |
-
}
|
| 595 |
-
|
| 596 |
-
return video_settings.get(video_name, (50, 756, 3))
|
| 597 |
-
|
| 598 |
-
def test_backend_connection():
|
| 599 |
-
"""Test if backend is actually working"""
|
| 600 |
-
global BACKEND_AVAILABLE
|
| 601 |
-
if not backend_client:
|
| 602 |
-
return False
|
| 603 |
-
|
| 604 |
try:
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 611 |
else:
|
| 612 |
-
|
| 613 |
-
return False
|
| 614 |
except Exception as e:
|
| 615 |
-
|
| 616 |
-
return False
|
| 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 |
|
| 642 |
-
#
|
| 643 |
-
|
| 644 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 645 |
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
| 650 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 651 |
|
| 652 |
-
|
| 653 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 654 |
|
| 655 |
-
with
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
.gradio-container {
|
| 660 |
-
max-width: 1200px !important;
|
| 661 |
-
margin: auto !important;
|
| 662 |
-
}
|
| 663 |
-
.gr-button {
|
| 664 |
-
margin: 5px;
|
| 665 |
-
}
|
| 666 |
-
.gr-form {
|
| 667 |
-
background: white;
|
| 668 |
-
border-radius: 10px;
|
| 669 |
-
padding: 20px;
|
| 670 |
-
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
| 671 |
-
}
|
| 672 |
-
/* 固定视频上传组件高度 */
|
| 673 |
-
.gr-video {
|
| 674 |
-
height: 300px !important;
|
| 675 |
-
min-height: 300px !important;
|
| 676 |
-
max-height: 300px !important;
|
| 677 |
-
}
|
| 678 |
-
.gr-video video {
|
| 679 |
-
height: 260px !important;
|
| 680 |
-
max-height: 260px !important;
|
| 681 |
-
object-fit: contain !important;
|
| 682 |
-
background: #f8f9fa;
|
| 683 |
-
}
|
| 684 |
-
.gr-video .gr-video-player {
|
| 685 |
-
height: 260px !important;
|
| 686 |
-
max-height: 260px !important;
|
| 687 |
-
}
|
| 688 |
-
/* 水平滚动的示例视频样式 */
|
| 689 |
-
.example-videos .gr-examples {
|
| 690 |
-
overflow: visible !important;
|
| 691 |
-
}
|
| 692 |
-
.example-videos .gr-examples .gr-table-wrapper {
|
| 693 |
-
overflow-x: auto !important;
|
| 694 |
-
overflow-y: hidden !important;
|
| 695 |
-
scrollbar-width: thin;
|
| 696 |
-
scrollbar-color: #667eea #f1f1f1;
|
| 697 |
-
}
|
| 698 |
-
.example-videos .gr-examples .gr-table-wrapper::-webkit-scrollbar {
|
| 699 |
-
height: 8px;
|
| 700 |
-
}
|
| 701 |
-
.example-videos .gr-examples .gr-table-wrapper::-webkit-scrollbar-track {
|
| 702 |
-
background: #f1f1f1;
|
| 703 |
-
border-radius: 4px;
|
| 704 |
-
}
|
| 705 |
-
.example-videos .gr-examples .gr-table-wrapper::-webkit-scrollbar-thumb {
|
| 706 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 707 |
-
border-radius: 4px;
|
| 708 |
-
}
|
| 709 |
-
.example-videos .gr-examples .gr-table-wrapper::-webkit-scrollbar-thumb:hover {
|
| 710 |
-
background: linear-gradient(135deg, #5a6fd8 0%, #6a4190 100%);
|
| 711 |
-
}
|
| 712 |
-
.example-videos .gr-examples .gr-table {
|
| 713 |
-
display: flex !important;
|
| 714 |
-
flex-wrap: nowrap !important;
|
| 715 |
-
min-width: max-content !important;
|
| 716 |
-
gap: 10px !important;
|
| 717 |
-
}
|
| 718 |
-
.example-videos .gr-examples .gr-table tbody {
|
| 719 |
-
display: flex !important;
|
| 720 |
-
flex-direction: row !important;
|
| 721 |
-
flex-wrap: nowrap !important;
|
| 722 |
-
gap: 10px !important;
|
| 723 |
-
}
|
| 724 |
-
.example-videos .gr-examples .gr-table tbody tr {
|
| 725 |
-
display: flex !important;
|
| 726 |
-
flex-direction: column !important;
|
| 727 |
-
min-width: 120px !important;
|
| 728 |
-
max-width: 120px !important;
|
| 729 |
-
margin: 0 !important;
|
| 730 |
-
background: white;
|
| 731 |
-
border-radius: 8px;
|
| 732 |
-
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
|
| 733 |
-
transition: all 0.3s ease;
|
| 734 |
-
cursor: pointer;
|
| 735 |
-
}
|
| 736 |
-
.example-videos .gr-examples .gr-table tbody tr:hover {
|
| 737 |
-
transform: translateY(-2px);
|
| 738 |
-
box-shadow: 0 4px 12px rgba(102, 126, 234, 0.2);
|
| 739 |
-
}
|
| 740 |
-
.example-videos .gr-examples .gr-table tbody tr td {
|
| 741 |
-
text-align: center !important;
|
| 742 |
-
padding: 8px !important;
|
| 743 |
-
border: none !important;
|
| 744 |
-
}
|
| 745 |
-
.example-videos .gr-examples .gr-table tbody tr td video {
|
| 746 |
-
border-radius: 6px !important;
|
| 747 |
-
width: 100% !important;
|
| 748 |
-
height: auto !important;
|
| 749 |
-
}
|
| 750 |
-
.example-videos .gr-examples .gr-table tbody tr td:last-child {
|
| 751 |
-
font-size: 12px !important;
|
| 752 |
-
font-weight: 500 !important;
|
| 753 |
-
color: #333 !important;
|
| 754 |
-
padding-top: 4px !important;
|
| 755 |
-
}
|
| 756 |
-
|
| 757 |
-
/* 新的水平滚动示例视频样式 */
|
| 758 |
-
.horizontal-examples .gr-examples {
|
| 759 |
-
overflow: visible !important;
|
| 760 |
-
}
|
| 761 |
-
.horizontal-examples .gr-examples .gr-table-wrapper {
|
| 762 |
-
overflow-x: auto !important;
|
| 763 |
-
overflow-y: hidden !important;
|
| 764 |
-
scrollbar-width: thin;
|
| 765 |
-
scrollbar-color: #667eea #f1f1f1;
|
| 766 |
-
padding: 10px 0;
|
| 767 |
-
}
|
| 768 |
-
.horizontal-examples .gr-examples .gr-table-wrapper::-webkit-scrollbar {
|
| 769 |
-
height: 8px;
|
| 770 |
-
}
|
| 771 |
-
.horizontal-examples .gr-examples .gr-table-wrapper::-webkit-scrollbar-track {
|
| 772 |
-
background: #f1f1f1;
|
| 773 |
-
border-radius: 4px;
|
| 774 |
-
}
|
| 775 |
-
.horizontal-examples .gr-examples .gr-table-wrapper::-webkit-scrollbar-thumb {
|
| 776 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 777 |
-
border-radius: 4px;
|
| 778 |
-
}
|
| 779 |
-
.horizontal-examples .gr-examples .gr-table-wrapper::-webkit-scrollbar-thumb:hover {
|
| 780 |
-
background: linear-gradient(135deg, #5a6fd8 0%, #6a4190 100%);
|
| 781 |
-
}
|
| 782 |
-
.horizontal-examples .gr-examples .gr-table {
|
| 783 |
-
display: flex !important;
|
| 784 |
-
flex-wrap: nowrap !important;
|
| 785 |
-
min-width: max-content !important;
|
| 786 |
-
gap: 15px !important;
|
| 787 |
-
padding-bottom: 10px;
|
| 788 |
-
}
|
| 789 |
-
.horizontal-examples .gr-examples .gr-table tbody {
|
| 790 |
-
display: flex !important;
|
| 791 |
-
flex-direction: row !important;
|
| 792 |
-
flex-wrap: nowrap !important;
|
| 793 |
-
gap: 15px !important;
|
| 794 |
-
}
|
| 795 |
-
.horizontal-examples .gr-examples .gr-table tbody tr {
|
| 796 |
-
display: flex !important;
|
| 797 |
-
flex-direction: column !important;
|
| 798 |
-
min-width: 160px !important;
|
| 799 |
-
max-width: 160px !important;
|
| 800 |
-
margin: 0 !important;
|
| 801 |
-
background: white;
|
| 802 |
-
border-radius: 12px;
|
| 803 |
-
box-shadow: 0 3px 12px rgba(0,0,0,0.12);
|
| 804 |
-
transition: all 0.3s ease;
|
| 805 |
-
cursor: pointer;
|
| 806 |
-
overflow: hidden;
|
| 807 |
-
}
|
| 808 |
-
.horizontal-examples .gr-examples .gr-table tbody tr:hover {
|
| 809 |
-
transform: translateY(-4px);
|
| 810 |
-
box-shadow: 0 8px 20px rgba(102, 126, 234, 0.25);
|
| 811 |
-
}
|
| 812 |
-
.horizontal-examples .gr-examples .gr-table tbody tr td {
|
| 813 |
-
text-align: center !important;
|
| 814 |
-
padding: 0 !important;
|
| 815 |
-
border: none !important;
|
| 816 |
-
}
|
| 817 |
-
.horizontal-examples .gr-examples .gr-table tbody tr td:first-child {
|
| 818 |
-
padding: 0 !important;
|
| 819 |
-
}
|
| 820 |
-
.horizontal-examples .gr-examples .gr-table tbody tr td video {
|
| 821 |
-
border-radius: 8px 8px 0 0 !important;
|
| 822 |
-
width: 100% !important;
|
| 823 |
-
height: 90px !important;
|
| 824 |
-
object-fit: cover !important;
|
| 825 |
-
}
|
| 826 |
-
.horizontal-examples .gr-examples .gr-table tbody tr td:last-child {
|
| 827 |
-
font-size: 11px !important;
|
| 828 |
-
font-weight: 600 !important;
|
| 829 |
-
color: #333 !important;
|
| 830 |
-
padding: 8px 12px !important;
|
| 831 |
-
background: linear-gradient(135deg, #f8f9ff 0%, #e6f3ff 100%);
|
| 832 |
-
border-radius: 0 0 8px 8px;
|
| 833 |
-
}
|
| 834 |
-
"""
|
| 835 |
-
) as demo:
|
| 836 |
-
|
| 837 |
-
gr.Markdown("""
|
| 838 |
-
# 🎯 SpatialTracker V2 - Frontend Interface
|
| 839 |
-
|
| 840 |
-
Welcome to SpatialTracker V2! This interface allows you to track objects in videos using advanced computer vision techniques.
|
| 841 |
-
|
| 842 |
-
**Instructions:**
|
| 843 |
-
1. Upload a video file or select from examples below
|
| 844 |
-
2. Click on the object you want to track in the first frame
|
| 845 |
-
3. Adjust tracking parameters if needed
|
| 846 |
-
4. Click "Launch Visualization" to start tracking
|
| 847 |
-
|
| 848 |
-
""")
|
| 849 |
|
| 850 |
-
|
| 851 |
-
|
| 852 |
-
|
| 853 |
-
|
| 854 |
-
|
| 855 |
-
|
| 856 |
-
|
| 857 |
-
|
| 858 |
-
|
| 859 |
-
|
| 860 |
-
|
| 861 |
-
|
| 862 |
-
gr.HTML("""
|
| 863 |
-
<div style='background: linear-gradient(135deg, #e8eaff 0%, #f0f2ff 100%);
|
| 864 |
-
border-radius: 10px;
|
| 865 |
-
padding: 15px;
|
| 866 |
-
margin: 15px 0;
|
| 867 |
-
box-shadow: 0 2px 8px rgba(102, 126, 234, 0.1);
|
| 868 |
-
border: 1px solid rgba(102, 126, 234, 0.15);'>
|
| 869 |
-
<div style='text-align: center; color: #4a5568;'>
|
| 870 |
-
<h3 style='margin: 0 0 10px 0; font-size: 18px; text-shadow: none; color: #2d3748;'>
|
| 871 |
-
⭐ Love SpatialTracker? Give us a Star! ⭐
|
| 872 |
-
</h3>
|
| 873 |
-
<p style='margin: 0 0 12px 0; font-size: 14px; opacity: 0.8; color: #4a5568;'>
|
| 874 |
-
Help us grow by starring our repository on GitHub! 🚀
|
| 875 |
-
</p>
|
| 876 |
-
<div style='display: flex; justify-content: center;'>
|
| 877 |
-
<a href="https://github.com/henry123-boy/SpaTrackerV2"
|
| 878 |
-
target="_blank"
|
| 879 |
-
style='display: inline-flex;
|
| 880 |
-
align-items: center;
|
| 881 |
-
gap: 6px;
|
| 882 |
-
background: rgba(102, 126, 234, 0.1);
|
| 883 |
-
color: #4a5568;
|
| 884 |
-
padding: 8px 16px;
|
| 885 |
-
border-radius: 20px;
|
| 886 |
-
text-decoration: none;
|
| 887 |
-
font-weight: bold;
|
| 888 |
-
font-size: 14px;
|
| 889 |
-
backdrop-filter: blur(5px);
|
| 890 |
-
border: 1px solid rgba(102, 126, 234, 0.2);
|
| 891 |
-
transition: all 0.3s ease;'
|
| 892 |
-
onmouseover="this.style.background='rgba(102, 126, 234, 0.15)'; this.style.transform='translateY(-1px)'"
|
| 893 |
-
onmouseout="this.style.background='rgba(102, 126, 234, 0.1)'; this.style.transform='translateY(0)'">
|
| 894 |
-
<span style='font-size: 16px;'>⭐</span>
|
| 895 |
-
Star on GitHub
|
| 896 |
-
</a>
|
| 897 |
-
</div>
|
| 898 |
-
</div>
|
| 899 |
-
</div>
|
| 900 |
-
""")
|
| 901 |
-
|
| 902 |
-
# Example videos section - moved to top
|
| 903 |
-
with gr.Group(elem_classes=["example-videos"]):
|
| 904 |
-
gr.Markdown("### 📂 Example Videos")
|
| 905 |
-
gr.Markdown("Try these example videos to get started quickly:")
|
| 906 |
|
| 907 |
-
|
| 908 |
-
|
| 909 |
-
<div style='background-color: #f8f9ff; border-radius: 8px; padding: 10px; margin: 10px 0; border-left: 4px solid #667eea;'>
|
| 910 |
-
<p style='margin: 0; font-size: 13px; color: #666; display: flex; align-items: center; gap: 8px;'>
|
| 911 |
-
<span style='font-size: 16px;'>💡</span>
|
| 912 |
-
<strong>Tip:</strong> Scroll horizontally below to see all example videos
|
| 913 |
-
</p>
|
| 914 |
-
</div>
|
| 915 |
-
""")
|
| 916 |
|
| 917 |
-
|
| 918 |
-
|
| 919 |
-
label="Upload Video or Select Example",
|
| 920 |
-
format="mp4",
|
| 921 |
-
height=300
|
| 922 |
-
)
|
| 923 |
|
| 924 |
-
|
| 925 |
-
|
| 926 |
-
gr.Examples(
|
| 927 |
-
examples=[
|
| 928 |
-
["examples/kiss.mp4"],
|
| 929 |
-
["examples/backpack.mp4"],
|
| 930 |
-
["examples/kitchen.mp4"],
|
| 931 |
-
["examples/pillow.mp4"],
|
| 932 |
-
["examples/hockey.mp4"],
|
| 933 |
-
["examples/drifting.mp4"],
|
| 934 |
-
["examples/ball.mp4"],
|
| 935 |
-
["examples/ken_block_0.mp4"],
|
| 936 |
-
["examples/ego_kc1.mp4"],
|
| 937 |
-
["examples/vertical_place.mp4"],
|
| 938 |
-
["examples/ego_teaser.mp4"],
|
| 939 |
-
["examples/robot_unitree.mp4"],
|
| 940 |
-
["examples/droid_robot.mp4"],
|
| 941 |
-
["examples/robot_2.mp4"],
|
| 942 |
-
["examples/cinema_0.mp4"],
|
| 943 |
-
["examples/cinema_1.mp4"],
|
| 944 |
-
],
|
| 945 |
-
inputs=video_input,
|
| 946 |
-
label="🎬 Click on any example to load it",
|
| 947 |
-
examples_per_page=16 # Show all examples on one page
|
| 948 |
-
)
|
| 949 |
-
|
| 950 |
-
with gr.Row():
|
| 951 |
-
with gr.Column(scale=1):
|
| 952 |
-
# Interactive frame display
|
| 953 |
-
with gr.Group():
|
| 954 |
-
gr.Markdown("### 🎯 Point Selection")
|
| 955 |
-
gr.Markdown("Click on the object you want to track in the frame below:")
|
| 956 |
-
|
| 957 |
-
interactive_frame = gr.Image(
|
| 958 |
-
label="Click to select tracking points",
|
| 959 |
-
type="numpy",
|
| 960 |
-
interactive=True
|
| 961 |
-
)
|
| 962 |
-
|
| 963 |
-
with gr.Row():
|
| 964 |
-
point_type = gr.Radio(
|
| 965 |
-
choices=["positive_point", "negative_point"],
|
| 966 |
-
value="positive_point",
|
| 967 |
-
label="Point Type",
|
| 968 |
-
info="Positive points indicate the object to track, negative points indicate areas to avoid"
|
| 969 |
-
)
|
| 970 |
-
|
| 971 |
-
with gr.Row():
|
| 972 |
-
reset_points_btn = gr.Button("🔄 Reset Points", variant="secondary")
|
| 973 |
-
clear_all_btn = gr.Button("🗑️ Clear All", variant="stop")
|
| 974 |
|
| 975 |
-
with gr.
|
| 976 |
-
|
| 977 |
-
|
| 978 |
-
|
| 979 |
-
|
| 980 |
-
|
| 981 |
-
|
| 982 |
-
|
| 983 |
-
|
| 984 |
-
|
| 985 |
-
|
| 986 |
-
|
| 987 |
-
|
| 988 |
-
|
| 989 |
-
|
| 990 |
-
|
| 991 |
-
|
| 992 |
-
|
| 993 |
-
|
| 994 |
-
|
| 995 |
-
|
| 996 |
-
|
| 997 |
-
|
| 998 |
-
|
| 999 |
-
|
| 1000 |
-
|
| 1001 |
-
|
| 1002 |
-
|
| 1003 |
-
|
| 1004 |
-
|
| 1005 |
-
|
| 1006 |
-
|
| 1007 |
-
|
| 1008 |
-
|
| 1009 |
-
|
| 1010 |
-
|
| 1011 |
-
|
| 1012 |
-
|
| 1013 |
-
|
| 1014 |
-
|
| 1015 |
-
|
| 1016 |
-
|
| 1017 |
-
|
| 1018 |
-
|
| 1019 |
-
|
| 1020 |
-
)
|
| 1021 |
-
|
| 1022 |
-
# Launch button
|
| 1023 |
-
with gr.Row():
|
| 1024 |
-
launch_btn = gr.Button("🚀 Launch Visualization", variant="primary", size="lg")
|
| 1025 |
-
|
| 1026 |
-
# Hidden state variables
|
| 1027 |
-
original_image_state = gr.State(None)
|
| 1028 |
-
selected_points = gr.State([])
|
| 1029 |
-
|
| 1030 |
-
# Event handlers
|
| 1031 |
-
video_input.change(
|
| 1032 |
-
fn=handle_video_upload,
|
| 1033 |
-
inputs=[video_input],
|
| 1034 |
-
outputs=[original_image_state, interactive_frame, selected_points, grid_size, vo_points, fps]
|
| 1035 |
-
)
|
| 1036 |
-
|
| 1037 |
-
interactive_frame.select(
|
| 1038 |
-
fn=select_point,
|
| 1039 |
-
inputs=[original_image_state, selected_points, point_type],
|
| 1040 |
-
outputs=[interactive_frame, selected_points]
|
| 1041 |
-
)
|
| 1042 |
|
| 1043 |
-
|
| 1044 |
-
fn=reset_points,
|
| 1045 |
-
inputs=[original_image_state, selected_points],
|
| 1046 |
-
outputs=[interactive_frame, selected_points]
|
| 1047 |
-
)
|
| 1048 |
|
| 1049 |
-
|
| 1050 |
-
|
| 1051 |
-
|
| 1052 |
-
|
|
|
|
|
|
|
| 1053 |
|
| 1054 |
-
|
| 1055 |
-
|
| 1056 |
-
inputs=[grid_size, vo_points, fps, original_image_state],
|
| 1057 |
-
outputs=[viz_html, tracking_result_video]
|
| 1058 |
-
)
|
| 1059 |
-
|
| 1060 |
-
# Launch the interface
|
| 1061 |
-
if __name__ == "__main__":
|
| 1062 |
-
print("🌟 Launching SpatialTracker V2 Frontend...")
|
| 1063 |
-
print(f"🔗 Backend Status: {'Connected' if BACKEND_AVAILABLE else 'Disconnected'}")
|
| 1064 |
|
| 1065 |
-
|
|
|
|
| 1066 |
server_name="0.0.0.0",
|
| 1067 |
server_port=7860,
|
| 1068 |
-
share=
|
| 1069 |
debug=True,
|
| 1070 |
show_error=True
|
| 1071 |
)
|
|
|
|
| 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 struct
|
| 34 |
+
import zlib
|
| 35 |
+
import argparse
|
| 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 |
+
# init the model
|
| 65 |
+
os.environ["VGGT_DIR"] = hf_hub_download("facebook/VGGT-1B", "model.pt")
|
| 66 |
+
|
| 67 |
+
if os.environ.get("VGGT_DIR", None) is not None:
|
| 68 |
+
from vggt.models.vggt import VGGT
|
| 69 |
+
from vggt.utils.load_fn import preprocess_image
|
| 70 |
+
from vggt.utils.pose_enc import pose_encoding_to_extri_intri
|
| 71 |
+
vggt_model = VGGT()
|
| 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 |
+
# Global model initialization
|
| 77 |
+
print("🚀 Initializing global models...")
|
| 78 |
+
|
| 79 |
+
def init_global_models():
|
| 80 |
+
"""Initialize global models (CPU only for ZeroGPU compatibility)"""
|
| 81 |
try:
|
| 82 |
+
print("🔧 Loading SAM predictor...")
|
| 83 |
+
sam_predictor = get_sam_predictor()
|
| 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 initializing models: {e}")
|
| 99 |
+
import traceback
|
| 100 |
+
traceback.print_exc()
|
| 101 |
return False
|
| 102 |
|
| 103 |
+
# Initialize models at startup
|
| 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 |
+
if hasattr(predictor, 'model'):
|
| 147 |
+
# For transformers SAM
|
| 148 |
+
predictor.model = predictor.model.cuda()
|
| 149 |
+
elif hasattr(predictor, 'sam'):
|
| 150 |
+
# For segment-anything SAM
|
| 151 |
+
predictor.sam = predictor.sam.cuda()
|
| 152 |
+
elif hasattr(predictor, 'to'):
|
| 153 |
+
# Generic PyTorch model
|
| 154 |
+
predictor = predictor.to('cuda')
|
| 155 |
+
|
| 156 |
+
# Also ensure image is on the right device if it's a tensor
|
| 157 |
+
if hasattr(image, 'cuda'):
|
| 158 |
+
image = image.cuda()
|
|
|
|
| 159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
except Exception as e:
|
| 161 |
+
print(f"Warning: Could not move predictor to GPU: {e}")
|
| 162 |
+
|
| 163 |
+
return run_inference(predictor, image, points, boxes)
|
| 164 |
|
| 165 |
+
@spaces.GPU
|
| 166 |
+
def gpu_run_tracker(temp_dir, video_name, grid_size, vo_points, fps):
|
| 167 |
+
"""GPU-accelerated tracking"""
|
| 168 |
+
import torchvision.transforms as T
|
| 169 |
+
import decord
|
| 170 |
|
| 171 |
+
# Initialize tracker model inside GPU function
|
| 172 |
+
out_dir = os.path.join(temp_dir, "results")
|
| 173 |
+
os.makedirs(out_dir, exist_ok=True)
|
| 174 |
+
tracker_model, tracker_viser = get_tracker_predictor(out_dir, vo_points=vo_points)
|
| 175 |
+
|
| 176 |
+
# Setup paths
|
| 177 |
+
video_path = os.path.join(temp_dir, f"{video_name}.mp4")
|
| 178 |
+
mask_path = os.path.join(temp_dir, f"{video_name}.png")
|
| 179 |
+
out_dir = os.path.join(temp_dir, "results")
|
| 180 |
+
os.makedirs(out_dir, exist_ok=True)
|
| 181 |
+
|
| 182 |
+
# Load video using decord
|
| 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 |
+
return os.path.join(out_dir, "result.npz"), os.path.join(out_dir, "test_pred_track.mp4")
|
| 288 |
+
|
| 289 |
+
def compress_and_write(filename, header, blob):
|
| 290 |
+
header_bytes = json.dumps(header).encode("utf-8")
|
| 291 |
+
header_len = struct.pack("<I", len(header_bytes))
|
| 292 |
+
with open(filename, "wb") as f:
|
| 293 |
+
f.write(header_len)
|
| 294 |
+
f.write(header_bytes)
|
| 295 |
+
f.write(blob)
|
| 296 |
+
|
| 297 |
+
def process_point_cloud_data(npz_file, width=256, height=192, fps=4):
|
| 298 |
+
fixed_size = (width, height)
|
| 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 |
+
header = {}
|
| 359 |
+
blob_parts = []
|
| 360 |
+
offset = 0
|
| 361 |
+
for key, arr in arrays.items():
|
| 362 |
+
arr = np.ascontiguousarray(arr)
|
| 363 |
+
arr_bytes = arr.tobytes()
|
| 364 |
+
header[key] = {
|
| 365 |
+
"dtype": str(arr.dtype),
|
| 366 |
+
"shape": arr.shape,
|
| 367 |
+
"offset": offset,
|
| 368 |
+
"length": len(arr_bytes)
|
| 369 |
+
}
|
| 370 |
+
blob_parts.append(arr_bytes)
|
| 371 |
+
offset += len(arr_bytes)
|
| 372 |
+
|
| 373 |
+
raw_blob = b"".join(blob_parts)
|
| 374 |
+
compressed_blob = zlib.compress(raw_blob, level=9)
|
| 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 |
"""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 |
+
# Backend API Functions
|
| 424 |
+
def backend_upload_video(video_path: str) -> Dict[str, Any]:
|
| 425 |
+
"""Backend API for video upload"""
|
| 426 |
try:
|
| 427 |
+
# Create user-specific temporary directory
|
| 428 |
+
user_temp_dir = create_user_temp_dir()
|
|
|
|
| 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 not success:
|
| 443 |
+
return {"success": False, "error": "Failed to read video"}
|
| 444 |
+
|
| 445 |
+
# Resize frame to have minimum side length of 336
|
| 446 |
+
h, w = frame.shape[:2]
|
| 447 |
+
scale = 336 / min(h, w)
|
| 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 |
+
logger.error(f"Error in backend_upload_video: {e}")
|
| 471 |
+
return {"success": False, "error": str(e)}
|
| 472 |
|
| 473 |
+
def backend_select_point(original_img: str, sel_pix: list, point_type: str, point_x: int, point_y: int) -> Dict[str, Any]:
|
| 474 |
+
"""Backend API for point selection"""
|
|
|
|
|
|
|
|
|
|
| 475 |
try:
|
| 476 |
+
# Convert stored image data back to numpy array
|
| 477 |
+
frame_data = json.loads(original_img)
|
| 478 |
+
original_img = base64_to_numpy(frame_data['data'], frame_data['shape'], frame_data['dtype'])
|
| 479 |
+
temp_dir = frame_data.get('temp_dir', 'temp') # Get user-specific temp dir
|
| 480 |
+
|
| 481 |
+
# Create a display image for visualization
|
| 482 |
+
display_img = original_img.copy()
|
| 483 |
+
# Create a new list instead of modifying the existing one
|
| 484 |
+
new_sel_pix = sel_pix.copy() if sel_pix else []
|
| 485 |
+
new_sel_pix.append(((point_x, point_y), 1 if point_type == 'positive_point' else 0))
|
| 486 |
+
|
| 487 |
+
# Run SAM inference
|
| 488 |
+
o_masks = gpu_run_sam(original_img, new_sel_pix, [])
|
| 489 |
+
|
| 490 |
+
# Draw points on display image
|
| 491 |
+
COLORS = [(0, 0, 255), (0, 255, 255)] # BGR: Red for negative, Yellow for positive
|
| 492 |
+
MARKERS = [1, 5] # Cross for negative, Star for positive
|
| 493 |
+
MARKER_SIZE = 8 # Increased marker size
|
| 494 |
+
|
| 495 |
+
for point, label in new_sel_pix:
|
| 496 |
+
cv2.drawMarker(display_img, point, COLORS[label], markerType=MARKERS[label], markerSize=MARKER_SIZE, thickness=2)
|
| 497 |
+
|
| 498 |
+
# Draw mask overlay on display image
|
| 499 |
+
if o_masks:
|
| 500 |
+
# Get the final mask (which is already processed as pos_mask - neg_mask)
|
| 501 |
+
mask = o_masks[0][0] # Get first mask
|
| 502 |
+
|
| 503 |
+
# Create a light blue overlay
|
| 504 |
+
overlay = display_img.copy()
|
| 505 |
+
overlay[mask.squeeze()!=0] = [20, 60, 200] # Light blue in BGR
|
| 506 |
+
# Blend with original image with lower alpha
|
| 507 |
+
display_img = cv2.addWeighted(overlay, 0.6, display_img, 0.4, 0)
|
| 508 |
+
|
| 509 |
+
# Save mask
|
| 510 |
+
if o_masks:
|
| 511 |
+
video_files = glob.glob(os.path.join(temp_dir, "*.mp4"))
|
| 512 |
+
if video_files:
|
| 513 |
+
video_name = get_video_name(video_files[0])
|
| 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 |
+
logger.error(f"Error in backend_select_point: {e}")
|
| 527 |
+
return {"success": False, "error": str(e)}
|
| 528 |
+
|
| 529 |
+
def backend_reset_points(original_img: str, sel_pix: list) -> Dict[str, Any]:
|
| 530 |
+
"""Backend API for resetting points"""
|
| 531 |
+
try:
|
| 532 |
+
# Convert stored image data back to numpy array
|
| 533 |
+
frame_data = json.loads(original_img)
|
| 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 |
+
os.remove(mask_file)
|
| 547 |
+
except Exception as e:
|
| 548 |
+
logger.warning(f"Failed to remove mask file {mask_file}: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
logger.error(f"Error in backend_reset_points: {e}")
|
| 558 |
+
return {"success": False, "error": str(e)}
|
| 559 |
|
| 560 |
+
def backend_run_tracker(grid_size: int, vo_points: int, fps: int, original_image_state: str) -> Dict[str, Any]:
|
| 561 |
+
"""Backend API for running tracker and visualization"""
|
|
|
|
|
|
|
|
|
|
| 562 |
try:
|
| 563 |
+
# Get user's temp directory from stored frame data
|
| 564 |
+
frame_data = json.loads(original_image_state)
|
| 565 |
+
temp_dir = frame_data.get('temp_dir', 'temp')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 566 |
|
| 567 |
+
video_files = glob.glob(os.path.join(temp_dir, "*.mp4"))
|
| 568 |
+
if not video_files:
|
| 569 |
+
return {"success": False, "error": "No video file found"}
|
| 570 |
|
| 571 |
+
video_path = video_files[0]
|
| 572 |
+
video_name = get_video_name(video_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 573 |
|
| 574 |
+
# Run tracker
|
| 575 |
+
npz_path, track2d_video = gpu_run_tracker(temp_dir, video_name, grid_size, vo_points, fps)
|
|
|
|
|
|
|
| 576 |
|
| 577 |
+
# Generate HTML content
|
| 578 |
+
html_content = process_point_cloud_data(npz_path)
|
| 579 |
|
| 580 |
+
# Schedule deletion of generated files
|
| 581 |
+
if os.path.exists(track2d_video):
|
| 582 |
+
delete_later(track2d_video, delay=600)
|
| 583 |
+
if os.path.exists(npz_path):
|
| 584 |
+
delete_later(npz_path, delay=600)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 585 |
|
| 586 |
+
return {
|
| 587 |
+
"success": True,
|
| 588 |
+
"viz_html": html_content,
|
| 589 |
+
"track_video_path": track2d_video
|
| 590 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 591 |
|
| 592 |
except Exception as e:
|
| 593 |
+
logger.error(f"Error in backend_run_tracker: {e}")
|
| 594 |
+
return {"success": False, "error": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 595 |
|
| 596 |
+
# Remove the separate interfaces and create a unified API handler
|
| 597 |
+
def unified_api_handler(function_type: str, *args) -> Dict[str, Any]:
|
| 598 |
+
"""Unified API handler for all backend functions"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 599 |
try:
|
| 600 |
+
if function_type == "upload_video":
|
| 601 |
+
# args[0] should be the video file
|
| 602 |
+
return backend_upload_video(args[0])
|
| 603 |
+
elif function_type == "select_point":
|
| 604 |
+
# args: original_img, sel_pix, point_type, point_x, point_y
|
| 605 |
+
return backend_select_point(args[0], args[1], args[2], args[3], args[4])
|
| 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 |
+
return {"success": False, "error": f"Unknown function type: {function_type}"}
|
|
|
|
| 614 |
except Exception as e:
|
| 615 |
+
logger.error(f"Error in unified_api_handler: {e}")
|
| 616 |
+
return {"success": False, "error": str(e)}
|
| 617 |
|
| 618 |
+
# Create the main unified API interface
|
| 619 |
+
main_api = gr.Interface(
|
| 620 |
+
fn=unified_api_handler,
|
| 621 |
+
inputs=[
|
| 622 |
+
gr.Dropdown(
|
| 623 |
+
choices=["upload_video", "select_point", "reset_points", "run_tracker"],
|
| 624 |
+
label="Function Type",
|
| 625 |
+
value="upload_video"
|
| 626 |
+
),
|
| 627 |
+
gr.File(label="Video File (for upload_video)", file_types=[".mp4", ".avi", ".mov"]),
|
| 628 |
+
gr.Textbox(label="Original Image State", value=""),
|
| 629 |
+
gr.JSON(label="Selected Points", value=[]),
|
| 630 |
+
gr.Radio(choices=['positive_point', 'negative_point'], label="Point Type", value='positive_point'),
|
| 631 |
+
gr.Number(label="Point X", value=0),
|
| 632 |
+
gr.Number(label="Point Y", value=0),
|
| 633 |
+
gr.Number(label="Grid Size", value=50),
|
| 634 |
+
gr.Number(label="VO Points", value=756),
|
| 635 |
+
gr.Number(label="FPS", value=3)
|
| 636 |
+
],
|
| 637 |
+
outputs=[
|
| 638 |
+
gr.JSON(label="Result")
|
| 639 |
+
],
|
| 640 |
+
title="SpaTrackV2 Backend API",
|
| 641 |
+
description="Unified Backend API for SpaTrackV2. This is a private Space that provides core functionality.",
|
| 642 |
+
api_name="unified_api"
|
| 643 |
+
)
|
| 644 |
|
| 645 |
+
# Create additional interfaces for individual API functions for manual testing
|
| 646 |
+
select_point_api = gr.Interface(
|
| 647 |
+
fn=backend_select_point,
|
| 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 |
+
reset_points_api = gr.Interface(
|
| 663 |
+
fn=backend_reset_points,
|
| 664 |
+
inputs=[
|
| 665 |
+
gr.Textbox(label="Original Image State"),
|
| 666 |
+
gr.JSON(label="Selected Points")
|
| 667 |
+
],
|
| 668 |
+
outputs=[
|
| 669 |
+
gr.JSON(label="Result")
|
| 670 |
+
],
|
| 671 |
+
title="Reset Points API",
|
| 672 |
+
description="API for resetting points"
|
| 673 |
+
)
|
| 674 |
|
| 675 |
+
tracker_api = gr.Interface(
|
| 676 |
+
fn=backend_run_tracker,
|
| 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 |
+
# Create a combined interface with tabs for manual testing
|
| 691 |
+
with gr.Blocks(title="SpaTrackV2 Backend API") as backend_app:
|
| 692 |
+
gr.Markdown("# 🚀 SpaTrackV2 Backend API")
|
| 693 |
+
gr.Markdown("This is a private backend Space that provides core SpaTrackV2 functionality.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 694 |
|
| 695 |
+
with gr.Tabs():
|
| 696 |
+
with gr.TabItem("Unified API"):
|
| 697 |
+
main_api.render()
|
| 698 |
+
|
| 699 |
+
with gr.TabItem("Upload Video"):
|
| 700 |
+
upload_api = gr.Interface(
|
| 701 |
+
fn=backend_upload_video,
|
| 702 |
+
inputs=[gr.File(label="Upload Video", file_types=[".mp4", ".avi", ".mov"])],
|
| 703 |
+
outputs=[gr.JSON(label="Result")],
|
| 704 |
+
title="Upload Video API"
|
| 705 |
+
)
|
| 706 |
+
upload_api.render()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 707 |
|
| 708 |
+
with gr.TabItem("Select Point"):
|
| 709 |
+
select_point_api.render()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 710 |
|
| 711 |
+
with gr.TabItem("Reset Points"):
|
| 712 |
+
reset_points_api.render()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 713 |
|
| 714 |
+
with gr.TabItem("Run Tracker"):
|
| 715 |
+
tracker_api.render()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 716 |
|
| 717 |
+
with gr.TabItem("API Info"):
|
| 718 |
+
gr.Markdown("""
|
| 719 |
+
## Available API Functions
|
| 720 |
+
|
| 721 |
+
### Unified API
|
| 722 |
+
- **Function**: `unified_api_handler`
|
| 723 |
+
- **Input**: Function type + parameters
|
| 724 |
+
- **Output**: JSON result
|
| 725 |
+
|
| 726 |
+
### Individual Functions
|
| 727 |
+
|
| 728 |
+
#### 1. Upload Video
|
| 729 |
+
- **Function**: `backend_upload_video`
|
| 730 |
+
- **Input**: Video file
|
| 731 |
+
- **Output**: Initial state and settings
|
| 732 |
+
|
| 733 |
+
#### 2. Select Point
|
| 734 |
+
- **Function**: `backend_select_point`
|
| 735 |
+
- **Input**: Image state + point coordinates
|
| 736 |
+
- **Output**: Updated image and points
|
| 737 |
+
|
| 738 |
+
#### 3. Reset Points
|
| 739 |
+
- **Function**: `backend_reset_points`
|
| 740 |
+
- **Input**: Image state + points
|
| 741 |
+
- **Output**: Reset image and empty points
|
| 742 |
+
|
| 743 |
+
#### 4. Run Tracker
|
| 744 |
+
- **Function**: `backend_run_tracker`
|
| 745 |
+
- **Input**: Parameters + image state
|
| 746 |
+
- **Output**: Visualization and tracking results
|
| 747 |
+
|
| 748 |
+
### 5. GPU Functions
|
| 749 |
+
- `gpu_run_sam(image, points, boxes)`: GPU-accelerated SAM inference
|
| 750 |
+
- `gpu_run_tracker(temp_dir, video_name, grid_size, vo_points, fps)`: GPU-accelerated tracking
|
| 751 |
+
""")
|
| 752 |
+
|
| 753 |
+
if __name__ == "__main__":
|
| 754 |
+
# Print startup information
|
| 755 |
+
print("🚀 Starting SpaTrackV2 Backend Space...")
|
| 756 |
+
print(f"🔧 Python version: {sys.version}")
|
| 757 |
+
print(f"🔧 Working directory: {os.getcwd()}")
|
| 758 |
+
print(f"🔧 GPU available: {torch.cuda.is_available()}")
|
| 759 |
+
if torch.cuda.is_available():
|
| 760 |
+
print(f"🔧 GPU device: {torch.cuda.get_device_name(0)}")
|
| 761 |
+
print(f"🔧 GPU memory: {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f} GB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 762 |
|
| 763 |
+
print(f"🔧 Initializing models and GPU resources...")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 764 |
|
| 765 |
+
# Initialize global models
|
| 766 |
+
init_success = init_global_models()
|
| 767 |
+
if init_success:
|
| 768 |
+
print("✅ Backend initialization complete!")
|
| 769 |
+
else:
|
| 770 |
+
print("❌ Backend initialization failed! Continuing with limited functionality...")
|
| 771 |
|
| 772 |
+
print("📡 Starting Gradio backend interface...")
|
| 773 |
+
print(f"🔧 Available GPU functions: {[name for name in globals() if name.startswith('gpu_')]}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 774 |
|
| 775 |
+
# Launch the complete backend app (not just main_api)
|
| 776 |
+
backend_app.launch(
|
| 777 |
server_name="0.0.0.0",
|
| 778 |
server_port=7860,
|
| 779 |
+
share=False, # Backend shouldn't need sharing
|
| 780 |
debug=True,
|
| 781 |
show_error=True
|
| 782 |
)
|