Spaces:
Sleeping
Sleeping
File size: 8,161 Bytes
899c526 a8c8616 899c526 a8c8616 50081a5 a8c8616 899c526 a8c8616 899c526 a8c8616 899c526 a8c8616 899c526 a8c8616 899c526 a8c8616 899c526 a8c8616 899c526 a8c8616 899c526 a8c8616 899c526 0e99e24 a8c8616 899c526 a8c8616 899c526 a8c8616 899c526 a8c8616 899c526 a8c8616 0e99e24 a8c8616 899c526 0e99e24 a8c8616 899c526 a8c8616 0e99e24 |
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 |
import gradio as gr
from gradio_rerun import Rerun
import rerun as rr
import rerun.blueprint as rrb
import mmcv
from timeit import default_timer as timer
from typing import Literal
from mini_dpvo.config import cfg as base_cfg
from mini_dpvo.api.inference import (
log_trajectory,
calib_from_dust3r,
create_reader,
calculate_num_frames,
)
import torch
import numpy as np
from pathlib import Path
from multiprocessing import Process, Queue
from mini_dpvo.dpvo import DPVO
from jaxtyping import UInt8, Float64, Float32
from mini_dust3r.model import AsymmetricCroCo3DStereo
from tqdm import tqdm
import tyro
from dataclasses import dataclass
if gr.NO_RELOAD:
NETWORK_PATH = "checkpoints/dpvo.pth"
DEVICE = (
"mps"
if torch.backends.mps.is_available()
else "cuda"
if torch.cuda.is_available()
else "cpu"
)
MODEL = AsymmetricCroCo3DStereo.from_pretrained(
"nielsr/DUSt3R_ViTLarge_BaseDecoder_512_dpt"
).to(DEVICE)
@dataclass
class GradioDPVOConfig:
share: bool = False
@rr.thread_local_stream("mini_dpvo")
@torch.no_grad()
def run_dpvo(
video_file_path: str,
jpg_quality: str,
stride: int = 1,
skip: int = 0,
config_type: Literal["accurate", "fast"] = "accurate",
progress=gr.Progress(),
):
# create a stream to send data back to the rerun viewer
stream = rr.binary_stream()
parent_log_path = Path("world")
rr.log(f"{parent_log_path}", rr.ViewCoordinates.RDF, timeless=True)
blueprint = rrb.Blueprint(
collapse_panels=True,
)
rr.send_blueprint(blueprint)
if config_type == "accurate":
base_cfg.merge_from_file("config/default.yaml")
elif config_type == "fast":
base_cfg.merge_from_file("config/fast.yaml")
else:
raise ValueError("Invalid config type")
base_cfg.BUFFER_SIZE = 2048
slam = None
start_time = timer()
queue = Queue(maxsize=8)
reader: Process = create_reader(video_file_path, None, stride, skip, queue)
reader.start()
# get the first frame
progress(progress=0.1, desc="Estimating Camera Intrinsics")
_, bgr_hw3, _ = queue.get()
K_33_pred = calib_from_dust3r(bgr_hw3, MODEL, DEVICE)
intri_np: Float64[np.ndarray, "4"] = np.array(
[K_33_pred[0, 0], K_33_pred[1, 1], K_33_pred[0, 2], K_33_pred[1, 2]]
)
num_frames = calculate_num_frames(video_file_path, stride, skip)
path_list = []
with tqdm(total=num_frames, desc="Processing Frames") as pbar:
while True:
timestep: int
bgr_hw3: UInt8[np.ndarray, "h w 3"]
intri_np: Float64[np.ndarray, "4"]
(timestep, bgr_hw3, _) = queue.get()
# queue will have a (-1, image, intrinsics) tuple when the reader is done
if timestep < 0:
break
rr.set_time_sequence(timeline="timestep", sequence=timestep)
bgr_3hw: UInt8[torch.Tensor, "h w 3"] = (
torch.from_numpy(bgr_hw3).permute(2, 0, 1).cuda()
)
intri_torch: Float64[torch.Tensor, "4"] = torch.from_numpy(intri_np).cuda()
if slam is None:
_, h, w = bgr_3hw.shape
slam = DPVO(base_cfg, NETWORK_PATH, ht=h, wd=w)
slam(timestep, bgr_3hw, intri_torch)
pbar.update(1)
if slam.is_initialized:
poses: Float32[torch.Tensor, "buffer_size 7"] = slam.poses_
points: Float32[torch.Tensor, "buffer_size*num_patches 3"] = (
slam.points_
)
colors: UInt8[torch.Tensor, "buffer_size num_patches 3"] = slam.colors_
path_list = log_trajectory(
parent_log_path,
poses,
points,
colors,
intri_np,
bgr_hw3,
path_list,
jpg_quality,
)
yield stream.read(), timer() - start_time
def on_file_upload(video_file_path: str) -> None:
video_reader = mmcv.VideoReader(video_file_path)
video_info = f"""
**Video Info:**
- Number of Frames: {video_reader.frame_cnt}
- FPS: {round(video_reader.fps)}
"""
return video_info
def main(gradio_config: GradioDPVOConfig):
with gr.Blocks(
css=""".gradio-container {margin: 0 !important; min-width: 100%};""",
title="Mini-DPVO Demo",
) as demo:
# scene state is save so that you can change conf_thr, cam_size... without rerunning the inference
gr.HTML('<h2 style="text-align: center;">Mini-DPVO Demo</h2>')
gr.HTML(
'<p style="text-align: center;">Unofficial DPVO demo using the mini-dpvo. Learn more about mini-dpvo <a href="https://github.com/pablovela5620/mini-dpvo">here</a>.</p>'
)
with gr.Column():
with gr.Row():
video_input = gr.File(
height=100,
file_count="single",
file_types=[".mp4", ".mov", ".MOV", ".webm"],
label="Video File",
)
with gr.Column():
video_info = gr.Markdown(
value="""
**Video Info:**
"""
)
time_taken = gr.Number(
label="Time Taken (s)", precision=2, interactive=False
)
with gr.Accordion(label="Advanced", open=False):
with gr.Row():
jpg_quality = gr.Radio(
label="JPEG Quality %: Lower quality means faster streaming",
choices=[10, 50, 90],
value=90,
type="value",
)
stride = gr.Slider(
label="Stride: How many frames to sample between each prediction",
minimum=1,
maximum=5,
step=1,
value=5,
)
skip = gr.Number(
label="Skip: How many frames to skip at the beginning",
value=0,
precision=0,
)
config_type = gr.Dropdown(
label="Config Type: Choose between accurate and fast",
value="fast",
choices=["accurate", "fast"],
max_choices=1,
)
with gr.Row():
start_btn = gr.Button("Run")
stop_btn = gr.Button("Stop")
rr_viewer = Rerun(height=600, streaming=True)
# Example videos
base_example_params = [50, 4, 0, "fast"]
example_dpvo_dir = Path("data/movies")
example_iphone_dir = Path("data/iphone")
example_video_paths = sorted(example_iphone_dir.glob("*.MOV")) + sorted(
example_dpvo_dir.glob("*.MOV")
)
example_video_paths = [str(path) for path in example_video_paths]
gr.Examples(
examples=[[path, *base_example_params] for path in example_video_paths],
inputs=[video_input, jpg_quality, stride, skip, config_type],
outputs=[rr_viewer],
fn=run_dpvo,
cache_examples=False,
)
click_event = start_btn.click(
fn=run_dpvo,
inputs=[video_input, jpg_quality, stride, skip, config_type],
outputs=[rr_viewer, time_taken],
)
stop_btn.click(
fn=None,
inputs=[],
outputs=[],
cancels=[click_event],
)
video_input.upload(
fn=on_file_upload, inputs=[video_input], outputs=[video_info]
)
demo.launch(share=gradio_config.share)
if __name__ == "__main__":
main(tyro.cli(GradioDPVOConfig))
|