forked from olin-toolboxes/Toolbox-ComputerVision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
face_detect.py
43 lines (35 loc) · 1.43 KB
/
face_detect.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
""" Experiment with face detection and image filtering using OpenCV """
import cv2
import numpy as np
black = (0, 0, 0)
white = (255, 255, 255)
cap = cv2.VideoCapture(0)
while True:
# detect face
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
ret, frame = cap.read()
faces = face_cascade.detectMultiScale(frame, scaleFactor=1.2, minSize=(20, 20))
# create a NumPy matrix, which controls the degree of blurring
kernel = np.ones((40, 40), 'uint8')
for (x, y, w, h) in faces:
# blurring the face
frame[y:y+h, x:x+w, :] = cv2.dilate(frame[y:y+h, x:x+w, :], kernel)
# draw a face
# mouth
cv2.line(frame, (int(x+w*.3), int(y+h*.75)), (int(x+w*.7), int(y+h*.75)), black, 10)
# Nose
cv2.line(frame, (int(x+w*.5), int(y+h*.55)), (int(x+w*.5), int(y+h*.4)), black, 10)
# eyes
cv2.circle(frame, (int(x+w*.7), int(y+h*.35)), 20, white, -1)
cv2.circle(frame, (int(x+w*.3), int(y+h*.35)), 20, white, -1)
cv2.circle(frame, (int(x+w*.7), int(y+h*.35)), 10, black, -1)
cv2.circle(frame, (int(x+w*.3), int(y+h*.35)), 10, black, -1)
# Display the resulting frame
cv2.imshow('frame', frame)
# Display the resulting frame
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()