mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-06 07:48:50 +08:00
Improving eval script with camera trajectory videos (#124)
* spiral traj working, specify version for hydra, better documentation * cleaned up spiral trajectory a bit
This commit is contained in:
1 parent
7cda0158c7
commit
4ddd146633
16 files changed
+3379
-769
No files matched your search
@@ -16,12 +16,15 @@
|
||||
Code for camera paths.
|
||||
"""
|
||||
|
||||
import copy
|
||||
from dataclasses import dataclass
|
||||
from typing import List
|
||||
from typing import List, Tuple
|
||||
|
||||
import torch
|
||||
from pyrad.cameras.cameras import Camera, get_camera, get_intrinsics_from_intrinsics_matrix
|
||||
from pyrad.cameras.transformations import get_interpolated_poses_many
|
||||
from pyrad.cameras.utils import get_interpolated_poses_many
|
||||
|
||||
import pyrad.cameras.utils as camera_utils
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -60,8 +63,59 @@ def get_interpolated_camera_path(camera_a: Camera, camera_b: Camera, steps: int)
|
||||
return CameraPath(cameras=cameras)
|
||||
|
||||
|
||||
def get_spiral_path(camera_a: Camera, camera_b: Camera, steps: int) -> CameraPath:
|
||||
def get_spiral_path(
|
||||
camera: Camera,
|
||||
steps: int = 30,
|
||||
radius: float = None,
|
||||
radiuses: Tuple[float] = None,
|
||||
rots: int = 2,
|
||||
zrate: float = 0.5,
|
||||
) -> CameraPath:
|
||||
"""
|
||||
Returns a list of camera in a sprial.
|
||||
Returns a list of camera in a sprial trajectory.
|
||||
|
||||
Args:
|
||||
camera: The camera to start the spiral from.
|
||||
steps: The number of cameras in the generated path.
|
||||
radius: The radius of the spiral for all xyz directions.
|
||||
radiuses: The list of radii for the spiral in xyz directions.
|
||||
rots: The number of rotations to apply to the camera.
|
||||
zrate: How much to change the z position of the camera.
|
||||
|
||||
Returns:
|
||||
CameraPath: A spiral camera path.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
assert radius is not None or radiuses is not None, "Either radius or radiuses must be specified."
|
||||
if radius is not None and radiuses is None:
|
||||
rad = torch.tensor([radius] * 3, device=camera.device)
|
||||
elif radiuses is not None and radius is None:
|
||||
rad = torch.tensor(radiuses, device=camera.device)
|
||||
else:
|
||||
raise ValueError("Only one of radius or radiuses must be specified.")
|
||||
|
||||
up = camera.camera_to_world[:3, 2] # scene is z up
|
||||
focal = min(camera.fx, camera.fy)
|
||||
target = torch.tensor([0, 0, -focal], device=camera.device) # camera looking in -z direction
|
||||
|
||||
c2wh_global = camera.get_camera_to_world_h()
|
||||
|
||||
local_c2whs = []
|
||||
for theta in torch.linspace(0.0, 2.0 * torch.pi * rots, steps + 1)[:-1]:
|
||||
center = (
|
||||
torch.tensor([torch.cos(theta), -torch.sin(theta), -torch.sin(theta * zrate)], device=camera.device) * rad
|
||||
)
|
||||
lookat = center - target
|
||||
c2w = camera_utils.viewmatrix(lookat, up, center)
|
||||
ones = torch.tensor([0, 0, 0, 1], device=c2w.device)[None]
|
||||
c2wh = torch.cat([c2w, ones], dim=0)
|
||||
local_c2whs.append(c2wh)
|
||||
|
||||
cameras = []
|
||||
for local_c2wh in local_c2whs:
|
||||
cam = copy.deepcopy(camera)
|
||||
c2wh = torch.matmul(c2wh_global, local_c2wh)
|
||||
cam.camera_to_world = c2wh[:3, :4]
|
||||
cameras.append(cam)
|
||||
|
||||
return CameraPath(cameras=cameras)
|
||||
@@ -37,6 +37,11 @@ class Camera:
|
||||
self.camera_to_world = camera_to_world
|
||||
self.camera_index = camera_index
|
||||
|
||||
@property
|
||||
def device(self):
|
||||
"""Returns the device that the camera is on."""
|
||||
return self.camera_to_world.device
|
||||
|
||||
@abstractmethod
|
||||
def get_num_intrinsics_params(self) -> int:
|
||||
"""
|
||||
|
||||
@@ -20,6 +20,7 @@ Camera transformation helper code.
|
||||
import math
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
|
||||
_EPS = np.finfo(float).eps * 4.0
|
||||
|
||||
@@ -289,3 +290,27 @@ def get_swirl_poses(pose):
|
||||
poses[i, 1, 3] += dy[i]
|
||||
poses[i, 2, 3] += dz[i]
|
||||
return poses
|
||||
|
||||
|
||||
def normalize(x):
|
||||
"""Returns a normalized vector."""
|
||||
return x / torch.linalg.norm(x)
|
||||
|
||||
|
||||
def viewmatrix(lookat, up, pos):
|
||||
"""Returns a camera transformation matrix.
|
||||
|
||||
Args:
|
||||
lookat: The direction the camera is looking.
|
||||
up: The upward direction of the camera.
|
||||
pos: The position of the camera.
|
||||
|
||||
Returns:
|
||||
torch.Tensor: A camera transformation matrix.
|
||||
"""
|
||||
vec2 = normalize(lookat)
|
||||
vec1_avg = normalize(up)
|
||||
vec0 = normalize(torch.cross(vec1_avg, vec2))
|
||||
vec1 = normalize(torch.cross(vec2, vec0))
|
||||
m = torch.stack([vec0, vec1, vec2, pos], 1)
|
||||
return m
|
||||
@@ -22,7 +22,7 @@ from omegaconf import ListConfig
|
||||
|
||||
from torchtyping import TensorType
|
||||
|
||||
from pyrad.cameras.cameras import get_camera
|
||||
from pyrad.cameras.cameras import Camera, get_camera
|
||||
from pyrad.cameras.rays import RayBundle
|
||||
from pyrad.data.image_dataset import ImageDataset
|
||||
from pyrad.data.image_sampler import ImageSampler
|
||||
@@ -116,11 +116,16 @@ class EvalDataloader: # pylint: disable=too-few-public-methods
|
||||
self.device = device
|
||||
self.kwargs = kwargs
|
||||
|
||||
def get_data_from_image_idx(self, image_idx) -> Tuple[RayBundle, Dict]:
|
||||
"""Returns the data for a specific image index."""
|
||||
def get_camera(self, image_idx) -> Camera:
|
||||
"""Get camera for the given image index"""
|
||||
intrinsics = self.intrinsics[image_idx].to(self.device)
|
||||
camera_to_world = self.camera_to_world[image_idx].to(self.device)
|
||||
camera = get_camera(intrinsics, camera_to_world, camera_index=image_idx)
|
||||
return camera
|
||||
|
||||
def get_data_from_image_idx(self, image_idx) -> Tuple[RayBundle, Dict]:
|
||||
"""Returns the data for a specific image index."""
|
||||
camera = self.get_camera(image_idx)
|
||||
ray_bundle = camera.get_camera_ray_bundle(device=self.device)
|
||||
ray_bundle.num_rays_per_chunk = self.num_rays_per_chunk
|
||||
batch = self.image_dataset[image_idx]
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
import numpy as np
|
||||
import torch
|
||||
|
||||
from pyrad.cameras.cameras import Camera, get_camera, get_intrinsics_from_intrinsics_matrix
|
||||
|
||||
|
||||
def get_chunks(lst, num_chunks=None, size_of_chunk=None):
|
||||
"""Returns list of n elements, constaining a sublist."""
|
||||
@@ -49,3 +51,18 @@ def get_intrinsics_matrix_and_camera_to_world_h(camera_object, image_height):
|
||||
camera_to_world_h = torch.tensor(get_chunks(camera_object["matrix"], size_of_chunk=4)).T.float()
|
||||
|
||||
return intrinsics_matrix, camera_to_world_h
|
||||
|
||||
|
||||
def get_camera_from_vis(vis, name="/Cameras/Main Camera", image_height=100):
|
||||
data = vis[name].get_object()
|
||||
if data is None:
|
||||
return None
|
||||
camera_object = data["object"]["object"]
|
||||
intrinsics_matrix, camera_to_world_h = get_intrinsics_matrix_and_camera_to_world_h(
|
||||
camera_object, image_height=image_height
|
||||
)
|
||||
|
||||
camera_to_world = camera_to_world_h[:3, :]
|
||||
intrinsics = get_intrinsics_from_intrinsics_matrix(intrinsics_matrix)
|
||||
camera = get_camera(intrinsics, camera_to_world)
|
||||
return camera
|
||||
@@ -36,7 +36,7 @@ from pyrad.cameras.rays import RayBundle
|
||||
from pyrad.utils import profiler
|
||||
from pyrad.utils.config import ViewerConfig
|
||||
from pyrad.viewer.server.visualizer import Viewer
|
||||
from pyrad.viewer.server.utils import get_intrinsics_matrix_and_camera_to_world_h
|
||||
from pyrad.viewer.server.utils import get_camera_from_vis, get_intrinsics_matrix_and_camera_to_world_h
|
||||
from pyrad.viewer.server.transformations import get_translation_matrix
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user