-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
95 lines (66 loc) · 2.4 KB
/
main.py
File metadata and controls
95 lines (66 loc) · 2.4 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
"""
Date: 23rd June, 2025
Author: Rosey
Inspirator: UNILAG Design Studio
AIM: To be able to configure drone with my settings and configuration with the use of Python libraries, Tello drone
openCV, and flight
Status of the drone: YESSSSSSS!!!! It Worked!!, but with few mis functions
Further improvements: To import yolo files for object detection and for face recognition.
"""
from djitellopy import Tello
import KeyboardTelloModule as kp
from ultralytics import YOLO
import cv2
import numpy
import time
tello = Tello()
Drone = tello
Drone.connect()
Drone.streamon()
# for checking battery %
print(Drone.get_battery())
model = YOLO("yolov8n.pt")
global img
def flight():
LR,FB ,UD, YV = 0,0,0,0
speed = 80
moveSpeed = 85
liftSpeed = 80
rotationSpeed = 100
if kp.getKey("LEFT"): LR = -speed
elif kp.getKey("RIGHT"): LR = speed
if kp.getKey("UP"): UD = moveSpeed
elif kp.getKey("DOWN"): UD = -moveSpeed
if kp.getKey("w"): FB = liftSpeed
elif kp.getKey("s"): FB = -liftSpeed
if kp.getKey("a"): YV = rotationSpeed
elif kp.getKey("d"): YV = -rotationSpeed
if kp.getKey("e"): Drone.takeoff()
elif kp.getKey("q"): Drone.land()
if kp.getKey("m"): #save image
cv2.imwrite(r"C:\Users\user\Documents\Computer Vision,Python projects\flight control + image processing + openCV\{}.jpg".format(time.time()), img)
time.sleep(0.3)
return(LR,FB ,UD, YV)
kp.init()
while True:
controls = flight()
Drone.send_rc_control(controls[0], controls[1], controls[2], controls[3])
img = Drone.get_frame_read().frame
img = cv2.resize(img, (640, 480))
results = model(img, stream=True)
for r in results:
for box in r.boxes:
x1, y1, x2, y2 = map(int, box.xyxy[0])
conf = float(box.conf[0])
cls = int(box.cls[0])
label = model.names[cls]
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.putText(img, f'{label} {conf:.2f}', (x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
cv2.imshow("YOLO + Tello Flight", img)
if cv2.waitKey(1) & 0xFF == 27:
print("[INFO] Landing and exiting...")
Drone.land()
break
Drone.streamoff()
cv2.destroyAllWindows()