MaskTheFace — CV based tool to mask face dataset | by Aqeel Anwar | Towards Data Science

Jump to code: https://github.com/aqeelanwar/MaskTheFace
MaskTheFace is a computer vision-based script to mask faces in images. It uses a dlib based face landmarks detector to identify the face tilt and six key features of the face necessary for applying the mask. Based on the face tilt, the corresponding mask template is selected from the library of the mask. The template mask is then transformed based on the six key features to fit perfectly on the face. The complete block diagram can be seen below. MaskTheFace provides several masks to select from. It is difficult to collect mask datasets under various conditions. MaskTheFace can be used to convert any existing face dataset to a masked-face dataset. MaskTheFace identifies all the faces within an image and applies the user-selected masks to them taking into account various limitations such as face angle, mask fit, lighting conditions, etc. A single image or entire directory of images can be used as input to code.
It’s advisable to make a new virtual environment with Python 3.6 and install the dependencies. Following steps can be taken to download get started with MaskTheFace
git clone https://github.com/aqeelanwar/MaskTheFace.git
The provided requirements.txt file can be used to install all the required packages. Use the following command
cd MaskTheFacepip install –r requirements.txt
This will install the required packages in the activated python environment.
cd MaskTheFace# Genericpython mask_the_face.py --path <path-to-file-or-dir> --mask_type <type-of-mask> --verbose --write_original_image# Examplepython mask_the_face.py --path 'data/office.jpg' --mask_type 'N95' --verbose --write_original_image
The — code argument can be used to apply different types of masks uniformly at random to the images in the dataset.
# Generic format--code < masktype1 >-< color as hex or texture file path >, < masktype2 >-< color as hex or texture file path>, ...# Example--code cloth-masks/textures/check/check_4.jpg, cloth-#e54294, cloth-#ff0000, cloth, cloth-masks/textures/others/heart_1.png, cloth-masks/textures/fruits/pineapple.png, N95
The example above will apply 7 different masks, uniform at random, to the selected face dataset (via — path argument).
Currently, MaskTheFace supports the following 4 mask types
SurgicalN95KN95ClothGas
Surgical
N95
KN95
Cloth
Gas
New masks are being added. Users can also add custom masks following the guidelines provided.
Each of the mask types mentioned above can be varied in the following terms to create even more masks
MaskTheFace provides 24 existing patterns that can be applied to mask types above to create more variations of the graph. Moreover, users can easily add custom patterns following the guidelines provided.
MaskTheFace provides a script to modify existing mask types in terms of colors to generate variations of existing graphs.
MaskTheFace provides a script to modify existing mask types in terms of intensity to generate variations of existing graphs.
Masked faces in the real-world for face recognition (MFR2) is a small dataset with 53 identities of celebrities and politicians with a total of 269 images that are collected from the internet. Each identity has an average of 5 images. The dataset contains both masked and unmasked faces of the identities. The dataset is processed in terms of face alignment and image dimensions. Each image has a dimension of (160x160x3). Sample images from the MFR2 data-set and the mask distribution can be seen below.
The dataset can be downloaded using the following command
cd MaskTheFacepython utils/fetch_dataset.py --dataset mfr2
This will download and extract the mfr2 dataset in the datasets folder.
# path to mfr2MaskTheFace/datasets/mfr2
Alternatively, you can download mfr2.zip file from here
The downloaded dataset folder contains
Folders with images of identitiesmfr2_labels.txt: A label text file with identity name, image number, and type of mask ground truthpairs.txt: A text file containing 848 positive and negative pairs to be used for testing.
Folders with images of identities
mfr2_labels.txt: A label text file with identity name, image number, and type of mask ground truth
pairs.txt: A text file containing 848 positive and negative pairs to be used for testing.
MaskTheFace tool can be used to generate a synthetic mask face dataset which can then be used to train various application-oriented ML networks such as face recognition for indoor attendance system, mask detection, mask classification, etc.
If this article was helpful to you, feel free to clap, share and respond to it. If you want to learn more about Machine Learning and Data Science, follow me @Aqeel Anwar or connect with me on LinkedIn.