e-candeloro / Driver-State-Detection

A real time, webcam based, driver attention state detection/monitoring system in Python3 using OpenCV and Mediapipe
MIT License
123 stars 28 forks source link
automotive collaborate computer-vision distracted-driving-detection dlib driver-safety human-attention machine-learning mediapipe opencv-python perclos pose-estimation python3 real-time

Real Time Driver State Detection

Python OpenCV

Real time, webcam based, driver attention state detection and monitoring using Python with the OpenCV and mediapipe libraries.

driver state detection demo

Note: This work is partially based on this paper for the scores and methods used.

Mediapipe Update

Thanks to the awesome contribution of MustafaLotfi, now the script uses the better performing and accurate face keypoints detection model from the Google Mediapipe library.

Features added:

NOTE: the old mediapipe version can still be found in the "dlib-based" repository branch.

How Does It Work?

This script searches for the driver face, then use the mediapipe library to predict 478 face and iris keypoints. The enumeration and location of all the face keypoints/landmarks can be seen here.

With those keypoints, the following scores are computed:

The driver states can be classified as:

Demo

MEDIAPIPE DEMO COMING SOON

OLD DEMO:

The Scores Explained

EAR

Eye Aspect Ratio is a normalized score that is useful to understand the rate of aperture of the eyes. Using the mediapipe face mesh keypoints for each eye (six for each), the eye lenght and width are estimated and using this data the EAR score is computed as explained in the image below: EAR

NOTE: the average of the two eyes EAR score is computed

Gaze Score Estimation

The gaze score gives information about how much the driver is looking away without turning his head.

To understand this, the distance between the eye center and the position of the pupil is computed. The result is then normalized by the eye width that can be different depending on the driver physionomy and distance from the camera.

The below image explains graphically how the Gaze Score for a single eye is computed: Gaze Score NOTE: the average of the two eyes Gaze Score is computed

Head Pose Estimation

For the head pose estimation, a standard 3d head model in world coordinates was considered, in combination of the respective face mesh keypoints in the image plane. In this way, using the solvePnP function of OpenCV, estimating the rotation and translation vector of the head in respect to the camera is possible. Then the 3 Euler angles are computed.

The partial snippets of code used for this task can be found in this article.

Installation

This projects runs on Python with the following libraries:

Or you can use poetry to automatically create a virtualenv with all the required packages:

pip install poetry #a global install of poetry is required

Then inside the repo directory:

poetry install

To activate the env to execute command lines:

poetry shell

Alternatively (not recommended), you can use the requirements.txt file provided in the repository using:

pip install -r requirements.txt

Usage

First navigate inside the driver state detection folder:

cd driver_state_detection

The scripts can be used with all default options and parameters by calling it via command line:

python main.py

For the list of possible arguments, write:

python main.py --help

Example of a possible use with parameters:

python main.py --ear_time_tresh 5

This will sets to 5 seconds the eye closure time before a warning message is shown on screen

Why this project

This project was developed as part for a final group project for the course of Computer Vision and Cognitive Systems done at the University of Modena and Reggio Emilia in the second semester of the academic year 2020/2021. Given the possible applications of Computer Vision, we wanted to focus mainly on the automotive field, developing a useful and potential life saving proof of concept project. In fact, sadly, many fatal accidents happens because of the driver distraction.

License and Contacts

This project is freely available under the MIT license. You can use/modify this code as long as you include the original license present in this repository in it.

For any question or if you want to contribute to this project, feel free to contact me or open a pull request.

Improvements to make