Introduction#
Working with video datasets, particularly with respect to detection of AI-based fake objects, is very challenging due to proper frame selection and face detection. To approach this challenge from R, one can make use of capabilities offered by OpenCV, magick, and keras.
Our approach consists of the following consequent steps:
- read all the videos
- capture and extract images from the videos
- detect faces from the extracted images
- crop the faces
- build an image classification model with Keras
Let’s quickly introduce the non-deep-learning libraries we’re using. OpenCV is a computer vision library that includes:
- Facial recognition technology
- Motion tracking
- Augmented reality
- and more.
On the other hand, magick is the open-source image-processing library that will help to read and extract useful features from video datasets:
- Read video files
- Extract images per second from the video
- Crop the faces from the images
Before we go into a detailed explanation, readers should know that there is no need to copy-paste code chunks. Because at the end of the post one can find a link to Google Colab with GPU acceleration. This kernel allows everyone to run and reproduce the same results.
Data exploration#
The dataset that we are going to analyze is provided by AWS, Facebook, Microsoft, the Partnership on AI’s Media Integrity Steering Committee, and various academics.
It contains both real and AI-generated fake videos. The total size is over 470 GB. However, the sample 4 GB dataset is separately available.
Frame extraction#
The videos in the folders are in the format of mp4 and have various lengths. Our task is to determine the number of images to capture per second of a video. We usually took 1-3 fps for every video.
Note: Set fps to NULL if you want to extract all frames.
|
|
We saw just the first frame. What about the rest of them?
Looking at the gif one can observe that some fakes are very easy to differentiate, but a small fraction looks pretty realistic. This is another challenge during data preparation.
Face detection#
At first, face locations need to be determined via bounding boxes, using OpenCV. Then, magick is used to automatically extract them from all images.
|
|
Face extraction#
If face locations are found, then it is very easy to extract them all.
|
|
Deep learning model#
After dataset preparation, it is time to build a deep learning model with Keras. We can quickly place all the images into folders and, using image generators, feed faces to a pre-trained Keras model.
|
|
Conclusion#
This post shows how to do video classification from R. The steps were:
- Read videos and extract images from the dataset
- Apply OpenCV to detect faces
- Extract faces via bounding boxes
- Build a deep learning model
However, readers should know that the implementation of the following steps may drastically improve model performance:
- extract all of the frames from the video files
- load different pre-trained weights, or use different pre-trained models
- use another technology to detect faces – e.g., “MTCNN face detector”
Feel free to try these options on the Deepfake detection challenge and share your results in the comments section!
Thanks for reading!