Drowsiness Alert System Thesis
Drowsiness Alert System Thesis
thesis Submitted
BACHELOR OF TECHNOLOGY
in
COMPUTER SCIENCE & ENGINEERING
BY
PROJECT GUIDE
Mr.K. BHASKER PRAKASH
(Assistant Professor)
CERTIFICATE
The success of any course depends mostly on the teachers who us. Only good teaching
can interpret the syllabus and produce desirable changes and competent citizens. This one was a
team effort and many people whose names do not appear on the cover deserve credit. First, we
thank god almighty for his manifold mercies in carrying out of our project successfully.
We heart fully thank our Principal, Dr. K. SHANKAR, Professor and Head of the
Department of Computer Science & Engineering Dr. Lt. RAVINDRA BABU KALLAM,
project coordinators Mr. D. PRASHANTH KUMAR Assistant Professor in CSE for providing
all the resources in completing our project.
We thank our internal guide Mr. K. BHASKER PRAKASH, Assistant Professor in
Department of Computer Science & Engineering, for his guidance and support.
We also thank other teaching and non-teaching staff of Kamala Institute of Technology
and Science, for supporting us in every stage of our major project, entitled “DROWSINESS
ALERT SYSTEM USING DEEP LEARNING’’.
We thank our Parents and Friends for their moral support throughout the project which
helped to strengthen our will.
i
ABSTRACT
Now-a-days, more and more professions require long-term concentration for driving.
Drivers must keep a close eye on the road, so they can react to sudden events immediately.
Driver fatigue often becomes a direct cause of many traffic accidents. Therefore, there is a need
to develop the systems that will detect and notify a driver of her/him bad psychophysical
condition, which could significantly reduce the number of fatigue-related car accidents.
The aim of this project is to develop a prototype drowsiness detection system. The focus
will be placed on designing a system that will accurately monitor the open or closed state of the
driver’s eyes in real-time.
By monitoring the eyes, it is believed that the symptoms of driver fatigue can be detected
early enough to avoid a car accident. Detection of fatigue involves the observation of eye
movements and blink patterns in a sequence of images of a face.
ii
LIST OF FIGURES
Title Page no
2.3 Module description
2.3.1 Facial Landmarks 03
2.3.2 Formula For Eye Aspect Ratio 04
3.1 Block Diagram 07
3.2 DFD Diagrams
3.2.1 Context level DFD 09
3.2.2 Top level DFD 09
3.2.3 Detailed level DFD 10
3.3 UML Diagrams
3.3.1 Use case Diagram 11
3.3.2 Sequence Diagram 12
3.3.3 Collaboration Diagram 13
3.3.4 Activity Diagram 14
4.3 Types of Pooling 18
6.1 Drowsiness detection
6.1.1Screenshot for Eye Detection 29
6.1.2 Screenshot for Drowsiness detection 29
6.1.3 Screenshot for Drowsiness alert 30
6.2 Yawn Detection
6.2.1 Screenshot for Mouth Detection 30
6.2.2 Screenshot for Yawn Detection 31
iii
CHAPTER 1
INTRODUCTION
ABOUT PROJECT:
In this project, we have used deep learning to predict drowsiness of the driver. Deep
learning is an artificial intelligence function that imitates the workings of the human brain in
processing data and creating patterns for use in decision making. Deep learning is a subset
of machine learning in artificial intelligence (AI) that has networks capable of learning
unsupervised from data that is unstructured or unlabeled.
Driver fatigue is one of the major causes of accidents in the world. Detecting the
drowsiness of the driver is one of the surest ways of measuring driver fatigue. In this project we
aim to develop a prototype drowsiness detection system. This system works by monitoring the
eyes of the driver and sounding an alarm when he/she is drowsy.
In this project the eye blink of the driver is detected. If the drivers eyes remain closed for
more than a certain period of time, the driver is said to be drowsy and an alarm is sounded. The
programming for this is done in OpenCV using the Haarcascade library for the detection of facial
features.
EXISTING SYSTEM:
• There is no such application which continuously keep track of drowsiness of the driver.
PROPOSED SYSTEM:
• We proposed a different approach in providing security for driver using drowsiness
detection. By this project, once the face is detected, we have to extract the eye regions.
• We have to compute the eye aspect ratio (EAR), to determine if the eyes are closed..If the
eye aspect ratio indicates that the eyes have been closed for a sufficiently long enough
amount of time, we’ll sound an alarm to wake up the drive
1
CHAPTER 2
ANALYSIS
2.1 HARDWARE AND SOFTWARE REQUIREMENTS
Software specifications of the system that is used in developing this application are:
• Operating System: WINDOWS or UBUNTU.
• Programming Language: Python.
• Tools Used: OpenCV.
2.1.2 HARDWARE REQUIREMENTS (PREFERABLE):
Hardware specifications of the system that is used in this project are:
• Processor: i3 or above.
• RAM: 2 GB or above.
2
2.3 MODULES DESCRIPTION:
Module: - A module allows you to logically organize your Python code. Grouping related code
into a module makes the code easier to understand and use. A module is a Python object with
arbitrarily named attributes that you can bind and reference.
The following modules are involved. They are:
OpenCV
Drowsiness Detection
Yawn Detection
Sound
OpenCV:-
Open CV is used to analyze the detected object from camera.Open CV-python makes use of
Numpy, which is a highly optimized library for numerical operations.Open CV is used for image
processing and video processing
Drowsiness Detection: -
Once the face is detected, we have to extract the eye regions.We have to compute the eye aspect
ratio(EAR),to determine if the eyes are closed..If the eye aspect ratio indicates that the eyes have
been closed for a sufficiently long enough amount of time, we’ll sound an alarm to wake up the
driver.
There are 68 facial landmarks
Yawn Detection
Yawn is detected by distance between the upper lip and bottom lip. Here calculated value is the
absolute distance of the upper lip and bottom lip values.
If the calculated value is greater than the threshold value, then the driver gets the alert message
and it warns background automatically.
Sound
When the drowsiness of the driver is detected successfully background alarm will be played.The
playsound module provides access to the basic sound-playing machinery provided by Windows
platforms.
2.4Modules In Python
2.4.1 OpenCV: -
4
Python works; it is a Python wrapper around original C++ implementation.And the support of
NumPy makes the task easier. NumPy is a highly optimized library for numerical operations. It
gives MATLAB-style syntax. All the OpenCV array structures are converted to-and-from
NumPy arrays. So whatever operations you can do in NumPy, you can combine it with OpenCV,
which increases number of weapons in your arsenal. Besides that, several other libraries like
SciPy, Matplotlib which supports NumPy can be used with this. So OpenCV-Python is an
appropriate tool for fast prototyping of computer vision problems.
5
NumPy is a Python package. It stands for 'Numerical Python'. It is a library consisting of
multidimensional array objects and a collection of routines for processing of array. Numeric, the
ancestor of NumPy, was developed by Jim Humulin.
Another package Numara was also developed, having some additional functionality. In
2005, Travis Oliphant created NumPy package by incorporating the features of Numara into
Numeric package. There are many contributors to this open source project.
Operations using NumPy: -
Using NumPy, a developer can perform the following operations:
Mathematical and logical operations on arrays.
Fourier transforms and routines for shape manipulation.
Operations related to linear algebra. NumPy has in-built functions for linear algebra and
random number generation.
2.4.3 Scipy: -
SciPy is a collection of mathematical algorithms and convenience functions built on the
Numpy extension of Python. It adds significant power to the interactive Python session by
providing the user with high-level commands and classes for manipulating and visualizing data.
With SciPy an interactive Python session becomes a data-processing and system-prototyping
environment rivaling systems such as MATLAB, IDL, Octave, R-Lab, and SciLab.
The additional benefit of basing SciPy on Python is that this also makes a powerful
programming language available for use in developing sophisticated programs and specialized
applications. Scientific applications using SciPy benefit from the development of additional
modules in numerous niches of the software landscape by developers across the world.
Everything from parallel programming to web and data-base subroutines and classes have been
made available to the Python programmer. All of this power is available in addition to the
mathematical libraries in SciPy.
6
CHAPTER 3
DESIGN
7
about timing of processes, or information about whether processes will operate in sequence or
parallel. A DFD is also called as “bubble chart”.
DFD Symbols:
In the DFD, there are four symbols:
• A square defines a source or destination of system data.
• An arrow indicates dataflow. It is the pipeline through which the information flows.
Process: People, procedures or devices that use or produce (Transform) data. The physical
component is not identified.
Sources: External sources or destination of data, which may be programs, organizations or other
entity.
In our project, we had built the data flow diagrams at the very beginning of business
process modelling in order to model the functions that our project has to carry out and the
interaction between those functions together with focusing on data exchanges between processes.
8
3.2.1 Context level DFD:
A Context level Data flow diagram created using select structured systems analysis and
design method (SSADM). This level shows the overall context of the system and its operating
environment and shows the whole system as just one process. It does not usually show data
stores, unless they are “owned” by external systems, e.g. are accessed by but not maintained by
this system, however, these are often shown as external entities.
The Context Level Data Flow Diagram shows the data flow from the application to the database
and to the system.
3.2.2 Top level DFD:
A data flow diagram is that which can be used to indicate the clear progress of a business
venture. In the process of coming up with a data flow diagram, the level one provides an
overview of the major functional areas of the undertaking. After presenting the values for most
important fields of discussion, it gives room for level two to be drawn.
10
• Structural Model View
i. In this model the data and functionality are arrived from inside the system.
ii. This model view models the static structures.
• Behavioural Model View
It represents the dynamic of behavioral as parts of the system, depicting the
interactions of collection between various structural elements described in the user model
and structural model view.
• Implementation model View
In this the structural and behavioral as parts of the system are represented as
they are to be built
• Environmental Model View
In this the structural and behavioral aspects of the environment in which the system is
to be implemented are represented.
3.3.1 USECASE DIAGRAM:
Use case diagrams are one of the five diagrams in the UML for modeling the dynamic
aspects of the systems (activity diagrams, sequence diagram, state chart diagram, collaboration
diagram are the four other kinds of diagrams in the UML for modeling the dynamic aspects of
systems).use case diagram are central to modeling the behavior of the system, a sub-system, or
a class. Each one shows a set of use cases and actors and relations.
11
Figure 3.3.1 USECASE DIAGRAM
12
The figure 3.3.2 represents the sequence diagram for our application which relates the messages
or the actions to be performed between User and Application. The Life time of particular object
is shown in the Diagram.
3.3.3 COLLABORATION DIAGRAM:
Collaboration diagram is an interaction diagram that emphasizes the structural
organization of the objects that send and receive messages. Collaboration diagram and
sequence diagram are isomorphic.
A collaboration diagram is an interaction diagram that emphasizes the structural
organization of the objects that send and receive messages. Collaboration diagrams are
isomorphic, meaning that you can take one and transform it into the other.
The collaboration diagram is also called as communication diagram or interaction
diagram, is an illustration of the relationships and interactions among software objects in the
unified modelling language.
It represents the structural organization of the system.
Structural organization consists of objects and links.
In the collaboration diagram, the method call sequence is indicated by some numbering
technique.
This figure shows the Collaboration Diagram for this application. In this, users and
organizations can perform Collaboration of actions.
13
3.3.4 Activity Diagram:
The process flows in the system are captured in the activity diagram similar to a state diagram
an activity diagram also consists of activities, action, transitions
Initial and final states and guard conditions
Processes involving different use case are show in work flows.
The figure shows the activity diagram of our application. In this, users and organizations
can perform actions.
14
CHAPTER 4
IMPLEMENTATION
Technologies Used:
4.1: What is Python?
Python is an interpreter, high-level programming language for general-purpose
programmingby “Guido van Rossum” and first released in 1991, Python has a design philosophy
that emphasizes code readability, and a syntax that allows programmers to express concepts in
fewer lines of code, notably using significant whitespace. It provides constructs that enable clear
programming on both small and large scales. Python features a dynamic type system and
automatic memory management. It supports multiple programming paradigms, including object-
oriented, imperative, functional, procedural, and has a large and comprehensive standard library.
Python interpreters are available for many operating systems. Python, the reference
implementation of Python, is open source software and has a community-based development
model, as do nearly all of its variant implementations. Python is managed by the non-profit
Python Software Foundation.
Python is a general purpose, dynamic, high level and interpreted programming language. It
supports object-oriented programming approach to develop applications. It is simple and easy to
learn and provides lots of high level data structures.
• Windows XP
• Python Programming
• Open source libraries: Pandas, NumPy, SciPy, matplotlib, OpenCV
Python Versions
Python 2.0 was released on 16 October 2000 and had many major new features, including
a cycle-detecting, garbage collector, and support for Unicode. With this release, the development
process became more transparent and community-backed.
Python 3.0 (initially called Python 3000 or py3k) was released on 3 December 2008 after
a long testing period. It is a major revision of the language that is not completely backward-
compatible with previous versions. However, many of its major features have been back ported
to the Python 2.6.xand 2.7.x version series, and releases of Python 3 include the 2to3 utility,
which automates the translation of Python 2 code to Python 3.
15
Python 2.7's end-of-life date (a.k.a. EOL, sunset date) was initially set at 2015, then postponed to
2020 out of concern that a large body of existing code could not easily be forward-ported to
Python 3.
In January 2017, Google announced work on a Python 2.7 to go Tran’s compiler to improve
performance under concurrent workloads.
Python 3.6 had changes regarding UTF-8 (in Windows, PEP 528 and PEP 529) and Python
3.7.0b1 (PEP 540) adds a new "UTF-8 Mode" (and overrides POSIX locale).
WHY PYTHON?
Python is a scripting language like PHP, Perl, and Ruby.
No licensing, distribution, or development fees
It is a Desktop application.
Linux, windows
Excellent documentation
Thriving developer community
For us job opportunity
16
Semi-supervised learning: The computer is given only an incomplete training signal: a
trainingset with some (often many) of the target outputs missing.
Active learning: The computer can only obtain training labels for a limited set of
instances(based on a budget), and also has to optimize its choice of objects to acquire labels for.
When used interactively, these can be presented to the user for labelling.
Reinforcement learning: Training data (in form of rewards and punishments) is given only
asfeedback to the program's actions in a dynamic environment, such as driving a vehicle or
playing a game against an opponent.
Unsupervised learning: No labels are given to the learning algorithm, leaving it on its own
tofind structure in its input. Unsupervised learning can be a goal in itself (discovering hidden
patterns in data) or a means towards an end (feature learning).
In regression, also a supervised problem, the outputs are continuous rather than discrete.
17
Convolution layer:
The objective of the Convolution Operation is to extract the high-level features such as edges,
from the input image. ConvNets need not be limited to only one Convolutional Layer.
Conventionally, the first ConvLayer is responsible for capturing the Low-Level features such as
edges, color, gradient orientation, etc. With added layers, the architecture adapts to the High-
Level features as well, giving us a network which has the wholesome understanding of images in
the dataset, similar to how we would.
Pooling Layer :
Similar to the Convolutional Layer, the Pooling layer is responsible for reducing the spatial size
of the Convolved Feature. This is to decrease the computational power required to
processthedata through dimensionality reduction. Furthermore, it is useful for extracting
dominantfeatureswhich are rotational and positional invariant, thus maintaining the process of
effectively training of the model.
There are two types of Pooling: Max Pooling and Average Pooling. Max Pooling returns
the maximum value from the portion of the image covered by the Kernel. On the other
hand, Average Pooling returns the average of all the values from the portion of the image covered
by the Kernel.
Max Pooling also performs as a Noise Suppressant. It discards the noisy activations altogether
and also performs de-noising along with dimensionality reduction. On the other hand, Average
Pooling simply performs dimensionality reduction as a noise suppressing mechanism. Hence, we
can say that Max Pooling performs a lot better than Average Pooling.
18
Figure 4.3Types of Pooling
The Convolutional Layer and the Pooling Layer together form the i-th layer of a
Convolutional Neural Network. Depending on the complexities in the images, the number of
such layers may be increased for capturing low-levels details even further, but at the cost of more
computational power.
After going through the above process, we have successfully enabled the model to understand
the features. Moving on, we are going to flatten the final output and feed it to a regular Neural
Network for classification purposes.
RELU layer :
ReLU is the abbreviation of rectified linear unit, which applies the non-
saturating activationfunction.It effectively removes negative values from an activation map by
setting them to zero
Now that we have converted our input image into a suitable form for our Multi-Level
Perceptions, we shall flatten the image into a column vector. The flattened output is fed to a
feed-forward neural network and back propagation applied to every iteration of training. Over a
series of epochs, the model is able to distinguish between dominating and certain low-level
features in images and classify them using the Softmax Classification technique.
CODE:
Code for drowsiness detection:
20
vs = VideoStream(src=args["webcam"]).start()
time.sleep(1.0)
while True:
frame = vs.read()
frame = imutils.resize(frame, width=450)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
rects = detector(gray, 0)
for rect in rects:
shape = predictor(gray, rect)
shape = face_utils.shape_to_np(shape)
leftEye = shape[lStart:lEnd]
rightEye = shape[rStart:rEnd]
leftEAR = eye_aspect_ratio(leftEye)
rightEAR = eye_aspect_ratio(rightEye)
ear = (leftEAR + rightEAR) / 2.0
leftEyeHull = cv2.convexHull(leftEye)
rightEyeHull = cv2.convexHull(rightEye)
cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1)
cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1)
if ear < EYE_AR_THRESH:
COUNTER += 1
if COUNTER >= EYE_AR_CONSEC_FRAMES:
if not ALARM_ON:
ALARM_ON = True
# check to see if an alarm file was supplied,
# and if so, start a thread to have the alarm
# sound played in the background
if args["alarm"] != "":
t = Thread(target=sound_alarm,
args=(args["alarm"],))
t.deamon = True
t.start()
# draw an alarm on the frame
21
cv2.putText(frame, "DROWSINESS ALERT!", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
import cv2
import numpy as np
import dlib
from playsound import playsound
path="C:/Users/NIHARIKA/Desktop/Yawn_Detector/shape_predictor_68_face_landmarks.dat"
predictor=dlib.shape_predictor(path)
detector=dlib.get_frontal_face_detector()
def get_landmarks(im):
22
rects=detector(im,1)#image and no.of rectangles to be drawn
if len(rects)>1:
print("Toomanyfaces")
return np.matrix([0])
if len(rects)==0:
print("Toofewfaces")
return np.matrix([0])
return np.matrix([[p.x,p.y] for p in predictor(im,rects[0]).parts()])
def place_landmarks(im,landmarks):
im=im.copy()
for idx,point in enumerate(landmarks):
pos=(point[0,0],point[0,1])
cv2.putText(im,str(idx),pos,fontFace=cv2.FONT_HERSHEY_SCRIPT_SIMPLEX,fontScale=0.
3,color=(0,255,255))
cv2.circle(im,pos,3,color=(0,255,255))
return im
def upper_lip(landmarks):
top_lip=[]
for i in range(50,53):
top_lip.append(landmarks[i])
for j in range(61,64):
top_lip.append(landmarks[j])
top_lip_point=(np.squeeze(np.asarray(top_lip)))
top_mean=np.mean(top_lip_point,axis=0)
return int(top_mean[1])
def low_lip(landmarks):
lower_lip=[]
for i in range(65,68):
lower_lip.append(landmarks[i])
for j in range(56,59):
lower_lip.append(landmarks[j])
lower_lip_point=(np.squeeze(np.asarray(lower_lip)))
lower_mean=np.mean(lower_lip_point,axis=0)
23
return int(lower_mean[1])
def decision(image):
landmarks=get_landmarks(image)
if(landmarks.all()==[0]):
return -10#Dummy value to prevent error
top_lip=upper_lip(landmarks)
lower_lip=low_lip(landmarks)
distance=abs(top_lip-lower_lip)
return distance
cap=cv2.VideoCapture(0)
yawns=0
while(True):
ret,frame=cap.read()
if(ret==True):
landmarks=get_landmarks(frame)
if(landmarks.all()!=[0]):
l1=[]
for k in range(48,60):
l1.append(landmarks[k])
l2=np.asarray(l1)
lips=cv2.convexHull(l2)
cv2.drawContours(frame, [lips], -1, (0, 255, 0), 1)
distance=decision(frame)
if(distance>21): #Use distance according to your convenience
yawns=yawns+1
if(yawns>1):
24
playsound('C://Users/NIHARIKA/Music/abc.mp3')
cv2.imshow("Subject Yawn Count",frame)
if cv2.waitKey(1)==13:
break
else:
continue
cap.release()
cv2.destroyAllWindow
25
CHAPTER 5
TESTING
26
Unit Testing: In which each unit (basic component) of the software is tested to verify
thatthe detailed design for the unit has been correctly implemented
Integration testing: In which progressively larger groups of tested software component
scorresponding to elements of the architectural design are integrated and tested until the
software works as a whole.
System testing: In which the software is integrated to the overall product and tested
toshow that all requirements are met
27
For example, if a user tries to type a letter in a numeric field, the correct behaviour in this
case would be to display the “Incorrect data type, please enter a number” message.
The purpose of negative testing is to detect such situations and prevent applications from
crashing.
Also, negative testing helps you improve the quality of your application and find its weak
points.
28
CHAPTER 6
RESULTS
After performing testing we get certain results. The results obtained from testing as
shown below. Results of the system can be expressed and evaluated in terms of output screen.
The output screen can be used to show the objectives set at the beginning are achieved at the end.
29
Figure 6.1.3 Screenshot for Drowsiness Alert
30
Figure 6.2.2 Screenshot for Yawn Detection
31
CHAPTER 7
LIMITATIONS
With poor lighting conditions even though face is easily detected, sometimes the system
is unable to detect the eyes.
The distance between face and webcam is not at optimum range then certain problems are
arising.
Even though the driver wears glasses the system fails to detect eyes which is the most
significant drawback of our system.
32
CHAPTER 8
CONCLUSION
The system proposed in this analysis provides accurate detection of driver fatigue.
This application can be implemented in the real time to reduce traffic accidents rate due
to drowsy drivers and it can also help drivers to stay awake when driving by giving a
warning when the driver is sleepy
.
33
CHAPTER 9
34
BIBLIOGRAPHY
[1].Modern Control systems, Dorf&Bishop–Princeton Hall, ISBN: 0133-70-285, 2008
[2].A Practitioners Guide to Software, Roger S.Pressman8th Edition –McGraw-Hill, ISBN: 978-
0-07-8022128, 2014
[3].The Unified Modeling Language User Guide, Low Price Edition Grady Booch, James
Rumbaugh, IvarJacob, ISBN: 81-7808-769-5, 91, 1997
[7]. Black_Box Testing: Techniques for Functional Testing of Software and Systems
Boris Beizer - Wiley Publications, ISBN: 978-0-471-120-940, 1995.
35
36