-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracking_main.py
More file actions
354 lines (283 loc) · 11.2 KB
/
tracking_main.py
File metadata and controls
354 lines (283 loc) · 11.2 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
from pynput import keyboard
import time
import cv2
from hsv_detection import HSV
import torch
import torch.backends.cudnn as cudnn
import numpy as np
from numpy import random
from models.experimental import attempt_load
from utils.datasets import letterbox
from utils.general import check_img_size, check_requirements, non_max_suppression, scale_coords, xyxy2xywh
from models.common import DetectMultiBackend
from utils.torch_utils import select_device, time_sync
import csv
import math
import os
# import queue
import shlex
import subprocess
import tempfile
import threading
# from olympe.messages.camera import (
# set_camera_mode,
# set_streaming_mode,
# )
import olympe
from olympe.messages.ardrone3.Piloting import TakeOff, Landing
from olympe.messages.ardrone3.Piloting import moveBy, PCMD
from olympe.messages.ardrone3.PilotingState import FlyingStateChanged
from olympe.messages.ardrone3.PilotingSettings import MaxTilt
from olympe.messages.ardrone3.GPSSettingsState import GPSFixStateChanged
olympe.log.update_config({"loggers": {"olympe": {"level": "WARNING"}}})
DRONE_IP = os.environ.get("DRONE_IP", "192.168.42.1")
DRONE_RTSP_PORT = os.environ.get("DRONE_RTSP_PORT")
# WEIGHTS = '/home/aims/drone_640_s.pt'
WEIGHTS = '/home/aims/drone_1280_s.pt'
IMG_SIZE = 640
DEVICE = '0'
AUGMENT = False
CONF_THRES = 0.25
IOU_THRES = 0.45
CLASSES = None
AGNOSTIC_NMS = False
# Initialize
device = select_device(DEVICE)
half = device.type != 'cpu' # half precision only supported on CUDA
# Load model
model = attempt_load(WEIGHTS, map_location=device) # load FP32 model
stride = int(model.stride.max()) # model stride
imgsz = check_img_size(IMG_SIZE, s=stride) # check img_size
if half:
model.half() # to FP16
# Get names and colors
names = model.module.names if hasattr(model, 'module') else model.names
colors = [[random.randint(0, 255) for _ in range(3)] for _ in names]
# Run inference
if device.type != 'cpu':
model(torch.zeros(1, 3, imgsz, imgsz).to(device).type_as(next(model.parameters()))) # run once
def yolo(image, model, AUGMENT = False) :
x , y, w, h, conf = 0, 0, 0, 0, 0.
# Load image
frame = image # BGR
xyxy = [0, 0, 0, 0]
# Padded resize
img = letterbox(frame, imgsz, stride=stride)[0]
# Convert
img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416
img = np.ascontiguousarray(img)
img = torch.from_numpy(img).to(device)
img = img.half() if half else img.float() # uint8 to fp16/32
img /= 255.0 # 0 - 255 to 0.0 - 1.0
if img.ndimension() == 3:
img = img.unsqueeze(0)
# Inference
t0 = time_sync()
pred = model(img, augment=AUGMENT)[0]
print('pred shape:', pred.shape)
# Apply NMS
pred = non_max_suppression(pred, CONF_THRES, IOU_THRES, classes=CLASSES, agnostic=AGNOSTIC_NMS)
# Process detections
det = pred[0]
print('det shape:', det.shape)
s = ''
s += '%gx%g ' % img.shape[2:] # print string
original = [0, 0, 0, 0]
if len(det):
# Rescale boxes from img_size to img0 size
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], frame.shape).round()
# Print results
for c in det[:, -1].unique():
n = (det[:, -1] == c).sum() # detections per class
s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string
# Write results
for *xyxy, conf, cls in reversed(det):
label = f'{names[int(cls)]} {conf:.2f}'
xyxy = np.array(torch.tensor(xyxy).view(1, 4).view(-1).tolist())
xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4))).view(-1).tolist()
x=xywh[0]
y=xywh[1]
w=xywh[2]
h=xywh[3]
print(xyxy)
print(conf)
print(f'Inferencing and Processing Done. ({time.time() - t0:.3f}s)')
# Stream results
print(s)
# frame = cv2.rectangle(frame,(int(xyxy[0]),int(xyxy[1])) ,(int(xyxy[2]),int(xyxy[3])) , (0,255,0),thickness = 5)
# frame = cv2.putText(frame, "x : %d y : %d w : %d h : %d conf : %.2f " %(x, y, w, h, conf), (0, 40), cv2.FONT_HERSHEY_PLAIN, 2, (255, 0, 0), 1, cv2.LINE_AA)
return xyxy, x, y, w, h, conf
def main():
streaming = Streaming()
# Start the video stream
streaming.start()
while not streaming.finish :
try :
frame = streaming.frame_dict['frame']
except KeyError :
continue
xyxy, x, y, w, h, conf = yolo(frame, model)
frame_disp = frame.copy()
cv2.rectangle(frame_disp, (int(xyxy[0]),int(xyxy[1])) ,(int(xyxy[2]),int(xyxy[3])) , (0,255,0),thickness = 5)
cv2.putText(frame_disp, "x : %d y : %d w : %d h : %d conf : %.2f " %(x, y, w, h, conf), (0, 40), cv2.FONT_HERSHEY_PLAIN, 2, (255, 0, 0), 1, cv2.LINE_AA)
cv2.imshow('result',frame_disp)
key = cv2.waitKey(1)
if key == ord('q'):
break
cv2.destroyAllWindows()
# Stop the video stream
streaming.stop()
class Streaming:
def __init__(self):
# Create the olympe.Drone object from its IP address
self.drone = olympe.Drone(DRONE_IP)
# self.drone(set_streaming_mode(cam_id=0, value=0))
# self.frame_queue = queue.Queue()
# self.processing_thread = threading.Thread(target=self.yuv_frame_processing)
self.tracking = False
self.angle = 40
self.yaw = 40
self.gaz = 10
self.input = {
'pitch' : 0,
'roll' : 0,
'yaw' : 0,
'gaz' : 0,
}
self.finish = False
self.pressed = set()
self.init_controls()
# self.yuv_frame_dict = {}
self.frame_dict = {}
# self.purple_lower = (113, 75, 14)
# self.purple_upper = (173, 255, 255)
# self.hsv = HSV(720, 1280, self.purple_lower, self.purple_upper)
def on_press(self, keyname):
"""handler for keyboard listener"""
try:
self.keydown = True
keyname = str(keyname).strip('\'')
print('+' + keyname)
self.pressed.add(keyname)
if keyname == 'Key.esc':
self.drone(Landing())
self.finish = not self.finish
exit(0)
if keyname == 't':
self.tracking = not self.tracking
print('Tracking : ', self.tracking)
if keyname == 'Key.tab' :
self.drone(TakeOff())
if keyname == 'Key.backspace' :
self.drone(Landing())
if keyname in self.controls:
key_handler = self.controls[keyname][0]
if isinstance(key_handler, str):
self.input[key_handler] = int(self.controls[keyname][1])
else:
key_handler()
self.drone(PCMD(1, self.input['roll'], self.input['pitch'], self.input['yaw'], self.input['gaz'], timestampAndSeqNum=0))
except AttributeError:
print('special key {0} pressed'.format(keyname))
def on_release(self, keyname):
"""Reset on key up from keyboard listener"""
keyname = str(keyname).strip('\'')
print('-' + keyname)
if keyname in self.pressed :
self.pressed.remove(keyname)
if keyname in self.controls:
key_handler = self.controls[keyname][0]
if isinstance(key_handler, str):
self.input[key_handler] = int(0)
else:
key_handler()
self.drone(PCMD(1, self.input['roll'], self.input['pitch'], self.input['yaw'], self.input['gaz'], timestampAndSeqNum=0))
def init_controls(self):
"""Define keys and add listener"""
self.controls = {
'w': ['gaz', str(self.gaz)] ,
's': ['gaz', str(-self.gaz)],
'a': ['yaw', str(-self.yaw)],
'd': ['yaw', str(self.yaw)],
'Key.left': ['roll', str(-self.angle)],
'Key.right': ['roll', str(self.angle)],
'Key.up': ['pitch', str(self.angle)],
'Key.down': ['pitch', str(-self.angle)],
}
self.key_listener = keyboard.Listener(on_press=self.on_press,
on_release=self.on_release)
self.key_listener.start()
def start(self):
# Connect the the drone
assert self.drone.connect(retry=3)
if DRONE_RTSP_PORT is not None:
self.drone.streaming.server_addr = f"{DRONE_IP}:{DRONE_RTSP_PORT}"
# Setup your callback functions to do some live video processing
self.drone.streaming.set_callbacks(
raw_cb=self.yuv_frame_cb,
)
# Start video streaming
self.drone.streaming.start()
# self.running = True
# self.processing_thread.start()
def stop(self):
self.running = False
# self.processing_thread.join()
# Properly stop the video stream and disconnect
assert self.drone.streaming.stop()
assert self.drone.disconnect()
def yuv_frame_cb(self, yuv_frame):
"""
This function will be called by Olympe for each decoded YUV frame.
:type yuv_frame: olympe.VideoFrame
"""
yuv_frame.ref()
# self.yuv_frame_dict.clear()
# self.yuv_frame_dict['frame'] = yuv_frame
cv2_cvt_color_flag = {
olympe.VDEF_I420: cv2.COLOR_YUV2BGR_I420,
olympe.VDEF_NV12: cv2.COLOR_YUV2BGR_NV12,
}[yuv_frame.format()]
self.frame_dict.clear()
self.frame_dict['frame'] = cv2.cvtColor(yuv_frame.as_ndarray(), cv2_cvt_color_flag)
yuv_frame.unref()
# using queue
# with self.frame_queue.mutex :
# self.frame_queue.queue.clear()
# self.frame_queue.put_nowait(yuv_frame)
def yuv_frame_processing(self):
a = 0
while self.running:
# using queue
# try:
# # yuv_frame = self.frame_queue.get(timeout=0.1)
# yuv_frame = self.frame_queue.get()
# with self.frame_queue.mutex :
# self.frame_queue.queue.clear()
# except queue.Empty:
# continue
try :
yuv_frame = self.yuv_frame_dict['frame']
self.yuv_frame_dict.clear()
except KeyError :
continue
# You should process your frames here and release (unref) them when you're done.
# Don't hold a reference on your frames for too long to avoid memory leaks and/or memory
# pool exhaustion.
info = yuv_frame.info()
height, width = ( # noqa
info["raw"]["frame"]["info"]["height"],
info["raw"]["frame"]["info"]["width"],
)
cv2_cvt_color_flag = {
olympe.VDEF_I420: cv2.COLOR_YUV2BGR_I420,
olympe.VDEF_NV12: cv2.COLOR_YUV2BGR_NV12,
}[yuv_frame.format()]
self.cv2frame = cv2.cvtColor(yuv_frame.as_ndarray(), cv2_cvt_color_flag)
if self.tracking :
pass
cv2.imshow('anafi', self.cv2frame)
cv2.waitKey(1)
yuv_frame.unref()
if __name__ == "__main__":
main()