Professional Documents
Culture Documents
IN
CERTIFICATE
VNRVJIET VNRVJIET
5-8-9-18-21-34-LE3
DECLARATION
ACKNOWLEDGEMENT
I would like to express my gratitude and appreciation to all those who gave me the
possibility to complete this report. Special thanks is due to our Professor,
Mr.P.Venkateswara Rao whose help, stimulating suggestions and encouragement
helped us in all time of fabrication process and in writing this report. I also sincerely
thank for the time spent proofreading and correcting our mistakes.
VNRVJIET has helped us transform ourselves from mere amateurs in the field of
Computer Science into skilled engineers capable of handling any given situation in
real time. I am are highly indebted to the institute for everything that it has given us.
We would like to express our gratitude towards the principal of our institute, Dr.
Challa Dhanunjaya Naidu and the Head of the Computer Science & Engineering
Department, Nagini Sabbineni for their kind co- operation and encouragement
which helped us complete the project in the stipulated time.
5-8-9-18-21-34-LE3
Project Objectives :
Driver drowsiness detection is a car safety technology which helps to save
the life of the driver by preventing accidents when the driver is getting
drowsy.
The main objective is to first design a system to detect driver’s drowsiness
by continuously monitoring the eye.
The system works in spite of driver wearing spectacles and in various
lighting conditions.
To alert the driver on the detection of drowsiness by using buzzer or alarm.
Traffic management can be maintained by reducing the accidents.
Project Outcomes : A training and testing model to identify drowsiness.
Description : Drivers face is continuously monitored using a video
camera. In order to detect the drowsiness the first step is
to detect the face using the series of frame shots taken by
the camera. Scores is incremented when eyes are closed
and vice versa when open. Based on the score, the alarm
beeps So that driver can be alerted.
Stages
involved:
Step 1 – Take image as input from a camera.
Step 2 – Detect the face in the image and create a Region of
Interest (ROI).
Step 3 – Detect the eyes from ROI and feed it to the classifier.
Step 4 – Classifier will categorize whether eyes are open or
closed.
Step 5 – Calculate score to check whether the person is
drowsy.
5-8-9-18-21-34-LE3
ABSTRACT
This system is a real time system which captures image continuously and measures the
state of the eye according to the specified algorithm and gives warning if required.
Car accident is the major cause of death in which around 1.3 million people die every
year. Majority of these accidents are caused because of distraction or the drowsiness of
driver. Construction of high-speed highway roads had diminished the margin of error
for the driver. The countless number of people drives for long distance every day and
night on the highway. Lack of sleep or distractions like the phone call, talking with the
passenger, etc may lead to an accident. To prevent such accidents we propose a system
which alerts the driver if the driver gets distracted or feels drowsy. Facial landmarks
detection is used with help of image processing of images of the face captured using the
camera, for detection of distraction or drowsiness. This whole system is deployed on
portable hardware which can be easily installed in the car for use. This project is aimed
towards developing a prototype of drowsiness detection system.
5-8-9-18-21-34-LE3
INDEX
Contents PageNo
1. Introduction........................................................................................ 10
2. Literature............................................................................................ 11
3. Requirements....................................................................................... 13
4. Proposed System................................................................................. 16
5. .Model Implementation.......................................................................17
6.Testing...................................................................................................24
7.Conclusion............................................................................................32
7.1 Execution..................................................................................................32
7.2 Conclusion……………………………………………………………….34
7.4 Bibilography............................................................................35
5-8-9-18-21-34-LE3
LIST OF FIGURES
Contents Page No.
9
5-8-9-18-21-34-LE3
CHAPTER 1
INTRODUCTION
1.1 Introduction
With this Python project, we will be making a drowsiness detection system. A
countless number of people drive on the highway day and night. Taxi drivers,
bus drivers, truck drivers and people traveling long-distance suffer from lack of
sleep. Due to which it becomes very dangerous to drive when feeling sleepy.
The majority of accidents happen due to the drowsiness of the driver. So, to
prevent these accidents we will build a system using Python, OpenCV, and
Keras which will alert the driver when he feels sleepy.
Drowsiness detection is a safety technology that can prevent accidents that are
caused by drivers who fell asleep while driving.
10
5-8-9-18-21-34-LE3
CHAPTER 2
LITERATURE
2.1 Convolution Neural Network (CNN)
A convolutional neural network, or CNN, is a deep learning neural network
sketched for processing structured arrays of data such as portrayals.
CNN are very satisfactory at picking up on design in the input image, such as
lines, gradients, circles, or even eyes and faces.
This characteristic that makes convolutional neural network so robust for
computer vision.
CNN can run directly on a underdone image and do not need any
preprocessing.
A convolutional neural network is a feed forward neural network, seldom with
up to 20.
The strength of a convolutional neural network comes from a particular kind of
layer called the convolutional layer.
CNN contains many convolutional layers assembled on top of each other, each
one competent of recognizing more sophisticated shapes.
With three or four convolutional layers it is viable to recognize handwritten
digits and with 25 layers it is possible to differentiate human faces.
The agenda for this sphere is to activate machines to view the world as humans
do, perceive it in a alike fashion and even use the knowledge for a multitude of
duty such as image and video recognition, image inspection and classification,
media recreation, recommendation systems, natural language processing, etc.
2.2 Extracting features
First we capture the frames from the video source and convert the picture into
grayscale.
Next we are going to extract the features of the face using the haar frontal face
xml file downloaded from github.
link:- https://github.com/opencv/opencv/blob/master/data/haarcascades_cuda/
haarcascade_frontalface_alt.xml
Next we are going to extract the left eye and the right eye from the grayscale
image using the haar left eye and haar right eye classifier.
link:-https://github.com/opencv/opencv/tree/master/data/haarcascades_cuda
Next we extract the features of both the left and the right eye using maxpooling
layer in CNN.
2.3 Performance Metrics
The Confusion matrix is one of the most intuitive and easiest (unless of course,
you are not confused)metrics used for finding the correctness and accuracy of the
model. It is used for Classification problem where the output can be of two or
11
5-8-9-18-21-34-LE3
12
5-8-9-18-21-34-LE3
CHAPTER 3
REQUIREMENTS
Requirements analysis in systems engineering and software engineering,
encompasses those tasks that go into determining the needs or conditions to
meet for a new or altered product or project, taking account of the possibly
conflicting requirements of the various stakeholders, analyzing, documenting,
validating and managing software or system requirements.
tool with the data exploration, interactive execution, deep inspection, and
beautiful visualization capabilities of a scientific package.
OpenCV:
OpenCV is a cross-platform library using which we can develop real-time
computer vision applications. It mainly focuses on image processing, video
capture and analysis including features like face detection and object
detection. In this tutorial, we explain how you can use OpenCV in your
applications.This tutorial has been prepared for beginners to make them
understand the basics of OpenCV library. We have used the Java
programming language in all the examples, therefore you should have a
basic exposure to Java in order to benefit from this tutorial.
Tensor flow:
TensorFlow is an open-source end-to-end platform for creating Machine
Learning applications. It is a symbolic math library that uses dataflow and
differentiable programming to perform various tasks focused on training
and inference of deep neural networks. It allows developers to create
machine learning applications using various tools, libraries, and
community resources.
Currently, the most famous deep learning library in the world is Google’s
TensorFlow. Google product uses machine learning in all of its products to
improve the search engine, translation, image captioning or
recommendations.
Keras:
P Keras is an open-source software library that provides a Python interface
for artificial neural networks. Keras acts as an interface for the TensorFlow
library.
Up until version 2.3, Keras supported multiple backends, including
TensorFlow, Microsoft Cognitive Toolkit, Theano, and PlaidML. As of
version 2.4, only TensorFlow is supported. Designed to enable fast
experimentation with deep neural networks, it focuses on being user-
friendly, modular, and extensible. It was developed as part of the research
effort of project ONEIROS (Open-ended Neuro-Electronic Intelligent
Robot Operating System), and its primary author and maintainer is
François Chollet, a Google engineer. Chollet is also the author of the
XCeption deep neural network model.
Pygame:
Pygame is a Python-based library which is filled with different modules
(files containing code) that are used for developing 2D videogames and
other multimedia programs. Popular among millions of people across the
14
5-8-9-18-21-34-LE3
world, Pygame is suitable for kids learning to code who want to create
something exciting, unique and fun.
15
5-8-9-18-21-34-LE3
CHAPTER 4
PROPOSED SOLUTION
Our model uses machine learning algorithms(convolutional neural networks) to
predict whether the eyes are closed or open and can be implemented easily on
low powered devices such as raspberry pi making it an efficient system for
mass production.
1. Proposed System
16
5-8-9-18-21-34-LE3
CHAPTER 5
MODEL IMPLEMENTATION
17
5-8-9-18-21-34-LE3
5.1 Dataset
18
5-8-9-18-21-34-LE3
return
gen.flow_from_directory(dir,batch_size=batch_size,shuffle=shuffle,color_mode='graysca
le',class_mode=class_mode,target_size=target_size)
BS= 32
TS=(24,24)
train_batch= generator('data/train',shuffle=True, batch_size=BS,target_size=TS)
valid_batch= generator('data/valid',shuffle=True, batch_size=BS,target_size=TS)
return
gen.flow_from_directory(dir,batch_size=batch_size,shuffle=shuffle,color_mode='graysca
le',class_mode=class_mode,target_size=target_size)
20
5-8-9-18-21-34-LE3
import os
from keras.preprocessing import image
import matplotlib.pyplot as plt
import numpy as np
from keras.utils.np_utils import to_categorical
import random,shutil
from keras.models import Sequential
from keras.layers import Dropout,Conv2D,Flatten,Dense, MaxPooling2D,
BatchNormalization
from keras.models import load_model
shuffle=True,batch_size=1,target_size=(24,24),class_mode='categorical' ):
return
gen.flow_from_directory(dir,batch_size=batch_size,shuffle=shuffle,color_mode='graysca
le',class_mode=class_mode,target_size=target_size)
BS= 32
TS=(24,24)
train_batch= generator('data/train',shuffle=True, batch_size=BS,target_size=TS)
valid_batch= generator('data/valid',shuffle=True, batch_size=BS,target_size=TS)
SPE= len(train_batch.classes)//BS
VS = len(valid_batch.classes)//BS
21
5-8-9-18-21-34-LE3
model = Sequential([
Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(24,24,1)),
MaxPooling2D(pool_size=(1,1)),
Conv2D(32,(3,3),activation='relu'),
MaxPooling2D(pool_size=(1,1)),
#32 convolution filters used each of size 3x3
#again
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D(pool_size=(1,1)),
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
model.fit_generator(train_batch,
validation_data=valid_batch,epochs=15,steps_per_epoch=SPE ,validation_steps=VS)
model.save('models/cnnCat2.h5', overwrite=True)
We are going to use adam compiler and we are going to use categorical cross entropy
for estimating the loss.
22
5-8-9-18-21-34-LE3
We are going to set the batch size as 32 and we are going to set the No. of epochs as 15. And also our
learning rate is 0.01
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
model.fit_generator(train_batch,
validation_data=valid_batch,epochs=15,steps_per_epoch=SPE ,validation_steps=VS)
model.save('models/cnnCat2.h5', overwrite=True)
After training the model for 15 epochs, we are going to check for accuracy and if accuracy satisfies
the requirements, we are going to save the model.
23
5-8-9-18-21-34-LE3
CHAPTER 6
TESTING
6.1 MAKING PREDICTIONS
1. import cv2
2. import os
4. import numpy as np
6. import time
7.
8. mixer.init()
9. sound = mixer.Sound('alarm.wav')
10.
14.
15. lbl=['Close','Open']
24
5-8-9-18-21-34-LE3
16.
21. count=0
22. score=0
23. thicc=2
24. rpred=[99]
25. lpred=[99]
26.
27. while(True):
30.
32.
33. faces =
face.detectMultiScale(gray,minNeighbors=5,scaleFactor=1.1,minSize=(25,25))
25
5-8-9-18-21-34-LE3
36.
38.
41.
43. r_eye=frame[y:y+h,x:x+w]
44. count=count+1
51. if(rpred[0]==1):
52. lbl='Open'
53. if(rpred[0]==0):
26
5-8-9-18-21-34-LE3
54. lbl='Closed'
55. break
56.
58. l_eye=frame[y:y+h,x:x+w]
59. count=count+1
63. l_eye=l_eye.reshape(24,24,-1)
66. if(lpred[0]==1):
67. lbl='Open'
68. if(lpred[0]==0):
69. lbl='Closed'
70. break
71.
27
5-8-9-18-21-34-LE3
73. score=score+1
76. else:
77. score=score-1
79.
80. if(score<0):
81. score=0
83. if(score>15):
85. cv2.imwrite(os.path.join(path,'image.jpg'),frame)
86. try:
87. sound.play()
88.
90. pass
28
5-8-9-18-21-34-LE3
91. if(thicc<16):
93. else:
94. thicc=thicc-2
95. if(thicc<2):
96. thicc=2
97. cv2.rectangle(frame,(0,0),(width,height),(0,0,255),thicc)
98. cv2.imshow('frame',frame)
100. break
101. cap.release()
102. cv2.destroyAllWindows()
With a webcam, we will take images as input. So to access the webcam, we made an
infinite loop that will capture each frame. We use the method provided by OpenCV,
cv2.VideoCapture(0) to access the camera and set the capture object (cap). cap.read()
will read each frame and we store the image in a frame variable.
Step 2 – Detect Face in the Image and Create a Region of Interest (ROI)
To detect the face in the image, we need to first convert the image into grayscale as the
OpenCV algorithm for object detection takes gray images in the input. We don’t need
color information to detect the objects. We will be using haar cascade classifier to
detect faces. This line is used to set our classifier face = cv2.CascadeClassifier(‘ path to
our haar cascade xml file’). Then we perform the detection using faces =
29
5-8-9-18-21-34-LE3
The same procedure to detect faces is used to detect eyes. First, we set the cascade
classifier for eyes in leye and reye respectively then detect the eyes using left_eye =
leye.detectMultiScale(gray). Now we need to extract only the eyes data from the full
image. This can be achieved by extracting the boundary box of the eye and then we can
pull out the eye image from the frame with this code.
We are using CNN classifier for predicting the eye status. To feed our image into the
model, we need to perform certain operations because the model needs the correct
dimensions to start with. First, we convert the color image into grayscale using r_eye =
cv2.cvtColor(r_eye, cv2.COLOR_BGR2GRAY). Then, we resize the image to 24*24
pixels as our model was trained on 24*24 pixel images cv2.resize(r_eye, (24,24)). We
normalize our data for better convergence r_eye = r_eye/255 (All values will be
between 0-1). Expand the dimensions to feed into our classifier. We loaded our model
using model = load_model(‘models/cnnCat2.h5’) . Now we predict each eye with our
model
lpred = model.predict_classes(l_eye). If the value of lpred[0] = 1, it states that eyes are
open, if value of lpred[0] = 0 then, it states that eyes are closed.
The score is basically a value we will use to determine how long the person has closed
his eyes. So if both eyes are closed, we will keep on increasing score and when eyes are
open, we decrease the score. We are drawing the result on the screen using
cv2.putText() function which will display real time status of the person.
31
5-8-9-18-21-34-LE3
CHAPTER 7
CONCLUSIO
N
7.1 Driver Drowsiness Detection Execution
Let’s execute drive drowsiness detection system and see the working of our ml project.
To start the project, you need to open a command prompt, go to the directory where
our main file “drowsiness detection.py” exists. Run the script with this command.
The score can be seen as 0 at starting. When the person closes his eyes, score starts
increasing and that can be seen closed eye detection pic. When score is reached to the
target score, It stars giving alarm, indicating driver that he is drowsy.
32
5-8-9-18-21-34-LE3
33
5-8-9-18-21-34-LE3
7.5 Conclusion
In this Python project, we have built a drowsy driver alert system that you can
implement in numerous ways. We used OpenCV to detect faces and eyes using a
haar cascade classifier and then we used a CNN model to predict the status.
7.6 Applications & Future Scope
Applications:
The proposed system is used to avoid various road accidents caused by drowsy
driving.
This system is used for security purpose of a driver to caution the driver if any
fire accident or any gas leakage occur.
Drowsy driver detection methods can form the basis of a system to potentially
reduce the number of crashes related to drowsy driving.
Future Scope
In future we can implement drowsiness detection system in aircraft in order to
alert pilot.
The alcoholic sensor is also used for drunk drivers.
34
5-8-9-18-21-34-LE3
CHAPTER 8
BIBILOGRAPHY
[1] CNN –
https://www.coursera.org/learn/machine-learning/home/welcome
[2] Computer vision-
https://www.udemy.com/course/python-for-computer-vision-with-opencv-and-deep-
learning/
[3] Machine learning:-Machine Learning Engineering Book by Andriy Burkov
[4] Introduction to Linear Regression Analysis
[5] Book by Douglas Montgomery, Elizabeth A. Peck, and G. Geoffrey Vining
[6] Learn Python the Hard Way - Shaw, Zed
[7] Dataset:-
https://www.kaggle.com/shehzadhanif/eyes-open-closed-dataset
35