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 find frontal human faces in an image and
|
|
|
|
# estimate their pose. The pose takes the form of 68 landmarks. These are
|
|
|
|
# points on the face such as the corners of the mouth, along the eyebrows, on
|
|
|
|
# the eyes, and so forth.
|
|
|
|
#
|
2017-09-11 10:16:54 +08:00
|
|
|
# The face detector we use is made using the classic Histogram of Oriented
|
2014-12-11 23:01:08 +08:00
|
|
|
# Gradients (HOG) feature combined with a linear classifier, an image pyramid,
|
|
|
|
# and sliding window detection scheme. The pose estimator was created by
|
|
|
|
# using dlib's implementation of the paper:
|
|
|
|
# One Millisecond Face Alignment with an Ensemble of Regression Trees by
|
|
|
|
# Vahid Kazemi and Josephine Sullivan, CVPR 2014
|
2017-09-11 10:16:54 +08:00
|
|
|
# and was trained on the iBUG 300-W face landmark dataset (see
|
|
|
|
# https://ibug.doc.ic.ac.uk/resources/facial-point-annotations/):
|
|
|
|
# C. Sagonas, E. Antonakos, G, Tzimiropoulos, S. Zafeiriou, M. Pantic.
|
|
|
|
# 300 faces In-the-wild challenge: Database and results.
|
|
|
|
# Image and Vision Computing (IMAVIS), Special Issue on Facial Landmark Localisation "In-The-Wild". 2016.
|
|
|
|
# You can get the trained model file from:
|
|
|
|
# http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2.
|
|
|
|
# Note that the license for the iBUG 300-W dataset excludes commercial use.
|
|
|
|
# So you should contact Imperial College London to find out if it's OK for
|
2017-09-16 07:57:33 +08:00
|
|
|
# you to use this model file in a commercial product.
|
2017-09-11 10:16:54 +08:00
|
|
|
#
|
2014-12-11 23:01:08 +08:00
|
|
|
#
|
|
|
|
# Also, note that you can train your own models using dlib's machine learning
|
|
|
|
# tools. See train_shape_predictor.py to see an example.
|
|
|
|
#
|
|
|
|
#
|
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
|
|
|
|
#
|
|
|
|
# Compiling dlib should work on any operating system so long as you have
|
2018-01-23 08:23:01 +08:00
|
|
|
# CMake installed. On Ubuntu, this can be done easily by running the
|
|
|
|
# command:
|
|
|
|
# sudo apt-get install cmake
|
2015-03-08 03:14:47 +08:00
|
|
|
#
|
2018-04-18 10:49:25 +08:00
|
|
|
# Also note that this example requires Numpy which can be installed
|
2015-03-08 03:14:47 +08:00
|
|
|
# via the command:
|
2018-04-18 10:49:25 +08:00
|
|
|
# pip install numpy
|
2015-03-08 03:14:47 +08:00
|
|
|
|
2014-12-11 23:01:08 +08:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import dlib
|
|
|
|
import glob
|
|
|
|
|
|
|
|
if len(sys.argv) != 3:
|
|
|
|
print(
|
|
|
|
"Give the path to the trained shape predictor model as the first "
|
|
|
|
"argument and then the directory containing the facial images.\n"
|
|
|
|
"For example, if you are in the python_examples folder then "
|
|
|
|
"execute this program by running:\n"
|
|
|
|
" ./face_landmark_detection.py shape_predictor_68_face_landmarks.dat ../examples/faces\n"
|
|
|
|
"You can download a trained facial shape predictor from:\n"
|
2016-06-26 02:00:38 +08:00
|
|
|
" http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2")
|
2014-12-11 23:01:08 +08:00
|
|
|
exit()
|
|
|
|
|
|
|
|
predictor_path = sys.argv[1]
|
|
|
|
faces_folder_path = sys.argv[2]
|
|
|
|
|
|
|
|
detector = dlib.get_frontal_face_detector()
|
|
|
|
predictor = dlib.shape_predictor(predictor_path)
|
|
|
|
win = dlib.image_window()
|
|
|
|
|
|
|
|
for f in glob.glob(os.path.join(faces_folder_path, "*.jpg")):
|
|
|
|
print("Processing file: {}".format(f))
|
2018-04-18 10:49:25 +08:00
|
|
|
img = dlib.load_rgb_image(f)
|
2014-12-11 23:01:08 +08:00
|
|
|
|
|
|
|
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()
|