Hey there! In today’s blog I am going to implement the face mask detector in real time video streaming. I hope you have gone through my last blog in which we have successfully created a deep learning model to determine whether a person is wearing a mask or not. If you haven’t read it yet, you can check it here.

Now, it’s time to determine if our model is working or not.

Is our face mask detector model capable of running in real-time?

Let’s go ahead and find it out.

Import libraries

from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.models import load_model
import numpy as np
import imutils
import time
import cv2
import os

I’ll be using tensorflow/keras to load our model and pre-process the input. OpenCV is used to display live video streaming.

Define function to detect mask

Next, I’ll define a function detectMask() which will determine if mask is detected or not.

def detectMask(frame,mask_model):
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cascade = 'haarcascade_frontalface_default.xml'
    faceCascade = cv2.CascadeClassifier(cascade)
    faces = faceCascade.detectMultiScale(gray,1.3,5)  
    coordinates = []
    predictions=[]
    for (x,y,w,h) in faces: 
        face = frame[y:y+h, x:x+h]
        face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
        face = cv2.resize(face, (224, 224))
        face = img_to_array(face)
        face = preprocess_input(face)
        face = np.expand_dims(face, axis=0)
        coordinates.append((x,y,w,h))
        
        if len(face)>0:
            predictions = mask_model.predict(face)
    return coordinates,predictions

The function accepts two parameters:

  • frame: live video stream
  • mask_model: model used to detect mask

Next, I’ll provide a path to my haar cascade file for face detection and will loop over the faces to determine the coordinates of face. I’ll also create two lists for storing coordinates and prediction.

Now, after extracting ROI, we are ready to detect if our faces have a mask on it or not. For detecting masks, I’ll pass face array to our pre-trained model and the model will generate predictions. Finally, we return the coordinates and predictions to the main block.

Main Block

In the main block, I’ll be initializing our pre-trained mask detector model.

model = "model_mask.h5"
# load the face mask detector model from disk
print("loading face mask detector model...")
mask_model = load_model(model)
print('MODEL LOADED')

Next, I will initialize our webcam and then I’ll start looping over the frames and pass each frame to detectMask() function. Then I’ll predict the label and annotate the label and face bounding box.

cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    frame = cv2.flip(frame,1)
    
    coordinates,predictions = detectMask(frame,mask_model)
    for rect,pred in  zip(coordinates,predictions):
        (x,y,w,h) = rect
        (mask, withoutMask) = pred

        if mask > withoutMask:
            label = "Mask"  
            color = (0, 255, 0)
        else:
            label = "No Mask"
            color = (0, 0, 255)

        label = "{}: {:.2f}%".format(label, max(mask, withoutMask) * 100)
        cv2.putText(frame, label, (x, y - 10),cv2.FONT_HERSHEY_SIMPLEX, 0.45, color, 2)
        cv2.rectangle(frame, (x, y), (x+w, y+h), color, 2)

    cv2.imshow("Frame", frame)
    if cv2.waitKey(1)==13:
        cap.release()
        break

cv2.destroyAllWindows()

Lastly, I’ll display the frame and perform clean up.

Implementation

Let’s try to run the program and check if it is running or not.

Conclusion

Congratulations guys! We have finally implemented the face mask detector model in real time.

What would you do differently? Let us know in the comments section.

Hope you liked it! Keep sharing! Keep learning!

-Pratiksha Goyal


12 Comments

Agen Judi Bola · June 3, 2020 at 9:02 pm

Hey there exceptional blog! Does running a blog similar to this require
a large amount of work? I have very little knowledge of coding however
I had been hoping to start my own blog in the near future.
Anyhow, should you have any recommendations or tips for new blog owners please share.
I understand this is off subject but I just had to
ask. Many thanks!

fortnite · June 3, 2020 at 9:15 pm

It’s very effortless to find out any topic on web as compared
to textbooks, as I found this article at this site.

agen poker · June 3, 2020 at 9:26 pm

I love your blog.. very nice colors & theme.

Did you make this website yourself or did you hire someone to do it for you?
Plz respond as I’m looking to create my own blog and would like to know where u got
this from. many thanks

หวย · June 3, 2020 at 9:33 pm

Very rapidly this site will be famous amid all blogging and site-building users, due to it’s
fastidious content

brayde · June 3, 2020 at 9:33 pm

Pretty! This was a really wonderful post. Thank you for providing this information.

Rosal · June 3, 2020 at 9:49 pm

I have read so many articles concerning the blogger lovers however this
article is actually a pleasant piece of writing, keep it up.

Nichol · June 3, 2020 at 9:51 pm

Fine way of describing, and good paragraph to obtain data about my presentation subject, which i am going
to deliver in academy.

Máy · June 3, 2020 at 10:07 pm

Its like you read my mind! You appear to know so much about this,
like you wrote the book in it or something. I think that you can do with
a few pics to drive the message home a little bit, but instead of that,
this is wonderful blog. A fantastic read. I’ll definitely be back.

Tylera · June 3, 2020 at 11:15 pm

Very rapidly this web site will be famous among all
blog users, due to it’s fastidious articles or
reviews

Vadislava · June 10, 2020 at 1:34 pm

Thanks a lot for the post.Really thank you! Much obliged.

Rupesh · June 13, 2020 at 7:43 am

Thank you for your blog post.Really thank you! Awesome.

Yuliya · June 13, 2020 at 7:59 am

Thanks so much for the post.Much thanks again. Really Cool.

Leave a Reply

Your email address will not be published. Required fields are marked *

Insert math as
Block
Inline
Additional settings
Formula color
Text color
#333333
Type math using LaTeX
Preview
\({}\)
Nothing to preview
Insert