We used a pre-trained Multi-Task Cascaded Convolutional Neural Network (facenet-pytorch MTCNN) and trained a PCA-SVM pipeline to detect faces in images and then classify them to see whether people in those images are wearing masks.
Fall 2020 COMP562 Project at UNC Chapel Hill
We used the Face Mask Detection dataset on Kaggle: https://www.kaggle.com/andrewmvd/face-mask-detection
1. Without mask:![]() |
2. Mask worn incorrectly:![]() |
3: With mask:![]() |
You will require Python 3 along with the following dependencies installed:
- numpy
- pandas
- os (standard Python library)
- re (standard Python library)
- xml.etree.ElementTree (standard Python library)
- scikit-learn
- matplotlib
- opencv-python (referred to as cv2 in the code, link is for CPU-only package)
- scipy
- pickle (standard Python library)
- pytorch
- facenet-pytorch
You will also require Jupyter Notebook
The application code is in the notebook in the repo's root directory: comp562-face-mask-detector.ipynb
Clone the GitHub repository, and open the notebook using Jupyter Notebook. To use just the app, simply run the last cell in the notebook. We have provided our trained PCA-SVM classifier model as a saved pickle object, face_mask_detect_model.pkl, that will get automatically loaded when you run the last cell, if it is kept in the same directory as the comp562-face-mask-detector.ipynb notebook.
If you would also like to train the model, or see how everything works, go in the order from first cell to last!
Please show your love, and leave a like if this helped!
If the app is classifying wrongly, try facing the camera head-on and move closer to the camera. Make sure light is falling onto your face to illuminate it enough.
-
The original MTCNN research paper: https://arxiv.org/abs/1604.02878
-
Face Mask Detection Dataset on Kaggle: https://www.kaggle.com/andrewmvd/face-mask-detection
-
Working of MTCNN: https://towardsdatascience.com/how-does-a-face-detection-program-work-using-neural-networks-17896df8e6ff
-
How to use MTCNN in facenet-pytorch: https://www.kaggle.com/timesler/guide-to-mtcnn-in-facenet-pytorch
-
For learning how to code up a PCA-SVM pipeline: https://jakevdp.github.io/PythonDataScienceHandbook/05.07-support-vector-machines.html
-
For handling the Face-Mask-Detection dataset and creating the data and labels arrays: https://www.kaggle.com/notadithyabhat/face-mask-detector/