Spaces:
Running
on
L40S
Running
on
L40S
import numpy as np | |
class Camera(object): | |
def __init__(self, c2w): | |
c2w_mat = np.array(c2w).reshape(4, 4) | |
self.c2w_mat = c2w_mat | |
self.w2c_mat = np.linalg.inv(c2w_mat) | |
def parse_matrix(matrix_str): | |
"""Parse camera matrix string from JSON format""" | |
rows = matrix_str.strip().split('] [') | |
matrix = [] | |
for row in rows: | |
row = row.replace('[', '').replace(']', '') | |
matrix.append(list(map(float, row.split()))) | |
return np.array(matrix) | |
def get_relative_pose(cam_params): | |
"""Calculate relative camera poses""" | |
abs_w2cs = [cam_param.w2c_mat for cam_param in cam_params] | |
abs_c2ws = [cam_param.c2w_mat for cam_param in cam_params] | |
cam_to_origin = 0 | |
target_cam_c2w = np.array([ | |
[1, 0, 0, 0], | |
[0, 1, 0, -cam_to_origin], | |
[0, 0, 1, 0], | |
[0, 0, 0, 1] | |
]) | |
abs2rel = target_cam_c2w @ abs_w2cs[0] | |
ret_poses = [target_cam_c2w, ] + [abs2rel @ abs_c2w for abs_c2w in abs_c2ws[1:]] | |
ret_poses = np.array(ret_poses, dtype=np.float32) | |
return ret_poses |