-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
342 lines (288 loc) · 11.4 KB
/
main.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
# Import necessary packages
import time
import cv2
import csv
import numpy as np
from tracker import *
from datetime import datetime, timedelta
import CVCascadeFilter
from threading import Timer
# Initialize Tracker
tracker = EuclideanDistTracker()
greenack = 0
redack = 1
redack1 = 1
greenack1 = 0
dt2 = 0
dt1 = 0
# Initialize the videocapture object
cap = cv2.VideoCapture('ind.mp4')
input_size = 320
# Detection confidence threshold
confThreshold = 0.8
nmsThreshold = 0.5
font_color = (52,235,198)
font_size = 0.5
font_thickness = 2
# Middle cross line position
middle_line_position = 300
up_line_position = middle_line_position - 10
down_line_position = middle_line_position + 10
# Store Coco Names in a list
classesFile = "coco.names"
classNames = open(classesFile).read().strip().split('\n')
# class index for our required detection classes
required_class_index = [2, 3, 5, 7]
detected_classNames = []
# Model Files
modelConfiguration = 'yolov3-320.cfg'
modelWeigheights = 'yolov3-320.weights'
# configure the network model
net = cv2.dnn.readNetFromDarknet(modelConfiguration, modelWeigheights)
# Configure the network backenda
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
# Define random colour for each class
np.random.seed(42)
colors = np.random.randint(0, 255, size=(len(classNames), 3), dtype='uint8')
def redLight(img):
global redack
global redack1
global greenack
global greenack1
global dt2
global dt1
if redack == 1:
if redack1 == 1:
dt2 = int(("%s" % datetime.now().second))
redack1 = 0
dt1 = int(("%s" % datetime.now().second))
#print("dt1",dt1,dt2)
# print("dt2",dt2)
cc = 0
cc2 = 0
if dt1 > dt2:
cc = dt1
cc2 = dt2
else:
cc = dt2
cc2 = dt1
if cc < (cc2 + 20):
cv2.circle(img, (530, 30), 14, (0, 0, 255), -1)
#cv2.circle(img, (430, 30), 14, (0, 0, 255), -1)
cv2.circle(img, (430, 65), 14, (0, 255, 0), -1)
str1 = str(dt1 - dt2)
cv2.putText(img, str1, (550, 30), cv2.FONT_HERSHEY_SIMPLEX, font_size, font_color, 1)
cv2.putText(img, str1, (450, 69), cv2.FONT_HERSHEY_SIMPLEX, font_size, font_color, 1)
greenack = 0
else:
greenack = 1
greenack1 = 1
redack = 0
def greenLight(img, density):
global redack
global redack1
global greenack
global greenack1
global dt2
global dt1
yu = 0
if density < 10:
yu = 5
else:
yu = 10
if greenack == 1:
if greenack1 == 1:
dt2 = int(("%s" % datetime.now().second))
greenack1 = 0
dt1 = int(("%s" % datetime.now().second))
#print("dt1",dt1, dt2)
# print("dt2",dt2)
cc = 0
cc2 = 0
if dt1 > dt2:
cc = dt1
cc2 = dt2
else:
cc = dt2
cc2 = dt1
if cc < cc2 + yu:
cv2.circle(img, (530, 65), 14, (0, 255, 0), -1)
#cv2.circle(img, (430, 65), 14, (0, 255, 0), -1)
cv2.circle(img, (430, 30), 14, (0, 0, 255), -1)
str1 = str(dt1 - dt2)
cv2.putText(img, str1, (550, 69), cv2.FONT_HERSHEY_SIMPLEX, font_size, font_color, 1)
cv2.putText(img, str1, (450, 30), cv2.FONT_HERSHEY_SIMPLEX, font_size, font_color, 1)
redack = 0
else:
redack = 1
redack1 = 1
greenack = 0
# Function for finding the center of a rectangle
def find_center(x, y, w, h):
x1 = int(w / 2)
y1 = int(h / 2)
cx = x + x1
cy = y + y1
return cx, cy
# List for store vehicle count information
temp_up_list = []
temp_down_list = []
up_list = [0, 0, 0, 0]
down_list = [0, 0, 0, 0]
startTracker = {} # STORE STARTING TIME OF CARS
endTracker = {} # STORE ENDING TIME OF CARS
markGap = 20
fpsFactor = 3
# Function for count vehicle
def count_vehicle(density, box_id, img):
x, y, w, h, id, index = box_id
# Find the center of the rectangle for detection
center = find_center(x, y, w, h)
ix, iy = center
# Find the current position of the vehicle
if (iy > up_line_position) and (iy < middle_line_position):
if id not in temp_up_list:
startTracker[id] = datetime.now()
temp_up_list.append(id)
elif (iy < down_line_position) and (iy > middle_line_position):
if id not in temp_down_list:
temp_down_list.append(id)
elif iy < up_line_position:
if id in temp_down_list:
temp_down_list.remove(id)
up_list[index] = up_list[index] + 1
elif iy > down_line_position:
if id in temp_up_list:
temp_up_list.remove(id)
endTracker[id] = datetime.now()
down_list[index] = down_list[index] + 1
# Draw circle in the middle of the rectangle
cv2.circle(img, center, 2, (0, 0, 255), -1) # end here
# print(up_list, down_list)
# Function for calculating the speed of vehicle
def estimateSpeed(position):
timeDiff = int(CVCascadeFilter.CVCascadeFilter.timings(bin(position).replace("0b", "")), 2)
# print("timeDiff", timeDiff)
speed = round(markGap / timeDiff * fpsFactor * 3.6, 2)
return speed
# Function for finding the detected objects from the network output
def postProcess(outputs, img):
global detected_classNames
height, width = img.shape[:2]
boxes = []
classIds = []
confidence_scores = []
detection = []
for output in outputs:
for det in output:
scores = det[5:]
classId = np.argmax(scores)
confidence = scores[classId]
if classId in required_class_index:
if confidence > confThreshold:
# print(classId)
w, h = int(det[2] * width), int(det[3] * height)
x, y = int((det[0] * width) - w / 2), int((det[1] * height) - h / 2)
boxes.append([x, y, w, h])
classIds.append(classId)
confidence_scores.append(float(confidence))
# Apply Non-Max Suppression
indices = cv2.dnn.NMSBoxes(boxes, confidence_scores, confThreshold, nmsThreshold)
# print(classIds)
for i in indices.flatten():
x, y, w, h = boxes[i][0], boxes[i][1], boxes[i][2], boxes[i][3]
# print(x,y,w,h)
color = [int(c) for c in colors[classIds[i]]]
name = classNames[classIds[i]]
detected_classNames.append(name)
# Draw classname and confidence score
speed = estimateSpeed(i)
cv2.putText(img, f'{name.upper()} {int(confidence_scores[i] * 100)}% {speed} Kmph',
(x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1)
# Draw bounding rectangle
cv2.rectangle(img, (x, y), (x + w, y + h), color, 1)
detection.append([x, y, w, h, required_class_index.index(classIds[i])])
# Update the tracker for each object
boxes_ids = tracker.update(detection)
density = len(boxes_ids)
for box_id in boxes_ids:
count_vehicle(density, box_id, img)
return density
def realTime():
while True:
success, img = cap.read()
img = cv2.resize(img, (0, 0), None, 0.5, 0.5)
ih, iw, channels = img.shape
blob = cv2.dnn.blobFromImage(img, 1 / 255, (input_size, input_size), [0, 0, 0], 1, crop=False)
# Set the input of the network
net.setInput(blob)
layersNames = net.getLayerNames()
outputNames = [layersNames[i - 1] for i in net.getUnconnectedOutLayers()]
#outputNames = net.getUnconnectedOutLayers()
# Feed data to the network
outputs = net.forward(outputNames)
# Find the objects from the network output
den = postProcess(outputs, img)
# print("denstity", den)
# Draw the crossing lines
cv2.line(img, (500, 10), (560, 10), (0, 255, 0), 2)
cv2.line(img, (500, 10), (500, 120), (0, 255, 0), 2)
cv2.line(img, (560, 10), (560, 120), (0, 255, 0), 2)
cv2.line(img, (500, 120), (560, 120), (0, 255, 0), 2)
cv2.circle(img, (530, 30), 15, (0, 0, 0), 2)
cv2.circle(img, (530, 65), 15, (0, 0, 0), 2)
cv2.circle(img, (530, 100), 15, (0, 0, 0), 2)
cv2.line(img, (400, 10), (460, 10), (0, 255, 0), 2)
cv2.line(img, (400, 10), (400, 120), (0, 255, 0), 2)
cv2.line(img, (460, 10), (460, 120), (0, 255, 0), 2)
cv2.line(img, (400, 120), (460, 120), (0, 255, 0), 2)
cv2.circle(img, (430, 30), 15, (0, 0, 0), 2)
cv2.circle(img, (430, 65), 15, (0, 0, 0), 2)
cv2.circle(img, (430, 100), 15, (0, 0, 0), 2)
redLight(img)
density = 0
if 0 <= den <= 20:
density = 5
elif 20 <= den <= 40:
density = 10
elif (41 <= den):
density = 15
greenLight(img, density)
cv2.line(img, (0, middle_line_position), (iw, middle_line_position), (255, 0, 255), 2)
cv2.putText(img, "Up ", (40, 270), cv2.FONT_HERSHEY_SIMPLEX, font_size, font_color, font_thickness)
cv2.line(img, (0, up_line_position), (iw, up_line_position), (28, 44, 253), 2)
cv2.putText(img, "Down ", (40, 340), cv2.FONT_HERSHEY_SIMPLEX, font_size, font_color, font_thickness)
cv2.line(img, (0, down_line_position), (iw, down_line_position), (23, 2, 7), 2)
# Draw counting texts in the frame
cv2.putText(img, "Up", (110, 20), cv2.FONT_HERSHEY_SIMPLEX, font_size, font_color, font_thickness)
cv2.putText(img, "Down", (160, 20), cv2.FONT_HERSHEY_SIMPLEX, font_size, font_color, font_thickness)
cv2.putText(img, "Car: " + str(up_list[0]) + " " + str(down_list[0]), (20, 40),
cv2.FONT_HERSHEY_SIMPLEX, font_size, font_color, font_thickness)
cv2.putText(img, "Motorbike: " + str(up_list[1]) + " " + str(down_list[1]), (20, 60),
cv2.FONT_HERSHEY_SIMPLEX, font_size, font_color, font_thickness)
cv2.putText(img, "Bus: " + str(up_list[2]) + " " + str(down_list[2]), (20, 80),
cv2.FONT_HERSHEY_SIMPLEX, font_size, font_color, font_thickness)
cv2.putText(img, "Truck: " + str(up_list[3]) + " " + str(down_list[3]), (20, 100),
cv2.FONT_HERSHEY_SIMPLEX, font_size, font_color, font_thickness)
# Show the frames
cv2.imshow('Output', img)
if cv2.waitKey(1) == ord('q'):
break
# Write the vehicle counting information in a file and save it
with open("data.csv", 'w') as f1:
cwriter = csv.writer(f1)
cwriter.writerow(['Direction', 'car', 'motorbike', 'bus', 'truck'])
up_list.insert(0, "Up")
down_list.insert(0, "Down")
cwriter.writerow(up_list)
cwriter.writerow(down_list)
f1.close()
# print("Data saved at 'data.csv'")
# Finally realese the capture object and destroy all active windows
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
realTime()
postProcess()
count_vehicle()