import os import cv2 import torch import numpy as np import gradio as gr import spaces from gradio_util import vggsfm_predictions_to_glb import trimesh import sys import os sys.path.append('vggsfm_code/') import shutil from datetime import datetime from vggsfm_code.hf_demo import demo_fn from omegaconf import DictConfig, OmegaConf from viz_utils.viz_fn import add_camera, apply_density_filter_np import glob from scipy.spatial.transform import Rotation import gc import open3d as o3d import time @spaces.GPU(duration=300) def vggsfm_demo( input_video, input_image, query_frame_num, max_query_pts=4096, ): start_time = time.time() gc.collect() torch.cuda.empty_cache() debug = False timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") max_input_image = 25 target_dir = f"input_images_{timestamp}" if os.path.exists(target_dir): shutil.rmtree(target_dir) os.makedirs(target_dir) target_dir_images = target_dir + "/images" os.makedirs(target_dir_images) if debug: predictions = torch.load("predictions_scene2.pth") else: if input_video is not None: if not isinstance(input_video, str): input_video = input_video["video"]["path"] cfg_file = "vggsfm_code/cfgs/demo.yaml" cfg = OmegaConf.load(cfg_file) if input_image is not None: input_image = sorted(input_image) input_image = input_image[:max_input_image] recon_num = len(input_image) if recon_num<3: return None, "Please input at least three frames" # Copy files to the new directory for file_name in input_image: shutil.copy(file_name, target_dir_images) elif input_video is not None: vs = cv2.VideoCapture(input_video) fps = vs.get(cv2.CAP_PROP_FPS) frame_rate = 1 frame_interval = int(fps * frame_rate) video_frame_num = 0 count = 0 while video_frame_num Welcome to VGGSfM 🤗 demo! This space demonstrates 3D reconstruction from input image frames.

Get Started

To get started quickly, you can click on our examples (at the bottom of the page). The example results are cached, allowing you to view them even when in a queue. If you want to reconstruct your own data, simply **(a)** upload images (.jpg, .png, etc.) or **(b)** upload a video (.mp4, .mov, etc.).

Hyperparameters

Typically, 4 query images and 2048 query points are sufficient. For a denser point cloud, use 4096 query points. If the reconstruction appears incomplete, increase to 6 query images. Note that excessive queries can lead to out-of-memory errors.

Converting Video to Frames

By default, we convert the input video to frames at 1 frame per second. To prevent hugging face space crashes, we limit reconstruction to the first 25 frames. If both images and videos are uploaded, the demo will only reconstruct the uploaded images.

Dynamic Scenes

SfM methods are designed for rigid/static reconstruction. When dealing with dynamic/moving inputs, these methods may still work by focusing on the rigid parts of the scene. However, to ensure high-quality results, it is better to minimize the presence of moving objects in the input data.

Runtime

The reconstruction typically takes up to 90 seconds. Longer runtimes can be attributed to difficult input data or a high number of query images/points. Please note that running reconstruction on Hugging Face is slower than on a local machine. Especially when using GPU zero, it will take additional 30 seconds to start up.

Contact

If you meet any problem, feel free to create an issue in our GitHub Repo
""") with gr.Row(): with gr.Column(scale=1): input_video = gr.Video(label="Upload Video", interactive=True) input_images = gr.File(file_count="multiple", label="Upload Images", interactive=True) num_query_images = gr.Slider(minimum=1, maximum=10, step=1, value=4, label="Number of query images (key frames)", info="More query images usually lead to better reconstruction at a lower speed. If the viewpoint differences between your images are minimal, you can set this value to 1. ") num_query_points = gr.Slider(minimum=600, maximum=6000, step=1, value=2048, label="Number of query points", info="More query points usually lead to denser reconstruction at a lower speed.") with gr.Column(scale=3): reconstruction_output = gr.Model3D(label="3D Reconstruction (Point Cloud and Camera Poses; Zoom in to see details)", height=520, zoom_speed=0.5, pan_speed=0.5) log_output = gr.Textbox(label="Log") with gr.Row(): submit_btn = gr.Button("Reconstruct", scale=1) # submit_btn = gr.Button("Reconstruct", scale=1, elem_attributes={"style": "background-color: blue; color: white;"}) clear_btn = gr.ClearButton([input_video, input_images, num_query_images, num_query_points, reconstruction_output, log_output], scale=1) examples = [ [flower_video, flower_images, 2, 4096], [kitchen_video, kitchen_images, 4, 2048], [person_video, person_images, 3, 2048], [statue_video, statue_images, 4, 2048], [drums_video, drums_images, 4, 2048], [counter_video, counter_images, 4, 2048], [fern_video, fern_images, 2, 4096], [horns_video, horns_images, 3, 4096], [apple_video, apple_images, 6, 2048], # [british_museum_video, british_museum_images, 1, 4096], [bonsai_video, bonsai_images, 3, 2048], # [face_video, face_images, 4, 2048], # [cake_video, cake_images, 3, 2048], ] gr.Examples(examples=examples, inputs=[input_video, input_images, num_query_images, num_query_points], outputs=[reconstruction_output, log_output], # Provide outputs fn=vggsfm_demo, # Provide the function cache_examples=True, examples_per_page=50, ) submit_btn.click( vggsfm_demo, [input_video, input_images, num_query_images, num_query_points], [reconstruction_output, log_output], concurrency_limit=1 ) # demo.launch(debug=True, share=True) demo.queue(max_size=20).launch(show_error=True, share=True) # demo.queue(max_size=20, concurrency_count=1).launch(debug=True, share=True) ########################################################################################################################