Spaces:
Running on Zero
Running on Zero
Download app.py from Charlie839242/StreamTAP3D: direct link, hf CLI and curl.
- Browser
- Download file 4.71 kB
-
https://huggingface.co/spaces/Charlie839242/StreamTAP3D/resolve/main/app.py
- Command line
-
hf download hf://spaces/Charlie839242/StreamTAP3D/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Charlie839242/StreamTAP3D/resolve/main/app.py
4.71 kB
| from pathlib import Path | |
| from typing import Any | |
| import gradio as gr | |
| import numpy as np | |
| import spaces | |
| from huggingface_hub import hf_hub_download | |
| import demo_core | |
| ROOT = Path(__file__).resolve().parent | |
| CHECKPOINT_DIR = ROOT / "checkpoints" | |
| MODEL_REPO = "Charlie839242/StreamTAP3D" | |
| EXAMPLES = [ | |
| ["assets/camel.mp4", "assets/camel_mask.png", 128, 1, 256, 4], | |
| ["assets/horse.mp4", "assets/horse_mask.png", 128, 1, 256, 4], | |
| ["assets/skating.mp4", "assets/skating_mask.png", 128, 1, 256, 4], | |
| ["assets/swing.mp4", "assets/swing_mask.png", 128, 1, 256, 4], | |
| ] | |
| def _video_path(video: Any) -> str: | |
| if video is None: | |
| raise gr.Error("Please upload or select a video.") | |
| if isinstance(video, str): | |
| return video | |
| if isinstance(video, dict): | |
| path = video.get("video") or video.get("path") or video.get("name") | |
| if path: | |
| return path | |
| if hasattr(video, "name"): | |
| return video.name | |
| raise gr.Error("Cannot read the uploaded video path.") | |
| def ensure_checkpoints() -> None: | |
| CHECKPOINT_DIR.mkdir(parents=True, exist_ok=True) | |
| for filename in ("tracker.pth", "abot_recon.safetensors"): | |
| hf_hub_download(MODEL_REPO, filename=filename, local_dir=str(CHECKPOINT_DIR)) | |
| def overlay_mask(image: np.ndarray, mask: np.ndarray, color=(36, 132, 255), alpha: float = 0.45) -> np.ndarray: | |
| image = np.asarray(image).copy() | |
| mask = demo_core.mask_to_binary(mask).astype(bool) | |
| overlay = image.copy() | |
| overlay[mask] = np.array(color, dtype=np.uint8) | |
| return np.where(mask[..., None], (image * (1.0 - alpha) + overlay * alpha).astype(np.uint8), image) | |
| def load_first_frame(video): | |
| path = _video_path(video) | |
| frame = demo_core.read_first_frame(path) | |
| return frame, None | |
| def preview_uploaded_mask(video, uploaded_mask): | |
| if video is None: | |
| return None | |
| frame = demo_core.read_first_frame(_video_path(video)) | |
| if uploaded_mask is None: | |
| return frame | |
| mask = demo_core.validate_mask_size(uploaded_mask, tuple(frame.shape[:2])) | |
| return overlay_mask(frame, mask) | |
| def run_demo(video, uploaded_mask, max_frames, frame_stride, num_points, trajectory_length): | |
| video_path = _video_path(video) | |
| if uploaded_mask is None: | |
| raise gr.Error("请上传与视频原始分辨率一致的第一帧 mask。") | |
| ensure_checkpoints() | |
| first_frame = demo_core.read_first_frame(video_path) | |
| mask = demo_core.validate_mask_size(uploaded_mask, tuple(first_frame.shape[:2])) | |
| return demo_core.run( | |
| video_path, | |
| mask, | |
| max_frames=int(max_frames), | |
| frame_stride=int(frame_stride), | |
| num_points=int(num_points), | |
| trajectory_length=int(trajectory_length), | |
| mask_margin=demo_core.DEFAULT_MASK_MARGIN, | |
| ) | |
| with gr.Blocks(title="StreamTAP3D ZeroGPU Demo") as demo: | |
| gr.Markdown( | |
| "# StreamTAP3D Demo\n" | |
| "Upload a video and a first-frame mask with exactly the same original resolution as the video. " | |
| "Free ZeroGPU quota is limited, so keep clips short." | |
| ) | |
| with gr.Row(): | |
| with gr.Column(): | |
| video = gr.Video(label="Input video", sources=["upload"]) | |
| uploaded_mask = gr.Image( | |
| label="First-frame mask (must match video resolution)", | |
| type="numpy", | |
| image_mode="RGBA", | |
| ) | |
| max_frames = gr.Slider(1, 128, value=128, step=1, label="Max frames") | |
| frame_stride = gr.Slider(1, 8, value=1, step=1, label="Frame stride") | |
| num_points = gr.Slider(1, 256, value=256, step=1, label="Tracking points") | |
| trajectory_length = gr.Slider(4, 8, value=4, step=1, label="Trajectory length") | |
| run_button = gr.Button("Run StreamTAP3D", variant="primary") | |
| with gr.Column(): | |
| first_frame = gr.Image(label="First frame / mask preview", type="numpy") | |
| output_video = gr.Video(label="Output") | |
| video.change( | |
| load_first_frame, | |
| inputs=[video], | |
| outputs=[first_frame, output_video], | |
| ) | |
| uploaded_mask.change( | |
| preview_uploaded_mask, | |
| inputs=[video, uploaded_mask], | |
| outputs=[first_frame], | |
| ) | |
| run_button.click( | |
| run_demo, | |
| inputs=[video, uploaded_mask, max_frames, frame_stride, num_points, trajectory_length], | |
| outputs=[output_video], | |
| ) | |
| gr.Examples( | |
| examples=EXAMPLES, | |
| inputs=[video, uploaded_mask, max_frames, frame_stride, num_points, trajectory_length], | |
| outputs=[output_video], | |
| fn=run_demo, | |
| cache_examples=False, | |
| ) | |
| if __name__ == "__main__": | |
| ensure_checkpoints() | |
| demo.queue(max_size=4).launch() | |