-
Notifications
You must be signed in to change notification settings - Fork 29
/
index.js
48 lines (41 loc) · 1.21 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
'use strict';
const log = require('silk-log')('main');
const Camera = require('silk-camera').default;
const path = require('path');
const FACE_CASCADE = path.join(__dirname, 'haarcascade_frontalface_alt.xml');
require('./device').init();
let camera = new Camera();
camera.init()
.then(() => {
log.info('camera initialized');
camera.startRecording();
}).catch(e => {
log.info('Error: ' + e);
});
let busy = false;
camera.on('frame', (when, image) => {
if (busy) {
// If the previous haar cascade is still running, skip this frame
return;
}
if (image.width() < 1 || image.height() < 1) {
throw new Error('Image has no size');
}
busy = true;
image.detectObject(FACE_CASCADE, {}, (err, faces) => {
busy = false;
if (err) throw err;
if (faces.length === 0) {
log.info('No faces detected');
} else {
log.info(faces.length + ' faces detected');
for (let i = 0; i < faces.length; i++) {
const face = faces[i];
image.ellipse(face.x + face.width / 2, face.y + face.height / 2, face.width / 2, face.height / 2);
}
const filename = '/data/face_' + Date.now() + '.png';
image.save(filename);
log.info('Saved ' + filename);
}
});
});