-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
111 lines (80 loc) · 3.16 KB
/
app.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
const cv = require('opencv4nodejs'),
fr = require('face-recognition').withCv(cv),
fs = require('fs'),
path = require('path'),
cam = require('livecam'),
faceDetector = require('./face-detector'),
recognizer = fr.FaceRecognizer(),
config = require('./config');
(() => {
// let rawImages = fs.readdirSync(config.path.raw)
// .map(imageName => path.resolve(`${config.path.raw}/${imageName}`));
// faceDetector({images: rawImages, storePath: config.path.faces});
/*const dataPath = path.resolve('./data/faces');
const classNames = ['artem'];
const allFiles = fs.readdirSync(dataPath);
const imagesByClass = classNames.map(c =>
allFiles
.filter(f => f.includes(c))
.map(f => path.join(dataPath, f))
.map(fp => fr.loadImage(fp))
);
const numTrainingFaces = 10;
const trainDataByClass = imagesByClass.map(imgs => imgs.slice(0, numTrainingFaces));
const testDataByClass = imagesByClass.map(imgs => imgs.slice(numTrainingFaces));*/
// trainDataByClass.forEach((faces, label) => {
// const name = classNames[label];
// recognizer.addFaces(faces, name);
// });
// const modelState = recognizer.serialize();
// fs.writeFileSync('model.json', JSON.stringify(modelState));
/*const modelState = require('./model.json');
recognizer.load(modelState)*/
/**
* TODO: refactor this shit
*/
/*const errors = classNames.map(_ => [])
testDataByClass.forEach((faces, label) => {
const name = classNames[label]
console.log()
console.log('testing %s', name)
faces.forEach((face, i) => {
const prediction = recognizer.predictBest(face)
console.log('%s (%s)', prediction.className, prediction.distance)
// count number of wrong classifications
if (prediction.className !== name) {
errors[label] = errors[label] + 1
}
})
})
const result = classNames.map((className, label) => {
const numTestFaces = testDataByClass[label].length
const numCorrect = numTestFaces - errors[label].length
const accuracy = parseInt((numCorrect / numTestFaces) * 10000) / 100
return `${className} ( ${accuracy}% ) : ${numCorrect} of ${numTestFaces} faces have been recognized correctly`
})
console.log('result:')
console.log(result)*/
let camera = new cv.VideoCapture(0);
// camera.setWidth(300);
// camera.setHeight(300);
const server = require('./server');
// let im = camera.read();
server.start();
const io = server.io;
io.on('connection', (socket) => {
setInterval(() => {
let im = camera.read();
// let imageCv = fr.CvImage(im);
// let imageRgb = fr.cvImageToImageRGB(imageCv);
socket.emit('frame', im.getData());
}, 1000);
});
// let myDuplexStream = new DuplexStream(aReadableStream, aWritableStream);
/* let opts = {
callbackReturn: "base64"
};
NodeWebcam.capture( "test_picture", opts, function( err, data ) {
let image = "<img src='" + data + "'>";
});*/
})();