Skip to content

eugenius-watchman/Face_Mask_Detection_Python

Repository files navigation

Core Statistical & Machine Learning Methods

This project applies the following key methodologies from applied statistics and machine learning:

Binary Classification: The core task is a binary classification problem (Mask/No Mask) using labeled image data.

Supervised Machine Learning: Implementation of a Support Vector Machine (SVM), a fundamental supervised learning algorithm for classification.

Feature Engineering: Extraction of facial features using the Haar Cascade Classifier to transform raw images into data suitable for statistical modeling.

Model Evaluation & Hypothesis Testing: The model's performance is evaluated using statistical metrics (accuracy), which inherently involves testing the hypothesis that the model's predictions are better than random chance.

Applied Statistical Modeling: The entire pipeline—from data collection and feature extraction to model training and prediction—constitutes an applied statistical modeling workflow.

A JPG image for experimenting the haar cascade classifier algorithm. Face Mask Detection using Python OPenCv Machine Learning and Python Sklearn.

Face Mask Detection System

This project aims to detect whether individuals are wearing face masks using a combination of computer vision and machine learning techniques. The system captures facial images from a webcam, processes them, and utilizes a Support Vector Machine (SVM) classifier to predict mask usage. Features

Face Data Capture: Captures images of faces with and without masks using Haar Cascade Classifier.
Data Storage: Saves captured face images as NumPy arrays for training.
Mask Detection: Uses SVM for classifying faces as "With Mask" or "Without Mask."
Real-time Detection: Displays the live video feed with predictions overlaid.

Requirements

OpenCV
NumPy
scikit-learn

Installation

Clone the repository:

bash

git clone https://github.com/eugenius-watchman/Face_Mask_Detection_Python.git

Navigate to the project directory:

bash

cd restaurant_app

Install the required packages:

bash

pip install -r requirements.txt

Usage Step 1: Capture Face Data

To capture images of faces, run the following code snippet:

python

Import necessary libraries

import cv2 import numpy as np

Load the Haar Cascade Classifier

haar_data = cv2.CascadeClassifier('haar_data.xml')

Function to capture face data

def capture_faces(data_list, label): ...

Capture without mask data

capture_faces(without_mask_data, "without mask")

Capture with mask data

capture_faces(with_mask_data, "with mask")

This will prompt you to capture face images without masks first, followed by images with masks. Step 2: Train the Classifier

After capturing the data, train the SVM classifier using the captured images:

python

Load the images

with_mask = np.load('with_mask.npy') without_mask = np.load('without_mask.npy')

Combine the data and prepare labels

X = np.r_[with_mask, without_mask] labels = np.zeros(X.shape[0]) labels[len(with_mask):] = 1.0 # 0 for with_mask, 1 for without_mask

Train the SVM

svm.fit(x_train, y_train)

Step 3: Real-time Mask Detection

To use the trained classifier for real-time mask detection:

python

Start capturing video

capture = cv2.VideoCapture(0) while True: ... pred = svm.predict(face_rect) ... cv2.imshow('result', img)

Press Esc to exit the live video feed. Results

The system will display the live video feed with bounding boxes around detected faces, and it will print whether each detected face is wearing a mask or not. Notes

Ensure that haar_data.xml is available in the project directory.
Adjust the max_samples variable to change the number of images captured during training.

Accuracy

The accuracy of the model will be printed after the classification is complete.

License

This project is licensed under the MIT License.

About

Face Mask Detection using Python OPenCv Machine Learning and Python Sklearn.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages