12/08/2018, 16:05

Facial Recognition System: Face Alignment

In my last post I've shown how to identify the face from the given image or video. In this post we are going to talk about "Face Alignment" which is a normalization technique, often used to improve the accuracy of face recognition algorithms, including deep learning models. A process of facial ...

In my last post I've shown how to identify the face from the given image or video. In this post we are going to talk about "Face Alignment" which is a normalization technique, often used to improve the accuracy of face recognition algorithms, including deep learning models.

A process of facial alignment is to transform all faces in database to be:

  • centered in the image.
  • rotated that such the eyes lie on a horizontal line (i.e., the face is rotated such that the eyes lie along the same y-coordinates).
  • scaled such that the size of the faces are approximately identical.

We gonna use function in imutils package to align the face before we save it.

Let's code

Because some code you may already know it since I wrote it in my previous post , so I'm going talk some new lines of code only.

we required use to pass the name of user when they run programm

ap.add_argument("-s", "--save", type=str, default="",
    help="folder name where use for store user images", required=True)

we init face aligner.

predictor = dlib.shape_predictor("model/shape_predictor_68_face_landmarks.dat")
fa = FaceAligner(predictor, desiredFaceWidth=256)

we init some variable. we check directory already exist if not we create one. so the for should have structure like this

images
   user_name
       0.png
       1.png
       ......
count_image = 0
save_file_name = args["save"]
base_dir = "images/"

if save_file_name:
    base_dir = base_dir + save_file_name + "/"
    if not os.path.exists(base_dir):
        os.makedirs(base_dir)

During programm running, we check if user press key s and face image been detected , we will save the face image in created directory. In each folder maximux faces are 10.

   # if the `s` key was pressed save the first found face
    if key == ord('s'):
        if len(rects) > 0:
            faceAligned = fa.align(frame, gray_frame, rects[0])
            image_name = base_dir + str(count_image) + ".png"
            # save image
            cv2.imwrite(image_name, faceAligned)
            # show image
            cv2.imshow(image_name, faceAligned)
            count_image += 1
            if count_image > 10:
                count_image = 0

Here the full source code:

from imutils.video import VideoStream
from imutils import face_utils
from imutils.face_utils import FaceAligner
import imutils
import argparse
import time
import cv2
import dlib
import os

ap = argparse.ArgumentParser()
ap.add_argument("-s", "--save", type=str, default="",
    help="folder name where use for store user images", required=True)
args = vars(ap.parse_args())

print("[INFO] loading facial landmark predictor...")
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("model/shape_predictor_68_face_landmarks.dat")
fa = FaceAligner(predictor, desiredFaceWidth=256)

print("[INFO] camera sensor warming up...")
vs = VideoStream().start()
time.sleep(2.0)
count_image = 0
save_file_name = args["save"]
base_dir = "images/"

if save_file_name:
    base_dir = base_dir + save_file_name + "/"
    if not os.path.exists(base_dir):
        os.makedirs(base_dir)
# loop over the frames from the video stream
while True:
    # grab the frame from the threaded video stream, resize it to
    # have a maximum awidth of 600 pixels, and convert it to
    # grayscale
    frame = vs.read()
    frame = imutils.resize(frame, awidth=600)
    height, awidth = frame.shape[:2]
    gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
     # detect faces in the grayayscale frame
    rects = detector(gray_frame, 0)


    key = cv2.waitKey(1) & 0xFF
     # if the `q` key was pressed, break from the loop
    if key == ord("q"):
        break

    # if the `s` key was pressed save the first found face
    if key == ord('s'):
        if len(rects) > 0:
            faceAligned = fa.align(frame, gray_frame, rects[0])
            image_name = base_dir + str(count_image) + ".png"
            # save image
            cv2.imwrite(image_name, faceAligned)
            # show image
            cv2.imshow(image_name, faceAligned)
            count_image += 1
            if count_image > 10:
                count_image = 0

    # loopop over the face detections
    for rect in rects:
        (x,y,w,h) = face_utils.rect_to_bb(rect)
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0,0,255), 1)

    # show the frame
    cv2.imshow("Frame", frame)

# do a bit of cleanup
cv2.destroyAllWindows()
vs.stop()

Let's run it.

python save_face_from_video.py -s rathanak

And here the result:

Resources

  • source code
  • http://www.pyimagesearch.com/2017/05/22/face-alignment-with-opencv-and-python/
  • https://github.com/davisking/dlib/blob/master/python_examples/face_recognition.py
  • https://github.com/jrosebr1/imutils

Next Step

In this post we talk about applying facial alignment with dlib, OpenCV and Python which is essential for improving the accuracy of face recognition algorithms, including deep learning models. Next , we'll use those faces to train our marchine and using the train model to recognize the new given face image.

0