-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrone_telemetry.py
More file actions
59 lines (43 loc) · 1.53 KB
/
Copy pathdrone_telemetry.py
File metadata and controls
59 lines (43 loc) · 1.53 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
import cv2
import numpy as np
from ultralytics import YOLO
model = YOLO("yolov8n.pt")
video_path = "traffic.mp4" # change if your video name is different
cap = cv2.VideoCapture(video_path)
frame_count = 0
max_frames = 150
while cap.isOpened() and frame_count < max_frames:
ret, frame = cap.read()
if not ret:
break
frame = cv2.resize(frame, (640,640))
results = model(frame)[0]
height, width, _ = frame.shape
center_left = int(width * 0.4)
center_right = int(width * 0.6)
cv2.rectangle(frame, (center_left,0), (center_right,height), (255,255,0), 2)
for box in results.boxes:
conf = float(box.conf[0])
if conf < 0.6:
continue
x1,y1,x2,y2 = map(int, box.xyxy[0])
center_x = int((x1+x2)/2)
box_height = y2 - y1
distance = round(1000/box_height,2)
label = model.names[int(box.cls[0])]
if center_left < center_x < center_right:
color = (0,0,255)
status = "TARGET LOCK"
else:
color = (0,255,0)
status = "SCANNING"
cv2.rectangle(frame,(x1,y1),(x2,y2),color,2)
cv2.putText(frame,f"{label} {conf:.2f} Dist:{distance}m",
(x1,y1-10),cv2.FONT_HERSHEY_SIMPLEX,0.6,color,2)
cv2.putText(frame,status,(20,40),
cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,255),3)
cv2.putText(frame,f"Frame: {frame_count}",
(20,80),cv2.FONT_HERSHEY_SIMPLEX,1,(255,0,0),3)
frame_count += 1
cap.release()
cv2.destroyAllWindows()