Extracting faces using OpenCV Face Detection Neural Network

Karan Bhanot
Towards Data Science
4 min readMar 4, 2019

--

Recently, I came across the website https://www.pyimagesearch.com/ which has some of the greatest tutorials on OpenCV. While reading through its numerous articles, I found that OpenCV has its own Face Detection Neural Network with really high accuracy.

So I decided to work on a project using this Neural Network from OpenCV and extract faces from images. Such a process would come handy whenever someone is working with faces and needs to extract them from a number of images.

The complete project is available as a GitHub repository. For this article, I’ve taken a picture from my Instagram account.

Image used for extracting face

Aim

The project has two essential elements:
1. Box around faces: Show white boxes around all the faces recognised in the image. The Python file is data_generator.py
2. Extracted faces: Extract faces from all images in a folder and save each face into a destination folder to create a handy dataset. The Python file is face_extractor.py

Face detection and extraction

First, let’s perform the common steps for the two parts, i.e. importing libraries, loading the face detection model, creating output directory, reading images and detecting faces.

Project

Import libraries

I import os to access various files in the directory. Then, cv2 will be used to work with images. numpy helps to easily work with multi-dimensional arrays.

Define paths and load model

The model’s prototxt and caffemodel is provided in the OpenCV repo itself. I used the same and placed them in the model_data directory in my project. prototxt file includes the text description of the network and caffemodel includes the weights. I read the two files and loaded my model using cv2.

Create directory

If the directory where the resultant images will get stored does not exist, I’ll create the directory. The output folder is updated_images.

When working with extracting faces, I’ll save the faces into the directory faces. If it is not present, I’ll create it.

Read images

I loop through all images inside the images folder. After extracting the extension, I check that the files are either of the type .png or .jpg and just operate with those files only.

Detect faces

Using cv2.imread, I read the image, and create a blob using cv2.dnn.blobFromImage. Then, I input this blob into the model and get back the detections from the page using model.forward().

The common steps are now complete. For the first task, I’ll plot white rectangles around faces and save them in updated_images directory. For the second task, I’ll save the extracted faces in faces directory.

1. Create boxes around faces

One by one, I iterate over all of the faces detected in the image and extract their start and end points. Then, I extract the confidence of detection. If the algorithm is more than 50% confident that the detection is a face, I show a rectangle around it.

Then, using cv2.imwrite, I save the image to the updated_images folder with the same name.

Image with white rectangle around face

2. Extracting faces

As described above, I iterate all faces, calculate the confidence of detection and if it is more than 50%, I extract the face. Notice the line frame = image[startY:endY, startX:endX]. It extracts the face from the image.

Then, I dump this new image into the faces folder with the name as face number, followed by _, and then the name of the file. If we extracted the first face from an image named sampleImage.png, the name of the face file will be 0_sampleImage.png. With each face, I increment the count and after complete execution, I print the count to the console.

Extracted face

Finally, the project is ready. You can feed in as many images as possible and generate datasets which can be used for further projects.

Conclusion

In this article, I discussed using OpenCV Face Detection Neural Network to detect faces in an image, label them with white rectangles and extract faces into separate images.

As always, I’d love to hear about your thoughts and suggestions.

--

--

Data science and Machine learning enthusiast. Technical Writer. Passionate Computer Science Engineer.