/
Eye_Tracking_Server.pde
289 lines (220 loc) · 8.38 KB
/
Eye_Tracking_Server.pde
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//__________________________________LIBARIES____________________________________//
import processing.video.*;
import gab.opencv.*;
import java.awt.Rectangle;
import processing.net.*;
//____________________________________GLOBAL VARIABLES______________________________//
Capture cam;
PImage mirrorCam;
PImage img;
OpenCV opencvEye;
OpenCV opencvFace;
OpenCV opencvBlob;
int scale = 4;
Rectangle[] eyes;
Rectangle[] face;
Rectangle[] blob;
int m;
int tolerance = 20;
float threshold = .28;
PImage smallerImgEye;
PImage smallerImgFace;
PImage BlobImage;
int pickedColor = color(0, 0, 0);
ArrayList<Contour> contours;
ArrayList<Contour> polygons;
float eyeX = 0;
float eyeY = 0;
Server s;
Client c;
String input;
int data[];
int interval = 20;
void setup() {
//_____________________________CANVAS SIZE______________________________________//
size(640, 360);
//_____________________________SET UP CAMERA______________________________________//
String[] cameras = Capture.list();
if (cameras.length == 0) {
println("No cameras available.");
exit();
} else {
println("Available cameras:");
for (int i = 0; i < cameras.length; i++) {
println(i + ": " + cameras[i]);
}
cam = new Capture(this, cameras[3]);
cam.start();
}
mirrorCam = createImage(640, 360, RGB);
//_____________________________OPEN CV SETUP______________________________________//
opencvEye = new OpenCV(this, 640/scale, 360/scale);
opencvFace = new OpenCV(this, 640/scale, 360/scale);
opencvEye.loadCascade("haarcascade_eye.xml");
opencvFace.loadCascade("haarcascade_frontalface_default.xml");
img = createImage(width, height, RGB);
//_____________________________Open CV Resizing______________________________________//
smallerImgEye = createImage(opencvEye.width, opencvEye.height, RGB);
smallerImgFace = createImage(opencvFace.width, opencvFace.height, RGB);
//_____________________________Connect the Sketches to Each other______________________________________//
s = new Server(this, 12345); // Start a simple server on a port
}
//void captureEvent(Capture cam) {
// cam.read();
//}
void draw() {
//_____________________________INITIALIZE CAMERA______________________________________//
if (cam.available()) {
cam.read();
mirroring();
smallerImgEye.copy(mirrorCam, 0, 0, mirrorCam.width, mirrorCam.height, 0, 0,
smallerImgEye.width, smallerImgEye.height);
smallerImgFace.copy(mirrorCam, 0, 0, mirrorCam.width, mirrorCam.height, 0, 0,
smallerImgFace.width, smallerImgFace.height);
smallerImgEye.updatePixels();
}
//_____________________________PIXELS______________________________________//
int h = mirrorCam.height;
int w = mirrorCam.width;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int i = x + y*w;
int r = int( red(mirrorCam.pixels[i]) );
int g = int( green(mirrorCam.pixels[i]) );
int b = int( blue(mirrorCam.pixels[i]) );
img.pixels[i] = color(r, g, b);
}
}
//_____________________________DISPLAY IMAGE______________________________________//
img.updatePixels();
image(img, 0, 0);
//_____________________________OPEN CV______________________________________//
opencvEye.loadImage(smallerImgEye);
opencvFace.loadImage(smallerImgFace);
eyes = opencvEye.detect();
face = opencvFace.detect();
//____________________________OPEN CV DRAW_____________________________________//
//image(opencvEye.getInput(), 0, 0);
//image(opencvFace.getInput(), 0, 0);
//__________________________FACE RECTANGLE________________________________________//
noFill();
stroke(0, 255, 0);
strokeWeight(3);
for ( m = 0; m < face.length; m++) {
rect(face[m].x *scale, face[m].y *scale, face[m].width *scale, face[m].height*scale);
//________________________EYE RECTANGLE_____________________________________________//
stroke(3, 134, 200);
strokeWeight(2);
for (int i = 0; i < eyes.length; i++) {
int faceSide = ((face[m].x) + (face[m].width));
//int faceTop = ((face[m].y) ; // bug
int faceBottom = ((face[m].y) + (face[m].height));
int eyeCenterX = eyes[i].x + eyes[i].width/2;
int eyeCenterY = eyes[i].y + eyes[i].height/2;
//________________________DEFINE WHERE TO DRAW EYES_________________________________________//
if (eyeCenterX > face[m].x && eyeCenterX < faceSide &&
eyeCenterY > face[m].y + face[m].height *1/8 && eyeCenterY < face[m].y + face[m].height *4/8) {
/*if ((eyes[i].y > (faceTop + adjustmentTop)) && (eyes[i].y < (face[m].y - adjustmentBottom))
&&(eyes[i].x < faceSide ) &&(eyes[i].x > face[m].x)) {*/
//________________________DRAW EYE RECTANGLES_____________________________________________//
rect(eyes[i].x *scale, eyes[i].y*scale, eyes[i].width*scale, eyes[i].height*scale);
PImage a = geteyeImage(mirrorCam, eyes[i].x *scale, eyes[i].y*scale, eyes[i].width*scale, eyes[i].height*scale);
a.filter(BLUR, 2);
a.filter(THRESHOLD, threshold);
image(a, 0, 0);
a.loadPixels();
//________________________COLOR TRACKING_____________________________________________//
int sumX = 0;
int sumY = 0;
int avgX = 0;
int avgY = 0;
int count = 0;
int ColorTrackw = a.width;
int ColorTrackh = a.height;
for (int ColorTracky = 0; ColorTracky < ColorTrackh; ColorTracky++) {
for (int ColorTrackx = 0; ColorTrackx < ColorTrackw; ColorTrackx++) {
int ColorTracki = ColorTrackx + ColorTracky*ColorTrackw;
int ColorTrackr = int( red(a.pixels[ColorTracki]) );
int ColorTrackg = int( green(a.pixels[ColorTracki]) );
int ColorTrackb = int( blue(a.pixels[ColorTracki]) );
float avgColor = (ColorTrackr+ColorTrackg+ColorTrackb) /3; // grayscaled
float focusedEye = 0.35;
if (avgColor < 100) {
if (ColorTrackx > ColorTrackw*focusedEye && ColorTrackx < ColorTrackw*(1-focusedEye) &&
ColorTracky < ColorTrackh*(1-focusedEye) && ColorTracky > ColorTrackw*(focusedEye)) {
sumX += ColorTrackx;
sumY += ColorTracky;
count++;
}
}
}
}
a.updatePixels();
// get average position to get the center position
if (count != 0) {
avgX = sumX / count;
avgY = sumY / count;
}
//eyeX = (int)map(avgX, 0, ColorTrackw, 0, 100);
//eyeY = (int)map(avgY, 0, ColorTrackh, 0, 100);
if (avgX != 0 && avgY != 0) {
eyeX = lerp(eyeX, map(avgX, 0, ColorTrackw, 0, 1), 0.1);
eyeY = lerp(eyeY, map(avgY, 0, ColorTrackh, 0, 1), 0.1);
String toSend = eyeX + " " +eyeY + "\n";
if (eyeX != 0 && eyeY != 0) {
print(toSend);
s.write(toSend);
}
pushStyle();
if (i == 0) {
fill(0, 0, 255);
} else {
fill(255, 0, 255);
}
ellipse((width*eyeX), (height*eyeY), 30, 30);
popStyle();
//lerpEyeX = lerp( avgX, eyeX, .1);
//lerpEyeY = lerp( avgY, eyeY, .1);
//_____________SEND VARIABLES TO OTHER SKETCH_____________________________________________//
}
}
println("---------------");
}
// show the center position
stroke(255, 0, 0);
//line(eyeX, 0, eyeX, a.width);
//line(0, eyeY, ColorTrackh, eyeY);
ellipse(eyeX, eyeY, 5, 5);
//println( " ( " +avgX + " , " +avgX + " ) ");
//println( " ( " +sumX + " , " +sumX + " ) ");
//______________________________Framerate______________________________________________//
fill(0);
textSize(10);
text(frameRate, 30, 60);
textSize(30);
text(eyeX, 50, 100);
text(eyeY, 150, 100);
}
}
PImage geteyeImage(PImage resource, int x, int y, int w, int h) {
PImage eyeImage = createImage(w, h, RGB);
eyeImage.copy(resource, x, y, w, h, 0, 0, w, h);
return eyeImage;
}
void keyPressed() {
println("pressed");
s.write(100 + " " + 100 + "\n");
}
void mirroring() {
int w = cam.width;
int h = cam.height;
cam.loadPixels();
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int camIndex = x + y*w;
int mirrorCamIndex = (w-1 - x) + y*w;
mirrorCam.pixels[mirrorCamIndex] = cam.pixels[camIndex];
}
}
mirrorCam.updatePixels();
}