-
Notifications
You must be signed in to change notification settings - Fork 67
/
load_blender.py
144 lines (119 loc) · 4.36 KB
/
load_blender.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
import os
import torch
import numpy as np
import imageio
import json
import torch.nn.functional as F
import cv2
trans_t = lambda t : torch.Tensor([
[1,0,0,0],
[0,1,0,0],
[0,0,1,t],
[0,0,0,1]]).float()
rot_phi = lambda phi : torch.Tensor([
[1,0,0,0],
[0,np.cos(phi),-np.sin(phi),0],
[0,np.sin(phi), np.cos(phi),0],
[0,0,0,1]]).float()
rot_theta = lambda th : torch.Tensor([
[np.cos(th),0,-np.sin(th),0],
[0,1,0,0],
[np.sin(th),0, np.cos(th),0],
[0,0,0,1]]).float()
def rodrigues_mat_to_rot(R):
eps =1e-16
trc = np.trace(R)
trc2 = (trc - 1.)/ 2.
#sinacostrc2 = np.sqrt(1 - trc2 * trc2)
s = np.array([R[2, 1] - R[1, 2], R[0, 2] - R[2, 0], R[1, 0] - R[0, 1]])
if (1 - trc2 * trc2) >= eps:
tHeta = np.arccos(trc2)
tHetaf = tHeta / (2 * (np.sin(tHeta)))
else:
tHeta = np.real(np.arccos(trc2))
tHetaf = 0.5 / (1 - tHeta / 6)
omega = tHetaf * s
return omega
def rodrigues_rot_to_mat(r):
wx,wy,wz = r
theta = np.sqrt(wx * wx + wy * wy + wz * wz)
a = np.cos(theta)
b = (1 - np.cos(theta)) / (theta*theta)
c = np.sin(theta) / theta
R = np.zeros([3,3])
R[0, 0] = a + b * (wx * wx)
R[0, 1] = b * wx * wy - c * wz
R[0, 2] = b * wx * wz + c * wy
R[1, 0] = b * wx * wy + c * wz
R[1, 1] = a + b * (wy * wy)
R[1, 2] = b * wy * wz - c * wx
R[2, 0] = b * wx * wz - c * wy
R[2, 1] = b * wz * wy + c * wx
R[2, 2] = a + b * (wz * wz)
return R
def pose_spherical(theta, phi, radius):
c2w = trans_t(radius)
c2w = rot_phi(phi/180.*np.pi) @ c2w
c2w = rot_theta(theta/180.*np.pi) @ c2w
c2w = torch.Tensor(np.array([[-1,0,0,0],[0,0,1,0],[0,1,0,0],[0,0,0,1]])) @ c2w
return c2w
def load_blender_data(basedir, half_res=False, testskip=1):
splits = ['train', 'val', 'test']
metas = {}
for s in splits:
with open(os.path.join(basedir, 'transforms_{}.json'.format(s)), 'r') as fp:
metas[s] = json.load(fp)
all_imgs = []
all_poses = []
all_times = []
counts = [0]
for s in splits:
meta = metas[s]
imgs = []
poses = []
times = []
# if s=='train' or testskip==0:
# skip = 2 # if you remove/change this 2, also change the /2 in the times vector
# else:
skip = testskip
for t, frame in enumerate(meta['frames'][::skip]):
fname = os.path.join(basedir, frame['file_path'] + '.png')
imgs.append(imageio.imread(fname))
poses.append(np.array(frame['transform_matrix']))
cur_time = frame['time'] if 'time' in frame else float(t) / (len(meta['frames'][::skip])-1)
times.append(cur_time)
assert times[0] == 0, "Time must start at 0"
imgs = (np.array(imgs) / 255.).astype(np.float32) # keep all 4 channels (RGBA)
poses = np.array(poses).astype(np.float32)
times = np.array(times).astype(np.float32)
counts.append(counts[-1] + imgs.shape[0])
all_imgs.append(imgs)
all_poses.append(poses)
all_times.append(times)
i_split = [np.arange(counts[i], counts[i+1]) for i in range(3)]
imgs = np.concatenate(all_imgs, 0)
poses = np.concatenate(all_poses, 0)
times = np.concatenate(all_times, 0)
H, W = imgs[0].shape[:2]
camera_angle_x = float(meta['camera_angle_x'])
focal = .5 * W / np.tan(.5 * camera_angle_x)
if os.path.exists(os.path.join(basedir, 'transforms_{}.json'.format('render'))):
with open(os.path.join(basedir, 'transforms_{}.json'.format('render')), 'r') as fp:
meta = json.load(fp)
render_poses = []
for frame in meta['frames']:
render_poses.append(np.array(frame['transform_matrix']))
render_poses = np.array(render_poses).astype(np.float32)
else:
render_poses = torch.stack([pose_spherical(angle, -30.0, 4.0) for angle in np.linspace(-180,180,40+1)[:-1]], 0)
render_times = torch.linspace(0., 1., render_poses.shape[0])
if half_res:
H = H//2
W = W//2
focal = focal/2.
imgs_half_res = np.zeros((imgs.shape[0], H, W, 4))
for i, img in enumerate(imgs):
imgs_half_res[i] = cv2.resize(img, (H, W), interpolation=cv2.INTER_AREA)
imgs = imgs_half_res
# imgs = tf.image.resize_area(imgs, [400, 400]).numpy()
return imgs, poses, times, render_poses, render_times, [H, W, focal], i_split