-
Notifications
You must be signed in to change notification settings - Fork 0
/
recoginze.py
98 lines (77 loc) · 3.68 KB
/
recoginze.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
import mediapipe as mp
import math
mp_hands = mp.solutions.hands
#range (3pi/4 - pi/8, 3pi/4 + pi/8), (pi/4 - pi/8, pi/4 + pi/8)
range1 = (math.tan(5*math.pi/8), math.tan(7*math.pi/8))
range2 = (math.tan(math.pi/8), math.tan(3*math.pi/8))
def hand_gesture(hand_landmarks):
coeff = 1
origin_landmarks_x = []
origin_landmarks_y = []
landmarks_x = [] #converted q
landmarks_y = []
for loc in mp_hands.HandLandmark:
origin_landmarks_x.append(1-coeff*hand_landmarks.landmark[loc].x)
origin_landmarks_y.append(1-coeff*hand_landmarks.landmark[loc].y)
# print(tmp, ": x: ", hand_landmarks.landmark[loc].x , "y: ", hand_landmarks.landmark[loc].y)
# tmp+=1
d0 = {'x': origin_landmarks_x[0], 'y': origin_landmarks_y[0]}
d9 = {'x': origin_landmarks_x[9], 'y': origin_landmarks_y[9]}
d9_from_d0 = {'x' : d9['x'] - d0['x'], 'y' : d9['y'] - d0['y']}
r = math.sqrt((d9['x'] - d0['x'])**2 + (d9['y'] - d0['y'])**2)
# get converted coordinate value x, y
landmarks_x.append(origin_landmarks_x[0]), landmarks_y.append(origin_landmarks_y[0])
for i in range(1, 9):
landmarks_x.append(((origin_landmarks_x[i]-d0['x'])*d9_from_d0['y'] - (origin_landmarks_y[i] - d0['y']) * d9_from_d0['x']))
landmarks_y.append(((origin_landmarks_x[i]-d0['x'])*d9_from_d0['x'] + (origin_landmarks_y[i] - d0['y']) * d9_from_d0['y']))
landmarks_x.append(origin_landmarks_x[9]), landmarks_y.append(origin_landmarks_y[9])
for i in range(9, 21):
landmarks_x.append(((origin_landmarks_x[i]-d0['x'])*d9_from_d0['y'] - (origin_landmarks_y[i] - d0['y']) * d9_from_d0['x']))
landmarks_y.append(((origin_landmarks_x[i]-d0['x'])*d9_from_d0['x'] + (origin_landmarks_y[i] - d0['y']) * d9_from_d0['y']))
# get satatus of each finger (OPEN OR CLOSE)
OPEN_THUMB = False
OPEN_INDEX_FINGER = False
OPEN_MIDDLE_FINGER = False
OPEN_RING_FINGER = False
OPEN_PINKY = False
pesudoFixKeyPoint = landmarks_x[2]
if(landmarks_x[3] > pesudoFixKeyPoint and landmarks_x[4] > pesudoFixKeyPoint):
OPEN_THUMB = True
pesudoFixKeyPoint = landmarks_y[6]
if (landmarks_y[7] > pesudoFixKeyPoint and landmarks_y[8] > pesudoFixKeyPoint):
OPEN_INDEX_FINGER = True
pesudoFixKeyPoint = landmarks_y[10]
if (landmarks_y[11] > pesudoFixKeyPoint and landmarks_y[12] > pesudoFixKeyPoint):
OPEN_MIDDLE_FINGER = True
pesudoFixKeyPoint = landmarks_y[14]
if (landmarks_y[15] > pesudoFixKeyPoint and landmarks_y[16] > pesudoFixKeyPoint):
OPEN_RING_FINGER = True
pesudoFixKeyPoint = landmarks_y[18]
if (landmarks_y[19] > pesudoFixKeyPoint and landmarks_y[20] > pesudoFixKeyPoint):
OPEN_PINKY = True
# get gesture
gesture = "UNKNOWN"
if(OPEN_INDEX_FINGER and OPEN_MIDDLE_FINGER and OPEN_RING_FINGER and OPEN_PINKY):
m = d9_from_d0['y']/d9_from_d0['x']
if((range1[0] < m and m < range1[1]) or (range2[0]< m <range2[1])):
gesture = "Go first"
else:
gesture = "Thank you"
if ((OPEN_THUMB) and (not OPEN_INDEX_FINGER) and (not OPEN_MIDDLE_FINGER) and (not OPEN_RING_FINGER) and (
OPEN_PINKY)):
gesture = "There is someone in front of me"
# if ((OPEN_THUMB) and (OPEN_INDEX_FINGER) and (OPEN_MIDDLE_FINGER) and (OPEN_RING_FINGER) and (OPEN_PINKY)):
# gesture = "FIVE"
#
# if ((not OPEN_THUMB) and (OPEN_INDEX_FINGER) and (OPEN_MIDDLE_FINGER) and (not OPEN_RING_FINGER) and (
# not OPEN_PINKY)):
# gesture = "TWO"
#
# if ((not OPEN_THUMB) and (OPEN_INDEX_FINGER) and (not OPEN_MIDDLE_FINGER) and (not OPEN_RING_FINGER) and (
# not OPEN_PINKY)):
# gesture = "ONE"
#
# if ((not OPEN_THUMB) and (not OPEN_INDEX_FINGER) and (not OPEN_MIDDLE_FINGER) and (not OPEN_RING_FINGER) and (
# not OPEN_PINKY)):
# gesture = "FIST"
return gesture