import os import cv2 import torch import shutil from datetime import datetime import glob import gc import gradio as gr import numpy as np import open3d as o3d import concerto from scipy.spatial.transform import Rotation as R import trimesh import time from typing import List, Tuple from pathlib import Path from einops import rearrange from tqdm import tqdm import camtools as ct from PIL import Image from torchvision import transforms as TF try: import flash_attn except ImportError: flash_attn = None from visual_util import predictions_to_glb from vggt.models.vggt import VGGT from vggt.utils.load_fn import load_and_preprocess_images from vggt.utils.pose_enc import pose_encoding_to_extri_intri from vggt.utils.geometry import unproject_depth_map_to_point_map device = "cuda" if torch.cuda.is_available() else "cpu" def run_model(target_dir, model) -> dict: """ Run the VGGT model on images in the 'target_dir/images' folder and return predictions. """ print(f"Processing images from {target_dir}") # if not torch.cuda.is_available(): # raise ValueError("CUDA is not available. Check your environment.") # Move model to device model = model.to(device) model.eval() # Load and preprocess images image_names = glob.glob(os.path.join(target_dir, "images", "*")) image_names = sorted(image_names) print(f"Found {len(image_names)} images") if len(image_names) == 0: raise ValueError("No images found. Check your upload.") images = load_and_preprocess_images(image_names).to(device) print(f"Preprocessed images shape: {images.shape}") # Run inference print("Running inference...") with torch.no_grad(): if device == "cuda": with torch.cuda.amp.autocast(dtype=torch.bfloat16): predictions = model(images) else: predictions = model(images) # Convert pose encoding to extrinsic and intrinsic matrices print("Converting pose encoding to extrinsic and intrinsic matrices...") extrinsic, intrinsic = pose_encoding_to_extri_intri(predictions["pose_enc"], images.shape[-2:]) predictions["extrinsic"] = extrinsic predictions["intrinsic"] = intrinsic # Convert tensors to numpy for key in predictions.keys(): if isinstance(predictions[key], torch.Tensor): predictions[key] = predictions[key].cpu().numpy().squeeze(0) # remove batch dimension # Generate world points from depth map print("Computing world points from depth map...") depth_map = predictions["depth"] # (S, H, W, 1) world_points = unproject_depth_map_to_point_map(depth_map, predictions["extrinsic"], predictions["intrinsic"]) predictions["world_points_from_depth"] = world_points # Clean up torch.cuda.empty_cache() return predictions def handle_uploads(input_file,input_video,conf_thres,frame_slider,prediction_mode,if_TSDF): """ Create a new 'target_dir' + 'images' subfolder, and place user-uploaded images or extracted frames from video into it. Return (target_dir, image_paths). """ start_time = time.time() gc.collect() torch.cuda.empty_cache() # Create a unique folder name timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f") target_dir = f"demo_output/inputs_{timestamp}" target_dir_images = os.path.join(target_dir, "images") target_dir_pcds = os.path.join(target_dir, "pcds") # Clean up if somehow that folder already exists if os.path.exists(target_dir): shutil.rmtree(target_dir) os.makedirs(target_dir) os.makedirs(target_dir_images) os.makedirs(target_dir_pcds) # --- Handle video --- if input_video is not None: print("processing video") if isinstance(input_video, dict) and "name" in input_video: video_path = input_video["name"] else: video_path = input_video vs = cv2.VideoCapture(video_path) fps = vs.get(cv2.CAP_PROP_FPS) frame_interval = int(fps * frame_slider) # 1 frame/sec count = 0 video_frame_num = 0 image_paths = [] while True: gotit, frame = vs.read() if not gotit: break count += 1 if count % frame_interval == 0: image_path = os.path.join(target_dir_images, f"{video_frame_num:06}.png") cv2.imwrite(image_path, frame) image_paths.append(image_path) video_frame_num += 1 # Sort final images for gallery image_paths = sorted(image_paths) original_points, original_colors, original_normals = parse_frames(target_dir,conf_thres,prediction_mode,if_TSDF) if input_file is not None: print("processing ply") pcd = o3d.io.read_point_cloud(input_file.name) pcd.estimate_normals() original_points = np.asarray(pcd.points) original_colors = np.asarray(pcd.colors) original_normals = np.asarray(pcd.normals) image_paths = None scene_3d = trimesh.Scene() point_cloud_data = trimesh.PointCloud(vertices=original_points, colors=original_colors, vertex_normals=original_normals) scene_3d.add_geometry(point_cloud_data) original_temp = os.path.join(target_dir_pcds,"original.glb") scene_3d.export(file_obj=original_temp) np.save(os.path.join(target_dir_pcds, f"points.npy"), original_points) np.save(os.path.join(target_dir_pcds, f"colors.npy"), original_colors) np.save(os.path.join(target_dir_pcds, f"normals.npy"), original_normals) end_time = time.time() print(f"Files copied to {target_dir}; took {end_time - start_time:.3f} seconds") return target_dir, image_paths,original_temp, end_time - start_time def update_gallery_on_upload(input_file,input_video,conf_thres,frame_slider,prediction_mode,TSDF_mode): """ Whenever user uploads or changes files, immediately handle them and show in the gallery. Return (target_dir, image_paths). If nothing is uploaded, returns "None" and empty list. """ if not input_video and not input_file: return None, None, None, None if_TSDF = True if TSDF_mode=="Yes" else False target_dir, image_paths,original_view, reconstruction_time = handle_uploads(input_file,input_video,conf_thres,frame_slider,prediction_mode,if_TSDF) if input_file is not None: return original_view, target_dir, [], f"Upload and preprocess complete with {reconstruction_time:.3f} sec. Click \"PCA Generate\" to begin PCA processing." if input_video is not None: return original_view, target_dir, image_paths, f"Upload and preprocess complete with {reconstruction_time:.3f} sec. Click \"PCA Generate\" to begin PCA processing." def clear_fields(): """ Clears the 3D viewer, the stored target_dir, and empties the gallery. """ return None def PCAing_log(is_example, log_output): """ Display a quick log message while waiting. """ if is_example: return log_output return "Loading for Doing PCA..." def reset_log(): """ Reset a quick log message. """ return "A new point cloud file or video is uploading and preprocessing..." def parse_frames( target_dir, conf_thres=3.0, prediction_mode="Pointmap Regression", if_TSDF=True, ): """ Perform reconstruction using the already-created target_dir/images. """ if not os.path.isdir(target_dir) or target_dir == "None": return None, "No valid target directory found. Please upload first.", None, None start_time = time.time() gc.collect() torch.cuda.empty_cache() # Prepare frame_filter dropdown target_dir_images = os.path.join(target_dir, "images") target_dir_pcds = os.path.join(target_dir, "pcds") all_files = sorted(os.listdir(target_dir_images)) if os.path.isdir(target_dir_images) else [] all_files = [f"{i}: {filename}" for i, filename in enumerate(all_files)] frame_filter_choices = ["All"] + all_files print("Running run_model...") with torch.no_grad(): predictions = run_model(target_dir, VGGT_model) # Save predictions prediction_save_path = os.path.join(target_dir, "predictions.npz") np.savez(prediction_save_path, **predictions) # Convert pose encoding to extrinsic and intrinsic matrices images = predictions["images"] Ts, Ks = predictions["extrinsic"],predictions["intrinsic"] Ts = ct.convert.pad_0001(Ts) Ts_inv = np.linalg.inv(Ts) Cs = np.array([ct.convert.T_to_C(T) for T in Ts]) # (n, 3) # [1, 8, 294, 518, 3] world_points = predictions["world_points"] # Compute view direction for each pixel # (b n h w c) - (n, 3) view_dirs = world_points - rearrange(Cs, "n c -> n 1 1 c") view_dirs = rearrange(view_dirs, "n h w c -> (n h w) c") view_dirs = view_dirs / np.linalg.norm(view_dirs, axis=-1, keepdims=True) # Extract points and colors # [1, 8, 3, 294, 518] img_num = world_points.shape[1] images = predictions["images"] points = rearrange(world_points, "n h w c -> (n h w) c") colors = rearrange(images, "n c h w -> (n h w) c") if prediction_mode=="Pointmap Branch": world_points_conf = predictions["world_points_conf"] conf = world_points_conf.reshape(-1) points,Ts_inv,_ = Coord2zup(points, Ts_inv) scale = 3 / (points[:, 2].max() - points[:, 2].min()) points *= scale Ts_inv[:, :3, 3] *= scale # Create a point cloud pcd = o3d.geometry.PointCloud() pcd.points = o3d.utility.Vector3dVector(points) pcd.colors = o3d.utility.Vector3dVector(colors) pcd.estimate_normals() # o3d.io.write_point_cloud("pcd.ply", pcd) try: pcd, inliers, rotation_matrix, offset = extract_and_align_ground_plane(pcd) except Exception as e: print(f"cannot find ground, err:{e}") # Filp normals such that normals always point to camera # Compute the dot product between the normal and the view direction # If the dot product is less than 0, flip the normal normals = np.asarray(pcd.normals) view_dirs = np.asarray(view_dirs) dot_product = np.sum(normals * view_dirs, axis=-1) flip_mask = dot_product > 0 normals[flip_mask] = -normals[flip_mask] # Normalize normals a nd m normals = normals / np.linalg.norm(normals, axis=-1, keepdims=True) pcd.normals = o3d.utility.Vector3dVector(normals) if conf_thres == 0.0: conf_threshold = 0.0 else: conf_threshold = np.percentile(conf, conf_thres) conf_mask = (conf >= conf_threshold) & (conf > 1e-5) points = points[conf_mask] colors = colors[conf_mask] normals = normals[conf_mask] elif prediction_mode=="Depthmap Branch": # Integrate RGBD images into a TSDF volume and extract a mesh # (n, h, w, 3) im_colors = rearrange(images, "n c h w -> (n) h w c") # (b, n, h, w, 3) im_dists = world_points - rearrange(Cs, "n c -> n 1 1 c") im_dists = np.linalg.norm(im_dists, axis=-1, keepdims=False) # Convert distance to depth im_depths = [] # (n, h, w, c) for im_dist, K in zip(im_dists, Ks): im_depth = ct.convert.im_distance_to_im_depth(im_dist, K) im_depths.append(im_depth) im_depths = np.stack(im_depths, axis=0) if if_TSDF: mesh = integrate_rgbd_to_mesh( Ks=Ks, Ts=Ts, im_depths=im_depths, im_colors=im_colors, voxel_size=1 / 512, ) rotation_angle = -np.pi / 2 rotation_axis = np.array([1, 0, 0]) # X 轴 mesh.rotate( o3d.geometry.get_rotation_matrix_from_axis_angle(rotation_axis * rotation_angle), center=(0,0,0) ) vertices = np.asarray(mesh.vertices) scale_factor = 3./(np.max(vertices[:,2])-np.min(vertices[:,2])) mesh.scale(scale_factor, center=(0,0,0)) points = np.asarray(mesh.vertices) colors = np.asarray(mesh.vertex_colors) if mesh.has_vertex_colors() else np.zeros_like(vertices) if not mesh.has_vertex_normals(): mesh.compute_vertex_normals() normals = np.asarray(mesh.vertex_normals) Ts_inv = rotx(Ts_inv, theta=-90) Ts_inv[:, :3, 3] *= scale_factor pcd = o3d.geometry.PointCloud() pcd.points = o3d.utility.Vector3dVector(points) pcd.colors = o3d.utility.Vector3dVector(colors) pcd.normals = o3d.utility.Vector3dVector(normals) else: points=[] for K, T, im_depth in zip(Ks, Ts, im_depths): point = ct.project.im_depth_to_point_cloud( im_depth=im_depth, K=K, T=T, to_image=False, ignore_invalid=False, ) points.append(point) points = np.vstack(points) colors = im_colors.reshape(-1,3) world_points_conf = predictions["depth_conf"] conf = world_points_conf.reshape(-1) if conf_thres == 0.0: conf_threshold = 0.0 else: conf_threshold = np.percentile(conf, conf_thres) conf_mask = (conf >= conf_threshold) & (conf > 1e-5) points = points[conf_mask] colors = colors[conf_mask] points,Ts_inv,_ = Coord2zup(points, Ts_inv) scale_factor = 3./(np.max(points[:,2])-np.min(points[:,2])) points *= scale_factor Ts_inv[:, :3, 3] *= scale_factor pcd = o3d.geometry.PointCloud() pcd.points = o3d.utility.Vector3dVector(points) pcd.colors = o3d.utility.Vector3dVector(colors) pcd.estimate_normals() try: pcd, inliers, rotation_matrix, offset = extract_and_align_ground_plane(pcd) except Exception as e: print(f"cannot find ground, err:{e}") original_points = np.asarray(pcd.points) original_colors = np.asarray(pcd.colors) original_normals = np.asarray(pcd.normals) # Cleanup del predictions gc.collect() torch.cuda.empty_cache() end_time = time.time() print(f"Total time: {end_time - start_time:.2f} seconds") return original_points, original_colors, original_normals def extract_and_align_ground_plane(pcd, height_percentile=20, ransac_distance_threshold=0.01, ransac_n=3, ransac_iterations=1000, max_angle_degree=40, max_trials=6): points = np.asarray(pcd.points) z_vals = points[:, 2] z_thresh = np.percentile(z_vals, height_percentile) low_indices = np.where(z_vals <= z_thresh)[0] remaining_indices = low_indices.copy() for trial in range(max_trials): if len(remaining_indices) < ransac_n: raise ValueError("Not enough points left to fit a plane.") low_pcd = pcd.select_by_index(remaining_indices) plane_model, inliers = low_pcd.segment_plane( distance_threshold=ransac_distance_threshold, ransac_n=ransac_n, num_iterations=ransac_iterations) a, b, c, d = plane_model normal = np.array([a, b, c]) normal /= np.linalg.norm(normal) # current_plane_pcd = pcd.select_by_index(remaining_indices[inliers]) # o3d.io.write_point_cloud("plane.ply",current_plane_pcd) # exit() angle = np.arccos(np.clip(np.dot(normal, [0, 0, 1]), -1.0, 1.0)) * 180 / np.pi if angle <= max_angle_degree: inliers_global = remaining_indices[inliers] target = np.array([0, 0, 1]) axis = np.cross(normal, target) axis_norm = np.linalg.norm(axis) if axis_norm < 1e-6: rotation_matrix = np.eye(3) else: axis /= axis_norm rot_angle = np.arccos(np.clip(np.dot(normal, target), -1.0, 1.0)) rotation = R.from_rotvec(axis * rot_angle) rotation_matrix = rotation.as_matrix() rotated_points = points @ rotation_matrix.T ground_points_z = rotated_points[inliers_global, 2] offset = np.mean(ground_points_z) rotated_points[:, 2] -= offset aligned_pcd = o3d.geometry.PointCloud() aligned_pcd.points = o3d.utility.Vector3dVector(rotated_points) if pcd.has_colors(): aligned_pcd.colors = pcd.colors if pcd.has_normals(): rotated_normals = np.asarray(pcd.normals) @ rotation_matrix.T aligned_pcd.normals = o3d.utility.Vector3dVector(rotated_normals) return aligned_pcd, inliers_global, rotation_matrix, offset else: rejected_indices = remaining_indices[inliers] remaining_indices = np.setdiff1d(remaining_indices, rejected_indices) raise ValueError("Failed to find a valid ground plane within max trials.") def rotx(x, theta=90): """ Rotate x by theta degrees around the x-axis """ theta = np.deg2rad(theta) rot_matrix = np.array( [ [1, 0, 0, 0], [0, np.cos(theta), -np.sin(theta), 0], [0, np.sin(theta), np.cos(theta), 0], [0, 0, 0, 1], ] ) return rot_matrix@ x def Coord2zup(points, extrinsics, normals = None): """ Convert the dust3r coordinate system to the z-up coordinate system """ points = np.concatenate([points, np.ones([points.shape[0], 1])], axis=1).T points = rotx(points, -90)[:3].T if normals is not None: normals = np.concatenate([normals, np.ones([normals.shape[0], 1])], axis=1).T normals = rotx(normals, -90)[:3].T normals = normals / np.linalg.norm(normals, axis=1, keepdims=True) t = np.min(points,axis=0) points -= t extrinsics = rotx(extrinsics, -90) extrinsics[:, :3, 3] -= t.T return points, extrinsics, normals def integrate_rgbd_to_mesh( Ks, Ts, im_depths, im_colors, voxel_size, bbox=None, ): """ Integrate RGBD images into a TSDF volume and extract a mesh. Args: Ks: (N, 3, 3) camera intrinsics. Ts: (N, 4, 4) camera extrinsics. im_depths: (N, H, W) depth images, already in world scale. im_colors: (N, H, W, 3) color images, float range in [0, 1]. voxel_size: TSDF voxel size, in meters, e.g. 3 / 512. bbox: Open3D axis-aligned bounding box, for cropping. Per Open3D convention, invalid depth values shall be set to 0. """ num_images = len(Ks) if ( len(Ts) != num_images or len(im_depths) != num_images or len(im_colors) != num_images ): raise ValueError("Ks, Ts, im_depths, im_colors must have the same length.") # Constants. trunc_voxel_multiplier = 8.0 sdf_trunc = trunc_voxel_multiplier * voxel_size volume = o3d.pipelines.integration.ScalableTSDFVolume( voxel_length=voxel_size, sdf_trunc=sdf_trunc, color_type=o3d.pipelines.integration.TSDFVolumeColorType.RGB8, ) for K, T, im_depth, im_color in tqdm( zip(Ks, Ts, im_depths, im_colors), total=len(Ks), desc="Integrating RGBD frames", ): # Set invalid depth values to 0, based on bounding box. if bbox is not None: points = ct.project.im_depth_to_point_cloud( im_depth=im_depth, K=K, T=T, to_image=False, ignore_invalid=False, ) assert len(points) == im_depth.shape[0] * im_depth.shape[1] point_indices_inside_bbox = bbox.get_point_indices_within_bounding_box( o3d.utility.Vector3dVector(points) ) point_indices_outside_bbox = np.setdiff1d( np.arange(len(points)), point_indices_inside_bbox ) im_depth.ravel()[point_indices_outside_bbox] = 0 im_color_uint8 = np.ascontiguousarray((im_color * 255).astype(np.uint8)) im_depth_uint16 = np.ascontiguousarray((im_depth * 1000).astype(np.uint16)) im_color_o3d = o3d.geometry.Image(im_color_uint8) im_depth_o3d = o3d.geometry.Image(im_depth_uint16) im_rgbd_o3d = o3d.geometry.RGBDImage.create_from_color_and_depth( im_color_o3d, im_depth_o3d, depth_scale=1000.0, depth_trunc=10.0, convert_rgb_to_intensity=False, ) o3d_intrinsic = o3d.camera.PinholeCameraIntrinsic( width=im_depth.shape[1], height=im_depth.shape[0], fx=K[0, 0], fy=K[1, 1], cx=K[0, 2], cy=K[1, 2], ) o3d_extrinsic = T volume.integrate( im_rgbd_o3d, o3d_intrinsic, o3d_extrinsic, ) mesh = volume.extract_triangle_mesh() return mesh def get_pca_color(feat, start = 0, brightness=1.25, center=True): u, s, v = torch.pca_lowrank(feat, center=center, q=3*(start+1), niter=5) projection = feat @ v projection = projection[:, 3*start:3*(start+1)] * 0.6 + projection[:, 3*start:3*(start+1)] * 0.4 min_val = projection.min(dim=-2, keepdim=True)[0] max_val = projection.max(dim=-2, keepdim=True)[0] div = torch.clamp(max_val - min_val, min=1e-6) color = (projection - min_val) / div * brightness color = color.clamp(0.0, 1.0) return color def Concerto_process(target_dir, original_points, original_colors, original_normals, slider_value, bright_value, model_type): gc.collect() torch.cuda.empty_cache() target_dir_pcds = os.path.join(target_dir, "pcds") point = {"coord": original_points, "color": original_colors, "normal":original_normals} original_coord = point["coord"].copy() original_color = point["color"].copy() point = transform(point) with torch.inference_mode(): for key in point.keys(): if isinstance(point[key], torch.Tensor) and device=="cuda": point[key] = point[key].cuda(non_blocking=True) # model forward: concerto_start_time = time.time() if model_type =="Concerto": point = concerto_model(point) elif model_type == "Sonata": point = sonata_model(point) concerto_end_time = time.time() # upcast point feature # Point is a structure contains all the information during forward for _ in range(2): assert "pooling_parent" in point.keys() assert "pooling_inverse" in point.keys() parent = point.pop("pooling_parent") inverse = point.pop("pooling_inverse") parent.feat = torch.cat([parent.feat, point.feat[inverse]], dim=-1) point = parent while "pooling_parent" in point.keys(): assert "pooling_inverse" in point.keys() parent = point.pop("pooling_parent") inverse = point.pop("pooling_inverse") parent.feat = point.feat[inverse] point = parent # here point is down-sampled by GridSampling in default transform pipeline # feature of point cloud in original scale can be acquired by: _ = point.feat[point.inverse] # PCA point_feat = point.feat.cpu().detach().numpy() np.save(os.path.join(target_dir_pcds,"feat.npy"),point_feat) pca_start_time = time.time() pca_color = get_pca_color(point.feat,start = slider_value, brightness=bright_value, center=True) pca_end_time = time.time() # inverse back to original scale before grid sampling # point.inverse is acquired from the GirdSampling transform point_inverse = point.inverse.cpu().detach().numpy() np.save(os.path.join(target_dir_pcds,"inverse.npy"),point_inverse) original_pca_color = pca_color[point.inverse] points = original_coord colors = original_pca_color.cpu().detach().numpy() end_time = time.time() return points, colors, concerto_end_time - concerto_start_time, pca_end_time - pca_start_time def gradio_demo(target_dir,pca_slider,bright_slider, model_type, if_color=True, if_normal=True): target_dir_pcds = os.path.join(target_dir, "pcds") if not os.path.isfile(os.path.join(target_dir_pcds,"points.npy")): return None, "No point cloud available. Please upload data first." original_points = np.load(os.path.join(target_dir_pcds,"points.npy")) if if_color: original_colors = np.load(os.path.join(target_dir_pcds,"colors.npy")) else: original_colors = np.zeros_like(original_points) if if_normal: original_normals = np.load(os.path.join(target_dir_pcds,"normals.npy")) else: original_normals = np.zeros_like(original_points) processed_temp = (os.path.join(target_dir_pcds,"processed.glb")) processed_points, processed_colors, concerto_time, pca_time = Concerto_process(target_dir,original_points, original_colors,original_normals, pca_slider, bright_slider, model_type) feat_3d = trimesh.Scene() feat_data = trimesh.PointCloud(vertices=processed_points, colors=processed_colors, vertex_normals=original_normals) feat_3d.add_geometry(feat_data) feat_3d.export(processed_temp) return processed_temp, f"Feature visualization process finished with {concerto_time:.3f} seconds using Concerto inference and {pca_time:.3f} seconds using PCA. Updating visualization." def concerto_slider_update(target_dir,pca_slider,bright_slider,is_example,log_output): if is_example == "True": return None, log_output else: target_dir_pcds = os.path.join(target_dir, "pcds") if os.path.isfile(os.path.join(target_dir_pcds,"feat.npy")): feat = np.load(os.path.join(target_dir_pcds,"feat.npy")) inverse = np.load(os.path.join(target_dir_pcds,"inverse.npy")) feat = torch.tensor(feat, device = device) inverse = torch.tensor(inverse, device = device) pca_start_time = time.time() pca_colors = get_pca_color(feat,start = pca_slider, brightness=bright_slider, center=True) processed_colors = pca_colors[inverse].cpu().detach().numpy() pca_end_time = time.time() pca_time = pca_end_time - pca_start_time processed_points = np.load(os.path.join(target_dir_pcds,"points.npy")) processed_normals = np.load(os.path.join(target_dir_pcds,"normals.npy")) processed_temp = (os.path.join(target_dir_pcds,"processed.glb")) feat_3d = trimesh.Scene() feat_data = trimesh.PointCloud(vertices=processed_points, colors=processed_colors, vertex_normals=processed_normals) feat_3d.add_geometry(feat_data) feat_3d.export(processed_temp) log_output = f"Feature visualization process finished with{pca_time:.3f} seconds using PCA. Updating visualization." else: processed_temp = None log_output = "No representations saved, please click PCA generate first." # processed_temp, log_output = gradio_demo(target_dir,pca_slider,bright_slider) return processed_temp, log_output # set random seed # (random seed affect pca color, yet change random seed need manual adjustment kmeans) # (the pca prevent in paper is with another version of cuda and pytorch environment) concerto.utils.set_seed(53124) # Load model if device == 'cuda' and flash_attn is not None: print("Loading model with Flash Attention on GPU.") concerto_model = concerto.load("concerto_large", repo_id="Pointcept/Concerto").to(device) sonata_model = concerto.model.load("sonata", repo_id="facebook/sonata").to(device) else: print("Loading model on CPU or without Flash Attention.") custom_config = dict( # enc_patch_size=[1024 for _ in range(5)], # reduce patch size if necessary enable_flash=False, ) concerto_model = concerto.load( "concerto_large", repo_id="Pointcept/Concerto", custom_config=custom_config ).to(device) sonata_model = concerto.load("sonata", repo_id="facebook/sonata", custom_config=custom_config).to(device) transform = concerto.transform.default() VGGT_model = VGGT().to(device) _URL = "https://huggingface.co/facebook/VGGT-1B/resolve/main/model.pt" VGGT_model.load_state_dict(torch.hub.load_state_dict_from_url(_URL)) # VGGT_model.load_state_dict(torch.load("vggt/ckpt/model.pt",weights_only=True)) examples_video = [ # ["example/video/conference_room.mp4", 0.0, 1, "Depthmap Branch", "Yes",0,1.2, "True"], # ["example/video/office.mp4", 0.0, 1, "Pointmap Branch", "Yes",2,1.1, "True"], ["example/video/re10k_1.mp4", 10.0, 1, "Depthmap Branch", "No",2,1.2, "True"], ["example/video/re10k_2.mp4", 30.0, 1, "Depthmap Branch", "Yes",1,1.2, "True"], ["example/video/re10k_3.mp4", 10.0, 1, "Depthmap Branch", "Yes",1,1.2, "True"], ["example/video/re10k_4.mp4", 10.0, 1, "Depthmap Branch", "Yes",1,1., "True"], ] examples_pcd = [ ["example/pcd/scannet_0024.png","example/pcd/scannet_0024.ply",2,1.2, "True"], ["example/pcd/scannet_0603.png","example/pcd/scannet_0603.ply",0,1.2, "True"], # ["example/pcd/hm3d_00012_kDgLKdMd5X8_2.png","example/pcd/hm3d_00012_kDgLKdMd5X8_2.ply",0,1.2, "True"], ["example/pcd/hm3d_00113_3goH1WRaCYC.png","example/pcd/hm3d_00113_3goH1WRaCYC.ply",0,1.2, "True"], ["example/pcd/s3dis_Area2_auditorium1.png","example/pcd/s3dis_Area2_auditorium1.ply",0,1.2, "True"], # ["example/pcd/s3dis_Area4_lobby1.png","example/pcd/s3dis_Area4_lobby1.ply",1,1., "True"], ] # ["example/pcd/scannetpp_2a1b555966.png","example/pcd/scannetpp_2a1b555966.ply",1,1.1, "True"], # ["example/pcd/hm3d_00012_kDgLKdMd5X8_1.png","example/pcd/hm3d_00012_kDgLKdMd5X8_1.ply",0,1.0, "True"], # ["example/pcd/s3dis_Area2_conferenceRoom1.png","example/pcd/s3dis_Area2_conferenceRoom1.ply",0,1.2, "True"], # ["example/pcd/s3dis_Area4_hallway3.png","example/pcd/s3dis_Area4_hallway3.ply",0,1.2, "True"], with gr.Blocks( css=""" .custom-log * { font-style: italic; font-size: 22px !important; background-image: linear-gradient(120deg, #0ea5e9 0%, #6ee7b7 60%, #34d399 100%); -webkit-background-clip: text; background-clip: text; font-weight: bold !important; color: transparent !important; text-align: center !important; width: 800px; height: 100px; } .example-log * { font-style: italic; font-size: 16px !important; background-image: linear-gradient(120deg, #0ea5e9 0%, #6ee7b7 60%, #34d399 100%); -webkit-background-clip: text; background-clip: text; color: transparent !important; } .common-markdown * { font-size: 22px !important; -webkit-background-clip: text; background-clip: text; font-weight: bold !important; color: #0ea5e9 !important; text-align: center !important; } #big-box { border: 3px solid #00bcd4; padding: 20px; background-color: transparent; border-radius: 15px; } #my_radio .wrap { display: flex; flex-wrap: nowrap; justify-content: center; align-items: center; } #my_radio .wrap label { display: flex; width: 50%; justify-content: center; align-items: center; margin: 0; padding: 10px 0; box-sizing: border-box; } """, ) as demo: gr.HTML( """