-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathface-detection.py
54 lines (39 loc) · 1.73 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
import cv2
# LOAD CASCADES
face_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_eye.xml')
# SET UP THE VIDEO RECORDER
cap = cv2.VideoCapture(0)
# MODIFY HOW THE VIDEO RECORDER
while True:
# this will capture the video frame-by-frame
ret, img = cap.read()
# I create a copy of the input image in grayscale mode
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# read the input image to detect faces
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
trc = (x,y) # face rectangle's top-right corner
blc = (x+w,y+h) # face rectangle's bottom-left corner
rectcolor = (255,0,0) # blue color
thick = 2 # line thickness
# draw a rectangle whenever a face is detected
cv2.rectangle(img, trc, blc, rectcolor, thick)
# slice image area containing the face
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
# read the face area and detect eyes
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
trc = (ex,ey) # eye rectangle's top-right corner
blc = (ex+ew,ey+eh) # eye rectangle's bottom-left corner
rectcolor = (0,255,0) # green color
thick = 2 # line thickness
#draw a rectangle whenever an eye is detected
cv2.rectangle(roi_color, trc, blc, rectcolor, thick)
cv2.imshow('Face Detector', img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()