forked from juraara/opencv-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect_eyes.cpp
151 lines (130 loc) · 4.5 KB
/
detect_eyes.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
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <iostream>
#include <vector>
using namespace cv;
using namespace std;
double _averageDuration = 0; // average duration (averageDuration)
int _fetchedClock = 0; // gets lClock() at 1s (averageFps)
double _averageFps = 0; // average fps (averageFps)
double _frameNo = 0; // no of frames in 1s (averageFps)
/* Clock */
int lClock() {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
// cout << "lClock: " << (ts.tv_sec * 1000) + (ts.tv_nsec * 1e-6) << endl;
return (ts.tv_sec * 1000) + (ts.tv_nsec * 1e-6);
}
/* Average Duration */
double averageDuration(double newDuration) {
_averageDuration = 0.98 * _averageDuration + 0.02 * newDuration;
return _averageDuration;
}
/* Average fps */
double averageFps() {
if ((lClock() - _fetchedClock) > 1000) {
_fetchedClock = lClock();
_averageFps = 0.7 * _averageFps + 0.3 * _frameNo;
// cout << "fps: " << _frameNo << endl;
_frameNo = 0;
}
_frameNo++;
return _averageFps;
}
int main() {
/* Contour Detection */
Mat crop, gray, blur, thresh;
int minThresh = 70; // for thresholding
int maxThresh = 255; // for thresholding
/* Camera */
VideoCapture cap(0); // default cam
Mat frame; // store frames here
int frameNo = 0;
/* Eye Detection */
CascadeClassifier faceCascade;
vector<Rect> faces;
Mat eyeROI;
Mat croppedEye;
faceCascade.load("./haarcascade_eye_tree_eyeglasses.xml");
int height = 0, width = 0;
float prevFrame = 0.0;
float currentFrame = 0.0;
float percentageDifference = 0.0;
int delay = 0; // delay
/* Histogram Utils */
MatND currentHist, prevHist;
int histSize = 256;
const int* channelNumbers = { 0 };
float channelRange[] = { 0.0, 256.0 };
const float* channelRanges = channelRange;
int numberBins = 256;
/* Init */
cap.read(frame); // read stored frame
crop = frame(Rect(170, 180, 230, 140)); // crop frame
cvtColor(crop, gray, COLOR_BGR2GRAY); // convert to grayscale
calcHist(&gray, 1, 0, Mat(), currentHist, 1, &numberBins, &channelRanges);
calcHist(&gray, 1, 0, Mat(), prevHist, 1, &numberBins, &channelRanges);
while (true) {
cap.read(frame); // read stored frame
if (frame.empty()) break;
crop = frame(Rect(170, 180, 230, 140)); // crop frame
cvtColor(crop, gray, COLOR_BGR2GRAY); // convert to grayscale
/* Calculate Histogram */
if (delay < 15) {
delay++;
calcHist(&gray, 1, 0, Mat(), currentHist, 1, &numberBins, &channelRanges);
} else {
delay = 0;
double histMatchingCorrelation = compareHist(currentHist, prevHist, HISTCMP_CORREL);
cout << "Matching Correlation: " << histMatchingCorrelation << endl;
/* Detect Eyes if Hist Value Changes */
if (histMatchingCorrelation < 0.95) {
vector<Rect> faces;
faceCascade.detectMultiScale(gray, faces, 1.1, 10);
for (int i = 0; i < faces.size(); i++) {
eyeROI = gray(faces[i]);
height = faces[i].height; cout << "Height: " << height << endl;
width = faces[i].width; cout << "Widht: " << width << endl;
croppedEye = eyeROI;
rectangle(gray, faces[i].tl(), faces[i].br(), Scalar(255, 255, 255), 2);
}
}
calcHist(&gray, 1, 0, Mat(), prevHist, 1, &numberBins, &channelRanges);
}
imshow("Graysclade", gray); // display window
if (!croppedEye.empty()) {
imshow("Cropped Eye", croppedEye); // display window
Mat histEqualized;
equalizeHist(croppedEye, histEqualized); // equalize
GaussianBlur(histEqualized, blur, Size(9, 9), 0); // apply gaussian blur
threshold(blur, thresh, minThresh, maxThresh, THRESH_BINARY_INV);
int lowerHeight = (int)((double)height*0.4);
int upperHeight = (int)((double)height*0.6);
Mat upper = thresh(Rect(0, 0, width, upperHeight));
Mat lower = thresh(Rect(0, upperHeight, width, lowerHeight));
/* Calculate Histogram for Upper and Lower Eye */
MatND upperHist, lowerHist;
calcHist(&upper, 1, 0, Mat(), upperHist, 1, &numberBins, &channelRanges);
calcHist(&lower, 1, 0, Mat(), lowerHist, 1, &numberBins, &channelRanges);
float lowerPixels = lowerHist.at<float>(255);
float upperPixels = upperHist.at<float>(255);
if (upperPixels < lowerPixels) {
cout << "close" << "fps" << averageFps() << endl;
} else {
cout << "open" << "fps" << averageFps() << endl;
}
imshow("Upper", upper);
imshow("Lower", lower);
}
/* Exit at esc key */
if (waitKey(1) == 27) {
cout << "Program terminated." << endl;
break;
}
}
cap.release();
destroyAllWindows();
return 0;
}