Spaces:
Running
on
Zero
Running
on
Zero
xiaoyuxi
commited on
Commit
·
488d6c3
1
Parent(s):
416aa34
backend
Browse files- app.py +351 -365
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -12,46 +12,23 @@ hf_token = os.getenv("HF_TOKEN") # Replace with your actual Hugging Face token
|
|
| 12 |
|
| 13 |
# Flag to track if backend is available
|
| 14 |
BACKEND_AVAILABLE = False
|
| 15 |
-
|
| 16 |
|
| 17 |
def initialize_backend():
|
| 18 |
-
"""Initialize backend connection"""
|
| 19 |
-
global
|
| 20 |
try:
|
| 21 |
print(f"Attempting to connect to backend: {BACKEND_SPACE_URL}")
|
| 22 |
-
backend_api = gr.load(f"spaces/{BACKEND_SPACE_URL}", token=hf_token)
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
print(f"🔧 predict type: {type(backend_api.predict)}")
|
| 31 |
-
print(f"🔧 predict content: {backend_api.predict}")
|
| 32 |
-
if hasattr(backend_api.predict, '__call__'):
|
| 33 |
-
print("✅ predict is callable")
|
| 34 |
-
else:
|
| 35 |
-
print("❌ predict is not callable")
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
print(f"🔧 fns content: {backend_api.fns}")
|
| 40 |
-
|
| 41 |
-
if hasattr(backend_api, 'call_function'):
|
| 42 |
-
print(f"🔧 call_function type: {type(backend_api.call_function)}")
|
| 43 |
-
|
| 44 |
-
# Check if it's a Blocks object and has fns
|
| 45 |
-
if hasattr(backend_api, 'fns') and backend_api.fns:
|
| 46 |
-
print(f"✅ Backend connection successful!")
|
| 47 |
-
print(f"🔧 Available functions in fns: {list(backend_api.fns.keys())}")
|
| 48 |
-
BACKEND_AVAILABLE = True
|
| 49 |
-
return True
|
| 50 |
-
else:
|
| 51 |
-
print("❌ Backend API functions not found")
|
| 52 |
-
print(f"🔧 Available methods: {[attr for attr in dir(backend_api) if not attr.startswith('_')]}")
|
| 53 |
-
BACKEND_AVAILABLE = False
|
| 54 |
-
return False
|
| 55 |
|
| 56 |
except Exception as e:
|
| 57 |
print(f"❌ Backend connection failed: {e}")
|
|
@@ -112,14 +89,25 @@ def handle_video_upload(video):
|
|
| 112 |
return None, None, [], 50, 756, 3
|
| 113 |
|
| 114 |
try:
|
| 115 |
-
if BACKEND_AVAILABLE and
|
| 116 |
# Try to use backend API
|
| 117 |
try:
|
| 118 |
print("🔧 Calling backend API for video upload...")
|
| 119 |
|
| 120 |
-
# Call
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
print(f"✅ Backend video upload API call successful!")
|
| 125 |
print(f"🔧 Result type: {type(result)}")
|
|
@@ -127,10 +115,16 @@ def handle_video_upload(video):
|
|
| 127 |
|
| 128 |
# Parse the result - expect a dict with success status
|
| 129 |
if isinstance(result, dict) and result.get("success"):
|
| 130 |
-
# Extract
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
else:
|
| 135 |
print("Backend processing failed, using local fallback")
|
| 136 |
# Fallback to local processing
|
|
@@ -150,8 +144,9 @@ def handle_video_upload(video):
|
|
| 150 |
"frame": "local_processing"
|
| 151 |
})
|
| 152 |
|
| 153 |
-
#
|
| 154 |
-
|
|
|
|
| 155 |
|
| 156 |
return original_image_state, display_image, [], grid_size_val, vo_points_val, fps_val
|
| 157 |
|
|
@@ -165,39 +160,37 @@ def select_point(original_img: str, sel_pix: list, point_type: str, evt: gr.Sele
|
|
| 165 |
return None, []
|
| 166 |
|
| 167 |
try:
|
| 168 |
-
if BACKEND_AVAILABLE and
|
| 169 |
# Try to use backend API
|
| 170 |
try:
|
| 171 |
print(f"🔧 Calling backend select point API: x={evt.index[0]}, y={evt.index[1]}, type={point_type}")
|
| 172 |
|
| 173 |
-
#
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
# Parse the result - expect a dict with success status
|
| 191 |
-
if isinstance(result, dict) and result.get("success"):
|
| 192 |
-
# For now, use local processing for visualization
|
| 193 |
-
# Fallback to local processing
|
| 194 |
-
pass
|
| 195 |
-
else:
|
| 196 |
-
print("Backend processing failed, using local fallback")
|
| 197 |
-
# Fallback to local processing
|
| 198 |
-
pass
|
| 199 |
else:
|
| 200 |
-
print("
|
| 201 |
# Fallback to local processing
|
| 202 |
pass
|
| 203 |
except Exception as e:
|
|
@@ -223,74 +216,58 @@ def select_point(original_img: str, sel_pix: list, point_type: str, evt: gr.Sele
|
|
| 223 |
x, y = evt.index[0], evt.index[1]
|
| 224 |
color = (0, 255, 0) if point_type == 'positive_point' else (255, 0, 0)
|
| 225 |
|
| 226 |
-
# Draw a more visible point
|
| 227 |
cv2.circle(display_image, (x, y), 8, color, -1)
|
| 228 |
-
cv2.circle(display_image, (x, y),
|
| 229 |
-
|
| 230 |
-
# Add a simple mask-like visualization (circle around the point)
|
| 231 |
-
mask_radius = 30
|
| 232 |
-
mask_color = (0, 255, 0, 50) if point_type == 'positive_point' else (255, 0, 0, 50)
|
| 233 |
-
|
| 234 |
-
# Create a simple mask visualization
|
| 235 |
-
mask_overlay = display_image.copy()
|
| 236 |
-
cv2.circle(mask_overlay, (x, y), mask_radius, color, -1)
|
| 237 |
-
|
| 238 |
-
# Blend the mask with the original image
|
| 239 |
-
alpha = 0.3
|
| 240 |
-
display_image = cv2.addWeighted(display_image, 1-alpha, mask_overlay, alpha, 0)
|
| 241 |
|
| 242 |
-
#
|
| 243 |
-
new_sel_pix = sel_pix
|
| 244 |
-
|
| 245 |
-
# Add text label
|
| 246 |
-
label = "Positive" if point_type == 'positive_point' else "Negative"
|
| 247 |
-
cv2.putText(display_image, label, (x+15, y-15), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
|
| 248 |
|
| 249 |
return display_image, new_sel_pix
|
| 250 |
|
| 251 |
-
return None,
|
| 252 |
|
| 253 |
except Exception as e:
|
| 254 |
print(f"Error in select_point: {e}")
|
| 255 |
-
return None,
|
| 256 |
|
| 257 |
def reset_points(original_img: str, sel_pix):
|
| 258 |
-
"""Reset
|
| 259 |
if original_img is None:
|
| 260 |
return None, []
|
| 261 |
|
| 262 |
try:
|
| 263 |
-
if BACKEND_AVAILABLE and
|
| 264 |
# Try to use backend API
|
| 265 |
try:
|
| 266 |
-
print("🔧 Calling backend reset points API")
|
| 267 |
|
| 268 |
-
#
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
# Parse the result - expect a dict with success status
|
| 284 |
-
if isinstance(result, dict) and result.get("success"):
|
| 285 |
-
# For now, use local processing for visualization
|
| 286 |
-
# Fallback to local processing
|
| 287 |
-
pass
|
| 288 |
-
else:
|
| 289 |
-
print("Backend processing failed, using local fallback")
|
| 290 |
-
# Fallback to local processing
|
| 291 |
-
pass
|
| 292 |
else:
|
| 293 |
-
print("
|
| 294 |
# Fallback to local processing
|
| 295 |
pass
|
| 296 |
except Exception as e:
|
|
@@ -299,7 +276,7 @@ def reset_points(original_img: str, sel_pix):
|
|
| 299 |
pass
|
| 300 |
|
| 301 |
# Fallback: local processing
|
| 302 |
-
print("Using local
|
| 303 |
|
| 304 |
# Parse original image state
|
| 305 |
try:
|
|
@@ -309,7 +286,7 @@ def reset_points(original_img: str, sel_pix):
|
|
| 309 |
video_path = None
|
| 310 |
|
| 311 |
if video_path:
|
| 312 |
-
# Re-extract frame
|
| 313 |
display_image = extract_first_frame(video_path)
|
| 314 |
return display_image, []
|
| 315 |
|
|
@@ -325,41 +302,39 @@ def launch_viz(grid_size, vo_points, fps, original_image_state):
|
|
| 325 |
return None, None
|
| 326 |
|
| 327 |
try:
|
| 328 |
-
if BACKEND_AVAILABLE and
|
| 329 |
# Try to use backend API
|
| 330 |
try:
|
| 331 |
print(f"🔧 Calling backend API with parameters: grid_size={grid_size}, vo_points={vo_points}, fps={fps}")
|
| 332 |
print(f"🔧 Original image state type: {type(original_image_state)}")
|
| 333 |
print(f"🔧 Original image state preview: {str(original_image_state)[:100]}...")
|
| 334 |
|
| 335 |
-
#
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
# Parse the result
|
| 353 |
-
if isinstance(result, dict) and result.get("success"):
|
| 354 |
-
viz_html = result.get("viz_html_path", "")
|
| 355 |
-
track_video_path = result.get("track_video_path", "")
|
| 356 |
-
return viz_html, track_video_path
|
| 357 |
-
else:
|
| 358 |
-
print("Backend processing failed, showing error message")
|
| 359 |
-
# Fallback to error message
|
| 360 |
-
pass
|
| 361 |
else:
|
| 362 |
-
print("
|
| 363 |
# Fallback to error message
|
| 364 |
pass
|
| 365 |
except Exception as e:
|
|
@@ -385,10 +360,9 @@ def launch_viz(grid_size, vo_points, fps, original_image_state):
|
|
| 385 |
<div style='background-color: #f8f9fa; border-radius: 5px; padding: 10px; margin-top: 10px;'>
|
| 386 |
<p style='color: #2d3436; font-weight: bold; margin: 0 0 5px 0;'>Debug Information:</p>
|
| 387 |
<p style='color: #666; font-size: 12px; margin: 0;'>Backend Available: {BACKEND_AVAILABLE}</p>
|
| 388 |
-
<p style='color: #666; font-size: 12px; margin: 0;'>Backend
|
| 389 |
<p style='color: #666; font-size: 12px; margin: 0;'>Backend URL: {BACKEND_SPACE_URL}</p>
|
| 390 |
-
<p style='color: #666; font-size: 12px; margin: 0;'>
|
| 391 |
-
<p style='color: #666; font-size: 12px; margin: 0;'>Available Functions: {list(backend_api.fns.keys()) if backend_api and hasattr(backend_api, 'fns') else 'None'}</p>
|
| 392 |
</div>
|
| 393 |
<p style='color: #2d3436; font-weight: bold; margin-top: 15px;'>
|
| 394 |
Current Status: Backend unavailable - Running in limited mode
|
|
@@ -403,48 +377,72 @@ def launch_viz(grid_size, vo_points, fps, original_image_state):
|
|
| 403 |
|
| 404 |
def clear_all():
|
| 405 |
"""Clear all buffers and temporary files"""
|
| 406 |
-
return None, None, []
|
| 407 |
|
| 408 |
-
def update_tracker_model(
|
| 409 |
-
|
|
|
|
| 410 |
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 419 |
|
| 420 |
-
return
|
| 421 |
|
| 422 |
def test_backend_connection():
|
| 423 |
"""Test if backend is actually working"""
|
| 424 |
global BACKEND_AVAILABLE
|
| 425 |
-
if not
|
| 426 |
return False
|
| 427 |
|
| 428 |
try:
|
| 429 |
-
# Try a simple API call to test connection
|
| 430 |
print("Testing backend connection with a simple call...")
|
| 431 |
# Check if we have fns available
|
| 432 |
-
if hasattr(
|
| 433 |
print("✅ Backend API functions are available")
|
| 434 |
-
print(f"🔧 Available function indices: {list(
|
| 435 |
return True
|
| 436 |
else:
|
| 437 |
print("❌ Backend API functions not found")
|
| 438 |
-
BACKEND_AVAILABLE = False
|
| 439 |
return False
|
| 440 |
except Exception as e:
|
| 441 |
print(f"❌ Backend connection test failed: {e}")
|
| 442 |
-
BACKEND_AVAILABLE = False
|
| 443 |
return False
|
| 444 |
|
| 445 |
def test_backend_api():
|
| 446 |
"""Test specific backend API functions"""
|
| 447 |
-
if not BACKEND_AVAILABLE or not
|
| 448 |
print("❌ Backend not available for testing")
|
| 449 |
return False
|
| 450 |
|
|
@@ -452,226 +450,214 @@ def test_backend_api():
|
|
| 452 |
print("🧪 Testing backend API functions...")
|
| 453 |
|
| 454 |
# Test if fns exist and show available indices
|
| 455 |
-
if hasattr(
|
| 456 |
-
print(f"✅ Backend has {len(
|
| 457 |
-
for idx in
|
| 458 |
print(f"✅ Function {idx} is available")
|
| 459 |
else:
|
| 460 |
-
print("❌ No functions
|
|
|
|
| 461 |
|
| 462 |
return True
|
|
|
|
| 463 |
except Exception as e:
|
| 464 |
print(f"❌ Backend API test failed: {e}")
|
| 465 |
return False
|
| 466 |
|
| 467 |
-
# Initialize backend connection
|
| 468 |
-
print("
|
| 469 |
initialize_backend()
|
| 470 |
|
| 471 |
-
# Test
|
| 472 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 473 |
|
| 474 |
-
#
|
| 475 |
-
|
| 476 |
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
}
|
| 485 |
-
#advanced_settings .gr-slider .gr-label {
|
| 486 |
-
font-size: 13px !important;
|
| 487 |
-
margin-bottom: 5px !important;
|
| 488 |
-
}
|
| 489 |
-
#advanced_settings .gr-slider .gr-info {
|
| 490 |
-
font-size: 12px !important;
|
| 491 |
-
}
|
| 492 |
-
#point_label_radio .gr-radio-group {
|
| 493 |
-
flex-direction: row !important;
|
| 494 |
-
gap: 15px !important;
|
| 495 |
-
}
|
| 496 |
-
#point_label_radio .gr-radio-group label {
|
| 497 |
-
margin-right: 0 !important;
|
| 498 |
-
margin-bottom: 0 !important;
|
| 499 |
-
}
|
| 500 |
-
/* Style for example videos label */
|
| 501 |
-
.gr-examples .gr-label {
|
| 502 |
-
font-weight: bold !important;
|
| 503 |
-
font-size: 16px !important;
|
| 504 |
-
}
|
| 505 |
-
/* Simple horizontal scroll for examples */
|
| 506 |
-
.gr-examples .gr-table-wrapper {
|
| 507 |
-
overflow-x: auto !important;
|
| 508 |
-
overflow-y: hidden !important;
|
| 509 |
-
}
|
| 510 |
-
.gr-examples .gr-table {
|
| 511 |
-
display: flex !important;
|
| 512 |
-
flex-wrap: nowrap !important;
|
| 513 |
-
min-width: max-content !important;
|
| 514 |
}
|
| 515 |
-
.gr-
|
| 516 |
-
|
| 517 |
-
flex-direction: row !important;
|
| 518 |
-
flex-wrap: nowrap !important;
|
| 519 |
}
|
| 520 |
-
.gr-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
}
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 534 |
|
| 535 |
with gr.Row():
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 562 |
|
| 563 |
-
<div style='background-color: {status_color}20; border: 2px solid {status_color}; border-radius: 8px; padding: 10px; margin-top: 15px;'>
|
| 564 |
-
<p style='font-size: 18px; color: {status_color}; margin: 0;'>
|
| 565 |
-
{status_icon} Backend Status: {status_text}
|
| 566 |
-
</p>
|
| 567 |
-
<p style='font-size: 14px; color: #666; margin: 5px 0 0 0;'>
|
| 568 |
-
{BACKEND_SPACE_URL}
|
| 569 |
-
</p>
|
| 570 |
-
<p style='font-size: 12px; color: #888; margin: 5px 0 0 0;'>
|
| 571 |
-
{'API methods available' if BACKEND_AVAILABLE else 'Connection failed - using local mode'}
|
| 572 |
-
</p>
|
| 573 |
-
</div>
|
| 574 |
-
</div>
|
| 575 |
-
""")
|
| 576 |
-
|
| 577 |
-
with gr.Row():
|
| 578 |
with gr.Column(scale=1):
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
# Add example videos using gr.Examples
|
| 606 |
-
examples_component = gr.Examples(
|
| 607 |
-
examples=[
|
| 608 |
-
"examples/kiss.mp4",
|
| 609 |
-
"examples/backpack.mp4",
|
| 610 |
-
"examples/pillow.mp4",
|
| 611 |
-
"examples/hockey.mp4",
|
| 612 |
-
"examples/drifting.mp4",
|
| 613 |
-
"examples/ken_block_0.mp4",
|
| 614 |
-
"examples/ball.mp4",
|
| 615 |
-
"examples/kitchen.mp4",
|
| 616 |
-
"examples/ego_teaser.mp4",
|
| 617 |
-
"examples/ego_kc1.mp4",
|
| 618 |
-
"examples/vertical_place.mp4",
|
| 619 |
-
"examples/robot_unitree.mp4",
|
| 620 |
-
"examples/droid_robot.mp4",
|
| 621 |
-
"examples/robot_2.mp4",
|
| 622 |
-
"examples/cinema_0.mp4",
|
| 623 |
-
],
|
| 624 |
-
inputs=[video_input],
|
| 625 |
-
label="📁 Example Videos",
|
| 626 |
-
examples_per_page=20 # Show all examples on one page to enable scrolling
|
| 627 |
)
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 648 |
video_input.change(
|
| 649 |
-
|
| 650 |
-
inputs=[video_input],
|
| 651 |
-
outputs=[original_image_state,
|
| 652 |
)
|
| 653 |
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
|
|
|
|
|
|
| 657 |
|
| 658 |
-
|
| 659 |
-
|
|
|
|
|
|
|
|
|
|
| 660 |
|
| 661 |
-
|
| 662 |
-
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
# Update tracker model when vo_points changes
|
| 666 |
-
vo_points.change(update_tracker_model,
|
| 667 |
-
inputs=[vo_points],
|
| 668 |
-
outputs=[])
|
| 669 |
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
|
|
|
| 674 |
|
| 675 |
-
# Launch the
|
| 676 |
if __name__ == "__main__":
|
| 677 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Flag to track if backend is available
|
| 14 |
BACKEND_AVAILABLE = False
|
| 15 |
+
backend_client = None
|
| 16 |
|
| 17 |
def initialize_backend():
|
| 18 |
+
"""Initialize backend connection using gradio_client"""
|
| 19 |
+
global backend_client, BACKEND_AVAILABLE
|
| 20 |
try:
|
| 21 |
print(f"Attempting to connect to backend: {BACKEND_SPACE_URL}")
|
|
|
|
| 22 |
|
| 23 |
+
# Use gradio_client for proper API access
|
| 24 |
+
from gradio_client import Client
|
| 25 |
+
backend_client = Client(f"https://huggingface.co/spaces/{BACKEND_SPACE_URL}", hf_token=hf_token)
|
| 26 |
|
| 27 |
+
print(f"✅ Backend connection successful!")
|
| 28 |
+
print(f"🔧 Backend client: {backend_client}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
BACKEND_AVAILABLE = True
|
| 31 |
+
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
except Exception as e:
|
| 34 |
print(f"❌ Backend connection failed: {e}")
|
|
|
|
| 89 |
return None, None, [], 50, 756, 3
|
| 90 |
|
| 91 |
try:
|
| 92 |
+
if BACKEND_AVAILABLE and backend_client:
|
| 93 |
# Try to use backend API
|
| 94 |
try:
|
| 95 |
print("🔧 Calling backend API for video upload...")
|
| 96 |
|
| 97 |
+
# Call the unified API with upload_video function type
|
| 98 |
+
result = backend_client.predict(
|
| 99 |
+
"upload_video", # function_type
|
| 100 |
+
video, # video file
|
| 101 |
+
"", # original_image_state (not used for upload)
|
| 102 |
+
[], # selected_points (not used for upload)
|
| 103 |
+
"positive_point", # point_type (not used for upload)
|
| 104 |
+
0, # point_x (not used for upload)
|
| 105 |
+
0, # point_y (not used for upload)
|
| 106 |
+
50, # grid_size (not used for upload)
|
| 107 |
+
756, # vo_points (not used for upload)
|
| 108 |
+
3, # fps (not used for upload)
|
| 109 |
+
api_name="/predict"
|
| 110 |
+
)
|
| 111 |
|
| 112 |
print(f"✅ Backend video upload API call successful!")
|
| 113 |
print(f"🔧 Result type: {type(result)}")
|
|
|
|
| 115 |
|
| 116 |
# Parse the result - expect a dict with success status
|
| 117 |
if isinstance(result, dict) and result.get("success"):
|
| 118 |
+
# Extract data from backend response
|
| 119 |
+
original_image_state = result.get("original_image_state", "")
|
| 120 |
+
display_image = result.get("display_image", None)
|
| 121 |
+
selected_points = result.get("selected_points", [])
|
| 122 |
+
|
| 123 |
+
# Get video settings based on video name
|
| 124 |
+
video_name = get_video_name(video)
|
| 125 |
+
grid_size_val, vo_points_val, fps_val = get_video_settings(video_name)
|
| 126 |
+
|
| 127 |
+
return original_image_state, display_image, selected_points, grid_size_val, vo_points_val, fps_val
|
| 128 |
else:
|
| 129 |
print("Backend processing failed, using local fallback")
|
| 130 |
# Fallback to local processing
|
|
|
|
| 144 |
"frame": "local_processing"
|
| 145 |
})
|
| 146 |
|
| 147 |
+
# Get video settings
|
| 148 |
+
video_name = get_video_name(video)
|
| 149 |
+
grid_size_val, vo_points_val, fps_val = get_video_settings(video_name)
|
| 150 |
|
| 151 |
return original_image_state, display_image, [], grid_size_val, vo_points_val, fps_val
|
| 152 |
|
|
|
|
| 160 |
return None, []
|
| 161 |
|
| 162 |
try:
|
| 163 |
+
if BACKEND_AVAILABLE and backend_client:
|
| 164 |
# Try to use backend API
|
| 165 |
try:
|
| 166 |
print(f"🔧 Calling backend select point API: x={evt.index[0]}, y={evt.index[1]}, type={point_type}")
|
| 167 |
|
| 168 |
+
# Call the unified API with select_point function type
|
| 169 |
+
result = backend_client.predict(
|
| 170 |
+
"select_point", # function_type
|
| 171 |
+
None, # video file (not used for select_point)
|
| 172 |
+
original_img, # original_image_state
|
| 173 |
+
sel_pix, # selected_points
|
| 174 |
+
point_type, # point_type
|
| 175 |
+
evt.index[0], # point_x
|
| 176 |
+
evt.index[1], # point_y
|
| 177 |
+
50, # grid_size (not used for select_point)
|
| 178 |
+
756, # vo_points (not used for select_point)
|
| 179 |
+
3, # fps (not used for select_point)
|
| 180 |
+
api_name="/predict"
|
| 181 |
+
)
|
| 182 |
|
| 183 |
+
print(f"✅ Backend select point API call successful!")
|
| 184 |
+
print(f"🔧 Result type: {type(result)}")
|
| 185 |
+
print(f"🔧 Result: {result}")
|
| 186 |
+
|
| 187 |
+
# Parse the result - expect a dict with success status
|
| 188 |
+
if isinstance(result, dict) and result.get("success"):
|
| 189 |
+
display_image = result.get("display_image", None)
|
| 190 |
+
new_sel_pix = result.get("selected_points", sel_pix)
|
| 191 |
+
return display_image, new_sel_pix
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
else:
|
| 193 |
+
print("Backend processing failed, using local fallback")
|
| 194 |
# Fallback to local processing
|
| 195 |
pass
|
| 196 |
except Exception as e:
|
|
|
|
| 216 |
x, y = evt.index[0], evt.index[1]
|
| 217 |
color = (0, 255, 0) if point_type == 'positive_point' else (255, 0, 0)
|
| 218 |
|
| 219 |
+
# Draw a larger, more visible point
|
| 220 |
cv2.circle(display_image, (x, y), 8, color, -1)
|
| 221 |
+
cv2.circle(display_image, (x, y), 12, (255, 255, 255), 2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
+
# Add point to selected points list
|
| 224 |
+
new_sel_pix = sel_pix.copy()
|
| 225 |
+
new_sel_pix.append([x, y, point_type])
|
|
|
|
|
|
|
|
|
|
| 226 |
|
| 227 |
return display_image, new_sel_pix
|
| 228 |
|
| 229 |
+
return None, []
|
| 230 |
|
| 231 |
except Exception as e:
|
| 232 |
print(f"Error in select_point: {e}")
|
| 233 |
+
return None, []
|
| 234 |
|
| 235 |
def reset_points(original_img: str, sel_pix):
|
| 236 |
+
"""Reset points and restore original image"""
|
| 237 |
if original_img is None:
|
| 238 |
return None, []
|
| 239 |
|
| 240 |
try:
|
| 241 |
+
if BACKEND_AVAILABLE and backend_client:
|
| 242 |
# Try to use backend API
|
| 243 |
try:
|
| 244 |
+
print("🔧 Calling backend reset points API...")
|
| 245 |
|
| 246 |
+
# Call the unified API with reset_points function type
|
| 247 |
+
result = backend_client.predict(
|
| 248 |
+
"reset_points", # function_type
|
| 249 |
+
None, # video file (not used for reset_points)
|
| 250 |
+
original_img, # original_image_state
|
| 251 |
+
sel_pix, # selected_points
|
| 252 |
+
"positive_point", # point_type (not used for reset_points)
|
| 253 |
+
0, # point_x (not used for reset_points)
|
| 254 |
+
0, # point_y (not used for reset_points)
|
| 255 |
+
50, # grid_size (not used for reset_points)
|
| 256 |
+
756, # vo_points (not used for reset_points)
|
| 257 |
+
3, # fps (not used for reset_points)
|
| 258 |
+
api_name="/predict"
|
| 259 |
+
)
|
| 260 |
|
| 261 |
+
print(f"✅ Backend reset points API call successful!")
|
| 262 |
+
print(f"🔧 Result: {result}")
|
| 263 |
+
|
| 264 |
+
# Parse the result
|
| 265 |
+
if isinstance(result, dict) and result.get("success"):
|
| 266 |
+
display_image = result.get("display_image", None)
|
| 267 |
+
new_sel_pix = result.get("selected_points", [])
|
| 268 |
+
return display_image, new_sel_pix
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
else:
|
| 270 |
+
print("Backend processing failed, using local fallback")
|
| 271 |
# Fallback to local processing
|
| 272 |
pass
|
| 273 |
except Exception as e:
|
|
|
|
| 276 |
pass
|
| 277 |
|
| 278 |
# Fallback: local processing
|
| 279 |
+
print("Using local reset points...")
|
| 280 |
|
| 281 |
# Parse original image state
|
| 282 |
try:
|
|
|
|
| 286 |
video_path = None
|
| 287 |
|
| 288 |
if video_path:
|
| 289 |
+
# Re-extract original frame
|
| 290 |
display_image = extract_first_frame(video_path)
|
| 291 |
return display_image, []
|
| 292 |
|
|
|
|
| 302 |
return None, None
|
| 303 |
|
| 304 |
try:
|
| 305 |
+
if BACKEND_AVAILABLE and backend_client:
|
| 306 |
# Try to use backend API
|
| 307 |
try:
|
| 308 |
print(f"🔧 Calling backend API with parameters: grid_size={grid_size}, vo_points={vo_points}, fps={fps}")
|
| 309 |
print(f"🔧 Original image state type: {type(original_image_state)}")
|
| 310 |
print(f"🔧 Original image state preview: {str(original_image_state)[:100]}...")
|
| 311 |
|
| 312 |
+
# Call the unified API with run_tracker function type
|
| 313 |
+
result = backend_client.predict(
|
| 314 |
+
"run_tracker", # function_type
|
| 315 |
+
None, # video file (not used for run_tracker)
|
| 316 |
+
original_image_state, # original_image_state
|
| 317 |
+
[], # selected_points (not used for run_tracker)
|
| 318 |
+
"positive_point", # point_type (not used for run_tracker)
|
| 319 |
+
0, # point_x (not used for run_tracker)
|
| 320 |
+
0, # point_y (not used for run_tracker)
|
| 321 |
+
grid_size, # grid_size
|
| 322 |
+
vo_points, # vo_points
|
| 323 |
+
fps, # fps
|
| 324 |
+
api_name="/predict"
|
| 325 |
+
)
|
| 326 |
|
| 327 |
+
print(f"✅ Backend API call successful!")
|
| 328 |
+
print(f"🔧 Result type: {type(result)}")
|
| 329 |
+
print(f"🔧 Result: {result}")
|
| 330 |
+
|
| 331 |
+
# Parse the result
|
| 332 |
+
if isinstance(result, dict) and result.get("success"):
|
| 333 |
+
viz_html = result.get("viz_html", "")
|
| 334 |
+
track_video_path = result.get("track_video_path", "")
|
| 335 |
+
return viz_html, track_video_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
else:
|
| 337 |
+
print("Backend processing failed, showing error message")
|
| 338 |
# Fallback to error message
|
| 339 |
pass
|
| 340 |
except Exception as e:
|
|
|
|
| 360 |
<div style='background-color: #f8f9fa; border-radius: 5px; padding: 10px; margin-top: 10px;'>
|
| 361 |
<p style='color: #2d3436; font-weight: bold; margin: 0 0 5px 0;'>Debug Information:</p>
|
| 362 |
<p style='color: #666; font-size: 12px; margin: 0;'>Backend Available: {BACKEND_AVAILABLE}</p>
|
| 363 |
+
<p style='color: #666; font-size: 12px; margin: 0;'>Backend Client: {backend_client is not None}</p>
|
| 364 |
<p style='color: #666; font-size: 12px; margin: 0;'>Backend URL: {BACKEND_SPACE_URL}</p>
|
| 365 |
+
<p style='color: #666; font-size: 12px; margin: 0;'>Client Type: {type(backend_client) if backend_client else 'None'}</p>
|
|
|
|
| 366 |
</div>
|
| 367 |
<p style='color: #2d3436; font-weight: bold; margin-top: 15px;'>
|
| 368 |
Current Status: Backend unavailable - Running in limited mode
|
|
|
|
| 377 |
|
| 378 |
def clear_all():
|
| 379 |
"""Clear all buffers and temporary files"""
|
| 380 |
+
return None, None, [], 50, 756, 3
|
| 381 |
|
| 382 |
+
def update_tracker_model(model_name):
|
| 383 |
+
"""Update tracker model (placeholder function)"""
|
| 384 |
+
return
|
| 385 |
|
| 386 |
+
def get_video_settings(video_name):
|
| 387 |
+
"""Get video-specific settings based on video name"""
|
| 388 |
+
video_settings = {
|
| 389 |
+
"blackswan": (50, 756, 3),
|
| 390 |
+
"bike-packing": (50, 756, 3),
|
| 391 |
+
"bmx-trees": (50, 756, 3),
|
| 392 |
+
"breakdance": (50, 756, 3),
|
| 393 |
+
"camel": (50, 756, 3),
|
| 394 |
+
"car-roundabout": (50, 756, 3),
|
| 395 |
+
"car-shadow": (50, 756, 3),
|
| 396 |
+
"cows": (50, 756, 3),
|
| 397 |
+
"dance-twirl": (50, 756, 3),
|
| 398 |
+
"dog": (50, 756, 3),
|
| 399 |
+
"dogs-jump": (50, 756, 3),
|
| 400 |
+
"drift-chicane": (50, 756, 3),
|
| 401 |
+
"drift-straight": (50, 756, 3),
|
| 402 |
+
"goat": (50, 756, 3),
|
| 403 |
+
"gold-fish": (50, 756, 3),
|
| 404 |
+
"horsejump-high": (50, 756, 3),
|
| 405 |
+
"india": (50, 756, 3),
|
| 406 |
+
"judo": (50, 756, 3),
|
| 407 |
+
"kite-surf": (50, 756, 3),
|
| 408 |
+
"lab-coat": (50, 756, 3),
|
| 409 |
+
"libby": (50, 756, 3),
|
| 410 |
+
"loading": (50, 756, 3),
|
| 411 |
+
"mbike-trick": (50, 756, 3),
|
| 412 |
+
"motocross-jump": (50, 756, 3),
|
| 413 |
+
"paragliding-launch": (50, 756, 3),
|
| 414 |
+
"parkour": (50, 756, 3),
|
| 415 |
+
"pigs": (50, 756, 3),
|
| 416 |
+
"scooter-black": (50, 756, 3),
|
| 417 |
+
"shooting": (50, 756, 3),
|
| 418 |
+
"soapbox": (50, 756, 3)
|
| 419 |
+
}
|
| 420 |
|
| 421 |
+
return video_settings.get(video_name, (50, 756, 3))
|
| 422 |
|
| 423 |
def test_backend_connection():
|
| 424 |
"""Test if backend is actually working"""
|
| 425 |
global BACKEND_AVAILABLE
|
| 426 |
+
if not backend_client:
|
| 427 |
return False
|
| 428 |
|
| 429 |
try:
|
|
|
|
| 430 |
print("Testing backend connection with a simple call...")
|
| 431 |
# Check if we have fns available
|
| 432 |
+
if hasattr(backend_client, 'fns') and backend_client.fns:
|
| 433 |
print("✅ Backend API functions are available")
|
| 434 |
+
print(f"🔧 Available function indices: {list(backend_client.fns.keys())}")
|
| 435 |
return True
|
| 436 |
else:
|
| 437 |
print("❌ Backend API functions not found")
|
|
|
|
| 438 |
return False
|
| 439 |
except Exception as e:
|
| 440 |
print(f"❌ Backend connection test failed: {e}")
|
|
|
|
| 441 |
return False
|
| 442 |
|
| 443 |
def test_backend_api():
|
| 444 |
"""Test specific backend API functions"""
|
| 445 |
+
if not BACKEND_AVAILABLE or not backend_client:
|
| 446 |
print("❌ Backend not available for testing")
|
| 447 |
return False
|
| 448 |
|
|
|
|
| 450 |
print("🧪 Testing backend API functions...")
|
| 451 |
|
| 452 |
# Test if fns exist and show available indices
|
| 453 |
+
if hasattr(backend_client, 'fns') and backend_client.fns:
|
| 454 |
+
print(f"✅ Backend has {len(backend_client.fns)} functions available")
|
| 455 |
+
for idx in backend_client.fns.keys():
|
| 456 |
print(f"✅ Function {idx} is available")
|
| 457 |
else:
|
| 458 |
+
print("❌ No functions found in backend API")
|
| 459 |
+
return False
|
| 460 |
|
| 461 |
return True
|
| 462 |
+
|
| 463 |
except Exception as e:
|
| 464 |
print(f"❌ Backend API test failed: {e}")
|
| 465 |
return False
|
| 466 |
|
| 467 |
+
# Initialize the backend connection
|
| 468 |
+
print("🚀 Initializing frontend application...")
|
| 469 |
initialize_backend()
|
| 470 |
|
| 471 |
+
# Test backend connection if available
|
| 472 |
+
if BACKEND_AVAILABLE:
|
| 473 |
+
print("🧪 Testing backend connection...")
|
| 474 |
+
test_result = test_backend_connection()
|
| 475 |
+
if test_result:
|
| 476 |
+
print("✅ Backend connection test passed!")
|
| 477 |
+
test_backend_api()
|
| 478 |
+
else:
|
| 479 |
+
print("❌ Backend connection test failed!")
|
| 480 |
+
BACKEND_AVAILABLE = False
|
| 481 |
|
| 482 |
+
# Create the Gradio interface
|
| 483 |
+
print("🎨 Creating Gradio interface...")
|
| 484 |
|
| 485 |
+
with gr.Blocks(
|
| 486 |
+
theme=gr.themes.Soft(),
|
| 487 |
+
title="SpatialTracker V2 - Frontend",
|
| 488 |
+
css="""
|
| 489 |
+
.gradio-container {
|
| 490 |
+
max-width: 1200px !important;
|
| 491 |
+
margin: auto !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 492 |
}
|
| 493 |
+
.gr-button {
|
| 494 |
+
margin: 5px;
|
|
|
|
|
|
|
| 495 |
}
|
| 496 |
+
.gr-form {
|
| 497 |
+
background: white;
|
| 498 |
+
border-radius: 10px;
|
| 499 |
+
padding: 20px;
|
| 500 |
+
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
| 501 |
}
|
| 502 |
+
"""
|
| 503 |
+
) as demo:
|
| 504 |
+
|
| 505 |
+
gr.Markdown("""
|
| 506 |
+
# 🎯 SpatialTracker V2 - Frontend Interface
|
| 507 |
+
|
| 508 |
+
Welcome to SpatialTracker V2! This interface allows you to track objects in videos using advanced computer vision techniques.
|
| 509 |
+
|
| 510 |
+
**Instructions:**
|
| 511 |
+
1. Upload a video file
|
| 512 |
+
2. Click on the object you want to track in the first frame
|
| 513 |
+
3. Adjust tracking parameters if needed
|
| 514 |
+
4. Click "Launch Visualization" to start tracking
|
| 515 |
+
|
| 516 |
+
""")
|
| 517 |
+
|
| 518 |
+
# Status indicator
|
| 519 |
+
status_text = "🟢 Backend Connected" if BACKEND_AVAILABLE else "🟡 Running in Standalone Mode"
|
| 520 |
+
gr.Markdown(f"**Status:** {status_text}")
|
| 521 |
|
| 522 |
with gr.Row():
|
| 523 |
+
with gr.Column(scale=1):
|
| 524 |
+
# Video upload section
|
| 525 |
+
with gr.Group():
|
| 526 |
+
gr.Markdown("### 📹 Video Upload")
|
| 527 |
+
video_input = gr.Video(
|
| 528 |
+
label="Upload Video",
|
| 529 |
+
format="mp4"
|
| 530 |
+
)
|
| 531 |
+
|
| 532 |
+
# Interactive frame display
|
| 533 |
+
with gr.Group():
|
| 534 |
+
gr.Markdown("### ��� Point Selection")
|
| 535 |
+
gr.Markdown("Click on the object you want to track in the frame below:")
|
| 536 |
+
|
| 537 |
+
interactive_frame = gr.Image(
|
| 538 |
+
label="Click to select tracking points",
|
| 539 |
+
type="numpy",
|
| 540 |
+
interactive=True
|
| 541 |
+
)
|
| 542 |
+
|
| 543 |
+
with gr.Row():
|
| 544 |
+
point_type = gr.Radio(
|
| 545 |
+
choices=["positive_point", "negative_point"],
|
| 546 |
+
value="positive_point",
|
| 547 |
+
label="Point Type",
|
| 548 |
+
info="Positive points indicate the object to track, negative points indicate areas to avoid"
|
| 549 |
+
)
|
| 550 |
+
|
| 551 |
+
with gr.Row():
|
| 552 |
+
reset_points_btn = gr.Button("🔄 Reset Points", variant="secondary")
|
| 553 |
+
clear_all_btn = gr.Button("🗑️ Clear All", variant="stop")
|
| 554 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 555 |
with gr.Column(scale=1):
|
| 556 |
+
# Tracking results
|
| 557 |
+
with gr.Group():
|
| 558 |
+
gr.Markdown("### 🎬 Tracking Results")
|
| 559 |
+
tracking_result_video = gr.Video(
|
| 560 |
+
label="Tracking Result Video",
|
| 561 |
+
interactive=False
|
| 562 |
+
)
|
| 563 |
+
|
| 564 |
+
# 3D Visualization
|
| 565 |
+
with gr.Group():
|
| 566 |
+
gr.Markdown("### 🌐 3D Visualization")
|
| 567 |
+
viz_html = gr.HTML(
|
| 568 |
+
label="3D Trajectory Visualization",
|
| 569 |
+
value="<p>Upload a video and select points to see 3D visualization here.</p>"
|
| 570 |
+
)
|
| 571 |
+
|
| 572 |
+
# Advanced settings section
|
| 573 |
+
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
| 574 |
+
with gr.Row():
|
| 575 |
+
grid_size = gr.Slider(
|
| 576 |
+
minimum=10,
|
| 577 |
+
maximum=100,
|
| 578 |
+
step=10,
|
| 579 |
+
value=50,
|
| 580 |
+
label="Grid Size",
|
| 581 |
+
info="Size of the tracking grid"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 582 |
)
|
| 583 |
+
vo_points = gr.Slider(
|
| 584 |
+
minimum=100,
|
| 585 |
+
maximum=2000,
|
| 586 |
+
step=50,
|
| 587 |
+
value=756,
|
| 588 |
+
label="VO Points",
|
| 589 |
+
info="Number of visual odometry points"
|
| 590 |
+
)
|
| 591 |
+
fps = gr.Slider(
|
| 592 |
+
minimum=1,
|
| 593 |
+
maximum=30,
|
| 594 |
+
step=1,
|
| 595 |
+
value=3,
|
| 596 |
+
label="FPS",
|
| 597 |
+
info="Frames per second for processing"
|
| 598 |
+
)
|
| 599 |
+
|
| 600 |
+
# Launch button
|
| 601 |
+
with gr.Row():
|
| 602 |
+
launch_btn = gr.Button("🚀 Launch Visualization", variant="primary", size="lg")
|
| 603 |
+
|
| 604 |
+
# Example videos section
|
| 605 |
+
with gr.Accordion("📂 Example Videos", open=False):
|
| 606 |
+
gr.Examples(
|
| 607 |
+
examples=[
|
| 608 |
+
["examples/blackswan.mp4"],
|
| 609 |
+
["examples/bike-packing.mp4"],
|
| 610 |
+
["examples/bmx-trees.mp4"],
|
| 611 |
+
["examples/breakdance.mp4"],
|
| 612 |
+
["examples/camel.mp4"],
|
| 613 |
+
],
|
| 614 |
+
inputs=video_input,
|
| 615 |
+
label="Try these example videos"
|
| 616 |
+
)
|
| 617 |
+
|
| 618 |
+
# Hidden state variables
|
| 619 |
+
original_image_state = gr.State(None)
|
| 620 |
+
selected_points = gr.State([])
|
| 621 |
+
|
| 622 |
+
# Event handlers
|
| 623 |
video_input.change(
|
| 624 |
+
fn=handle_video_upload,
|
| 625 |
+
inputs=[video_input],
|
| 626 |
+
outputs=[original_image_state, interactive_frame, selected_points, grid_size, vo_points, fps]
|
| 627 |
)
|
| 628 |
|
| 629 |
+
interactive_frame.select(
|
| 630 |
+
fn=select_point,
|
| 631 |
+
inputs=[original_image_state, selected_points, point_type],
|
| 632 |
+
outputs=[interactive_frame, selected_points]
|
| 633 |
+
)
|
| 634 |
|
| 635 |
+
reset_points_btn.click(
|
| 636 |
+
fn=reset_points,
|
| 637 |
+
inputs=[original_image_state, selected_points],
|
| 638 |
+
outputs=[interactive_frame, selected_points]
|
| 639 |
+
)
|
| 640 |
|
| 641 |
+
clear_all_btn.click(
|
| 642 |
+
fn=clear_all,
|
| 643 |
+
outputs=[video_input, interactive_frame, selected_points, grid_size, vo_points, fps]
|
| 644 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 645 |
|
| 646 |
+
launch_btn.click(
|
| 647 |
+
fn=launch_viz,
|
| 648 |
+
inputs=[grid_size, vo_points, fps, original_image_state],
|
| 649 |
+
outputs=[viz_html, tracking_result_video]
|
| 650 |
+
)
|
| 651 |
|
| 652 |
+
# Launch the interface
|
| 653 |
if __name__ == "__main__":
|
| 654 |
+
print("🌟 Launching SpatialTracker V2 Frontend...")
|
| 655 |
+
print(f"🔗 Backend Status: {'Connected' if BACKEND_AVAILABLE else 'Disconnected'}")
|
| 656 |
+
|
| 657 |
+
demo.launch(
|
| 658 |
+
server_name="0.0.0.0",
|
| 659 |
+
server_port=7860,
|
| 660 |
+
share=True,
|
| 661 |
+
debug=True,
|
| 662 |
+
show_error=True
|
| 663 |
+
)
|
requirements.txt
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
gradio==5.31.0
|
|
|
|
| 2 |
opencv-python-headless
|
|
|
|
| 1 |
gradio==5.31.0
|
| 2 |
+
gradio_client
|
| 3 |
opencv-python-headless
|