-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathtpose_pdf_dataset.py
265 lines (226 loc) · 9.77 KB
/
tpose_pdf_dataset.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import torch.utils.data as data
from lib.utils import base_utils
from PIL import Image
import numpy as np
import json
import os
import imageio
import cv2
from lib.config import cfg
from lib.utils.if_nerf import if_nerf_data_utils as if_nerf_dutils
from plyfile import PlyData
class Dataset(data.Dataset):
def __init__(self, data_root, human, ann_file, split):
super(Dataset, self).__init__()
self.data_root = data_root
self.human = human
self.split = split
annots = np.load(ann_file, allow_pickle=True).item()
self.cams = annots['cams']
num_cams = len(self.cams['K'])
if len(cfg.test_view) == 0:
test_view = [
i for i in range(num_cams) if i not in cfg.training_view
]
if len(test_view) == 0:
test_view = [0]
else:
test_view = cfg.test_view
view = cfg.training_view if split == 'train' else test_view
i = cfg.begin_ith_frame
i_intv = cfg.frame_interval
ni = cfg.num_train_frame
if cfg.test_novel_pose or cfg.aninerf_animation:
i = cfg.begin_ith_frame + cfg.num_train_frame * i_intv
ni = cfg.num_eval_frame
if self.human == 'CoreView_390':
i = 0
self.ims = np.array([
np.array(ims_data['ims'])[view]
for ims_data in annots['ims'][i:i + ni * i_intv][::i_intv]
]).ravel()
self.cam_inds = np.array([
np.arange(len(ims_data['ims']))[view]
for ims_data in annots['ims'][i:i + ni * i_intv][::i_intv]
]).ravel()
self.num_cams = len(view)
self.lbs_root = os.path.join(self.data_root, 'lbs')
joints = np.load(os.path.join(self.lbs_root, 'joints.npy'))
self.joints = joints.astype(np.float32)
self.parents = np.load(os.path.join(self.lbs_root, 'parents.npy'))
weights = np.load(os.path.join(self.lbs_root, 'weights.npy'))
self.weights = weights.astype(np.float32)
self.big_A = self.load_bigpose()
if cfg.test_novel_pose or cfg.aninerf_animation:
training_joints_path = os.path.join(self.lbs_root, 'training_joints.npy')
if os.path.exists(training_joints_path):
self.training_joints = np.load(training_joints_path)
self.nrays = cfg.N_rand
def load_bigpose(self):
big_poses = np.zeros([len(self.joints), 3]).astype(np.float32).ravel()
angle = 30
big_poses[5] = np.deg2rad(angle)
big_poses[8] = np.deg2rad(-angle)
big_poses = big_poses.reshape(-1, 3)
big_A = if_nerf_dutils.get_rigid_transformation(
big_poses, self.joints, self.parents)
big_A = big_A.astype(np.float32)
return big_A
def get_mask(self, index):
msk_path = os.path.join(self.data_root, 'mask_cihp',
self.ims[index])[:-4] + '.png'
if not os.path.exists(msk_path):
msk_path = os.path.join(self.data_root, 'mask',
self.ims[index])[:-4] + '.png'
if not os.path.exists(msk_path):
msk_path = os.path.join(self.data_root, self.ims[index].replace(
'images', 'mask'))[:-4] + '.png'
if not os.path.exists(msk_path):
msk_path = os.path.join(self.data_root, self.ims[index].replace(
'images', 'mask'))[:-4] + '.jpg'
msk_cihp = imageio.imread(msk_path)
if len(msk_cihp.shape) == 3:
msk_cihp = msk_cihp[..., 0]
if 'deepcap' in self.data_root or 'nerfcap' in self.data_root:
msk_cihp = (msk_cihp > 125).astype(np.uint8)
else:
msk_cihp = (msk_cihp != 0).astype(np.uint8)
msk = msk_cihp
orig_msk = msk.copy()
if not cfg.eval and cfg.erode_edge:
border = 5
kernel = np.ones((border, border), np.uint8)
msk_erode = cv2.erode(msk.copy(), kernel)
msk_dilate = cv2.dilate(msk.copy(), kernel)
msk[(msk_dilate - msk_erode) == 1] = 100
return msk, orig_msk
def prepare_input(self, i):
# read xyz in the world coordinate system
vertices_path = os.path.join(self.data_root, cfg.vertices,
'{}.npy'.format(i))
if not os.path.exists(vertices_path):
vertices_path = os.path.join(self.data_root, cfg.vertices,
'{:06d}.npy'.format(i))
wxyz = np.load(vertices_path).astype(np.float32)
# transform smpl from the world coordinate to the smpl coordinate
params_path = os.path.join(self.data_root, cfg.params,
'{}.npy'.format(i))
if not os.path.exists(params_path):
params_path = os.path.join(self.data_root, cfg.params,
'{:06d}.npy'.format(i))
params = np.load(params_path, allow_pickle=True).item()
Rh = params['Rh'].astype(np.float32)
Th = params['Th'].astype(np.float32)
# prepare sp input of param pose
R = cv2.Rodrigues(Rh)[0].astype(np.float32)
pxyz = np.dot(wxyz - Th, R).astype(np.float32)
# calculate the skeleton transformation
poses = params['poses'].reshape(-1, 3)
joints = self.joints
parents = self.parents
A, canonical_joints = if_nerf_dutils.get_rigid_transformation(
poses, joints, parents, return_joints=True)
posed_joints = np.dot(canonical_joints, R.T) + Th
# find the nearest training frame
if (cfg.test_novel_pose or cfg.aninerf_animation) and hasattr(self, "training_joints"):
nearest_frame_index = np.linalg.norm(self.training_joints -
posed_joints[None],
axis=2).mean(axis=1).argmin()
else:
nearest_frame_index = 0
poses = poses.ravel().astype(np.float32)
return wxyz, pxyz, A, Rh, Th, poses, nearest_frame_index
def __getitem__(self, index):
img_path = os.path.join(self.data_root, self.ims[index])
img = imageio.imread(img_path).astype(np.float32) / 255.
msk, orig_msk = self.get_mask(index)
H, W = img.shape[:2]
msk = cv2.resize(msk, (W, H), interpolation=cv2.INTER_NEAREST)
orig_msk = cv2.resize(orig_msk, (W, H),
interpolation=cv2.INTER_NEAREST)
cam_ind = self.cam_inds[index]
K = np.array(self.cams['K'][cam_ind])
D = np.array(self.cams['D'][cam_ind])
img = cv2.undistort(img, K, D)
msk = cv2.undistort(msk, K, D)
orig_msk = cv2.undistort(orig_msk, K, D)
R = np.array(self.cams['R'][cam_ind])
T = np.array(self.cams['T'][cam_ind]) / 1000.
# reduce the image resolution by ratio
H, W = int(img.shape[0] * cfg.ratio), int(img.shape[1] * cfg.ratio)
img = cv2.resize(img, (W, H), interpolation=cv2.INTER_AREA)
msk = cv2.resize(msk, (W, H), interpolation=cv2.INTER_NEAREST)
orig_msk = cv2.resize(orig_msk, (W, H),
interpolation=cv2.INTER_NEAREST)
if cfg.mask_bkgd:
img[msk == 0] = 0
K[:2] = K[:2] * cfg.ratio
if self.human in ['CoreView_313', 'CoreView_315']:
i = int(os.path.basename(img_path).split('_')[4])
frame_index = i - 1
elif 'deepcap' in self.data_root or 'nerfcap' in self.data_root:
i = int(os.path.basename(img_path).split('_')[-1][:-4])
frame_index = i
else:
i = int(os.path.basename(img_path)[:-4])
frame_index = i
# read v_shaped
if cfg.get('use_bigpose', False):
vertices_path = os.path.join(self.lbs_root, 'bigpose_vertices.npy')
else:
vertices_path = os.path.join(self.lbs_root, 'tvertices.npy')
tvertices = np.load(vertices_path).astype(np.float32)
tbounds = if_nerf_dutils.get_bounds(tvertices)
wpts, pvertices, A, Rh, Th, poses, nearest_frame_index = self.prepare_input(i)
pbounds = if_nerf_dutils.get_bounds(pvertices)
wbounds = if_nerf_dutils.get_bounds(wpts)
rgb, ray_o, ray_d, near, far, coord, mask_at_box = if_nerf_dutils.sample_ray_h36m(
img, msk, K, R, T, wbounds, self.nrays, self.split)
if cfg.erode_edge:
orig_msk = if_nerf_dutils.crop_mask_edge(orig_msk)
occupancy = orig_msk[coord[:, 0], coord[:, 1]]
# nerf
ret = {
'rgb': rgb,
'occupancy': occupancy,
'ray_o': ray_o,
'ray_d': ray_d,
'near': near,
'far': far,
'mask_at_box': mask_at_box
}
# blend weight
meta = {
'A': A,
'big_A': self.big_A,
'poses': poses,
'weights': self.weights,
'tvertices': tvertices,
'pvertices': pvertices,
'pbounds': pbounds,
'wbounds': wbounds,
'tbounds': tbounds
}
ret.update(meta)
# transformation
R = cv2.Rodrigues(Rh)[0].astype(np.float32)
meta = {'R': R, 'Th': Th, 'H': H, 'W': W}
ret.update(meta)
latent_index = index // self.num_cams
bw_latent_index = index // self.num_cams
if cfg.test_novel_pose or cfg.aninerf_animation:
if 'h36m' in self.data_root:
latent_index = 0
else:
latent_index = cfg.num_train_frame - 1
latent_index = nearest_frame_index
meta = {
'latent_index': latent_index,
'bw_latent_index': bw_latent_index,
'frame_index': frame_index,
'cam_ind': cam_ind
}
ret.update(meta)
return ret
def __len__(self):
return len(self.ims)