Introduction
In this tutorial we will learn how to use dlib and Python to perform face detection in images.
If you are looking for a similar tutorial using OpenCV, please check here. For an introductory tutorial on how to install dlib and how to read and display an image, please go here.
The code was tested using dlib version 19.22.0 and Python version 3.7.2, on Windows 8.1.
The code
We will start by importing the dlib module.
After that we will call the load_rgb_image function to load an image from the file system. This function receives as input the path to the image, as a string, and returns a numpy ndarray. We will store the result in a variable.
img = dlib.load_rgb_image("C:/Users/N/Desktop/Test.jpg")
After this we will create an object of class image_window, where we will be able to display our original image and also the rectangle around the detected face. As input of the constructor we will pass our previously loaded image, which will be displayed on the window.
Note that the image_window class also has a parameterless constructor that allows to initialize the window without any image. This allows us to set the image later, with a call to the set_image method.
win = dlib.image_window(img)
Then we will call the get_frontal_face_detector function from the dlib module. This will return to us an object of class fhog_object_detector (the default detector from dlib), which we can use to detect faces.
detector = dlib.get_frontal_face_detector()
Instances of this class are callable (check more about callables in Python here and the definition of __call__ for this class here). In our case, to perform the face detection, we simply need to call the instance and pass as input the image where we want to detect faces. As output, it will return an array of objects of class rectangle (it will be empty in case no face is found).
face = detector(img)
Now that we have our array of rectangles, we can simply call the add_overlay method on or image_window, passing as input the rectangles. The rectangles will be drawn on the window, over the image we have previously loaded.
win.add_overlay(face)
To finalize, we will call the wait_until_closed method on our image_window object, to ensure the window stays opened until we close it.
win.wait_until_closed()
The complete code can be seen below.
import dlib
img = dlib.load_rgb_image("C:/Users/N/Desktop/Test.jpg")
win = dlib.image_window(img)
detector = dlib.get_frontal_face_detector()
face = detector(img)
win.add_overlay(face)
win.wait_until_closed()
To test the code, simply run it using a tool of your choice. In my case, I’m using PyCharm, a Python IDE. Before running the code, don’t forget to change the string passed as input of the load_rgb_image to point to an image in your file system.
Upon running the code, you should obtain a result similar to figure 1. As can be seen, the face is correctly detected in the image and the overlaying rectangle is rendered, as expected.
Upsampling the image
In this section we are going to test our code against a slightly more complex use case, where the image containing the faces is smaller. Note that the code we are going to use is exactly the same, with exception to an additional parameter that we will pass to our detector.
Like covered in the previous section, the fhog_object_detector instance we obtain is callable and we should pass as input the image where we want to detect faces. Besides the image, we can pass an optional parameter called upsample_num_times that we omitted in the previous section.
The upsample_num_times argument defaults to zero but, when specified, the detector will upsample the image N times before applying the face detection. The logic applied under the hood can be seen here but, in short, it calls the pyramid_up function (if we check the face detection library example in C++, this pyramid_up function is actually called in the application code).
Naturally, doing the upsampling of the image will make the detector run slower due to the fact that it will need to process a bigger image.
In our case, we are going to pass a value of 1 for the number of times to upsample the image.
import dlib
img = dlib.load_rgb_image("C:/Users/N/Desktop/Test.jpg")
win = dlib.image_window(img)
detector = dlib.get_frontal_face_detector()
face = detector(img, 1)
win.add_overlay(face)
win.wait_until_closed()
Once again, simply run the code pointing to an image in your file system. In my case, I’m running it against an image where none of the faces was detected without upsampling, but are detected when the image is upsampled by a factor of 1.
The result is shown in figure 2. It also shows that the program works as expected when there are multiple faces in the image.
Additional resources
- dlib GitHub page
- dlib face detection tutorial (Python, C++)