Spaces:
Running
on
Zero
Running
on
Zero
xiaoyuxi
commited on
Commit
·
5c652de
1
Parent(s):
83ff4c3
backend
Browse files
app.py
CHANGED
|
@@ -208,7 +208,10 @@ def extract_first_frame(video_path):
|
|
| 208 |
def handle_video_upload(video):
|
| 209 |
"""Handle video upload and extract first frame"""
|
| 210 |
if video is None:
|
| 211 |
-
return None, None, [],
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
try:
|
| 214 |
if BACKEND_AVAILABLE and backend_client:
|
|
@@ -249,9 +252,14 @@ def handle_video_upload(video):
|
|
| 249 |
|
| 250 |
# Get video settings based on video name
|
| 251 |
video_name = get_video_name(video)
|
|
|
|
| 252 |
grid_size_val, vo_points_val, fps_val = get_video_settings(video_name)
|
|
|
|
| 253 |
|
| 254 |
-
return original_image_state, display_image, selected_points,
|
|
|
|
|
|
|
|
|
|
| 255 |
else:
|
| 256 |
print("Backend processing failed, using local fallback")
|
| 257 |
# Fallback to local processing
|
|
@@ -300,13 +308,21 @@ def handle_video_upload(video):
|
|
| 300 |
|
| 301 |
# Get video settings
|
| 302 |
video_name = get_video_name(video)
|
|
|
|
| 303 |
grid_size_val, vo_points_val, fps_val = get_video_settings(video_name)
|
|
|
|
| 304 |
|
| 305 |
-
return original_image_state, display_image, [],
|
|
|
|
|
|
|
|
|
|
| 306 |
|
| 307 |
except Exception as e:
|
| 308 |
print(f"Error in handle_video_upload: {e}")
|
| 309 |
-
return None, None, [],
|
|
|
|
|
|
|
|
|
|
| 310 |
|
| 311 |
def select_point(original_img: str, sel_pix: list, point_type: str, evt: gr.SelectData):
|
| 312 |
"""Handle point selection for SAM"""
|
|
@@ -533,56 +549,19 @@ def launch_viz(grid_size, vo_points, fps, original_image_state):
|
|
| 533 |
track_video_content = result.get("track_video_content", None)
|
| 534 |
track_video_filename = result.get("track_video_filename", "tracked_video.mp4")
|
| 535 |
|
| 536 |
-
#
|
| 537 |
-
|
| 538 |
-
|
|
|
|
| 539 |
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
print(f" - Contains script tags: {has_script}")
|
| 549 |
-
print(f" - HTML preview: {viz_html[:200]}...")
|
| 550 |
-
|
| 551 |
-
if not has_base64:
|
| 552 |
-
print("⚠️ Warning: HTML doesn't contain embedded base64 data!")
|
| 553 |
-
if not has_canvas:
|
| 554 |
-
print("⚠️ Warning: HTML doesn't contain canvas element!")
|
| 555 |
-
|
| 556 |
-
# 💾 保存HTML到临时文件
|
| 557 |
-
temp_viz_dir = "_viz"
|
| 558 |
-
os.makedirs(temp_viz_dir, exist_ok=True)
|
| 559 |
-
|
| 560 |
-
# 生成唯一文件名
|
| 561 |
-
timestamp = str(int(time.time() * 1000))
|
| 562 |
-
html_filename = f"viz_{timestamp}.html"
|
| 563 |
-
html_path = os.path.join(temp_viz_dir, html_filename)
|
| 564 |
-
|
| 565 |
-
# 保存HTML文件
|
| 566 |
-
with open(html_path, 'w', encoding='utf-8') as f:
|
| 567 |
-
f.write(viz_html)
|
| 568 |
-
|
| 569 |
-
print(f"✅ Saved HTML visualization to: {html_path}")
|
| 570 |
-
|
| 571 |
-
# 🖼️ 创建iframe HTML来显示保存的文件
|
| 572 |
-
iframe_html = f"""
|
| 573 |
-
<div style='border: 3px solid #667eea; border-radius: 10px; overflow: hidden; box-shadow: 0 8px 32px rgba(102, 126, 234, 0.3);'>
|
| 574 |
-
<iframe id="viz_iframe" src="/gradio_api/file={html_path}" width="100%" height="950px" style="border:none;"></iframe>
|
| 575 |
-
</div>
|
| 576 |
-
"""
|
| 577 |
-
|
| 578 |
-
else:
|
| 579 |
-
print("❌ Warning: Empty HTML content received from backend!")
|
| 580 |
-
iframe_html = """
|
| 581 |
-
<div style='border: 3px solid #ff6b6b; border-radius: 10px; padding: 20px; background-color: #fff5f5; text-align: center;'>
|
| 582 |
-
<h3 style='color: #d63031;'>❌ Empty Visualization Data</h3>
|
| 583 |
-
<p style='color: #2d3436;'>Backend returned empty HTML content.</p>
|
| 584 |
-
</div>
|
| 585 |
-
"""
|
| 586 |
|
| 587 |
# If we have base64 encoded video content, save it as a temporary file
|
| 588 |
local_video_path = None
|
|
@@ -692,7 +671,10 @@ def launch_viz(grid_size, vo_points, fps, original_image_state):
|
|
| 692 |
|
| 693 |
def clear_all():
|
| 694 |
"""Clear all buffers and temporary files"""
|
| 695 |
-
return None, None, [],
|
|
|
|
|
|
|
|
|
|
| 696 |
|
| 697 |
def update_tracker_model(model_name):
|
| 698 |
"""Update tracker model (placeholder function)"""
|
|
|
|
| 208 |
def handle_video_upload(video):
|
| 209 |
"""Handle video upload and extract first frame"""
|
| 210 |
if video is None:
|
| 211 |
+
return (None, None, [],
|
| 212 |
+
gr.update(value=50),
|
| 213 |
+
gr.update(value=756),
|
| 214 |
+
gr.update(value=3))
|
| 215 |
|
| 216 |
try:
|
| 217 |
if BACKEND_AVAILABLE and backend_client:
|
|
|
|
| 252 |
|
| 253 |
# Get video settings based on video name
|
| 254 |
video_name = get_video_name(video)
|
| 255 |
+
print(f"🎬 Video path: '{video}' -> Video name: '{video_name}'")
|
| 256 |
grid_size_val, vo_points_val, fps_val = get_video_settings(video_name)
|
| 257 |
+
print(f"🎬 Video settings for '{video_name}': grid_size={grid_size_val}, vo_points={vo_points_val}, fps={fps_val}")
|
| 258 |
|
| 259 |
+
return (original_image_state, display_image, selected_points,
|
| 260 |
+
gr.update(value=grid_size_val),
|
| 261 |
+
gr.update(value=vo_points_val),
|
| 262 |
+
gr.update(value=fps_val))
|
| 263 |
else:
|
| 264 |
print("Backend processing failed, using local fallback")
|
| 265 |
# Fallback to local processing
|
|
|
|
| 308 |
|
| 309 |
# Get video settings
|
| 310 |
video_name = get_video_name(video)
|
| 311 |
+
print(f"🎬 Local fallback - Video path: '{video}' -> Video name: '{video_name}'")
|
| 312 |
grid_size_val, vo_points_val, fps_val = get_video_settings(video_name)
|
| 313 |
+
print(f"🎬 Local fallback - Video settings for '{video_name}': grid_size={grid_size_val}, vo_points={vo_points_val}, fps={fps_val}")
|
| 314 |
|
| 315 |
+
return (original_image_state, display_image, [],
|
| 316 |
+
gr.update(value=grid_size_val),
|
| 317 |
+
gr.update(value=vo_points_val),
|
| 318 |
+
gr.update(value=fps_val))
|
| 319 |
|
| 320 |
except Exception as e:
|
| 321 |
print(f"Error in handle_video_upload: {e}")
|
| 322 |
+
return (None, None, [],
|
| 323 |
+
gr.update(value=50),
|
| 324 |
+
gr.update(value=756),
|
| 325 |
+
gr.update(value=3))
|
| 326 |
|
| 327 |
def select_point(original_img: str, sel_pix: list, point_type: str, evt: gr.SelectData):
|
| 328 |
"""Handle point selection for SAM"""
|
|
|
|
| 549 |
track_video_content = result.get("track_video_content", None)
|
| 550 |
track_video_filename = result.get("track_video_filename", "tracked_video.mp4")
|
| 551 |
|
| 552 |
+
# Save HTML to _viz directory (like local version)
|
| 553 |
+
viz_dir = './_viz'
|
| 554 |
+
os.makedirs(viz_dir, exist_ok=True)
|
| 555 |
+
random_path = f'./_viz/_{time.time()}.html'
|
| 556 |
|
| 557 |
+
with open(random_path, 'w', encoding='utf-8') as f:
|
| 558 |
+
f.write(viz_html)
|
| 559 |
+
|
| 560 |
+
# Create iframe HTML to display the saved file
|
| 561 |
+
iframe_html = f'<iframe id="viz_iframe" src="{random_path}" width="100%" height="600px" frameborder="0"></iframe>'
|
| 562 |
+
|
| 563 |
+
print(f"💾 HTML saved to: {random_path}")
|
| 564 |
+
print(f"📊 HTML content preview: {viz_html[:200]}...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 565 |
|
| 566 |
# If we have base64 encoded video content, save it as a temporary file
|
| 567 |
local_video_path = None
|
|
|
|
| 671 |
|
| 672 |
def clear_all():
|
| 673 |
"""Clear all buffers and temporary files"""
|
| 674 |
+
return (None, None, [],
|
| 675 |
+
gr.update(value=50),
|
| 676 |
+
gr.update(value=756),
|
| 677 |
+
gr.update(value=3))
|
| 678 |
|
| 679 |
def update_tracker_model(model_name):
|
| 680 |
"""Update tracker model (placeholder function)"""
|