File size: 8,212 Bytes
66dec57 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | from __future__ import annotations
import math
from pathlib import Path
from typing import Final
import gradio as gr
from video_to_colmap import ConversionOutputs, convert_video_to_colmap_archive
APP_DIR: Final[Path] = Path(__file__).resolve().parent
OUTPUTS_DIR: Final[Path] = APP_DIR / "outputs"
OUTPUTS_DIR.mkdir(parents=True, exist_ok=True)
gr.set_static_paths(paths=[str(OUTPUTS_DIR)])
CSS: Final[str] = """
html { scrollbar-gutter: stable; }
body { overflow: auto; }
.gradio-container {
max-width: none;
width: 100%;
margin: 0;
padding: 0.75rem 1rem 1rem;
}
#main-row {
gap: 1rem;
align-items: stretch;
}
#controls-panel {
display: flex;
flex-direction: column;
gap: 0.75rem;
}
#preview-panel {
min-height: 540px;
}
.preview-placeholder {
width: 100%;
min-height: 540px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 14px;
background: linear-gradient(135deg, #111827 0%, #1f2937 100%);
border: 1px solid rgba(148, 163, 184, 0.2);
color: #e5e7eb;
}
.preview-inner {
max-width: 460px;
padding: 32px;
text-align: center;
}
.preview-title {
font-size: 20px;
font-weight: 600;
margin-bottom: 8px;
}
.preview-desc {
font-size: 14px;
line-height: 1.5;
opacity: 0.82;
}
#status-text {
font-size: 13px;
opacity: 0.92;
}
@media (max-width: 900px) {
#main-row {
flex-direction: column;
}
#preview-panel,
.preview-placeholder {
min-height: 420px;
}
}
"""
def preview_placeholder_html(title: str, description: str) -> str:
return f"""
<div class="preview-placeholder">
<div class="preview-inner">
<div class="preview-title">{title}</div>
<div class="preview-desc">{description}</div>
</div>
</div>
"""
def start_generation() -> tuple[object, object, str]:
return (
gr.update(interactive=False, value="Converting..."),
gr.update(interactive=False),
preview_placeholder_html(
"Preparing Video for COLMAP",
"Normalizing the clip, selecting sharp overlapping keyframes, and running sparse reconstruction.",
),
)
def _status_text(outputs: ConversionOutputs) -> str:
coverage = 0.0
if outputs.selected_frames:
coverage = outputs.registered_frames / outputs.selected_frames
return (
f"Prepared **{outputs.scene_name}** from a **{outputs.duration_seconds:.1f}s** clip. "
f"Selected **{outputs.selected_frames}** keyframes, COLMAP registered **{outputs.registered_frames}**, "
f"and the reconstruction quality is **{outputs.quality_label}** "
f"({math.floor(coverage * 100)}% registration)."
)
def run_conversion(
video_path: str | None,
target_frames: str,
sampling_profile: str,
max_edge: str,
) -> tuple[object, object, object, str]:
if not video_path:
raise gr.Error("Upload a video first.")
try:
outputs = convert_video_to_colmap_archive(
video_path=video_path,
target_frames=int(target_frames),
profile_key=sampling_profile,
max_image_edge=int(max_edge),
)
return (
gr.update(value=str(outputs.archive_path), visible=True, interactive=True),
gr.update(value=str(outputs.report_path), visible=True, interactive=True),
gr.update(value=str(outputs.contact_sheet_path), visible=True),
_status_text(outputs),
)
except gr.Error:
raise
except Exception as exc:
raise gr.Error(f"Conversion failed: {type(exc).__name__}: {exc}") from exc
def clear_all() -> tuple[None, object, object, object, str]:
return (
None,
gr.update(value=None, visible=False),
gr.update(value=None, visible=False),
gr.update(value=None, visible=False),
"",
)
def on_video_change(video_path: str | None) -> tuple[object, object]:
has_video = bool(video_path)
return (
gr.update(interactive=has_video, value="Build COLMAP Archive"),
gr.update(interactive=has_video),
)
def build_demo() -> gr.Blocks:
with gr.Blocks(
css=CSS,
title="Video to COLMAP for tttLRM",
theme=gr.themes.Origin(),
) as demo:
gr.Markdown("## Video to COLMAP for tttLRM")
gr.Markdown(
"Upload a single video. The Space will pick sharp overlapping keyframes, run COLMAP, and export a raw scene archive ready for the `tttLRM` Space."
)
with gr.Row(elem_id="main-row", equal_height=True):
with gr.Column(scale=3, min_width=320, elem_id="controls-panel"):
video_in = gr.File(
label="Input Video",
type="filepath",
file_types=[".mp4", ".mov", ".webm", ".mkv", ".avi"],
)
target_frames = gr.Dropdown(
label="Target Keyframes",
choices=["16", "24", "32", "48"],
value="24",
)
sampling_profile = gr.Dropdown(
label="Sampling Profile",
choices=["balanced", "dense", "sparse"],
value="balanced",
)
max_edge = gr.Dropdown(
label="Max Frame Edge",
choices=["960", "1280", "1600"],
value="1280",
)
with gr.Row():
generate_btn = gr.Button("Build COLMAP Archive", variant="primary", interactive=False)
clear_btn = gr.Button("Clear", interactive=False)
archive_download = gr.File(label="Download Raw COLMAP Archive", visible=False)
report_download = gr.File(label="Download Reconstruction Report", visible=False)
status_text = gr.Markdown(elem_id="status-text")
with gr.Column(scale=7, min_width=520):
preview_html = gr.HTML(
value=preview_placeholder_html(
"Keyframe Selection Preview",
"After conversion, the selected frames contact sheet will appear here so you can check overlap and viewpoint coverage.",
),
elem_id="preview-panel",
)
contact_sheet = gr.Image(label="Selected Keyframes", visible=False, type="filepath")
video_in.change(
on_video_change,
inputs=[video_in],
outputs=[generate_btn, clear_btn],
)
generate_btn.click(
start_generation,
outputs=[generate_btn, clear_btn, preview_html],
queue=False,
).then(
run_conversion,
inputs=[video_in, target_frames, sampling_profile, max_edge],
outputs=[archive_download, report_download, contact_sheet, status_text],
).then(
lambda: (
gr.update(interactive=True, value="Build COLMAP Archive"),
gr.update(interactive=True),
preview_placeholder_html(
"Keyframe Selection Complete",
"Review the contact sheet below and download the raw COLMAP archive for the `tttLRM` Space.",
),
),
outputs=[generate_btn, clear_btn, preview_html],
queue=False,
)
clear_btn.click(
clear_all,
outputs=[video_in, archive_download, report_download, contact_sheet, status_text],
queue=False,
).then(
lambda: (
gr.update(interactive=False),
gr.update(interactive=False),
preview_placeholder_html(
"Keyframe Selection Preview",
"After conversion, the selected frames contact sheet will appear here so you can check overlap and viewpoint coverage.",
),
),
outputs=[generate_btn, clear_btn, preview_html],
queue=False,
)
demo.queue(max_size=4)
return demo
if __name__ == "__main__":
build_demo().launch()
|