Spaces:
Running
on
Zero
Running
on
Zero
xiaoyuxi
commited on
Commit
·
0eb8b91
1
Parent(s):
3d4f5b5
vggt
Browse files
app.py
CHANGED
|
@@ -25,15 +25,14 @@ def initialize_backend():
|
|
| 25 |
print(f"🔧 Backend API object type: {type(backend_api)}")
|
| 26 |
print(f"🔧 Backend API object attributes: {dir(backend_api)}")
|
| 27 |
|
| 28 |
-
# Check if it's
|
| 29 |
-
if hasattr(backend_api, '
|
| 30 |
-
print(f"
|
|
|
|
| 31 |
BACKEND_AVAILABLE = True
|
| 32 |
-
print("✅ Backend connection successful!")
|
| 33 |
-
print("✅ Backend API functions are available")
|
| 34 |
return True
|
| 35 |
else:
|
| 36 |
-
print("❌ Backend API
|
| 37 |
print(f"🔧 Available methods: {[attr for attr in dir(backend_api) if not attr.startswith('_')]}")
|
| 38 |
BACKEND_AVAILABLE = False
|
| 39 |
return False
|
|
@@ -97,14 +96,20 @@ def handle_video_upload(video):
|
|
| 97 |
return None, None, [], 50, 756, 3
|
| 98 |
|
| 99 |
try:
|
| 100 |
-
if BACKEND_AVAILABLE and backend_api
|
| 101 |
# Try to use backend API
|
| 102 |
try:
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
if isinstance(result, dict) and result.get("success"):
|
| 109 |
# For now, just extract the first frame locally
|
| 110 |
display_image = extract_first_frame(video)
|
|
@@ -115,7 +120,7 @@ def handle_video_upload(video):
|
|
| 115 |
# Fallback to local processing
|
| 116 |
pass
|
| 117 |
else:
|
| 118 |
-
print("Backend function
|
| 119 |
# Fallback to local processing
|
| 120 |
pass
|
| 121 |
except Exception as e:
|
|
@@ -151,18 +156,25 @@ def select_point(original_img: str, sel_pix: list, point_type: str, evt: gr.Sele
|
|
| 151 |
if BACKEND_AVAILABLE and backend_api:
|
| 152 |
# Try to use backend API
|
| 153 |
try:
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
except Exception as e:
|
| 160 |
print(f"Backend API call failed: {e}")
|
| 161 |
# Fallback to local processing
|
| 162 |
pass
|
| 163 |
|
| 164 |
-
# Fallback: local processing
|
| 165 |
-
print("Using local point selection...")
|
| 166 |
|
| 167 |
# Parse original image state
|
| 168 |
try:
|
|
@@ -172,16 +184,36 @@ def select_point(original_img: str, sel_pix: list, point_type: str, evt: gr.Sele
|
|
| 172 |
video_path = None
|
| 173 |
|
| 174 |
if video_path:
|
| 175 |
-
# Re-extract frame and add point
|
| 176 |
display_image = extract_first_frame(video_path)
|
| 177 |
if display_image is not None:
|
| 178 |
-
# Add point to the image
|
| 179 |
x, y = evt.index[0], evt.index[1]
|
| 180 |
color = (0, 255, 0) if point_type == 'positive_point' else (255, 0, 0)
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
# Update selected points
|
| 184 |
new_sel_pix = sel_pix + [(x, y, point_type)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
return display_image, new_sel_pix
|
| 186 |
|
| 187 |
return None, sel_pix
|
|
@@ -199,9 +231,18 @@ def reset_points(original_img: str, sel_pix):
|
|
| 199 |
if BACKEND_AVAILABLE and backend_api:
|
| 200 |
# Try to use backend API
|
| 201 |
try:
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
except Exception as e:
|
| 206 |
print(f"Backend API call failed: {e}")
|
| 207 |
# Fallback to local processing
|
|
@@ -234,18 +275,18 @@ def launch_viz(grid_size, vo_points, fps, original_image_state):
|
|
| 234 |
return None, None
|
| 235 |
|
| 236 |
try:
|
| 237 |
-
if BACKEND_AVAILABLE and backend_api
|
| 238 |
# Try to use backend API
|
| 239 |
try:
|
| 240 |
print(f"🔧 Calling backend API with parameters: grid_size={grid_size}, vo_points={vo_points}, fps={fps}")
|
| 241 |
print(f"🔧 Original image state type: {type(original_image_state)}")
|
| 242 |
print(f"🔧 Original image state preview: {str(original_image_state)[:100]}...")
|
| 243 |
|
| 244 |
-
#
|
| 245 |
-
if
|
| 246 |
-
print("🔧
|
| 247 |
# For now, we'll use empty points since we're in local mode
|
| 248 |
-
result = backend_api.
|
| 249 |
|
| 250 |
print(f"✅ Backend API call successful!")
|
| 251 |
print(f"🔧 Result type: {type(result)}")
|
|
@@ -261,7 +302,7 @@ def launch_viz(grid_size, vo_points, fps, original_image_state):
|
|
| 261 |
# Fallback to error message
|
| 262 |
pass
|
| 263 |
else:
|
| 264 |
-
print("Backend function
|
| 265 |
# Fallback to error message
|
| 266 |
pass
|
| 267 |
except Exception as e:
|
|
@@ -289,7 +330,7 @@ def launch_viz(grid_size, vo_points, fps, original_image_state):
|
|
| 289 |
<p style='color: #666; font-size: 12px; margin: 0;'>Backend Available: {BACKEND_AVAILABLE}</p>
|
| 290 |
<p style='color: #666; font-size: 12px; margin: 0;'>Backend API Object: {backend_api is not None}</p>
|
| 291 |
<p style='color: #666; font-size: 12px; margin: 0;'>Backend URL: {BACKEND_SPACE_URL}</p>
|
| 292 |
-
<p style='color: #666; font-size: 12px; margin: 0;'>
|
| 293 |
</div>
|
| 294 |
<p style='color: #2d3436; font-weight: bold; margin-top: 15px;'>
|
| 295 |
Current Status: Backend unavailable - Running in limited mode
|
|
@@ -329,13 +370,12 @@ def test_backend_connection():
|
|
| 329 |
try:
|
| 330 |
# Try a simple API call to test connection
|
| 331 |
print("Testing backend connection with a simple call...")
|
| 332 |
-
# Check if we have
|
| 333 |
-
if hasattr(backend_api, '
|
| 334 |
-
print("✅ Backend API
|
| 335 |
-
print(f"🔧 Available function indices: {list(backend_api.fns.keys())}")
|
| 336 |
return True
|
| 337 |
else:
|
| 338 |
-
print("❌ Backend API
|
| 339 |
BACKEND_AVAILABLE = False
|
| 340 |
return False
|
| 341 |
except Exception as e:
|
|
@@ -352,13 +392,12 @@ def test_backend_api():
|
|
| 352 |
try:
|
| 353 |
print("🧪 Testing backend API functions...")
|
| 354 |
|
| 355 |
-
# Test if
|
| 356 |
-
if hasattr(backend_api, '
|
| 357 |
-
print(
|
| 358 |
-
|
| 359 |
-
print(f"✅ Function {idx} is available")
|
| 360 |
else:
|
| 361 |
-
print("❌ No
|
| 362 |
|
| 363 |
return True
|
| 364 |
except Exception as e:
|
|
@@ -449,6 +488,18 @@ with gr.Blocks(css="""
|
|
| 449 |
<li>⚡ Click 'Run Tracker and Visualize' when done</li>
|
| 450 |
<li>🔍 Iterative 3D result will be shown in the visualization</li>
|
| 451 |
</ol>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 452 |
<div style='background-color: {status_color}20; border: 2px solid {status_color}; border-radius: 8px; padding: 10px; margin-top: 15px;'>
|
| 453 |
<p style='font-size: 18px; color: {status_color}; margin: 0;'>
|
| 454 |
{status_icon} Backend Status: {status_text}
|
|
|
|
| 25 |
print(f"🔧 Backend API object type: {type(backend_api)}")
|
| 26 |
print(f"🔧 Backend API object attributes: {dir(backend_api)}")
|
| 27 |
|
| 28 |
+
# Check if it's an Interface object and has fn
|
| 29 |
+
if hasattr(backend_api, 'fn'):
|
| 30 |
+
print(f"✅ Backend connection successful!")
|
| 31 |
+
print("✅ Backend API function is available")
|
| 32 |
BACKEND_AVAILABLE = True
|
|
|
|
|
|
|
| 33 |
return True
|
| 34 |
else:
|
| 35 |
+
print("❌ Backend API function not found")
|
| 36 |
print(f"🔧 Available methods: {[attr for attr in dir(backend_api) if not attr.startswith('_')]}")
|
| 37 |
BACKEND_AVAILABLE = False
|
| 38 |
return False
|
|
|
|
| 96 |
return None, None, [], 50, 756, 3
|
| 97 |
|
| 98 |
try:
|
| 99 |
+
if BACKEND_AVAILABLE and backend_api:
|
| 100 |
# Try to use backend API
|
| 101 |
try:
|
| 102 |
+
print("🔧 Calling backend API for video upload...")
|
| 103 |
+
|
| 104 |
+
# For gr.Interface, we can call the function directly
|
| 105 |
+
if hasattr(backend_api, 'fn'):
|
| 106 |
+
print("🔧 Using backend Interface function")
|
| 107 |
+
result = backend_api.fn(video, [], 50, 756, 3)
|
| 108 |
+
|
| 109 |
+
print(f"✅ Backend video upload API call successful!")
|
| 110 |
+
print(f"🔧 Result type: {type(result)}")
|
| 111 |
+
|
| 112 |
+
# Parse the result - expect a dict with success status
|
| 113 |
if isinstance(result, dict) and result.get("success"):
|
| 114 |
# For now, just extract the first frame locally
|
| 115 |
display_image = extract_first_frame(video)
|
|
|
|
| 120 |
# Fallback to local processing
|
| 121 |
pass
|
| 122 |
else:
|
| 123 |
+
print("Backend function not found")
|
| 124 |
# Fallback to local processing
|
| 125 |
pass
|
| 126 |
except Exception as e:
|
|
|
|
| 156 |
if BACKEND_AVAILABLE and backend_api:
|
| 157 |
# Try to use backend API
|
| 158 |
try:
|
| 159 |
+
print(f"🔧 Calling backend select point API: x={evt.index[0]}, y={evt.index[1]}, type={point_type}")
|
| 160 |
+
|
| 161 |
+
# For gr.Interface, we can call the function directly
|
| 162 |
+
if hasattr(backend_api, 'fn'):
|
| 163 |
+
print("🔧 Using backend Interface function for point selection")
|
| 164 |
+
# Note: This might not work as expected since the interface expects different inputs
|
| 165 |
+
# For now, fallback to local processing
|
| 166 |
+
pass
|
| 167 |
+
else:
|
| 168 |
+
print("Backend function not found")
|
| 169 |
+
# Fallback to local processing
|
| 170 |
+
pass
|
| 171 |
except Exception as e:
|
| 172 |
print(f"Backend API call failed: {e}")
|
| 173 |
# Fallback to local processing
|
| 174 |
pass
|
| 175 |
|
| 176 |
+
# Fallback: local processing with improved visualization
|
| 177 |
+
print("Using local point selection with enhanced visualization...")
|
| 178 |
|
| 179 |
# Parse original image state
|
| 180 |
try:
|
|
|
|
| 184 |
video_path = None
|
| 185 |
|
| 186 |
if video_path:
|
| 187 |
+
# Re-extract frame and add point with mask visualization
|
| 188 |
display_image = extract_first_frame(video_path)
|
| 189 |
if display_image is not None:
|
| 190 |
+
# Add point to the image with enhanced visualization
|
| 191 |
x, y = evt.index[0], evt.index[1]
|
| 192 |
color = (0, 255, 0) if point_type == 'positive_point' else (255, 0, 0)
|
| 193 |
+
|
| 194 |
+
# Draw a more visible point
|
| 195 |
+
cv2.circle(display_image, (x, y), 8, color, -1)
|
| 196 |
+
cv2.circle(display_image, (x, y), 10, (255, 255, 255), 2)
|
| 197 |
+
|
| 198 |
+
# Add a simple mask-like visualization (circle around the point)
|
| 199 |
+
mask_radius = 30
|
| 200 |
+
mask_color = (0, 255, 0, 50) if point_type == 'positive_point' else (255, 0, 0, 50)
|
| 201 |
+
|
| 202 |
+
# Create a simple mask visualization
|
| 203 |
+
mask_overlay = display_image.copy()
|
| 204 |
+
cv2.circle(mask_overlay, (x, y), mask_radius, color, -1)
|
| 205 |
+
|
| 206 |
+
# Blend the mask with the original image
|
| 207 |
+
alpha = 0.3
|
| 208 |
+
display_image = cv2.addWeighted(display_image, 1-alpha, mask_overlay, alpha, 0)
|
| 209 |
|
| 210 |
# Update selected points
|
| 211 |
new_sel_pix = sel_pix + [(x, y, point_type)]
|
| 212 |
+
|
| 213 |
+
# Add text label
|
| 214 |
+
label = "Positive" if point_type == 'positive_point' else "Negative"
|
| 215 |
+
cv2.putText(display_image, label, (x+15, y-15), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
|
| 216 |
+
|
| 217 |
return display_image, new_sel_pix
|
| 218 |
|
| 219 |
return None, sel_pix
|
|
|
|
| 231 |
if BACKEND_AVAILABLE and backend_api:
|
| 232 |
# Try to use backend API
|
| 233 |
try:
|
| 234 |
+
print("🔧 Calling backend reset points API")
|
| 235 |
+
|
| 236 |
+
# For gr.Interface, we can call the function directly
|
| 237 |
+
if hasattr(backend_api, 'fn'):
|
| 238 |
+
print("🔧 Using backend Interface function for reset")
|
| 239 |
+
# Note: This might not work as expected since the interface expects different inputs
|
| 240 |
+
# For now, fallback to local processing
|
| 241 |
+
pass
|
| 242 |
+
else:
|
| 243 |
+
print("Backend function not found")
|
| 244 |
+
# Fallback to local processing
|
| 245 |
+
pass
|
| 246 |
except Exception as e:
|
| 247 |
print(f"Backend API call failed: {e}")
|
| 248 |
# Fallback to local processing
|
|
|
|
| 275 |
return None, None
|
| 276 |
|
| 277 |
try:
|
| 278 |
+
if BACKEND_AVAILABLE and backend_api:
|
| 279 |
# Try to use backend API
|
| 280 |
try:
|
| 281 |
print(f"🔧 Calling backend API with parameters: grid_size={grid_size}, vo_points={vo_points}, fps={fps}")
|
| 282 |
print(f"🔧 Original image state type: {type(original_image_state)}")
|
| 283 |
print(f"🔧 Original image state preview: {str(original_image_state)[:100]}...")
|
| 284 |
|
| 285 |
+
# For gr.Interface, we can call the function directly
|
| 286 |
+
if hasattr(backend_api, 'fn'):
|
| 287 |
+
print("🔧 Using backend Interface function")
|
| 288 |
# For now, we'll use empty points since we're in local mode
|
| 289 |
+
result = backend_api.fn(None, [], grid_size, vo_points, fps)
|
| 290 |
|
| 291 |
print(f"✅ Backend API call successful!")
|
| 292 |
print(f"🔧 Result type: {type(result)}")
|
|
|
|
| 302 |
# Fallback to error message
|
| 303 |
pass
|
| 304 |
else:
|
| 305 |
+
print("Backend function not found")
|
| 306 |
# Fallback to error message
|
| 307 |
pass
|
| 308 |
except Exception as e:
|
|
|
|
| 330 |
<p style='color: #666; font-size: 12px; margin: 0;'>Backend Available: {BACKEND_AVAILABLE}</p>
|
| 331 |
<p style='color: #666; font-size: 12px; margin: 0;'>Backend API Object: {backend_api is not None}</p>
|
| 332 |
<p style='color: #666; font-size: 12px; margin: 0;'>Backend URL: {BACKEND_SPACE_URL}</p>
|
| 333 |
+
<p style='color: #666; font-size: 12px; margin: 0;'>Backend Type: {type(backend_api) if backend_api else 'None'}</p>
|
| 334 |
</div>
|
| 335 |
<p style='color: #2d3436; font-weight: bold; margin-top: 15px;'>
|
| 336 |
Current Status: Backend unavailable - Running in limited mode
|
|
|
|
| 370 |
try:
|
| 371 |
# Try a simple API call to test connection
|
| 372 |
print("Testing backend connection with a simple call...")
|
| 373 |
+
# Check if we have fn available
|
| 374 |
+
if hasattr(backend_api, 'fn'):
|
| 375 |
+
print("✅ Backend API function is available")
|
|
|
|
| 376 |
return True
|
| 377 |
else:
|
| 378 |
+
print("❌ Backend API function not found")
|
| 379 |
BACKEND_AVAILABLE = False
|
| 380 |
return False
|
| 381 |
except Exception as e:
|
|
|
|
| 392 |
try:
|
| 393 |
print("🧪 Testing backend API functions...")
|
| 394 |
|
| 395 |
+
# Test if fn exists
|
| 396 |
+
if hasattr(backend_api, 'fn'):
|
| 397 |
+
print("✅ Backend has function available")
|
| 398 |
+
print(f"✅ Function type: {type(backend_api.fn)}")
|
|
|
|
| 399 |
else:
|
| 400 |
+
print("❌ No function available in backend")
|
| 401 |
|
| 402 |
return True
|
| 403 |
except Exception as e:
|
|
|
|
| 488 |
<li>⚡ Click 'Run Tracker and Visualize' when done</li>
|
| 489 |
<li>🔍 Iterative 3D result will be shown in the visualization</li>
|
| 490 |
</ol>
|
| 491 |
+
|
| 492 |
+
<div style='background-color: #fff3cd; border: 2px solid #ffc107; border-radius: 8px; padding: 15px; margin-top: 15px;'>
|
| 493 |
+
<p style='font-size: 18px; color: #856404; margin: 0 0 10px 0; font-weight: bold;'>
|
| 494 |
+
⭐ If you like our work, please give us a star on GitHub!
|
| 495 |
+
</p>
|
| 496 |
+
<p style='font-size: 16px; color: #856404; margin: 0;'>
|
| 497 |
+
<a href="https://github.com/henry123-boy/SpaTrackerV2" target="_blank" style="color: #0066cc; text-decoration: none; font-weight: bold;">
|
| 498 |
+
🌟 https://github.com/henry123-boy/SpaTrackerV2
|
| 499 |
+
</a>
|
| 500 |
+
</p>
|
| 501 |
+
</div>
|
| 502 |
+
|
| 503 |
<div style='background-color: {status_color}20; border: 2px solid {status_color}; border-radius: 8px; padding: 10px; margin-top: 15px;'>
|
| 504 |
<p style='font-size: 18px; color: {status_color}; margin: 0;'>
|
| 505 |
{status_icon} Backend Status: {status_text}
|