Spaces:
Running
on
Zero
Running
on
Zero
dumb down demo
Browse files
app.py
CHANGED
|
@@ -12,12 +12,13 @@ import torch
|
|
| 12 |
import tempfile
|
| 13 |
import gradio
|
| 14 |
import shutil
|
|
|
|
| 15 |
|
| 16 |
HERE_PATH = path.normpath(path.dirname(__file__)) # noqa
|
| 17 |
MASt3R_REPO_PATH = path.normpath(path.join(HERE_PATH, './mast3r')) # noqa
|
| 18 |
sys.path.insert(0, MASt3R_REPO_PATH) # noqa
|
| 19 |
|
| 20 |
-
from mast3r.demo import get_reconstructed_scene
|
| 21 |
from mast3r.model import AsymmetricMASt3R
|
| 22 |
from mast3r.utils.misc import hash_md5
|
| 23 |
|
|
@@ -39,133 +40,74 @@ silent = True
|
|
| 39 |
gradio_delete_cache = 7200
|
| 40 |
|
| 41 |
|
| 42 |
-
|
| 43 |
-
def
|
| 44 |
-
|
| 45 |
-
as_pointcloud, mask_sky, clean_depth, transparent_cams, cam_size, scenegraph_type, winsize,
|
| 46 |
-
win_cyclic, refid, TSDF_thresh, shared_intrinsics, **kw):
|
| 47 |
-
return get_reconstructed_scene(tmpdirname, gradio_delete_cache, model, device, silent, image_size, current_scene_state,
|
| 48 |
-
filelist, optim_level, lr1, niter1, lr2, niter2, min_conf_thr, matching_conf_thr,
|
| 49 |
-
as_pointcloud, mask_sky, clean_depth, transparent_cams, cam_size, scenegraph_type, winsize,
|
| 50 |
-
win_cyclic, refid, TSDF_thresh, shared_intrinsics, **kw)
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
@spaces.GPU()
|
| 54 |
-
def local_get_3D_model_from_scene(scene_state, min_conf_thr=2, as_pointcloud=False, mask_sky=False,
|
| 55 |
-
clean_depth=False, transparent_cams=False, cam_size=0.05, TSDF_thresh=0):
|
| 56 |
-
return get_3D_model_from_scene(silent, scene_state, min_conf_thr, as_pointcloud, mask_sky,
|
| 57 |
-
clean_depth, transparent_cams, cam_size, TSDF_thresh)
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
recon_fun = local_get_reconstructed_scene
|
| 61 |
-
model_from_scene_fun = local_get_3D_model_from_scene
|
| 62 |
|
| 63 |
-
|
| 64 |
-
def
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
else:
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
gradio.HTML('<h2 style="text-align: center;">MASt3R Demo</h2>')
|
| 77 |
with gradio.Column():
|
| 78 |
inputfiles = gradio.File(file_count="multiple")
|
| 79 |
with gradio.Row():
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
niter1 = gradio.Number(value=500, precision=0, minimum=0, maximum=10_000,
|
| 84 |
-
label="num_iterations", info="For coarse alignment!")
|
| 85 |
-
lr2 = gradio.Slider(label="Fine LR", value=0.014, minimum=0.005, maximum=0.05, step=0.001)
|
| 86 |
-
niter2 = gradio.Number(value=200, precision=0, minimum=0, maximum=100_000,
|
| 87 |
-
label="num_iterations", info="For refinement!")
|
| 88 |
-
optim_level = gradio.Dropdown(["coarse", "refine", "refine+depth"],
|
| 89 |
-
value='refine', label="OptLevel",
|
| 90 |
-
info="Optimization level")
|
| 91 |
-
with gradio.Row():
|
| 92 |
-
matching_conf_thr = gradio.Slider(label="Matching Confidence Thr", value=5.,
|
| 93 |
-
minimum=0., maximum=30., step=0.1,
|
| 94 |
-
info="Before Fallback to Regr3D!")
|
| 95 |
-
shared_intrinsics = gradio.Checkbox(value=False, label="Shared intrinsics",
|
| 96 |
-
info="Only optimize one set of intrinsics for all views")
|
| 97 |
-
scenegraph_type = gradio.Dropdown([("complete: all possible image pairs", "complete"),
|
| 98 |
-
("swin: sliding window", "swin"),
|
| 99 |
-
("logwin: sliding window with long range", "logwin"),
|
| 100 |
-
("oneref: match one image with all", "oneref")],
|
| 101 |
-
value='complete', label="Scenegraph",
|
| 102 |
-
info="Define how to make pairs",
|
| 103 |
-
interactive=True)
|
| 104 |
-
with gradio.Column(visible=False) as win_col:
|
| 105 |
-
winsize = gradio.Slider(label="Scene Graph: Window Size", value=1,
|
| 106 |
-
minimum=1, maximum=1, step=1)
|
| 107 |
-
win_cyclic = gradio.Checkbox(value=False, label="Cyclic sequence")
|
| 108 |
-
refid = gradio.Slider(label="Scene Graph: Id", value=0,
|
| 109 |
-
minimum=0, maximum=0, step=1, visible=False)
|
| 110 |
-
run_btn = gradio.Button("Run")
|
| 111 |
-
|
| 112 |
-
with gradio.Row():
|
| 113 |
# adjust the confidence threshold
|
| 114 |
min_conf_thr = gradio.Slider(label="min_conf_thr", value=1.5, minimum=0.0, maximum=10, step=0.1)
|
| 115 |
# adjust the camera size in the output pointcloud
|
| 116 |
cam_size = gradio.Slider(label="cam_size", value=0.2, minimum=0.001, maximum=1.0, step=0.001)
|
| 117 |
-
TSDF_thresh = gradio.Slider(label="TSDF Threshold", value=0., minimum=0., maximum=1., step=0.01)
|
| 118 |
with gradio.Row():
|
| 119 |
as_pointcloud = gradio.Checkbox(value=True, label="As pointcloud")
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
transparent_cams = gradio.Checkbox(value=False, label="Transparent cameras")
|
| 124 |
-
|
| 125 |
outmodel = gradio.Model3D()
|
| 126 |
|
| 127 |
# events
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
win_cyclic.change(set_scenegraph_options,
|
| 135 |
-
inputs=[inputfiles, win_cyclic, refid, scenegraph_type],
|
| 136 |
-
outputs=[win_col, winsize, win_cyclic, refid])
|
| 137 |
-
run_btn.click(fn=recon_fun,
|
| 138 |
-
inputs=[scene, inputfiles, optim_level, lr1, niter1, lr2, niter2, min_conf_thr, matching_conf_thr,
|
| 139 |
-
as_pointcloud, mask_sky, clean_depth, transparent_cams, cam_size,
|
| 140 |
-
scenegraph_type, winsize, win_cyclic, refid, TSDF_thresh, shared_intrinsics],
|
| 141 |
-
outputs=[scene, outmodel])
|
| 142 |
-
min_conf_thr.release(fn=model_from_scene_fun,
|
| 143 |
-
inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,
|
| 144 |
-
clean_depth, transparent_cams, cam_size, TSDF_thresh],
|
| 145 |
-
outputs=outmodel)
|
| 146 |
-
cam_size.change(fn=model_from_scene_fun,
|
| 147 |
-
inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,
|
| 148 |
-
clean_depth, transparent_cams, cam_size, TSDF_thresh],
|
| 149 |
-
outputs=outmodel)
|
| 150 |
-
TSDF_thresh.change(fn=model_from_scene_fun,
|
| 151 |
-
inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,
|
| 152 |
-
clean_depth, transparent_cams, cam_size, TSDF_thresh],
|
| 153 |
-
outputs=outmodel)
|
| 154 |
-
as_pointcloud.change(fn=model_from_scene_fun,
|
| 155 |
-
inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,
|
| 156 |
-
clean_depth, transparent_cams, cam_size, TSDF_thresh],
|
| 157 |
-
outputs=outmodel)
|
| 158 |
-
mask_sky.change(fn=model_from_scene_fun,
|
| 159 |
-
inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,
|
| 160 |
-
clean_depth, transparent_cams, cam_size, TSDF_thresh],
|
| 161 |
-
outputs=outmodel)
|
| 162 |
-
clean_depth.change(fn=model_from_scene_fun,
|
| 163 |
-
inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,
|
| 164 |
-
clean_depth, transparent_cams, cam_size, TSDF_thresh],
|
| 165 |
-
outputs=outmodel)
|
| 166 |
-
transparent_cams.change(model_from_scene_fun,
|
| 167 |
-
inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,
|
| 168 |
-
clean_depth, transparent_cams, cam_size, TSDF_thresh],
|
| 169 |
-
outputs=outmodel)
|
| 170 |
demo.launch(share=None, server_name=None, server_port=None)
|
| 171 |
shutil.rmtree(tmpdirname)
|
|
|
|
| 12 |
import tempfile
|
| 13 |
import gradio
|
| 14 |
import shutil
|
| 15 |
+
import math
|
| 16 |
|
| 17 |
HERE_PATH = path.normpath(path.dirname(__file__)) # noqa
|
| 18 |
MASt3R_REPO_PATH = path.normpath(path.join(HERE_PATH, './mast3r')) # noqa
|
| 19 |
sys.path.insert(0, MASt3R_REPO_PATH) # noqa
|
| 20 |
|
| 21 |
+
from mast3r.demo import get_reconstructed_scene
|
| 22 |
from mast3r.model import AsymmetricMASt3R
|
| 23 |
from mast3r.utils.misc import hash_md5
|
| 24 |
|
|
|
|
| 40 |
gradio_delete_cache = 7200
|
| 41 |
|
| 42 |
|
| 43 |
+
class FileState:
|
| 44 |
+
def __init__(self, outfile_name=None):
|
| 45 |
+
self.outfile_name = outfile_name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
def __del__(self):
|
| 48 |
+
if self.outfile_name is not None and os.path.isfile(self.outfile_name):
|
| 49 |
+
os.remove(self.outfile_name)
|
| 50 |
+
self.outfile_name = None
|
| 51 |
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
@spaces.GPU()
|
| 54 |
+
def local_get_reconstructed_scene(filelist, min_conf_thr, matching_conf_thr,
|
| 55 |
+
as_pointcloud, cam_size,
|
| 56 |
+
shared_intrinsics, **kw):
|
| 57 |
+
lr1 = 0.07
|
| 58 |
+
niter1 = 500
|
| 59 |
+
lr2 = 0.014
|
| 60 |
+
niter2 = 200
|
| 61 |
+
optim_level = 'refine'
|
| 62 |
+
mask_sky, clean_depth, transparent_cams = False, True, False
|
| 63 |
+
if len(filelist) < 5:
|
| 64 |
+
scenegraph_type = 'complete'
|
| 65 |
+
winsize = 1
|
| 66 |
else:
|
| 67 |
+
scenegraph_type = 'logwin'
|
| 68 |
+
half_size = math.ceil((len(filelist) - 1) / 2)
|
| 69 |
+
max_winsize = max(1, math.ceil(math.log(half_size, 2)))
|
| 70 |
+
winsize = min(5, max_winsize)
|
| 71 |
+
refid = 0
|
| 72 |
+
win_cyclic = False
|
| 73 |
+
scene_state, outfile = get_reconstructed_scene(tmpdirname, gradio_delete_cache, model, device, silent, image_size, None,
|
| 74 |
+
filelist, optim_level, lr1, niter1, lr2, niter2, min_conf_thr, matching_conf_thr,
|
| 75 |
+
as_pointcloud, mask_sky, clean_depth, transparent_cams, cam_size, scenegraph_type, winsize,
|
| 76 |
+
win_cyclic, refid, TSDF_thresh=0, shared_intrinsics=shared_intrinsics, **kw)
|
| 77 |
+
filestate = FileState(scene_state.outfile_name)
|
| 78 |
+
scene_state.outfile_name = None
|
| 79 |
+
del scene_state
|
| 80 |
+
return filestate, outfile
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
css = """.gradio-container {margin: 0 !important; min-width: 100%};"""
|
| 84 |
+
title = "MASt3R Demo"
|
| 85 |
+
with gradio.Blocks(css=css, title=title, delete_cache=(gradio_delete_cache, gradio_delete_cache)) as demo:
|
| 86 |
+
filestate = gradio.State(None)
|
| 87 |
gradio.HTML('<h2 style="text-align: center;">MASt3R Demo</h2>')
|
| 88 |
with gradio.Column():
|
| 89 |
inputfiles = gradio.File(file_count="multiple")
|
| 90 |
with gradio.Row():
|
| 91 |
+
matching_conf_thr = gradio.Slider(label="Matching Confidence Thr", value=5.,
|
| 92 |
+
minimum=0., maximum=30., step=0.1,
|
| 93 |
+
info="Before Fallback to Regr3D!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
# adjust the confidence threshold
|
| 95 |
min_conf_thr = gradio.Slider(label="min_conf_thr", value=1.5, minimum=0.0, maximum=10, step=0.1)
|
| 96 |
# adjust the camera size in the output pointcloud
|
| 97 |
cam_size = gradio.Slider(label="cam_size", value=0.2, minimum=0.001, maximum=1.0, step=0.001)
|
|
|
|
| 98 |
with gradio.Row():
|
| 99 |
as_pointcloud = gradio.Checkbox(value=True, label="As pointcloud")
|
| 100 |
+
shared_intrinsics = gradio.Checkbox(value=False, label="Shared intrinsics",
|
| 101 |
+
info="Only optimize one set of intrinsics for all views")
|
| 102 |
+
run_btn = gradio.Button("Run")
|
|
|
|
|
|
|
| 103 |
outmodel = gradio.Model3D()
|
| 104 |
|
| 105 |
# events
|
| 106 |
+
run_btn.click(fn=local_get_reconstructed_scene,
|
| 107 |
+
inputs=[inputfiles, min_conf_thr, matching_conf_thr,
|
| 108 |
+
as_pointcloud,
|
| 109 |
+
cam_size, shared_intrinsics],
|
| 110 |
+
outputs=[filestate, outmodel])
|
| 111 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
demo.launch(share=None, server_name=None, server_port=None)
|
| 113 |
shutil.rmtree(tmpdirname)
|