-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrender.js
83 lines (75 loc) · 2.21 KB
/
render.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
const fs = require('fs');
const path = require('path');
const cv = require('opencv');
var pathFile = '';
var streaming = false;
function startVideo(){
streaming = !streaming;
let sasBnt = document.getElementById('startAndStop');
let sBtn = document.getElementById('saveImage');
if (streaming){
sasBnt.textContent = 'Stop';
}else{
sasBnt.textContent = 'Start';
}
sBtn.disabled = !sBtn.disabled;
sasBnt.classList.toggle(stop);
sasBnt.classList.toggle(start);
findFace();
}
function findFace() {
let camera = new cv.VideoCapture(0);
if (camera){
let cnvs = document.getElementById('video-flow');
processVideo(camera, cnvs);
}else{
alert("camera not found");
}
};
function processVideo(camera, cnvs) {
const FPS = 30;
try {
if (!streaming) {
clearTimeout(id);
return;
}
let begin = Date.now();
camera.read((err, im)=>{
if (err) throw err;
if (im.size()[0] > 0 && im.size()[1] >1){
im.detectObject(cv.FACE_CASCADE, {}, (err, faces)=>{
if (err) throw err;
for (let i= 0 ; i < faces.length; i++){
let face = faces[i];
im.rectangle([face.x, face.y], [face.width, face.height], rectColor, rectThickness);
}
cv.imshow(cnvs, im);
});
}
});
let delay = 1000/FPS - (Date.now() - begin);
var id = setTimeout(processVideo, delay);
} catch (err) {
console.log(err);
}
};
function saveImage(){
let fileName = 'screen' + Date.now()+ '.jpeg';
let canvas = document.getElementById('video-flow');
var image = canvas.toDataURL("image/png").replace(/^data:image\/\w+;base64,/, "");;
var buf = new Buffer(image, 'base64');
fs.writeFile(path.join(pathFile, fileName), buf, (err)=>{
if (err) throw err;
});
}
function setupDirectory(){
let setupDir = document.getElementById('setupDir');
if (setupDir.files.length >= 1 ){
fs.writeFileSync('path.txt', path.normalize(setupDir.files[0].path));
readDirectory();
}
}
function readDirectory(){
var res = fs.readFileSync('path.txt', 'utf-8');
pathFile = res;
}