-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouth_detection.py
executable file
·50 lines (42 loc) · 1.4 KB
/
mouth_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
# coding=utf-8
import dlib
import cv2
import numpy as np
class MouthDetection(object):
def __init__(self):
self.detector = dlib.get_frontal_face_detector()
self.predictor = dlib.shape_predictor('./model/shape_predictor_68_face_landmarks.dat')
def get_mouth_keypoints(self, image):
pos = np.zeros((3, 2), dtype=np.int8)
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
gray = cv2.equalizeHist(gray)
dets = self.detector(image, 1)
# 有些问题
for index, face in enumerate(dets):
shape = self.predictor(gray, face)
points = shape.parts()
pos[0][0] = points[56].x
pos[0][1] = points[56].y
pos[1][0] = points[57].x
pos[1][1] = points[57].y
pos[2][0] = points[58].x
pos[2][1] = points[58].y
return pos
def crop(self, image, pos):
images = list()
x1 = pos[2][0]
y1 = pos[2][1]
x2 = pos[1][0]
y2 = pos[1][1]
d = abs(x2 - x1)
images.append(image[(int)(y1 - d * 0.75):y2, x1:x2])
x1 = pos[1][0]
y1 = pos[1][1]
x2 = pos[0][0]
y2 = pos[0][1]
d = abs(x1 - x2)
images.append(image[y1 - d:y2, x1:x2])
return images
def get_mouth_images(self, image):
pos = self.get_mouth_keypoints(image.copy())
return self.crop(image, pos)