-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempCodeRunnerFile.py
More file actions
82 lines (66 loc) · 1.86 KB
/
tempCodeRunnerFile.py
File metadata and controls
82 lines (66 loc) · 1.86 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
from ultralytics import YOLO
import supervision as sv
import KeyPressModule as kp
from djitellopy import tello
from time import sleep
import cv2
#import time
from time import time
kp.init()
me = tello.Tello()
me.connect()
# load the model
model = YOLO("yolov8n.pt")
# box annotator
bbox_annotator = sv.BoxAnnotator()
def get_keyboard_input():
lr, fb, ud, yv = 0, 0, 0, 0
speed = 50
if kp.getKey('LEFT'): lr = -speed
elif kp.getKey('RIGHT'): lr = speed
if kp.getKey('UP'): fb = speed
elif kp.getKey('DOWN'): fb = -speed
if kp.getKey('w'): ud = speed
elif kp.getKey('s'): ud = -speed
if kp.getKey('a'): yv = -speed
elif kp.getKey('d'): yv = speed
if kp.getKey('q'): me.land()
if kp.getKey('t'): me.takeoff()
# save image if 'i' is pressed
if kp.getKey('i'):
img = me.get_frame_read().frame
img = cv2.resize(img, (360, 240))
cv2.imwrite(f'Images/{time()}.jpg', img)
sleep(0.3)
return [lr, fb, ud, yv]
me.streamon()
while True:
vals = get_keyboard_input()
me.send_rc_control(vals[0], vals[1], vals[2], vals[3])
sleep(0.05)
# get battery and height
print(me.get_battery())
print(me.get_height())
# stream video
img = me.get_frame_read().frame
#resize the image
img = cv2.resize(img, (360, 240))
# change bgr to rgb
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# detect objects
result = model(img)[0]
# detection
detection = sv.Detections.from_yolov8(result)
# confidence 80%
detection = detection[detection.confidence > 0.8]
labels = [
result.names[class_id]
for class_id in detection.class_id
]
frame = bbox_annotator.annotate(img, detection, labels)
# display the image
cv2.imshow("Image", frame)
# q for land
if cv2.waitKey(1) & 0xFF == ord('q'):
me.land()
break