Face mask detection using Pretrained MobilenetV2 using TensorFlow 2.3 and Keras API with help of Opencv's haarcascade to help with Facial recognition before feeding the data to the model.
- Python 3.6 or Higher
- TensorFlow 2.x
- opencv
- numpy
This command will help to install the required independenies:
pip install -r requirements.txt
To use your GPU in computing, You must install the required version of CUDA Toolkit suitable with the version of your installed Tensorflow for more help installing CUDA Toolkit visit Tensorflow site.
The dataset came form Kaggle and had 12K examples splited into 2 class:
- Wearing Mask.
- Not Wearing Mask.
Which was split to 3 categories:
- Training
- Testing
- Validation
To check the dataset from Kaggle you can visit this link .
The model consist of the pretrained network of MobileNetV2 with adding two extra layers to be trained on our data:
- Flatten Layer
- Dense layer with 2 neuron; one for each class with activation function of Softmax
The model Takes RGB images with shape of (1,128,128,3) and gives out A Vector of two Values. The First value indicates the confidence of the predication of wearing a mask. And the Second Value indicates the confidence of not wearing a mask.
Due to using Softmax Function the sum of the two Values equals to one.
To run the model using Webcam and highlighting the faces in the feed images with a Bounding Box indicating the Prediction and the position of the face run this command
python "WebCam.py"
WebCam.py
script uses opencv's HaarCascade to crop the faces from the input frames from the webcam. And feeds the cropped image to the trained MobileNetV2 to make a prediction and based on it a Bounding Box with the prediction will be drawn on the output.
To Terminate the program after running press ESC.
The frame read from the WebCam maybe like this image.
later the Haar Cascade crops out the faces in the image like this.
Then each face is fed to the MobileNetV2 individually to make a predication.
A Bounding Box is drawn using the coordinates given by the Haar Cascade. The box indicates both predication and position of the face.