-
Notifications
You must be signed in to change notification settings - Fork 27
/
cameras.py
83 lines (64 loc) · 2.58 KB
/
cameras.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import argparse
import json
import os
from pathlib import Path
import cv2
import matplotlib.pyplot as plt
import numpy as np
from scipy.spatial.transform import Rotation as R
from videodepth_video import VideoData
def prepare_cameras(
video : VideoData,
ui_data_path : str,
backend_data_path: str
):
video_Ks = []
# video_Rts = []
video_Rs = []
video_ts = []
ui_cam_data = []
width, height = video.get_depth_res()
for i in range(video.first_frame_idx, video.last_frame_idx + 1):
camera_i = video.get_camera(i)
video_Ks.append(camera_i.K(width, height))
rot_mat = camera_i.R
translation = camera_i.t / video.down_scale_factor
# Rt = np.column_stack([rot_mat.T, - rot_mat.T @ translation])
# video_Rts.append(Rt)
video_Rs.append(rot_mat)
video_ts.append(translation)
# Export camera data
camera_projection_transform = camera_i.get_open_gl_projection_matrix(video.camera_near, video.camera_far, width, height)
cam_data = {
'rotation': R.from_matrix(rot_mat).as_quat().tolist(),
'translation': translation.tolist(),
'cameraProjectionTransform': camera_projection_transform.flatten().tolist(), # in threejs matrices are loaded in row-major convention
'depthRange': video.depth_range,
'depthOffset': video.depth_offset
}
ui_cam_data.append(cam_data)
# Export cameras
if not os.path.exists(os.path.join(ui_data_path, video.video_name)):
os.makedirs(os.path.join(ui_data_path, video.video_name))
with open(os.path.join(ui_data_path, video.video_name, 'camera.json'), 'w') as fp:
json.dump(ui_cam_data, fp)
# Export backend cameras file
if not os.path.exists(os.path.join(backend_data_path, video.video_name)):
os.makedirs(os.path.join(backend_data_path, video.video_name))
# np.savez(os.path.join(backend_data_path, video.video_name, "gl_cameras"),
# Ks = np.array(video_Ks),
# RTs = np.array(video_Rts),
# res = np.array([width, height]),
# down_scale_factor = video.down_scale_factor,
# near = video.camera_near,
# far = video.camera_far
# )
np.savez(os.path.join(backend_data_path, video.video_name, "cameras"),
Ks = np.array(video_Ks),
Rs = np.array(video_Rs),
ts = np.array(video_ts),
res = np.array([width, height]),
down_scale_factor = video.down_scale_factor,
near = video.camera_near,
far = video.camera_far
)