Spaces:
Running
on
Zero
Running
on
Zero
xiaoyuxi
commited on
Commit
·
6f5c5a6
1
Parent(s):
ca773c4
vggt
Browse files
app.py
CHANGED
|
@@ -10,6 +10,25 @@ from typing import List, Tuple
|
|
| 10 |
BACKEND_SPACE_URL = "Yuxihenry/SpatialTrackerV2_Backend" # Replace with actual backend space URL
|
| 11 |
hf_token = os.getenv("HF_TOKEN") # Replace with your actual Hugging Face token
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
def numpy_to_base64(arr):
|
| 14 |
"""Convert numpy array to base64 string"""
|
| 15 |
return base64.b64encode(arr.tobytes()).decode('utf-8')
|
|
@@ -40,22 +59,54 @@ def get_video_name(video_path):
|
|
| 40 |
"""Extract video name without extension"""
|
| 41 |
return os.path.splitext(os.path.basename(video_path))[0]
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
def handle_video_upload(video):
|
| 44 |
"""Handle video upload and extract first frame"""
|
| 45 |
if video is None:
|
| 46 |
-
return None, None, []
|
| 47 |
|
| 48 |
try:
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
|
|
|
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
except Exception as e:
|
| 61 |
print(f"Error in handle_video_upload: {e}")
|
|
@@ -67,18 +118,43 @@ def select_point(original_img: str, sel_pix: list, point_type: str, evt: gr.Sele
|
|
| 67 |
return None, []
|
| 68 |
|
| 69 |
try:
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
#
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
return
|
| 82 |
|
| 83 |
except Exception as e:
|
| 84 |
print(f"Error in select_point: {e}")
|
|
@@ -90,16 +166,33 @@ def reset_points(original_img: str, sel_pix):
|
|
| 90 |
return None, []
|
| 91 |
|
| 92 |
try:
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
#
|
| 97 |
-
|
| 98 |
|
| 99 |
-
#
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
except Exception as e:
|
| 105 |
print(f"Error in reset_points: {e}")
|
|
@@ -111,15 +204,37 @@ def launch_viz(grid_size, vo_points, fps, original_image_state):
|
|
| 111 |
return None, None
|
| 112 |
|
| 113 |
try:
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
-
#
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
except Exception as e:
|
| 125 |
print(f"Error in launch_viz: {e}")
|
|
@@ -143,6 +258,10 @@ def handle_video_change(video):
|
|
| 143 |
|
| 144 |
return original_image_state, display_image, selected_points, grid_size_val, vo_points_val, fps_val
|
| 145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
# Build UI
|
| 147 |
with gr.Blocks(css="""
|
| 148 |
#advanced_settings .wrap {
|
|
@@ -202,7 +321,12 @@ with gr.Blocks(css="""
|
|
| 202 |
original_image_state = gr.State() # Store original image in state
|
| 203 |
|
| 204 |
with gr.Row():
|
| 205 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
# ✨ SpaTrackV2 Frontend (Client)
|
| 207 |
<div style='background-color: #e6f3ff; padding: 20px; border-radius: 10px; margin: 10px 0;'>
|
| 208 |
<h2 style='color: #0066cc; margin-bottom: 15px;'>Instructions:</h2>
|
|
@@ -212,7 +336,14 @@ with gr.Blocks(css="""
|
|
| 212 |
<li>⚡ Click 'Run Tracker and Visualize' when done</li>
|
| 213 |
<li>🔍 Iterative 3D result will be shown in the visualization</li>
|
| 214 |
</ol>
|
| 215 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
</div>
|
| 217 |
""")
|
| 218 |
|
|
@@ -268,14 +399,10 @@ with gr.Blocks(css="""
|
|
| 268 |
examples_per_page=20 # Show all examples on one page to enable scrolling
|
| 269 |
)
|
| 270 |
|
| 271 |
-
# Initialize with
|
| 272 |
viz_iframe = gr.HTML("""
|
| 273 |
-
<div style='border: 3px solid #667eea; border-radius: 10px; overflow: hidden; box-shadow: 0 8px 32px rgba(102, 126, 234, 0.3);
|
| 274 |
-
<
|
| 275 |
-
<h3 style='margin-bottom: 20px; color: #667eea;'>🎮 Interactive 3D Tracking</h3>
|
| 276 |
-
<p style='font-size: 16px; margin-bottom: 10px;'>Upload a video and select points to start tracking</p>
|
| 277 |
-
<p style='font-size: 14px; color: #999;'>Powered by SpaTrackV2</p>
|
| 278 |
-
</div>
|
| 279 |
</div>
|
| 280 |
""")
|
| 281 |
|
|
|
|
| 10 |
BACKEND_SPACE_URL = "Yuxihenry/SpatialTrackerV2_Backend" # Replace with actual backend space URL
|
| 11 |
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 |
+
backend_api = None
|
| 16 |
+
|
| 17 |
+
def initialize_backend():
|
| 18 |
+
"""Initialize backend connection"""
|
| 19 |
+
global backend_api, BACKEND_AVAILABLE
|
| 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 |
+
BACKEND_AVAILABLE = True
|
| 24 |
+
print("✅ Backend connection successful!")
|
| 25 |
+
return True
|
| 26 |
+
except Exception as e:
|
| 27 |
+
print(f"❌ Backend connection failed: {e}")
|
| 28 |
+
print("⚠️ Running in standalone mode (some features may be limited)")
|
| 29 |
+
BACKEND_AVAILABLE = False
|
| 30 |
+
return False
|
| 31 |
+
|
| 32 |
def numpy_to_base64(arr):
|
| 33 |
"""Convert numpy array to base64 string"""
|
| 34 |
return base64.b64encode(arr.tobytes()).decode('utf-8')
|
|
|
|
| 59 |
"""Extract video name without extension"""
|
| 60 |
return os.path.splitext(os.path.basename(video_path))[0]
|
| 61 |
|
| 62 |
+
def extract_first_frame(video_path):
|
| 63 |
+
"""Extract first frame from video file"""
|
| 64 |
+
try:
|
| 65 |
+
cap = cv2.VideoCapture(video_path)
|
| 66 |
+
ret, frame = cap.read()
|
| 67 |
+
cap.release()
|
| 68 |
+
|
| 69 |
+
if ret:
|
| 70 |
+
# Convert BGR to RGB
|
| 71 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 72 |
+
return frame_rgb
|
| 73 |
+
else:
|
| 74 |
+
return None
|
| 75 |
+
except Exception as e:
|
| 76 |
+
print(f"Error extracting first frame: {e}")
|
| 77 |
+
return None
|
| 78 |
+
|
| 79 |
def handle_video_upload(video):
|
| 80 |
"""Handle video upload and extract first frame"""
|
| 81 |
if video is None:
|
| 82 |
+
return None, None, [], 50, 756, 3
|
| 83 |
|
| 84 |
try:
|
| 85 |
+
if BACKEND_AVAILABLE and backend_api:
|
| 86 |
+
# Try to use backend API
|
| 87 |
+
try:
|
| 88 |
+
original_image_state, display_image_b64, selected_points, grid_size_val, vo_points_val, fps_val = backend_api.upload_video_api(video)
|
| 89 |
+
display_image = base64_to_image(display_image_b64)
|
| 90 |
+
return original_image_state, display_image, selected_points, grid_size_val, vo_points_val, fps_val
|
| 91 |
+
except Exception as e:
|
| 92 |
+
print(f"Backend API call failed: {e}")
|
| 93 |
+
# Fallback to local processing
|
| 94 |
+
pass
|
| 95 |
|
| 96 |
+
# Fallback: local processing
|
| 97 |
+
print("Using local video processing...")
|
| 98 |
+
display_image = extract_first_frame(video)
|
| 99 |
|
| 100 |
+
# Create a simple state representation
|
| 101 |
+
original_image_state = json.dumps({
|
| 102 |
+
"video_path": video,
|
| 103 |
+
"frame": "local_processing"
|
| 104 |
+
})
|
| 105 |
|
| 106 |
+
# Default settings
|
| 107 |
+
grid_size_val, vo_points_val, fps_val = 50, 756, 3
|
| 108 |
+
|
| 109 |
+
return original_image_state, display_image, [], grid_size_val, vo_points_val, fps_val
|
| 110 |
|
| 111 |
except Exception as e:
|
| 112 |
print(f"Error in handle_video_upload: {e}")
|
|
|
|
| 118 |
return None, []
|
| 119 |
|
| 120 |
try:
|
| 121 |
+
if BACKEND_AVAILABLE and backend_api:
|
| 122 |
+
# Try to use backend API
|
| 123 |
+
try:
|
| 124 |
+
display_image_b64, new_sel_pix = backend_api.select_point_api(
|
| 125 |
+
original_img, sel_pix, point_type, evt.index[0], evt.index[1]
|
| 126 |
+
)
|
| 127 |
+
display_image = base64_to_image(display_image_b64)
|
| 128 |
+
return display_image, new_sel_pix
|
| 129 |
+
except Exception as e:
|
| 130 |
+
print(f"Backend API call failed: {e}")
|
| 131 |
+
# Fallback to local processing
|
| 132 |
+
pass
|
| 133 |
+
|
| 134 |
+
# Fallback: local processing
|
| 135 |
+
print("Using local point selection...")
|
| 136 |
|
| 137 |
+
# Parse original image state
|
| 138 |
+
try:
|
| 139 |
+
state_data = json.loads(original_img)
|
| 140 |
+
video_path = state_data.get("video_path")
|
| 141 |
+
except:
|
| 142 |
+
video_path = None
|
| 143 |
|
| 144 |
+
if video_path:
|
| 145 |
+
# Re-extract frame and add point
|
| 146 |
+
display_image = extract_first_frame(video_path)
|
| 147 |
+
if display_image is not None:
|
| 148 |
+
# Add point to the image (simple visualization)
|
| 149 |
+
x, y = evt.index[0], evt.index[1]
|
| 150 |
+
color = (0, 255, 0) if point_type == 'positive_point' else (255, 0, 0)
|
| 151 |
+
cv2.circle(display_image, (x, y), 5, color, -1)
|
| 152 |
+
|
| 153 |
+
# Update selected points
|
| 154 |
+
new_sel_pix = sel_pix + [(x, y, point_type)]
|
| 155 |
+
return display_image, new_sel_pix
|
| 156 |
|
| 157 |
+
return None, sel_pix
|
| 158 |
|
| 159 |
except Exception as e:
|
| 160 |
print(f"Error in select_point: {e}")
|
|
|
|
| 166 |
return None, []
|
| 167 |
|
| 168 |
try:
|
| 169 |
+
if BACKEND_AVAILABLE and backend_api:
|
| 170 |
+
# Try to use backend API
|
| 171 |
+
try:
|
| 172 |
+
display_image_b64, new_sel_pix = backend_api.reset_points_api(original_img, sel_pix)
|
| 173 |
+
display_image = base64_to_image(display_image_b64)
|
| 174 |
+
return display_image, new_sel_pix
|
| 175 |
+
except Exception as e:
|
| 176 |
+
print(f"Backend API call failed: {e}")
|
| 177 |
+
# Fallback to local processing
|
| 178 |
+
pass
|
| 179 |
|
| 180 |
+
# Fallback: local processing
|
| 181 |
+
print("Using local point reset...")
|
| 182 |
|
| 183 |
+
# Parse original image state
|
| 184 |
+
try:
|
| 185 |
+
state_data = json.loads(original_img)
|
| 186 |
+
video_path = state_data.get("video_path")
|
| 187 |
+
except:
|
| 188 |
+
video_path = None
|
| 189 |
|
| 190 |
+
if video_path:
|
| 191 |
+
# Re-extract frame without points
|
| 192 |
+
display_image = extract_first_frame(video_path)
|
| 193 |
+
return display_image, []
|
| 194 |
+
|
| 195 |
+
return None, []
|
| 196 |
|
| 197 |
except Exception as e:
|
| 198 |
print(f"Error in reset_points: {e}")
|
|
|
|
| 204 |
return None, None
|
| 205 |
|
| 206 |
try:
|
| 207 |
+
if BACKEND_AVAILABLE and backend_api:
|
| 208 |
+
# Try to use backend API
|
| 209 |
+
try:
|
| 210 |
+
viz_iframe_html, track_video_path = backend_api.run_tracker_api(
|
| 211 |
+
grid_size, vo_points, fps, original_image_state
|
| 212 |
+
)
|
| 213 |
+
return viz_iframe_html, track_video_path
|
| 214 |
+
except Exception as e:
|
| 215 |
+
print(f"Backend API call failed: {e}")
|
| 216 |
+
# Fallback to local processing
|
| 217 |
+
pass
|
| 218 |
|
| 219 |
+
# Fallback: show message that backend is required
|
| 220 |
+
error_message = """
|
| 221 |
+
<div style='border: 3px solid #ff6b6b; border-radius: 10px; padding: 20px; background-color: #fff5f5;'>
|
| 222 |
+
<h3 style='color: #d63031; margin-bottom: 15px;'>⚠️ Backend Connection Required</h3>
|
| 223 |
+
<p style='color: #2d3436; line-height: 1.6;'>
|
| 224 |
+
The tracking and visualization features require a connection to the backend Space.
|
| 225 |
+
Please ensure:
|
| 226 |
+
</p>
|
| 227 |
+
<ul style='color: #2d3436; line-height: 1.6;'>
|
| 228 |
+
<li>The backend Space is deployed and running</li>
|
| 229 |
+
<li>The BACKEND_SPACE_URL is correctly configured</li>
|
| 230 |
+
<li>You have proper access permissions to the backend Space</li>
|
| 231 |
+
</ul>
|
| 232 |
+
<p style='color: #2d3436; font-weight: bold; margin-top: 15px;'>
|
| 233 |
+
Current Status: Backend unavailable - Running in limited mode
|
| 234 |
+
</p>
|
| 235 |
+
</div>
|
| 236 |
+
"""
|
| 237 |
+
return error_message, None
|
| 238 |
|
| 239 |
except Exception as e:
|
| 240 |
print(f"Error in launch_viz: {e}")
|
|
|
|
| 258 |
|
| 259 |
return original_image_state, display_image, selected_points, grid_size_val, vo_points_val, fps_val
|
| 260 |
|
| 261 |
+
# Initialize backend connection
|
| 262 |
+
print("🔧 Initializing backend connection...")
|
| 263 |
+
initialize_backend()
|
| 264 |
+
|
| 265 |
# Build UI
|
| 266 |
with gr.Blocks(css="""
|
| 267 |
#advanced_settings .wrap {
|
|
|
|
| 321 |
original_image_state = gr.State() # Store original image in state
|
| 322 |
|
| 323 |
with gr.Row():
|
| 324 |
+
# Show backend status
|
| 325 |
+
status_color = "#28a745" if BACKEND_AVAILABLE else "#dc3545"
|
| 326 |
+
status_text = "Connected" if BACKEND_AVAILABLE else "Disconnected"
|
| 327 |
+
status_icon = "✅" if BACKEND_AVAILABLE else "❌"
|
| 328 |
+
|
| 329 |
+
gr.Markdown(f"""
|
| 330 |
# ✨ SpaTrackV2 Frontend (Client)
|
| 331 |
<div style='background-color: #e6f3ff; padding: 20px; border-radius: 10px; margin: 10px 0;'>
|
| 332 |
<h2 style='color: #0066cc; margin-bottom: 15px;'>Instructions:</h2>
|
|
|
|
| 336 |
<li>⚡ Click 'Run Tracker and Visualize' when done</li>
|
| 337 |
<li>🔍 Iterative 3D result will be shown in the visualization</li>
|
| 338 |
</ol>
|
| 339 |
+
<div style='background-color: {status_color}20; border: 2px solid {status_color}; border-radius: 8px; padding: 10px; margin-top: 15px;'>
|
| 340 |
+
<p style='font-size: 18px; color: {status_color}; margin: 0;'>
|
| 341 |
+
{status_icon} Backend Status: {status_text}
|
| 342 |
+
</p>
|
| 343 |
+
<p style='font-size: 14px; color: #666; margin: 5px 0 0 0;'>
|
| 344 |
+
{BACKEND_SPACE_URL}
|
| 345 |
+
</p>
|
| 346 |
+
</div>
|
| 347 |
</div>
|
| 348 |
""")
|
| 349 |
|
|
|
|
| 399 |
examples_per_page=20 # Show all examples on one page to enable scrolling
|
| 400 |
)
|
| 401 |
|
| 402 |
+
# Initialize with the template interface showing "Interactive 3D Tracking"
|
| 403 |
viz_iframe = gr.HTML("""
|
| 404 |
+
<div style='border: 3px solid #667eea; border-radius: 10px; overflow: hidden; box-shadow: 0 8px 32px rgba(102, 126, 234, 0.3);'>
|
| 405 |
+
<iframe id="viz_iframe" src="/gradio_api/file=_viz/viz_template.html" width="100%" height="950px" style="border:none;"></iframe>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 406 |
</div>
|
| 407 |
""")
|
| 408 |
|