Duval LC

Python Project: Build an AI Face Detection Alarm System with Your Webcam

Programming


Python is widely recognized as a leading language for artificial intelligence and computer vision projects. By harnessing the capabilities of libraries like OpenCV and MediaPipe, you can create an intelligent system that detects faces and triggers alerts in real-time. In this tutorial, we will guide you through a straightforward project that turns your webcam into a face detection alert system.


image



Note: This guide uses Windows 11, but the concepts can easily be adapted for Linux or macOS users.


Getting Started: Dependencies Installation

To begin, ensure you have Python installed on your system. We are using version 3.12.x in this tutorial, paired with VS Code as our code editor. Feel free to use any IDE you prefer. The first step is to set up your virtual environment:

  1. Create a new project folder and navigate into it.
  2. Open your terminal (PowerShell for Windows users).
  3. Run the following command to create a virtual environment:
  4. python -m venv .venv
    

  5. Activate the virtual environment with:
  6. .\.venv\Scripts\Activate.ps1  # Windows PowerShell
    source venv/bin/activate       # Linux or macOS
    

Next, install the necessary Python libraries:

pip install opencv-python mediapipe playsound3

To confirm successful installation, you can check your installed packages:

pip list

Lastly, you'll need an audio alert file (e.g., alert.wav) saved in your project directory.



Setting Up the Project

With our dependencies ready, let’s start building the core of our AI alert system. Create a file named main.py for your code. Start by importing OpenCV:

import cv2

We’ll use OpenCV to access the webcam and read video frames. Initialize your webcam with:

cap = cv2.VideoCapture(0)

Safety Check

It's essential to confirm that the webcam is accessible:

if not cap.isOpened():
    raise RuntimeError("Could not open webcam.")

Creating the Main Loop

Next, set up a loop to continuously read frames from the webcam and display them. This loop will terminate when the user presses Q:

while True:
    ret, frame = cap.read()
    if not ret:
        break

    cv2.imshow("AI Alert System", frame)

    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

At the end of the loop, make sure to release the webcam:

cap.release()
cv2.destroyAllWindows()

Now, run your code:

python main.py

You should see a live feed from your webcam!



Implementing Face Detection

Now that we have the webcam feed, let’s add face detection using MediaPipe. Import the library at the top of your main.py file:

import mediapipe as mp

Next, set up the face detection model:

mp_face = mp.solutions.face_detection
face_detector = mp_face.FaceDetection(model_selection=0, min_detection_confidence=0.5)

This configuration uses a lightweight model optimal for close-range detection.


Detecting Faces in Real Time

Within your main loop, after reading the webcam feed, convert the frame for MediaPipe:

rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = face_detector.process(rgb)
faces_detected = results.detections is not None

To visualize facial detection, draw a bounding box around detected faces:

if faces_detected:
    for detection in results.detections:
        box = detection.location_data.relative_bounding_box
        h, w, _ = frame.shape

        x = int(box.xmin * w)
        y = int(box.ymin * h)
        w_box = int(box.width * w)
        h_box = int(box.height * h)

        cv2.rectangle(frame, (x, y), (x + w_box, y + h_box), (0, 255, 0), 2)
else:
    cv2.putText(frame, "No face detected", (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)

Save and run your code again. Move in front of your webcam to see the detection in action.


Adding Sound Alerts for Face Detection

To make the system more interactive, let’s add sound alerts when a face is detected. Import the necessary libraries at the top:

import playsound3
import threading

Define a function to play the sound:

def play_alert():
    playsound3.playsound("alert.wav")

Sound Logic in the Detection Loop

We need to prevent the sound from being played multiple times. Track the sound state with a boolean variable:

sound_played = False

Modify your detection logic to trigger the sound alert:

if faces_detected and not sound_played:
    threading.Thread(target=play_alert).start()
    sound_played = True
elif not faces_detected:
    sound_played = False


Final Code

Here is your complete code for the AI alert system:

import cv2
import mediapipe as mp
import playsound3
import threading

mp_face = mp.solutions.face_detection
face_detector = mp_face.FaceDetection(model_selection=0, min_detection_confidence=0.5)

cap = cv2.VideoCapture(0)
if not cap.isOpened():
    raise RuntimeError("Could not open webcam.")

# Track sound state
sound_played = False

def play_alert():
    playsound3.playsound("alert.wav")

while True:
    ret, frame = cap.read()
    if not ret:
        break

    # Convert to RGB because Mediapipe expects RGB frames
    rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

    # Run face detection
    results = face_detector.process(rgb)

    # Check if any faces were found
    faces_detected = results.detections is not None

    if faces_detected:
        for detection in results.detections:
            box = detection.location_data.relative_bounding_box
            h, w, _ = frame.shape

            x = int(box.xmin * w)
            y = int(box.ymin * h)
            w_box = int(box.width * w)
            h_box = int(box.height * h)

            # Draw clean green face box
            cv2.rectangle(frame, (x, y), (x + w_box, y + h_box), (0, 255, 0), 2)
    else:
        # No face case
        cv2.putText(frame, "No face detected", (20, 40), cv2.FONT_HERSHEY_SIMPLEX,
                    1, (0, 0, 255), 2)
        
    # Play alert only once per detection
    if faces_detected and not sound_played:
        threading.Thread(target=play_alert).start()
        sound_played = True
        print("Sound played")
    elif not faces_detected:
        sound_played = False

    cv2.imshow("AI Alert System", frame)

    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

cap.release()
cv2.destroyAllWindows()


Congratulations! You've successfully built a face detection alert system with Python. This foundational project can be further developed into a more complex smart home application. Explore adding hardware for notifications or integrating with home automation systems.