-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface detection.py
57 lines (43 loc) · 1.7 KB
/
face 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
import cv2
# Load the Haar cascade file
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_eye.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')
if eye_cascade.empty():
raise IOError('Unable to load the eye cascade classifier xml file')
# Initialize the video capture object
cap = cv2.VideoCapture(0)
# 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 face detector on the grayscale image
face_rects = face_cascade.detectMultiScale(gray, 1.3, 5)
# Run the eye detector on the grayscale image
eye_rects = eye_cascade.detectMultiScale(gray, 1.3, 5)
# Draw a rectangle around the face
for (x,y,w,h) in face_rects:
cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,0), 3)
# Draw a rectangle around the eye
for (x,y,w,h) in eye_rects:
cv2.rectangle(frame, (x,y), (x+w,y+h), (255,0,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()