-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
244 lines (199 loc) · 7.75 KB
/
app.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
from djitellopy.tello import Tello
import cv2
import pygame
import numpy as np
import time
import sys
######################################################################
width = 640 # WIDTH OF THE IMAGE
height = 480 # HEIGHT OF THE IMAGE
deadZone = 100
######################################################################
# Speed of the drone
S = 30
# Frames per second of the pygame window display
FPS = 25
# Face height for distance
faceX = 405
faceY = 285
faceWidth = 100
faceHeight = 100
frameWidth = width
frameHeight = height
font = cv2.FONT_HERSHEY_SIMPLEX
# org
org = (50, 50)
# fontScale
fontScale = 1
# Blue color in BGR
color = (255, 0, 0)
# Line thickness of 2 px
thickness = 2
faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
# cap = cv2.VideoCapture(1)
# cap.set(3, frameWidth)
# cap.set(4, frameHeight)
# cap.set(10,200)
class FrontEnd(object):
def __init__(self):
# Init pygame
pygame.init()
# Create pygame window
pygame.display.set_caption("Tello video stream")
self.screen = pygame.display.set_mode([960, 720])
# Init Tello object that interacts with the Tello drone
self.tello = Tello()
# Drone velocities between -100~100
self.for_back_velocity = 0
self.left_right_velocity = 0
self.up_down_velocity = 0
self.yaw_velocity = 0
self.speed = 10
self.send_rc_control = False
# create update timer
pygame.time.set_timer(pygame.USEREVENT + 1, 50)
def run(self):
if not self.tello.connect():
print("Tello not connected")
return
if not self.tello.set_speed(self.speed):
print("Not set speed to lowest possible")
return
# In case streaming is on. This happens when we quit this program without the escape key.
if not self.tello.streamoff():
print("Could not stop video stream")
return
if not self.tello.streamon():
print("Could not start video stream")
return
frame_read = self.tello.get_frame_read()
# TELLO EVENT REGION
should_stop = False
while not should_stop:
for event in pygame.event.get():
if event.type == pygame.USEREVENT + 1:
self.update()
elif event.type == pygame.QUIT:
should_stop = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
should_stop = True
else:
self.keydown(event.key)
elif event.type == pygame.KEYUP:
self.keyup(event.key)
if frame_read.stopped:
frame_read.stop()
break
self.updateOnFaces(frame_read)
self.screen.fill([0, 0, 0])
frame = cv2.cvtColor(frame_read.frame, cv2.COLOR_BGR2RGB)
frame = np.rot90(frame)
frame = np.flipud(frame)
frame = pygame.surfarray.make_surface(frame)
self.screen.blit(frame, (0, 0))
# HAND TRACKING
pygame.display.update()
time.sleep(1 / FPS)
# Call it always before finishing. To deallocate resources.
self.tello.end()
def updateOnFaces(self, frame_read):
gray = cv2.cvtColor(frame_read.frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
)
# Draw a rectangle around the faces
index = 0
for (x, y, w, h) in faces:
if index != 0:
return
if x < 960 / 2:
image = cv2.putText(frame_read.frame, 'LEFT', org, font,
fontScale, color, thickness, cv2.LINE_AA)
self.left_right_velocity = -S
elif x > 960 / 2:
image = cv2.putText(frame_read.frame, 'RIGHT', org, font,
fontScale, color, thickness, cv2.LINE_AA)
self.left_right_velocity = S
if y > (720 / 2) - h:
image = cv2.putText(frame_read.frame, 'DOWN', (900, 50), font,
fontScale, color, thickness, cv2.LINE_AA)
self.up_down_velocity = -S
elif y < 720 / 2:
image = cv2.putText(frame_read.frame, 'UP', (900, 50), font,
fontScale, color, thickness, cv2.LINE_AA)
self.up_down_velocity = S
if (w * h) < faceWidth*faceHeight:
image = cv2.putText(frame_read.frame, 'FORWARD', (50, 100), font,
fontScale, color, thickness, cv2.LINE_AA)
self.for_back_velocity = S
elif (w * h) > faceWidth*faceHeight:
image = cv2.putText(frame_read.frame, 'BACKWARD', (50, 100), font,
fontScale, color, thickness, cv2.LINE_AA)
self.for_back_velocity = -S
cv2.rectangle(frame_read.frame, (faceX, faceY), (faceX+faceWidth, faceY+faceHeight), (0, 0, 255), 2)
cv2.rectangle(frame_read.frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
index += 1
def keydown(self, key):
""" Update velocities based on key pressed
Arguments:
key: pygame key
"""
if key == pygame.K_UP: # set forward velocity
self.for_back_velocity = S
elif key == pygame.K_DOWN: # set backward velocity
self.for_back_velocity = -S
elif key == pygame.K_LEFT: # set left velocity
self.left_right_velocity = -S
elif key == pygame.K_RIGHT: # set right velocity
self.left_right_velocity = S
elif key == pygame.K_w: # set up velocity
self.up_down_velocity = S
elif key == pygame.K_s: # set down velocity
self.up_down_velocity = -S
elif key == pygame.K_a: # set yaw counter clockwise velocity
self.yaw_velocity = -S
elif key == pygame.K_d: # set yaw clockwise velocity
self.yaw_velocity = S
def keyup(self, key):
""" Update velocities based on key released
Arguments:
key: pygame key
"""
keys = pygame.key.get_pressed()
if key == pygame.K_UP or key == pygame.K_DOWN: # set zero forward/backward velocity
self.for_back_velocity = 0
elif key == pygame.K_LEFT or key == pygame.K_RIGHT: # set zero left/right velocity
self.left_right_velocity = 0
elif key == pygame.K_w or key == pygame.K_s: # set zero up/down velocity
self.up_down_velocity = 0
elif key == pygame.K_a or key == pygame.K_d: # set zero yaw velocity
self.yaw_velocity = 0
elif key == pygame.K_t: # takeoff
self.tello.takeoff()
self.send_rc_control = True
elif key == pygame.K_l: # land
self.tello.land()
self.send_rc_control = False
elif key == pygame.K_f:
self.tello.flip_back()
elif key == pygame.K_g:
self.tello.flip_forward()
elif key == pygame.K_h:
self.tello.flip_right()
elif key == pygame.K_j:
self.tello.flip_left()
def update(self):
""" Update routine. Send velocities to Tello."""
if self.send_rc_control:
self.tello.send_rc_control(self.left_right_velocity, self.for_back_velocity, self.up_down_velocity,
self.yaw_velocity)
def main():
frontend = FrontEnd()
# run frontend
frontend.run()
if __name__ == '__main__':
main()