-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCar_Ped_Detection.py
50 lines (38 loc) · 1.3 KB
/
Car_Ped_Detection.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
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 15 00:46:04 2021
@author: prashastha
"""
import cv2
# importing cascade classifier file with haar features
car_class = 'car_classifier.xml'
ped_class = 'ped_classifier.xml'
# importing video
video = cv2.VideoCapture('vehicles_and_pedestrian.mp4')
# generating cascade classifier
car_tracker = cv2.CascadeClassifier(car_class)
ped_tracker = cv2.CascadeClassifier(ped_class)
# loop through video
while True:
# read each frame
(read_successful, frame) = video.read()
if read_successful:
# convert each frame to greyscale
grayscale_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
else:
break
# apply classifier for identification
# will be returning a list of arrays (x_coordinate, y_coordinate,
# width, height)
cars = car_tracker.detectMultiScale(grayscale_frame)
peds = ped_tracker.detectMultiScale(grayscale_frame)
# green bounding box
for (x, y, w, h) in cars:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
# red bounding box
for (x, y, w, h) in peds:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 2)
cv2.imshow('Pedestrian and Vehicle Detection Device', frame)
cv2.waitKey(4)
cv2.waitKey(1)
cv2.destroyAllWindows()