-
Notifications
You must be signed in to change notification settings - Fork 1
/
gestures.py
119 lines (101 loc) · 4.22 KB
/
gestures.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
# New gesture. Checks if the index finger is extended to the right of the hand.
def is_picking(result):
try:
index_tip = None
index_dip = None
index_pip = None
thumb_tip = None
middle_finger_pip = None
hand = None
if result.handedness[0][0].category_name == "Right":
index_tip = result.hand_landmarks[0][8].x
index_dip = result.hand_landmarks[0][7].x
index_pip = result.hand_landmarks[0][6].x
thumb_tip = result.hand_landmarks[0][4].x
middle_finger_pip = result.hand_landmarks[0][10].x
hand = 0
elif result.handedness[0][0].category_name == "Left":
index_tip = result.hand_landmarks[1][8].x
index_dip = result.hand_landmarks[1][7].x
index_pip = result.hand_landmarks[1][6].x
thumb_tip = result.hand_landmarks[1][4].x
middle_finger_pip = result.hand_landmarks[1][10].x
hand = 1
if (index_tip > result.hand_landmarks[hand][6].x and
index_tip > index_dip and
index_tip > index_pip and
index_tip > thumb_tip and
index_tip > middle_finger_pip and
index_tip > result.hand_landmarks[hand][12].x and
index_tip > result.hand_landmarks[hand][16].x and
index_tip > result.hand_landmarks[hand][20].x):
return True
else:
return False
except Exception as e:
print(e)
return False
# New gesture. Checks if the index finger and the thumb are extended, forming an L.
def is_gesture_L(result):
try:
index_tip_y = None
index_dip_y = None
thumb_tip_x = None
thumb_tip_y = None
thumb_ip_x = None
hand = None
if result.handedness[0][0].category_name == "Left":
index_tip_y = result.hand_landmarks[0][8].y
thumb_tip_x = result.hand_landmarks[0][4].x
# Included check for thumb tip vs index finger tip
thumb_tip_y = result.hand_landmarks[0][4].y
thumb_ip_x = result.hand_landmarks[0][3].x
hand = 0
elif result.handedness[0][0].category_name == "Right":
index_tip_y = result.hand_landmarks[1][8].y
thumb_tip_x = result.hand_landmarks[1][4].x
thumb_tip_y = result.hand_landmarks[1][4].y
thumb_ip_x = result.hand_landmarks[1][3].x
hand = 1
if not (index_tip_y < result.hand_landmarks[hand][6].y and
index_tip_y < result.hand_landmarks[hand][7].y and
index_tip_y < result.hand_landmarks[hand][12].y and
index_tip_y < result.hand_landmarks[hand][16].y and
index_tip_y < result.hand_landmarks[hand][20].y and
thumb_tip_y > result.hand_landmarks[hand][6].y):
return False
if not (thumb_tip_x < thumb_ip_x):
return False
return True
except Exception as e:
print(e)
return False
# New gesture. Checks if the pinky is extended and the other fingers are not
def is_pinky_up(result):
try:
index_tip_y = None
middle_tip_y = None
ring_tip_y = None
pinky_tip_y = None
thumb_tip_y = None
if result.handedness[0][0].category_name == "Left":
thumb_tip_y = result.hand_landmarks[0][4].y
index_tip_y = result.hand_landmarks[0][8].y
middle_tip_y = result.hand_landmarks[0][12].y
ring_tip_y = result.hand_landmarks[0][16].y
pinky_tip_y = result.hand_landmarks[0][20].y
elif result.handedness[0][0].category_name == "Right":
thumb_tip_y = result.hand_landmarks[1][4].y
index_tip_y = result.hand_landmarks[1][8].y
middle_tip_y = result.hand_landmarks[1][12].y
ring_tip_y = result.hand_landmarks[1][16].y
pinky_tip_y = result.hand_landmarks[1][20].y
if not (pinky_tip_y < thumb_tip_y and
pinky_tip_y < index_tip_y and
pinky_tip_y < middle_tip_y and
pinky_tip_y < ring_tip_y):
return False
return True
except Exception as e:
print(e)
return False