-
Notifications
You must be signed in to change notification settings - Fork 0
/
skeleton_graph.py
858 lines (731 loc) · 28.9 KB
/
skeleton_graph.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
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
import re
from enum import IntEnum
from mediapipe.python.solutions.face_mesh_connections import FACEMESH_CONTOURS
from mediapipe.python.solutions.hands_connections import HAND_CONNECTIONS
from mediapipe.python.solutions.pose_connections import POSE_CONNECTIONS
from mediapipe.python.solutions.pose import PoseLandmark
from mediapipe.python.solutions.hands import HandLandmark
def atoi(text):
return int(text) if text.isdigit() else text
def natural_keys(text):
'''
alist.sort(key=natural_keys) sorts in human order
http://nedbatchelder.com/blog/200712/human_sorting.html
'''
return [atoi(c) for c in re.split(r'(\d+)', text)]
def prefix(prefix, ls):
'''
prefix(ls) prefixs all the elements in a list
'''
return [prefix + "_" + str(v) for v in ls]
class FaceLandmark(IntEnum):
LEFT_LOWER_EYEBROW_INNER = 285
LEFT_LOWER_EYEBROW_INNER_SECOND = 295
LEFT_LOWER_EYEBROW_OUTER = 276
LEFT_UPPER_EYEBROW_INNER = 336
LEFT_UPPER_EYEBROW_OUTER = 300
RIGHT_LOWER_EYEBROW_INNER = 55
RIGHT_LOWER_EYEBROW_INNER_SECOND = 65
RIGHT_LOWER_EYEBROW_OUTER = 46
RIGHT_UPPER_EYEBROW_INNER = 107
RIGHT_UPPER_EYEBROW_OUTER = 70
LEFT_EYE_INNER = 362
LEFT_EYE_INNER_SECOND = 382
RIGHT_EYE_INNER = 133
RIGHT_EYE_INNER_SECOND = 155
LIPS_OUTTER = 0
LIPS_INNER = 13
class Graph:
def __init__(self, nodes, edges=[]):
self.nodes = nodes.copy()
self.num_nodes = len(nodes)
self.edges = edges.copy()
self.num_edges = len(edges)
self.adj = [[0 for _ in range(self.num_nodes)]
for _ in range(self.num_nodes)]
for a, b in edges:
a_idx = self.nodes.index(a)
b_idx = self.nodes.index(b)
self.adj[a_idx][b_idx] = 1
self.adj[b_idx][a_idx] = 1
# Function to add a bidirectional edge to the graph
def add_edge(self, start, end):
a_idx = self.nodes.index(start)
b_idx = self.nodes.index(end)
self.adj[a_idx][b_idx] = 1
self.adj[b_idx][a_idx] = 1
self.edges.append((start, end))
# Function to remove a bidirectional edge to the graph
def remove_edge(self, start, end):
a_idx = self.nodes.index(start)
b_idx = self.nodes.index(end)
self.adj[a_idx][b_idx] = 0
self.adj[b_idx][a_idx] = 0
idx = self.edges.index((start, end))
del self.edges[idx]
# Function to add a bidirectional edge to the graph by index
def add_edge_by_index(self, a_idx, b_idx):
a = self.nodes[a_idx]
b = self.nodes[b_idx]
self.adj[a_idx][b_idx] = 1
self.adj[b_idx][a_idx] = 1
self.edges.append((a, b))
# Function to remove a bidirectional edge to the graph by index
def remove_edge_by_index(self, a_idx, b_idx):
a = self.nodes[a_idx]
b = self.nodes[b_idx]
self.adj[a_idx][b_idx] = 0
self.adj[b_idx][a_idx] = 0
idx = self.edges.index((a, b))
del self.edges[idx]
# Function to visit a node
def visit_by_index(self, start_idx, visited):
# Init path with the start node
path = [start_idx]
# Set current node as visited
visited[start_idx] = True
# For every node of the graph
for i in range(self.num_nodes):
if self.adj[start_idx][i] == 1 and not visited[i]:
path = path + self.visit_by_index(i, visited) + [start_idx]
return path
# Function to perform DFS on the graph
# Returns a list of nodes' indexes
def dfs_by_index(self, start_idx):
paths = []
visited = [False] * self.num_nodes
while True:
paths.append(self.visit_by_index(start_idx, visited))
if False in visited:
start_idx = visited.index(False)
else:
break
return paths
def tssi_legacy(debug=False):
RIGHT_EYEBROW_JOINTS = [46, 52, 53, 55, 65]
LEFT_EYEBROW_JOINTS = [276, 282, 283, 285, 295]
LIPS_INNER_JOINTS = [13, 14, 78, 80, 81, 82, 87, 88,
95, 178, 191, 308, 310, 311, 312, 317, 318, 324, 402, 415]
POSE_FACE_JOINTS = [
PoseLandmark.NOSE.value,
PoseLandmark.RIGHT_EYE_OUTER.value,
PoseLandmark.RIGHT_EYE.value,
PoseLandmark.RIGHT_EYE_INNER.value,
PoseLandmark.LEFT_EYE_OUTER.value,
PoseLandmark.LEFT_EYE.value,
PoseLandmark.LEFT_EYE_INNER.value
]
POSE_BODY_JOINTS = [
PoseLandmark.RIGHT_SHOULDER.value,
PoseLandmark.RIGHT_ELBOW.value,
# PoseLandmark.RIGHT_WRIST.value,
PoseLandmark.RIGHT_HIP.value,
PoseLandmark.LEFT_SHOULDER.value,
PoseLandmark.LEFT_ELBOW.value,
# PoseLandmark.LEFT_WRIST.value,
PoseLandmark.LEFT_HIP.value
]
HAND_JOINTS = [
HandLandmark.WRIST.value,
HandLandmark.THUMB_CMC.value,
HandLandmark.THUMB_MCP.value,
HandLandmark.THUMB_IP.value,
HandLandmark.THUMB_TIP.value,
HandLandmark.INDEX_FINGER_MCP.value,
HandLandmark.INDEX_FINGER_PIP.value,
HandLandmark.INDEX_FINGER_DIP.value,
HandLandmark.INDEX_FINGER_TIP.value,
HandLandmark.MIDDLE_FINGER_MCP.value,
HandLandmark.MIDDLE_FINGER_PIP.value,
HandLandmark.MIDDLE_FINGER_DIP.value,
HandLandmark.MIDDLE_FINGER_TIP.value,
HandLandmark.RING_FINGER_MCP.value,
HandLandmark.RING_FINGER_PIP.value,
HandLandmark.RING_FINGER_DIP.value,
HandLandmark.RING_FINGER_TIP.value,
HandLandmark.PINKY_MCP.value,
HandLandmark.PINKY_PIP.value,
HandLandmark.PINKY_DIP.value,
HandLandmark.PINKY_TIP.value
]
FACE_JOINTS = RIGHT_EYEBROW_JOINTS + LEFT_EYEBROW_JOINTS + LIPS_INNER_JOINTS
POSE_JOINTS = POSE_FACE_JOINTS + POSE_BODY_JOINTS
FILTERED_FACEMESH_CONNECTIONS = [(u, v) for (
u, v) in FACEMESH_CONTOURS if u in FACE_JOINTS and v in FACE_JOINTS]
FILTERED_POSE_CONNECTIONS = [(u, v) for (
u, v) in POSE_CONNECTIONS if u in POSE_JOINTS and v in POSE_JOINTS]
joints = ['root'] + prefix('face', RIGHT_EYEBROW_JOINTS) + prefix('face', LEFT_EYEBROW_JOINTS) + prefix('pose', POSE_FACE_JOINTS) + prefix(
'face', LIPS_INNER_JOINTS) + prefix('pose', POSE_BODY_JOINTS) + prefix('rightHand', HAND_JOINTS) + prefix('leftHand', HAND_JOINTS) + ['midhip']
# Define graph
graph = Graph(joints)
# Setup connections
for connection in FILTERED_FACEMESH_CONNECTIONS:
start_id, end_id = connection
start = "face_" + str(start_id)
end = "face_" + str(end_id)
graph.add_edge(start, end)
for connection in HAND_CONNECTIONS:
start_id, end_id = connection
start = "leftHand_" + str(start_id)
end = "leftHand_" + str(end_id)
graph.add_edge(start, end)
for connection in HAND_CONNECTIONS:
start_id, end_id = connection
start = "rightHand_" + str(start_id)
end = "rightHand_" + str(end_id)
graph.add_edge(start, end)
for connection in FILTERED_POSE_CONNECTIONS:
start_id, end_id = connection
start = "pose_" + str(start_id)
end = "pose_" + str(end_id)
graph.add_edge(start, end)
# join the nose with the left eyebrow
graph.add_edge(
"pose_" + str(PoseLandmark.NOSE.value),
"face_" + str(FaceLandmark.LEFT_LOWER_EYEBROW_INNER.value))
# join the nose with the right eyebrow
graph.add_edge(
"pose_" + str(PoseLandmark.NOSE.value),
"face_" + str(FaceLandmark.RIGHT_LOWER_EYEBROW_INNER.value))
# join the nose with the inner shape of the lips
graph.add_edge(
"pose_" + str(PoseLandmark.NOSE.value),
"face_" + str(FaceLandmark.LIPS_INNER.value))
# join the pose left wrist to the left wrist of the hand
# graph.add_edge(
# "pose_" + str(PoseLandmark.LEFT_WRIST.value),
# "leftHand_" + str(HandLandmark.WRIST.value))
# join the pose right wrist to the right wrist of the hand
# graph.add_edge(
# "pose_" + str(PoseLandmark.RIGHT_WRIST.value),
# "rightHand_" + str(HandLandmark.WRIST.value))
# join the pose left elbow to the left wrist of the hand
graph.add_edge(
"pose_" + str(PoseLandmark.LEFT_ELBOW.value),
"leftHand_" + str(HandLandmark.WRIST.value))
# join the pose right elbow to the right wrist of the hand
graph.add_edge(
"pose_" + str(PoseLandmark.RIGHT_ELBOW.value),
"rightHand_" + str(HandLandmark.WRIST.value))
# join the ROOT with the nose
graph.add_edge(
"root",
"pose_" + str(PoseLandmark.NOSE.value))
# join the ROOT with the left shoulder
graph.add_edge(
"root",
"pose_" + str(PoseLandmark.LEFT_SHOULDER.value))
# join the ROOT with the right shoulder
graph.add_edge(
"root",
"pose_" + str(PoseLandmark.RIGHT_SHOULDER.value))
# REMOVE the connection between the left shoulder and the right shoulder
graph.remove_edge(
"pose_" + str(PoseLandmark.LEFT_SHOULDER.value),
"pose_" + str(PoseLandmark.RIGHT_SHOULDER.value))
# REMOVE the connection between the left shoulder and the left hip
graph.remove_edge(
"pose_" + str(PoseLandmark.LEFT_SHOULDER.value),
"pose_" + str(PoseLandmark.LEFT_HIP.value))
# REMOVE the connection between the right shoulder and the right hip
graph.remove_edge(
"pose_" + str(PoseLandmark.RIGHT_SHOULDER.value),
"pose_" + str(PoseLandmark.RIGHT_HIP.value))
# REMOVE the connection between the left hip and the right hip
graph.remove_edge(
"pose_" + str(PoseLandmark.LEFT_HIP.value),
"pose_" + str(PoseLandmark.RIGHT_HIP.value))
# ADD the connection between the root and the midhip
graph.add_edge(
"root",
"midhip")
# ADD the connection between the root and the left hip
graph.add_edge(
"midhip",
"pose_" + str(PoseLandmark.LEFT_HIP.value))
# ADD the connection between the root and the right hip
graph.add_edge(
"midhip",
"pose_" + str(PoseLandmark.RIGHT_HIP.value))
# Perform DFS starting at the root
root_index = graph.nodes.index("root")
paths = graph.dfs_by_index(root_index)
tree_path = [graph.nodes[i] for path in paths[:1] for i in path]
# Debug info
info = [
("ROOT:", tree_path.index("root")),
("NOSE:", tree_path.index("pose_0")),
("RIGHT EYEBROW:", tree_path.index("face_" +
str(FaceLandmark.RIGHT_LOWER_EYEBROW_INNER.value))),
("LEFT EYEBROW:", tree_path.index("face_" +
str(FaceLandmark.LEFT_LOWER_EYEBROW_INNER.value))),
("RIGHT EYE INNER:", tree_path.index(
"pose_" + str(PoseLandmark.RIGHT_EYE_INNER.value))),
("LEFT EYE INNER:", tree_path.index(
"pose_" + str(PoseLandmark.LEFT_EYE_INNER.value))),
("MOUTH (INNER LIPS):", tree_path.index(
"face_" + str(FaceLandmark.LIPS_INNER.value))),
("RIGHT SHOULDER:", tree_path.index(
"pose_" + str(PoseLandmark.RIGHT_SHOULDER.value))),
("RIGHT WRIST:", tree_path.index(
"rightHand_" + str(HandLandmark.WRIST.value))),
("LEFT SHOULDER:", tree_path.index(
"pose_" + str(int(PoseLandmark.LEFT_SHOULDER)))),
("LEFT WRIST:", tree_path.index(
"leftHand_" + str(HandLandmark.WRIST.value))),
]
info.sort(key=lambda x: x[1])
if debug:
print(info)
return graph, tree_path
def tssi_v2(debug=False):
RIGHT_EYEBROW_JOINTS = [46, 52, 53, 65]
LEFT_EYEBROW_JOINTS = [276, 282, 283, 295]
RIGHT_EYE = [7, 159, 155, 145]
LEFT_EYE = [382, 386, 249, 374]
MOUTH = [324, 13, 78, 14]
BODY_JOINTS = [
PoseLandmark.RIGHT_SHOULDER.value,
PoseLandmark.RIGHT_ELBOW.value,
PoseLandmark.LEFT_SHOULDER.value,
PoseLandmark.LEFT_ELBOW.value
]
HAND_JOINTS = [
HandLandmark.WRIST.value,
HandLandmark.THUMB_CMC.value,
HandLandmark.THUMB_MCP.value,
HandLandmark.THUMB_IP.value,
HandLandmark.THUMB_TIP.value,
HandLandmark.INDEX_FINGER_MCP.value,
HandLandmark.INDEX_FINGER_PIP.value,
HandLandmark.INDEX_FINGER_DIP.value,
HandLandmark.INDEX_FINGER_TIP.value,
HandLandmark.MIDDLE_FINGER_MCP.value,
HandLandmark.MIDDLE_FINGER_PIP.value,
HandLandmark.MIDDLE_FINGER_DIP.value,
HandLandmark.MIDDLE_FINGER_TIP.value,
HandLandmark.RING_FINGER_MCP.value,
HandLandmark.RING_FINGER_PIP.value,
HandLandmark.RING_FINGER_DIP.value,
HandLandmark.RING_FINGER_TIP.value,
HandLandmark.PINKY_MCP.value,
HandLandmark.PINKY_PIP.value,
HandLandmark.PINKY_DIP.value,
HandLandmark.PINKY_TIP.value
]
FACE_JOINTS = RIGHT_EYEBROW_JOINTS + \
LEFT_EYEBROW_JOINTS + RIGHT_EYE + LEFT_EYE + MOUTH
FILTERED_FACEMESH_CONNECTIONS = [(u, v) for (
u, v) in FACEMESH_CONTOURS if u in FACE_JOINTS and v in FACE_JOINTS]
FILTERED_POSE_CONNECTIONS = [(u, v) for (
u, v) in POSE_CONNECTIONS if u in BODY_JOINTS and v in BODY_JOINTS]
joints = ['root', 'pose_0'] + prefix('face', FACE_JOINTS) + prefix(
'pose', BODY_JOINTS) + prefix('rightHand', HAND_JOINTS) + prefix('leftHand', HAND_JOINTS)
# Define graph
graph = Graph(joints)
# Setup connections
for connection in FILTERED_FACEMESH_CONNECTIONS:
start_id, end_id = connection
start = "face_" + str(start_id)
end = "face_" + str(end_id)
graph.add_edge(start, end)
for connection in HAND_CONNECTIONS:
start_id, end_id = connection
start = "leftHand_" + str(start_id)
end = "leftHand_" + str(end_id)
graph.add_edge(start, end)
for connection in HAND_CONNECTIONS:
start_id, end_id = connection
start = "rightHand_" + str(start_id)
end = "rightHand_" + str(end_id)
graph.add_edge(start, end)
for connection in FILTERED_POSE_CONNECTIONS:
start_id, end_id = connection
start = "pose_" + str(start_id)
end = "pose_" + str(end_id)
graph.add_edge(start, end)
# ADD the connections in the left eye
graph.add_edge(
"face_" + str(155),
"face_" + str(159))
graph.add_edge(
"face_" + str(159),
"face_" + str(7))
graph.add_edge(
"face_" + str(7),
"face_" + str(145))
graph.add_edge(
"face_" + str(145),
"face_" + str(155))
# ADD the connections in the right eye
graph.add_edge(
"face_" + str(382),
"face_" + str(386))
graph.add_edge(
"face_" + str(386),
"face_" + str(249))
graph.add_edge(
"face_" + str(249),
"face_" + str(374))
graph.add_edge(
"face_" + str(374),
"face_" + str(382))
# ADD the connections in the mouth
graph.add_edge(
"face_" + str(13),
"face_" + str(78))
graph.add_edge(
"face_" + str(78),
"face_" + str(14))
graph.add_edge(
"face_" + str(14),
"face_" + str(324))
graph.add_edge(
"face_" + str(324),
"face_" + str(13))
# ADD the connection between the nose and the left eyebrow
graph.add_edge(
"pose_" + str(PoseLandmark.NOSE.value),
"face_" + str(FaceLandmark.LEFT_LOWER_EYEBROW_INNER_SECOND.value))
# ADD the connection between the nose and the right eyebrow
graph.add_edge(
"pose_" + str(PoseLandmark.NOSE.value),
"face_" + str(FaceLandmark.RIGHT_LOWER_EYEBROW_INNER_SECOND.value))
# ADD the connection between the nose and the left eye
graph.add_edge(
"pose_" + str(PoseLandmark.NOSE.value),
"face_" + str(FaceLandmark.LEFT_EYE_INNER_SECOND.value))
# ADD the connection between the nose and the right eye
graph.add_edge(
"pose_" + str(PoseLandmark.NOSE.value),
"face_" + str(FaceLandmark.RIGHT_EYE_INNER_SECOND.value))
# ADD the connection between the nose and the mouth
graph.add_edge(
"pose_" + str(PoseLandmark.NOSE.value),
"face_" + str(FaceLandmark.LIPS_INNER.value))
# ADD the connection between the left elbow and the left wrist
graph.add_edge(
"pose_" + str(PoseLandmark.LEFT_ELBOW.value),
"leftHand_" + str(HandLandmark.WRIST.value))
# ADD the connection between the right elbow and the right wrist
graph.add_edge(
"pose_" + str(PoseLandmark.RIGHT_ELBOW.value),
"rightHand_" + str(HandLandmark.WRIST.value))
# ADD the connection between the ROOT and the nose
graph.add_edge(
"root",
"pose_" + str(PoseLandmark.NOSE.value))
# ADD the connection between the ROOT and the left shoulder
graph.add_edge(
"root",
"pose_" + str(PoseLandmark.RIGHT_SHOULDER.value))
# ADD the connection between the ROOT and the right shoulder
graph.add_edge(
"root",
"pose_" + str(PoseLandmark.LEFT_SHOULDER.value))
# REMOVE the connection between the left shoulder and the right shoulder
graph.remove_edge(
"pose_" + str(PoseLandmark.LEFT_SHOULDER.value),
"pose_" + str(PoseLandmark.RIGHT_SHOULDER.value))
# Perform DFS starting at the root
root_index = graph.nodes.index("root")
paths = graph.dfs_by_index(root_index)
tree_path = [graph.nodes[i] for path in paths[:1] for i in path]
# Debug info
info = [
("ROOT:", tree_path.index("root")),
("NOSE:", tree_path.index("pose_0")),
("RIGHT EYEBROW:", tree_path.index("face_" +
str(FaceLandmark.RIGHT_LOWER_EYEBROW_INNER_SECOND.value))),
("LEFT EYEBROW:", tree_path.index("face_" +
str(FaceLandmark.LEFT_LOWER_EYEBROW_INNER_SECOND.value))),
("RIGHT EYE INNER:", tree_path.index(
"face_" + str(FaceLandmark.RIGHT_EYE_INNER_SECOND.value))),
("LEFT EYE INNER:", tree_path.index(
"face_" + str(FaceLandmark.LEFT_EYE_INNER_SECOND.value))),
("MOUTH (INNER LIPS):", tree_path.index(
"face_" + str(FaceLandmark.LIPS_INNER.value))),
("RIGHT SHOULDER:", tree_path.index(
"pose_" + str(PoseLandmark.RIGHT_SHOULDER.value))),
("RIGHT WRIST:", tree_path.index(
"rightHand_" + str(HandLandmark.WRIST.value))),
("LEFT SHOULDER:", tree_path.index(
"pose_" + str(int(PoseLandmark.LEFT_SHOULDER)))),
("LEFT WRIST:", tree_path.index(
"leftHand_" + str(HandLandmark.WRIST.value))),
]
info.sort(key=lambda x: x[1])
if debug:
print(info)
return graph, tree_path
def tssi_v3(debug=False):
BODY_JOINTS = [
PoseLandmark.RIGHT_SHOULDER.value,
PoseLandmark.RIGHT_ELBOW.value,
PoseLandmark.LEFT_SHOULDER.value,
PoseLandmark.LEFT_ELBOW.value
]
HAND_JOINTS = [
HandLandmark.WRIST.value,
HandLandmark.THUMB_CMC.value,
HandLandmark.THUMB_MCP.value,
HandLandmark.THUMB_IP.value,
HandLandmark.THUMB_TIP.value,
HandLandmark.INDEX_FINGER_MCP.value,
HandLandmark.INDEX_FINGER_PIP.value,
HandLandmark.INDEX_FINGER_DIP.value,
HandLandmark.INDEX_FINGER_TIP.value,
HandLandmark.MIDDLE_FINGER_MCP.value,
HandLandmark.MIDDLE_FINGER_PIP.value,
HandLandmark.MIDDLE_FINGER_DIP.value,
HandLandmark.MIDDLE_FINGER_TIP.value,
HandLandmark.RING_FINGER_MCP.value,
HandLandmark.RING_FINGER_PIP.value,
HandLandmark.RING_FINGER_DIP.value,
HandLandmark.RING_FINGER_TIP.value,
HandLandmark.PINKY_MCP.value,
HandLandmark.PINKY_PIP.value,
HandLandmark.PINKY_DIP.value,
HandLandmark.PINKY_TIP.value
]
FILTERED_POSE_CONNECTIONS = [(u, v) for (
u, v) in POSE_CONNECTIONS if u in BODY_JOINTS and v in BODY_JOINTS]
joints = ['root', 'pose_0'] + prefix(
'pose', BODY_JOINTS) + prefix('rightHand', HAND_JOINTS) + prefix('leftHand', HAND_JOINTS)
# Define graph
graph = Graph(joints)
for connection in HAND_CONNECTIONS:
start_id, end_id = connection
start = "leftHand_" + str(start_id)
end = "leftHand_" + str(end_id)
graph.add_edge(start, end)
for connection in HAND_CONNECTIONS:
start_id, end_id = connection
start = "rightHand_" + str(start_id)
end = "rightHand_" + str(end_id)
graph.add_edge(start, end)
for connection in FILTERED_POSE_CONNECTIONS:
start_id, end_id = connection
start = "pose_" + str(start_id)
end = "pose_" + str(end_id)
graph.add_edge(start, end)
# ADD the connection between the left elbow and the left wrist
graph.add_edge(
"pose_" + str(PoseLandmark.LEFT_ELBOW.value),
"leftHand_" + str(HandLandmark.WRIST.value))
# ADD the connection between the right elbow and the right wrist
graph.add_edge(
"pose_" + str(PoseLandmark.RIGHT_ELBOW.value),
"rightHand_" + str(HandLandmark.WRIST.value))
# ADD the connection between the ROOT and the nose
graph.add_edge(
"root",
"pose_" + str(PoseLandmark.NOSE.value))
# ADD the connection between the ROOT and the left shoulder
graph.add_edge(
"root",
"pose_" + str(PoseLandmark.RIGHT_SHOULDER.value))
# ADD the connection between the ROOT and the right shoulder
graph.add_edge(
"root",
"pose_" + str(PoseLandmark.LEFT_SHOULDER.value))
# REMOVE the connection between the left shoulder and the right shoulder
graph.remove_edge(
"pose_" + str(PoseLandmark.LEFT_SHOULDER.value),
"pose_" + str(PoseLandmark.RIGHT_SHOULDER.value))
# Perform DFS starting at the root
root_index = graph.nodes.index("root")
paths = graph.dfs_by_index(root_index)
tree_path = [graph.nodes[i] for path in paths[:1] for i in path]
# Debug info
info = [
("ROOT:", tree_path.index("root")),
("NOSE:", tree_path.index("pose_0")),
("RIGHT SHOULDER:", tree_path.index(
"pose_" + str(PoseLandmark.RIGHT_SHOULDER.value))),
("RIGHT WRIST:", tree_path.index(
"rightHand_" + str(HandLandmark.WRIST.value))),
("LEFT SHOULDER:", tree_path.index(
"pose_" + str(int(PoseLandmark.LEFT_SHOULDER)))),
("LEFT WRIST:", tree_path.index(
"leftHand_" + str(HandLandmark.WRIST.value))),
]
info.sort(key=lambda x: x[1])
if debug:
print(info)
return graph, tree_path
def tssi_mejiaperez(debug=False):
RIGHT_EYEBROW_JOINTS = [46, 52, 53, 65]
LEFT_EYEBROW_JOINTS = [276, 282, 283, 295]
RIGHT_EYE = [7, 159, 155, 145]
LEFT_EYE = [382, 386, 249, 374]
MOUTH = [324, 13, 78, 14]
BODY_JOINTS = [
PoseLandmark.RIGHT_SHOULDER.value,
PoseLandmark.RIGHT_ELBOW.value,
PoseLandmark.LEFT_SHOULDER.value,
PoseLandmark.LEFT_ELBOW.value
]
HAND_JOINTS = [
HandLandmark.WRIST.value,
HandLandmark.THUMB_CMC.value,
HandLandmark.THUMB_MCP.value,
HandLandmark.THUMB_IP.value,
HandLandmark.THUMB_TIP.value,
HandLandmark.INDEX_FINGER_MCP.value,
HandLandmark.INDEX_FINGER_PIP.value,
HandLandmark.INDEX_FINGER_DIP.value,
HandLandmark.INDEX_FINGER_TIP.value,
HandLandmark.MIDDLE_FINGER_MCP.value,
HandLandmark.MIDDLE_FINGER_PIP.value,
HandLandmark.MIDDLE_FINGER_DIP.value,
HandLandmark.MIDDLE_FINGER_TIP.value,
HandLandmark.RING_FINGER_MCP.value,
HandLandmark.RING_FINGER_PIP.value,
HandLandmark.RING_FINGER_DIP.value,
HandLandmark.RING_FINGER_TIP.value,
HandLandmark.PINKY_MCP.value,
HandLandmark.PINKY_PIP.value,
HandLandmark.PINKY_DIP.value,
HandLandmark.PINKY_TIP.value
]
FACE_JOINTS = RIGHT_EYEBROW_JOINTS + \
LEFT_EYEBROW_JOINTS + RIGHT_EYE + LEFT_EYE + MOUTH
FILTERED_FACEMESH_CONNECTIONS = [(u, v) for (
u, v) in FACEMESH_CONTOURS if u in FACE_JOINTS and v in FACE_JOINTS]
FILTERED_POSE_CONNECTIONS = [(u, v) for (
u, v) in POSE_CONNECTIONS if u in BODY_JOINTS and v in BODY_JOINTS]
joints = ['root'] + prefix('face', FACE_JOINTS) + prefix(
'pose', BODY_JOINTS) + prefix('rightHand', HAND_JOINTS) + prefix('leftHand', HAND_JOINTS)
# Define graph
graph = Graph(joints)
# Setup connections
for connection in FILTERED_FACEMESH_CONNECTIONS:
start_id, end_id = connection
start = "face_" + str(start_id)
end = "face_" + str(end_id)
graph.add_edge(start, end)
for connection in HAND_CONNECTIONS:
start_id, end_id = connection
start = "leftHand_" + str(start_id)
end = "leftHand_" + str(end_id)
graph.add_edge(start, end)
for connection in HAND_CONNECTIONS:
start_id, end_id = connection
start = "rightHand_" + str(start_id)
end = "rightHand_" + str(end_id)
graph.add_edge(start, end)
for connection in FILTERED_POSE_CONNECTIONS:
start_id, end_id = connection
start = "pose_" + str(start_id)
end = "pose_" + str(end_id)
graph.add_edge(start, end)
# ADD the connections in the left eye
graph.add_edge(
"face_" + str(155),
"face_" + str(159))
graph.add_edge(
"face_" + str(159),
"face_" + str(7))
graph.add_edge(
"face_" + str(7),
"face_" + str(145))
graph.add_edge(
"face_" + str(145),
"face_" + str(155))
# ADD the connections in the right eye
graph.add_edge(
"face_" + str(382),
"face_" + str(386))
graph.add_edge(
"face_" + str(386),
"face_" + str(249))
graph.add_edge(
"face_" + str(249),
"face_" + str(374))
graph.add_edge(
"face_" + str(374),
"face_" + str(382))
# ADD the connections in the mouth
graph.add_edge(
"face_" + str(13),
"face_" + str(78))
graph.add_edge(
"face_" + str(78),
"face_" + str(14))
graph.add_edge(
"face_" + str(14),
"face_" + str(324))
graph.add_edge(
"face_" + str(324),
"face_" + str(13))
# ADD the connection between the root and the left eyebrow
graph.add_edge(
"root",
"face_" + str(FaceLandmark.LEFT_LOWER_EYEBROW_INNER_SECOND.value))
# ADD the connection between the root and the right eyebrow
graph.add_edge(
"root",
"face_" + str(FaceLandmark.RIGHT_LOWER_EYEBROW_INNER_SECOND.value))
# ADD the connection between the root and the left eye
graph.add_edge(
"root",
"face_" + str(FaceLandmark.LEFT_EYE_INNER_SECOND.value))
# ADD the connection between the root and the right eye
graph.add_edge(
"root",
"face_" + str(FaceLandmark.RIGHT_EYE_INNER_SECOND.value))
# ADD the connection between the root and the mouth
graph.add_edge(
"root",
"face_" + str(FaceLandmark.LIPS_INNER.value))
# ADD the connection between the left elbow and the left wrist
graph.add_edge(
"pose_" + str(PoseLandmark.LEFT_ELBOW.value),
"leftHand_" + str(HandLandmark.WRIST.value))
# ADD the connection between the right elbow and the right wrist
graph.add_edge(
"pose_" + str(PoseLandmark.RIGHT_ELBOW.value),
"rightHand_" + str(HandLandmark.WRIST.value))
# ADD the connection between the ROOT and the left shoulder
graph.add_edge(
"root",
"pose_" + str(PoseLandmark.RIGHT_SHOULDER.value))
# ADD the connection between the ROOT and the right shoulder
graph.add_edge(
"root",
"pose_" + str(PoseLandmark.LEFT_SHOULDER.value))
# REMOVE the connection between the left shoulder and the right shoulder
graph.remove_edge(
"pose_" + str(PoseLandmark.LEFT_SHOULDER.value),
"pose_" + str(PoseLandmark.RIGHT_SHOULDER.value))
# Perform DFS starting at the root
root_index = graph.nodes.index("root")
paths = graph.dfs_by_index(root_index)
tree_path = [graph.nodes[i] for path in paths[:1] for i in path]
# Debug info
info = [
("ROOT:", tree_path.index("root")),
("RIGHT EYEBROW:", tree_path.index("face_" +
str(FaceLandmark.RIGHT_LOWER_EYEBROW_INNER_SECOND.value))),
("LEFT EYEBROW:", tree_path.index("face_" +
str(FaceLandmark.LEFT_LOWER_EYEBROW_INNER_SECOND.value))),
("RIGHT EYE INNER:", tree_path.index(
"face_" + str(FaceLandmark.RIGHT_EYE_INNER_SECOND.value))),
("LEFT EYE INNER:", tree_path.index(
"face_" + str(FaceLandmark.LEFT_EYE_INNER_SECOND.value))),
("MOUTH (INNER LIPS):", tree_path.index(
"face_" + str(FaceLandmark.LIPS_INNER.value))),
("RIGHT SHOULDER:", tree_path.index(
"pose_" + str(PoseLandmark.RIGHT_SHOULDER.value))),
("RIGHT WRIST:", tree_path.index(
"rightHand_" + str(HandLandmark.WRIST.value))),
("LEFT SHOULDER:", tree_path.index(
"pose_" + str(int(PoseLandmark.LEFT_SHOULDER)))),
("LEFT WRIST:", tree_path.index(
"leftHand_" + str(HandLandmark.WRIST.value))),
]
info.sort(key=lambda x: x[1])
if debug:
print(info)
return graph, tree_path