Drawing Face Filter

Let’s learn how to apply a face filter on a digital face image.

Introduction

Some social media apps like Snapchat have gained unprecedented popularity because of their unique and creative features. One of these innovative features is face filters. Nowadays, more and more teenagers are extensively using face filters for fun, and to make their pictures look different.

This face filter technique, continuously evolving, makes use of artificial intelligence and computer vision to detect a human face and its underlying features. Then, the AI can overlay a gallery of filters on the face—an entertaining and amusing gimmick.

Objective

This lesson will walk you through how to add a face filter to a preexisting photo. It will also illustrate the steps needed to develop a lightweight Python utility that allows us to drastically distort the faces in a digital image by overlaying the face of the Joker (a DC comics villain) over the localized faces.

Dependencies

We’ll be using the following Python libraries:

Library

Version

mediapipe

0.8.9

opencv-python

4.4.0.46

numpy

1.19.4

filetype

1.0.7

Let’s code the utility

Let’s take a look at the core functions of this utility.

The read_filter_img function

This function is used to load the face filter image and collect the coordinates of its respective landmark points. Let’s go through this function.

  • Line 5 loads the face filter image specified by the preset global parameter FILTER_IMG.

  • Line 6 collects the image dimensions.

  • Line 7 converts the image from the BGR to the RGB format. It’s worth noting that OpenCV loads images in BGR format.

  • Line 10 grabs the landmarks relative to the face filter image with the help of the MediaPipe FaceMesh ...