-
Notifications
You must be signed in to change notification settings - Fork 1
/
drowsiness_detection.cpp
250 lines (212 loc) · 8.35 KB
/
drowsiness_detection.cpp
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/********************************************************
Name: Concentration Reminder - Drowsiness Detection
Author: Lisa Palathingal
Date: 12/08/2014
**********************************************************/
//Header files
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include <fstream>
//Declaring namespaces
using namespace std;
using namespace cv;
void detectAndDisplay (Mat frame, int index, int history[]);
/*********************************************************************************
Function Name: Help
Description: Qt GUI in OpenCV provides a control panel to which buttons can be
attached using the function, createButton. When the user presses
Help button, a help manual is displayed on the status bar of Qt GUI.
***********************************************************************************/
void Help(int state, void *pointer)
{
displayStatusBar("Drowsiness Detection", "Concentration Reminder considers 100 frames for drowsiness detection."
"If system detects closed eyes in 30 or more frames, you are sleeping. Else, you are active", 0);
}
/********************************************************************************
Function Name: Quit
Description: Qt GUI in OpenCV provides a control panel to which buttons can be
attached using the function, createButton. When the user presses
Quit button, the application is closed.
*********************************************************************************/
void Quit(int state, void *pointer)
{
exit(0);
}
/******************************************************************************
Function Name: Pause
Description: Qt GUI in OpenCV provides a control panel to which buttons can be
attached using the function, createButton. When the user presses
Pause button, the application is paused. The application can be
resumed by pressing any key. The statusbar displays a message to
the user, " Press any key to continue".
*******************************************************************************/
void Pause(int state, void *pointer)
{
waitKey(0);
displayStatusBar("Drowsiness Detection", "Press any key to resume detection", 0);
}
// Global variables
String face_cascade_name = "test/haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "test/haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
//Display window name
string window_name = "Drowsiness Detection";
RNG rng(12345);
/************************************************************************************************
Function Name: Main
Description: A history array[100] is initized with value 0. Loads face and eye classifiers.
Capture video from the webcamera. If video is captured, get the first frame.
detectAndDisplay function is called to detect face and eyes on the frame. Closed
count is incremented if history[] cell value is 1. If number of closed counts
is >= 25, message is displayed indicating that the user is sleeping.
***************************************************************************************************/
int main( int argc, const char** argv )
{
CvCapture* capture;
Mat frame;
namedWindow(window_name, CV_WINDOW_NORMAL | CV_WINDOW_KEEPRATIO);
resizeWindow(window_name, 1280, 960);
//createButton function creates button on the status bar of Qt GUI
createButton("Pause", Pause, (void*) "test", CV_PUSH_BUTTON, 1);
createButton("Help", Help, (void*) "test", CV_PUSH_BUTTON, 1);
createButton("Quit", Quit, (void*) "test", CV_PUSH_BUTTON, 1);
//Load the cascades
if (!face_cascade.load(face_cascade_name))
{
printf("--(!)Error loading\n");
return -1;
};
if (!eyes_cascade.load(eyes_cascade_name))
{
printf("--(!)Error loading\n");
return -1;
};
//Read the video stream
capture = cvCaptureFromCAM( -1 );
if (capture)
{
int size = 100;
int index = -1;
int i;
int history[size]; // A history array with size 100
int closed = 0;
//Initialize history[] cells to 0
for(i = 0; i < size; i++)
{
history[i] = 0;
}
while( true )
{
frame = cvQueryFrame( capture );
index ++;
//If index of history[] reaches the value 100, reset index value to 0.
if(index == size)
{
index = 0;
for(i = 0; i < size; i++)
{
history[i] = 0;
}
}
//Apply the classifier to the frame
if( !frame.empty() )
{
detectAndDisplay( frame, index, history );
}
else
{
printf(" --(!) No captured frame -- Break!");
break;
}
if(index == 0)
{
if(history[index] == 1)
{
closed = closed + 1;
printf("\nCount = %d\n", closed);
}
}
else if(index > 0)
{
if(history[index] == 1)
{
if( (history[index-1] == 1) || (history[index-1] == 0) )
{
closed = closed + 1;
printf("\nCount = %d\n", closed);
if(closed >= 30)
{
closed = 0;
printf("You are sleeping\n");
}
}
}
else if(history[index] == 0)
{
if(closed < 30)
{
closed = 0;
printf("\nCount = %d\n", closed);
}
else if(closed >= 30)
{
closed = 0;
printf("You are sleeping\n");
}
}
}
int c = waitKey(10);
}
}
return 0;
}
/***********************************************************************************
Function Name: detectAndDisplay
Input: frame, index, history[]
Output: Detected eyes from the frame, updated values for history array[]
Description: detectAndDisplay detects face and eyes from the input frame and draws
a circle around detected eyes. If open eyes are detected, value 0 is
inserted in the corresponding index of history[]. If closed eyes are
detected, value 1 is inserted in the corresponding index of history[].
**************************************************************************************/
void detectAndDisplay( Mat frame, int index, int history[] )
{
std::vector<Rect> faces;
Mat frame_gray;
cvtColor( frame, frame_gray, CV_BGR2GRAY );
equalizeHist( frame_gray, frame_gray );
//Detect face from the frame
face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
for( size_t i = 0; i < faces.size(); i++ )
{
Mat faceROI = frame_gray( faces[i] );
std::vector<Rect> eyes;
//Detect eyes from the detected face
eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );
//Draw circles around detected eyes
for( size_t j = 0; j < eyes.size(); j++ )
{
Point center( faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5 );
int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
circle( frame, center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );
}
/*If size() is greater than 0, open eyes are detected. history[] value is 0.
Else, closed eyes are detected. history[] value is updated to 1.*/
if (eyes.size() > 0)
{
history[index] = 0;
}
else
{
history[index] = 1;
}
}
//Display window name and frame as output
imshow( window_name, frame );
}