Spaces:
Running
on
Zero
Running
on
Zero
File size: 14,024 Bytes
a93af6f 5a03db1 11e8a80 5a03db1 a93af6f 5a03db1 34d287c 5a03db1 34d287c dc32bb2 5a03db1 34d287c dbccd94 d814d5a 34d287c 3a82b5b c2c23af dbccd94 b22e53a 11e8a80 a93af6f 1a8b5a6 34d287c 5a03db1 d814d5a 34d287c d814d5a 5a03db1 dbccd94 b22e53a dbccd94 7d141d7 559ce8f d814d5a dbccd94 34d287c dc32bb2 b19c7bf 34d287c dc32bb2 34d287c dbccd94 34d287c dbccd94 34d287c dbccd94 34d287c dbccd94 8f3bf53 dbccd94 d814d5a dbccd94 d814d5a dbccd94 34d287c 8f3bf53 dbccd94 d814d5a dbccd94 8f3bf53 b47a04b dbccd94 8f3bf53 dbccd94 34d287c dbccd94 34d287c d814d5a 34d287c d814d5a dbccd94 d814d5a ef862e7 34d287c dbccd94 b47a04b 5a03db1 34d287c dbccd94 34d287c 5a03db1 34d287c dbccd94 34d287c dbccd94 34d287c 5a03db1 34d287c 5a03db1 34d287c 5a03db1 34d287c 5d6ede9 dbccd94 5d6ede9 dbccd94 d814d5a b22e53a 70db994 d814d5a 3b22732 dbccd94 1160d19 3a82b5b 1160d19 3a82b5b 34d287c 3a82b5b 34d287c 427d516 70db994 3b22732 d814d5a dbccd94 1160d19 5d6ede9 1160d19 1bfa5fd 3a82b5b b22e53a 3a82b5b 427d516 3a82b5b 1bfa5fd 5d6ede9 b22e53a 8f3bf53 b22e53a 1f8b395 b22e53a 5d6ede9 b22e53a 5d6ede9 8f3bf53 9a1dda4 b22e53a 7d0f4d6 b22e53a 5d6ede9 b47a04b 5d6ede9 f6a127b 1bfa5fd f6a127b 427d516 5d6ede9 07b64a3 427d516 3a82b5b 427d516 07b64a3 3a82b5b 07b64a3 4caa7fb 3378e4a 3b22732 427d516 4ef72bc 5d6ede9 7d0f4d6 5d6ede9 1bfa5fd 6293410 5d6ede9 b19c7bf 5d6ede9 18581d3 34d287c 757e693 e161f62 757e693 5d6ede9 |
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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
import os
import cv2
import torch
import numpy as np
import gradio as gr
import spaces
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 PIL
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<max_input_image:
(gotit, frame) = vs.read()
count +=1
if not gotit:
break
if count % frame_interval == 0:
cv2.imwrite(target_dir_images+"/"+f"{video_frame_num:06}.png", frame)
video_frame_num+=1
recon_num = video_frame_num
if recon_num<3:
return None, "Please input at least three frames"
else:
return None, "Uploading not finished or Incorrect input format"
cfg.query_frame_num = query_frame_num
cfg.max_query_pts = max_query_pts
print(f"Files have been copied to {target_dir_images}")
cfg.SCENE_DIR = target_dir
# try:
predictions = demo_fn(cfg)
# except:
# return None, "Something seems to be incorrect. Please verify that your inputs are formatted correctly. If the issue persists, kindly create a GitHub issue for further assistance."
glbscene = vggsfm_predictions_to_glb(predictions)
glbfile = target_dir + "/glbscene.glb"
glbscene.export(file_obj=glbfile)
# glbscene.export(file_obj=glbfile, line_settings= {'point_size': 20})
del predictions
gc.collect()
torch.cuda.empty_cache()
print(input_image)
print(input_video)
end_time = time.time()
execution_time = end_time - start_time
print(f"Execution time: {execution_time} seconds")
# recon_num
return glbfile, f"Reconstruction complete ({recon_num} frames)"
def vggsfm_predictions_to_glb(predictions, sphere=False):
# del predictions['reconstruction']
# torch.save(predictions, "predictions_scene2.pth")
# learned from https://github.com/naver/dust3r/blob/main/dust3r/viz.py
points3D = predictions["points3D"].cpu().numpy()
points3D_rgb = predictions["points3D_rgb"].cpu().numpy()
points3D_rgb = (points3D_rgb*255).astype(np.uint8)
extrinsics_opencv = predictions["extrinsics_opencv"].cpu().numpy()
intrinsics_opencv = predictions["intrinsics_opencv"].cpu().numpy()
raw_image_paths = predictions["raw_image_paths"]
images = predictions["images"].permute(0,2,3,1).cpu().numpy()
images = (images*255).astype(np.uint8)
glbscene = trimesh.Scene()
if True:
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(points3D)
pcd.colors = o3d.utility.Vector3dVector(points3D_rgb)
cl, ind = pcd.remove_statistical_outlier(nb_neighbors=20, std_ratio=1.0)
filtered_pcd = pcd.select_by_index(ind)
print(f"Filter out {len(points3D) - len(filtered_pcd.points)} 3D points")
points3D = np.asarray(filtered_pcd.points)
points3D_rgb = np.asarray(filtered_pcd.colors)
if sphere:
# TOO SLOW
print("testing sphere")
# point_size = 0.02
else:
point_cloud = trimesh.PointCloud(points3D, colors=points3D_rgb)
glbscene.add_geometry(point_cloud)
camera_edge_colors = [(255, 0, 0), (0, 0, 255), (0, 255, 0), (255, 0, 255), (255, 204, 0), (0, 204, 204),
(128, 255, 255), (255, 128, 255), (255, 255, 128), (0, 0, 0), (128, 128, 128)]
frame_num = len(extrinsics_opencv)
extrinsics_opencv_4x4 = np.zeros((frame_num, 4, 4))
extrinsics_opencv_4x4[:, :3, :4] = extrinsics_opencv
extrinsics_opencv_4x4[:, 3, 3] = 1
for idx in range(frame_num):
cam_from_world = extrinsics_opencv_4x4[idx]
cam_to_world = np.linalg.inv(cam_from_world)
cur_cam_color = camera_edge_colors[idx % len(camera_edge_colors)]
cur_focal = intrinsics_opencv[idx, 0, 0]
add_camera(glbscene, cam_to_world, cur_cam_color, image=None, imsize=(1024,1024),
focal=None,screen_width=0.35)
opengl_mat = np.array([[1, 0, 0, 0],
[0, -1, 0, 0],
[0, 0, -1, 0],
[0, 0, 0, 1]])
rot = np.eye(4)
rot[:3, :3] = Rotation.from_euler('y', np.deg2rad(180)).as_matrix()
glbscene.apply_transform(np.linalg.inv(np.linalg.inv(extrinsics_opencv_4x4[0]) @ opengl_mat @ rot))
# Calculate the bounding box center and apply the translation
# bounding_box = glbscene.bounds
# center = (bounding_box[0] + bounding_box[1]) / 2
# translation = np.eye(4)
# translation[:3, 3] = -center
# glbscene.apply_transform(translation)
# glbfile = "glbscene.glb"
# glbscene.export(file_obj=glbfile)
return glbscene
statue_video = "vggsfm_code/examples/videos/statue_video.mp4"
apple_video = "vggsfm_code/examples/videos/apple_video.mp4"
british_museum_video = "vggsfm_code/examples/videos/british_museum_video.mp4"
cake_video = "vggsfm_code/examples/videos/cake_video.mp4"
bonsai_video = "vggsfm_code/examples/videos/bonsai_video.mp4"
face_video = "vggsfm_code/examples/videos/in2n_face_video.mp4"
counter_video = "vggsfm_code/examples/videos/in2n_counter_video.mp4"
horns_video = "vggsfm_code/examples/videos/llff_horns_video.mp4"
person_video = "vggsfm_code/examples/videos/in2n_person_video.mp4"
flower_video = "vggsfm_code/examples/videos/llff_flower_video.mp4"
fern_video = "vggsfm_code/examples/videos/llff_fern_video.mp4"
drums_video = "vggsfm_code/examples/videos/drums_video.mp4"
kitchen_video = "vggsfm_code/examples/videos/kitchen_video.mp4"
###########################################################################################
apple_images = glob.glob(f'vggsfm_code/examples/apple/images/*')
bonsai_images = glob.glob(f'vggsfm_code/examples/bonsai/images/*')
cake_images = glob.glob(f'vggsfm_code/examples/cake/images/*')
british_museum_images = glob.glob(f'vggsfm_code/examples/british_museum/images/*')
face_images = glob.glob(f'vggsfm_code/examples/in2n_face/images/*')
counter_images = glob.glob(f'vggsfm_code/examples/in2n_counter/images/*')
horns_images = glob.glob(f'vggsfm_code/examples/llff_horns/images/*')
person_images = glob.glob(f'vggsfm_code/examples/in2n_person/images/*')
flower_images = glob.glob(f'vggsfm_code/examples/llff_flower/images/*')
fern_images = glob.glob(f'vggsfm_code/examples/llff_fern/images/*')
statue_images = glob.glob(f'vggsfm_code/examples/statue/images/*')
drums_images = glob.glob(f'vggsfm_code/examples/drums/images/*')
kitchen_images = glob.glob(f'vggsfm_code/examples/kitchen/images/*')
###########################################################################################
with gr.Blocks() as demo:
gr.Markdown("""
# 🏛️ VGGSfM: Visual Geometry Grounded Deep Structure From Motion
<div style="font-size: 16px; line-height: 1.2;">
Welcome to <a href="https://vggsfm.github.io/" target="_blank" style="color: #2a9d8f;">VGGSfM</a> 🤗 demo! This space demonstrates 3D reconstruction from input image frames.
<h3 style="color: #2a9d8f;">Get Started</h3>
To get started quickly, you can click on our <strong>examples (at the bottom of the page)</strong>. 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.).
<div style="font-size: 16px; line-height: 1.2;">
<h3 style="color: #2a9d8f;">Hyperparameters</h3>
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.
<h3 style="color: #2a9d8f;">Converting Video to Frames</h3>
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.
<h3 style="color: #2a9d8f;">Dynamic Scenes</h3>
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.
<h3 style="color: #2a9d8f;">Runtime</h3>
The reconstruction typically takes <strong>up to 90 seconds</strong>. 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.
<h3 style="color: #2a9d8f;">Contact</h3>
If you meet any problem, feel free to create an issue in our <a href="https://github.com/facebookresearch/vggsfm" target="_blank" style="color: #2a9d8f;">GitHub Repo</a> ⭐
</div>
""")
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)
# demo.queue(max_size=20, concurrency_count=1).launch(debug=True, share=True)
########################################################################################################################
|