Driver Drowsiness Detector Using Machine Learning (Report) Completed
Driver Drowsiness Detector Using Machine Learning (Report) Completed
MACHINE LEARNING
A MINI PROJECT REPORT
Submitted by
APRIL 2024
ANNA UNIVERSITY: CHENNAI-600025
BONAFIDE CERTIFICATE
Certified that this project report “driver drowsiness detection using machine
SIGNATURE SIGNATURE
First and foremost, we would like to extend our heartfelt respect and gratitude
to Management, Director Dr. B. Chidhambara Rajan, M.E., Ph.D., Principal
Dr. M. Murugan, and vice principal Dr. S. Visalakshi, M.E., Ph.D., who
helped us in our endeavour’s.
We would also thank all the Teaching and Non-Teaching staff members of
our department for their constant support and encouragement throughout the
course of this project work.
Finally, the constant support from our lovable Parents and Friends is untold
and immeasurable.
ABSTRACT
Drowsiness and tiredness are primary factors contributing to traffic accidents.
Precautions can be taken to avoid them, such as ensuring sufficient sleep before
driving, having coffee or an energy drink, or taking a break when drowsiness is
noticed. Although the conventional method for drowsiness detection involves
intricate techniques like EEG and ECG, it is highly accurate but requires contact
sensors and has limitations in monitoring driver fatigue and drowsiness in real-
time driving situations. This paper presents an alternative approach to
identifying signs of drowsiness in drivers by assessing the rate of eye closure
and yawning. The study details the process of detecting eyes and mouth
movements from video recordings of an experiment carried out by IIROS
(Indian Institute of Road Safety). The participant engages in a driving
simulation while being filmed by a webcam positioned in front of the simulator.
The video footage captures the progression from alertness to fatigue and
eventually drowsiness. The system is designed to recognize facial features in
these images, focusing on isolating the eyes and mouth. By detecting the face
area, the program can identify and analyse the eyes and mouth through
algorithms for detecting the left and right eyes and the mouth. The analysis of
eye and mouth movements is based on frames extracted from the video footage.
The positions of the eyes and mouth can be detected using this method. When
the eyes are located, changes in their intensity serve as indicators of open or
closed eyes. If the eyes remain closed for four consecutive frames, it indicates
that the driver is experiencing drowsiness.
ABSTRACT
LIST OF TABLES
LIST OF FIGURES
LIST OF ABBRIVATIONS
1 INTRODUTION
1.1 Introduction
1.2 Objective
1.3 Significance of project
1.4 Background of study
1.5 Problem statement
1.6 Scope of study
1.7 Relevancy of the project
LITERATURE REVIEW
2
Detection
4 METHODOLOGY
4.1 Functional requirement
4.2 Non-functional requirement
4.3 System configuration
4.3.1 Software requirements
4.3.2 Hardware requirements
4.4 Technology used
4.5 Packages used
4.5.1 Open cv
4.5.1.1 Opencv-python
4.5.2 Numpy
4.5.2.1 What is numpy
4.5.2.2 Why use numpy
4.5.2.3 Why is numpy faster than lists
4.5.2.4 Which language is numpy
written
4.5.3 Pandas
4.5.3.1 Working with pandas
4.5.4 Tensorflow
4.5.4.2 What is tensorflow and how it
is used
4.5.4.3 Why is it Called tensorflow
4.5.4.4 What is tensorflow backend
IMPLEMENTATION
5.1 Important libraries
5
5.2 Driver alertness monitoring dataset
5.3 Face detection
5.4 Eye state tracking
5.5 Yawning detection
5.6 Alert system module
5.7 Head tilt alert
5.8 Real time monitoring module
5.9 Data logging and storage module
7.1 Result
8
APPENDIX
SOURCE CODE
REFERENCE
LIST OF TABLES
FIGURE NO DESCRIPTION PAGE NO
ABBREVATIONS EXPANSION
EEG Electroencephalography
PERCLOS percentage of eye closure
LIST OF ABBREVATIONS
CHAPTER 01
INTRODUCTION
1.1 INTRODUCTION
Nowadays the driver’s safety in the car is one of the most wanted
systems to avoid accidents. Our goal of the undertaking is to ensure the security
framework. For upgrading the wellbeing, we are identifying the eye flickers of
the driver and assessing the driver's status and controlling the vehicle likewise.
The venture primarily centres around these targets:
2. To examine eyes and mouth from the video pictures of the members in the
trial of driving reenactment led by IIROS that can be utilized as a marker of
exhaustion and tiredness.
Determine the drowsiness from these parameters - Eye blink - - Area of the
pupils detected at eyes Yawning
Subsequent to going through the exploration papers and the current techniques,
this task suggested that eyes and yawning location strategies will be utilized.
Eye flicker term gives the information that the more extended the individuals
nearby their eyes, the drowsier it. PERCLOS technique recommends that
languor is estimated by ascertaining the level of the eyelid 'hangs'. Sets of eyes
open and eye shut have been put away in the product library to be utilized as a
boundary to separate whether the eyes is completely open or completely shut.
For eyelids to hang, it occurs in much more slow time as the individual is
gradually nodding off. Consequently, the progress of the driver's laziness can be
recorded.
A use case diagram illustrates the interactions between actors (users or external
systems) and a system to achieve specific goals. In context of driver drowsiness
detector, use case diagram represents interaction between driver and the system.
A data flow diagram (DFD) is a graphical representation that illustrates the flow
of data within a system or process. It visually depicts how data moves through
various processes, data stores, and external entities in a system. DFDs are
commonly used in system analysis and design to model the data flow and
transformations within a system, helping to understand and document its
functionality. In the context of driver drowsiness detection, a data flow diagram
(DFD) would illustrate how video frames captured from a camera are processed,
analysed, and used to generate alerts when drowsiness is detected.
• Continuous evaluation of driver’s facial features over the course of long trip.
Non-functional requirements are basically the quality constraints that the system
must satisfy according to the project contract. These are also called non-
behavioural requirements.
• Language: Python 3
• RAM: 4 GB or more
• Camera: A webcam
•OpenCV-Python is the Python API for OpenCV, solidifying the best attributes
of the OpenCV C++ API and the Python language.
4.5.1.1 OPENCV-PYTHON
•Compared to tongues like C/C++, Python is all the more lethargic. Taking
everything into account, Python can be successfully loosened up with C/C++,
which grants us to form computationally genuine code in C/C++ and make
Python covers that can be used as Python modules. This gives us two
advantages: first, the code is practically pretty much as fast as the principal
C/C++ code (since it is the genuine C++ code working in establishment) and
second, it is easier to code in Python than C/C++. OpenCV-Python is a Python
covering for the primary OpenCV C++ execution.
4.5.2 NUMPY
•NumPy is a Python library that gives a basic yet incredible information
structure: the n-dimensional cluster. This is the establishment on which
practically all the force of Python's information science tool compartment is
fabricated, and learning NumPy is the initial step on any Python information
researcher's excursion.
•In Python we have records that fill the need of clusters, yet they are delayed to
measure.
•Arrays are regularly utilized in information science, where speed and assets
are vital.
•NumPy exhibits are put away at one nonstop spot in memory not at all like
records, so cycles can get to and control them productively.
•This is the primary motivation behind why NumPy is quicker than records.
Likewise, it is advanced to work with the most recent CPU engineering.
4.5.3 PANDAS
•Pandas is a Python library. Pandas is utilized to break down information.
Learning by perusing.
•Pandas are a quick, amazing, adaptable and simple to utilize open source
information examination and control device, based on top of the Python
programming language.
Stacking and Saving Data with Pandas at the point when you need to utilize
Pandas for information investigation, you'll as a rule use it in one of three
unique ways:
4.5.4 TENSORFLOW
TensorFlow makes it simple for fledglings and specialists to make AI models
for work area, versatile, web, and cloud.
•Tensor Flow is a Python library for quick mathematical registering made and
delivered by Google. It is an establishment library that can be utilized to make
Deep Learning models straightforwardly or by utilizing covering libraries that
work on the cycle based on top of TensorFlow.
•Created by the Google Brain group, Tensor Flow is an opensource library for
mathematical calculation and huge scope AI. TensorFlow packages together a
huge number of AI and profound learning models and calculations and makes
them helpful via a typical analogy.
The name TensorFlow gets from the tasks that such neural organizations
perform on multidimensional information exhibits, which are alluded to as
tensors. During the Google I/O Conference in June2016, Jeff Dean expressed
that 1500 stores on GitHub referenced Tensor Flow, of which just 5 were from
Google.
CHAPTER 05
IMPLEMENTATION
5.1 IMPORT LIBRARIES
• Numpy is used for handling the data from dlib and mathematical functions.
OpenCV will help us in gathering the frames from the webcam and writing over
them and also displaying the resultant frames
.
• In our program we used Dlib, a pre-trained program trained on the HELEN
dataset to detect human faces using the pre-defined 68 landmarks
• Dlib is an open source toolkit written in c++ that has a variety of machine
learning models implemented and optimized. Preference is given to dlib over
other libraries and training your own model because it is fairly accurate, fast,
well documented, and available for academic, research, and even commercial
use.
• Dlib’s accuracy and speed are comparable with the most state-of-the-art
neural networks, and because the scope of this project is not to train one, we’ll
be using dlib python wrapper.
• Here we prepare our capture call to OpenCV’s video capture method that will
capture the frames from the webcam in an infinite loop till we break it and stop
the capture.
• Keep the downloaded landmark detection .dat file in the same folder as this
code file or provide a complete path in the dlib.shape_predictor function.
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
• As we are going to use this more than once in a call, we create a separate
function for this.
The initial focus lies on gathering the necessary data for our drowsiness
detection system. Leveraging the dlib library, we process the video feed
frame by frame to identify and isolate crucial facial features such as the
left and right eyes.
The E.A.R. serves as a critical metric for assessing the level of eye
closure, while the Mouth Aspect Ratio helps in monitoring mouth
movements, providing additional insights into the driver's state of
alertness.
This technique involves separating the skin tones from the rest of the
image, effectively isolating the facial regions of interest. By leveraging
the YCbCr color space, which separates luminance (Y) from chrominance
(Cb and Cr), skin segmentation can be achieved with greater accuracy
compared to other color spaces.
This process results in the extraction of the skin areas while discarding a
significant portion of non-face elements present in the image. Through
skin segmentation, the system can effectively filter out irrelevant
background noise, ensuring that subsequent analyses are performed
exclusively on the facial features of interest.
In the Eye State Analysis phase, the primary objective is to isolate and
analyse the state of each eye with a focus on symmetrical properties. This
process begins with the separation of both eyes using edge identification
techniques, which prioritize the preservation of symmetrical features.
Edge detection is crucial for accurately delineating the boundaries of the
eyes and capturing subtle changes in pixel intensities indicative of eye
movements.
To achieve this, the Sobel operator is employed for edge discovery due to
its effectiveness in avoiding image blurring while maintaining clear and
distinct edges. The Sobel operator operates by convolving the image with
a pair of kernels, which effectively capture gradients in the horizontal and
vertical directions. This approach ensures that edges are accurately
identified while preserving image clarity.
left_point = (face_roi_landmark.part(eye_landmark[0]).x,
face_roi_landmark.part(eye_landmark[0]).y)
right_point = (face_roi_landmark.part(eye_landmark[3]).x,
face_roi_landmark.part(eye_landmark[3]).y)
center_top = mid(face_roi_landmark.part(eye_landmark[1]),
face_roi_landmark.part(eye_landmark[2]))
center_bottom = mid(face_roi_landmark.part(eye_landmark[5]),
face_roi_landmark.part(eye_landmark[4]))
Similarly, we define the mouth ratio function for finding out if a person is
yawning or not. This function gives the ratio of height to width of mouth.
If height is more than width it means that the mouth is wide open.
For this as well we use a series of points from the dlib detector to find the
ratio.
def mouth_aspect_ratio(lips_landmark, face_roi_landmark): left_point =
(face_roi_landmark.part(lips_landmark[0]).x,
face_roi_landmark.part(lips_landmark[0]).y) right_point =
(face_roi_landmark.part(lips_landmark[2]).x,
face_roi_landmark.part(lips_landmark[2]).y) center_top =
(face_roi_landmark.part(lips_landmark[1]).x,
face_roi_landmark.part(lips_landmark[1]).y) center_bottom =
(face_roi_landmark.part(lips_landmark[3]).x,
face_roi_landmark.part(lips_landmark[3]).y) hor_line_length =
hypot((left_point[0] - right_point[0]), (left_point[1] - right_point[1]))
ver_line_length = hypot((center_top[0] - center_bottom[0]), (center_top[1] -
center_bottom[1])) if hor_line_length == 0: return ver_line_length ratio =
ver_line_length / hor_line_length return ratio
• We create a counter variable to count the number of frames the eye has been
close for or the person is yawning and later use to define drowsiness in driver
drowsiness detection system project
The extracted features are then input into the trained machine learning
model, which has been previously trained on labeled datasets to recognize
patterns associated with drowsiness. The model employs algorithms such
as Convolutional Neural Networks (CNNs) or Support Vector Machines
(SVMs) to make real-time predictions about the driver's state of alertness.
Based on the predictions made by the machine learning model, the system
initiates appropriate actions to alert the driver or trigger precautionary
measures. Figure 5.14: Alert Emerges This may include issuing auditory
or visual alerts, activating haptic feedback mechanisms, or sending
notifications to the driver or vehicle control systems.
Figure 5.16: Real Time Monitoring
In the driver drowsiness detection system, the analysis and storage of data
play essential roles in improving system performance and ensuring
comprehensive evaluation of driver behaviour over time. The Analysis
Data component of the system is responsible for recording various data
points obtained during the drowsiness detection process.
It is the testing of individual software units of the application .it is done after
the completion of an individual unit before integration. This is a structural
testing, that relies on knowledge of its construction and is invasive.
Unit tests perform basic tests at component level and test a specific business
process, application, and/or system configuration. Unit tests ensure that each
unique path of a business process performs accurately to the documented
specifications and contains clearly defined inputs and expected results.
6.3 FUNCTIONAL TEST
Functional tests provide systematic demonstrations that functions tested are
available as specified by the business and technical requirements, system
documentation, and user manuals.
System testing ensures that the entire integrated software system meets
requirements. It tests a configuration to ensure known and predictable results.
An example of system testing is the configuration-oriented system integration
test. System testing is based on process descriptions and flows, emphasizing
pre-driven process links and integration points.
6.5 WHITE BOX TESTING
White Box Testing is a testing in which in which the software tester has
knowledge of the inner workings, structure and language of the software, or at
least its purpose. It is purpose. It is used to test areas that cannot be reached
from a black box level.
o Black Box Testing is testing the software without any knowledge of the
inner workings, structure or language of the module being tested. Black
box tests, as most other kinds of tests, must be written from a definitive
source document, such as Specification or requirements document, such
as specification or requirements document.
Test Results:
All the test cases mentioned above passed successfully. No defects encountered.
TABLE 6.1: Test Case Result 1
import numpy as np
import imutils
import dlib
import cv2
mixer.init()
mixer.music.load("C:\\Users\\kumaravishnu\\Desktop\\mini project\\
music.wav")
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
def mouth_aspect_ratio(mouth):
A = distance.euclidean(mouth[14], mouth[18])
B = distance.euclidean(mouth[12], mouth[16])
C = distance.euclidean(mouth[0], mouth[6])
mar = (A + B) / (2.0 * C)
return mar
image_points = np.array([
shape[8], # Chin
], dtype="double")
model_points = np.array([
])
focal_length = frame_shape[1]
rvec_matrix = cv2.Rodrigues(rotation_vector)[0]
eulerAngles = cv2.decomposeProjectionMatrix(proj_matrix)[6]
return eulerAngles
frame_check = 20
detect = dlib.get_frontal_face_detector()
predict = dlib.shape_predictor("C:\\Users\\kumaravishnu\\Desktop\\mini
project\\shape_predictor_68_face_landmarks.dat")
cap = cv2.VideoCapture(0)
flag = 0
head_turn_flag = 0
eye_closed_flag = 0
yawn_flag = 0
alert_duration = 2 # seconds
while True:
shape = face_utils.shape_to_np(shape)
yaw_angle = eulerAngles[1]
leftEye = shape[lStart:lEnd]
rightEye = shape[rStart:rEnd]
leftEAR = eye_aspect_ratio(leftEye)
rightEAR = eye_aspect_ratio(rightEye)
mouth = shape[mStart:mEnd]
mar = mouth_aspect_ratio(mouth)
rightEyeHull = cv2.convexHull(rightEye)
mouthHull = cv2.convexHull(mouth)
eye_closed_flag += 1
mixer.music.play()
else:
eye_closed_flag = 0
head_turn_flag += 1
mixer.music.play()
yawn_flag += 1
mixer.music.play()
cv2.imshow("Frame", frame)
if key == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
REFERENCES
[1] National Highway Traffic Safety Administration. “Traffic safety facts crash
stats: Drowsy driving 2019,” Oct. 2017. [Online]. Available:
http://www.nhtsa.gov/ri sky driving/drowsy-driving
[2] European New Car Assessment Program. “Euro NCAP 2025 Roadmap,”
Sep. 2019. [Online]. Available: https://cdn.euroncap.com/media/30700/eur
oncap roadmap-2025-v4.pdf
[3] A. Sahayadhas, K. Sundaraj, and M. Murugappan, “Detecting driver
drowsiness based on sensors: A review,” Sensors, vol. 12, no. 12, pp. 6937–
16953, Dec. 2018.
[4] Y. Dong, Z. Hu, K. Uchimura, and N. Murayama, “Driver inattention
monitoring system for intelligent vehicles: A review,” IEEE Trans. Transp.
Syst., vol. 12, no. 2, pp. 596–614, Jun. 2020.
[5] C. Bila, F. Sivrikaya, M. A. Khan, and S. Albayrak, “Vehicles of the future:
A survey of research on safety issues,” IEEE Trans. Intell. Transp. Syst., vol. 18,
no. 5, pp. 1046–1065, 2020.
[6] R. Alharbey, Mohammed M Dessouky, A Sedik, A. I. Siam and M. A.
Elaskily, "Fatigue State Detection for Tired Persons in Presence of Driving
Periods," in IEEE Access, vol. 10, pp. 79403-79418, 2022.
[7] Q. Zhuang, Z. Kehua, J. Wang and Q. Chen, "Driver Fatigue Detection
Method Based on Eye States With Pupil and Iris Segmentation," in IEEE
Access, vol. 8, pp. 173440-173449, 2020.
[8] W. Deng and R. Wu, "Real-Time Driver-Drowsiness Detection System
Using Facial Features," in IEEE Access, vol. 7, pp. 118727-118738, 2019.
[9] M. H. Alkinani, W. Z. Khan and Q. Arshad, "Detecting Human Driver
Inattentive and Aggressive Driving Behavior Using Deep Learning: Recent
Advances, Requirements and Open Challenges," in IEEE Access, vol. 8, pp.
105008-105030, 2020.
[10] A. Altameem, A. Kumar, R. C. Poonia, S. Kumar and A. K. J.Saudagar,
"Early Identification and Detection of Driver Drowsiness by Hybrid Machine
Learning," in IEEE Access, vol. 9, pp. 162805-162819, 2021.