Skip to content

Commit c4fa32a

Browse files
authored
Merge pull request #170 from itsankitdwivedi/drowsydetection
Ankit Dwivedi | Driver Drowsiness Detector
2 parents 7a65d64 + 3d23cea commit c4fa32a

File tree

8 files changed

+39347
-0
lines changed

8 files changed

+39347
-0
lines changed

Drowsiness detection/Readme.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Driver Drowsiness Detection System
2+
## Author name - Ankit Dwivedi
3+
In this Python project, we will be using OpenCV for gathering the images from webcam and feed them into a Deep Learning model which will classify whether the person’s eyes are ‘Open’ or ‘Closed’. The approach we will be using for this Python project is as follows :
4+
5+
Step 1 – Take image as input from a camera.
6+
7+
Step 2 – Detect the face in the image and create a Region of Interest (ROI).
8+
9+
Step 3 – Detect the eyes from ROI and feed it to the classifier.
10+
11+
Step 4 – Classifier will categorize whether eyes are open or closed.
12+
13+
Step 5 – Calculate score to check whether the person is drowsy.
14+
15+
## Driver Drowsiness Detection Dataset
16+
The dataset used for this model is created by us. To create the dataset, we wrote a script that captures eyes from a camera and stores in our local disk. We separated them into their respective labels ‘Open’ or ‘Closed’. The data was manually cleaned by removing the unwanted images which were not necessary for building the model. The data comprises around 7000 images of people’s eyes under different lighting conditions. After training the model on our dataset, we have attached the final weights and model architecture file “models/cnnCat2.h5”.
17+
18+
Now, you can use this model to classify if a person’s eye is open or closed.
19+
20+
### The Model Architecture
21+
The model we used is built with Keras using Convolutional Neural Networks (CNN). A convolutional neural network is a special type of deep neural network which performs extremely well for image classification purposes. A CNN basically consists of an input layer, an output layer and a hidden layer which can have multiple layers. A convolution operation is performed on these layers using a filter that performs 2D matrix multiplication on the layer and filter.
22+
23+
The CNN model architecture consists of the following layers:
24+
25+
Convolutional layer; 32 nodes, kernel size 3
26+
27+
Convolutional layer; 32 nodes, kernel size 3
28+
29+
Convolutional layer; 64 nodes, kernel size 3
30+
31+
Fully connected layer; 128 nodes
32+
33+
The final layer is also a fully connected layer with 2 nodes. A Relu activation function is used in all the layers except the output layer in which we used Softmax.
34+
35+
# Project Prerequisites
36+
The requirement for this Python project is a webcam through which we will capture images. You need to have Python (3.6 version recommended) installed on your system, then using pip, you can install the necessary packages.
37+
38+
1. OpenCV – pip install opencv-python (face and eye detection).
39+
40+
2. TensorFlow – pip install tensorflow (keras uses TensorFlow as backend).
41+
42+
3. Keras – pip install keras (to build our classification model).
43+
44+
4. Pygame – pip install pygame (to play alarm sound).

Drowsiness detection/alarm.wav

996 KB
Binary file not shown.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import cv2
2+
import os
3+
from keras.models import load_model
4+
import numpy as np
5+
from pygame import mixer
6+
import time
7+
8+
mixer.init()
9+
sound = mixer.Sound('alarm.wav')
10+
11+
face = cv2.CascadeClassifier('haar cascade files\haarcascade_frontalface_alt.xml')
12+
leye = cv2.CascadeClassifier('haar cascade files\haarcascade_lefteye_2splits.xml')
13+
reye = cv2.CascadeClassifier('haar cascade files\haarcascade_righteye_2splits.xml')
14+
lbl=['Close','Open']
15+
16+
model = load_model('models/cnncat2.h5')
17+
path = os.getcwd()
18+
cap = cv2.VideoCapture(0)
19+
font = cv2.FONT_HERSHEY_COMPLEX_SMALL
20+
count=0
21+
score=0
22+
thicc=2
23+
rpred=[99]
24+
lpred=[99]
25+
26+
while(True):
27+
ret, frame = cap.read()
28+
height,width = frame.shape[:2]
29+
30+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
31+
32+
faces = face.detectMultiScale(gray,minNeighbors=5,scaleFactor=1.1,minSize=(25,25))
33+
left_eye = leye.detectMultiScale(gray)
34+
right_eye = reye.detectMultiScale(gray)
35+
cv2.rectangle(frame, (0,height-50) , (200,height) , (0,0,0) , thickness=cv2.FILLED )
36+
37+
for (x,y,w,h) in faces:
38+
cv2.rectangle(frame, (x,y) , (x+w,y+h) , (100,100,100) , 1 )
39+
40+
for (x,y,w,h) in right_eye:
41+
r_eye=frame[y:y+h,x:x+w]
42+
count=count+1
43+
r_eye = cv2.cvtColor(r_eye,cv2.COLOR_BGR2GRAY)
44+
r_eye = cv2.resize(r_eye,(24,24))
45+
r_eye= r_eye/255
46+
r_eye= r_eye.reshape(24,24,-1)
47+
r_eye = np.expand_dims(r_eye,axis=0)
48+
rpred = model.predict_classes(r_eye)
49+
if(rpred[0]==1):
50+
lbl='Open'
51+
if(rpred[0]==0):
52+
lbl='Closed'
53+
break
54+
55+
for (x,y,w,h) in left_eye:
56+
l_eye=frame[y:y+h,x:x+w]
57+
count=count+1
58+
l_eye = cv2.cvtColor(l_eye,cv2.COLOR_BGR2GRAY)
59+
l_eye = cv2.resize(l_eye,(24,24))
60+
l_eye= l_eye/255
61+
l_eye=l_eye.reshape(24,24,-1)
62+
l_eye = np.expand_dims(l_eye,axis=0)
63+
lpred = model.predict_classes(l_eye)
64+
if(lpred[0]==1):
65+
lbl='Open'
66+
if(lpred[0]==0):
67+
lbl='Closed'
68+
break
69+
70+
if(rpred[0]==0 and lpred[0]==0):
71+
score=score+1
72+
cv2.putText(frame,"Closed",(10,height-20), font, 1,(255,255,255),1,cv2.LINE_AA)
73+
# if(rpred[0]==1 or lpred[0]==1):
74+
else:
75+
score=score-1
76+
cv2.putText(frame,"Open",(10,height-20), font, 1,(255,255,255),1,cv2.LINE_AA)
77+
if(score<0):
78+
score=0
79+
cv2.putText(frame,'Score:'+str(score),(100,height-20), font, 1,(255,255,255),1,cv2.LINE_AA)
80+
if(score>15):
81+
#person is feeling sleepy so we beep the alarm
82+
cv2.imwrite(os.path.join(path,'image.jpg'),frame)
83+
try:
84+
sound.play()
85+
86+
except: # isplaying = False
87+
pass
88+
if(thicc<16):
89+
thicc= thicc+2
90+
else:
91+
thicc=thicc-2
92+
if(thicc<2):
93+
thicc=2
94+
cv2.rectangle(frame,(0,0),(width,height),(0,0,255),thicc)
95+
cv2.imshow('frame',frame)
96+
if cv2.waitKey(1) & 0xFF == ord('q'):
97+
break
98+
cap.release()
99+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)