-
Notifications
You must be signed in to change notification settings - Fork 2
/
watcher.js
269 lines (216 loc) · 7.25 KB
/
watcher.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
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
var settings = require('../package.json');
var path = require('path');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var exec = require('child_process').exec;
var proc;
var cv = require('opencv');
var _ = require('underscore');
var numbers = require('numbers');
var Watcher = function() {
var self = this;
self.settings = settings.config.watcher;
// (B)lue, (G)reen, (R)ed
self.playerColors = [
[255, 255, 255], //white (ball / match)
[0, 0, 255], //red (player 1)
[0, 255, 0], //green (player 2)
[255, 0, 0], //blue (player 3)
[0, 255, 255] //yellow (player 4)
];
self.CVoptions = {
camWidth: 1280,
camHeight: 720,
interval: 50, // refresh (polling) rate
maskLowThresh: 0,
maskHighThresh: 1,
threshold: 175, // http://docs.opencv.org/2.4/modules/imgproc/doc/miscellaneous_transformations.html?highlight=threshold#double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type)
blurRadius: 3,
maskFile: 'firepower-4player-mask.png'
//maskFile: 'firepower-1player-mask.png' // try changing this for just 1 player score
};
self.status = {
isReady: false,
scores: []
};
// initialize function
self.init = function() {
try {
//to use your webcam, uncomment this line, and comment out the test code below
//self.camera = new cv.VideoCapture(self.settings.index);
var testMOV = path.join(__dirname, 'img', 'firepower-4player.mp4');
console.log(testMOV);
self.camera = new cv.VideoCapture(testMOV);
if (self.camera) {
self.camera.setWidth(self.CVoptions.camWidth);
self.camera.setHeight(self.CVoptions.camHeight);
if (self.settings.debug) {
self.window = new cv.NamedWindow('Video', cv.CV_WINDOW_AUTOSIZE);
}
cv.readImage(path.join(__dirname, 'img', self.CVoptions.maskFile), function(err, im) {
if (err) {
self.showError(err);
} else {
self.mask = im;
}
});
if (self.findROI(self.CVoptions.maskFile) > 0) {
console.log("📸 tracking " + self.status.scores.length + " score regions.");
self.connect();
} else {
self.showError("no score regions found");
}
} else {
self.showError("no camera");
}
} catch (e) {
self.showError(e);
}
}
self.findROI = function() {
var out = new cv.Matrix(self.CVoptions.camHeight, self.CVoptions.camWidth);
im_canny = self.mask.copy();
im_canny.canny(self.CVoptions.maskLowThresh, self.CVoptions.maskHighThresh);
var contours = im_canny.findContours();
for (i = 0; i < contours.size(); i++) {
var rect = contours.boundingRect(i);
var JSONrect = JSON.stringify(rect);
var regionExists = false;
_.each(self.status.scores, function(score) {
if (JSON.stringify(score.rect) === JSONrect) {
regionExists = true;
}
});
if (!regionExists) {
score = {
rect: rect,
isWide: false,
smooth: [0, 0, 0, 0, 0], //score must appear at least 3 times before being reported
strValue: '00',
intValue: 0,
playerIndex: 0
};
self.status.scores.push(score);
}
}
self.getIndexes();
return self.status.scores.length;
}
self.getIndexes = function() {
var index = 0;
_.each(self.status.scores, function(score) {
var ROI = self.mask.roi(score.rect.x, score.rect.y, score.rect.width, score.rect.height);
//center pixel of masked score region for color
var val = ROI.pixel(Math.round(score.rect.height / 2), Math.round(score.rect.width / 2));
//uncomment to verify mask regions
/*
var file = path.join(__dirname, 'img/watch', 'area' + index + '.png');
ROI.save(file);
*/
_.each(self.playerColors, function(color) {
if (JSON.stringify(val) == JSON.stringify(color)) {
score.playerIndex = self.playerColors.indexOf(color);
score.color = color;
if (score.playerIndex > 0) {
score.isWide = true;
score.strValue = '000000';
}
}
});
index += 1;
});
}
self.connect = function() {
self.changeStatus(true);
setInterval(function() {
self.camera.read(function(err, im) {
if (err) {
self.showError(err);
} else {
if (im.size()[0] > 0 && im.size()[1] > 0) {
var index = 0;
_.each(self.status.scores, function(score) {
im.rectangle([score.rect.x, score.rect.y], [score.rect.width, score.rect.height], score.color, 2);
var ROI = im.roi(score.rect.x, score.rect.y, score.rect.width, score.rect.height);
ROI.convertGrayscale();
var invertROI = ROI.threshold(self.CVoptions.threshold, 255, "Binary Inverted");
//invertROI.gaussianBlur([self.CVoptions.blurRadius, self.CVoptions.blurRadius]);
score.file = path.join(__dirname, 'img/watch', 'output' + score.playerIndex + '.png');
invertROI.save(score.file);
self.parseImage(score);
index++;
});
if (self.window) {
self.window.show(im);
}
}
if (self.window) {
self.window.blockingWaitKey(0, self.CVoptions.interval);
}
}
});
}, self.CVoptions.interval);
}
self.parseImage = function(score) {
//openCV seems to be adding a strange border around the image. luckilly SSOCR has a crop param:
var cmd = "ssocr -d -1 crop 2 2 " + (score.rect.width - 3) + " " + (score.rect.height - 3) + " " + score.file;
proc = exec(cmd, function(error, stdout, stderr) {
if (stderr) {
self.showError(stderr);
} else {
if ((stdout.indexOf('_') == -1) && (stdout.indexOf('-') == -1)) {
score.strValue = stdout;
var val = parseInt(stdout);
score.smooth.shift();
score.smooth.push(val);
var mode = numbers.statistic.mode(score.smooth);
if (mode != score.intValue) {
score.intValue = mode;
self.updateScores(score.playerIndex);
}
}
}
});
}
self.updateScores = function(currentPlayer) {
var smScores = [];
_.each(self.status.scores, function(score) {
var sc;
if (score.isWide) {
sc = {
name: 'player ' + score.playerIndex,
value: score.intValue
};
} else {
sc = {
name: 'ball',
value: score.intValue
};
}
if (score.playerIndex == currentPlayer) {
sc.isActive = true;
} else {
sc.isActive = false;
}
smScores.push(sc);
});
self.emit('scored', smScores);
}
self.changeStatus = function(isReady) {
if (isReady != undefined) {
self.status.isReady = isReady;
}
self.emit('ready', self.status.isReady);
if (self.status.isReady == true) {
console.log('📸 👍');
} else {
console.log('📸 👎');
}
}
self.showError = function(error) {
console.error('📸 😢 : ' + error);
self.changeStatus(false);
}
}
util.inherits(Watcher, EventEmitter);
module.exports = Watcher;