-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCar & Pedestrian Detection.py
89 lines (56 loc) · 1.84 KB
/
Car & Pedestrian 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
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
#!/usr/bin/env python
# coding: utf-8
# ## Car & Pedestrian Detection
#
#
# ### Pedistrian Detection
# In[1]:
import cv2
import numpy as np
# Create our body classifier
body_classifier = cv2.CascadeClassifier('Haarcascades\haarcascade_fullbody.xml')
# Initiate video capture for video file
cap = cv2.VideoCapture('image_examples/walking.avi')
# Loop once video is successfully loaded
while cap.isOpened():
# Read first frame
ret, frame = cap.read()
#frame = cv2.resize(frame, None,fx=0.5, fy=0.5, interpolation = cv2.INTER_LINEAR)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Pass frame to our body classifier
bodies = body_classifier.detectMultiScale(gray, 1.2, 3)
# Extract bounding boxes for any bodies identified
for (x,y,w,h) in bodies:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 255), 2)
cv2.imshow('Pedestrians', frame)
if cv2.waitKey(1) == 13: #13 is the Enter Key
break
cap.release()
cv2.destroyAllWindows()
# ### Car Detection
#
# In[ ]:
import cv2
import time
import numpy as np
# Create our body classifier
car_classifier = cv2.CascadeClassifier('Haarcascades\haarcascade_car.xml')
# Initiate video capture for video file
cap = cv2.VideoCapture('image_examples/cars.avi')
# Loop once video is successfully loaded
while cap.isOpened():
time.sleep(.05)
# Read first frame
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Pass frame to our car classifier
cars = car_classifier.detectMultiScale(gray, 1.4, 2)
# Extract bounding boxes for any bodies identified
for (x,y,w,h) in cars:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 255), 2)
cv2.imshow('Cars', frame)
if cv2.waitKey(1) == 13: #13 is the Enter Key
break
cap.release()
cv2.destroyAllWindows()
# In[ ]: