-
Notifications
You must be signed in to change notification settings - Fork 7
/
FilterDetectFace.py
77 lines (65 loc) · 2.22 KB
/
FilterDetectFace.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
# face_mask.py
from imutils.video import VideoStream
from imutils import face_utils
import imutils
import time
import cv2
import dlib
print("[INFO] loading facial landmark predictor...")
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
print("[INFO] camera sensor warming up...")
vs = VideoStream(0, framerate=30).start()
time.sleep(2.0)
# loop over the frames from the video stream
while True:
# grayscale
frame = vs.read()
frame = imutils.resize(frame, height=550)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# detect faces in the grayayscale frame
rects = detector(gray, 0)
# loopop over found faces
for rect in rects:
shape = predictor(frame, rect)
shape = face_utils.shape_to_np(shape)
for (i, (x, y)) in enumerate(shape):
# face features number
print(i)
print(x)
print(y)
for (i, (x, y)) in enumerate(shape):
if (i + 1) == 37:
eyeLeftSide = x - 40
if (i + 1) == 38:
eyeTopSide = y - 30
if (i + 1) == 46:
eyeRightSide = x + 40
if (i + 1) == 48:
eyeBottomSide = y + 30
if (i + 1) == 2:
moustacheLeftSide = x
moustacheTopSide = y - 10
if (i + 1) == 16:
moustacheRightSide = x
if (i + 1) == 9:
moustacheBottomSide = y
eyesWidth = eyeRightSide - eyeLeftSide
if eyesWidth < 0:
eyesWidth = eyesWidth * -1
# add glasses
fitedGlass = imutils.resize(glass, width=eyesWidth)
moustacheWidth = moustacheRightSide - moustacheLeftSide
if moustacheWidth < 0:
moustacheWidth = moustacheWidth * -1
# add moustache
fitedMoustache = imutils.resize(moustache, width=moustacheWidth)
key = cv2.waitKey(1) & 0xFF
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break
# show the frame
cv2.imshow("Face Mask", frame)
# do a bit of cleanup
cv2.destroyAllWindows()
vs.stop()