File size: 14,410 Bytes
febf487 2529861 febf487 2529861 8b29fee febf487 2529861 febf487 2529861 febf487 8b29fee febf487 2529861 8b29fee febf487 2529861 ae06e9d febf487 2529861 febf487 2529861 febf487 2529861 febf487 2529861 febf487 2529861 febf487 2529861 ae06e9d 2529861 febf487 ae06e9d 2529861 febf487 2529861 febf487 9cf2c32 2529861 8b29fee 2529861 e332171 ae06e9d 9cf2c32 2529861 9cf2c32 2529861 ae06e9d 2529861 ae06e9d 2529861 febf487 9cf2c32 febf487 8b29fee ae06e9d 41709af ae06e9d febf487 8b29fee 41709af ae06e9d febf487 9cf2c32 febf487 2529861 fa0b66b 2529861 d35072c 2529861 d35072c 2529861 febf487 2529861 febf487 2529861 9cf2c32 2529861 e038053 2529861 8b29fee ae06e9d 2529861 8b29fee febf487 8b29fee 2529861 febf487 903257b ae06e9d febf487 ae06e9d 2529861 700094c febf487 8b29fee febf487 8b29fee e332171 8b29fee ae06e9d 8b29fee febf487 2529861 8b29fee 2529861 ae06e9d 2529861 8b29fee ae06e9d 8b29fee ae06e9d 8b29fee ae06e9d 8b29fee 2529861 febf487 8b29fee |
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 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
import os
import cv2
import torch
import numpy as np
import gradio as gr
import sys
import os
import socket
import webbrowser
sys.path.append('vggt/')
import shutil
from datetime import datetime
from demo_hf import demo_fn #, initialize_model
from omegaconf import DictConfig, OmegaConf
import glob
import gc
import time
from viser_fn import viser_wrapper
from gradio_util import demo_predictions_to_glb
from hydra.utils import instantiate
import spaces
print("Loading model")
cfg_file = "config/base.yaml"
cfg = OmegaConf.load(cfg_file)
vggt_model = instantiate(cfg, _recursive_=False)
_VGGT_URL = "https://huggingface.co./facebook/vggt_alpha/resolve/main/vggt_alpha_v0.pt"
# Reload vggt_model
pretrain_model = torch.hub.load_state_dict_from_url(_VGGT_URL)
if "vggt_model" in pretrain_model:
model_dict = pretrain_model["vggt_model"]
vggt_model.load_state_dict(model_dict, strict=False)
else:
vggt_model.load_state_dict(pretrain_model, strict=True)
print("Model loaded")
# @torch.inference_mode()
@spaces.GPU(duration=120)
def vggt_demo(
input_video,
input_image,
conf_thres=3.0,
frame_filter="all",
mask_black_bg=False,
show_cam=True,
mask_sky=False
):
start_time = time.time()
gc.collect()
torch.cuda.empty_cache()
# timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
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 input_video is not None:
if not isinstance(input_video, str):
input_video = input_video["video"]["path"]
cfg_file = "config/base.yaml"
cfg = OmegaConf.load(cfg_file)
if input_image is not None:
input_image = sorted(input_image)
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 True:
(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
else:
return None, "Uploading not finished or Incorrect input format", None, None
all_files = sorted(os.listdir(target_dir_images))
all_files = [f"{i}: {filename}" for i, filename in enumerate(all_files)]
# Update frame_filter choices
frame_filter_choices = ["All"] + all_files
print(f"Files have been copied to {target_dir_images}")
cfg.SCENE_DIR = target_dir
print("Running demo_fn")
with torch.no_grad():
predictions = demo_fn(cfg, vggt_model)
predictions["pred_extrinsic_list"] = None
print("Saving predictions")
prediction_save_path = f"{target_dir}/predictions.npz"
np.savez(prediction_save_path, **predictions)
glbfile = target_dir + f"/glbscene_{conf_thres}_{frame_filter.replace('.', '_')}_mask{mask_black_bg}_cam{show_cam}_sky{mask_sky}.glb"
glbscene = demo_predictions_to_glb(predictions, conf_thres=conf_thres, filter_by_frames=frame_filter, mask_black_bg=mask_black_bg, show_cam=show_cam, mask_sky=mask_sky, target_dir=target_dir)
glbscene.export(file_obj=glbfile)
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")
# Return None for the 3D vggt_model (since we're using viser) and the viser URL
# viser_url = f"Viser visualization is ready at: http://localhost:{viser_port}"
# print(viser_url) # Debug print
log = "Reconstruction Success. Waiting for visualization."
return glbfile, log, target_dir, gr.Dropdown(choices=frame_filter_choices, value=frame_filter, interactive=True)
def clear_fields():
"""
Return None for reconstruction_output and target_dir_output
(and optionally reset frame_filter to "All" or something else if needed).
"""
return None, None
def update_log():
"""
Return None for reconstruction_output and target_dir_output
(and optionally reset frame_filter to "All" or something else if needed).
"""
return "Loading and Reconstructing..."
def update_visualization(target_dir, conf_thres, frame_filter, mask_black_bg, show_cam, mask_sky):
# Return early if target_dir is None, "None", empty string, or otherwise invalid
# Check if the predictions file exists
predictions_path = f"{target_dir}/predictions.npz"
if target_dir is None or target_dir == "None" or target_dir == "" or not os.path.isdir(target_dir):
return None, f"No reconstruction available. Please run 'Reconstruct' first.", None
if not os.path.exists(predictions_path):
return None, f"No reconstruction available at {predictions_path}. Please run 'Reconstruct' first.", None
loaded = np.load(predictions_path, allow_pickle=True)
# predictions = np.load(f"{target_dir}/predictions.npz", allow_pickle=True)
# predictions["arr_0"]
# for key in predictions.files: print(key)
predictions = {key: loaded[key] for key in loaded.keys()}
glbfile = target_dir + f"/glbscene_{conf_thres}_{frame_filter.replace('.', '_')}_mask{mask_black_bg}_cam{show_cam}_sky{mask_sky}.glb"
if not os.path.exists(glbfile):
glbscene = demo_predictions_to_glb(predictions, conf_thres=conf_thres, filter_by_frames=frame_filter, mask_black_bg=mask_black_bg, show_cam=show_cam, mask_sky=mask_sky, target_dir=target_dir)
glbscene.export(file_obj=glbfile)
return glbfile, "Updating Visualization", target_dir
statue_video = "examples/videos/statue_video.mp4"
apple_video = "examples/videos/apple_video.mp4"
british_museum_video = "examples/videos/british_museum_video.mp4"
cake_video = "examples/videos/cake_video.mp4"
bonsai_video = "examples/videos/bonsai_video.mp4"
face_video = "examples/videos/in2n_face_video.mp4"
counter_video = "examples/videos/in2n_counter_video.mp4"
horns_video = "examples/videos/llff_horns_video.mp4"
person_video = "examples/videos/in2n_person_video.mp4"
flower_video = "examples/videos/llff_flower_video.mp4"
fern_video = "examples/videos/llff_fern_video.mp4"
drums_video = "examples/videos/drums_video.mp4"
kitchen_video = "examples/videos/kitchen_video.mp4"
room_video = "examples/videos/room_video.mp4"
# Add the new video examples
single_video = "examples/videos/single.mp4"
single_cartoon_video = "examples/videos/single_cartoon.mp4"
single_oil_painting_video = "examples/videos/single_oil_painting.mp4"
###########################################################################################
apple_images = glob.glob(f'examples/apple/images/*')
bonsai_images = glob.glob(f'examples/bonsai/images/*')
cake_images = glob.glob(f'examples/cake/images/*')
british_museum_images = glob.glob(f'examples/british_museum/images/*')
face_images = glob.glob(f'examples/in2n_face/images/*')
counter_images = glob.glob(f'examples/in2n_counter/images/*')
horns_images = glob.glob(f'examples/llff_horns/images/*')
person_images = glob.glob(f'examples/in2n_person/images/*')
flower_images = glob.glob(f'examples/llff_flower/images/*')
fern_images = glob.glob(f'examples/llff_fern/images/*')
statue_images = glob.glob(f'examples/statue/images/*')
drums_images = glob.glob(f'examples/drums/images/*')
kitchen_images = glob.glob(f'examples/kitchen/images/*')
room_images = glob.glob(f'examples/room/images/*')
single_images = glob.glob(f'examples/single/images/*')
single_cartoon_images = glob.glob(f'examples/single_cartoon/images/*')
single_oil_painting_images = glob.glob(f'examples/single_oil_painting/images/*')
###########################################################################################
with gr.Blocks(css="""
.custom-log {
font-style: italic;
font-size: 0.85em;
color: #555;
text-align: center; /* Centers the text */
}
""") as demo:
gr.Markdown("""
# ๐๏ธ VGGT: Visual Geometry Grounded Transformer
<div style="font-size: 16px; line-height: 1.5;">
<p><strong>Alpha version</strong> (under fast change)</p>
<p>Upload a video or images to create a 3D reconstruction. Once your media appears in the left panel, click the "Reconstruct" button to begin processing.</p>
<h3>Usage Tips:</h3>
<ol>
<li>After reconstruction, you can adjust the visualization by adjusting the confidence threshold, selecting specific frames to display, and so on.</li>
<li>Performance note: While the model itself processes quickly (~0.2 seconds), initial setup and visualization may take longer. First-time use requires downloading model weights, and rendering dense point clouds can be resource-intensive.</li>
<li>Known limitation: The model currently exhibits weird behavior with videos centered around human subjects. This issue is being addressed in upcoming updates.</li>
</ol>
</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)
with gr.Column(scale=3):
with gr.Column():
gr.Markdown("**3D Reconstruction (Point Cloud and Camera Poses; Zoom in to see details)**")
log_output = gr.Markdown("Ready", elem_classes=["custom-log"])
reconstruction_output = gr.Model3D(height=520, zoom_speed=0.5, pan_speed=0.5)
# 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)
# Move these controls to a new row above the log output
with gr.Row():
conf_thres = gr.Slider(minimum=0.1, maximum=10.0, value=2.0, step=0.1, label="Conf Thres")
frame_filter = gr.Dropdown(choices=["All"], value="All", label="Show Points from Frame")
with gr.Column():
show_cam = gr.Checkbox(label="Show Camera", value=True)
mask_black_bg = gr.Checkbox(label="Filter Black Background", value=False)
mask_sky = gr.Checkbox(label="Filter Sky", value=False)
# Add a hidden textbox for target_dir with default value "None"
target_dir_output = gr.Textbox(label="Target Dir", visible=False, value="None")
with gr.Row():
submit_btn = gr.Button("Reconstruct", scale=1)
# revisual_btn = gr.Button("Update Visualization", scale=1)
clear_btn = gr.ClearButton([input_video, input_images, reconstruction_output, log_output, target_dir_output], scale=1) #Modified reconstruction_output
examples = [
# [single_video, single_images, 0.5, "All", False, True, False],
[single_cartoon_video, single_cartoon_images, 0.5, "All", False, True, False],
[single_oil_painting_video, single_oil_painting_images, 0.5, "All", False, True, True],
[room_video, room_images, 1.1, "All", False, True, False],
[counter_video, counter_images, 1.5, "All", False, True, False],
[flower_video, flower_images, 1.5, "All", False, True, False],
[kitchen_video, kitchen_images, 3, "All", False, True, False],
[fern_video, fern_images, 1.5, "All", False, True, False],
# Add the new examples
]
gr.Examples(examples=examples,
inputs=[input_video, input_images, conf_thres, frame_filter, mask_black_bg, show_cam, mask_sky],
outputs=[reconstruction_output, log_output, target_dir_output, frame_filter], # Added frame_filter
fn=vggt_demo, # Use our wrapper function
cache_examples=False,
examples_per_page=50,
)
submit_btn.click(
fn=clear_fields,
inputs=[],
outputs=[reconstruction_output, target_dir_output]
).then(
fn=update_log,
inputs=[],
outputs=[log_output]
).then(
fn=vggt_demo,
inputs=[input_video, input_images, conf_thres, frame_filter, mask_black_bg, show_cam, mask_sky],
outputs=[reconstruction_output, log_output, target_dir_output, frame_filter]
)
# Add event handlers for automatic updates when parameters change
conf_thres.change(
update_visualization,
[target_dir_output, conf_thres, frame_filter, mask_black_bg, show_cam, mask_sky],
[reconstruction_output, log_output, target_dir_output],
)
frame_filter.change(
update_visualization,
[target_dir_output, conf_thres, frame_filter, mask_black_bg, show_cam, mask_sky],
[reconstruction_output, log_output, target_dir_output],
)
mask_black_bg.change(
update_visualization,
[target_dir_output, conf_thres, frame_filter, mask_black_bg, show_cam, mask_sky],
[reconstruction_output, log_output, target_dir_output],
)
show_cam.change(
update_visualization,
[target_dir_output, conf_thres, frame_filter, mask_black_bg, show_cam, mask_sky],
[reconstruction_output, log_output, target_dir_output],
)
mask_sky.change(
update_visualization,
[target_dir_output, conf_thres, frame_filter, mask_black_bg, show_cam, mask_sky],
[reconstruction_output, log_output, target_dir_output],
)
demo.queue(max_size=20).launch(show_error=True, share=True) #, share=True, server_port=7888, server_name="0.0.0.0")
# share=True
# demo.queue(max_size=20, concurrency_count=1).launch(debug=True, share=True)
########################################################################################################################
|