/face_mask_detect

Face mask detection including face ROI with 100% accuracy. Build with OpenCV and Keras/TensorFlow.

Primary LanguageJupyter NotebookMIT LicenseMIT

Face Mask Detection

Face Mask Detection system built with OpenCV, Keras, TensorFlow using Convolutional Neural Network approach to detect face masks including Face ROI in static images as well as in real-time video streams.

Python contributions welcome

👇 Support me here!

ko-fi

⚠️ Framework used

⭐ Features

This face mask detection experiment use 3 different datasets with 2 different models architecture. More details in Dataset and Models section respectively. The most accurate model, which has 100% accuracy, is trained using dataset_1 with the MobileNetV2 architecture. Using MobileNetV2 architecture also computationally efficient and thus making it easier to deploy the model to embedded systems (Raspberry Pi, Google Coral, NVidia Jetson, etc.). More comparative detail in Result section.

This system can be used in real-time applications which require face-mask detection. This project can also be integrated with embedded systems for application in airports, railway stations, offices, schools, and public places to ensure that public safety guidelines are followed.

📁 Dataset

There are three different dataset used. All the dataset could can be downloaded directly in this repository on each folder respectively. The images were collected from the following sources:

  • dataset_1 (See The Source) In this first dataset, an image of a person using a mask is deliberately created, or it is called an augmented image. The image of the mask is placed between the nose and the chin matches the position of the person in the image. The dataset consist of 1376 pictures beloging into two classes: with_mask: 690 pictures without_mask: 686 pictures

  • dataset_2 (See The Source) The dataset used original images of people using different types of masks. This dataset consists of a total of 853 images. Unlike the previous dataset, in this dataset one image can be included in both classes because in one image there can be many images of human faces. The labels on each face in the image are stored in an annotation in the xml file. From those xml file information, cropping can be done to save faces with or without masks. From the cropping results, the number of classes with_mask is 3355 images and without_mask is 717 images.

  • dataset_3 (See The Source) This dataset contains about 1006 equally distributed images of 2 distinct types: with_mask: 503 pictures without_mask: 503 pictures

💡 Models

The experiment of this system are using two architecture model:

  1. CNN

    The first model uses a general convolutional layer. Input images in 100x100 size with a grayscale transform. Followed by two Conv2D layer with 200 and 100 filter size respectively. Then using fully connected layer (Dense 64) and (Dense 2). Below is the view of the neural network layer:

layer_cnn

  1. MobileNetV2 CNN

    The second model uses MobileNetV2 as base model. Input image in 224x224 RGB. The output from MobileNetV2 comes into AveragePooling then connected to fully connected layer (Dense 128) and (Dense 2) for a probability beetween with_mask or without_mask.

    layer_cnn_mobilenetv2

🔑 Prerequisites

All the dependencies and required libraries are included in the file requirements.txt See here

🚀  Installation

  1. Clone the repo
git clone https://github.com/nadhirfr/face_mask_detect
  1. Change your directory to the cloned repo
cd face_mask_detect
  1. Now, run the following command in your Terminal/Command Prompt to install the libraries required
pip install -r requirements.txt

💡 Run the Detection

  1. To detect face masks in an image type the following command:
python detect_mask_image.py --image "path/to/image.jpeg" --model "trained_model_1.h5"

To detect mask in an image with MobileNetV2 models use following command:

python detect_mask_image_MobileNetV2.py --image "path/to/image.jpeg" --model "trained_model_1_MobileNetV2.h5"
  1. To detect face masks in real-time video streams type the following command:
python detect_mask_video.py --model "trained_model_1.h5"

To detect face masks in real-time video streams with MobileNetV2 models type the following command:

python detect_mask_video_MobileNetV2.py --model "trained_model_1_MobileNetV2.h5"

Use script and trained models ended with MobileNetV2 respectively to run MobileNetV2 models. The number in trained models name indicates the dataset used, for example: "trained_model_1_MobileNetV2.h5"

this trained model is trained using dataset_1 with MobileNetV2

🔑 Results

We have done with total 6 combination of training, below is the detail:

Model Dataset Training Notebook Trained Model Accuracy
CNN dataset_1 train_mask_detector_1.ipynb Open In Colab trained_model_1.h5 88%
CNN dataset_2 train_mask_detector_2.ipynb Open In Colab trained_model_2.h5 92%
CNN dataset_3 train_mask_detector_3.ipynb Open In Colab trained_model_3.h5 85%
MobileNetV2 CNN dataset_1 train_mask_detector_1_MobileNetV2.ipynb Open In Colab trained_model_1_MobileNetV2.h5 100%
MobileNetV2 CNN dataset_2 train_mask_detector_2_MobileNetV2.ipynb Open In Colab trained_model_2_MobileNetV2.h5 90%
MobileNetV2 CNN dataset_3 train_mask_detector_3_MobileNetV2.ipynb Open In Colab trained_model_3_MobileNetV2.h5 99%

The best model result is used the dataset_1 with the MobileNetV2 CNN which has 100% accuracy. Below is the graph and classification report:

best_result_accuracy_graph

best_result_classification_report

To see the other accuracy graph and classification report kindly open the training notebook in Google Colab.

👏 And it's done!

Feel free to mail me for any doubts/query :email: nadhir.rozam@gmail.com

👍 Credits

🙋 Citation

You are allowed to cite any part of the code or our dataset. You can use it in your Research Work or Project. Remember to provide credit to the Maintainer Nadhir Rozam by mentioning a link to this repository and her GitHub Profile.

Follow this format:

  • Author's name - Nadhir Rozam
  • Date of publication or update in parentheses.
  • Title or description of document.
  • URL.

👀 License

MIT © LICENSE