forked from VibhinnS/Doraemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_prev.py
149 lines (113 loc) · 4.02 KB
/
utils_prev.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
import pandas as pd
import numpy as np
landmark_list = {
"wrist": 0,
"thumb_cmc": 0,
"thumb_mcp": 0,
"thumb_ip": 0,
"thumb_tip": 0,
"index_finger_mcp": 0,
"index_finger_pip": 0,
"index_finger_dip": 0,
"index_finger_tip": 0,
"middle_finger_mcp": 0,
"middle_finger_pip": 0,
"middle_finger_dip": 0,
"middle_finger_tip": 0,
"ring_finger_mcp": 0,
"ring_finger_pip": 0,
"ring_finger_dip": 0,
"ring_finger_tip": 0,
"pinky_finger_mcp": 0,
"pinky_finger_pip": 0,
"pinky_finger_dip": 0,
"pinky_finger_tip": 0
}
def get_current_landmarks(image, hand_landmarks):
landmarks = {}
for i, key in enumerate(landmark_list.keys()):
landmarks[key] = (int(hand_landmarks.landmark[i].x * image.shape[1]),
int(hand_landmarks.landmark[i].y * image.shape[0]))
return landmarks
def predict_gesture(model, landmarks):
landmarks = list(landmarks.values())
# Make them relative to each other so that it doesnt matter if hand is on left side or right side
landmarks = list(map(lambda l: (l[0] - landmarks[0][0], l[1] - landmarks[0][1]), landmarks))
# Normalize them so that distance from camera doesnt matter
getx = lambda i: abs(i[0])
gety = lambda i: abs(i[1])
max_x = max(list(map(getx, landmarks)))
max_y = max(list(map(gety, landmarks)))
# final_landmarks = list(map(lambda l: (round(l[0]/max_x, 5), round(l[1]/max_y, 5)), landmarks))
final_landmarks = list(map(lambda l: (l[0]/max_x, l[1]/max_y), landmarks))
arr = []
for i in range(21):
arr.append(final_landmarks[i][0])
arr.append(final_landmarks[i][1])
predict_result = model.predict(np.array([arr]), verbose=False)
# print(np.squeeze(predict_result))
result = np.argmax(np.squeeze(predict_result))
dic = {
0: "Backward",
1: "Down",
2: "Flip",
3: "Forward",
4: "Land",
5: "Left",
6: "Right",
7: "Up"
}
return dic[result]
def create_dataset(landmarks, label, dataset):
landmarks = list(landmarks.values())
# Make them relative to each other so that it doesnt matter if hand is on left side or right side
landmarks = list(map(lambda l: (l[0] - landmarks[0][0], l[1] - landmarks[0][1]), landmarks))
# Normalize them so that distance from camera doesnt matter
getx = lambda i: abs(i[0])
gety = lambda i: abs(i[1])
max_x = max(list(map(getx, landmarks)))
max_y = max(list(map(gety, landmarks)))
# final_landmarks = list(map(lambda l: (round(l[0]/max_x, 5), round(l[1]/max_y, 5)), landmarks))
final_landmarks = list(map(lambda l: (l[0]/max_x, l[1]/max_y), landmarks))
df = pd.read_csv(dataset, index_col=0)
columns = list(landmark_list.keys())
num = df.shape[0]
for i, col in enumerate(columns):
df.loc[num, col+'_x'] = final_landmarks[i][0]
df.loc[num, col+'_y'] = final_landmarks[i][1]
df.loc[num, 'label'] = label
df.to_csv(dataset)
print(num)
def Action(gesture, tello):
speed = 20
lr = fb = ud = 0
takeoff = False
try:
gesture = gesture.lower()
if gesture == "land":
tello.land()
takeoff = False
elif gesture == "forward":
fb = speed
elif gesture == "backward":
fb = -speed
elif gesture == "left":
lr = -speed
elif gesture == "right":
lr = speed
elif gesture == "up":
# if not takeoff:
# tello.takeoff()
# takeoff = True
# else:
ud = speed
elif gesture == "down":
ud = -speed
elif gesture == "flip":
tello.flip_left()
# send_rc_control(left_right_velocity, forward_backward_velocity, up_down_velocity, yaw_velocity)
tello.send_rc_control(lr, fb, ud, 0)
lr = fb = ud = 0
except Exception as e:
print('command exception, shutting down')
tello.land()