-
Notifications
You must be signed in to change notification settings - Fork 0
/
darknet_video.py
361 lines (275 loc) · 10.7 KB
/
darknet_video.py
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
355
356
357
358
359
360
361
from ctypes import *
import math
import random
#import mahotas
import os
import cv2
import numpy as np
import time
import darknet
import matplotlib.pyplot as plt
from numpy import *
from scipy.interpolate import *
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
def initialgetCoordinates(a,b,c,d):
vehicle=[]
instance = []
vehicle.append(a)
vehicle.append(b)
vehicle.append(c)
vehicle.append(d)
instance.append(vehicle)
coordinates.append(instance)
#print(coordinates)
def postCoordinates(vehicles):
used_vehicles = []
used_coordinates = []
not_used_coordinates=[]
#print(len(coordinates))
for i in range(0,len(vehicles)):
current_vehicle = vehicles[i]
calc = []
for j in range(0,len(coordinates)):
current_coordinate = coordinates[j][-1]
cal = (int(current_vehicle[0])- int(current_coordinate[0]))**2 + (int(current_vehicle[1])- int(current_coordinate[1]))**2 + (int(current_vehicle[2])- int(current_coordinate[2]))**2 + (int(current_vehicle[3])- int(current_coordinate[3]))**2
calc.append(cal)
#print(calc)
#print(calc)
if(len(calc)>0):
minimum = min(calc)
#print(minimum)
inde = calc.index(minimum)
#print(inde)
if(minimum<200):
coordinates[inde].append(current_vehicle)
used_coordinates.append(inde)
used_vehicles.append(i)
for i in range(0,len(coordinates)):
if(i not in used_coordinates):
not_used_coordinates.append(coordinates[i])
for i in range(0,len(not_used_coordinates)):
if(not_used_coordinates[i] in coordinates ):
coordinates.remove(not_used_coordinates[i])
for i in range(0,len(vehicles)):
if(i not in used_vehicles):
coordinates.append([vehicles[i]])
#print(coordinates)
return coordinates
def convertBack(x, y, w, h):
#xmin = int(round(x - (w / 2)))
xmin = float(x - (w / 2))
xmax = float(x + (w / 2))
ymin = float(y - (h / 2))
ymax = float(y + (h / 2))
return xmin, ymin, xmax, ymax
coordinates = []
def cvDrawBoxes(detections, img,count):
#dirName = 'crop/processed_images%d' %count
#try:
# Create target Directory
#os.mkdir(dirName)
#print("Directory " , dirName , " Created ")
#except FileExistsError:
#print("Directory " , dirName , " already exists")
#mahotas.imsave(dirName+"/aaaaa.jpg",img)
detected_vehicles = []
#calculating all the coordinates of all the vehicles in one frame
for i in range(0,len(detections)):
detection = detections[i]
x, y, w, h = detection[2][0],\
detection[2][1],\
detection[2][2],\
detection[2][3]
xmin, ymin, xmax, ymax = convertBack(
float(x), float(y), float(w), float(h))
pt1 = (xmin, ymin)
pt2 = (xmax, ymax)
pt11 = (int(xmin),int(ymin))
pt22 = (int(xmax), int(ymax))
vehicles = []
for detection in detections:
#print(detection[2])
#print(detection)
#if(str(detection[0])== "b'traffic light'"):
#print(detection[0])
var = detections.index(detection)
#print(detection)
x, y, w, h = detection[2][0],\
detection[2][1],\
detection[2][2],\
detection[2][3]
#print(x,y,w,h)
xmin, ymin, xmax, ymax = convertBack(
float(x), float(y), float(w), float(h))
#print(xmin, ymin, xmax, ymax,count)
pt1 = (xmin, ymin)
pt2 = (xmax, ymax)
pt11 = (int(xmin),int(ymin))
pt22 = (int(xmax), int(ymax))
#cropped_image = img[ymin:ymax, xmin:xmax]
#cropped_image = img.crop((pt1(0),pt1(1),pt2(0),pt2(1)))
#mahotas.imsave(dirName+"/image%d.jpg"%var,cropped_image)
add = {}
if(15<abs(pt1[0]-pt2[0])<250 and detection[1]>0.6 and pt2[1]<340 and str(detection[0])!="b'traffic light'" ):
if(count==0):
initialgetCoordinates(pt1[0],pt2[0],pt1[1],pt2[1])
else:
vehicle_i =[]
vehicle_i.append(pt1[0])
vehicle_i.append(pt2[0])
vehicle_i.append(pt1[1])
vehicle_i.append(pt2[1])
#print(vehicle_i)
vehicles.append(vehicle_i)
#print("pt11 is ," + str(pt11) )
cv2.rectangle(img, pt11, pt22, (0, 255, 0), 1)
cv2.putText(img,
detection[0].decode() +
" [" + str(round(detection[1] * 100, 2)) + "]",
(pt11[0], pt11[1] - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
[0, 255, 0], 2)
if(count>0):
#print(vehicles)
postCoordinates(vehicles)
img = checkWarning(coordinates,img,count)
#print(coordinates)
return img
def checkWarning(coordinates,img,count):
for i in range(0,len(coordinates)):
current = coordinates[i]
length = len(current)
if(length<3):
total_y = 0
for j in range(0,length):
total_y = total_y + current[j][-1]
average_y = total_y/length
#print(average_y)
#if(average_y > 330):
#print("average warning")
#cv2.putText(img,"warning",(10,50),cv2.FONT_HERSHEY_SIMPLEX,fontScale=2.5,thickness=5,color=(255,0,0))
#break
else:
y = []
y1 = [] #this is the x coordinates
y.append(current[-3][-1])
y.append(current[-2][-1])
y.append(current[-1][-1])
v2 = current[-3][-1] - current[-2][-1]
v1 = current[-2][-1] - current[-1][-1]
a = v2 -v1
average_v = (v1+v2)/2
s = (average_v*30) + (0.5*a*30*30)
num1 = (current[-3][0] + current[-3][1])/2
num2 = (current[-2][0] + current[-2][1])/2
num3 = (current[-1][0] + current[-1][1])/2
y1.append(num1)
y1.append(num2)
y1.append(num3)
x = [1,2,3]
#print(y)
y = array(y)
x = array(x)
y1 = array(y1)
model = polyfit(x,y,1)
predict = poly1d(model)
predicted_y = predict(120)
model1 = polyfit(x,y1,1)
predict1 = poly1d(model1)
predicted_x = predict1(120)
#if(420<s<800 and 125<predicted_x<275):
#cv2.putText(img,"warning",(10,50),cv2.FONT_HERSHEY_SIMPLEX,fontScale=2.5,thickness=5,color=(255,0,0))
#break
#print(predicted_y)
#print(s)
#print(predicted_x)
if(350<predicted_y and 85<predicted_x<380):
#print(y)
#print(predicted_y)
#print(y1)
#print("current vehicle is : " + str(current))
#print("predicted_x is : " + str(predicted_x))
#print("predicted_y is : " + str(predicted_y))
print(count)
print("predict warning")
#print(current)
cv2.putText(img,"warning",(10,50),cv2.FONT_HERSHEY_SIMPLEX,fontScale=2.5,thickness=5,color=(255,0,0))
break
return img
netMain = None
metaMain = None
altNames = None
def YOLO():
global metaMain, netMain, altNames
configPath = "./cfg/yolov3.cfg"
weightPath = "./yolov3.weights"
metaPath = "./cfg/coco.data"
if not os.path.exists(configPath):
raise ValueError("Invalid config path `" +
os.path.abspath(configPath)+"`")
if not os.path.exists(weightPath):
raise ValueError("Invalid weight path `" +
os.path.abspath(weightPath)+"`")
if not os.path.exists(metaPath):
raise ValueError("Invalid data file path `" +
os.path.abspath(metaPath)+"`")
if netMain is None:
netMain = darknet.load_net_custom(configPath.encode(
"ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1
if metaMain is None:
metaMain = darknet.load_meta(metaPath.encode("ascii"))
if altNames is None:
try:
with open(metaPath) as metaFH:
metaContents = metaFH.read()
import re
match = re.search("names *= *(.*)$", metaContents,
re.IGNORECASE | re.MULTILINE)
if match:
result = match.group(1)
else:
result = None
try:
if os.path.exists(result):
with open(result) as namesFH:
namesList = namesFH.read().strip().split("\n")
altNames = [x.strip() for x in namesList]
except TypeError:
pass
except Exception:
pass
#cap = cv2.VideoCapture(0)
cap = cv2.VideoCapture("008.mp4")
count = 0
cap.set(3, 1280)
cap.set(4, 720)
fps = cap.get(cv2.CAP_PROP_FPS)
print(fps)
out = cv2.VideoWriter(
"008.avi", cv2.VideoWriter_fourcc(*"XVID"), fps,
(darknet.network_width(netMain), darknet.network_height(netMain)))
print("Starting the YOLO loop...")
# Create an image we reuse for each detect
darknet_image = darknet.make_image(darknet.network_width(netMain),
darknet.network_height(netMain),3)
while True:
prev_time = time.time()
ret, frame_read = cap.read()
frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB)
frame_resized = cv2.resize(frame_rgb,
(darknet.network_width(netMain),
darknet.network_height(netMain)),
interpolation=cv2.INTER_LINEAR)
darknet.copy_image_from_bytes(darknet_image,frame_resized.tobytes())
detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.25)
image = cvDrawBoxes(detections, frame_resized,count)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
#cv2.putText(image,"warning",(10,50),cv2.FONT_HERSHEY_SIMPLEX,fontScale=2.5,thickness=5,color=(255,255,255))
out.write(image)
count = count+1
#print(1/(time.time()-prev_time))
cv2.waitKey(3)
cap.release()
out.release()
if __name__ == "__main__":
YOLO()