achen353 / Face-Mask-Detector

Proper Mask Wearing Detection and Alarm System based on Deep Learning and trained using Tensorflow/Keras
MIT License
14 stars 11 forks source link
covid-19 deep-learning face-mask-detection keras-tensorflow mask-detection

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

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

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:

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:

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
  2. Download and install Miniconda here.
  3. Create an environment with the packages on requirements.txt installed:
    $ conda create --name env_name --file requirements.txt
  4. 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:

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
  3. 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

License

MIT © Andrew Chen