Download examples/visualizer_code/source/evaluation/scale4d_inputs.py from facebook/SCALE4D: direct link, hf CLI and curl.
- Browser
- Download file 4.6 kB
-
https://huggingface.co/facebook/SCALE4D/resolve/main/examples/visualizer_code/source/evaluation/scale4d_inputs.py
- Command line
-
hf download hf://facebook/SCALE4D/examples/visualizer_code/source/evaluation/scale4d_inputs.py
-
curl -L -o scale4d_inputs.py https://huggingface.co/facebook/SCALE4D/resolve/main/examples/visualizer_code/source/evaluation/scale4d_inputs.py
4.6 kB
| """Prepare videos, arrays, or calibrated NPZ inputs for SCALE4D inference.""" | |
| from __future__ import annotations | |
| import json | |
| from pathlib import Path | |
| import cv2 | |
| import numpy as np | |
| import torch | |
| from evaluation.ucpe_inputs import ClipInput | |
| def array_clip( | |
| rgbs: np.ndarray | torch.Tensor, | |
| *, | |
| query_uv: np.ndarray | torch.Tensor | None = None, | |
| query_times: np.ndarray | torch.Tensor | None = None, | |
| cameras_to_world: np.ndarray | torch.Tensor | None = None, | |
| intrinsics: np.ndarray | torch.Tensor | None = None, | |
| world_up: np.ndarray | torch.Tensor | None = None, | |
| grid_size: int = 16, | |
| metadata: dict | None = None, | |
| ) -> ClipInput: | |
| rgbs = torch.as_tensor(rgbs).cpu() | |
| if rgbs.ndim == 4: | |
| rgbs = rgbs[None] | |
| if rgbs.ndim != 5 or min(rgbs.shape) < 1: | |
| raise ValueError("RGB must have shape (T, 3, H, W) or (V, T, 3, H, W)") | |
| views, frames, _, height, width = rgbs.shape | |
| if grid_size < 1: | |
| raise ValueError("grid_size must be positive") | |
| if query_uv is None: | |
| ys, xs = torch.meshgrid( | |
| torch.linspace(0, height - 1, grid_size), | |
| torch.linspace(0, width - 1, grid_size), | |
| indexing="ij", | |
| ) | |
| query_uv = torch.stack([xs, ys], dim=-1).reshape(-1, 2) | |
| query_uv = torch.as_tensor(query_uv).float().cpu() | |
| count = len(query_uv) | |
| query_times = ( | |
| torch.zeros(count, dtype=torch.int64) | |
| if query_times is None | |
| else torch.as_tensor(query_times).cpu() | |
| ) | |
| if cameras_to_world is None: | |
| cameras_to_world = torch.eye(4).expand(views, frames, 4, 4).clone() | |
| synthetic_intrinsics = intrinsics is None | |
| if intrinsics is None: | |
| focal = float(max(height, width)) | |
| intrinsics = ( | |
| torch.tensor( | |
| [[focal, 0, (width - 1) / 2], [0, focal, (height - 1) / 2], [0, 0, 1]] | |
| ) | |
| .expand(views, frames, 3, 3) | |
| .clone() | |
| ) | |
| info = dict(metadata or {}) | |
| info.setdefault("dataset_type", "scale4d") | |
| info.setdefault("camera_ids", list(range(views))) | |
| info["camera_calibration"] = "synthetic" if synthetic_intrinsics else "provided" | |
| result = ClipInput( | |
| rgbs=rgbs, | |
| cameras_to_world=torch.as_tensor(cameras_to_world).cpu(), | |
| intrinsics=torch.as_tensor(intrinsics).cpu(), | |
| query_uv=query_uv, | |
| query_times=query_times, | |
| query_views=torch.zeros(count, dtype=torch.int64), | |
| world_up=None if world_up is None else torch.as_tensor(world_up).cpu(), | |
| metadata=info, | |
| ) | |
| result.validate() | |
| return result | |
| def video_clip(path: str | Path, frames: int = 48, grid_size: int = 16) -> ClipInput: | |
| path = Path(path).expanduser() | |
| capture = cv2.VideoCapture(str(path)) | |
| try: | |
| total = int(capture.get(cv2.CAP_PROP_FRAME_COUNT)) | |
| if total < 1: | |
| raise ValueError(f"Cannot read video frames: {path}") | |
| indices = np.linspace(0, total - 1, frames).round().astype(np.int64) | |
| images = [] | |
| for index in indices: | |
| capture.set(cv2.CAP_PROP_POS_FRAMES, int(index)) | |
| ok, image = capture.read() | |
| if not ok: | |
| raise ValueError(f"Cannot decode frame {index} of {path}") | |
| images.append(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) | |
| finally: | |
| capture.release() | |
| return array_clip( | |
| np.stack(images).transpose(0, 3, 1, 2), | |
| grid_size=grid_size, | |
| metadata={ | |
| "dataset_type": "video", | |
| "source_frame_indices": indices.tolist(), | |
| }, | |
| ) | |
| def npz_clip(path: str | Path) -> ClipInput: | |
| with np.load(Path(path).expanduser(), allow_pickle=False) as data: | |
| metadata = ( | |
| json.loads(str(data["metadata_json"].item())) | |
| if "metadata_json" in data | |
| else {} | |
| ) | |
| values = { | |
| name: torch.from_numpy(data[name].copy()) | |
| for name in ( | |
| "rgbs", | |
| "cameras_to_world", | |
| "intrinsics", | |
| "query_uv", | |
| "query_times", | |
| "query_views", | |
| ) | |
| } | |
| metadata.setdefault("dataset_type", "scale4d") | |
| metadata.setdefault("camera_ids", list(range(values["rgbs"].shape[0]))) | |
| metadata.setdefault("camera_calibration", "provided") | |
| result = ClipInput( | |
| **values, | |
| world_up=torch.from_numpy(data["world_up"].copy()) | |
| if "world_up" in data | |
| else None, | |
| metadata=metadata, | |
| ) | |
| result.validate() | |
| return result | |