"""Per-frame pointmaps and cameras: VGGT-Omega over the whole clip (default) or MoGe per frame.""" import os import tempfile import numpy as np OMEGA_REPO, OMEGA_CKPT = "facebook/VGGT-Omega", "vggt_omega_1b_512.pt" def vggt_omega_pointmaps(frames, res=512): """VGGT-Omega on the full frames: per-frame camera-space pointmaps (pytorch3d convention), normalized K, and the scene.""" import torch from huggingface_hub import hf_hub_download from vggt_omega.models import VGGTOmega from vggt_omega.utils.load_fn import load_and_preprocess_images from vggt_omega.utils.pose_enc import encoding_to_camera w0, h0 = frames[0].size assert 0.5 <= h0 / w0 <= 2.0, f"aspect {h0 / w0:.2f} would be center-cropped by the VGGT-Omega loader" tmp = tempfile.mkdtemp(prefix="a4d_omega_") paths = [] for t, im in enumerate(frames): p = os.path.join(tmp, f"{t:04d}.jpg"); im.save(p, quality=95); paths.append(p) model = VGGTOmega().to("cuda").eval() model.load_state_dict(torch.load(hf_hub_download(OMEGA_REPO, OMEGA_CKPT), map_location="cpu")) images = load_and_preprocess_images(paths, image_resolution=res).to("cuda") with torch.inference_mode(): pred = model(images) extr, intr = encoding_to_camera(pred["pose_enc"], pred["images"].shape[-2:]) extr, intr = extr[0].float().cpu().numpy(), intr[0].float().cpu().numpy() depth = pred["depth"][0, ..., 0].float().cpu().numpy() conf = pred["depth_conf"][0].float().cpu().numpy().reshape(depth.shape) colors = images.permute(0, 2, 3, 1).float().cpu().numpy() del model, pred, images torch.cuda.empty_cache() for p in paths: os.remove(p) os.rmdir(tmp) S, H, W = depth.shape u = np.tile(np.arange(W, dtype=np.float32)[None, :], (H, 1)); v = np.tile(np.arange(H, dtype=np.float32)[:, None], (1, W)) pms, Kn, c2w, world = [], [], [], [] for s in range(S): K, D = intr[s], depth[s] X = (u - K[0, 2]) / K[0, 0] * D; Y = (v - K[1, 2]) / K[1, 1] * D pms.append(np.stack([-X, -Y, D], 0)) Kn.append(np.array([[K[0, 0] / W, 0, K[0, 2] / W], [0, K[1, 1] / H, K[1, 2] / H], [0, 0, 1]], np.float32)) E = np.eye(4, dtype=np.float32); E[:3] = extr[s]; Ci = np.linalg.inv(E); c2w.append(Ci) world.append(np.stack([X, Y, D], -1) @ Ci[:3, :3].T + Ci[:3, 3]) pms = np.stack(pms).astype(np.float16).astype(np.float32) scene = {"pointmaps": pms, "intrinsics": np.stack(Kn), "intrinsics_px": intr.astype(np.float32), "cam_c2w": np.stack(c2w), "world_points": np.stack(world).astype(np.float32), "depth": depth.astype(np.float32), "conf": conf.astype(np.float32), "colors": colors.astype(np.float16), "proc_hw": np.array([H, W])} return list(pms), list(scene["intrinsics"]), scene def _alias_utils3d_pt(): """MoGe v1 calls utils3d.pt; newer utils3d only exposes utils3d.torch.""" import importlib import sys import utils3d if "pt" not in vars(utils3d): sys.modules.setdefault("utils3d.pt", importlib.import_module("utils3d.torch")) utils3d.pt = sys.modules["utils3d.pt"] def moge_pointmaps(workspace, imgs, msks): """Per-frame MoGe pointmaps through the released SAM 3D Objects pipeline (no camera motion: identity poses).""" import torch from hydra.utils import instantiate _alias_utils3d_pt() from omegaconf import OmegaConf cfg = OmegaConf.load(os.path.join(workspace, "pipeline.yaml")) cfg.workspace_dir, cfg.compile_model = workspace, False released = instantiate(cfg) pms, Ks = [], [] for im, mk in zip(imgs, msks): rgba = np.asarray(released.merge_image_and_mask(np.asarray(im), mk.astype(np.uint8) * 255), dtype=np.uint8) d = released.compute_pointmap(rgba, pointmap=None) pms.append(d["pointmap"].detach().float().cpu().numpy()) Ks.append(d["intrinsics"].detach().float().cpu().numpy().reshape(3, 3)) del released torch.cuda.empty_cache() return pms, Ks