-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
236 lines (194 loc) · 6.36 KB
/
app.js
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
"use strict";
var Path = require("path");
var Cylon = require("cylon");
var ws = require("nodejs-websocket");
var http = require('http');
var url = require('url');
var path = require('path');
var fs = require('fs');
var windowed = true;
process.argv.forEach(function (val) {
if (val === "--no-window") {
windowed = false;
}
if (val === "--help") {
console.log("Usage: node " + Path.basename(process.argv[1]) + " [--no-window] [--help]");
process.exit(1);
}
});
var EventedArray = function(handler) {
this.stack = [];
this.mutationHandler = handler || function() {};
this.setHandler = function(f) {
this.mutationHandler = f;
};
this.callHandler = function() {
if(typeof this.mutationHandler === 'function') {
this.mutationHandler();
}
};
this.push = function(obj) {
this.stack.push(obj);
this.callHandler();
};
this.pop = function() {
return this.stack.pop();
};
this.getArray = function() {
return this.stack;
}
this.clearArray = function() {
return this.stack = [];
}
}
var messageReceivedQueue = new EventedArray();
var clearMessageReceivedQueue = function() {
messageReceivedQueue.clearArray();
}
var server = ws.createServer(function (conn) {
if (server.connections.length == 1) {
clearMessageReceivedQueue();
}
messageReceivedQueue.setHandler(function(){
var recentMessage = messageReceivedQueue.pop();
if (recentMessage)
var message = { "facesDected": recentMessage.length,
"faceDetails": recentMessage
};
server.connections.forEach(function (conn) {
conn.sendText(JSON.stringify(message));
})
});
conn.on("close", function (code, reason) {
if (server.connections.length == 0) {
clearMessageReceivedQueue();
}
})
}).listen(8001)
var allowedFiles = ['/view.html', '/fabulous.js', '/paper-full.js', '/poster.png'];
var httpServer = http.createServer(function (request, response) {
// Get the filename from the request.
var pathToFile = url.parse(request.url).pathname;
if (pathToFile === '/' || pathToFile === '') {
pathToFile = '/view.html';
}
// Do not serve the other files.
if (allowedFiles.indexOf(pathToFile) == -1) {
response.writeHead(404, { 'Content-Type': 'text/plain'});
response.write('404 Not Found\n');
response.end();
return;
}
var filename = path.join(process.cwd(), pathToFile);
fs.readFile(filename, 'binary', function (err, file) {
if (err) {
response.writeHead(404, { 'Content-Type': 'text/plain'});
response.write('404 Not Found\n');
response.end();
return;
}
response.writeHead(200);
response.write(file, 'binary');
response.end();
});
}).listen(8080, function () {
console.log('HTTP Server is running at http://localhost:%s', 8080);
});
Cylon.robot({
connections: {
opencv: { adaptor: "opencv" }
},
devices: {
window: { driver: "window" },
camera: {
driver: "camera",
camera: 0,
haarcascade: __dirname + "/haarcascade_frontalface_alt.xml"
}
},
faces: undefined,
work: function(my) {
// We setup our face detection when the camera is ready to
// display images, we use `once` instead of `on` to make sure
// other event listeners are only registered once.
my.camera.once("cameraReady", function() {
console.log("The camera is ready!");
// We add a listener for the facesDetected event
// here, we will get (err, image/frame, faces) params back in
// the listener function that we pass.
// The faces param is an array conaining any face detected
// in the frame (im).
my.camera.on("facesDetected", function(err, im, faces) {
if (err) { console.log(err); }
// We loop through the faces and manipulate the image
// to display a square in the coordinates for the detected
// faces.
if( this.faces === undefined ) {
// setup
this.faces = faces;
}
var persistentFaces = [];
var matchedAnyFace = false;
// console.log("faces count: " + faces.length);
var self = this;
faces.forEach( function(face) {
var matchedMultipleFrames = false;
for (var i = 0; i < self.faces.length; i++) {
var otherFace = self.faces[i];
if (otherFace.matchCount === undefined) {
otherFace.matchCount = 0;
}
if (Math.abs(otherFace.x - face.x) < 100 && Math.abs(otherFace.y - face.y) < 100) {
otherFace.matchCount++;
matchedAnyFace = true;
// console.log("face matchCount: " + otherFace.matchCount);
if (otherFace.matchCount > 2) {
matchedMultipleFrames = true;
persistentFaces.push(otherFace);
}
break;
}
}
var color = [0, 255, 0];
if (matchedMultipleFrames === true) {
color = [255,0,0];
}
if (windowed === true) {
im.rectangle(
[face.x, face.y],
[face.width, face.height],
color,
2
);
}
}); // forEach
if (matchedAnyFace === false) {
// reset faces array
// console.log("reset");
this.faces = faces;
}
messageReceivedQueue.push(persistentFaces);
// The second to last param is the color of the rectangle
// as an rgb array e.g. [r,g,b].
// Once the image has been updated with rectangles around
// the faces detected, we display it in our window.
if (windowed === true) {
my.window.show(im, 40);
}
// After displaying the updated image we trigger another
// frame read to ensure the fastest processing possible.
// We could also use an interval to try and get a set
// amount of processed frames per second, see below.
my.camera.readFrame();
});
// We listen for frameReady event, when triggered
// we start the face detection passing the frame
// that we just got from the camera feed.
my.camera.on("frameReady", function(err, im) {
if (err) { console.log(err); }
my.camera.detectFaces(im);
});
my.camera.readFrame();
});
}
}).start();