AI Mask Detection System
1. Introduction
The AI Mask Detection System is designed to detect and classify whether individuals are wearing a face mask in real-time. This system uses deep learning techniques and computer vision libraries such as OpenCV and TensorFlow/Keras. It is particularly useful in ensuring compliance with mask mandates in public spaces.
2. Prerequisites
• Python: Install Python 3.x from the official Python
website.
• Required Libraries:
- opencv-python: Install using pip
install opencv-python
- numpy: Install using pip install
numpy
- tensorflow: Install using pip install
tensorflow
- keras: Install using pip install
keras
- pretrained mask detection model (if
available).
• Webcam or camera-enabled device for real-time detection.
3. Project Setup
1. Create a Project Directory:
- Name your project folder, e.g., `AI_Mask_Detection`.
- Inside this folder, create the Python script file (`mask_detection.py`).
2. Install Required Libraries:
Ensure TensorFlow, Keras, and OpenCV are installed using `pip`.
4. Writing the Code
Below is an example code snippet for the AI Mask Detection System:
import cv2
import numpy as np
from tensorflow.keras.models import load_model
# Load pre-trained model for mask detection
model = load_model('mask_detection_model.h5')
# Load face detector (Haar cascades or DNN model)
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +
'haarcascade_frontalface_default.xml')
# Function to predict mask usage
def detect_mask(face_img):
face_img = cv2.resize(face_img, (128,
128))
face_img = np.expand_dims(face_img,
axis=0)
face_img = face_img / 255.0
prediction = model.predict(face_img)[0]
return "Mask" if
prediction[0] > 0.5 else "No Mask", prediction
# Main function for video stream processing
def main():
video_capture = cv2.VideoCapture(0)
while True:
ret, frame = video_capture.read()
if not ret:
break
gray = cv2.cvtColor(frame,
cv2.COLOR_BGR2GRAY)
faces =
face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5,
minSize=(60, 60))
for (x, y, w, h) in faces:
face = frame[y:y+h, x:x+w]
label, prediction =
detect_mask(face)
color = (0, 255, 0) if label
== "Mask" else (0, 0, 255)
cv2.rectangle(frame, (x, y),
(x+w, y+h), color, 2)
cv2.putText(frame, label, (x,
y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2)
cv2.imshow('AI Mask Detection',
frame)
if cv2.waitKey(1) & 0xFF ==
ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()
5. Key Components
• Face Detection: Detects faces using Haar cascades or a
pre-trained deep learning model.
• Mask Classification: Classifies detected faces as 'Mask' or 'No Mask' using a
deep learning model.
• Real-Time Processing: Captures and processes live video streams.
6. Testing
1. Ensure the mask detection model is trained and saved as `mask_detection_model.h5`.
2. Run the script:
python mask_detection.py
3. Verify the detection and classification of faces in real-time.
7. Enhancements
• Accuracy Improvements: Train on a larger dataset for
better model accuracy.
• Multi-Class Detection: Include additional classifications (e.g., improper
mask usage).
• Notifications: Integrate alerts for 'No Mask' detections.
8. Troubleshooting
• Detection Issues: Ensure proper lighting and clear video
input.
• Model Loading Errors: Verify the path and compatibility of the mask detection
model.
• Performance Lag: Optimize the model or reduce input resolution.
9. Conclusion
The AI Mask Detection System is a practical application for promoting public safety in health-sensitive environments. With further improvements, it can be deployed in public areas, workplaces, and transportation hubs.