This project uses Python computer vision to monitor a user's eyes and detect signs of drowsiness. This technology has the potential to prevent accidents related to drowsiness, such as those that occur while driving.
The necessary packages are imported at the beginning of the code. The main packages include:
dlib
: A library containing machine learning algorithms and image processing tools, including face detection.cv2 (OpenCV)
: A computer vision library that contains functions for processing images and videos.imutils
: A library that simplifies some basic image processing operations with OpenCV.numpy
: A library used for scientific computing with Python.playsound
: A library for playing sounds.argparse
: A library for creating user-friendly command line interfaces.scipy.spatial
: A module for calculating distances and performing operations in n-dimensional spaces.matplotlib.pyplot
: Used for plotting data.
The structure of the project is as follows:
.
├── Readme.md
├── alarm.wav
├── environment.yaml
└── main.py
└── shape_predictor_68_face_landmarks.dat
Description of the files:
alarm.wav
: Alarm sound that will be played when signs of fatigue are detected.main.py
: Main Python script that contains the logic of the fatigue detector.requirements.txt
: A file that lists the dependencies required to run the program.shape_predictor_68_face_landmarks.dat
: Data file used by the dlib facial landmarks detector.
Two custom functions are defined at the beginning of the program. The play_alarm(path)
function plays an alarm sound. The calculate_ear(eye)
function computes the Eye Aspect Ratio (EAR). The EAR is a numeric value that indicates the degree of eye openness. When a person blinks, the EAR decreases.
The program accepts two command line arguments: "-a" to turn the sound alarm on or off, and "-w" to select the camera that will be used to capture the video.
The program defines some constants, such as the EAR threshold that indicates a blink (EAR_THRESHOLD
) and the number of consecutive frames where the EAR must be below the threshold to trigger the alarm (NUM_CONSEC_FRAMES
). It also initializes some variables and loads the dlib face detector and facial landmarks predictor.
The program enters a continuous loop, where each iteration of the loop processes a single frame of the video. For each frame, the program detects present faces, identifies the facial features for the face region, extracts the coordinates of the left and right eyes, and calculates the EAR for both eyes.
The program draws the contours of the eyes on the original image using the cv2.drawContours()
method. The EAR ratio is displayed on a real-time graph with each iteration.
If the EAR is below the threshold for a set number of consecutive frames, the program considers the user to be becoming drowsy and triggers an alarm.
If the EAR returns to being above the threshold, the frame counter and the alarm are reset.
The code also displays the calculated EAR on the video frame to aid in debugging and adjusting the appropriate EAR thresholds.
The video processing loop continues until the 'q' key is pressed. When this happens, the program ends and finalizes the video stream.
SOUKUPOVA, Tereza; CECH, Jan. Real-time eye blink detection using facial landmarks. In: 21st computer vision winter workshop, Rimske Toplice, Slovenia. 2016.
This project is licensed under the MIT License