/
eyeanalyzer.cpp
117 lines (101 loc) · 2.82 KB
/
eyeanalyzer.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
#include "eyeanalyzer.h"
EyeAnalyzer::EyeAnalyzer(QObject *parent) :
QObject(parent),
last10EyePixel(50),
last50EyePixel(100)
{
eyeCloseThresh = 5;
isEyeBlinked = false;
isEyeClosed = false;
cliffDiff = 1;
lastCliffDiff = 2;
lastState = EYE_OPEN;
currState = EYE_OPEN;
showBlink = 0;
}
void EyeAnalyzer::setCliffDiff(int diff) {
this->cliffDiff = diff;
}
void EyeAnalyzer::doAnalyze(void) {
clearStatus();
getEyeStatus();
}
void EyeAnalyzer::getEyeStatus() {
int i = 0;
int eyeCloseCount = 0;
int eyeBlinkCount = 0;
// Blinking and closing detection
if (cliffDiff < eyeCloseThresh)
{
isEyeClosed = true;
currState = EYE_CLOSED;
}
else
{
isEyeClosed = false;
currState = EYE_OPEN;
}
if (currState == EYE_OPEN && lastState == EYE_CLOSED)
{
isEyeBlinked = true;
showBlink = 5;
emit eyeStatus(EYE_STATUS_WARNING);
}
else
{
isEyeBlinked = false;
}
lastCliffDiff = cliffDiff;
lastState = currState;
if (showBlink > 0)
{
emit eyeStatus(EYE_STATUS_BLINK);
//util::printText(originalFrame, text, cv::Scalar(255, 255, 0), cv::Point(10, 20));
showBlink--;
}
// Ring buffers
last10EyePixel.put((int)isEyeClosed);
last50EyePixel.put((int)isEyeBlinked);
// Statistic
// Sleep
if (last10EyePixel.isFull() == true) {
for (i = 0; i < last10EyePixel.getSize(); i++) {
if (last10EyePixel.getBase()[i] == (int)true && i != last10EyePixel.getRearIndex()) {
eyeCloseCount++;
}
}
if (eyeCloseCount >= last10EyePixel.getSize() * 0.8) {
emit eyeStatus(EYE_STATUS_CLOSED);
//util::printText(originalFrame, text, cv::Scalar(0, 0, 255), cv::Point(10, 50));
}
}
// Blinking rate
if (last50EyePixel.isFull() == true)
{
for (i = 0; i < last50EyePixel.getSize(); i++)
{
if (last50EyePixel.getBase()[i] == (int)true && i != last50EyePixel.getRearIndex())
{
eyeBlinkCount++;
}
}
blinkRate = eyeBlinkCount;
//sprintf(text_c_str, "Blink rate = %d / 100 frame", blinkRate);
//text.assign(text_c_str);
//if(blinkRate <= 7)
//util::printText(originalFrame, text, cv::Scalar(255, 255, 0), cv::Point(10, 90));
//else
//util::printText(originalFrame, text, cv::Scalar(0, 0, 255), cv::Point(10, 90));
//std::cout << text << std::endl;
}
}
void EyeAnalyzer::setOriginalFrame(const cv::Mat& frame) {
frame.copyTo(originalFrame);
}
cv::Mat EyeAnalyzer::getOriginalFrame() {
return originalFrame;
}
void EyeAnalyzer::clearStatus(void) {
isEyeBlinked = false;
isEyeClosed = false;
}