mirror of
https://github.com/deepfakes/faceswap
synced 2025-06-08 03:26:47 -04:00
* Preparing GAN plugin * Adding multithreading for extract * Adding support for mmod human face detector * Adding face filter argument * Added process number argument to multiprocessing extractor. Fixed progressbar for multiprocessing. * Added tiff as image type. compression artefacts hurt my feelings. * Cleanup
34 lines
1.2 KiB
Python
34 lines
1.2 KiB
Python
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, x, w, y, h, landmarks):
|
|
self.image = image
|
|
self.x = x
|
|
self.w = w
|
|
self.y = y
|
|
self.h = h
|
|
self.landmarks = landmarks
|
|
|
|
def landmarksAsXY(self):
|
|
return [(p.x, p.y) for p in self.landmarks.parts()]
|