-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcar_detector.py
More file actions
47 lines (34 loc) · 1.15 KB
/
car_detector.py
File metadata and controls
47 lines (34 loc) · 1.15 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
46
47
# Load the Haar cascade file
import cv2
face_cascade = cv2.CascadeClassifier('cars.xml')
# Check if the cascade file has been loaded correctly
if face_cascade.empty():
raise IOError('Unable to load the face cascade classifier xml file')
cap = cv2.VideoCapture('race2.mp4')
# Define the scaling factor
scaling_factor = 1
# Iterate until the user hits the 'Esc' key
while True:
# Capture the current frame
_, frame = cap.read()
# Resize the frame
frame = cv2.resize(frame, None,
fx=scaling_factor, fy=scaling_factor,
interpolation=cv2.INTER_AREA)
# Convert to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Run the car detector on the grayscale image
face_rects = face_cascade.detectMultiScale(gray, 1.3, 5)
# Draw a rectangle around the car
for (x,y,w,h) in face_rects:
cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,0), 3)
# Display the output
cv2.imshow('Face Detector', frame)
# Check if the user hit the 'Esc' key
c = cv2.waitKey(1)
if c == 27:
break
# Release the video capture object
cap.release()
# Close all the windows
cv2.destroyAllWindows()