FYP Report Format - F15
FYP Report Format - F15
Haseeb Ahmad
2504/FET/BSEE/F15
Inamullah khan
2491/FET/BSEE/F15
Supervised by
Engr. Rehan Ahmad
June 2019
Certificate of Approval
It is certified that we have checked the project presented and demonstrated by Haseeb Ahmad
2504-FET/BSEE/F15, Muhammad Abdul Mateen Qazi 2478-FET/BSEE/F15, Inamullah
khan 2491-FET/BSEE/F15 and approved it.
Supervisor
Engr. Rehan Ahmad
Lecturer
Page ii
In the name of Allah (SWT), the most beneficent and the most merciful
Page iii
A BS Final Year Project submitted to the
Department of Electrical Engineering
International Islamic University, Islamabad
In partial fulfillment of the requirements
For the award of the degree of
Bachelor of Science in Electronic Engineering
Page iv
DECLARATION
We hereby declare that this work, neither as a whole nor as a part there of has been copied out
from any source. No portion of the work presented in this report has been submitted in support
of any application for any other degree or qualification of this or any other university or institute
of learning. We further declare that the referred text is properly cited in the references.
Haseeb Ahmad
2504 -FET/BSEE/F15
Inamullah khan
2491-FET/BSEE/F15
Page v
ACKNOWLEDGMENTS
This BS thesis in Electronic Engineering has been conducted at Department of Electrical Engineering,
Faculty of Engineering & Technology, International Islamic University, as part of the degree program.
We would like to thank Engr. Rehan Ahmad for providing us an opportunity to work on this project,
under his supervision and guidance throughout the project. . We would also like to thank Engr. Rehan
Ahmad for his help, efforts and dedicated support throughout the project.
Further we are particularly thankful to almighty Allah and grateful to our parents, brothers and sisters
who always supported and encouraged us during our project and studies at IIUI.
Haseeb Ahmad
Inamullah Khan
Page vi
Project Title: Distracted Driver Detection
Supervised By:
Tools Used:
Anaconda IDE
Python
Raspberry pi
Raspbian OS
Raspberry pi Camera
SD Card Formatter
Page vii
ABSTRACT
For the past few years there is a continuous increase in the number of road accidents worldwide.
According to WHO nearly 1.25 million deaths annually are due to road accidents and nearly one out of
five accidents are mistakes of distracted drivers. Motivated by the performance of Convolutional Neural
Networks in computer vision, we present a CNN based system that not only detects the distracted driver
but also identifies the cause of distraction. We worked to develop an accurate and robust system for
detecting distracted driver and alert him against his distraction. We used Convolution Neural Network
(CNN) to solve this problem. We designed a Convolution Neural Network (CNN) based system that
detects the driver and alert him if necessary. The system consist of ten classes one of them is safe driving
while the remaining are distractions.
Page viii
Table of Contents
CHAPTER 1 ........................................................................................................ 1
Introduction ......................................................................................................... 1
1.1 History ......................................................................................................................... 1
CHAPTER 2 ........................................................................................................ 5
CHAPTER 3 ........................................................................................................ 8
Page ix
3.3.1 Tensor Flow ......................................................................................................... 8
3.3.3 Numpy.................................................................................................................. 9
3.3.4 Matplotlib............................................................................................................. 9
............................................................................................................................. 11
3.5 Results ....................................................................................................................... 12
3.9.3 Numpy................................................................................................................ 19
3.9.4 Matplotlib........................................................................................................... 19
Results ................................................................................................................ 21
Future Recommendations..................................................................................................... 23
References .......................................................................................................... 24
Page x
Page xi
List of Figures
Figure 2: CNN............................................................................................................................ 3
Page xii
List of Tables
Page xiii
List of Abbreviations
Page xiv
CHAPTER 1
Introduction
1.1 History
According to the World Health Organization (WHO) survey, 1.3 million people worldwide die in traffic
accidents each year, making them the eighth leading cause of death and an additional 20-50 million are
injured/ disabled. The report also states that the total number of deaths have risen to 1.46 lakhs in 2015
and driver error is the most common cause behind these traffic accidents. The number of accidents
because of distracted driver has been increasing since few years. National Highway Traffic Safety
Administrator of United States (NHTSA) reports deaths of 3477 people and injuries to 391000 people
in motor vehicle crashes because of distracted drivers in 2015 [2]. In the United States, everyday
approximately 9 people are killed and more than 1,000 are injured in road crashes that are reported to
involve a distracted driver [1]. NTHSA describes distracted driving as “any activity that diverts attention
of the driver from the task of driving” which can be classified into Manual, Visual or Cognitive
distraction [2] [1]. As per the definitions of Center for Disease Control and Prevention (CDC), cognitive
distraction is basically “driver’s mind is off the driving”. In other words, even though the driver is in
safe driving posture, he is mentally distracted from the task of driving. He might be lost in thoughts,
daydreaming etc. Distraction because of inattention, sleepiness, fatigue or drowsiness falls into visual
distraction class where “driver’s eyes are off the road”. Manual distractions are concerned with various
activities where “driver’s hands are off the wheel”. Such distractions include talking or texting using
mobile phones, eating and drinking, talking to passengers in the vehicle, adjusting the radio, makeup
etc.
1.2 Motivation
Distracted driving is an ongoing problem that seems to only be getting worse with the dependence on
technology. Not only is the general public becoming more reliant on vehicles, but also on distractions
like cell phones and GPS systems within them. Most modern day vehicles come with touch screen
navigation that requires some level of interaction to operate. Cellphones, too, are a big part of everyday
life interactions, with constant incoming notifications and calls. These upward trends in dependence
seem to have a correlation to the number of crashes as a result of distracted driving.
It is evident that distracted driving is an ongoing issue, and perhaps the very technology that typically
causes distractions can be used to alleviate them. With the current advances in machine learning and
hardware, computer vision techniques has also followed suit and have shown their powerful
capabilities. This work is a stepping stone in the ultimate goal of using computer vision to help fight
the distracted driver problem. If a system within the vehicle is able to detect if a driver is distracted, it
could perhaps remind the driver to focus their attention on driving. Many drivers may not realize what
they do is taking their attention away from driving, possibly because the distractions are so habitual and
ingrained in their daily routine. The hypothetical distracted driver detection system in the car could,
through feedback, make the drivers consciously aware of their actions, and help correct them over time.
This kind of proposed work would, of course, have to eventually be properly engineered, fine-tuned,
and tested before becoming a successful prevention in distracted driving. However, until then, small
steps need to be taken in solving the problem of distracted driver detection
Page 1
1.3 Machine Learning
Machine learning is an application of artificial intelligence that provides systems the ability to
automatically learn and improve from experience without being explicitly programmed. Machine
learning focuses on the development of computer programs that can access data and use it learn for
themselves.
CNNs rely on the idea that local understanding of an image is good enough, with the practical advantage
of having fewer parameters, as a result reducing the computation time and data required to train the
model. Rather than have a fully connected layer for every pixel, CNNs have enough weights to look at
small parts of the image at a time. The process generally involves a convolution layer, followed by a
pooling and an activation function, but not necessarily in that exact order. These three separate
operations can be applied as separate layers to the original image, generally multiple times. Finally, a
fully connected layer or layers are added at the end in order to classify an image accordingly. With the
highly variable number of combinations and permutations, it can be difficult to find the exact one that
gives the optimal performance. CNN designs are driven by the community and research who thankfully
have made some successful results, and made their CNNs publicly available for others to use and
Page 2
improve upon.
Figure 2: CNN
Page 3
1.4 Existing Systems
Nowadays, Advanced Driver Assistance Systems (ADAS) are being developed to prevent accidents by
offering technologies that alert the driver to potential problems and to keep the car’s driver and
occupants safe if an accident does occur. But even today’s latest autonomous vehicles require the driver
to be attentive and ready to take the control of the wheel back in case of emergency. Tesla autopilot’s
crash with the white truck-trailer in Williston, Florida in May 2016 was the first fatal crash in testing of
autonomous vehicle. Recently in March 2018, Uber’s self-driving car with an emergency backup driver
behind the wheel struck and killed a pedestrian in Arizona. In both of these fatalities, the safety driver
could have avoided the crashes but evidences reveal that he was clearly distracted. This makes detection
of distracted driver an essential part of the self-driving cars as well. We believe that distracted driver
detection is utmost important for further preventive measures. If the vehicle could detect such
distractions and then warn the driver against it, number of road crashes can be reduced.
Page 4
CHAPTER 2
Project Overview
2.1 Related Work
This section summaries review of some of the relevant and significant work from literature for distracted
driver detection. The major cause of manual distractions is usage of cellphones. Many solutions already
exist because this problem was a public challenge hosted by State Farm, an insurance company, on
Kaggle. Many top solutions used pre-trained CNN models and the most popular are VGG-16 and
ResNet, which were state-of-the-art one or two years ago and had been improving. But arrives when
we fit this trained model into the small memory of Raspberry pi because of smaller memory size these
did not help.
The training images come with correct labels and the challenge is to make the best multi-class
classifications possible.
2.3 Classes
There are ten classes in total:
CLASSES DESCRIPTION
c7: Drinking
Table 1: 10 Classes
The dataset consists of images of drivers performing one of 10 possible activities, one of which is safe
driving. The rest of the images belong to classes where the driver can be considered distracted (e.g.
texting). An example of the images used can be seen in Figure 1. It’s worth noting that all the images
are typically taken from the same angle, in the same environmental conditions (i.e. daytime) which is
both good and bad in training the model. It could be considered a positive event because, if a subset of
this data is used as the test set, then the model will most likely perform well since the test images have
the same conditions. However, this posses an issue when the model is generalized to images that don’t
necessarily meet the same conditions as the ones in the original dataset. The hypothesis is that a drastic
change in angle, or incoming light will cause the model to perform very poorly. This hypothesis is hard
to confirm without finding a new dataset of distracted drivers in varying conditions, which, to our
knowledge, does not exist.
Page 6
In order to train a model to detect distracted drivers, a dataset of images of both distracted and non-
distracted drivers is required. This would only require a binary classifier model that would predicted if
a driver is distracted or not. A more interesting problem would be one where the drivers are distracted
in different ways, like eating or fixing their hair for example. This is a harder problem and requires a
multi classifier model and a more specific dataset.
Page 7
CHAPTER 3
System Design
Designing system consists of two main parts. First of all we need to gather the required data already
explained in the previous chapter for training the model that we are about to develop and then we are
going to load that trained model on our embedded board for real time predictions. Hence our designing
process can simply be divided in to two main categories Software and Hardware parts. Now let’s explain
software part first.
3.1 Python
Python is a general purpose and one of the high-level programming languages. A variable can be used
directly without its declaration while writing code in python.
Python is known for its simplicity, easy to read concerning code and recognized as one of the high-level
programming languages. Python consists of inbuilt constructs which have the feature of executing clear
code on small as well as large scale. Python is an object-oriented programming language.
3.2 Anaconda
The open-source Anaconda Distribution is the easiest way to perform Python/R data science and
machine learning on Linux, Windows, and Mac OS X. With over 11 million users worldwide, it is the
industry standard for developing, testing, and training on a single machine, enabling individual data
scientists to.
Develop and train machine learning and deep learning models with scikit-learn, TensorFlow,
and Theano
Analyze data with scalability and performance with Dask, NumPy, pandas, and Numba
3.3 Libraries
Advance libraries and high level APIs are essential part of this project. Python supports more than 1500
libraries. We need to install only those libraries that are essential for our work. Here we are going to
install Keras, Tensor Flow, OpenCV, Scikit-learn, Sci py etc. Commands for installation of these are
given below
Page 8
3.3.2 Keras
Keras is an open-source neural-network library written in Python. It is capable of running on
top of TensorFlow, Microsoft Cognitive Toolkit, Theano. Designed to enable fast experimentation with
deep neural networks,
3.3.3 Numpy
NumPy is the fundamental package for scientific computing with Python. It contains among
other things:
Besides its obvious scientific uses, NumPy can also be used as an efficient multi-dimensional container
of generic data. Arbitrary data-types can be defined. This allows NumPy to seamlessly and speedily
integrate with a wide variety of databases.
3.3.4 Matplotlib
Matplotlib is a Python 2D plotting library which produces publication quality figures in a
variety of hardcopy formats and interactive environments across platforms. Matplotlib can be used in
Python scripts, the Python and IPython shells, the Jupyter notebook, web application servers, and four
graphical user interface toolkits.
3.3.6 SciPy
SciPy (pronounced “Sigh Pie”) is a Python-based ecosystem of open-source software for mathematics,
science, and engineering
3.3.7 OpenCV
OpenCV (Open Source Computer Vision Library) is an open source computer vision and
machine learning software library. OpenCV was built to provide a common infrastructure for computer
Page 9
vision applications and to accelerate the use of machine perception in the commercial products. Being
a BSD-licensed product, OpenCV makes it easy for businesses to utilize and modify the code.
The library has more than 2500 optimized algorithms, which includes a comprehensive set of both
classic and state-of-the-art computer vision and machine learning algorithms. These algorithms can be
used to detect and recognize faces, identify objects, classify human actions in videos
MODEL ACCURACY
Inception V3 95.98%
VGG 16 95.54%
Our all these models perform very well and the highest performing can be chosen but problem arrives
when we have to fit these models on Raspberry Pi small memory. Where all the got failed to fit that
was the point when we decided to make our own model and also reduced the data by going for gray
scaled images instead of going for RGB three times the gray scaled data.
The designed model output graphs are quit impressive but when came unseen data exposes its potential
hence possess some overfitting .
Overfitting occurs when your model perform exceptionally well on trained data but when came across
unseen data fails to predict right. This is where concept of fine tuning came to help reduce overfitting.
Keras fine tuning is there to help reduce the overfitting in your CNN model by adding some Dropouts
explained in previous chapter, reducing the convolution layers and also by increasing the trained data
through some techniques or by introducing new data.
In our case Dropout and data increasing help a lot in achieving good accuracy but we are also trading
with approximately 66% of our data by converting RGB to Gray Scale. Hence we need to increase the
data and by doing so our model performs quit well.
Currently our model perform quit good in real time but our trained model accuracy was about 80%
which was quit good but clearly shows the tradeoff that we made with Pretrained model to meet our
hardware compatibility.
In our model we use train images to train our CNN model to learn ten classes patterns and required
dimension well enough so that when came across fresh data then model possess the ability to make
correct predictions. Now we are able test our models by pushing them to face some fresh data. This
block diagram completely demonstrate the hierarchy of our model flow.
Page 10
Training Neural
Data Network
Model
Acquisition
Page 11
One important thing here is don’t use same data for training as well as for validation this will lead you
to overfitted model and model will not perform well on unseen data. So for that purpose we need to
select few driver whom pictures should be kept separate for random testing.
3.5 Results
As discussed above that the model performs well but not quit well as compared to the results of
pretrained model. The comparison between the accuracy and losses was necessary to discuss here.
Overall accuracy achieved through this model was 80% .some results are shown here in figures.
Page 12
Figure 6: Losses vs Epochs
Now it’s the time to move on second part of the project which is hardware.
USB 4 1/1
Page 13
3.6.1 Raspberry Pi
The Raspberry Pi is a credit-card-sized single-board computer developed in the UK by the
Raspberry Pi Foundation with the intention of promoting the teaching of basic computer science in
schools.
The secret sauce that makes this computer so small and powerful is the Broadcom BCM2837. The ARM
core hardware is a quad core ARM Cortex (ARMv8) cluster. The ARM cores are capable to run up to
1.4GHz and the Video Core IV runs at 400MHz. The ARM core is 64-bit, while the Video Core IV is
32-bit. The model of Raspberry Pi used for this project is 3B+.
The biggest advantage of raspberry pi is its small size as credit card. it works as if a
normal computer at a relatively low price.
One of other advantage of raspberry pi is usually speeding: problems can be solved in
‘hardware’ rather than in software, make use of parallelization.
Figure 7: Raspberry Pi
Page 14
Specifications:
A 1.4GHz 64-bit quad-core ARM Cortex-A53 CPU
2.4GHz and 5GHz IEEE 802.11.b/g/n/ac wireless LAN, Bluetooth 4.2,
BLE
Faster Ethernet (Gigabit Ethernet)
Power-over-Ethernet support (with separate PoE HAT)
Improved PXE network and USB mass-storage booting
Improved thermal management
1GB LPDDR2 SDRAM
4 × USB 2.0 ports
H.264, MPEG-4 decode (1080p30); H.264 encode (1080p30); OpenGL
ES 1.1, 2.0 graphics
40-pin GPIO header
64-bit 64-bit
Processor ARMv6 single core ARMv7 quad ARM Cortex- ARM Cortex-
core A53 single A53 single core
core
USB 2.0 1 2 4
Page 15
3.7 Raspbian
The new Raspberry pi does not come with any operating system. Raspbian is the recommended
operating system for normal use, which is a Debian based operating system. Many other operating
system can also run on raspberry pi like RISC OS, LibreELEC, OSMC (Open Source Media Center),
Windows IOT Core and Arch Linux.
For this project Raspbian is installed in R-pi.
Now install some developer tools like CMAKE, which will help us configure the OpenCV build
process.
Now, we need to install some image and video I/O packages that allow us to load various image and
video file formats from disk.
Page 16
sudo apt-get install libxvidcore-dev libx264-dev
Now we need to compile the highgui which is used to display images and build basic GUIs. module,
we need to install the GTK development library:
Many operations inside of OpenCV can be optimized further by installing a few extra dependencies:
sudo apt-get install libatlas-base-dev gfortran
These optimization libraries are especially important for resource constrained devices such as the
Raspberry Pi.
Now install both the Python 2.7 and Python 3 header files so we can compile OpenCV with Python
bindings:
We’ll want the full install of OpenCV 3 to have access all features so we also need to grab the
opencv_contrib repository as well:
wget -O opencv_contrib.zip
https://github.com/Itseez/opencv_contrib/archive/3.3.0.zip
unzip opencv_contrib.zip
Before we can start compiling OpenCV on our Raspberry Pi, we first need to install pip , a
Python package manager:
we are using Python3 for our project that is why we are using downloading pip3.
wget https://bootstrap.pypa.io/get-pip.py
sudo python3 get-pip.py
CONF_SWAPSIZE=1024
3.9 Libraries
You need to install all the libraries that you have install in anaconda environment and used them while
developing your model. This require you adopt the procedure again.
3.9.2 Keras
Keras is an open-source neural-network library written in Python. It is capable of running on
top of TensorFlow, Microsoft Cognitive Toolkit, Theano. Designed to enable fast experimentation with
deep neural networks,
Page 18
sudo pip3 install keras
3.9.3 Numpy
NumPy is the fundamental package for scientific computing with Python. It contains among
other things:
Besides its obvious scientific uses, NumPy can also be used as an efficient multi-dimensional container
of generic data. Arbitrary data-types can be defined. This allows NumPy to seamlessly and speedily
integrate with a wide variety of databases.
3.9.4 Matplotlib
Matplotlib is a Python 2D plotting library which produces publication quality figures in a
variety of hardcopy formats and interactive environments across platforms. Matplotlib can be used in
Python scripts, web application servers, and four graphical user interface toolkits.
3.9.6 SciPy
SciPy (pronounced “Sigh Pie”) is a Python-based ecosystem of open-source software for
mathematics, science, and engineering
Page 19
3.10 Raspberry Pi Camera
The Camera used for this project is a standard USB type camera.
8 mega pixels
Still image capture up to 640*480 pixels
Automatic color and brightness adjustment
Resolution 352*288(CIF)
First you need to enable Raspberry PI Camera using raspi-config go to Camera option and enable to
interface camera correctly.
3.11 Testing
Load the trained model save earlier and save it in Raspberry Pi card through USB or Mail the “DDD.h5”.
Now load it and perform preprocessing on it that you have perform while training like gray scale,
resizing etc.
Now capture the frame and pass that frame to classifier to get predictions.
Acquisition
Figure 8: Test/Predict
Page 20
Results
As discussed above that the model performs well but not quit well as compared to the results of
pretrained model. The comparison between the accuracy and losses was necessary to discuss here.
Overall accuracy achieved through this model was 80% .some results are shown here in figures.
Page 21
Figure 8: Losses vs Epochs
Page 22
Future Recommendations
Now there are three system that already exist
Sleep Detector
Eye Detector
Distracted Driver Detection
Integrate these systems to develop more satisfying system
Increase data for eastern drivers
Page 23
References
[1] Center for disease control and prevention. https: //www.cdc.gov/motorvehiclesafety/
distracted_driving/.
[4] https://elinux.org/Jetson_TK1
[5] https://www.wikipedia.org/
Page 24