Last active
January 21, 2020 16:13
-
-
Save hammertoe/5791e6789a3bb73c63fbf2d3235bd2ab to your computer and use it in GitHub Desktop.
align_faces.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# USAGE | |
# python align_faces.py --shape-predictor shape_predictor_68_face_landmarks.dat input/example_*.jpg | |
# import the necessary packages | |
from imutils.face_utils import FaceAligner | |
from imutils.face_utils import rect_to_bb | |
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", "--images", required=True, nargs='+', | |
help="input images") | |
args = vars(ap.parse_args()) | |
# initialize dlib's face detector (HOG-based) and then create | |
# the facial landmark predictor and the face aligner | |
detector = dlib.get_frontal_face_detector() | |
predictor = dlib.shape_predictor(args["shape_predictor"]) | |
fa = FaceAligner(predictor, desiredFaceWidth=256) | |
for image in args['images']: | |
print("processing:", image) | |
# load the input image, resize it, and convert it to grayscale | |
image = cv2.imread(image) | |
image = imutils.resize(image, width=800) | |
# pad the border, in case the face is right on the edge of the image | |
image = cv2.copyMakeBorder(image, 200, 200, 200, 200, cv2.BORDER_CONSTANT) | |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) | |
rects = detector(gray, 2) | |
# loop over the face detections | |
for rect in rects: | |
# extract the ROI of the *original* face, then align the face | |
# using facial landmarks | |
(x, y, w, h) = rect_to_bb(rect) | |
faceOrig = imutils.resize(image[y:y + h, x:x + w], width=256) | |
faceAligned = fa.align(image, gray, rect) | |
import uuid | |
f = str(uuid.uuid4()) | |
cv2.imwrite("aligned/" + f + ".png", faceAligned) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment