forked from deepfakes/faceswap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaces_detect.py
38 lines (31 loc) · 1.43 KB
/
faces_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
import dlib
import face_recognition
import face_recognition_models
def detect_faces(frame, model="hog"):
face_locations = face_recognition.face_locations(frame, model=model)
landmarks = _raw_face_landmarks(frame, face_locations)
for ((y, right, bottom, x), landmarks) in zip(face_locations, landmarks):
yield DetectedFace(frame[y: bottom, x: right], x, right - x, y, bottom - y, landmarks)
# Copy/Paste (mostly) from private method in face_recognition
predictor_68_point_model = face_recognition_models.pose_predictor_model_location()
pose_predictor = dlib.shape_predictor(predictor_68_point_model)
def _raw_face_landmarks(face_image, face_locations):
face_locations = [_css_to_rect(face_location) for face_location in face_locations]
return [pose_predictor(face_image, face_location) for face_location in face_locations]
def _css_to_rect(css):
return dlib.rectangle(css[3], css[0], css[1], css[2])
# end of Copy/Paste
class DetectedFace(object):
def __init__(self, image=None, x=None, w=None, y=None, h=None, landmarks=None, landmarksXY=None):
self.image = image
self.x = x
self.w = w
self.y = y
self.h = h
self.landmarks = landmarks
self.landmarksXY = landmarksXY
def landmarksAsXY(self):
if self.landmarksXY:
return self.landmarksXY
self.landmarksXY = [(p.x, p.y) for p in self.landmarks.parts()]
return self.landmarksXY