Implement Face-mask Model on OpenCV

This is our final part of the realtime face mask detection project. Firstly please read the first and second part of this project. Previously we are training vgg16 model with face mask image and save the model. This mode is used here for real-time prediction. Let’s begin to the final part of this project.

Import Library

for realtime face mask detection, these libraries are required. If you are not installing these libraries on your pc. Install it by using PIP install Package_name Command.

from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.models import load_model
from import VideoStream
import numpy as np
import imutils
import time
import cv2
import os
import pygame

After importing all libraries initialized pygame by the following code. For play alert sound while predicting real-time data.


Load model

In this section Load our model. We have one our train model and prototext and weight. Load model and create a face net by the following code. prototxt and weight are available here.

prototxtPath = r"./face_detector/deploy.prototxt"
weightsPath = r"./face_detector/res10_300x300_ssd_iter_140000.caffemodel"
faceNet = cv2.dnn.readNet(prototxtPath, weightsPath)
maskNet = load_model("mask_detector.model")


for face recognize we do not use haar cascade. We use Caffe model to identify the location of faces on the frame. So Firstly create on function detect_predict_mask() that take the three-parameter frame, facenet, and masknet. Where the frame is camera capture data, facenet is Caffe model to recognize the face and mask net is our face mask detection model which predict the outcome.

Insite this function firstly extract height and width of image from the frame. Then create blob from an image that helps to identify the location of image.usign this we can predict the outcome. Here is code for full implementation of this function.

def detect_and_predict_mask(frame, faceNet, maskNet):
	(h, w) = frame.shape[:2]
	blob = cv2.dnn.blobFromImage(frame, 1.0, (100,100),
		(104.0, 177.0, 123.0))

	detections = faceNet.forward()
	faces = []
	locs = []
	preds = []

	for i in range(0, detections.shape[2]):

		confidence = detections[0, 0, i, 2]

		if confidence > 0.5:

			box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
			(startX, startY, endX, endY) = box.astype("int")

			(startX, startY) = (max(0, startX), max(0, startY))
			(endX, endY) = (min(w - 1, endX), min(h - 1, endY))

			face = frame[startY:endY, startX:endX]
			face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
			face = cv2.resize(face, (100, 100))
			face = img_to_array(face)

			locs.append((startX, startY, endX, endY))

	if len(faces) > 0:

		faces = np.array(faces, dtype="float32")
		preds = maskNet.predict(faces)

	return (locs, preds)

Final Result

let’s start the video stream for real-time prediction. For that call video stream function with parameter 0. While video stream capture image further operation is performed. Following code are used to predict real-time data.

vs = VideoStream(src=0).start()
while True:
	frame =
	frame = imutils.resize(frame, width=400)

	(locs, preds) = detect_and_predict_mask(frame, faceNet, maskNet)
	for (box, pred) in zip(locs, preds):
		label='mask' if pred[0]>0.5 else 'No Mask'
		if label=='mask':
		color = (0, 255, 0) if label == "mask" else (0, 0, 255)

		label = "{}: {:.2f}%".format(label, conf)

		cv2.putText(frame, label, (x, y - 10),
		cv2.FONT_HERSHEY_SIMPLEX, 0.45, color, 2)
		cv2.rectangle(frame, (x, y), (x2, y2), color, 2)
	cv2.imshow('frame', frame)
	key = cv2.waitKey(1) & 0xFF

	if key == ord("q"):

At last need to destroyed all window. And also stop the video capture by following line of code.


After codding is complete and run the final code. The camera is open and frame that predicts our face with a mask or not as below.

predict no mask
realtime face mask detection
predict mask


Ok, this is the end of the project I hope you you can get a good lesson what I deliver in this project. I ask forgiveness for any word and behave which are not to be. Thank you for your kind and attention guys. Stay tuned for my next project

Leave a Reply