How to Create a Face Mask Detection App with Python and TensorFlow

In recent times, face mask detection has become crucial for maintaining public health and safety. By leveraging machine learning and computer vision techniques, we can now build face mask detection apps that can easily identify individuals wearing masks and those who aren’t.

In this tutorial, we will walk through the process of creating a face mask detection app using Python and TensorFlow. We will train a deep learning model to detect face masks in images, and then build a user-friendly app that can detect masks in real-time using a webcam. Let’s get started!

Table of Contents

  1. Prerequisites
  2. Setting up the Environment
  3. Data Collection and Preprocessing
  4. Training the Face Mask Detection Model
  5. Building the Face Mask Detection App
  6. Conclusion

1. Prerequisites

Before we begin, make sure you have the following prerequisites installed on your system:
– Python 3
– TensorFlow
– OpenCV
– NumPy
– Matplotlib

You can install these packages using pip, the Python package manager. Open your terminal and run the following command to install all the dependencies:

pip install tensorflow opencv-python numpy matplotlib

2. Setting up the Environment

To get started, let’s create a new directory for our project. Open your terminal and run the following command to create the directory:

mkdir face_mask_detection_app
cd face_mask_detection_app

Inside this directory, we will store all our project files.

3. Data Collection and Preprocessing

The first step in building a face mask detection app is to collect and preprocess the data. We need two sets of images: one containing individuals wearing face masks, and the other without face masks.

You can collect your own dataset or use publicly available datasets. Some popular face mask datasets include:
The Face Mask Detection Dataset by Prajna Bhandary
The Medical Masks Dataset by Anthem
The Face Mask Detection Dataset by Wobot Intelligence

Once you have downloaded the dataset, create two subdirectories inside your project directory: with_mask and without_mask. Place the corresponding images into these directories.

Next, we need to preprocess the data by resizing the images to a fixed size. Open your code editor and create a new Python file called data_preprocessing.py inside the project directory. Write the following code:

import os
import cv2

# Specify the directories containing the images
with_mask_dir = "with_mask/"
without_mask_dir = "without_mask/"

# Specify the directory to store the preprocessed images
output_dir = "preprocessed_data/"

# Create the output directory if it doesn't exist
os.makedirs(output_dir, exist_ok=True)

# Rescale the image to a fixed size of 224x224 pixels
def preprocess_image(image_path):
    image = cv2.imread(image_path)
    image = cv2.resize(image, (224, 224))
    return image

# Preprocess the images with face masks
with_mask_images = os.listdir(with_mask_dir)
for image_name in with_mask_images:
    image_path = os.path.join(with_mask_dir, image_name)
    output_path = os.path.join(output_dir, "with_mask_" + image_name)
    preprocessed_image = preprocess_image(image_path)
    cv2.imwrite(output_path, preprocessed_image)

# Preprocess the images without face masks
without_mask_images = os.listdir(without_mask_dir)
for image_name in without_mask_images:
    image_path = os.path.join(without_mask_dir, image_name)
    output_path = os.path.join(output_dir, "without_mask_" + image_name)
    preprocessed_image = preprocess_image(image_path)
    cv2.imwrite(output_path, preprocessed_image)

Save the file and run it by executing the following command in your terminal:

python data_preprocessing.py

This script will preprocess the images by resizing them to a fixed size of 224×224 pixels and save them in the preprocessed_data directory.

4. Training the Face Mask Detection Model

Now that we have preprocessed the data, let’s train a deep learning model to detect face masks in images. For this tutorial, we will use transfer learning with the MobileNetV2 architecture, which is a popular choice for image classification tasks.

Open your code editor and create a new Python file called train_model.py inside the project directory. Write the following code:

import os
import tensorflow as tf
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D

# Specify the directories containing the preprocessed images
train_data_dir = "preprocessed_data/"

# Load the MobileNetV2 model
base_model = MobileNetV2(weights="imagenet", include_top=False, input_shape=(224, 224, 3))

# Add custom output layers for face mask detection
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(64, activation="relu")(x)
predictions = Dense(1, activation="sigmoid")(x)
model = Model(inputs=base_model.input, outputs=predictions)

# Freeze the base model layers
for layer in base_model.layers:
    layer.trainable = False

# Compile the model
model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])

# Load the preprocessed images
image_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)
data_generator = image_generator.flow_from_directory(
    train_data_dir,
    target_size=(224, 224),
    batch_size=32,
    class_mode="binary"
)

# Train the model
model.fit(data_generator, epochs=10)

# Save the trained model
model.save("face_mask_detection_model.h5")

Save the file and run it by executing the following command in your terminal:

python train_model.py

This script will load the preprocessed images, build a model architecture using transfer learning, train the model on the images, and save the trained model to a file called face_mask_detection_model.h5.

5. Building the Face Mask Detection App

Now that we have a trained model to detect face masks, let’s create an app to put it into action. We will build a simple GUI application that uses a webcam to detect face masks in real-time.

Open your code editor and create a new Python file called face_mask_detection_app.py inside the project directory. Write the following code:

import cv2
import tensorflow as tf

# Load the trained model
model = tf.keras.models.load_model("face_mask_detection_model.h5")

# Initialize the video capture object
capture = cv2.VideoCapture(0)

# Open a window to display the output
cv2.namedWindow("Face Mask Detection")

# Define the colors for drawing bounding boxes
with_mask_color = (0, 255, 0)       # Green
without_mask_color = (0, 0, 255)    # Red

# Start the video capture loop
while True:
    # Read a frame from the video stream
    ret, frame = capture.read()

    # Check if the frame is valid
    if not ret:
        break

    # Convert the frame to grayscale
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Perform face detection
    face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(60, 60))

    # Iterate over the detected faces
    for (x, y, w, h) in faces:
        # Preprocess the face image
        face = frame[y:y+h, x:x+w]
        face = cv2.resize(face, (224, 224))
        face = tf.keras.preprocessing.image.img_to_array(face)
        face = tf.expand_dims(face, axis=0)
        face = face / 255.0

        # Perform face mask detection
        result = model.predict(face)[0][0]
        if result < 0.5:
            label = "With Mask"
            color = with_mask_color
        else:
            label = "Without Mask"
            color = without_mask_color

        # Draw a bounding box around the face and display the label
        cv2.rectangle(frame, (x, y), (x+w, y+h), color, 2)
        cv2.putText(frame, label, (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, 2)

    # Display the frame with the detections
    cv2.imshow("Face Mask Detection", frame)

    # Break the loop on "q" key press
    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

# Release the video capture object and close the windows
capture.release()
cv2.destroyAllWindows()

Save the file and run it by executing the following command in your terminal:

python face_mask_detection_app.py

This script will load the trained model, initialize the video capture object for the webcam, detect faces in real-time, perform face mask detection on each frame, draw bounding boxes around the faces, and display the result in a window.

6. Conclusion

Congratulations! You have successfully created a face mask detection app using Python and TensorFlow. You have learned how to collect and preprocess the data, train a deep learning model using transfer learning, and build a user-friendly app to detect face masks in real-time.

Feel free to explore further by improving the app’s performance, deploying it as a web application, or even integrating it with other technologies. The possibilities are endless!

Remember, face mask detection is just one application of computer vision. You can apply similar techniques to solve various other real-world problems. Keep exploring and happy coding!

Related Post