face_alignment.py 2.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/python
# The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
#
#   This example shows how to use dlib's face recognition tool for image alignment.
#
# COMPILING/INSTALLING THE DLIB PYTHON INTERFACE
#   You can install dlib using the command:
#       pip install dlib
#
#   Alternatively, if you want to compile dlib yourself then go into the dlib
#   root folder and run:
#       python setup.py install
#   or
#       python setup.py install --yes USE_AVX_INSTRUCTIONS
#   if you have a CPU that supports AVX instructions, since this makes some
#   things run faster.  This code will also use CUDA if you have CUDA and cuDNN
#   installed.
#
#   Compiling dlib should work on any operating system so long as you have
20
21
22
#   CMake installed.  On Ubuntu, this can be done easily by running the
#   command:
#       sudo apt-get install cmake
23
#
24
#   Also note that this example requires Numpy which can be installed
25
#   via the command:
26
#       pip install numpy
27
28

import sys
Varun Chatterji's avatar
Varun Chatterji committed
29

30
31
import dlib

Varun Chatterji's avatar
Varun Chatterji committed
32
if len(sys.argv) != 3:
33
34
    print(
        "Call this program like this:\n"
Varun Chatterji's avatar
Varun Chatterji committed
35
36
37
        "   ./face_alignment.py shape_predictor_5_face_landmarks.dat ../examples/faces/bald_guys.jpg\n"
        "You can download a trained facial shape predictor from:\n"
        "    http://dlib.net/files/shape_predictor_5_face_landmarks.dat.bz2\n")
38
39
40
    exit()

predictor_path = sys.argv[1]
Varun Chatterji's avatar
Varun Chatterji committed
41
face_file_path = sys.argv[2]
42
43

# Load all the models we need: a detector to find the faces, a shape predictor
Varun Chatterji's avatar
Varun Chatterji committed
44
# to find face landmarks so we can precisely localize the face
45
46
47
detector = dlib.get_frontal_face_detector()
sp = dlib.shape_predictor(predictor_path)

48
49
# Load the image using Dlib
img = dlib.load_rgb_image(face_file_path)
50
51
52
53
54
55
56
57
58
59
60

# Ask the detector to find the bounding boxes of each face. The 1 in the
# second argument indicates that we should upsample the image 1 time. This
# will make everything bigger and allow us to detect more faces.
dets = detector(img, 1)

num_faces = len(dets)
if num_faces == 0:
    print("Sorry, there were no faces found in '{}'".format(face_file_path))
    exit()

61
# Find the 5 face landmarks we need to do the alignment.
62
63
64
65
faces = dlib.full_object_detections()
for detection in dets:
    faces.append(sp(img, detection))

66
67
window = dlib.image_window()

68
69
70
71
72
# Get the aligned face images
# Optionally: 
# images = dlib.get_face_chips(img, faces, size=160, padding=0.25)
images = dlib.get_face_chips(img, faces, size=320)
for image in images:
73
74
    window.set_image(image)
    dlib.hit_enter_to_continue()
75
76
77

# It is also possible to get a single chip
image = dlib.get_face_chip(img, faces[0])
78
79
window.set_image(image)
dlib.hit_enter_to_continue()