-
Notifications
You must be signed in to change notification settings - Fork 14
/
data_parser.py
264 lines (210 loc) · 8.96 KB
/
data_parser.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
# -*- coding: utf-8 -*-
# Max-Planck-Gesellschaft zur Förderung der Wissenschaften e.V. (MPG) is
# holder of all proprietary rights on this computer program.
# You can only use this computer program if you have closed
# a license agreement with MPG or you get the right to use the computer
# program from someone who is authorized to grant you that right.
# Any use of the computer program without a valid license is prohibited and
# liable to prosecution.
#
# Copyright©2019 Max-Planck-Gesellschaft zur Förderung
# der Wissenschaften e.V. (MPG). acting on behalf of its Max Planck Institute
# for Intelligent Systems and the Max Planck Institute for Biological
# Cybernetics. All rights reserved.
#
# Contact: ps-license@tuebingen.mpg.de
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
import sys
import os
import os.path as osp
import json
from collections import namedtuple
import cv2
import numpy as np
import scipy.io as sio
import torch
from torch.utils.data import Dataset
from utils import smpl_to_openpose
Keypoints = namedtuple('Keypoints',
['keypoints', 'gender_gt', 'gender_pd'])
Keypoints.__new__.__defaults__ = (None,) * len(Keypoints._fields)
def create_dataset(dataset='openpose', data_folder='data', **kwargs):
if dataset.lower() == 'openpose':
return OpenPose(data_folder, **kwargs)
else:
raise ValueError('Unknown dataset: {}'.format(dataset))
def read_keypoints(keypoint_fn, use_hands=True, use_face=True,
use_face_contour=False):
with open(keypoint_fn) as keypoint_file:
data = json.load(keypoint_file)
keypoints = []
gender_pd = []
gender_gt = []
for idx, person_data in enumerate(data['people']):
body_keypoints = np.array(person_data['pose_keypoints_2d'],
dtype=np.float32)
body_keypoints = body_keypoints.reshape([-1, 3])
if use_hands:
left_hand_keyp = np.array(
person_data['hand_left_keypoints_2d'],
dtype=np.float32).reshape([-1, 3])
right_hand_keyp = np.array(
person_data['hand_right_keypoints_2d'],
dtype=np.float32).reshape([-1, 3])
body_keypoints = np.concatenate(
[body_keypoints, left_hand_keyp, right_hand_keyp], axis=0)
if use_face:
# TODO: Make parameters, 17 is the offset for the eye brows,
# etc. 51 is the total number of FLAME compatible landmarks
face_keypoints = np.array(
person_data['face_keypoints_2d'],
dtype=np.float32).reshape([-1, 3])[17: 17 + 51, :]
contour_keyps = np.array(
[], dtype=body_keypoints.dtype).reshape(0, 3)
if use_face_contour:
contour_keyps = np.array(
person_data['face_keypoints_2d'],
dtype=np.float32).reshape([-1, 3])[:17, :]
body_keypoints = np.concatenate(
[body_keypoints, face_keypoints, contour_keyps], axis=0)
if 'gender_pd' in person_data:
gender_pd.append(person_data['gender_pd'])
if 'gender_gt' in person_data:
gender_gt.append(person_data['gender_gt'])
keypoints.append(body_keypoints)
return Keypoints(keypoints=keypoints, gender_pd=gender_pd,
gender_gt=gender_gt)
def generate_cam_Rt(center, direction, right, up):
def normalize_vector(v):
v_norm = np.linalg.norm(v)
return v if v_norm == 0 else v / v_norm
center = center.reshape([-1])
direction = direction.reshape([-1])
right = right.reshape([-1])
up = up.reshape([-1])
rot_mat = np.eye(3)
s = right
s = normalize_vector(s)
rot_mat[0, :] = s
u = up
u = normalize_vector(u)
rot_mat[1, :] = -u
rot_mat[2, :] = normalize_vector(direction)
trans = -np.dot(rot_mat, center)
return rot_mat, trans
class OpenPose(Dataset):
NUM_BODY_JOINTS = 25
NUM_HAND_JOINTS = 20
def __init__(self, data_folder, img_folder='color',
keyp_folder='keypoints',
cam_subpath='meta/cam_data.mat',
use_hands=False,
use_face=False,
dtype=torch.float32,
model_type='smplx',
joints_to_ign=None,
use_face_contour=False,
openpose_format='coco25',
**kwargs):
super(OpenPose, self).__init__()
self.use_hands = use_hands
self.use_face = use_face
self.model_type = model_type
self.dtype = dtype
self.joints_to_ign = joints_to_ign
self.use_face_contour = use_face_contour
self.openpose_format = openpose_format
self.num_joints = (self.NUM_BODY_JOINTS +
2 * self.NUM_HAND_JOINTS * use_hands)
self.img_folder = osp.join(data_folder, img_folder)
self.keyp_folder = osp.join(data_folder, keyp_folder)
self.cam_fpath = osp.join(data_folder, cam_subpath)
self.img_paths = [osp.join(self.img_folder, img_fn)
for img_fn in os.listdir(self.img_folder)
if img_fn.endswith('.png') or
img_fn.endswith('.jpg') and
not img_fn.startswith('.')]
self.img_paths = sorted(self.img_paths)
self.cnt = 0
def get_model2data(self):
return smpl_to_openpose(self.model_type, use_hands=self.use_hands,
use_face=self.use_face,
use_face_contour=self.use_face_contour,
openpose_format=self.openpose_format)
def get_left_shoulder(self):
return 2
def get_right_shoulder(self):
return 5
def get_joint_weights(self):
# The weights for the joint terms in the optimization
optim_weights = np.ones(self.num_joints + 2 * self.use_hands +
self.use_face * 51 +
17 * self.use_face_contour,
dtype=np.float32)
# Neck, Left and right hip
# These joints are ignored because SMPL has no neck joint and the
# annotation of the hips is ambiguous.
if self.joints_to_ign is not None and -1 not in self.joints_to_ign:
optim_weights[self.joints_to_ign] = 0.
return torch.tensor(optim_weights, dtype=self.dtype)
def __len__(self):
return len(self.img_paths)
def __getitem__(self, idx):
img_path = self.img_paths[idx]
return self.read_item(img_path)
def read_item(self, img_path):
# read images
img = cv2.imread(img_path).astype(np.float32)[:, :, :] / 255.0
img_fn = osp.split(img_path)[1]
img_fn, _ = osp.splitext(osp.split(img_path)[1])
# read key points
keypoint_fn = osp.join(self.keyp_folder,
img_fn + '_keypoints.json')
keyp_tuple = read_keypoints(keypoint_fn, use_hands=self.use_hands,
use_face=self.use_face,
use_face_contour=self.use_face_contour)
if len(keyp_tuple.keypoints) < 1:
return {}
keypoints = np.stack(keyp_tuple.keypoints)
output_dict = {'fn': img_fn,
'img_path': img_path,
'keypoints': keypoints,
'img': img}
if keyp_tuple.gender_gt is not None:
if len(keyp_tuple.gender_gt) > 0:
output_dict['gender_gt'] = keyp_tuple.gender_gt
if keyp_tuple.gender_pd is not None:
if len(keyp_tuple.gender_pd) > 0:
output_dict['gender_pd'] = keyp_tuple.gender_pd
# read camera
cam_id = int(img_fn)
cam_data = sio.loadmat(self.cam_fpath)['cam'][0]
cam_param = cam_data[cam_id]
cam_R, cam_t = generate_cam_Rt(
center=cam_param['center'][0, 0], right=cam_param['right'][0, 0],
up=cam_param['up'][0, 0], direction=cam_param['direction'][0, 0])
cam_R = cam_R.astype(np.float32)
cam_t = cam_t.astype(np.float32)
# cam_r = np.float32(cam_data['cam_rs'][cam_id])
# cam_t = np.float32(cam_data['cam_ts'][cam_id])
# cam_R = cv2.Rodrigues(cam_r)[0]
output_dict['cam_id'] = cam_id
output_dict['cam_R'] = np.float32(cam_R)
output_dict['cam_t'] = np.float32(cam_t)
output_dict['cam_fx'] = 5000.0
output_dict['cam_fy'] = 5000.0
output_dict['cam_cx'] = img.shape[1] / 2
output_dict['cam_cy'] = img.shape[0] / 2
return output_dict
def __iter__(self):
return self
def __next__(self):
return self.next()
def next(self):
if self.cnt >= len(self.img_paths):
raise StopIteration
img_path = self.img_paths[self.cnt]
self.cnt += 1
return self.read_item(img_path)