Spaces:
Running
on
Zero
Running
on
Zero
xiaoyuxi
commited on
Commit
·
bf11931
1
Parent(s):
07b4d20
vggt
Browse files- .gitattributes +1 -0
- app.py +312 -4
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
*.mp4 filter=lfs diff=lfs merge=lfs -text
|
app.py
CHANGED
|
@@ -1,6 +1,314 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
demo.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
import numpy as np
|
| 5 |
+
import cv2
|
| 6 |
+
import base64
|
| 7 |
+
from typing import List, Tuple
|
| 8 |
+
|
| 9 |
+
# Backend Space URL - replace with your actual backend space URL
|
| 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 |
+
def numpy_to_base64(arr):
|
| 13 |
+
"""Convert numpy array to base64 string"""
|
| 14 |
+
return base64.b64encode(arr.tobytes()).decode('utf-8')
|
| 15 |
+
|
| 16 |
+
def base64_to_numpy(b64_str, shape, dtype):
|
| 17 |
+
"""Convert base64 string back to numpy array"""
|
| 18 |
+
return np.frombuffer(base64.b64decode(b64_str), dtype=dtype).reshape(shape)
|
| 19 |
+
|
| 20 |
+
def base64_to_image(b64_str):
|
| 21 |
+
"""Convert base64 string to numpy image array"""
|
| 22 |
+
if not b64_str:
|
| 23 |
+
return None
|
| 24 |
+
try:
|
| 25 |
+
# Decode base64 to bytes
|
| 26 |
+
img_bytes = base64.b64decode(b64_str)
|
| 27 |
+
# Convert bytes to numpy array
|
| 28 |
+
nparr = np.frombuffer(img_bytes, np.uint8)
|
| 29 |
+
# Decode image
|
| 30 |
+
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 31 |
+
# Convert BGR to RGB
|
| 32 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 33 |
+
return img
|
| 34 |
+
except Exception as e:
|
| 35 |
+
print(f"Error converting base64 to image: {e}")
|
| 36 |
+
return None
|
| 37 |
+
|
| 38 |
+
def get_video_name(video_path):
|
| 39 |
+
"""Extract video name without extension"""
|
| 40 |
+
return os.path.splitext(os.path.basename(video_path))[0]
|
| 41 |
+
|
| 42 |
+
def handle_video_upload(video):
|
| 43 |
+
"""Handle video upload and extract first frame"""
|
| 44 |
+
if video is None:
|
| 45 |
+
return None, None, []
|
| 46 |
+
|
| 47 |
+
try:
|
| 48 |
+
# Load backend API
|
| 49 |
+
backend_api = gr.load(f"spaces/{BACKEND_SPACE_URL}", hf_token=hf_token)
|
| 50 |
+
|
| 51 |
+
# Call backend upload API
|
| 52 |
+
original_image_state, display_image_b64, selected_points, grid_size_val, vo_points_val, fps_val = backend_api.upload_video_api(video)
|
| 53 |
+
|
| 54 |
+
# Convert base64 image back to numpy array
|
| 55 |
+
display_image = base64_to_image(display_image_b64)
|
| 56 |
+
|
| 57 |
+
return original_image_state, display_image, selected_points, grid_size_val, vo_points_val, fps_val
|
| 58 |
+
|
| 59 |
+
except Exception as e:
|
| 60 |
+
print(f"Error in handle_video_upload: {e}")
|
| 61 |
+
return None, None, [], 50, 756, 3
|
| 62 |
+
|
| 63 |
+
def select_point(original_img: str, sel_pix: list, point_type: str, evt: gr.SelectData):
|
| 64 |
+
"""Handle point selection for SAM"""
|
| 65 |
+
if original_img is None:
|
| 66 |
+
return None, []
|
| 67 |
+
|
| 68 |
+
try:
|
| 69 |
+
# Load backend API
|
| 70 |
+
backend_api = gr.load(f"spaces/{BACKEND_SPACE_URL}", hf_token=hf_token)
|
| 71 |
+
|
| 72 |
+
# Call backend select point API
|
| 73 |
+
display_image_b64, new_sel_pix = backend_api.select_point_api(
|
| 74 |
+
original_img, sel_pix, point_type, evt.index[0], evt.index[1]
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
# Convert base64 image back to numpy array
|
| 78 |
+
display_image = base64_to_image(display_image_b64)
|
| 79 |
+
|
| 80 |
+
return display_image, new_sel_pix
|
| 81 |
+
|
| 82 |
+
except Exception as e:
|
| 83 |
+
print(f"Error in select_point: {e}")
|
| 84 |
+
return None, sel_pix
|
| 85 |
+
|
| 86 |
+
def reset_points(original_img: str, sel_pix):
|
| 87 |
+
"""Reset all points and clear the mask"""
|
| 88 |
+
if original_img is None:
|
| 89 |
+
return None, []
|
| 90 |
+
|
| 91 |
+
try:
|
| 92 |
+
# Load backend API
|
| 93 |
+
backend_api = gr.load(f"spaces/{BACKEND_SPACE_URL}", hf_token=hf_token)
|
| 94 |
+
|
| 95 |
+
# Call backend reset points API
|
| 96 |
+
display_image_b64, new_sel_pix = backend_api.reset_points_api(original_img, sel_pix)
|
| 97 |
+
|
| 98 |
+
# Convert base64 image back to numpy array
|
| 99 |
+
display_image = base64_to_image(display_image_b64)
|
| 100 |
+
|
| 101 |
+
return display_image, new_sel_pix
|
| 102 |
+
|
| 103 |
+
except Exception as e:
|
| 104 |
+
print(f"Error in reset_points: {e}")
|
| 105 |
+
return None, []
|
| 106 |
+
|
| 107 |
+
def launch_viz(grid_size, vo_points, fps, original_image_state):
|
| 108 |
+
"""Launch visualization with user-specific temp directory"""
|
| 109 |
+
if original_image_state is None:
|
| 110 |
+
return None, None
|
| 111 |
+
|
| 112 |
+
try:
|
| 113 |
+
# Load backend API
|
| 114 |
+
backend_api = gr.load(f"spaces/{BACKEND_SPACE_URL}", hf_token=hf_token)
|
| 115 |
+
|
| 116 |
+
# Call backend run tracker API
|
| 117 |
+
viz_iframe_html, track_video_path = backend_api.run_tracker_api(
|
| 118 |
+
grid_size, vo_points, fps, original_image_state
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
return viz_iframe_html, track_video_path
|
| 122 |
+
|
| 123 |
+
except Exception as e:
|
| 124 |
+
print(f"Error in launch_viz: {e}")
|
| 125 |
+
return None, None
|
| 126 |
+
|
| 127 |
+
def clear_all():
|
| 128 |
+
"""Clear all buffers and temporary files"""
|
| 129 |
+
return None, None, []
|
| 130 |
+
|
| 131 |
+
def update_tracker_model(vo_points):
|
| 132 |
+
return None # No output needed
|
| 133 |
+
|
| 134 |
+
# Function to handle both manual upload and example selection
|
| 135 |
+
def handle_video_change(video):
|
| 136 |
+
"""Handle video change from both manual upload and example selection"""
|
| 137 |
+
if video is None:
|
| 138 |
+
return None, None, [], 50, 756, 3
|
| 139 |
+
|
| 140 |
+
# Handle video upload (extract first frame)
|
| 141 |
+
original_image_state, display_image, selected_points, grid_size_val, vo_points_val, fps_val = handle_video_upload(video)
|
| 142 |
+
|
| 143 |
+
return original_image_state, display_image, selected_points, grid_size_val, vo_points_val, fps_val
|
| 144 |
+
|
| 145 |
+
# Build UI
|
| 146 |
+
with gr.Blocks(css="""
|
| 147 |
+
#advanced_settings .wrap {
|
| 148 |
+
font-size: 14px !important;
|
| 149 |
+
}
|
| 150 |
+
#advanced_settings .gr-slider {
|
| 151 |
+
font-size: 13px !important;
|
| 152 |
+
}
|
| 153 |
+
#advanced_settings .gr-slider .gr-label {
|
| 154 |
+
font-size: 13px !important;
|
| 155 |
+
margin-bottom: 5px !important;
|
| 156 |
+
}
|
| 157 |
+
#advanced_settings .gr-slider .gr-info {
|
| 158 |
+
font-size: 12px !important;
|
| 159 |
+
}
|
| 160 |
+
#point_label_radio .gr-radio-group {
|
| 161 |
+
flex-direction: row !important;
|
| 162 |
+
gap: 15px !important;
|
| 163 |
+
}
|
| 164 |
+
#point_label_radio .gr-radio-group label {
|
| 165 |
+
margin-right: 0 !important;
|
| 166 |
+
margin-bottom: 0 !important;
|
| 167 |
+
}
|
| 168 |
+
/* Style for example videos label */
|
| 169 |
+
.gr-examples .gr-label {
|
| 170 |
+
font-weight: bold !important;
|
| 171 |
+
font-size: 16px !important;
|
| 172 |
+
}
|
| 173 |
+
/* Simple horizontal scroll for examples */
|
| 174 |
+
.gr-examples .gr-table-wrapper {
|
| 175 |
+
overflow-x: auto !important;
|
| 176 |
+
overflow-y: hidden !important;
|
| 177 |
+
}
|
| 178 |
+
.gr-examples .gr-table {
|
| 179 |
+
display: flex !important;
|
| 180 |
+
flex-wrap: nowrap !important;
|
| 181 |
+
min-width: max-content !important;
|
| 182 |
+
}
|
| 183 |
+
.gr-examples .gr-table tbody {
|
| 184 |
+
display: flex !important;
|
| 185 |
+
flex-direction: row !important;
|
| 186 |
+
flex-wrap: nowrap !important;
|
| 187 |
+
}
|
| 188 |
+
.gr-examples .gr-table tbody tr {
|
| 189 |
+
display: flex !important;
|
| 190 |
+
flex-direction: column !important;
|
| 191 |
+
min-width: 150px !important;
|
| 192 |
+
margin-right: 10px !important;
|
| 193 |
+
}
|
| 194 |
+
.gr-examples .gr-table tbody tr td {
|
| 195 |
+
text-align: center !important;
|
| 196 |
+
padding: 5px !important;
|
| 197 |
+
}
|
| 198 |
+
""") as demo:
|
| 199 |
+
# Initialize states inside Blocks
|
| 200 |
+
selected_points = gr.State([])
|
| 201 |
+
original_image_state = gr.State() # Store original image in state
|
| 202 |
+
|
| 203 |
+
with gr.Row():
|
| 204 |
+
gr.Markdown("""
|
| 205 |
+
# ✨ SpaTrackV2 Frontend (Client)
|
| 206 |
+
<div style='background-color: #e6f3ff; padding: 20px; border-radius: 10px; margin: 10px 0;'>
|
| 207 |
+
<h2 style='color: #0066cc; margin-bottom: 15px;'>Instructions:</h2>
|
| 208 |
+
<ol style='font-size: 20px; line-height: 1.6;'>
|
| 209 |
+
<li>🎬 Upload a video or select from examples below</li>
|
| 210 |
+
<li>🎯 Select positive points (green) and negative points (red) on the first frame</li>
|
| 211 |
+
<li>⚡ Click 'Run Tracker and Visualize' when done</li>
|
| 212 |
+
<li>🔍 Iterative 3D result will be shown in the visualization</li>
|
| 213 |
+
</ol>
|
| 214 |
+
<p style='font-size: 22px;'>❗ This frontend connects to a private backend Space for processing</p>
|
| 215 |
+
</div>
|
| 216 |
+
""")
|
| 217 |
+
|
| 218 |
+
with gr.Row():
|
| 219 |
+
with gr.Column(scale=1):
|
| 220 |
+
video_input = gr.Video(label="Upload Video", format="mp4", height=300)
|
| 221 |
+
|
| 222 |
+
# Move Interactive Frame and 2D Tracking under video upload
|
| 223 |
+
with gr.Row():
|
| 224 |
+
display_image = gr.Image(type="numpy", label="📸 Interactive Frame", height=250)
|
| 225 |
+
track_video = gr.Video(label="🎯 2D Tracking Result", height=250)
|
| 226 |
+
|
| 227 |
+
with gr.Row():
|
| 228 |
+
fg_bg_radio = gr.Radio(choices=['positive_point', 'negative_point'],
|
| 229 |
+
label='Point label',
|
| 230 |
+
value='positive_point',
|
| 231 |
+
elem_id="point_label_radio")
|
| 232 |
+
reset_button = gr.Button("Reset points")
|
| 233 |
+
clear_button = gr.Button("Clear All", variant="secondary")
|
| 234 |
+
|
| 235 |
+
with gr.Accordion("⚙️ Advanced Settings", open=True, elem_id="advanced_settings"):
|
| 236 |
+
grid_size = gr.Slider(minimum=10, maximum=100, value=50, step=1,
|
| 237 |
+
label="Grid Size", info="Size of the tracking grid")
|
| 238 |
+
vo_points = gr.Slider(minimum=256, maximum=4096, value=756, step=50,
|
| 239 |
+
label="VO Points", info="Number of points for solving camera pose")
|
| 240 |
+
fps_slider = gr.Slider(minimum=1, maximum=10, value=3, step=1,
|
| 241 |
+
label="FPS", info="FPS of the output video")
|
| 242 |
+
|
| 243 |
+
viz_button = gr.Button("🚀 Run Tracker and Visualize", variant="primary", size="lg")
|
| 244 |
+
|
| 245 |
+
with gr.Column(scale=2):
|
| 246 |
+
# Add example videos using gr.Examples
|
| 247 |
+
examples_component = gr.Examples(
|
| 248 |
+
examples=[
|
| 249 |
+
"examples/kiss.mp4",
|
| 250 |
+
"examples/backpack.mp4",
|
| 251 |
+
"examples/pillow.mp4",
|
| 252 |
+
"examples/hockey.mp4",
|
| 253 |
+
"examples/drifting.mp4",
|
| 254 |
+
"examples/ken_block_0.mp4",
|
| 255 |
+
"examples/ball.mp4",
|
| 256 |
+
"examples/kitchen.mp4",
|
| 257 |
+
"examples/ego_teaser.mp4",
|
| 258 |
+
"examples/ego_kc1.mp4",
|
| 259 |
+
"examples/vertical_place.mp4",
|
| 260 |
+
"examples/robot_unitree.mp4",
|
| 261 |
+
"examples/droid_robot.mp4",
|
| 262 |
+
"examples/robot_2.mp4",
|
| 263 |
+
"examples/cinema_0.mp4",
|
| 264 |
+
],
|
| 265 |
+
inputs=[video_input],
|
| 266 |
+
label="📁 Example Videos",
|
| 267 |
+
examples_per_page=20 # Show all examples on one page to enable scrolling
|
| 268 |
+
)
|
| 269 |
+
|
| 270 |
+
# Initialize with the template interface showing "Interactive 3D Tracking"
|
| 271 |
+
viz_iframe = gr.HTML("""
|
| 272 |
+
<div style='border: 3px solid #667eea; border-radius: 10px; overflow: hidden; box-shadow: 0 8px 32px rgba(102, 126, 234, 0.3);'>
|
| 273 |
+
<iframe id="viz_iframe" src="/gradio_api/file=_viz/viz_template.html" width="100%" height="950px" style="border:none;"></iframe>
|
| 274 |
+
</div>
|
| 275 |
+
""")
|
| 276 |
+
|
| 277 |
+
# Simple description below the visualization
|
| 278 |
+
gr.HTML("""
|
| 279 |
+
<div style='text-align: center; margin-top: 15px; color: #666; font-size: 14px;'>
|
| 280 |
+
🎮 Interactive 3D visualization adapted from <a href="https://tapip3d.github.io/" target="_blank" style="color: #667eea;">TAPIP3D</a>
|
| 281 |
+
</div>
|
| 282 |
+
""")
|
| 283 |
+
|
| 284 |
+
# Bind events
|
| 285 |
+
video_input.change(
|
| 286 |
+
handle_video_change,
|
| 287 |
+
inputs=[video_input],
|
| 288 |
+
outputs=[original_image_state, display_image, selected_points, grid_size, vo_points, fps_slider]
|
| 289 |
+
)
|
| 290 |
+
|
| 291 |
+
reset_button.click(reset_points,
|
| 292 |
+
inputs=[original_image_state, selected_points],
|
| 293 |
+
outputs=[display_image, selected_points])
|
| 294 |
+
|
| 295 |
+
clear_button.click(clear_all,
|
| 296 |
+
outputs=[video_input, display_image, selected_points])
|
| 297 |
+
|
| 298 |
+
display_image.select(select_point,
|
| 299 |
+
inputs=[original_image_state, selected_points, fg_bg_radio],
|
| 300 |
+
outputs=[display_image, selected_points])
|
| 301 |
+
|
| 302 |
+
# Update tracker model when vo_points changes
|
| 303 |
+
vo_points.change(update_tracker_model,
|
| 304 |
+
inputs=[vo_points],
|
| 305 |
+
outputs=[])
|
| 306 |
+
|
| 307 |
+
viz_button.click(launch_viz,
|
| 308 |
+
inputs=[grid_size, vo_points, fps_slider, original_image_state],
|
| 309 |
+
outputs=[viz_iframe, track_video],
|
| 310 |
+
)
|
| 311 |
|
| 312 |
+
# Launch the demo
|
| 313 |
+
if __name__ == "__main__":
|
| 314 |
+
demo.launch()
|