Libraries required:
- tensorflow>=1.15.2
- keras==2.3.1
- imutils==0.5.3
- numpy==1.18.2
- opencv-python==4.2.0.*
- matplotlib==3.2.1
- scipy==1.4.1
Steps:
Step 1: Data Visualization
In the first step, let us visualize the total number of images in our dataset in both categories. We can see that there are 690 images in the ‘yes’ class and 686 images in the ‘no’ class.
Step 2: Data Augmentation
In the next step, we augment our dataset to include more number of images for our training. In this step of data augmentation, we rotate and flip each of the images in our dataset. We see that, after data augmentation, we have a total of 2751 images with 1380 images in the ‘yes’ class and ‘1371’ images in the ‘no’ class.
Step 3: Splitting the data
In this step, we split our data into the training set which will contain the images on which the CNN model will be trained and the test set with the images on which our model will be tested.
Step 4: Building the Model
In the next step, we build our Sequential CNN model with various layers such as Conv2D, MaxPooling2D, Flatten, Dropout and Dense. In the last Dense layer, we use the ‘softmax’ function to output a vector that gives the probability of each of the two classes.
Step 5: Pre-Training the CNN model
After building our model, let us create the ‘train_mask_detector’ to fit them to our model in the next step. We see that there are a total of 2200 images in the training set and 551 images in the test set.
Step 6: Detecting the Faces with and without Masks
In the last step, we use the OpenCV library to run an infinite loop to use our web camera in which we detect the face using the Cascade Classifier.