Python Project - Real-Time Face Mask Detector with Python, OpenCV, Keras
During pandemic COVID-19, WHO has made wearing masks compulsory to protect against this deadly virus. In this tutorial we will develop a machine learning project – Real-time Face Mask Detector with Python.
We will build a real-time system to detect whether the person on the webcam is wearing a mask or not. We will train the face mask detector model using Keras and OpenCV.
The dataset we are working on consists of 1376 images with 690 images containing images of people wearing masks and 686 images with people without masks.
Download the dataset: Face Mask Dataset
Before proceeding ahead, please download the project source code: Face Mask Detector Project
In this machine learning project for beginners, we will use Jupyter Notebook for the development. Let’s see steps for the installation and configuration of Jupyter Notebook.
Using pip python package manager you can install Jupyter notebook:
And that’s it, you have installed jupyter notebook
After installing Jupyter notebook you can run the notebook server. To run the notebook, open terminal and type:
It will start the notebook server at http://localhost:8888
To create a new project click on the “new” tab on the right panel, it will generate a new .ipynb file.
Create a new file and write the code which you have downloaded
We are going to build this project in two parts. In the first part, we will write a python script using Keras to train face mask detector model. In the second part, we test the results in a real-time webcam using OpenCV.
Make a python file train.py to write the code for training the neural network on our dataset. Follow the steps:
1. Imports:
Import all the libraries and modules required.
2. Build the neural network:
This convolution network consists of two pairs of Conv and MaxPool layers to extract features from the dataset. Which is then followed by a Flatten and Dropout layer to convert the data in 1D and ensure overfitting.
And then two Dense layers for classification.
3. Image Data Generation/Augmentation:
4. Initialize a callback checkpoint to keep saving best model after each epoch while training:
5. Train the model:
Now we will test the results of face mask detector model using OpenCV.
Make a python file “test.py” and paste the below script.
Run the project and observe the model performance.
In this project, we have developed a deep learning model for face mask detection using Python, Keras, and OpenCV. We developed the face mask detector model for detecting whether person is wearing a mask or not. We have trained the model using Keras with network architecture. Training the model is the first part of this project and testing using webcam using OpenCV is the second part.
This is a nice project for beginners to implement their learnings and gain expertise.