Enhance Road Safety with AI

Create a Python-Powered Drowsiness Detection System for Safer Driving



Hey folks! Today I'll give you a brief overview of how the drowsiness detection system works using Python, so stay tuned!

What is a "Drowsiness Detection System"?

Countless people, including taxi drivers, bus drivers, truck drivers, and long-distance travelers, drive on highways day and night. Due to lack of sleep, driving can become very dangerous when feeling sleepy. To prevent this, we build a system using Python, OpenCV, and Keras to alert the driver when they feel sleepy.

Objective

The main objective of this project is to build a drowsiness detection system that detects if a person's eyes are closed for a few seconds. If yes, the system will alert the driver when drowsiness is detected.

Prerequisites

  1. Webcam: To capture the image.
  2. Python: Version 3.6 is recommended.
  3. Necessary Packages: Install using pip:
    • OpenCV (for face and eye detection): pip install opencv-python
    • TensorFlow (Keras uses TensorFlow as backend): pip install tensorflow
    • Keras (to build our classification model): pip install keras
    • Pygame (to play alarm sound): pip install pygame

Steps

  1. Capture an image from the camera.
  2. Detect the face in the image and create a region of interest (ROI).
  3. Detect the eyes from ROI and feed them into the classifier.
  4. Classifier categorizes whether eyes are open or closed.
  5. Calculate a score to check if the person is drowsy.

Dataset

  • The dataset used for this model is created by capturing images of eyes using a camera and storing them locally.
  • Images are labeled as "open" or "closed".
  • The data was manually cleaned by removing unwanted images.
  • The dataset comprises around 7,000 images of people's eyes under different lighting conditions.
  • After training the model on this dataset, the final weights and model architecture are saved in model/cnncat2.h5.
  • This model can classify whether a person's eyes are "open" or "closed".

Model Architecture

The model is built with Keras using a Convolutional Neural Network (CNN), which performs well for image classification tasks. The CNN model architecture consists of the following layers:

  1. Convolutional layer: 32 nodes, kernel size 3
  2. Convolutional layer: 32 nodes, kernel size 3
  3. Convolutional layer: 64 nodes, kernel size 3
  4. Fully connected layer: 128 nodes

The final layer is also a fully connected layer with 2 nodes. A Rectified Linear Activation Function (ReLU) is used in all layers except the output layer, which uses Softmax.

How the Algorithm Works

  1. Capture Image from Camera:

    • An infinite loop captures each frame using OpenCV's cv2.VideoCapture(0).
    • The captured image is stored in a frame variable.
  2. Detect Face and Create ROI:

    • Convert the image to grayscale as OpenCV's algorithm requires grayscale images.
    • Use a cascade classifier to detect faces: face = cv2.CascadeClassifier("path_to_haar_cascade.xml").
    • Perform face detection: face.detectMultiScale(gray).
  3. Detect Eyes from ROI and Feed into Classifier:

    • Detect eyes using classifiers for left and right eyes.
    • Extract the eye data from the full image.
    • Convert the extracted eye image to grayscale and normalize it.
    • Load the trained model and predict the eye status using the classifier.
  4. Classify Eyes as Open or Closed:

    • If lpred[0] = 1, the eyes are open.
    • If lpred[0] = 0, the eyes are closed.
  5. Calculate Drowsiness Score:

    • The score indicates how long the eyes have been closed.
    • If both eyes are closed, increase the score.
    • If eyes are open, decrease the score.
    • Display real-time status using cv2.putText().
    • If the score exceeds 15, beep the alarm using sound.Play().

This system helps in preventing accidents by alerting the driver when they become drowsy, thereby enhancing road safety.

If you want access to the full code repository for this project, you can refer to my GitHub link: GitHub Repository.

This repository contains the complete implementation of the drowsiness detection system, including the model training script, data preprocessing steps, and the real-time detection script.

Feel free to check it out, and happy coding!

Comments

Post a Comment