-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvehicle_detection1.py
391 lines (310 loc) · 12.3 KB
/
vehicle_detection1.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import cv2
import numpy as np
# Load the pre-trained YOLOv4 model for vehicle detection
net = cv2.dnn.readNetFromDarknet('C:/Users/Administrator/Documents/JIO INSTITUTE/computer vision/Autonomous_vehicles/yolov4.cfg',
'C:/Users/Administrator/Documents/JIO INSTITUTE/computer vision/Autonomous_vehicles/yolov4.weights')
# Reading the input image
image = cv2.imread('vehicles.jpg')
# Open a video file or stream
cap = cv2.VideoCapture('vehicle_movement.mp4')
# Preprocess the input image
blob = cv2.dnn.blobFromImage(image, 1/255, (416, 416), swapRB=True, crop=False)
# Preprocess the video frames
while True:
ret, frame = cap.read()
if not ret:
break
blob = cv2.dnn.blobFromImage(
frame, 1/255, (416, 416), swapRB=True, crop=False)
# Feed the input image through the YOLO model
net.setInput(blob)
detections = net.forward()
# # Feed the video frames through the YOLO model
# net.setInput(blob)
# detections = net.forward()
# Postprocess the detection results for the input image
boxes = []
confidences = []
class_ids = []
for detection in detections:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5 and class_id == 2: # 2 is the class ID for vehicles in YOLOv4
center_x = int(detection[0] * image.shape[1])
center_y = int(detection[1] * image.shape[0])
width = int(detection[2] * image.shape[1])
height = int(detection[3] * image.shape[0])
left = int(center_x - width/2)
top = int(center_y - height/2)
boxes.append([left, top, width, height])
confidences.append(float(confidence))
class_ids.append(class_id)
indices = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
# Extract the vehicle bounding boxes and classification labels for the input image
# vehicle_boxes = []
# vehicle_labels = []
# for i in indices:
# i = i[0]
# if class_ids[i] == 2: # 2 is the class ID for vehicles in YOLOv4
# box = boxes[i]
# left = box[0]
# top = box[1]
# width = box[2]
# height = box[3]
# vehicle_boxes.append((left, top, left+width, top+height))
# vehicle_labels.append('vehicle')
# Postprocess the detection results for the video frames
# while True:
# ret, frame = cap.read()
# if not ret:
# break
# Preprocess the current video frame
# if frame is not None and frame.shape[0] > 0 and frame.shape[1] > 0:
# blob = cv2.dnn.blobFromImage(
# frame, 1/255, (416, 416), swapRB=True, crop=False)
# net.setInput(blob)
# # Your code for processing the frame goes here
# # blob = cv2.dnn.blobFromImage(frame, 1/255, (416, 416), swapRB=True, crop=False)
# # Feed the current video frame through the YOLO model
# net.setInput(blob)
# detections = net.forward()
# # Postprocess the detection results for the current video frame
# boxes = []
# confidences = []
# class_ids = []
# for detection in detections:
# scores = detection[5:]
# class_id = np.argmax(scores)
# confidence = scores[class_id]
# if confidence > 0.5 and class_id == 2: # 2 is the class ID for vehicles in YOLOv4
# center_x = int(detection[0] * frame.shape[1])
# center_y = int(detection[1] * frame.shape[0])
# width = int(detection[2] * frame.shape[1])
# height = int(detection[3] * frame.shape[0])
# left = int(center_x - width/2)
# top = int(center_y - height/2)
# boxes.append([left, top, width, height])
# confidences.append(float(confidence))
# class_ids.append(class_id)
# indices = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
# Extract the vehicle bounding boxes and classification labels for the current video frame
current_vehicle_boxes = []
current_vehicle_labels = []
for i in indices:
i = i[0]
if class_ids[i] == 2: # 2 is the class ID for vehicles in YOLOv4
box = boxes[i]
left = box[0]
top = box[1]
width = box[2]
height = box[3]
current_vehicle_boxes.append((left, top, left+width, top+height))
current_vehicle_labels.append('vehicle')
# Display the current video frame with vehicle bounding boxes and classification labels
for box, label in zip(current_vehicle_boxes, current_vehicle_labels):
left, top, right, bottom = box
cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
cv2.putText(frame, label, (left, top-5),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# cv2.imshow('Vehicle Detection', frame)
# Exit the video display window when the 'q' key is pressed
if cv2.waitKey(1) == ord('q'):
break
# Release the resources used for the video analysis
cap.release()
cv2.destroyAllWindows()
'''checking something here, kindly ignore the codes below until it works well'''
# Draw the bounding boxes on the image
for box, label in zip(vehicle_boxes, vehicle_labels):
left, top, right, bottom = box
cv2.rectangle(image, (left, top), (right, bottom), (0, 255, 0), 2)
cv2.putText(image, label, (left, top-5),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# Save the image to the current directory
cv2.imwrite('output.jpg', image)
'''checking the video without running all'''
# Open input video file
cap = cv2.VideoCapture('vehicle_movement.mp4')
# Get input video stream properties
fps = int(cap.get(cv2.CAP_PROP_FPS))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# Create output video writer
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('output_video.mp4', fourcc, fps, (width, height))
# Loop through each frame in the input video stream
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Process frame here
processed_frame = frame # Replace this with your own processing code
# Write processed frame to output video stream
out.write(processed_frame)
# Display processed frame (optional)
cv2.imshow('Processed Frame', processed_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release input and output video streams
cap.release()
out.release()
# Close all windows
cv2.destroyAllWindows()
'''Using another preprocessing'''
# Load YOLOv4 model
net = cv2.dnn.readNet('yolov4.weights', 'yolov4.cfg')
# Load class names
with open('coco.names', 'r') as f:
classes = [line.strip() for line in f.readlines()]
# Set input and output video paths
input_path = 'vehicle_movement.mp4'
output_path = 'output_new.mp4'
# Open input video stream
cap = cv2.VideoCapture(input_path)
# Get input video stream properties
fps = int(cap.get(cv2.CAP_PROP_FPS))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# Create output video writer
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
# Loop through each frame in the input video stream
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Preprocess frame
blob = cv2.dnn.blobFromImage(
frame, 1 / 255, (416, 416), swapRB=True, crop=False)
# Set input to YOLOv4 model
net.setInput(blob)
# Forward pass through YOLOv4 model
outputs = net.forward(net.getUnconnectedOutLayersNames())
# Postprocess detections
boxes = []
confidences = []
class_ids = []
for output in outputs:
for detection in output:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
x = int(center_x - w / 2)
y = int(center_y - h / 2)
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(class_id)
# Apply non-max suppression
indices = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
# Draw bounding boxes around detected objects
for i in indices:
i = i[0]
x, y, w, h = boxes[i]
label = classes[class_ids[i]]
color = (0, 255, 0)
cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
cv2.putText(frame, label, (x, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
# Write processed frame to output video stream
out.write(frame)
# Display processed frame (optional)
cv2.imshow('Processed Frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release input and output video streams
cap.release()
out.release()
# Close all windows
cv2.destroyAllWindows()
'''Third part'''
# Load YOLOv4 model
net = cv2.dnn.readNet('yolov4.weights', 'yolov4.cfg')
# Load class names
with open('coco.names', 'r') as f:
classes = [line.strip() for line in f.readlines()]
# Set input and output video paths
input_path = 'vehicle_movement.mp4'
output_path = 'output_new.mp4'
# Open input video stream
cap = cv2.VideoCapture(input_path)
# Get input video stream properties
fps = int(cap.get(cv2.CAP_PROP_FPS))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# Create output video writer
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
# Define ROI (Region of Interest)
roi = [(int(width * 0.45), int(height * 0.6)), (int(width * 0.55), int(height * 0.6)),
(int(width * 0.8), int(height)), (int(width * 0.2), int(height))]
# Initialize vehicle counter
vehicle_counter = 0
# Loop through each frame in the input video stream
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Preprocess frame
blob = cv2.dnn.blobFromImage(
frame, 1 / 255, (416, 416), swapRB=True, crop=False)
# Set input to YOLOv4 model
net.setInput(blob)
# Forward pass through YOLOv4 model
outputs = net.forward(net.getUnconnectedOutLayersNames())
# Postprocess detections
boxes = []
confidences = []
class_ids = []
for output in outputs:
for detection in output:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
x = int(center_x - w / 2)
y = int(center_y - h / 2)
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(class_id)
# Apply non-max suppression
indices = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
# Draw bounding boxes around detected objects
for i in indices:
i = i[0]
x, y, w, h = boxes[i]
label = classes[class_ids[i]]
color = (0, 255, 0)
cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
cv2.putText(frame, label, (x, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
# Check if object center is within ROI
object_center_x = int(x + w / 2)
object_center_y = int(y + h / 2)
point = (object_center_x, object_center_y)
if cv2.pointPolygonTest(roi_contour, point, False) >= 0:
# Object is within ROI, update count
object_count += 1
# Write object count on frame
cv2.putText(frame, f"Object count: {object_count}",
(50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
# Write processed frame to output video stream
out.write(frame)
# Display processed frame (optional)
cv2.imshow('Processed Frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release input and output video streams
cap.release()
out.release()
# Close all windows
cv2.destroyAllWindows()