Add bounded FFmpeg video frame sampling
Browse files
app.py
CHANGED
|
@@ -4,7 +4,10 @@ from __future__ import annotations
|
|
| 4 |
|
| 5 |
import hashlib
|
| 6 |
import json
|
|
|
|
|
|
|
| 7 |
import sys
|
|
|
|
| 8 |
import time
|
| 9 |
from pathlib import Path
|
| 10 |
|
|
@@ -37,13 +40,43 @@ def _hash_file(path):
|
|
| 37 |
return {"name": Path(path).name, "bytes": size, "sha256": digest.hexdigest()}
|
| 38 |
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
def ingest(images, videos, mode):
|
| 41 |
image_paths, video_paths = _paths(images), _paths(videos)
|
| 42 |
if not image_paths and not video_paths:
|
| 43 |
raise gr.Error("Add at least one image or video.")
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
points, colors = image_billboard_points(first)
|
| 48 |
out_dir = Path("output")
|
| 49 |
out_dir.mkdir(exist_ok=True)
|
|
@@ -55,10 +88,12 @@ def ingest(images, videos, mode):
|
|
| 55 |
"mode": mode,
|
| 56 |
"images": [_hash_file(path) for path in image_paths],
|
| 57 |
"videos": [_hash_file(path) for path in video_paths],
|
|
|
|
|
|
|
| 58 |
"preview_points": int(len(points)),
|
| 59 |
"preview_type": "2.5D image billboard; not a learned 3D reconstruction",
|
| 60 |
"limitations": [
|
| 61 |
-
"The preview places image
|
| 62 |
"No depth, camera pose, occlusion completion, or Gaussian optimization is claimed.",
|
| 63 |
"Real image/video-to-Gaussian backends must be evaluated separately on held-out views.",
|
| 64 |
],
|
|
@@ -72,7 +107,7 @@ with gr.Blocks(title="SimamAnything2GS") as demo:
|
|
| 72 |
gr.Markdown("# SimamAnything2GS\nImage/video ingest toward Gaussian reconstruction")
|
| 73 |
gr.Markdown(
|
| 74 |
"This first slice accepts arbitrary media and exports a deterministic 2.5D Gaussian-style preview. "
|
| 75 |
-
"Depth, pose estimation,
|
| 76 |
)
|
| 77 |
with gr.Row():
|
| 78 |
with gr.Column():
|
|
|
|
| 4 |
|
| 5 |
import hashlib
|
| 6 |
import json
|
| 7 |
+
import shutil
|
| 8 |
+
import subprocess
|
| 9 |
import sys
|
| 10 |
+
import tempfile
|
| 11 |
import time
|
| 12 |
from pathlib import Path
|
| 13 |
|
|
|
|
| 40 |
return {"name": Path(path).name, "bytes": size, "sha256": digest.hexdigest()}
|
| 41 |
|
| 42 |
|
| 43 |
+
def _sample_video(video_path: str, max_frames: int = 8) -> list[Path]:
|
| 44 |
+
"""Extract a bounded set of frames using the FFmpeg bundled by Spaces."""
|
| 45 |
+
frame_dir = Path(tempfile.mkdtemp(prefix="simamanything2gs_"))
|
| 46 |
+
pattern = frame_dir / "frame_%02d.jpg"
|
| 47 |
+
try:
|
| 48 |
+
subprocess.run(
|
| 49 |
+
["ffmpeg", "-hide_banner", "-loglevel", "error", "-i", str(video_path),
|
| 50 |
+
"-vf", "fps=10", "-frames:v", str(max_frames), "-q:v", "3", str(pattern)],
|
| 51 |
+
check=True,
|
| 52 |
+
capture_output=True,
|
| 53 |
+
text=True,
|
| 54 |
+
timeout=120,
|
| 55 |
+
)
|
| 56 |
+
except FileNotFoundError as exc:
|
| 57 |
+
raise gr.Error("FFmpeg is unavailable; this Space cannot sample video frames yet.") from exc
|
| 58 |
+
except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as exc:
|
| 59 |
+
detail = getattr(exc, "stderr", "") or "invalid or unreadable video"
|
| 60 |
+
raise gr.Error(f"Video frame extraction failed: {str(detail).strip()[-300:]}") from exc
|
| 61 |
+
frames = sorted(frame_dir.glob("frame_*.jpg"))
|
| 62 |
+
if not frames:
|
| 63 |
+
raise gr.Error("No readable frames were found in the uploaded video.")
|
| 64 |
+
return frames
|
| 65 |
+
|
| 66 |
+
|
| 67 |
def ingest(images, videos, mode):
|
| 68 |
image_paths, video_paths = _paths(images), _paths(videos)
|
| 69 |
if not image_paths and not video_paths:
|
| 70 |
raise gr.Error("Add at least one image or video.")
|
| 71 |
+
sampled_frames = []
|
| 72 |
+
if image_paths:
|
| 73 |
+
first_path = image_paths[0]
|
| 74 |
+
else:
|
| 75 |
+
sampled_frames = _sample_video(video_paths[0])
|
| 76 |
+
first_path = sampled_frames[0]
|
| 77 |
+
first = Image.open(first_path).convert("RGB")
|
| 78 |
+
if sampled_frames:
|
| 79 |
+
shutil.rmtree(sampled_frames[0].parent, ignore_errors=True)
|
| 80 |
points, colors = image_billboard_points(first)
|
| 81 |
out_dir = Path("output")
|
| 82 |
out_dir.mkdir(exist_ok=True)
|
|
|
|
| 88 |
"mode": mode,
|
| 89 |
"images": [_hash_file(path) for path in image_paths],
|
| 90 |
"videos": [_hash_file(path) for path in video_paths],
|
| 91 |
+
"sampled_frame_count": len(sampled_frames),
|
| 92 |
+
"sampling": "FFmpeg fps=10, max 8 frames" if sampled_frames else "not needed",
|
| 93 |
"preview_points": int(len(points)),
|
| 94 |
"preview_type": "2.5D image billboard; not a learned 3D reconstruction",
|
| 95 |
"limitations": [
|
| 96 |
+
"The preview places pixels from the first image or sampled video frame on a single plane at z=1.",
|
| 97 |
"No depth, camera pose, occlusion completion, or Gaussian optimization is claimed.",
|
| 98 |
"Real image/video-to-Gaussian backends must be evaluated separately on held-out views.",
|
| 99 |
],
|
|
|
|
| 107 |
gr.Markdown("# SimamAnything2GS\nImage/video ingest toward Gaussian reconstruction")
|
| 108 |
gr.Markdown(
|
| 109 |
"This first slice accepts arbitrary media and exports a deterministic 2.5D Gaussian-style preview. "
|
| 110 |
+
"Depth, pose estimation, and learned Gaussian optimization are explicit future adapters."
|
| 111 |
)
|
| 112 |
with gr.Row():
|
| 113 |
with gr.Column():
|