-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpath.py
148 lines (120 loc) · 5.22 KB
/
path.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import nerfstudio
from nerfstudio.models.base_model import Model, ModelConfig
from nerfstudio.models.nerfacto import NerfactoModel, NerfactoModelConfig
from nerfstudio.cameras.cameras import Cameras, CameraType
from nerfstudio.data.scene_box import SceneBox
import torch
import numpy as np
import json
import os
from nerfstudio.utils.eval_utils import eval_setup
from pathlib import Path
import yaml
import matplotlib.pyplot as plt
from nerfstudio.utils import colormaps
from torchvision.utils import save_image
from scipy.spatial.transform import Rotation
import copy
if __name__ == "__main__":
script_dir = os.path.dirname(os.path.realpath(__file__))
# fn = os.path.join(script_dir, '../data/IRL2/transforms.json')
# with open(fn,'r') as f:
# data = json.load(f)
# transforms = []
# frames = data['frames']
# for frame in frames:
# transform_matrix = frame['transform_matrix']
# tmp = np.array(transform_matrix)[:3,:]
# transforms.append(tmp)
# transforms = np.array(transforms)
# print(transforms.shape)
# transforms_tensor = torch.FloatTensor(transforms)
# scene_box = SceneBox.from_camera_poses(transforms_tensor, 1)
config_fn = os.path.join(script_dir, './outputs/IRL2/nerfacto/2023-09-21_210511/config.yml')
config_path = Path(config_fn)
_, pipeline, _, step = eval_setup(
config_path,
eval_num_rays_per_chunk=None,
test_mode='inference'
)
# config = yaml.load(config_path.read_text(), Loader=yaml.Loader)
# pipeline = config.pipeline.setup(device='cuda', test_mode='test')
model = pipeline.model
# scene_box = SceneBox(aabb=torch.FloatTensor([[-1.,-1.,-1.],[1.,1.,1.]]))
# model = config.pipeline.model.setup(
# scene_box = scene_box,
# num_train_data = 0,
# ).to('cuda')
f = open('camera_path.json')
data = json.load(f)
data_out = copy.deepcopy(data)
import matplotlib.image
j = 0
for idx in range(len(data['camera_path'])-1):
i = data['camera_path'][idx]
future = data['camera_path'][idx+1]
f_x = future['camera_to_world'][3]
f_y = future['camera_to_world'][7]
yaw = np.arctan2( f_y - i['camera_to_world'][7],f_x - i['camera_to_world'][3] ) - np.pi/2
print("yaw:", yaw)
camera_to_world = np.array(i['camera_to_world'][:-4]).reshape((3,4))
# camera_to_world[0,:-1] = [np.cos(yaw), -np.sin(yaw), 0]
# camera_to_world[1,:-1] = [np.sin(yaw), np.cos(yaw), 0]
# camera_to_world[2,:-1] = [0,0,1]
# pitch = np.array([[0,0,1],[0,1,0],[-1,0,0]])
rpy = Rotation.from_euler('xyz', [np.deg2rad(90), 0, yaw])
camera_to_world[:,:-1] = rpy.as_matrix()
out_mat = np.vstack((camera_to_world, np.array([[0,0,0,1]])))
out_mat = np.reshape(out_mat, (out_mat.size, )).tolist()
data_out['camera_path'][idx]['camera_to_world'] = out_mat
camera_to_world = torch.FloatTensor([ camera_to_world ])
#camera_to_world = torch.FloatTensor([
# [ 6.6384e-01, -1.2349e-01, 7.3761e-01, 6.1554e-01],
# [ 7.4787e-01, 1.0962e-01, -6.5473e-01, -5.5424e-01],
# [ 6.9389e-17, 9.8627e-01, 1.6512e-01, -9.1273e-03]
#])
# camera_to_world = torch.FloatTensor([
# [
# 0.012817036360502243,
# 0.19525104761123657,
# -0.9806695580482483,
# -0.07062844187021255
# ],
# [
# 0.9997958540916443,
# 0.012817036360502243,
# 0.015618880279362202,
# -0.027591701596975327
# ],
# [
# 0.015618880279362202,
# -0.9806695580482483,
# -0.19504690170288086,
# -0.001556280069053173
# ]
# ])
fx = fy = (320.0/2)/(np.tan(np.deg2rad(50)/2))
cx = cy = 160.0
width = height = 320
distCoeffs = np.array([0.0, 0.0, 0.0, 0.0, 0.0])
camera_type = CameraType.PERSPECTIVE
camera = Cameras(camera_to_worlds = camera_to_world, fx = fx, fy = fy, cx = cx, cy = cy, width=width, height=height, camera_type=camera_type)
camera = camera.to('cuda')
ray_bundle = camera.generate_rays(camera_indices=0, aabb_box=None)
print(len(ray_bundle))
# model = Model(config, scene_box, num_train_data=1)
# loaded_state = torch.load(model_fn)
# model.load_state_dict(loaded_state)
with torch.no_grad():
tmp = model.get_outputs_for_camera_ray_bundle(ray_bundle)
# img = model.get_rgba_image(res)
img = tmp['rgb']
img =(colormaps.apply_colormap(image=img, colormap_options=colormaps.ColormapOptions())).cpu().numpy()
print(img.shape)
matplotlib.image.imsave('images/foo'+ str(j) + '.png', img)
#plt.imshow(img)
#plt.show()
j+=1
print("stop")
with open('camera_path2.json', 'w+') as f:
json.dump(data_out, f, indent=4)