-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouth.py
43 lines (31 loc) · 1.12 KB
/
mouth.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
#python mouth.py --shape-predictor shape_predictor_68_face_landmarks.dat --image 1.jpg
from imutils import face_utils
import numpy as np
import argparse
import imutils
import dlib
import cv2
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--shape-predictor", required=True,
help="path to facial landmark predictor")
ap.add_argument("-i", "--image", required=True,
help="path to input image")
args = vars(ap.parse_args())
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(args["shape_predictor"])
image = cv2.imread(args["image"])
image = imutils.resize(image, width=500)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# detect faces in the grayscale image
rects = detector(gray, 1)
for (i, name) in enumerate(face_utils.FACIAL_LANDMARKS_IDXS.keys()):
shape = predictor(gray, rects[0])
shape = face_utils.shape_to_np(shape)
(j, k) = face_utils.FACIAL_LANDMARKS_IDXS[name]
pts = shape[j:k]
if name == "mouth":
hull = cv2.convexHull(pts)
cv2.drawContours(image, [hull], -1, (163, 38, 32), -1)
cv2.imshow("Result", image)
cv2.waitKey(0)