-
Notifications
You must be signed in to change notification settings - Fork 21
/
measure.py
502 lines (394 loc) · 18.5 KB
/
measure.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
from typing import List, Dict
import numpy as np
import trimesh
import torch
import smplx
from pprint import pprint
import os
import argparse
from measurement_definitions import *
from utils import *
from visualize import Visualizer
from landmark_definitions import *
from joint_definitions import *
def set_shape(model, shape_coefs):
'''
Set shape of body model.
:param model: smplx body model
:param shape_coefs: torch.tensor dim (10,)
Return
shaped smplx body model
'''
shape_coefs = shape_coefs.to(torch.float32)
return model(betas=shape_coefs, return_verts=True)
def create_model(model_type, model_root, gender, num_betas=10, num_thetas=24):
'''
Create SMPL/SMPLX/etc. body model
:param model_type: str of model type: smpl, smplx, etc.
:param model_root: str of location where there are smpl/smplx/etc. folders with .pkl models
(clumsy definition in smplx package)
:param gender: str of gender: MALE or FEMALE or NEUTRAL
:param num_betas: int of number of shape coefficients
requires the model with num_coefs in model_root
:param num_thetas: int of number of pose coefficients
Return:
:param smplx body model (SMPL, SMPLX, etc.)
'''
#body_pose = torch.zeros((1, (num_thetas-1) * 3))
return smplx.create(model_path=model_root,
model_type=model_type,
gender=gender,
use_face_contour=False,
num_betas=num_betas,
#body_pose=body_pose,
ext='pkl')
class Measurer():
'''
Measure a parametric body model defined either.
Parent class for Measure{SMPL,SMPLX,..}.
All the measurements are expressed in cm.
'''
def __init__(self):
self.verts = None
self.faces = None
self.joints = None
self.gender = None
self.measurements = {}
self.height_normalized_measurements = {}
self.labeled_measurements = {}
self.height_normalized_labeled_measurements = {}
self.labels2names = {}
def from_verts(self):
pass
def from_body_model(self):
pass
def measure(self,
measurement_names: List[str]
):
'''
Measure the given measurement names from measurement_names list
:param measurement_names - list of strings of defined measurements
to measure from MeasurementDefinitions class
'''
for m_name in measurement_names:
if m_name not in self.all_possible_measurements:
print(f"Measurement {m_name} not defined.")
pass
if m_name in self.measurements:
pass
if self.measurement_types[m_name] == MeasurementType().LENGTH:
value = self.measure_length(m_name)
self.measurements[m_name] = value
elif self.measurement_types[m_name] == MeasurementType().CIRCUMFERENCE:
value = self.measure_circumference(m_name)
self.measurements[m_name] = value
else:
print(f"Measurement {m_name} not defined")
def measure_length(self, measurement_name: str):
'''
Measure distance between 2 landmarks
:param measurement_name: str - defined in MeasurementDefinitions
Returns
:float of measurement in cm
'''
measurement_landmarks_inds = self.length_definitions[measurement_name]
landmark_points = []
for i in range(2):
if isinstance(measurement_landmarks_inds[i],tuple):
# if touple of indices for landmark, take their average
lm = (self.verts[measurement_landmarks_inds[i][0]] +
self.verts[measurement_landmarks_inds[i][1]]) / 2
else:
lm = self.verts[measurement_landmarks_inds[i]]
landmark_points.append(lm)
landmark_points = np.vstack(landmark_points)[None,...]
return self._get_dist(landmark_points)
@staticmethod
def _get_dist(verts: np.ndarray) -> float:
'''
The Euclidean distance between vertices.
The distance is found as the sum of each pair i
of 3D vertices (i,0,:) and (i,1,:)
:param verts: np.ndarray (N,2,3) - vertices used
to find distances
Returns:
:param dist: float, sumed distances between vertices
'''
verts_distances = np.linalg.norm(verts[:, 1] - verts[:, 0],axis=1)
distance = np.sum(verts_distances)
distance_cm = distance * 100 # convert to cm
return distance_cm
def measure_circumference(self,
measurement_name: str,
):
'''
Measure circumferences. Circumferences are defined with
landmarks and joints - the measurement is found by cutting the
SMPL model with the plane defined by a point (landmark point) and
normal (vector connecting the two joints).
:param measurement_name: str - measurement name
Return
float of measurement value in cm
'''
measurement_definition = self.circumf_definitions[measurement_name]
circumf_landmarks = measurement_definition["LANDMARKS"]
circumf_landmark_indices = [self.landmarks[l_name] for l_name in circumf_landmarks]
circumf_n1, circumf_n2 = self.circumf_definitions[measurement_name]["JOINTS"]
circumf_n1, circumf_n2 = self.joint2ind[circumf_n1], self.joint2ind[circumf_n2]
plane_origin = np.mean(self.verts[circumf_landmark_indices,:],axis=0)
plane_normal = self.joints[circumf_n1,:] - self.joints[circumf_n2,:]
mesh = trimesh.Trimesh(vertices=self.verts, faces=self.faces)
# new version
slice_segments, sliced_faces = trimesh.intersections.mesh_plane(mesh,
plane_normal=plane_normal,
plane_origin=plane_origin,
return_faces=True) # (N, 2, 3), (N,)
slice_segments = filter_body_part_slices(slice_segments,
sliced_faces,
measurement_name,
self.circumf_2_bodypart,
self.face_segmentation)
slice_segments_hull = convex_hull_from_3D_points(slice_segments)
return self._get_dist(slice_segments_hull)
def height_normalize_measurements(self, new_height: float):
'''
Scale all measurements so that the height measurement gets
the value of new_height:
new_measurement = (old_measurement / old_height) * new_height
NOTE the measurements and body model remain unchanged, a new
dictionary height_normalized_measurements is created.
Input:
:param new_height: float, the newly defined height.
Return:
self.height_normalized_measurements: dict of
{measurement:value} pairs with
height measurement = new_height, and other measurements
scaled accordingly
'''
if self.measurements != {}:
old_height = self.measurements["height"]
for m_name, m_value in self.measurements.items():
norm_value = (m_value / old_height) * new_height
self.height_normalized_measurements[m_name] = norm_value
if self.labeled_measurements != {}:
for m_name, m_value in self.labeled_measurements.items():
norm_value = (m_value / old_height) * new_height
self.height_normalized_labeled_measurements[m_name] = norm_value
def label_measurements(self,set_measurement_labels: Dict[str, str]):
'''
Create labeled_measurements dictionary with "label: x cm" structure
for each given measurement.
NOTE: This overwrites any prior labeling!
:param set_measurement_labels: dict of labels and measurement names
(example. {"A": "head_circumference"})
'''
if self.labeled_measurements != {}:
print("Overwriting old labels")
self.labeled_measurements = {}
self.labels2names = {}
for set_label, set_name in set_measurement_labels.items():
if set_name not in self.all_possible_measurements:
print(f"Measurement {set_name} not defined.")
pass
if set_name not in self.measurements.keys():
self.measure([set_name])
self.labeled_measurements[set_label] = self.measurements[set_name]
self.labels2names[set_label] = set_name
def visualize(self,
measurement_names: List[str] = [],
landmark_names: List[str] = [],
title="Measurement visualization",
visualize_body: bool = True,
visualize_landmarks: bool = True,
visualize_joints: bool = True,
visualize_measurements: bool=True):
# TODO: create default model if not defined
# if self.verts is None:
# print("Model has not been defined. \
# Visualizing on default male model")
# model = create_model(self.smpl_path, "MALE", num_coefs=10)
# shape = torch.zeros((1, 10), dtype=torch.float32)
# model_output = set_shape(model, shape)
# verts = model_output.vertices.detach().cpu().numpy().squeeze()
# faces = model.faces.squeeze()
# else:
# verts = self.verts
# faces = self.faces
if measurement_names == []:
measurement_names = self.all_possible_measurements
if landmark_names == []:
landmark_names = list(self.landmarks.keys())
vizz = Visualizer(verts=self.verts,
faces=self.faces,
joints=self.joints,
landmarks=self.landmarks,
measurements=self.measurements,
measurement_types=self.measurement_types,
length_definitions=self.length_definitions,
circumf_definitions=self.circumf_definitions,
joint2ind=self.joint2ind,
circumf_2_bodypart=self.circumf_2_bodypart,
face_segmentation=self.face_segmentation,
visualize_body=visualize_body,
visualize_landmarks=visualize_landmarks,
visualize_joints=visualize_joints,
visualize_measurements=visualize_measurements,
title=title
)
vizz.visualize(measurement_names=measurement_names,
landmark_names=landmark_names,
title=title)
class MeasureSMPL(Measurer):
'''
Measure the SMPL model defined either by the shape parameters or
by its 6890 vertices.
All the measurements are expressed in cm.
'''
def __init__(self):
super().__init__()
self.model_type = "smpl"
self.body_model_root = "data"
self.body_model_path = os.path.join(self.body_model_root,
self.model_type)
self.faces = smplx.SMPL(self.body_model_path, ext="pkl").faces
face_segmentation_path = os.path.join(self.body_model_path,
f"{self.model_type}_body_parts_2_faces.json")
self.face_segmentation = load_face_segmentation(face_segmentation_path)
self.landmarks = SMPL_LANDMARK_INDICES
self.measurement_types = MEASUREMENT_TYPES
self.length_definitions = SMPLMeasurementDefinitions().LENGTHS
self.circumf_definitions = SMPLMeasurementDefinitions().CIRCUMFERENCES
self.circumf_2_bodypart = SMPLMeasurementDefinitions().CIRCUMFERENCE_TO_BODYPARTS
self.all_possible_measurements = SMPLMeasurementDefinitions().possible_measurements
self.joint2ind = SMPL_JOINT2IND
self.num_joints = SMPL_NUM_JOINTS
self.num_points = 6890
def from_verts(self,
verts: torch.tensor):
'''
Construct body model from only vertices.
:param verts: torch.tensor (6890,3) of SMPL vertices
'''
verts = verts.squeeze()
error_msg = f"verts need to be of dimension ({self.num_points},3)"
assert verts.shape == torch.Size([self.num_points,3]), error_msg
joint_regressor = get_joint_regressor(self.model_type,
self.body_model_root,
gender="NEUTRAL",
num_thetas=self.num_joints)
joints = torch.matmul(joint_regressor, verts)
self.joints = joints.numpy()
self.verts = verts.numpy()
def from_body_model(self,
gender: str,
shape: torch.tensor):
'''
Construct body model from given gender and shape params
of SMPl model.
:param gender: str, MALE or FEMALE or NEUTRAL
:param shape: torch.tensor, (1,10) beta parameters
for SMPL model
'''
model = create_model(model_type=self.model_type,
model_root=self.body_model_root,
gender=gender,
num_betas=10,
num_thetas=self.num_joints)
model_output = set_shape(model, shape)
self.verts = model_output.vertices.detach().cpu().numpy().squeeze()
self.joints = model_output.joints.squeeze().detach().cpu().numpy()
self.gender = gender
class MeasureSMPLX(Measurer):
'''
Measure the SMPLX model defined either by the shape parameters or
by its 10475 vertices.
All the measurements are expressed in cm.
'''
def __init__(self):
super().__init__()
self.model_type = "smplx"
self.body_model_root = "data"
self.body_model_path = os.path.join(self.body_model_root,
self.model_type)
self.faces = smplx.SMPLX(self.body_model_path, ext="pkl").faces
face_segmentation_path = os.path.join(self.body_model_path,
f"{self.model_type}_body_parts_2_faces.json")
self.face_segmentation = load_face_segmentation(face_segmentation_path)
self.landmarks = SMPLX_LANDMARK_INDICES
self.measurement_types = MEASUREMENT_TYPES
self.length_definitions = SMPLXMeasurementDefinitions().LENGTHS
self.circumf_definitions = SMPLXMeasurementDefinitions().CIRCUMFERENCES
self.circumf_2_bodypart = SMPLXMeasurementDefinitions().CIRCUMFERENCE_TO_BODYPARTS
self.all_possible_measurements = SMPLXMeasurementDefinitions().possible_measurements
self.joint2ind = SMPLX_JOINT2IND
self.num_joints = SMPLX_NUM_JOINTS
self.num_points = 10475
def from_verts(self,
verts: torch.tensor):
'''
Construct body model from only vertices.
:param verts: torch.tensor (10475,3) of SMPLX vertices
'''
verts = verts.squeeze()
error_msg = f"verts need to be of dimension ({self.num_points},3)"
assert verts.shape == torch.Size([self.num_points,3]), error_msg
joint_regressor = get_joint_regressor(self.model_type,
self.body_model_root,
gender="NEUTRAL",
num_thetas=self.num_joints)
joints = torch.matmul(joint_regressor, verts)
self.joints = joints.numpy()
self.verts = verts.numpy()
def from_body_model(self,
gender: str,
shape: torch.tensor):
'''
Construct body model from given gender and shape params
of SMPl model.
:param gender: str, MALE or FEMALE or NEUTRAL
:param shape: torch.tensor, (1,10) beta parameters
for SMPL model
'''
model = create_model(model_type=self.model_type,
model_root=self.body_model_root,
gender=gender,
num_betas=10,
num_thetas=self.num_joints)
model_output = set_shape(model, shape)
self.verts = model_output.vertices.detach().cpu().numpy().squeeze()
self.joints = model_output.joints.squeeze().detach().cpu().numpy()
self.gender = gender
class MeasureBody():
def __new__(cls, model_type):
model_type = model_type.lower()
if model_type == 'smpl':
return MeasureSMPL()
elif model_type == 'smplx':
return MeasureSMPLX()
else:
raise NotImplementedError("Model type not defined")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Measure body models.')
parser.add_argument('--measure_neutral_smpl_with_mean_shape', action='store_true',
help="Measure a mean shape smpl model.")
parser.add_argument('--measure_neutral_smplx_with_mean_shape', action='store_true',
help="Measure a mean shape smplx model.")
args = parser.parse_args()
model_types_to_measure = []
if args.measure_neutral_smpl_with_mean_shape:
model_types_to_measure.append("smpl")
elif args.measure_neutral_smplx_with_mean_shape:
model_types_to_measure.append("smplx")
for model_type in model_types_to_measure:
print(f"Measuring {model_type} body model")
measurer = MeasureBody(model_type)
betas = torch.zeros((1, 10), dtype=torch.float32)
measurer.from_body_model(gender="NEUTRAL", shape=betas)
measurement_names = measurer.all_possible_measurements
measurer.measure(measurement_names)
print("Measurements")
pprint(measurer.measurements)
measurer.label_measurements(STANDARD_LABELS)
print("Labeled measurements")
pprint(measurer.labeled_measurements)
measurer.visualize()