Skip to content

Proper Mask Wearing Detection and Alarm System based on Deep Learning and trained using Tensorflow/Keras

License

Notifications You must be signed in to change notification settings

achen353/Face-Mask-Detector

Repository files navigation

Proper Mask Wearing Detection and Alarm System

A lightweight face mask detection that is easy to deploy
Trained on Tensorflow/Keras. Deployed using Dash on Google App Engine.



Read this in 繁體中文.

Table of Contents

Features

  • Lightweight models: only 2,422,339 and 2,422,210 parameters for the MFN and RMFD models, respectively
  • Detection of multiple faces: able to detect multiple faces in one frame
  • Support for detection in webcam stream: our app supports detection in images and video streams
  • Support for detection of improper mask wearing: our MFN model is able to detect improper mask wearing including (1) uncovered chin, (2) uncovered nose, and (3) uncovered nose and mouth.

About

This app detects human faces and proper mask wearing in images and webcam streams.

Under the COVID-19 pandemic, wearing mask has shown to be an effective means to control the spread of virus. The demand for an effective mask detection on embedded systems of limited computing capabilities has surged, especially in highly populated areas such as public transportations, hospitals, etc. Trained on MobileNetV2, a state-of-the-art lightweight deep learning model on image classification, the app is computationally efficient to deploy to help control the spread of the disease.

While many work on face mask detection has been developed since the start of the pandemic, few distinguishes whether a mask is worn correctly or incorrectly. Given the discovery of the new coronavirus variant in UK, we aim to provide a more precise detection model to help strengthen enforcement of mask mandate around the world.

Frameworks and Libraries

  • OpenCV: computer vision library used to process images
  • OpenCV DNN Face Detector: Caffe-based Single Shot-Multibox Detector (SSD) model used to detect faces
  • Tensorflow / Keras: deep learning framework used to build and train our models
  • MobileNet V2: lightweight pre-trained model available in Keras Applications; used as a base model for our transfer learning
  • Dash: framework built upon Plotly.js, React and Flask; used built the demo app

Datasets

We provide two models trained on two different datasets. Our RMFD dataset is built from the Real World Masked Face Dataset and the MFN dataset is built from the MaskedFace-Net and Flickr-Faces-HQ Dataset (FFHQ).

RMFD dataset

This dataset consists of 4,408 images:

  • face_no_mask: 2,204 images
  • face_with_mask: 2,204 images

Each image is a cropped real-world face image of unfixed sizes. The face_no_mask data is randomly sampled from the 90,568 no mask data from the Real World Masked Face Dataset and the face_with_mask data entirely provided by the original dataset.

MFN dataset

This dataset consists of 200,627 images:

  • face_with_mask_correctly: 67,193 images
  • face_with_mask_incorrectly: 66,899 images
  • face_no_mask: 66,535 images

The face_with_mask_correctly and face_with_mask_incorrectly classes consist of the resized 128*128 images from the original MaskedFace-Net work without any sampling. The face_no_mask is built from the Flickr-Faces-HQ Dataset (FFHQ) upon which the MaskedFace-Net data was created. All images in MaskedFace-Net are morphed mask-wearing images and face_with_mask_incorrectly consists of 10% uncovered chin, 10% uncovered nose, and 80% uncovered nose and mouth images.

Download

The dataset is now available here! (June 11, 2021)

Training Results

Both models are trained on 80% of their respectively dataset and validated/tested on the other 20%. They both achieved 99% accuracy on their validation data.

MFN Model RMFD Model

However, the MFN model sometimes classifies face_no_mask as face_with_mask_incorrectly. Though this would not affect goal of reminding people to wear mask properly, any suggestion to improve the model is welcomed.

Requirements

This project is built using Python 3.8 on MacOS Big Sur 11.1. The training of the model is performed on custom GCP Compute Engine (8 vCPUs, 13.75 GB memory) with tensorflow==2.4.0. All dependencies and packages are listed in requirements.txt.

Note: We used opencv-python-headless==4.5.1 due to an issue with cv2.imshow on MacOS Big Sur. However, recent release of opencv-python 4.5.1.48 seems to have fixed the problem. Feel free to modify the requirements.txt before you install all the listed packages.

Setup

  1. Open your terminal, cd into where you'd like to clone this project, and clone the project:
$ git clone https://github.com/achen353/Face-Mask-Detector.git
  1. Download and install Miniconda here.
  2. Create an environment with the packages on requirements.txt installed:
$ conda create --name env_name --file requirements.txt
  1. Now you can cd into the clone repository to run or inspect the code.

How to Run

To detect masked faces in images

cd into /src/ and enter the following command:

$ python detect_mask_images.py -i <image-path> [-m <model>] [-c <confidence>]

To detect masked faces in webcam streams

cd into /src/ and enter the following command:

$ python detect_mask_video.py [-m <model>] [-c <confidence>]

To train the model again on the dataset

cd into /src/ and enter the following command:

$ python train.py [-d <dataset>]

Make sure to modify the paths in train.py to avoid overwriting existing models.

Note:

  • <image-path> should be relative to the project root directory instead of /src/
  • <model> should be of str type; accepted values are MFN and RMFD with default value MFN
  • <confidence> should be float; accepting values between 0 and 1 with default value 0.5
  • <dataset> should be of str type; accepted values are MFN and RMFD with default value MFN

Dash App Demo

The demo of the app is available here; it is still under testing.

Run the app yourself

  1. Modify app.run_server(host='0.0.0.0', port=8080, debug=True) to app.run_server(debug=True):
  2. Run the app:
$ python main.py
  1. Enter http://127.0.0.1:8050/ in your browser to open the app on the Dash app's default host and port. Feel free to modify the host and port number if the default port is taken.

Credits

  • 口罩遮挡人脸数据集(Real-World Masked Face Dataset ,RMFD)
  • Adnane Cabani, Karim Hammoudi, Halim Benhabiles, and Mahmoud Melkemi, "MaskedFace-Net - A dataset of correctly/incorrectly masked face images in the context of COVID-19", Smart Health, ISSN 2352-6483, Elsevier, 2020, DOI:10.1016/j.smhl.2020.100144
  • Karim Hammoudi, Adnane Cabani, Halim Benhabiles, and Mahmoud Melkemi,"Validating the correct wearing of protection mask by taking a selfie: design of a mobile application "CheckYourMask" to limit the spread of COVID-19", CMES-Computer Modeling in Engineering & Sciences, Vol.124, No.3, pp. 1049-1059, 2020, DOI:10.32604/cmes.2020.011663
  • Flickr-Faces-HQ Dataset (FFHQ)
  • Face Mask Detection
  • Object Detection

License

MIT © Andrew Chen

About

Proper Mask Wearing Detection and Alarm System based on Deep Learning and trained using Tensorflow/Keras

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published