forked from nischi/MMM-Face-Reco-DNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
129 lines (117 loc) · 3.61 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
/* Magic Mirror
* Module: MMM-Face-Reco-DNN
*
* By Thierry Nischelwitzer http://nischi.ch
* MIT Licensed.
*/
'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',
stderrParser: line => JSON.stringify(line),
args: [
'--cascade=' + this.config.cascade,
'--encodings=' + this.config.encodings,
'--usePiCamera=' + this.config.usePiCamera,
'--source=' + this.config.source,
'--rotateCamera=' + this.config.rotateCamera,
'--method=' + this.config.method,
'--detectionMethod=' + this.config.detectionMethod,
'--interval=' + this.config.checkInterval,
'--output=' + 0,
'--extendDataset=' + extendedDataset,
'--dataset=' + this.config.dataset,
'--tolerance=' + this.config.tolerance,
'--minArea=' + this.config.minArea
],
};
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/facerecognition.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 (message.hasOwnProperty('status')) {
console.log('[' + self.name + '] ' + message.status);
}
if (message.hasOwnProperty('motion')) {
console.log('[' +self.name +'] ' + 'Motion detected');
self.sendSocketNotification('motion');
}
// Somebody new are in front of the camera, send it back to the Magic Mirror Module
if (message.hasOwnProperty('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 (message.hasOwnProperty('logout')) {
console.log(
'[' +
self.name +
'] ' +
'Users ' +
message.logout.names.join(' - ') +
' logged out.'
);
self.sendSocketNotification('user', {
action: 'logout',
users: message.logout.names,
});
}
});
// Shutdown node helper
self.pyshell.end(function(err) {
if (err) throw err;
console.log('[' + self.name + '] ' + 'finished running...');
});
onExit(function(code, signal) {
self.destroy();
});
},
python_stop: function() {
this.destroy();
},
destroy: function() {
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();
}
}
},
stop: function() {
pythonStarted = false;
this.python_stop();
},
});