-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheyetracker.cpp
208 lines (194 loc) · 6.45 KB
/
eyetracker.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
//compilar con g++ <archivo.cpp> -o <archivo.out> `pkg-config --cflags --libs opencv x11`
#include <iostream>
#include <fstream>
#include <ctime>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <X11/Xlib.h>
cv::Vec3f getEyeball(cv::Mat &eye, std::vector<cv::Vec3f> &circles)
{
std::cerr << "Flag getEyeball." << std::endl;
std::vector<int> sums(circles.size(), 0);
for (int y = 0; y < eye.rows; y++)
{
uchar *ptr = eye.ptr<uchar>(y);
for (int x = 0; x < eye.cols; x++)
{
int value = static_cast<int>(*ptr);
for (int i = 0; i < circles.size(); i++)
{
cv::Point center((int)std::round(circles[i][0]), (int)std::round(circles[i][1]));
int radius = (int)std::round(circles[i][2]);
if (std::pow(x - center.x, 2) + std::pow(y - center.y, 2) < std::pow(radius, 2))
{
sums[i] += value;
}
}
++ptr;
}
}
int smallestSum = 9999999;
int smallestSumIndex = -1;
for (int i = 0; i < circles.size(); i++)
{
if (sums[i] < smallestSum)
{
smallestSum = sums[i];
smallestSumIndex = i;
}
}
return circles[smallestSumIndex];
}
cv::Rect getLeftmostEye(std::vector<cv::Rect> &eyes)
{
std::cerr << "Flag getLeftmostEye." << std::endl;
int leftmost = 99999999;
int leftmostIndex = -1;
for (int i = 0; i < eyes.size(); i++)
{
if (eyes[i].tl().x < leftmost)
{
leftmost = eyes[i].tl().x;
leftmostIndex = i;
}
}
return eyes[leftmostIndex];
}
std::vector<cv::Point> centers;
cv::Point lastPoint;
cv::Point mousePoint;
cv::Point stabilize(std::vector<cv::Point> &points, int windowSize)
{
std::cerr << "Flag Point stabilize." << std::endl;
float sumX = 0;
float sumY = 0;
int count = 0;
for (int i = std::max(0, (int)(points.size() - windowSize)); i < points.size(); i++)
{
sumX += points[i].x;
sumY += points[i].y;
++count;
}
if (count > 0)
{
sumX /= count;
sumY /= count;
}
return cv::Point(sumX, sumY);
}
void detectEyes(cv::Mat &frame, cv::CascadeClassifier &faceCascade, cv::CascadeClassifier &eyeCascade)
{
std::cerr << "Flag detectEyes." << std::endl;
cv::Mat grayscale;
cv::cvtColor(frame, grayscale, CV_BGR2GRAY); // convert image to grayscale
cv::equalizeHist(grayscale, grayscale); // enhance image contrast
std::vector<cv::Rect> faces;
faceCascade.detectMultiScale(grayscale, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, cv::Size(150, 150));
if (faces.size() == 0) return; // none face was detected
cv::Mat face = grayscale(faces[0]); // crop the face
std::vector<cv::Rect> eyes;
eyeCascade.detectMultiScale(face, eyes, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, cv::Size(30, 30)); // same thing as above
rectangle(frame, faces[0].tl(), faces[0].br(), cv::Scalar(255, 0, 0), 2);
if (eyes.size() != 2) return; // both eyes were not detected
for (cv::Rect &eye : eyes)
{
rectangle(frame, faces[0].tl() + eye.tl(), faces[0].tl() + eye.br(), cv::Scalar(0, 255, 0), 2);
}
cv::Rect eyeRect = getLeftmostEye(eyes);
cv::Mat eye = face(eyeRect); // crop the leftmost eye
cv::equalizeHist(eye, eye);
std::vector<cv::Vec3f> circles;
cv::HoughCircles(eye, circles, CV_HOUGH_GRADIENT, 1, eye.cols / 8, 250, 15, eye.rows / 8, eye.rows / 3);
if (circles.size() > 0)
{
cv::Vec3f eyeball = getEyeball(eye, circles);
cv::Point center(eyeball[0], eyeball[1]);
centers.push_back(center);
center = stabilize(centers, 5);
if (centers.size() > 1)
{
cv::Point diff;
diff.x = (center.x - lastPoint.x) * 20;
diff.y = (center.y - lastPoint.y) * -30;
mousePoint += diff;
}
lastPoint = center;
int radius = (int)eyeball[2];
cv::circle(frame, faces[0].tl() + eyeRect.tl() + center, radius, cv::Scalar(0, 0, 255), 2);
cv::circle(eye, center, radius, cv::Scalar(255, 255, 255), 2);
}
cv::imshow("Eye", eye);
}
void changeMouse(cv::Mat &frame, cv::Point &location, std::ofstream &prueba)
{
time_t now = time(0);
tm *ltm = localtime(&now);
std::cerr << "Flag changeMouse." << std::endl;
if (location.x > frame.cols) location.x = frame.cols;
if (location.x < 0) location.x = 0;
if (location.y > frame.rows) location.y = frame.rows;
if (location.y < 0) location.y = 0;
system(("xdotool mousemove " + std::to_string(location.x) + " " + std::to_string(location.y)).c_str());
std::cout << location.x << "," << location.y << std::endl;
prueba << 1 + ltm->tm_hour << ":" << 1 + ltm->tm_min << ":" << 1 + ltm->tm_sec << "," << location.x << "," << location.y << "\n" ;
}
int main(int argc, char **argv)
{
int idx,width,height;
Display* disp = XOpenDisplay(NULL);
Screen* scrn = DefaultScreenOfDisplay(disp);
width = scrn->width;
height = scrn->height;
std::cout << "Webcam Index: " ;
std::cin >> idx;
if (argc != 2)
{
std::cerr << "Usage: EyeDetector <WEBCAM_INDEX>" << std::endl;
//return -1;
}
cv::CascadeClassifier faceCascade;
cv::CascadeClassifier eyeCascade;
if (!faceCascade.load("./haarcascade_frontalface_alt.xml"))
{
std::cerr << "Could not load face detector." << std::endl;
return -1;
}
if (!eyeCascade.load("./haarcascade_eye_tree_eyeglasses.xml"))
{
std::cerr << "Could not load eye detector." << std::endl;
return -1;
}
//cv::VideoCapture cap("./sample.mp4"); // the fist webcam connected to your PC
cv::VideoCapture cap(idx); // the fist webcam connected to your PC
if (!cap.isOpened())
{
std::cerr << "Webcam not detected." << std::endl;
return -1;
}
int nbImage=0;
cv::Mat frame0;
//mousePoint = cv::Point(800, 800);
mousePoint = cv::Point(width/2,height/2);
std::ofstream prueba;
prueba.open ("ejemplo.csv");
while (true)
{
cap >> frame0; // outputs the webcam image to a Mat
if (!frame0.data) break;
detectEyes(frame0, faceCascade, eyeCascade);
changeMouse(frame0, mousePoint, prueba);
cv::imshow("Video0", frame0); // displays the Mat
int c = cvWaitKey(60);
if (static_cast<char>(c)=='s')
{
imwrite(format("image0_%d.jpg",nbImage),frame0);
//imwrite(format("image1_%d.jpg",nbImage++),frame1);
}
//exit the loop if user press "Esc" key (ASCII value of "Esc" is 27)
if(27 == char(c)) break;
}
prueba.close();
return 0;
}