Train a CNN to recognize and blur your face from a video with PyTorch and OpenCV

Rosa Gradilla
3 min readAug 10, 2020

--

This is the third post of my series for Face Detection with Pytorch and OpenCV. This story is following my previous post: How to build a Face Detection application using PyTorch and OpenCV. In this post we will be adding a CNN classifier and training it to recognize your face (or anyone else of your choice).

  1. Collect the Data

The first thing you need to do is collect the data. The data will consist of two simple categories: you vs not-you. You will need about the same number of images of your face and of other people. I had about 300 images of my face and 300 images of other people, but the more you can collect the better.

Once you get your images you need to split them into training and validation sets. In your Dataset folder create two folders: train and val. Inside each folder have a folder for each category:

Directory tree structure for Dataset

Make sure your dataset images are cropped to only the face. You can use MTCNN to detect the face and save the bounding box only. If you would like some help doing this, I have a video in which I explain how I take the frames of a video, collect the bounding box of my face and save them as images to build the dataset.

2. Prepare the Data

To train the CNN we will be using transfer learning. Transfer learning is when we use a CNN that has been trained for something else and we freeze all the weights except for the final fully connected layer. This last layer is replaced with random weights and only this layer is trained. Freezing all the other weights is helpful because since this CNN has already been trained, we can trust these weights are capable of extracting meaningful features.

The following is a script to label our data and export it as npy files that we will later use in a Google Colab notebook to train.

Make sure you change the PATH for your dataset directory in line 30. This script will output 2 npy files: train_data.npy and val_data.npy. We will use these files in a Colab Google notebook to train. This is very helpful if you are like me and don’t have access to a GPU.

3. Train the classifier

In order to train our classifier we will use Google Colab. First add the npy files to your google drive, we will get access to these files by mounting to google drive. You can follow this notebook just make sure you mount to your own drive and change the path if necessary.

I have a video going over the code and what each cell does. Please watch that if you are interested.

4. Incorporate the classifier to the Face Detector

Now that we have trained the classifier we will incorporate it to our Face Detector by adding class methods to our FaceDetector class. You can find the code for the FaceDetector class in my previous story. We will add a method to blur the face out of the frame (_blur_face()), a method to run the classifier and determine wether it is your face or not (_is_it_Rosa()), and we will also incorporate some lines in the _run() method to use the classifier. The new FaceDetector class with all the new methods is the following:

I have a video going over the script here.

Now we can run the app from a different script by importing all the necessary scripts:

and you guessed it, there’s also a video

Run your script through the terminal by typing python app.py when inside the folder in which you have your app.py script and look at your masterpiece!

Note: you can also turn the blurring feature off by running the script as: python app.py — blur False.

Thank you for following until the end!

--

--

Rosa Gradilla

Data Science graduate student interested in deep learning and computer vision