2017-12-08 22:59:27 +08:00
|
|
|
#!/usr/bin/python
|
|
|
|
# The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
|
|
|
|
#
|
2017-12-08 23:16:08 +08:00
|
|
|
# This example shows how faces were jittered and augmented to create training
|
|
|
|
# data for dlib's face recognition model. It takes an input image and
|
|
|
|
# disturbs the colors as well as applies random translations, rotations, and
|
|
|
|
# scaling.
|
|
|
|
|
2017-12-08 22:59:27 +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
|
2017-12-08 22:59:27 +08:00
|
|
|
#
|
2018-04-18 10:49:25 +08:00
|
|
|
# Also note that this example requires Numpy which can be installed
|
2017-12-08 22:59:27 +08:00
|
|
|
# via the command:
|
2018-04-18 10:49:25 +08:00
|
|
|
# pip install numpy
|
2017-12-08 22:59:27 +08:00
|
|
|
#
|
|
|
|
# The image file used in this example is in the public domain:
|
|
|
|
# https://commons.wikimedia.org/wiki/File:Tom_Cruise_avp_2014_4.jpg
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import dlib
|
|
|
|
|
2018-04-18 10:49:25 +08:00
|
|
|
def show_jittered_images(window, jittered_images):
|
2017-12-08 22:59:27 +08:00
|
|
|
'''
|
|
|
|
Shows the specified jittered images one by one
|
|
|
|
'''
|
|
|
|
for img in jittered_images:
|
2018-04-18 10:49:25 +08:00
|
|
|
window.set_image(img)
|
|
|
|
dlib.hit_enter_to_continue()
|
2017-12-08 22:59:27 +08:00
|
|
|
|
|
|
|
if len(sys.argv) != 2:
|
|
|
|
print(
|
|
|
|
"Call this program like this:\n"
|
|
|
|
" ./face_jitter.py shape_predictor_5_face_landmarks.dat\n"
|
|
|
|
"You can download a trained facial shape predictor from:\n"
|
|
|
|
" http://dlib.net/files/shape_predictor_5_face_landmarks.dat.bz2\n")
|
|
|
|
exit()
|
|
|
|
|
|
|
|
predictor_path = sys.argv[1]
|
|
|
|
face_file_path = "../examples/faces/Tom_Cruise_avp_2014_4.jpg"
|
|
|
|
|
|
|
|
# Load all the models we need: a detector to find the faces, a shape predictor
|
|
|
|
# to find face landmarks so we can precisely localize the face
|
|
|
|
detector = dlib.get_frontal_face_detector()
|
|
|
|
sp = dlib.shape_predictor(predictor_path)
|
|
|
|
|
2018-04-18 10:49:25 +08:00
|
|
|
# Load the image using dlib
|
|
|
|
img = dlib.load_rgb_image(face_file_path)
|
2017-12-08 22:59:27 +08:00
|
|
|
|
|
|
|
# Ask the detector to find the bounding boxes of each face.
|
|
|
|
dets = detector(img)
|
|
|
|
|
|
|
|
num_faces = len(dets)
|
|
|
|
|
|
|
|
# Find the 5 face landmarks we need to do the alignment.
|
|
|
|
faces = dlib.full_object_detections()
|
|
|
|
for detection in dets:
|
|
|
|
faces.append(sp(img, detection))
|
|
|
|
|
|
|
|
# Get the aligned face image and show it
|
|
|
|
image = dlib.get_face_chip(img, faces[0], size=320)
|
2018-04-18 10:49:25 +08:00
|
|
|
window = dlib.image_window()
|
|
|
|
window.set_image(image)
|
|
|
|
dlib.hit_enter_to_continue()
|
2017-12-08 22:59:27 +08:00
|
|
|
|
|
|
|
# Show 5 jittered images without data augmentation
|
2017-12-08 23:16:08 +08:00
|
|
|
jittered_images = dlib.jitter_image(image, num_jitters=5)
|
2018-04-18 10:49:25 +08:00
|
|
|
show_jittered_images(window, jittered_images)
|
2017-12-08 22:59:27 +08:00
|
|
|
|
|
|
|
# Show 5 jittered images with data augmentation
|
2017-12-08 23:16:08 +08:00
|
|
|
jittered_images = dlib.jitter_image(image, num_jitters=5, disturb_colors=True)
|
2018-04-18 10:49:25 +08:00
|
|
|
show_jittered_images(window, jittered_images)
|