Git Product home page Git Product logo

drowsiness-detection's Introduction

Drowsiness Detection System(webapp)

Click Here to see the demo
GitHub last commit Libraries.io dependency status for GitHub repo

Motivation:

According to the National Highway Traffic Safety Administration, every year about 100,000 police-reported crashes involve drowsy driving. These crashes result in more than 1,550 fatalities and 71,000 injuries. The real number may be much higher, however, as it is difficult to determine whether a driver was drowsy at the time of a crash. So, we tried to build a system, that detects whether a person is drowsy and alert him.

Installing and Configuring dlib:

We need to create an enivronment in order to install dlib, as it cannot be directly installed using pip. So, follow this commands in order to install dlib into your system if you haven't installed it previously. Make sure you have Anaconda installed, as we will be doing everyting in Anaconda Prompt.

Step 1: Update conda

conda update conda

Step 2: Update anaconda

conda update anaconda 

Step 3: Create a virtual environment

conda create -n env_dlib 

Step 4: Activate the virtual environment

conda activate env_dlib

Step 5: Install dlib

conda install -c conda-forge dlib 

If all these steps are completed successfully, then dlib will be installed in the virtual environment env_dlib. Make sure to use this environment to run the entire project.

Step to deactivate the virtual environment

conda deactivate 

Running the system:

Step 1:

Clone the repository into your system by:

git clone https://github.com/fear-the-lord/Drowsiness-Detection.git

Or directly download the zip.

Step 2:

Download the file shape_predictor_68_face_landmarks.dat here. Make sure you download it in the same folder.

Step 3:

Install all the system requirments by:

pip install -r requirements.txt

Step 4:

After the system has been setup. Run the command:

python app1.py

Step 5:

Open your browser and in the search bar type: localhost:8000 as this port is mostly used by flask. In case, this port is not available in your system, flask will try to use another port. The port number will be displayed in the command prompt. So, type in the same port number in that case as: localhost:<port_number>.

After all these steps have been completed successfully, you will see a web page opening up in the browser. Now you are free to explore the system.

Working Details:

The basic thing about drowsiness detection is pretty simple. We first detect a face using dlib's frontal face detector. Once the face is detected , we try to detect the facial landmarks in the face using the dlib's landmark predictor. The landmark predictor returns 68 (x, y) coordinates representing different regions of the face, namely - mouth, left eyebrow, right eyebrow, right eye, left eye, nose and jaw. Ofcourse, we don't need all the landmarks, here we need to extract only the eye and the mouth region.

Now, after extraxting the landmarks we calculate the Eye Aspect Ratio (EAR) as:

def eye_aspect_ratio(eye):
	# Vertical eye landmarks
	A = dist.euclidean(eye[1], eye[5])
	B = dist.euclidean(eye[2], eye[4])
	# Horizontal eye landmarks 
	C = dist.euclidean(eye[0], eye[3])

	# The EAR Equation 
	EAR = (A + B) / (2.0 * C)
	return EAR

The eye region is marked by 6 coordinates. These coordinates can be used to find whether the eye is open or closed if the value of EAR is checked with a certain threshold value.
blink_detection_plot

In the same way I have calculated the aspect ratio for the mouth to detect if a person is yawning. Although, there is no specific metric for calculating this, so I have taken for points, 2 each from the upper and lower lip and calculated the mean distance between them as:

def mouth_aspect_ratio(mouth): 
	A = dist.euclidean(mouth[13], mouth[19])
	B = dist.euclidean(mouth[14], mouth[18])
	C = dist.euclidean(mouth[15], mouth[17])

	MAR = (A + B + C) / 3.0
	return MAR

Note: Learn more about dlib here.

Results:

The GUI has been created using basic HTML, CSS and JavaScript and we have used Flask to render the python code into the website. Tkinter has also been used in order to make things simpler. It has 2 buttons: Run and Exit. The GUI looks like: df01ae7c-afc9-4676-b95b-b6cec592ddf0 (online-video-cutter com) (1)

The outputs of the working system detecting drowsiness is shown as:
frame_yawn1 Screenshot (405)

Also, in order to keep a proof of the moment when the person was sleeping or yawning, we kept a seperate folder where those frames are stored as:
Screenshot (408)

Streaming using Phone Camera

We have used and Android App available for free in Play Store, named IP Webcam. It can be downloaded from this link. After downloading it, open the app and scroll down to the option Start Server. It will look like:

After starting the server, an IP will be displayed on the screen. Open the file android_cam.py. In line 36 put the given IP.

url = "http://<YOUR_IP_HERE>/shot.jpg"

Also, make sure that the phone and PC/Laptop is connected to the same network.

Then, run the system in the same way as mentioned above. Click on the Run Using Phone Cam button to see the results:

Also, in order to toggle between the front and back camera, type the IP upto "http://<YOUR_IP_HERE>" in the search bar of yor browser and explore the page which will look like this:

Also, we have tried plotting the MAR and EAR graph Vs. Time in order to make the working clearer to the audience. The graph looks like:

Future Scope:

Any leads on hosting this Flask App will be useful.

References:

[1]Facial landmarks with dlib, OpenCV and Python: https://www.pyimagesearch.com/2017/04/03/facial-landmarks-dlib-opencv-python/
[2]Eye blink detection with OpenCV, Python, and dlib: https://www.pyimagesearch.com/2017/04/24/eye-blink-detection-opencv-python-dlib/
[3]Drowsiness Detection with OpenCV: https://www.pyimagesearch.com/2017/05/08/drowsiness-detection-opencv/
[4]Real-Time Eye Blink Detection using Facial Landmarks: http://vision.fe.uni-lj.si/cvww2016/proceedings/papers/05.pdf

drowsiness-detection's People

Contributors

dependabot[bot] avatar fear-the-lord avatar rajarsigit avatar sawonbhattacharya avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

drowsiness-detection's Issues

Missing references and citations

Hi there @fear-the-lord -- Adrian here from PyImageSearch.com. I noticed that both your Medium article as well as this GitHub repo do not provide proper citations to PyImageSearch's articles, despite you are using code derived from those articles.

My FAQ states that you need to cite my articles if you use them in your own work.

Specifically, you need to be referencing the original drowsiness detection article.

Kindly update your references in the Medium article and GitHub repo. Thank you.

Regarding libraries

Hello , I have some issues regarding the libraries. Please I need your help to solve these problems.

Dataset not available

Hey please create a new link for shape_predictor_68_face_landmarks.dat the file is not available anymore. I need it urgently. Thanks

I am having some error in clicking Start button.

2021-04-06 16:17:34.729 Python[835:24639] WARNING: NSWindow drag regions should only be invalidated on the Main Thread! This will throw an exception in the future. Called from (
0 AppKit 0x00007fff38f9c7f0 -[NSWindow(NSWindow_Theme) _postWindowNeedsToResetDragMarginsUnlessPostingDisabled] + 371
1 AppKit 0x00007fff38f99ce1 -[NSWindow _initContent:styleMask:backing:defer:contentView:] + 1416
2 AppKit 0x00007fff38f99753 -[NSWindow initWithContentRect:styleMask:backing:defer:] + 42
3 libtk8.6.dylib 0x0000000108961a55 TkMacOSXMakeRealWindowExist + 729
4 libtk8.6.dylib 0x000000010896168a TkWmMapWindow + 56
5 libtk8.6.dylib 0x00000001088c4788 Tk_MapWindow + 69
6 libtk8.6.dylib 0x00000001088cda15 MapFrame + 59
7 libtcl8.6.dylib 0x0000000108807570 TclServiceIdle + 87
8 libtcl8.6.dylib 0x00000001087ead27 Tcl_DoOneEvent + 349
9 _tkinter.cpython-38-darwin.so 0x00000001087163de _tkinter_tkapp_mainloop + 382
10 Python 0x00000001070976ee method_vectorcall_FASTCALL + 254
11 Python 0x000000010715c9cc call_function + 444
12 Python 0x000000010715980a _PyEval_EvalFrameDefault + 25642
13 Python 0x000000010715d814 _PyEval_EvalCodeWithName + 2804
14 Python 0x000000010708ebee _PyFunction_Vectorcall + 270
15 Python 0x000000010709111a method_vectorcall + 170
16 Python 0x000000010715c9cc call_function + 444
17 Python 0x000000010715982e _PyEval_EvalFrameDefault + 25678
18 Python 0x000000010708ea50 function_code_fastcall + 128
19 Python 0x000000010715c9cc call_function + 444
20 Python 0x00000001071598c5 _PyEval_EvalFrameDefault + 25829
21 Python 0x000000010708ea50 function_code_fastcall + 128
22 Python 0x000000010708e42d PyVectorcall_Call + 109
23 Python 0x0000000107159d38 _PyEval_EvalFrameDefault + 26968
24 Python 0x000000010708ea50 function_code_fastcall + 128
25 Python 0x000000010715c9cc call_function + 444
26 Python 0x000000010715980a _PyEval_EvalFrameDefault + 25642
27 Python 0x000000010708ea50 function_code_fastcall + 128
28 Python 0x000000010715c9cc call_function + 444
29 Python 0x000000010715980a _PyEval_EvalFrameDefault + 25642
30 Python 0x000000010708ea50 function_code_fastcall + 128
31 Python 0x000000010715c9cc call_function + 444
32 Python 0x000000010715980a _PyEval_EvalFrameDefault + 25642
33 Python 0x000000010708ea50 function_code_fastcall + 128
34 Python 0x000000010708df57 _PyObject_FastCallDict + 247
35 Python 0x000000010708f55f _PyObject_Call_Prepend + 143
36 Python 0x00000001070e4036 slot_tp_call + 150
37 Python 0x000000010708e105 _PyObject_MakeTpCall + 373
38 Python 0x000000010715ca25 call_function + 533
39 Python 0x000000010715982e _PyEval_EvalFrameDefault + 25678
40 Python 0x000000010709edce gen_send_ex + 206
41 Python 0x0000000107158f8f _PyEval_EvalFrameDefault + 23471
42 Python 0x000000010715d814 _PyEval_EvalCodeWithName + 2804
43 Python 0x000000010708ebee _PyFunction_Vectorcall + 270
44 Python 0x000000010715c9cc call_function + 444
45 Python 0x00000001071598c5 _PyEval_EvalFrameDefault + 25829
46 Python 0x000000010715d814 _PyEval_EvalCodeWithName + 2804
47 Python 0x000000010708ebee _PyFunction_Vectorcall + 270
48 Python 0x000000010715c9cc call_function + 444
49 Python 0x000000010715980a _PyEval_EvalFrameDefault + 25642
50 Python 0x000000010708ea50 function_code_fastcall + 128
51 Python 0x000000010715c9cc call_function + 444
52 Python 0x000000010715980a _PyEval_EvalFrameDefault + 25642
53 Python 0x000000010708ea50 function_code_fastcall + 128
54 Python 0x000000010715c9cc call_function + 444
55 Python 0x000000010715982e _PyEval_EvalFrameDefault + 25678
56 Python 0x000000010708ea50 function_code_fastcall + 128
57 Python 0x000000010715c9cc call_function + 444
58 Python 0x000000010715980a _PyEval_EvalFrameDefault + 25642
59 Python 0x000000010708ea50 function_code_fastcall + 128
60 Python 0x000000010708df57 _PyObject_FastCallDict + 247
61 Python 0x000000010708f55f _PyObject_Call_Prepend + 143
62 Python 0x00000001070e56c1 slot_tp_init + 145
63 Python 0x00000001070e0b99 type_call + 297
64 Python 0x000000010708e105 _PyObject_MakeTpCall + 373
65 Python 0x000000010715ca25 call_function + 533
66 Python 0x000000010715982e _PyEval_EvalFrameDefault + 25678
67 Python 0x000000010708ea50 function_code_fastcall + 128
68 Python 0x000000010715c9cc call_function + 444
69 Python 0x000000010715980a _PyEval_EvalFrameDefault + 25642
70 Python 0x000000010708ea50 function_code_fastcall + 128
71 Python 0x00000001070911fc method_vectorcall + 396
72 Python 0x000000010708e42d PyVectorcall_Call + 109
73 Python 0x0000000107159d38 _PyEval_EvalFrameDefault + 26968
74 Python 0x000000010708ea50 function_code_fastcall + 128
75 Python 0x000000010715c9cc call_function + 444
76 Python 0x000000010715980a _PyEval_EvalFrameDefault + 25642
77 Python 0x000000010708ea50 function_code_fastcall + 128
78 Python 0x000000010715c9cc call_function + 444
79 Python 0x000000010715980a _PyEval_EvalFrameDefault + 25642
80 Python 0x000000010708ea50 function_code_fastcall + 128
81 Python 0x000000010709115b method_vectorcall + 235
82 Python 0x000000010708e42d PyVectorcall_Call + 109
83 Python 0x00000001071fc61a t_bootstrap + 74
84 Python 0x00000001071af819 pythread_wrapper + 25
85 libsystem_pthread.dylib 0x00007fff73078d76 _pthread_start + 125
86 libsystem_pthread.dylib 0x00007fff730755d7 thread_start + 15

Brother please help me on this one. I have final year's project submission tomorrow. Thank you in advance.

Positioning phone when driving

I am curious how one should position the phone in front of their face when driving. Maybe include something about this in the readme.

Please also clarify how much of the training data is collected specifically for the intended use case (while the subject is driving).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.