2014-12-11 23:01:08 +08:00
|
|
|
#!/usr/bin/python
|
|
|
|
# The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
|
|
|
|
#
|
|
|
|
# This example program shows how to use dlib's implementation of the paper:
|
|
|
|
# One Millisecond Face Alignment with an Ensemble of Regression Trees by
|
|
|
|
# Vahid Kazemi and Josephine Sullivan, CVPR 2014
|
|
|
|
#
|
|
|
|
# In particular, we will train a face landmarking model based on a small
|
|
|
|
# dataset and then evaluate it. If you want to visualize the output of the
|
|
|
|
# trained model on some images then you can run the
|
2014-12-28 04:30:56 +08:00
|
|
|
# face_landmark_detection.py example program with predictor.dat as the input
|
2014-12-11 23:01:08 +08:00
|
|
|
# model.
|
|
|
|
#
|
|
|
|
# It should also be noted that this kind of model, while often used for face
|
|
|
|
# landmarking, is quite general and can be used for a variety of shape
|
|
|
|
# prediction tasks. But here we demonstrate it only on a simple face
|
|
|
|
# landmarking task.
|
|
|
|
#
|
2015-10-27 20:25:43 +08:00
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# Compiling dlib should work on any operating system so long as you have
|
|
|
|
# CMake and boost-python installed. On Ubuntu, this can be done easily by
|
|
|
|
# running the command:
|
2014-12-11 23:01:08 +08:00
|
|
|
# sudo apt-get install libboost-python-dev cmake
|
2015-03-08 03:14:47 +08:00
|
|
|
#
|
|
|
|
# Also note that this example requires scikit-image which can be installed
|
|
|
|
# via the command:
|
2015-10-27 20:50:44 +08:00
|
|
|
# pip install scikit-image
|
2015-03-08 03:14:47 +08:00
|
|
|
# Or downloaded from http://scikit-image.org/download.html.
|
|
|
|
|
2014-12-11 23:01:08 +08:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import glob
|
|
|
|
|
|
|
|
import dlib
|
|
|
|
from skimage import io
|
|
|
|
|
|
|
|
|
|
|
|
# In this example we are going to train a face detector based on the small
|
|
|
|
# faces dataset in the examples/faces directory. This means you need to supply
|
|
|
|
# the path to this faces folder as a command line argument so we will know
|
|
|
|
# where it is.
|
|
|
|
if len(sys.argv) != 2:
|
|
|
|
print(
|
|
|
|
"Give the path to the examples/faces directory as the argument to this "
|
|
|
|
"program. For example, if you are in the python_examples folder then "
|
|
|
|
"execute this program by running:\n"
|
|
|
|
" ./train_shape_predictor.py ../examples/faces")
|
|
|
|
exit()
|
|
|
|
faces_folder = sys.argv[1]
|
|
|
|
|
|
|
|
options = dlib.shape_predictor_training_options()
|
|
|
|
# Now make the object responsible for training the model.
|
|
|
|
# This algorithm has a bunch of parameters you can mess with. The
|
|
|
|
# documentation for the shape_predictor_trainer explains all of them.
|
2014-12-28 04:30:56 +08:00
|
|
|
# You should also read Kazemi's paper which explains all the parameters
|
2014-12-11 23:01:08 +08:00
|
|
|
# in great detail. However, here I'm just setting three of them
|
|
|
|
# differently than their default values. I'm doing this because we
|
|
|
|
# have a very small dataset. In particular, setting the oversampling
|
|
|
|
# to a high amount (300) effectively boosts the training set size, so
|
|
|
|
# that helps this example.
|
|
|
|
options.oversampling_amount = 300
|
|
|
|
# I'm also reducing the capacity of the model by explicitly increasing
|
|
|
|
# the regularization (making nu smaller) and by using trees with
|
|
|
|
# smaller depths.
|
|
|
|
options.nu = 0.05
|
|
|
|
options.tree_depth = 2
|
|
|
|
options.be_verbose = True
|
|
|
|
|
2014-12-28 04:30:56 +08:00
|
|
|
# dlib.train_shape_predictor() does the actual training. It will save the
|
|
|
|
# final predictor to predictor.dat. The input is an XML file that lists the
|
|
|
|
# images in the training dataset and also contains the positions of the face
|
|
|
|
# parts.
|
2014-12-11 23:01:08 +08:00
|
|
|
training_xml_path = os.path.join(faces_folder, "training_with_face_landmarks.xml")
|
|
|
|
dlib.train_shape_predictor(training_xml_path, "predictor.dat", options)
|
|
|
|
|
2014-12-28 04:30:56 +08:00
|
|
|
# Now that we have a model we can test it. dlib.test_shape_predictor()
|
|
|
|
# measures the average distance between a face landmark output by the
|
|
|
|
# shape_predictor and where it should be according to the truth data.
|
|
|
|
print("\nTraining accuracy: {}".format(
|
2014-12-11 23:01:08 +08:00
|
|
|
dlib.test_shape_predictor(training_xml_path, "predictor.dat")))
|
2014-12-28 04:30:56 +08:00
|
|
|
# The real test is to see how well it does on data it wasn't trained on. We
|
|
|
|
# trained it on a very small dataset so the accuracy is not extremely high, but
|
|
|
|
# it's still doing quite good. Moreover, if you train it on one of the large
|
|
|
|
# face landmarking datasets you will obtain state-of-the-art results, as shown
|
|
|
|
# in the Kazemi paper.
|
|
|
|
testing_xml_path = os.path.join(faces_folder, "testing_with_face_landmarks.xml")
|
2014-12-11 23:01:08 +08:00
|
|
|
print("Testing accuracy: {}".format(
|
|
|
|
dlib.test_shape_predictor(testing_xml_path, "predictor.dat")))
|
|
|
|
|
2014-12-28 07:15:07 +08:00
|
|
|
# Now let's use it as you would in a normal application. First we will load it
|
2014-12-28 04:30:56 +08:00
|
|
|
# from disk. We also need to load a face detector to provide the initial
|
|
|
|
# estimate of the facial location.
|
2014-12-11 23:01:08 +08:00
|
|
|
predictor = dlib.shape_predictor("predictor.dat")
|
2014-12-28 04:30:56 +08:00
|
|
|
detector = dlib.get_frontal_face_detector()
|
2014-12-11 23:01:08 +08:00
|
|
|
|
2014-12-28 04:30:56 +08:00
|
|
|
# Now let's run the detector and shape_predictor over the images in the faces
|
|
|
|
# folder and display the results.
|
2014-12-11 23:01:08 +08:00
|
|
|
print("Showing detections and predictions on the images in the faces folder...")
|
|
|
|
win = dlib.image_window()
|
|
|
|
for f in glob.glob(os.path.join(faces_folder, "*.jpg")):
|
|
|
|
print("Processing file: {}".format(f))
|
|
|
|
img = io.imread(f)
|
|
|
|
|
|
|
|
win.clear_overlay()
|
|
|
|
win.set_image(img)
|
|
|
|
|
2014-12-28 04:30:56 +08:00
|
|
|
# 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.
|
2014-12-11 23:01:08 +08:00
|
|
|
dets = detector(img, 1)
|
|
|
|
print("Number of faces detected: {}".format(len(dets)))
|
|
|
|
for k, d in enumerate(dets):
|
|
|
|
print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
|
|
|
|
k, d.left(), d.top(), d.right(), d.bottom()))
|
2014-12-28 04:30:56 +08:00
|
|
|
# Get the landmarks/parts for the face in box d.
|
|
|
|
shape = predictor(img, d)
|
|
|
|
print("Part 0: {}, Part 1: {} ...".format(shape.part(0),
|
|
|
|
shape.part(1)))
|
|
|
|
# Draw the face landmarks on the screen.
|
|
|
|
win.add_overlay(shape)
|
2014-12-11 23:01:08 +08:00
|
|
|
|
|
|
|
win.add_overlay(dets)
|
2015-03-23 06:45:08 +08:00
|
|
|
dlib.hit_enter_to_continue()
|
2014-12-11 23:01:08 +08:00
|
|
|
|