-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlinkDetection.cpp
229 lines (185 loc) · 6.14 KB
/
BlinkDetection.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
#include "BlinkDetection.h"
BlinkDetection::BlinkDetection( float _treshval ) {
startTime = 0;
curTime = 0;
treshval = _treshval;
flag_match = true;
flag_ncc[0] = (flag_ncc[1]= (flag_ncc[2]= false ));
templ_img = vector<IplImage*>();
templ_r = vector<CvRect*>();
m_frame = 0;
blinksec = 0;
count_lost = 0;
}
BlinkDetection::~BlinkDetection() {
if(!templ_img.empty())templ_img.clear();
if(!templ_r.empty())templ_r.clear();
if(m_frame)cvReleaseImage(&m_frame);
}
bool BlinkDetection::detect( IplImage* frame, vector<CvRect*> eyes ) {
vector<IplImage*> cur_img = vector<IplImage*>();
vector<CvRect*> cur_r = vector<CvRect*>();
IplImage* leftTmp = 0;
IplImage* rightTmp = 0;
curTime = time(0);
if( !frame ||
( eyes == (vector<CvRect*>)0 || !eyes.at(0) || !eyes.at(1) ) ) {
startTime = 0;
flag_match = false;
flag_ncc[0] = (flag_ncc[1]= (flag_ncc[2]= false ));
blinksec = 0;
return false;
}
if(count_lost > 15) {
count_lost = 0;
templ_img.clear();
templ_r.clear();
}
if(templ_img.empty() || templ_r.empty()) { // init template
createTemplate(frame,eyes.at(0),eyes.at(1));
}else{
if(flag_match) { // start prev/cur matching, start timer
startTime = 0;
flag_match = false;
flag_ncc[0] = (flag_ncc[1]= (flag_ncc[2]= false ));
blinksec = 0;
}
cur_r.insert(cur_r.end(), eyes.at(0));
cur_r.insert(cur_r.end(), eyes.at(1));
leftTmp = cvCreateImage( cvSize(cur_r.at(0)->width,cur_r.at(0)->height), frame->depth, frame->nChannels );
rightTmp = cvCreateImage( cvSize(cur_r.at(1)->width,cur_r.at(1)->height), frame->depth, frame->nChannels );
cvSetImageROI( frame, *cur_r.at(0) );
cvCopy(frame, leftTmp);
cvResetImageROI(frame);
cur_img.insert( cur_img.end(), leftTmp );
cvSetImageROI( frame, *cur_r.at(1) );
cvCopyImage(frame, rightTmp);
cvResetImageROI(frame);
cur_img.insert( cur_img.end(),rightTmp );
if( match( cur_img, cur_r ) ) {
if( startTime != 0 && blinksec > 2 ) {
flag_match = true;
return true;
}
return false;
}
else
flag_match = true;
}
// release
if(!cur_img.empty())cur_img.clear();
if(!cur_r.empty())cur_r.clear();
if(leftTmp)cvReleaseImage(&leftTmp);
if(rightTmp)cvReleaseImage(&rightTmp);
return false;
}
void BlinkDetection::createTemplate(IplImage* frame,CvRect* leftEye,CvRect* rightEye) {
if( frame && leftEye && rightEye ) {
if(!templ_img.empty())templ_img.clear();
if(!templ_r.empty())templ_r.clear();
int width, height;
CvRect* lSize = 0;
CvRect* rSize = 0;
IplImage* leftTmp = 0;
IplImage* rightTmp = 0;
lSize = new CvRect();
rSize = new CvRect();
width = (leftEye->width + rightEye->width)/2;
height = (leftEye->height + rightEye->height)/2;
// fit template window to eye
lSize->x = leftEye->x + width/33;
rSize->y = leftEye->y + height/4;
rSize->x = rightEye->x + width/45;
lSize->y = rightEye->y + height/4;
width = width*0.9;
height = height*0.8;
lSize->width = width;
lSize->height = height;
rSize->width = width;
rSize->height = height;
templ_r.insert(templ_r.end(), leftEye);
templ_r.insert(templ_r.end(), rightEye);
templ_r.insert(templ_r.begin(), rSize);
templ_r.insert(templ_r.begin(), lSize);
leftTmp = cvCreateImage( cvSize(width,height), frame->depth, frame->nChannels );
rightTmp = cvCreateImage( cvSize(width,height), frame->depth, frame->nChannels );
cvSetImageROI( frame, *templ_r.at(0) );
cvCopyImage(frame, leftTmp, NULL );
cvResetImageROI(frame);
templ_img.insert( templ_img.end(), cvCloneImage(leftTmp) );
cvSetImageROI( frame, *templ_r.at(1) );
cvCopyImage(frame, rightTmp, NULL);
cvResetImageROI(frame);
templ_img.insert( templ_img.end(), cvCloneImage(rightTmp) );
if(leftTmp)cvReleaseImage(&leftTmp);
if(rightTmp)cvReleaseImage(&rightTmp);
}
}
bool BlinkDetection::match( vector<IplImage*> cur_img, vector<CvRect*> cur_r ) {
int x = 0;
int y = 0;
int width = 0;
int height = 0;
IplImage* tm_img = 0;
double maxval, leftMaxval,rightMaxval;
CvPoint leftMaxloc,rightMaxloc;
CvMat* mat = 0;
CvMat stub;
if( cur_img.empty() && cur_r.empty() )
return false;
/* left eye */
// size of matching window
width = cur_img.at(0)->width - templ_img.at(0)->width + 1;
height = cur_img.at(0)->height - templ_img.at(0)->height + 1;
if( width <= 0 && height <= 0 )
return false;
tm_img = cvCreateImage(cvSize(width, height), IPL_DEPTH_32F, 1);
// template matching
cvMatchTemplate(cur_img.at(0), templ_img.at(0), tm_img, CV_TM_CCOEFF_NORMED);
cvMinMaxLoc(tm_img, 0, &leftMaxval, 0, &leftMaxloc, 0);
/* right eye */
// size of matching window
if(tm_img)cvReleaseImage(&tm_img);
width = cur_img.at(1)->width - templ_img.at(1)->width + 1;
height = cur_img.at(1)->height - templ_img.at(1)->height + 1;
tm_img = cvCreateImage(cvSize(width, height), IPL_DEPTH_32F, 1);
// template matching
cvMatchTemplate(cur_img.at(1), templ_img.at(1), tm_img, CV_TM_CCOEFF_NORMED);
cvMinMaxLoc(tm_img, 0, &rightMaxval, 0, &rightMaxloc, 0);
maxval = (leftMaxval + rightMaxval) / 2;
//cout << "val of eyes: " << leftMaxval << " " << rightMaxval << " " << maxval << endl;
if( maxval < 0.45 ) { // lost eye position
return false;
cout << maxval << " lost eye" <<endl;
}
else if( maxval >= 0.45 && maxval < 0.6 ) {// closed eye
--count_lost;
if(startTime == 0)
startTime = time(0);
flag_ncc[1] = true;
cout << maxval << " closed eye" <<endl;
}
else if( maxval > 0.75 ) { // open eye
--count_lost;
if(flag_ncc[1]) {
flag_ncc[2] = true;
this->blinksec = difftime(curTime,startTime);
startTime = 0; cout<< "1";
}
else{
--count_lost;
flag_ncc[0] = true;
startTime = 0;cout<< "2";
}
cout << maxval << " open eye" <<endl;
} else {
--count_lost; // something in between
startTime = 0;
cout << maxval << " between " <<endl;
}
if( flag_ncc[0] && flag_ncc[1] && flag_ncc[2] ) {
flag_ncc[0] = (flag_ncc[1]= (flag_ncc[2]= false ));
cout << maxval << " true blinked " <<endl;
}
return true;
}