-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
244 lines (177 loc) · 6.08 KB
/
index.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
237
238
239
240
241
242
243
244
/*******************************************************************************
* Copyright (c) 2015 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
'use strict';
var fs = require('fs');
var HumixSense = require('humix-sense');
var path = require('path');
var _ = require('lodash');
var config = require('./lib/config');
var FaceRec = require('./lib/facerec').FaceRec;
var moduleConfig = {
moduleName: 'facerec',
commands: [
'train',
'detect-face',
'no-detect',
'detect-motion',
'track-face',
'no-track'
],
events: [
'detectComplete',
'faceDetected',
'trainComplete',
'facePosition',
'motion-detected',
'face-right',
'face-left',
'face-up',
'face-down'
],
debug: true
}
var humix = new HumixSense(moduleConfig);
var hsm;
var hfr = new FaceRec(config);
var trainedImg = path.resolve(__dirname, 'images', 'orig', 'trained.jpg');
var origImgRoot = path.resolve(__dirname, 'images', 'orig');
// long running events
var detecting;
var detectingMotion;
var faceTrackingTask;
var detectedHistory = {};
humix.on('connection', function(humixSensorModule) {
hsm = humixSensorModule;
console.log('Communication with humix-sense is now ready.');
hsm.on('train', function(data) {
data = JSON.parse(data);
if (hfr.captureAndTrain(data.name, 20)) {
var image = fs.readFileSync(trainedImg).toString('base64');
hsm.event('trainComplete',
JSON.stringify({
id: data.id,
image: image
}));
} else {
//failed to perform the training
hsm.event('trainComplete',
JSON.stringify({
id: data.id,
msg: 'failed'
}));
}
});
hsm.on('detect-face', function(data) {
data = JSON.stringify(data);
console.log('start detecting face');
detecting = setInterval(doDetect, 1000);
});
hsm.on('no-detect', function(data) {
console.log('disable face detection');
if (detecting) {
clearInterval(detecting);
detecting = undefined;
}
});
hsm.on('detect-motion', function(data) {
console.log('start detecting motion');
if (detectingMotion) {
clearInterval(detectingMotion);
detectingMotion = undefined;
}
//detectingMotion = setInterval
});
hsm.on('track-face', function(data) {
console.log('start tracking face');
doFaceTracking();
});
hsm.on('no-track', function(data) {
console.log('disable face tracking');
if (faceTrackingTask) {
clearInterval(faceTrackingTask);
faceTrackingTask = undefined;
}
});
});
var facePrevX = -1;
var facePrevY = -1;
var threshold = 50;
var baseX = 300;
var baseY = 260;
function doFaceTracking() {
if (faceTrackingTask) {
console.log('disable previous face tracking task');
clearInterval(detectingMotion);
faceTrackingTask = null;
}
faceTrackingTask = setInterval(function() {
var result = hfr.track();
//console.log('result -- ' + JSON.stringify(result));
if (result && result.pos_x && result.pos_y) {
var newX = result.pos_x;
var newY = result.pos_y;
console.log('old x:' + facePrevX + ", new x:" + newX);
console.log('old y:' + facePrevY + ", new x:" + newY);
var deltaX = newX - baseX;
var deltaY = newY - baseY;
if (Math.abs(deltaX) > threshold) {
var direction = deltaX > 0 ? "right" : "left";
console.log("moving head to " + direction + " , distance" + Math.abs(deltaX));
hsm.event(deltaX > 0 ? 'face-right' : 'face-left',
JSON.stringify({
posX: newX,
deltaX: deltaX
}));
console.log('updating x ');
facePrevX = newX;
}
if (Math.abs(deltaY) > threshold) {
var direction = deltaY > 0 ? "down" : "up";
console.log("moving head to " + direction + " , distance" + Math.abs(deltaY));
hsm.event(deltaY > 0 ? 'face-down' : 'face-up',
JSON.stringify({
posY: newY,
deltaY: deltaY
}));
facePrevY = newY;
}
}
}, 2000);
}
function doDetect() {
var result = hfr.detect();
if (!_.isUndefined(result)) {
//see if we detect this person within 30 seconds
try {
var historyEntry = detectedHistory[result.name];
//if (_.isUndefined(historyEntry) || (Date.now() - historyEntry) > 30000) {
detectedHistory[result.name] = Date.now();
//var img = fs.readFileSync(path.resolve(origImgRoot, result.name + '.jpg')).toString('base64');
console.log('face detected. Pos:' + result.pos_x + ',' + result.pos_y);
hsm.event('faceDetected',
JSON.stringify({
name: result.name,
conf: result.conf,
pos_x: result.pos_x,
pos_y: result.pos_y
//image: img
}));
//}
} catch (e) {
console.error(e);
}
}
}