-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
45 lines (29 loc) · 1.18 KB
/
main.py
File metadata and controls
45 lines (29 loc) · 1.18 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
# This file contains code on live Traffic Detection of a video file
import cv2
# Enter the path of video links below :
video = cv2.VideoCapture('Videos/Autopilot_Dashcam.mp4')
# video = cv2.VideoCapture('Videos/Demo_Vid.mp4')
# video = cv2.VideoCapture('Videos/Vid_3_1.mp4')
# video = cv2.VideoCapture('Videos/Pedestrians_1.mp4')
car_tracker_file = 'Classifier/car_detector.xml'
pedestrian_tracker_file = 'Classifier/pedestrian_detector.xml'
car_tracker = cv2.CascadeClassifier(car_tracker_file)
pedestrian_tracker = cv2.CascadeClassifier(pedestrian_tracker_file)
while True:
read_successful, frame = video.read()
#safe coding
if read_successful:
grayscaled_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
else:
break
cars = car_tracker.detectMultiScale(grayscaled_frame)
pedestrians = pedestrian_tracker.detectMultiScale(grayscaled_frame)
for (x, y, w, h) in cars:
cv2.rectangle(frame, (x,y), (x+w, y+h), (0, 0, 255),2)
for (x, y, w, h) in pedestrians:
cv2.rectangle(frame, (x,y), (x+w, y+h), (0, 255, 255),2)
cv2.imshow('Video',frame)
key = cv2.waitKey(1)
if key==81 or key==113:
break
video.release()