Skip to main content

Introduction:

Fatigue and drowsy driving are significant contributors to road accidents worldwide. To tackle this issue, we can create a drowsiness detection system that monitors the driver’s facial landmarks and alerts them when signs of drowsiness are detected. In this blog, we will walk through the process of building a drowsiness detector using Python and facial landmark detection. We will utilize the Dlib library, which provides pre-trained models for facial landmark detection, and OpenCV to process video frames. So, let’s get started!

Table of Contents:

  • Understanding Facial Landmarks
  • Setting Up the Environment
  • Installing Dependencies
  • Loading the Facial Landmark Detector
  • Detecting Facial Landmarks
  • Calculating Eye Aspect Ratio (EAR)
  • Drowsiness Detection Algorithm
  • Creating the Drowsiness Detector
  • Conclusion

1. Understanding Facial Landmarks:

Facial landmarks are specific points on the face, such as the corners of the eyes, nose, and mouth, that can be used to identify unique features. They are essential for various computer vision applications, including facial recognition and emotion detection. In our drowsiness detector, we will focus on the eye landmarks to monitor eye closure patterns.

2. Setting Up the Environment:

Before we begin, ensure you have Python installed on your system. It’s also beneficial to create a virtual environment for this project to keep dependencies isolated.

3. Installing Dependencies:

We need to install the following libraries:

bash
pip install numpy opencv-python dlib imutils

4. Loading the Facial Landmark Detector:

We will use the pre-trained facial landmark detector from the Dlib library. To download it, go to the following link: Facial Landmark Pre-trained

Extract the downloaded file and place it in the project directory.

5. Detecting Facial Landmarks:

Let’s write the code to detect facial landmarks using Dlib:

code
import dlib
import cv2
def detect_facial_landmarks(image):
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
rects = detector(gray, 0)
if len(rects) == 0:
return None
shape = predictor(gray, rects[0])
landmarks = [(shape.part(i).x, shape.part(i).y) for i in range(68)]
return landmarks

6. Calculating Eye Aspect Ratio (EAR):

The Eye Aspect Ratio (EAR) is a measure used to detect eye closure. It is calculated as the ratio of the vertical distance between two eye landmarks to the horizontal distance between two other landmarks. When a person is drowsy, their EAR decreases.

code
def eye_aspect_ratio(eye):
A = distance.euclidean(eye[1], eye[5])
B = distance.euclidean(eye[2], eye[4])
C = distance.euclidean(eye[0], eye[3])
ear = (A + B) / (2.0 * C)
return ear

7. Drowsiness Detection Algorithm:

To detect drowsiness, we will continuously monitor the eye-aspect ratio (EAR) and raise an alert if it falls below a certain threshold for a specific duration.

 

8. Creating the Drowsiness Detector:

code
import time
from scipy.spatial import distance
def is_drowsy(ear, threshold, frames):
consecutive_frames = 0
for i in range(1, len(ear)):
if ear[i] < threshold:
consecutive_frames += 1
else:
consecutive_frames = 0
if consecutive_frames >= frames:
return True
return False

Conclusion:

In this blog, we have successfully built a drowsiness detector using facial landmarks and Python. By monitoring the Eye Aspect Ratio (EAR) over time, we can detect signs of drowsiness in the driver and prevent potential accidents on the road.

Remember, drowsy driving is dangerous, and it’s crucial to take regular breaks during long journeys. Implementing such a system in real-life vehicles could significantly contribute to road safety.

Stay safe and alert on the road! Happy coding!