-
Notifications
You must be signed in to change notification settings - Fork 2
/
tello-tracking.py
executable file
·174 lines (142 loc) · 5.35 KB
/
tello-tracking.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
#!/usr/bin/env python3
import cv2
import sys
import math
import time
import numpy as np
import traceback
import jetson.inference
import jetson.utils
from datetime import datetime
from utils.mtcnn import TrtMtcnn
from threading import Thread
from djitellopy import Tello
# Video recoding frame rate (Max 15 on Jetson Nano)
video_fps = 15
# Land if battery is below percentage
low_bat = 15
# Enable video recoder
enableVideoRecoder = True
# Max velocity settings (Keep in mind the delay!)
max_yaw_velocity = 50
max_up_down_velocity = 40
max_forward_backward_velocity = 40
def limitVelocity(velocity, max_velocity):
return min(max_velocity, max(-max_velocity, velocity))
def videoRecorder():
global frame_read
now = datetime.now().strftime("%Y_%m_%d-%H_%M_%S")
height, width, _ = frame_read.frame.shape
video = cv2.VideoWriter('video/video-' + now + '.avi', cv2.VideoWriter_fourcc(*'XVID'), video_fps, (width, height))
while keepRecording:
start = time.time()
currentFrame = frame_read.frame
video.write(currentFrame)
end = time.time();
elapsed = end - start
time.sleep(max(0,1 / video_fps - elapsed))
end = time.time();
video.release()
def dist(p1,p2):
return math.sqrt( ((p1[0]-p2[0])**2)+((p1[1]-p2[1])**2) )
def detectionDistance(detectionElem):
global detection
if detection == False:
return 0
return dist(detectionElem.Center,detection.Center)
def getDetectedPerson(detections, lastDetection):
global net
persons = []
for detection in detections:
className = net.GetClassDesc(detection.ClassID)
if className == 'person':
persons.append(detection)
persons.sort(key=detectionDistance)
if len(persons) > 0:
return persons[0]
return False
def main():
global net, detection, frame_read, keepRecording, low_bat, max_yaw_velocity, max_up_down_velocity, max_forward_backward_velocity;
currentFrame = False
keepRecording = True
# Init model
net = jetson.inference.detectNet("ssd-mobilenet-v2", threshold=0.5)
tello = Tello()
tello.connect()
battery = tello.get_battery()
print ("Battery: ", tello.get_battery());
if battery < low_bat:
print ("Battery low")
exit()
tello.streamon()
frame_read = tello.get_frame_read()
currentFrame = frame_read.frame
if enableVideoRecoder:
recorder = Thread(target=videoRecorder)
recorder.start()
tello.send_rc_control(0, 0, 0, 0)
tello.takeoff()
tello.move_up(50)
winname = "Tello"
running = True
detection = False
while running:
battery = tello.get_battery();
if battery < low_bat:
print ("Battery low")
running = False
currentFrame = frame_read.frame
cuda_img = jetson.utils.cudaFromNumpy(currentFrame, isBGR=False)
detections = net.Detect(cuda_img, overlay='none')
np_img = jetson.utils.cudaToNumpy(cuda_img)
h,w,c = np_img.shape
yaw_velocity = 0
left_right_velocity = 0
up_down_velocity = 0
forward_backward_velocity = 0
if len(detections) > 0:
detection = getDetectedPerson(detections, detection)
if detection != False:
color = (0, 255, 0)
np_img = cv2.rectangle(np_img, (int(detection.Left), int(detection.Top)), (int(detection.Right), int(detection.Bottom)), color, 2)
np_img = cv2.circle(np_img, (int(detection.Center[0]),int(detection.Center[1])), 5, color, 5, cv2.LINE_AA)
detectX = int(detection.Center[0])
centerX = w / 2;
yaw_velocity = int((detectX - centerX) / 7) # proportional only
up_down_velocity = int ((30 - detection.Top) / 2)
forward_backward_velocity = int((h - detection.Bottom - 30) / 2)
yaw_velocity = limitVelocity(yaw_velocity, max_yaw_velocity)
up_down_velocity = limitVelocity(up_down_velocity, max_up_down_velocity)
forward_backward_velocity = limitVelocity(forward_backward_velocity, max_forward_backward_velocity)
left_right_velocity = 0
print (w,h,detection.Top, detection.Bottom, forward_backward_velocity, up_down_velocity)
else:
print ("No Person detected")
else:
print ("No detection")
# Tello remote control by object detection
tello.send_rc_control(left_right_velocity, forward_backward_velocity, up_down_velocity, yaw_velocity)
# scale image for small screen
scale_percent = 50 # percent of original size
width = int(np_img.shape[1] * scale_percent / 100)
height = int(np_img.shape[0] * scale_percent / 100)
dim = (width, height)
resized = cv2.resize(np_img, dim, interpolation = cv2.INTER_AREA)
# Show image on screen (Needs running X-Server!)
cv2.namedWindow(winname)
cv2.moveWindow(winname, 0,0)
cv2.imshow(winname,resized)
keyCode = cv2.waitKey(1) & 0xFF
if keyCode == 27:
break
# Goodby
cv2.destroyAllWindows()
tello.send_rc_control(0, 0, 0, 0)
battery = tello.get_battery()
tello.land()
if enableVideoRecoder:
recorder.join()
keepRecording = False
if __name__ == "__main__":
# Execute if run as a script
main()