forked from juraara/opencv-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cv.cpp
144 lines (123 loc) · 4.19 KB
/
cv.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
/* Image Processing Module
* (Contour Detection)
* Note: If the pixel value is smaller than the threshold, it is set to 0, otherwise it is set to a maximum value */
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <time.h>
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;
}
/* Compare Contour Areas */
bool compareContourAreas(vector<Point> contour1, vector<Point> contour2) {
double i = fabs(contourArea(Mat(contour1))); // return absolute value
double j = fabs(contourArea(Mat(contour2))); // return absolute value
return (i < j);
}
/* Main */
int main() {
/* PERCLOS */
int eyeState[65]; // 65 frames in 14fps should make 5fps for PERCLOS
double perclos = 0; // store result
int counter;
// double counterTime = 0; // 5s counter
// int overallTime = 0; // overall time
/* Contour Detection */
Mat crop, gray, blur, thresh;
int minThresh = 20; // for thresholding
int maxThresh = 255; // for thresholding
vector<vector<Point>> contours; // for finding contours
vector<Vec4i> hierarchy; // for finding contours
/* Camera */
VideoCapture cap(0); // default cam
Mat frame; // store frames here
int frameNo = 0;
/* Blink Util */
int tempEyeState = 0;
while (true) {
clock_t start = lClock();
cap.read(frame); // read stored frame
// imshow("Image", frame); // window
if (frame.empty()) break;
/* Calculate Histogram */
crop = frame(Rect(170, 180, 230, 140)); // crop frame
// imshow("Crop", crop); // display window
cvtColor(crop, gray, COLOR_BGR2GRAY); // convert to grayscale
imshow("Grayscale", gray); // display window
GaussianBlur(gray, blur, Size(9, 9), 0); // apply gaussian blur
// imshow("GaussianBlur", blur); // display window
threshold(blur, thresh, minThresh, maxThresh, THRESH_BINARY_INV); // apply thresholding
imshow("Threshold", thresh); // display window
findContours(thresh, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
sort(contours.begin(), contours.end(), compareContourAreas);
/* Create delay (500ms) after blink is detected (testing) */
/* if ((lClock() - fetchedClock) > 500) {
fetchedClock = lClock(); // start delay
} */
if (contours.size() == 0) {
tempEyeState = 1;
eyeState[counter] = 1; // blink
}
else {
tempEyeState = 0;
eyeState[counter] = 0; // blink~janai
}
counter++; // increment counter
if(counter == 65) {
counter = 0;
/* Calculate PERCLOS */
int sum = 0;
for (int j = 0; j < 65; j++) {
sum += eyeState[j];
}
perclos = (sum/65.0) * 100;
}
/* Print to Terminal */
double duration = lClock() - start; // stop counting
double averageTimePerFrame = averageDuration(duration); // avg time per frame
if (tempEyeState == 1) {
cout << "(Close)" << " Avg tpf: " << averageTimePerFrame << "ms" << " Avg fps: " << averageFps() << " Perclos: " << perclos << " Frame no: " << frameNo++ << endl;
} else {
cout << "(Open)" << " Avg tpf: " << averageTimePerFrame << "ms" << " Avg fps: " << averageFps() << " Perclos: " << perclos << " Frame no: " << frameNo++ << endl;
}
/* Exit at esc key */
if (waitKey(1) == 27) {
cout << "Program terminated." << endl;
break;
}
/* Exit at esc key */
if (waitKey(1) == 27) {
cout << "Program terminated." << endl;
break;
}
}
cap.release();
destroyAllWindows();
return 0;
}