Face mask detection with deep learning

As we all know the current pandemic situation of the world due to covid19.due to corona virus no one can go outside of the house without mask. As per World Health Organization(WHO) wearing mask is a way to prevent coronavirus transmission. For that I am making one deep learning model which will detect whether the person has worn a mask or not.

what is deep learning?

Deep learning is part of a broader family of machine learning methods based on artificial neural networks with representation learning.

Deep learning


The basic purpose of this model is to detect whether the person has worn a mask or not.

For this currently I have done up to face detection and I have learnt different libraries related to my project. I have also made a dataset to train the model. and I am working on that model.


First of all I made the dataset which contains with mask and without mask images. After that I will train deep learning model based on that dataset. With the help of OpenCV I will detect face from video frame. I will test faces in that model. Based on that test the deep learning model will predict that whether the person has worn a mask or not. The work will be done in 5 phases.

  1. Installing dependencies
  2. The dataset
  3. Data preprocessing
  4. Training the model
  5. Run the model in real time video

Phase 1: Installing dependencies

For this project I am using different libraries

  1. Numpy
  2. OpenCV
  3. Matplotlib
importing the libraries

If user hasn’t imported the libraries he has to install the libraries first.

  1. download pip package

Run python get-pip.py This will install or upgrade pip.

2. Installing the libraries

Run pip install OpenCV-python This will install OpenCV library.

Run pip install Numpy This will install Numpy library.

Run pip install Matplotlib This will install Matplotlib library.

Phase 2 The Dataset:

Let’s take a look at the dataset I’ll be using to train our model. For these I have collected different photos of people with mask and without mask. I have further divided the photos into two different folders named as ‘with mask’ and ‘without mask’.


This dataset is consist of 3830 images belonging two classes.

  • with mask: 1915 images
  • without mask : 1915 images

I have collected this images from kaggle, few open source image libraries and google.

Phase 3 Data Preprocessing:

I have converted all images of both folder with mask and without mask into arrays. With this arrays I will create the deep learning model. I have created two list. First list contains the images and second list contains the labels of ‘with mask’ or ‘without mask’ corresponding to the first list. With the help of os.listdir(), load_img() and img_to_array() methods I have converted images to array.

Phase 4: Training the model:

For training purpose, I have used Mobile nets Convolution neural network. Using mobile nets, I have created two models. The first model will be mobile net model whose output will be passed in normal model. It can be called as head model and base model. By using ImageDataGenerator I have created more images from one image, this will be used for training and testing the model. With use of MobileNetV2() the base model will be created. After the output of base model will be passed in head model. With help of model.predict() method the evaluation of model will be done.

Phase 5: Use model in real time video:

With help of face detector files, the face will be detected and with the help of model prediction will be done that whether a person has worn a mask or not. For camera operations OpenCV library will be used. First the model will be loaded. With the use of VideoStream() the camera will be open. With the use of some methods in OpenCV the face will be detected. And with help of the model file prediction will be done that whether the person has worn a mask or not.


Let’s look at the libraries which I have learnt.

  1. OpenCv

OpenCV is a library of programming functions mainly aimed at real-time computer vision. I used a cascade classifier of OpenCV for face detection. To use this cascade classifier we need the haarcascade_frontalface_default.xml file which includes all the haar cascade features of a face.

Face detection using OpenCV

There are some methods in OpenCV to take Images. using videoframe of OpenCV I’m taking frames and from that, I’m extracting only face images using cascade classifier.

2. Matplotlib

Matplotlib is python package used for 2D graphics. Pyplot is a Matplotlib module which provides a MATLAB-like interface. It is used for data visualization. We can use it to plot different types of graph like Bar graph, Histograms, Scatter plot, Pie plot etc.

Bar plot

3. Numpy

NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.

Use of Numpy

Now , Concluding I would like to thank my teachers who helped me guiding me for the project. First of all, I would like to thank my counsellor Prof. Mikin Patel for giving special time for giving me guidance. Now I would like to thank my Head of Department (Smt Kundanben Dinsha Patel Department of Information Technology), Dr. Parth Shah Chandu Bhai S Patel Institute of Technology, Changa, Anand Gujarat.

Here is the link to my Github repository for this project.

“All our dreams can come true if we have the courage to pursue them.” — Walt Disney



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store