-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathvisualize.py
More file actions
896 lines (719 loc) · 31.7 KB
/
visualize.py
File metadata and controls
896 lines (719 loc) · 31.7 KB
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
import numpy as np
from typing import List
import plotly
import plotly.graph_objects as go
import plotly.express as px
import trimesh
import argparse
import smplx
import json
import torch
#import ipywidgets as widgets
from plotly.subplots import make_subplots
from measurement_definitions import MeasurementType
from utils import convex_hull_from_3D_points, filter_body_part_slices
from joint_definitions import SMPL_IND2JOINT, SMPLX_IND2JOINT
from landmark_definitions import SMPL_LANDMARK_INDICES, SMPLX_LANDMARK_INDICES
class Visualizer():
'''
Visualize the body model with measurements, landmarks and joints.
All the measurements are expressed in cm.
'''
def __init__(self,
verts: np.ndarray,
faces: np.ndarray,
joints: np.ndarray,
landmarks: dict,
measurements: dict,
measurement_types: dict,
length_definitions: dict,
circumf_definitions: dict,
joint2ind: dict,
circumf_2_bodypart: dict,
face_segmentation: dict,
visualize_body: bool = True,
visualize_landmarks: bool = True,
visualize_joints: bool = True,
visualize_measurements: bool=True,
title: str = "Measurement visualization"
):
self.verts = verts
self.faces = faces
self.joints = joints
self.landmarks = landmarks
self.measurements = measurements
self.measurement_types = measurement_types
self.length_definitions = length_definitions
self.circumf_definitions = circumf_definitions
self.joint2ind = joint2ind
self.circumf_2_bodypart = circumf_2_bodypart
self.face_segmentation = face_segmentation
self.visualize_body = visualize_body
self.visualize_landmarks = visualize_landmarks
self.visualize_joints = visualize_joints
self.visualize_measurements = visualize_measurements
self.title = title
@staticmethod
def create_mesh_plot(verts: np.ndarray, faces: np.ndarray):
'''
Visualize smpl body mesh.
:param verts: np.array (N,3) of vertices
:param faces: np.array (F,3) of faces connecting the vertices
Return:
plotly Mesh3d object for plotting
'''
mesh_plot = go.Mesh3d(
x=verts[:,0],
y=verts[:,1],
z=verts[:,2],
color="gray",
hovertemplate ='<i>Index</i>: %{text}',
text = [i for i in range(verts.shape[0])],
# i, j and k give the vertices of triangles
i=faces[:,0],
j=faces[:,1],
k=faces[:,2],
opacity=0.6,
name='body',
)
return mesh_plot
@staticmethod
def create_joint_plot(joints: np.ndarray):
return go.Scatter3d(x = joints[:,0],
y = joints[:,1],
z = joints[:,2],
mode='markers',
marker=dict(size=8,
color="black",
opacity=1,
symbol="cross"
),
name="joints"
)
@staticmethod
def create_wireframe_plot(verts: np.ndarray,faces: np.ndarray):
'''
Given vertices and faces, creates a wireframe of plotly segments.
Used for visualizing the wireframe.
:param verts: np.array (N,3) of vertices
:param faces: np.array (F,3) of faces connecting the verts
'''
i=faces[:,0]
j=faces[:,1]
k=faces[:,2]
triangles = np.vstack((i,j,k)).T
x=verts[:,0]
y=verts[:,1]
z=verts[:,2]
vertices = np.vstack((x,y,z)).T
tri_points = vertices[triangles]
#extract the lists of x, y, z coordinates of the triangle
# vertices and connect them by a "line" by adding None
# this is a plotly convention for plotting segments
Xe = []
Ye = []
Ze = []
for T in tri_points:
Xe.extend([T[k%3][0] for k in range(4)]+[ None])
Ye.extend([T[k%3][1] for k in range(4)]+[ None])
Ze.extend([T[k%3][2] for k in range(4)]+[ None])
# return Xe, Ye, Ze
wireframe = go.Scatter3d(
x=Xe,
y=Ye,
z=Ze,
mode='lines',
name='wireframe',
line=dict(color= 'rgb(70,70,70)', width=1)
)
return wireframe
def create_landmarks_plot(self,
landmark_names: List[str],
verts: np.ndarray
) -> List[plotly.graph_objs.Scatter3d]:
'''
Visualize landmarks from landmark_names list
:param landmark_names: List[str] of landmark names to visualize
Return
:param plots: list of plotly objects to plot
'''
plots = []
landmark_colors = dict(zip(self.landmarks.keys(),
px.colors.qualitative.Alphabet))
for lm_name in landmark_names:
if lm_name not in self.landmarks.keys():
print(f"Landmark {lm_name} is not defined.")
pass
lm_index = self.landmarks[lm_name]
if isinstance(lm_index,tuple):
lm = (verts[lm_index[0]] + verts[lm_index[1]]) / 2
else:
lm = verts[lm_index]
plot = go.Scatter3d(x = [lm[0]],
y = [lm[1]],
z = [lm[2]],
mode='markers',
marker=dict(size=8,
color=landmark_colors[lm_name],
opacity=1,
),
name=lm_name
)
plots.append(plot)
return plots
def create_measurement_length_plot(self,
measurement_name: str,
verts: np.ndarray,
color: str
):
'''
Create length measurement plot.
:param measurement_name: str, measurement name to plot
:param verts: np.array (N,3) of vertices
:param color: str of color to color the measurement
Return
plotly object to plot
'''
measurement_landmarks_inds = self.length_definitions[measurement_name]
segments = {"x":[],"y":[],"z":[]}
for i in range(2):
if isinstance(measurement_landmarks_inds[i],tuple):
lm_tnp = (verts[measurement_landmarks_inds[i][0]] +
verts[measurement_landmarks_inds[i][1]]) / 2
else:
lm_tnp = verts[measurement_landmarks_inds[i]]
segments["x"].append(lm_tnp[0])
segments["y"].append(lm_tnp[1])
segments["z"].append(lm_tnp[2])
for ax in ["x","y","z"]:
segments[ax].append(None)
if measurement_name in self.measurements:
m_viz_name = f"{measurement_name}: {self.measurements[measurement_name]:.2f}cm"
else:
m_viz_name = measurement_name
return go.Scatter3d(x=segments["x"],
y=segments["y"],
z=segments["z"],
marker=dict(
size=4,
color="rgba(0,0,0,0)",
),
line=dict(
color=color,
width=10),
name=m_viz_name
)
def create_measurement_circumference_plot(self,
measurement_name: str,
verts: np.ndarray,
faces: np.ndarray,
color: str):
'''
Create circumference measurement plot
:param measurement_name: str, measurement name to plot
:param verts: np.array (N,3) of vertices
:param faces: np.array (F,3) of faces connecting the vertices
:param color: str of color to color the measurement
Return
plotly object to plot
'''
circumf_landmarks = self.circumf_definitions[measurement_name]["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(verts[circumf_landmark_indices,:],axis=0)
plane_normal = self.joints[circumf_n1,:] - self.joints[circumf_n2,:]
mesh = trimesh.Trimesh(vertices=verts, faces=faces)
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)
draw_segments = {"x":[],"y":[],"z":[]}
map_ax = {0:"x",1:"y",2:"z"}
for i in range(slice_segments_hull.shape[0]):
for j in range(3):
draw_segments[map_ax[j]].append(slice_segments_hull[i,0,j])
draw_segments[map_ax[j]].append(slice_segments_hull[i,1,j])
draw_segments[map_ax[j]].append(None)
if measurement_name in self.measurements:
m_viz_name = f"{measurement_name}: {self.measurements[measurement_name]:.2f}cm"
else:
m_viz_name = measurement_name
return go.Scatter3d(
x=draw_segments["x"],
y=draw_segments["y"],
z=draw_segments["z"],
mode="lines",
line=dict(
color=color,
width=10),
name=m_viz_name
)
def visualize(self,
measurement_names: List[str] = [],
landmark_names: List[str] = [],
title="Measurement visualization"
):
'''
Visualize the body model with measurements, landmarks and joints.
:param measurement_names: List[str], list of strings with measurement names
:param landmark_names: List[str], list of strings with landmark names
:param title: str, title of plot
'''
fig = go.Figure()
if self.visualize_body:
# visualize model mesh
mesh_plot = self.create_mesh_plot(self.verts, self.faces)
fig.add_trace(mesh_plot)
# visualize wireframe
wireframe_plot = self.create_wireframe_plot(self.verts, self.faces)
fig.add_trace(wireframe_plot)
# visualize joints
if self.visualize_joints:
joint_plot = self.create_joint_plot(self.joints)
fig.add_trace(joint_plot)
# visualize landmarks
if self.visualize_landmarks:
landmarks_plot = self.create_landmarks_plot(landmark_names, self.verts)
fig.add_traces(landmarks_plot)
# visualize measurements
measurement_colors = dict(zip(self.measurement_types.keys(),
px.colors.qualitative.Alphabet))
if self.visualize_measurements:
for m_name in measurement_names:
if m_name not in self.measurement_types.keys():
print(f"Measurement {m_name} not defined.")
pass
if self.measurement_types[m_name] == MeasurementType().LENGTH:
measurement_plot = self.create_measurement_length_plot(measurement_name=m_name,
verts=self.verts,
color=measurement_colors[m_name])
elif self.measurement_types[m_name] == MeasurementType().CIRCUMFERENCE:
measurement_plot = self.create_measurement_circumference_plot(measurement_name=m_name,
verts=self.verts,
faces=self.faces,
color=measurement_colors[m_name])
fig.add_trace(measurement_plot)
fig.update_layout(scene_aspectmode='data',
width=1000, height=700,
title=title,
)
fig.show()
def viz_smplx_joints(visualize_body=True,fig=None,show=True,title="SMPLX joints"):
"""
Visualize smpl joints on the same plot.
:param visualize_body: bool, whether to visualize the body or not.
:param fig: plotly Figure object, if None, create new figure.
"""
betas = torch.zeros((1, 10), dtype=torch.float32)
smplx_model = smplx.create(model_path="data",
model_type="smplx",
gender="NEUTRAL",
use_face_contour=False,
num_betas=10,
#body_pose=torch.zeros((1, (55-1) * 3)),
ext='pkl')
smplx_model = smplx_model(betas=betas, return_verts=True)
smplx_joints = smplx_model.joints.detach().numpy()[0]
smplx_joint_pelvis = smplx_joints[0,:]
smplx_joints = smplx_joints - smplx_joint_pelvis
smplx_vertices = smplx_model.vertices.detach().numpy()[0]
smplx_vertices = smplx_vertices - smplx_joint_pelvis
smplx_faces = smplx.SMPLX("data/smplx",ext="pkl").faces
joint_colors = px.colors.qualitative.Alphabet + \
px.colors.qualitative.Dark24 + \
px.colors.qualitative.Alphabet + \
px.colors.qualitative.Dark24 + \
px.colors.qualitative.Alphabet + \
["#000000"]
if isinstance(fig,type(None)):
fig = go.Figure()
for i in range(smplx_joints.shape[0]):
if i in SMPLX_IND2JOINT.keys():
joint_name = SMPLX_IND2JOINT[i]
else:
joint_name = f"noname-{i}"
joint_plot = go.Scatter3d(x = [smplx_joints[i,0]],
y = [smplx_joints[i,1]],
z = [smplx_joints[i,2]],
mode='markers',
marker=dict(size=10,
color=joint_colors[i],
opacity=1,
symbol="circle"
),
name="smplx-"+joint_name
)
fig.add_trace(joint_plot)
if visualize_body:
plot_body = go.Mesh3d(
x=smplx_vertices[:,0],
y=smplx_vertices[:,1],
z=smplx_vertices[:,2],
color = "red",
i=smplx_faces[:,0],
j=smplx_faces[:,1],
k=smplx_faces[:,2],
name='smplx mesh',
showscale=True,
opacity=0.5
)
fig.add_trace(plot_body)
fig.update_layout(scene_aspectmode='data',
width=1000, height=700,
title=title,
)
if show:
fig.show()
else:
return fig
def viz_smpl_joints(visualize_body=True,fig=None,show=True,title="SMPL joints"):
"""
Visualize smpl joints on the same plot.
:param visualize_body: bool, whether to visualize the body or not.
:param fig: plotly Figure object, if None, create new figure.
"""
betas = torch.zeros((1, 10), dtype=torch.float32)
smpl_model = smplx.create(model_path="data",
model_type="smpl",
gender="NEUTRAL",
use_face_contour=False,
num_betas=10,
ext='pkl')
smpl_model = smpl_model(betas=betas, return_verts=True)
smpl_joints = smpl_model.joints.detach().numpy()[0]
smpl_joints_pelvis = smpl_joints[0,:]
smpl_joints = smpl_joints - smpl_joints_pelvis
smpl_vertices = smpl_model.vertices.detach().numpy()[0]
smpl_vertices = smpl_vertices - smpl_joints_pelvis
smpl_faces = smplx.SMPL("data/smpl",ext="pkl").faces
joint_colors = px.colors.qualitative.Alphabet + \
px.colors.qualitative.Dark24 + \
px.colors.qualitative.Alphabet + \
px.colors.qualitative.Dark24 + \
px.colors.qualitative.Alphabet + \
["#000000"]
if isinstance(fig,type(None)):
fig = go.Figure()
for i in range(smpl_joints.shape[0]):
if i in SMPL_IND2JOINT.keys():
joint_name = SMPL_IND2JOINT[i]
else:
joint_name = f"noname-{i}"
joint_plot = go.Scatter3d(x = [smpl_joints[i,0]],
y = [smpl_joints[i,1]],
z = [smpl_joints[i,2]],
mode='markers',
marker=dict(size=10,
color=joint_colors[i],
opacity=1,
symbol="cross"
),
name="smpl-"+joint_name
)
fig.add_trace(joint_plot)
if visualize_body:
plot_body = go.Mesh3d(
x=smpl_vertices[:,0],
y=smpl_vertices[:,1],
z=smpl_vertices[:,2],
#facecolor=face_colors,
color = "blue",
i=smpl_faces[:,0],
j=smpl_faces[:,1],
k=smpl_faces[:,2],
name='smpl mesh',
showscale=True,
opacity=0.5
)
fig.add_trace(plot_body)
fig.update_layout(scene_aspectmode='data',
width=1000, height=700,
title=title,
)
if show:
fig.show()
else:
return fig
def viz_face_segmentation(verts,faces,face_colors,
title="Segmented body",name="mesh",show=True):
"""
Visualize face segmentation defined in face_colors.
:param verts: np.ndarray - (N,3) representing the vertices
:param faces: np.ndarray - (F,3) representing the indices of the faces
:param face_colors: np.ndarray - (F,3) representing the colors of the faces
"""
import plotly.graph_objects as go
fig = go.Figure()
mesh_plot = go.Mesh3d(
x=verts[:,0],
y=verts[:,1],
z=verts[:,2],
facecolor=face_colors,
i=faces[:,0],
j=faces[:,1],
k=faces[:,2],
name=name,
showscale=True,
opacity=1
)
fig.add_trace(mesh_plot)
fig.update_layout(scene_aspectmode='data',
width=1000, height=700,
title=title)
if show:
fig.show()
else:
return fig
def viz_smpl_face_segmentation(fig=None, show=True, title="SMPL face segmentation"):
body = smplx.SMPL("data/smpl",ext="pkl")
with open("data/smpl/smpl_body_parts_2_faces.json","r") as f:
face_segmentation = json.load(f)
faces = body.faces
verts = body.v_template
# create colors for each face
colors = px.colors.qualitative.Alphabet + \
px.colors.qualitative.Dark24
mapping_bp2ind = dict(zip(face_segmentation.keys(),
range(len(face_segmentation.keys()))
))
face_colors = [0]*faces.shape[0]
for bp_name,bp_indices in face_segmentation.items():
bp_label = mapping_bp2ind[bp_name]
for i in bp_indices:
face_colors[i] = colors[bp_label]
if isinstance(fig,type(None)):
fig = go.Figure()
fig = viz_face_segmentation(verts,faces,face_colors,title=title,name="smpl",show=False)
if show:
fig.show()
else:
return fig
def viz_smplx_face_segmentation(fig=None,show=True,title="SMPLX face segmentation"):
"""
Visualize face segmentations for smplx.
"""
body = smplx.SMPLX("data/smplx",ext="pkl")
with open("data/smplx/smplx_body_parts_2_faces.json","r") as f:
face_segmentation = json.load(f)
faces = body.faces
verts = body.v_template
# create colors for each face
colors = px.colors.qualitative.Alphabet + \
px.colors.qualitative.Dark24
mapping_bp2ind = dict(zip(face_segmentation.keys(),
range(len(face_segmentation.keys()))
))
face_colors = [0]*faces.shape[0]
for bp_name,bp_indices in face_segmentation.items():
bp_label = mapping_bp2ind[bp_name]
for i in bp_indices:
face_colors[i] = colors[bp_label]
if isinstance(fig,type(None)):
fig = go.Figure()
fig = viz_face_segmentation(verts,faces,face_colors,title=title,name="smpl",show=False)
if show:
fig.show()
else:
return fig
def viz_point_segmentation(verts,point_segm,title="Segmented body",fig=None,show=True):
"""
Visualze points and their segmentation defined in dict point_segm.
:param verts: np.ndarray - (N,3) representing the vertices
:param point_segm: dict - dict mapping body part to all points belonging
to it
"""
colors = px.colors.qualitative.Alphabet + \
px.colors.qualitative.Dark24
if isinstance(fig,type(None)):
fig = go.Figure()
for i, (body_part, body_indices) in enumerate(point_segm.items()):
plot = go.Scatter3d(x = verts[body_indices,0],
y = verts[body_indices,1],
z = verts[body_indices,2],
mode='markers',
marker=dict(size=5,
color=colors[i],
opacity=1,
#symbol="cross"
),
name=body_part
)
fig.add_trace(plot)
fig.update_layout(scene_aspectmode='data',
width=1000, height=700,
title=title)
if show:
fig.show()
return fig
def viz_smplx_point_segmentation(fig=None,show=True,title="SMPLX point segmentation"):
"""
Visualize point segmentations for smplx.
"""
model_path = "data/smplx"
smpl_verts = smplx.SMPLX(model_path,ext="pkl").v_template
with open("data/smplx/point_segmentation_meshcapade.json","r") as f:
point_segm = json.load(f)
fig = viz_point_segmentation(smpl_verts,point_segm,title=title,fig=fig,show=show)
if show:
fig.show()
else:
return fig
def viz_smpl_point_segmentation(fig=None,show=True,title="SMPL point segmentation"):
"""
Visualize point segmentations for smpl.
"""
model_path = "data/smpl"
smpl_verts = smplx.SMPL(model_path,ext="pkl").v_template
with open("data/smpl/point_segmentation_meshcapade.json","r") as f:
point_segm = json.load(f)
fig = viz_point_segmentation(smpl_verts,point_segm,title=title,fig=fig,show=show)
if show:
fig.show()
else:
return fig
def viz_landmarks(verts,landmark_dict,title="Visualize landmarks",fig=None,show=True,name="points"):
if isinstance(fig,type(None)):
fig = go.Figure()
plot = go.Scatter3d(x = verts[:,0],
y = verts[:,1],
z = verts[:,2],
mode='markers',
hovertemplate ='<i>Index</i>: %{text}',
text = [i for i in range(verts.shape[0])],
marker=dict(size=5,
color="black",
opacity=0.2,
# line=dict(color='black',width=1)
),
name=name
)
fig.add_trace(plot)
colors = px.colors.qualitative.Alphabet + \
px.colors.qualitative.Dark24 + \
px.colors.qualitative.Alphabet + \
px.colors.qualitative.Dark24
for i, (lm_name, lm_ind) in enumerate(landmark_dict.items()):
plot = go.Scatter3d(x = [verts[lm_ind,0]],
y = [verts[lm_ind,1]],
z = [verts[lm_ind,2]],
mode='markers',
marker=dict(size=10,
color=colors[i],
opacity=1,
symbol="cross"
),
name=name+"-"+lm_name
)
fig.add_trace(plot)
fig.update_layout(scene_aspectmode='data',
width=1000, height=700,
title=title)
if show:
fig.show()
else:
return fig
def viz_smpl_landmarks(fig=None,show=True,title="SMPL landmarks"):
"""
Visualize smpl landmarks.
"""
verts = smplx.SMPL("data/smpl",ext="pkl").v_template
landmark_dict = SMPL_LANDMARK_INDICES
if isinstance(fig,type(None)):
fig=go.Figure()
fig = viz_landmarks(verts,
landmark_dict,
title="Visualize landmarks",
fig=fig,
show=show,
name="smpl")
if show:
fig.show()
else:
return fig
def viz_smplx_landmarks(fig=None,show=True,title="SMPLX landmarks"):
"""
Visualize smplx landmarks.
"""
verts = smplx.SMPLX("data/smplx",ext="pkl").v_template
landmark_dict = SMPLX_LANDMARK_INDICES
if isinstance(fig,type(None)):
fig=go.Figure()
fig = viz_landmarks(verts,
landmark_dict,
title="Visualize landmarks",
fig=fig,
show=show,
name="smplx")
if show:
fig.show()
else:
return fig
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Visualize body models, joints and segmentations..')
parser.add_argument('--visualize_smpl_and_smplx_face_segmentation', action='store_true',
help="Visualize face segmentations for smplx model.")
parser.add_argument('--visualize_smpl_and_smplx_joints', action='store_true',
help="visualize smpl and smplx joints on same plot.")
parser.add_argument('--visualize_smpl_and_smplx_point_segmentation', action='store_true',
help="visualize smpl and smplx point segmentation on two separate plots.")
parser.add_argument('--visualize_smpl_and_smplx_landmarks', action='store_true',
help="visualize smpl and smplx landmarks on two separate plots.")
args = parser.parse_args()
if args.visualize_smpl_and_smplx_face_segmentation:
# mesh is not compatible with subplots so these are plotted
# onto separate plots
viz_smpl_face_segmentation(fig=None, show=True)
viz_smplx_face_segmentation(fig=None,show=True)
if args.visualize_smpl_and_smplx_joints:
title = "SMPL and SMPLX joints"
fig = viz_smpl_joints(visualize_body=True,
fig=None,
show=False,
title=title)
viz_smplx_joints(visualize_body=True,
fig=fig,
show=True,
title=title)
if args.visualize_smpl_and_smplx_point_segmentation:
fig = make_subplots(rows=1, cols=2,
specs=[[{'type': 'scene'},
{'type': 'scene'}]],
subplot_titles=("SMPL", "SMPLX"))
title="SMPL and SMPLX point segmentation"
fig_smpl = viz_smpl_point_segmentation(fig=None,show=False,title=title)
fig_smplx = viz_smplx_point_segmentation(fig=None,show=False,title=title)
for i in range(len(fig_smpl.data)):
fig.add_trace(fig_smpl.data[i],row=1,col=1)
for i in range(len(fig_smplx.data)):
fig.add_trace(fig_smplx.data[i],row=1,col=2)
fig.update_layout(fig_smpl.layout)
fig.update_layout(scene2_aspectmode="data",
showlegend=False,
width=1200,
height=700)
fig.show()
if args.visualize_smpl_and_smplx_landmarks:
fig = make_subplots(rows=1, cols=2,
specs=[[{'type': 'scene'},
{'type': 'scene'}]],
subplot_titles=("SMPL", "SMPLX"))
title="SMPL and SMPLX landmarks"
fig_smpl = viz_smpl_landmarks(fig=None,show=False,title=title)
fig_smplx = viz_smplx_landmarks(fig=None,show=False,title=title)
for i in range(len(fig_smpl.data)):
fig.add_trace(fig_smpl.data[i],row=1,col=1)
for i in range(len(fig_smplx.data)):
fig.add_trace(fig_smplx.data[i],row=1,col=2)
fig.update_layout(fig_smpl.layout)
fig.update_layout(scene2_aspectmode="data",
showlegend=False,
width=1200,
height=700)
fig.show()