Drowsiness Detection

Drowsiness Detection

Drowsiness Detection refers to feeling abnormally sleepy during the day. People who are drowsy may fall asleep in inappropriate situations or at inappropriate times.

Driver drowsiness detection is a car safety technology that helps prevent accidents caused by the driver getting drowsy. Various studies have suggested that around 20% of all road accidents are fatigue-related, up to 50% on certain roads.

dlib’s facial landmark Drowsiness Detection:-

  • The pre-trained facial landmark detector inside the dlib library estimates the location of 68 (x, y)-coordinates that map to facial structures on the face.
  • Facial landmarks are used for localizing and representing salient regions or facial parts of the person’s face, such as:

Nose

Jaws

Left eye

Right eye

Left eyebrow

Mouth

Right eyebrow

dlib’s facial landmark detector:-

The Locations of the Facial Parts are as follows:

  • The left eye is accessed with points [42, 47].
  • The mouth is accessed through points [48, 67].
  • The left eyebrow is accessed through points [22, 26].
  • The nose is accessed using points [27, 34].
  • The right eyebrow is accessed through points [17, 21].
  • The right eye is accessed using points [36, 41].
  • And the jaw is accessed via points [0, 16].

Block Diagram:-

Source Code:-

from scipy.spatial import distance as dist
from imutils import face_utils
import imutils
import dlib
import cv2
import winsound
frequency = 2500
duration = 1000

def eyeAspectRatio(eye):

A = dist.euclidean(eye[1], eye[5])
B = dist.euclidean(eye[2], eye[4])
C = dist.euclidean(eye[0], eye[3])
ear = (A + B) / (2.0 * C)
return ear

 

count = 0
earThresh = 0.3 #distance between vertical eye coordinate Threshold
earFrames = 48 #consecutive frames for eye closure
shapePredictor = "shape_predictor_68_face_landmarks.dat"

cam = cv2.VideoCapture(0)
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(shapePredictor)

#get the coord of left & right eye
(lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
(rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]

while True:

_, frame = cam.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 = eyeAspectRatio(leftEye)
rightEAR = eyeAspectRatio(rightEye)

ear = (leftEAR + rightEAR) / 2.0

leftEyeHull = cv2.convexHull(leftEye)
rightEyeHull = cv2.convexHull(rightEye)
cv2.drawContours(frame, [leftEyeHull], -1, (0, 0, 255), 1)
cv2.drawContours(frame, [rightEyeHull], -1, (0, 0, 255), 1)

if ear < earThresh:

count += 1

if count >= earFrames:

cv2.putText(frame, "DROWSINESS DETECTED", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
winsound.Beep(frequency, duration)

else:

count = 0

cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF

if key == ord("q"):
break

cam.release()
cv2.destroyAllWindows()

Driver Drowsiness Detection Dataset

To create the dataset, we wrote a script that captures eyes from a camera and stores them in our local disk. We separated them into their respective labels ‘Open’ or ‘Closed’. 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”.

Now, you can use this model to classify if a person’s eye is open or closed.

Alternatively, if you want to build and train your own model, you can download the dataset: Driver Drowsiness Dataset

The Model Architecture

A convolutional neural network is a special type of deep neural network which performs exceptionally well for image classification purposes. A CNN consists of an input layer, an output layer, and a hidden layer with multiple layers.

The CNN model architecture consists of the following layers:

  • Convolutional layer; 32 nodes, kernel size 3
  • Convolutional layer; 32 nodes, kernel size 3
  • Convolutional layer; 64 nodes, kernel size 3
  • Fully connected layer; 128 nodes

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.