Spaces:
Running
on
Zero
Running
on
Zero
Use only global variables
Browse files- demo/mast3r_demo.py +21 -21
demo/mast3r_demo.py
CHANGED
@@ -112,7 +112,7 @@ def _convert_scene_output_to_glb(outfile, imgs, pts3d, mask, focals, cams2world,
|
|
112 |
return outfile
|
113 |
|
114 |
|
115 |
-
def get_3D_model_from_scene(
|
116 |
clean_depth=False, transparent_cams=False, cam_size=0.05, TSDF_thresh=0):
|
117 |
"""
|
118 |
extract 3D_model (glb file) from a reconstructed scene
|
@@ -139,7 +139,7 @@ def get_3D_model_from_scene(silent, scene_state, min_conf_thr=2, as_pointcloud=F
|
|
139 |
# torch.save(confs, '/app/data/confs.pt')
|
140 |
msk = to_numpy([c > min_conf_thr for c in confs])
|
141 |
return _convert_scene_output_to_glb(outfile, rgbimg, pts3d, msk, focals, cams2world, as_pointcloud=as_pointcloud,
|
142 |
-
transparent_cams=transparent_cams, cam_size=cam_size, silent=
|
143 |
|
144 |
# def save_colmap_scene(scene, save_dir, min_conf_thr=2, clean_depth=False):
|
145 |
# if 'save_pointcloud_with_normals' not in globals():
|
@@ -177,7 +177,7 @@ def get_3D_model_from_scene(silent, scene_state, min_conf_thr=2, as_pointcloud=F
|
|
177 |
# return save_path
|
178 |
|
179 |
@spaces.GPU(duration=10)
|
180 |
-
def get_reconstructed_scene(
|
181 |
filelist, optim_level, lr1, niter1, lr2, niter2, min_conf_thr, matching_conf_thr,
|
182 |
as_pointcloud, mask_sky, clean_depth, transparent_cams, cam_size, scenegraph_type, winsize,
|
183 |
win_cyclic, refid, TSDF_thresh, shared_intrinsics, **kw):
|
@@ -185,7 +185,7 @@ def get_reconstructed_scene(outdir, model, device, silent, image_size, current_s
|
|
185 |
from a list of images, run mast3r inference, sparse global aligner.
|
186 |
then run get_3D_model_from_scene
|
187 |
"""
|
188 |
-
imgs = load_images(filelist, size=image_size, verbose=not
|
189 |
if len(imgs) == 1:
|
190 |
imgs = [imgs[0], copy.deepcopy(imgs[0])]
|
191 |
imgs[1]['idx'] = 1
|
@@ -203,7 +203,7 @@ def get_reconstructed_scene(outdir, model, device, silent, image_size, current_s
|
|
203 |
if optim_level == 'coarse':
|
204 |
niter2 = 0
|
205 |
|
206 |
-
base_cache_dir = os.path.join(
|
207 |
os.makedirs(base_cache_dir, exist_ok=True)
|
208 |
def get_next_dir(base_dir):
|
209 |
run_counter = 0
|
@@ -217,11 +217,11 @@ def get_reconstructed_scene(outdir, model, device, silent, image_size, current_s
|
|
217 |
|
218 |
cache_dir = get_next_dir(base_cache_dir)
|
219 |
scene = sparse_global_alignment(filelist, pairs, cache_dir,
|
220 |
-
|
221 |
opt_depth='depth' in optim_level, shared_intrinsics=shared_intrinsics,
|
222 |
matching_conf_thr=matching_conf_thr, **kw)
|
223 |
|
224 |
-
base_colmapdata_dir = os.path.join(
|
225 |
os.makedirs(base_colmapdata_dir, exist_ok=True)
|
226 |
colmap_data_dir = get_next_dir(base_colmapdata_dir)
|
227 |
#
|
@@ -231,10 +231,10 @@ def get_reconstructed_scene(outdir, model, device, silent, image_size, current_s
|
|
231 |
current_scene_state.outfile_name is not None:
|
232 |
outfile_name = current_scene_state.outfile_name
|
233 |
else:
|
234 |
-
outfile_name = tempfile.mktemp(suffix='_scene.glb', dir=
|
235 |
|
236 |
scene_state = SparseGAState(scene, cache_dir, outfile_name)
|
237 |
-
outfile = get_3D_model_from_scene(
|
238 |
clean_depth, transparent_cams, cam_size, TSDF_thresh)
|
239 |
print(f"colmap_data_dir: {colmap_data_dir}")
|
240 |
print(f"outfile_name: {outfile_name}")
|
@@ -268,11 +268,11 @@ def set_scenegraph_options(inputfiles, win_cyclic, refid, scenegraph_type):
|
|
268 |
return win_col, winsize, win_cyclic, refid
|
269 |
|
270 |
|
271 |
-
def get_reconstructed_scene_wrapper_func(*args, **kwargs):
|
272 |
-
|
273 |
|
274 |
-
def update_3D_model_from_scene(*args, **kwargs):
|
275 |
-
|
276 |
|
277 |
def mast3r_demo_tab():
|
278 |
|
@@ -345,38 +345,38 @@ def mast3r_demo_tab():
|
|
345 |
inputs=[inputfiles, win_cyclic, refid, scenegraph_type],
|
346 |
outputs=[win_col, winsize, win_cyclic, refid])
|
347 |
run_btn.click(
|
348 |
-
fn=
|
349 |
inputs=[image_size, scene, inputfiles, optim_level, lr1, niter1, lr2, niter2, min_conf_thr, matching_conf_thr,
|
350 |
as_pointcloud, mask_sky, clean_depth, transparent_cams, cam_size, scenegraph_type, winsize,
|
351 |
win_cyclic, refid, TSDF_thresh, shared_intrinsics],
|
352 |
outputs=[scene, outmodel]
|
353 |
)
|
354 |
min_conf_thr.release(
|
355 |
-
fn=
|
356 |
inputs=[scene, min_conf_thr, as_pointcloud, mask_sky, clean_depth, transparent_cams, cam_size, TSDF_thresh],
|
357 |
outputs=outmodel
|
358 |
)
|
359 |
-
cam_size.change(fn=
|
360 |
inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,
|
361 |
clean_depth, transparent_cams, cam_size, TSDF_thresh],
|
362 |
outputs=outmodel)
|
363 |
-
TSDF_thresh.change(fn=
|
364 |
inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,
|
365 |
clean_depth, transparent_cams, cam_size, TSDF_thresh],
|
366 |
outputs=outmodel)
|
367 |
-
as_pointcloud.change(fn=
|
368 |
inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,
|
369 |
clean_depth, transparent_cams, cam_size, TSDF_thresh],
|
370 |
outputs=outmodel)
|
371 |
-
mask_sky.change(fn=
|
372 |
inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,
|
373 |
clean_depth, transparent_cams, cam_size, TSDF_thresh],
|
374 |
outputs=outmodel)
|
375 |
-
clean_depth.change(fn=
|
376 |
inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,
|
377 |
clean_depth, transparent_cams, cam_size, TSDF_thresh],
|
378 |
outputs=outmodel)
|
379 |
-
transparent_cams.change(fn=
|
380 |
inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,
|
381 |
clean_depth, transparent_cams, cam_size, TSDF_thresh],
|
382 |
outputs=outmodel)
|
|
|
112 |
return outfile
|
113 |
|
114 |
|
115 |
+
def get_3D_model_from_scene(scene_state, min_conf_thr=2, as_pointcloud=False, mask_sky=False,
|
116 |
clean_depth=False, transparent_cams=False, cam_size=0.05, TSDF_thresh=0):
|
117 |
"""
|
118 |
extract 3D_model (glb file) from a reconstructed scene
|
|
|
139 |
# torch.save(confs, '/app/data/confs.pt')
|
140 |
msk = to_numpy([c > min_conf_thr for c in confs])
|
141 |
return _convert_scene_output_to_glb(outfile, rgbimg, pts3d, msk, focals, cams2world, as_pointcloud=as_pointcloud,
|
142 |
+
transparent_cams=transparent_cams, cam_size=cam_size, silent=SILENT)
|
143 |
|
144 |
# def save_colmap_scene(scene, save_dir, min_conf_thr=2, clean_depth=False):
|
145 |
# if 'save_pointcloud_with_normals' not in globals():
|
|
|
177 |
# return save_path
|
178 |
|
179 |
@spaces.GPU(duration=10)
|
180 |
+
def get_reconstructed_scene(image_size, current_scene_state,
|
181 |
filelist, optim_level, lr1, niter1, lr2, niter2, min_conf_thr, matching_conf_thr,
|
182 |
as_pointcloud, mask_sky, clean_depth, transparent_cams, cam_size, scenegraph_type, winsize,
|
183 |
win_cyclic, refid, TSDF_thresh, shared_intrinsics, **kw):
|
|
|
185 |
from a list of images, run mast3r inference, sparse global aligner.
|
186 |
then run get_3D_model_from_scene
|
187 |
"""
|
188 |
+
imgs = load_images(filelist, size=image_size, verbose=not SILENT)
|
189 |
if len(imgs) == 1:
|
190 |
imgs = [imgs[0], copy.deepcopy(imgs[0])]
|
191 |
imgs[1]['idx'] = 1
|
|
|
203 |
if optim_level == 'coarse':
|
204 |
niter2 = 0
|
205 |
|
206 |
+
base_cache_dir = os.path.join(CACHE_PATH, 'cache')
|
207 |
os.makedirs(base_cache_dir, exist_ok=True)
|
208 |
def get_next_dir(base_dir):
|
209 |
run_counter = 0
|
|
|
217 |
|
218 |
cache_dir = get_next_dir(base_cache_dir)
|
219 |
scene = sparse_global_alignment(filelist, pairs, cache_dir,
|
220 |
+
MODEL, lr1=lr1, niter1=niter1, lr2=lr2, niter2=niter2, device=DEVICE,
|
221 |
opt_depth='depth' in optim_level, shared_intrinsics=shared_intrinsics,
|
222 |
matching_conf_thr=matching_conf_thr, **kw)
|
223 |
|
224 |
+
base_colmapdata_dir = os.path.join(CACHE_PATH, 'colmap_data')
|
225 |
os.makedirs(base_colmapdata_dir, exist_ok=True)
|
226 |
colmap_data_dir = get_next_dir(base_colmapdata_dir)
|
227 |
#
|
|
|
231 |
current_scene_state.outfile_name is not None:
|
232 |
outfile_name = current_scene_state.outfile_name
|
233 |
else:
|
234 |
+
outfile_name = tempfile.mktemp(suffix='_scene.glb', dir=CACHE_PATH)
|
235 |
|
236 |
scene_state = SparseGAState(scene, cache_dir, outfile_name)
|
237 |
+
outfile = get_3D_model_from_scene(scene_state, min_conf_thr, as_pointcloud, mask_sky,
|
238 |
clean_depth, transparent_cams, cam_size, TSDF_thresh)
|
239 |
print(f"colmap_data_dir: {colmap_data_dir}")
|
240 |
print(f"outfile_name: {outfile_name}")
|
|
|
268 |
return win_col, winsize, win_cyclic, refid
|
269 |
|
270 |
|
271 |
+
# def get_reconstructed_scene_wrapper_func(*args, **kwargs):
|
272 |
+
# return get_reconstructed_scene(CACHE_PATH, MODEL, DEVICE, SILENT, *args, **kwargs)
|
273 |
|
274 |
+
# def update_3D_model_from_scene(*args, **kwargs):
|
275 |
+
# return get_3D_model_from_scene(SILENT, *args, **kwargs)
|
276 |
|
277 |
def mast3r_demo_tab():
|
278 |
|
|
|
345 |
inputs=[inputfiles, win_cyclic, refid, scenegraph_type],
|
346 |
outputs=[win_col, winsize, win_cyclic, refid])
|
347 |
run_btn.click(
|
348 |
+
fn=get_reconstructed_scene,
|
349 |
inputs=[image_size, scene, inputfiles, optim_level, lr1, niter1, lr2, niter2, min_conf_thr, matching_conf_thr,
|
350 |
as_pointcloud, mask_sky, clean_depth, transparent_cams, cam_size, scenegraph_type, winsize,
|
351 |
win_cyclic, refid, TSDF_thresh, shared_intrinsics],
|
352 |
outputs=[scene, outmodel]
|
353 |
)
|
354 |
min_conf_thr.release(
|
355 |
+
fn=get_3D_model_from_scene,
|
356 |
inputs=[scene, min_conf_thr, as_pointcloud, mask_sky, clean_depth, transparent_cams, cam_size, TSDF_thresh],
|
357 |
outputs=outmodel
|
358 |
)
|
359 |
+
cam_size.change(fn=get_3D_model_from_scene,
|
360 |
inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,
|
361 |
clean_depth, transparent_cams, cam_size, TSDF_thresh],
|
362 |
outputs=outmodel)
|
363 |
+
TSDF_thresh.change(fn=get_3D_model_from_scene,
|
364 |
inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,
|
365 |
clean_depth, transparent_cams, cam_size, TSDF_thresh],
|
366 |
outputs=outmodel)
|
367 |
+
as_pointcloud.change(fn=get_3D_model_from_scene,
|
368 |
inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,
|
369 |
clean_depth, transparent_cams, cam_size, TSDF_thresh],
|
370 |
outputs=outmodel)
|
371 |
+
mask_sky.change(fn=get_3D_model_from_scene,
|
372 |
inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,
|
373 |
clean_depth, transparent_cams, cam_size, TSDF_thresh],
|
374 |
outputs=outmodel)
|
375 |
+
clean_depth.change(fn=get_3D_model_from_scene,
|
376 |
inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,
|
377 |
clean_depth, transparent_cams, cam_size, TSDF_thresh],
|
378 |
outputs=outmodel)
|
379 |
+
transparent_cams.change(fn=get_3D_model_from_scene,
|
380 |
inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,
|
381 |
clean_depth, transparent_cams, cam_size, TSDF_thresh],
|
382 |
outputs=outmodel)
|