-
Notifications
You must be signed in to change notification settings - Fork 47
/
node_helper.js
137 lines (119 loc) · 4.35 KB
/
node_helper.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
/* Magic Mirror
* Module: MMM-Face-Reco-DNN
*
* By Thierry Nischelwitzer http://nischi.ch
* MIT Licensed.
*/
/*global require, module, log*/
'use strict';
const NodeHelper = require('node_helper');
const { PythonShell } = require('python-shell');
const onExit = require('signal-exit');
var pythonStarted = false;
module.exports = NodeHelper.create({
pyshell: null,
python_start: function () {
const self = this;
const extendedDataset = this.config.extendDataset ? 'True' : 'False';
const options = {
mode: 'json',
pythonOptions: ['-u'], // Immediately flush buffer for std out/in monitoring/writing to work
stderrParser: line => JSON.stringify(line),
args: [
'--cascade=' + this.config.cascade,
'--encodings=' + this.config.encodings,
'--rotateCamera=' + this.config.rotateCamera,
'--method=' + this.config.method,
'--detectionMethod=' + this.config.detectionMethod,
'--interval=' + this.config.checkInterval,
'--output=' + this.config.output,
'--outputmm=' + this.config.outputmm,
'--extendDataset=' + extendedDataset,
'--dataset=' + this.config.dataset,
'--tolerance=' + this.config.tolerance,
'--brightness=' + this.config.brightness,
'--contrast=' + this.config.contrast,
'--resolution=' + this.config.resolution,
'--processWidth=' + this.config.processWidth,
'--run-only-on-notification=' + (this.config.external_trigger_notification !== '' ? '1' : '0'),
],
};
if (this.config.pythonPath != null && this.config.pythonPath !== '') {
options.pythonPath = this.config.pythonPath;
}
// Start face reco script
self.pyshell = new PythonShell('modules/' + this.name + '/tools/recognition.py', options);
// check if a message of the python script is comming in
self.pyshell.on('message', function (message) {
// A status message has received and will log
if (Object.prototype.hasOwnProperty.call(message, 'status')) {
console.log('[' + self.name + '] ' + message.status);
}
// Somebody new are in front of the camera, send it back to the Magic Mirror Module
if (Object.prototype.hasOwnProperty.call(message, 'camera_image')) {
self.sendSocketNotification('camera_image', {
image: message.camera_image.image,
});
}
// Check if we get an image to show in the mirror
if (Object.prototype.hasOwnProperty.call(message, 'login')) {
console.log('[' + self.name + '] ' + 'Users ' + message.login.names.join(' - ') + ' logged in.');
self.sendSocketNotification('user', {
action: 'login',
users: message.login.names,
});
}
// Somebody left the camera, send it back to the Magic Mirror Module
if (Object.prototype.hasOwnProperty.call(message, 'logout')) {
console.log('[' + self.name + '] ' + 'Users ' + message.logout.names.join(' - ') + ' logged out.');
self.sendSocketNotification('user', {
action: 'logout',
users: message.logout.names,
});
}
});
onExit(function (_code, _signal) {
self.destroy();
});
},
send_python_cmd: function (cmd) {
this.pyshell.send(cmd);
},
python_stop: function () {
this.destroy();
},
destroy: function () {
const self = this
this.pyshell.end(function (err) {
if (err) throw err;
console.log('[' + self.name + '] ' + 'finished running...');
});
console.log('[' + this.name + '] ' + 'Terminate python');
this.pyshell.childProcess.kill();
},
socketNotificationReceived: function (notification, payload) {
// Configuration are received
if (notification === 'CONFIG') {
this.config = payload;
// Set static output to 0, because we do not need any output for MMM
this.config.output = 0;
if (!pythonStarted) {
pythonStarted = true;
this.python_start();
}
}
// Notification for triggering face recognition received. Only send to python subprocess
// if it has been started
if (notification === this.config.external_trigger_notification && pythonStarted) {
if (payload === true) {
this.send_python_cmd('start');
} else {
this.send_python_cmd('stop');
}
}
},
stop: function () {
pythonStarted = false;
this.python_stop();
},
});