-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwarhawk.py
More file actions
185 lines (167 loc) · 10.4 KB
/
Copy pathwarhawk.py
File metadata and controls
185 lines (167 loc) · 10.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
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
from ultralytics import YOLO as warhawkAi
import cvzone as warhawkAiInterpretor
import cv2 as warhawkAiMedia
import math as warhawkMath
import warhawkObjectClassnames_fire
import numpy as nm
from sort import *
def detectVideo(path_x):
videoPath = path_x
warhawkAiClassnames = warhawkObjectClassnames_fire.warhawkAiClassnames
droneFeedCapture = warhawkAiMedia.VideoCapture(videoPath)
warhawkModel = warhawkAi("../warhawk-weights/yolov8m.pt")
warhawkAiClassesToDetect = [0, 1, 2, 3, 4, 5, 6, 7]
humanTracker = Sort(max_age=40, min_hits=8, iou_threshold=0.3)
carTracker = Sort(max_age=40, min_hits=8, iou_threshold=0.3)
busTracker = Sort(max_age=40, min_hits=6, iou_threshold=0.3)
truckTracker = Sort(max_age=40, min_hits=6, iou_threshold=0.3)
boatTracker = Sort(max_age=40, min_hits=6, iou_threshold=0.3)
airplaneTracker = Sort(max_age=40, min_hits=6, iou_threshold=0.3)
bicycleTracker = Sort(max_age=40, min_hits=6, iou_threshold=0.3)
motorcycleTracker = Sort(max_age=40, min_hits=6, iou_threshold=0.3)
humanLogs = []
carLogs = []
busLogs = []
truckLogs = []
boatLogs = []
airplaneLogs = []
bicycleLogs = []
motorcycleLogs = []
while True:
success, img = droneFeedCapture.read()
results = warhawkModel(img, stream=True, classes=warhawkAiClassesToDetect)
humanDetections = np.empty((0, 5))
carDetections = np.empty((0, 5))
busDetections = np.empty((0, 5))
truckDetections = np.empty((0, 5))
boatDetections = np.empty((0, 5))
airplaneDetections = np.empty((0, 5))
motorcycleDetections = np.empty((0, 5))
bicycleDetections = np.empty((0, 5))
for r in results:
for box in r.boxes:
x1, y1, x2, y2 = box.xyxy[0]
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
conf = warhawkMath.ceil((box.conf[0] * 100)) / 100
objectClass = warhawkAiClassnames[int(box.cls[0])]
if objectClass == 'person':
warhawkAiInterpretor.cornerRect(img, (x1, y1, x2 - x1, y2 - y1), l=9, rt=2, t=2)
warhawkAiInterpretor.putTextRect(img, f'{objectClass} {conf}', (max(0, x1), max(35, y1)), scale=1,
thickness=1, offset=10, font=warhawkAiMedia.FONT_HERSHEY_PLAIN)
currentArray = np.array([x1, y1, x2, y2, conf])
humanDetections = np.vstack((humanDetections, currentArray))
elif objectClass == 'car':
warhawkAiInterpretor.cornerRect(img, (x1, y1, x2 - x1, y2 - y1), l=9, rt=2, t=2, colorR=(255, 0, 0))
warhawkAiInterpretor.putTextRect(img, f'{objectClass} {conf}', (max(0, x1), max(0, y1)), scale=1,
thickness=1, offset=10, colorR=(255, 0, 0),
font=warhawkAiMedia.FONT_HERSHEY_PLAIN)
currentArray = np.array([x1, y1, x2, y2, conf])
carDetections = np.vstack((carDetections, currentArray))
elif objectClass == 'truck':
warhawkAiInterpretor.cornerRect(img, (x1, y1, x2 - x1, y2 - y1), l=9, rt=2, t=2,
colorR=(0, 75, 150))
warhawkAiInterpretor.putTextRect(img, f'{objectClass} {conf}', (max(0, x1), max(35, y1)), scale=1,
thickness=1, offset=10, colorR=(0, 75, 150),
font=warhawkAiMedia.FONT_HERSHEY_PLAIN)
currentArray = np.array([x1, y1, x2, y2, conf])
truckDetections = np.vstack((truckDetections, currentArray))
elif objectClass == 'bus':
warhawkAiInterpretor.cornerRect(img, (x1, y1, x2 - x1, y2 - y1), l=9, rt=2, t=2, colorR=(11, 11, 155))
warhawkAiInterpretor.putTextRect(img, f'{objectClass} {conf}', (max(0, x1), max(35, y1)), scale=1,
thickness=1, offset=10, colorR=(11, 11, 155), font=warhawkAiMedia.FONT_HERSHEY_PLAIN,)
currentArray = np.array([x1, y1, x2, y2, conf])
busDetections = np.vstack((busDetections, currentArray))
elif objectClass == 'boat':
warhawkAiInterpretor.cornerRect(img, (x1, y1, x2 - x1, y2 - y1), l=9, rt=2, t=2, colorR=(255, 255, 0))
warhawkAiInterpretor.putTextRect(img, f'{objectClass} {conf}', (max(0, x1), max(35, y1)), scale=1,
thickness=1, offset=10, colorR=(255, 100, 225), font=warhawkAiMedia.FONT_HERSHEY_PLAIN,)
currentArray = np.array([x1, y1, x2, y2, conf])
boatDetections = np.vstack((boatDetections, currentArray))
elif objectClass == 'motorcycle':
warhawkAiInterpretor.cornerRect(img, (x1, y1, x2 - x1, y2 - y1), l=9, rt=2, t=2, colorR=(192, 192, 192))
warhawkAiInterpretor.putTextRect(img, f'{objectClass} {conf}', (max(0, x1), max(35, y1)), scale=1,
thickness=1, offset=10, colorR=(192, 192, 192), font=warhawkAiMedia.FONT_HERSHEY_PLAIN,)
currentArray = np.array([x1, y1, x2, y2, conf])
motorcycleDetections = np.vstack((motorcycleDetections, currentArray))
elif objectClass == 'airplane':
warhawkAiInterpretor.cornerRect(img, (x1, y1, x2 - x1, y2 - y1), l=9, rt=2, t=2, colorR=(0, 0, 0))
warhawkAiInterpretor.putTextRect(img, f'{objectClass} {conf}', (max(0, x1), max(35, y1)), scale=1,
thickness=1, offset=10, colorR=(0, 0, 0), font=warhawkAiMedia.FONT_HERSHEY_PLAIN,)
currentArray = np.array([x1, y1, x2, y2, conf])
airplaneDetections = np.vstack((airplaneDetections, currentArray))
elif objectClass == 'bicycle':
warhawkAiInterpretor.cornerRect(img, (x1, y1, x2 - x1, y2 - y1), l=9, rt=2, t=2, colorR=(8, 33, 12))
warhawkAiInterpretor.putTextRect(img, f'{objectClass} {conf}', (max(0, x1), max(35, y1)), scale=1,
thickness=1, offset=10, colorR=(8, 33, 12), font=warhawkAiMedia.FONT_HERSHEY_PLAIN,)
currentArray = np.array([x1, y1, x2, y2, conf])
bicycleDetections = np.vstack((bicycleDetections, currentArray))
if r.boxes:
humanTrackerResults = humanTracker.update(humanDetections)
carTrackerResults = carTracker.update(carDetections)
busTrackerResults = busTracker.update(busDetections)
truckTrackerResults = truckTracker.update(truckDetections)
boatTrackerResults = boatTracker.update(boatDetections)
airplaneTrackerResults = airplaneTracker.update(airplaneDetections)
bicycleTrackerResults = bicycleTracker.update(bicycleDetections)
motorcycleTrackerResults = motorcycleTracker.update(motorcycleDetections)
for result in humanTrackerResults:
x1, y1, x2, y2, id = result
if int(id) not in humanLogs:
humanLogs.append(int(id))
with open("./data/persons.log", "w") as txt_file:
for line in humanLogs:
txt_file.write(f'Person-{line} (Co-ordinates)' + "\n")
for result in truckTrackerResults:
x1, y1, x2, y2, id = result
if int(id) not in truckLogs:
truckLogs.append(int(id))
with open("./data/trucks.log", "w") as txt_file:
for line in truckLogs:
txt_file.write(f'Truck-{line} (Co-ordinates)' + "\n")
for result in carTrackerResults:
x1, y1, x2, y2, id = result
if int(id) not in carLogs:
carLogs.append(int(id))
with open("./data/cars.log", "w") as txt_file:
for line in carLogs:
txt_file.write(f'Cars-{line} (Co-ordinates)' + "\n")
for result in busTrackerResults:
x1, y1, x2, y2, id = result
if int(id) not in busLogs:
busLogs.append(int(id))
with (open("./data/bus.log", "w") as txt_file):
for line in busLogs:
txt_file.write(f'Bus-{line} (Co-ordinates)' + "\n")
for result in boatTrackerResults:
x1, y1, x2, y2, id = result
if int(id) not in boatLogs:
boatLogs.append(int(id))
with (open("./data/boat.log", "w") as txt_file):
for line in boatLogs:
txt_file.write(f'Boat-{line} (Co-ordinates)' + "\n")
for result in motorcycleTrackerResults:
x1, y1, x2, y2, id = result
if int(id) not in motorcycleLogs:
motorcycleLogs.append(int(id))
with (open("./data/motorcycle.log", "w") as txt_file):
for line in motorcycleLogs:
txt_file.write(f'Motorcycle-{line} (Co-ordinates)' + "\n")
for result in airplaneTrackerResults:
x1, y1, x2, y2, id = result
if int(id) not in airplaneLogs:
airplaneLogs.append(int(id))
with (open("./data/airplane.log", "w") as txt_file):
for line in airplaneLogs:
txt_file.write(f'Airplane-{line} (Co-ordinates)' + "\n")
for result in bicycleTrackerResults:
x1, y1, x2, y2, id = result
if int(id) not in bicycleLogs:
bicycleLogs.append(int(id))
with (open("./data/bicycle.log", "w") as txt_file):
for line in bicycleLogs:
txt_file.write(f'Bicycle-{line} (Co-ordinates)' + "\n")
yield img
# warhawkAiMedia.imshow("Drone Live Video", img)
# if (warhawkAiMedia.waitKey(1) & 0xFF) == ord('q'):
# warhawkAiMedia.destroyAllWindows()
# break