Home Drowsiness Detection
Post
Cancel

Drowsiness Detection

Drowsiness Detection

Drowsiness detection is a technology aimed at identifying when a person, typically a driver, is becoming drowsy or fatigued. This is crucial for preventing accidents and ensuring safety, particularly in the context of driving, operating heavy machinery, or performing tasks that require sustained attention.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import cv2
import dlib
import requests
import pyttsx3
from scipy.spatial import distance

import time
import sys

from ibmiotf import device
import random

#Provide your IBM Watson Device Credentials
organization = " "
deviceType = " "
deviceId = " "
authMethod = " "
authToken = " "

def ibmstart(x):
    
    def myCommandCallback(cmd):
        print("Command received: %s" % cmd.data['command'])
        print(cmd)

    try:
      deviceOptions = {"org": organization, "type": deviceType, "id": deviceId, "auth-method": authMethod, "auth-token": authToken}
      deviceCli = device.Client(deviceOptions)
      #..............................................
      
    except Exception as e:
      print("Caught exception connecting device: %s" % str(e))
      sys.exit()
    
    deviceCli.connect()  
    data = { 'Status' : x}
    #print data
    def myOnPublishCallback():
        print ("Published Status = %s" % x, "to IBM Watson")

    success = deviceCli.publishEvent("DD", "json", data, qos=0, on_publish=myOnPublishCallback)
    if not success:
        print("Not connected to IoTF")
                
        
    deviceCli.commandCallback = myCommandCallback
    deviceCli.disconnect()
    


# INITIALIZING THE pyttsx3 SO THAT
# ALERT AUDIO MESSAGE CAN BE DELIVERED
engine = pyttsx3.init()

# SETTING UP OF CAMERA TO 1 YOU CAN
# EVEN CHOOSE 0 IN PLACE OF 1
cap = cv2.VideoCapture(0)

# FACE DETECTION OR MAPPING THE FACE TO
# GET THE Eye AND EYES DETECTED
face_detector = dlib.get_frontal_face_detector()

# PUT THE LOCATION OF .DAT FILE (FILE FOR
# PREDECTING THE LANDMARKS ON FACE )
dlib_facelandmark = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

# FUNCTION CALCULATING THE ASPECT RATIO FOR
# THE Eye BY USING EUCLIDEAN DISTANCE FUNCTION
def Detect_Eye(eye):
  poi_A = distance.euclidean(eye[1], eye[5])
  poi_B = distance.euclidean(eye[2], eye[4])
  poi_C = distance.euclidean(eye[0], eye[3])
  aspect_ratio_Eye = (poi_A+poi_B)/(2*poi_C)
  return aspect_ratio_Eye


# MAIN LOOP IT WILL RUN ALL THE UNLESS AND
# UNTIL THE PROGRAM IS BEING KILLED BY THE USER
while True:
        
  null, frame = cap.read()
  flag=0
  gray_scale = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

  faces = face_detector(gray_scale)

  for face in faces:
    face_landmarks = dlib_facelandmark(gray_scale, face)
    leftEye = []
    rightEye = []

    # THESE ARE THE POINTS ALLOCATION FOR THE
    # LEFT EYES IN .DAT FILE THAT ARE FROM 42 TO 47
    for n in range(42, 48):
      x = face_landmarks.part(n).x
      y = face_landmarks.part(n).y
      rightEye.append((x, y))
      next_point = n+1
      if n == 47:
        next_point = 42
      x2 = face_landmarks.part(next_point).x
      y2 = face_landmarks.part(next_point).y
      cv2.line(frame, (x, y), (x2, y2), (0, 255, 0), 1)

    # THESE ARE THE POINTS ALLOCATION FOR THE
    # RIGHT EYES IN .DAT FILE THAT ARE FROM 36 TO 41
    for n in range(36, 42):
      x = face_landmarks.part(n).x
      y = face_landmarks.part(n).y
      leftEye.append((x, y))
      next_point = n+1
      if n == 41:
        next_point = 36
      x2 = face_landmarks.part(next_point).x
      y2 = face_landmarks.part(next_point).y
      cv2.line(frame, (x, y), (x2, y2), (255, 255, 0), 1)

    # CALCULATING THE ASPECT RATIO FOR LEFT
    # AND RIGHT EYE
    right_Eye = Detect_Eye(rightEye)
    left_Eye = Detect_Eye(leftEye)
    Eye_Rat = (left_Eye+right_Eye)/2

    # NOW ROUND OF THE VALUE OF AVERAGE MEAN
    # OF RIGHT AND LEFT EYES
    Eye_Rat = round(Eye_Rat, 2)

    # THIS VALUE OF 0.25 (YOU CAN EVEN CHANGE IT)
    # WILL DECIDE WHETHER THE PERSONS'S EYES ARE CLOSE OR NOT
    if Eye_Rat < 0.25:
      cv2.putText(frame, "DROWSINESS DETECTED", (50, 100),
            cv2.FONT_HERSHEY_PLAIN, 2, (21, 56, 210), 3)
      cv2.putText(frame, "Alert!!!! WAKE UP DUDE", (50, 450),
            cv2.FONT_HERSHEY_PLAIN, 2, (21, 56, 212), 3)

      # CALLING THE AUDIO FUNCTION OF TEXT TO
      # AUDIO FOR ALERTING THE PERSON
      engine.say("Alert!!!! WAKE UP DUDE")
      flag=1
      engine.runAndWait()

           

  cv2.imshow("Drowsiness DETECTOR IN OPENCV2", frame)
  print(flag)
  ibmstart(flag)
  '''
  while True:
        
                data = { 'Status' : x}
                #print data
                def myOnPublishCallback():
                    print ("Published Status = %s" % x, "to IBM Watson")

                success = deviceCli.publishEvent("DD", "json", data, qos=0, on_publish=myOnPublishCallback)
                if not success:
                    print("Not connected to IoTF")
                time.sleep(1)
        
                deviceCli.commandCallback = myCommandCallback
  '''

  key = cv2.waitKey(9)
  if key == 20:
    break

# Disconnect the device and application from the cloud
#deviceCli.disconnect()
cap.release()
cv2.destroyAllWindows()

GitHub - Click Here to download Code

This post is licensed under CC BY 4.0 by the author.

-

Data Transform

Comments powered by Disqus.