-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
105 lines (83 loc) · 3.31 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
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
var AClicked = false
var BClicked = false
var CClicked = false
document.getElementById('class-a').addEventListener('click', () => { AClicked = true}); //addExample(0, bboxImg, net));
document.getElementById('class-b').addEventListener('click', () => { BClicked = true}); //addExample(1, bboxImg, net));
document.getElementById('class-c').addEventListener('click', () => { CClicked = true}); //addExample(2, bboxImg, net));
async function addExampleHandler(bboxImg, net){
if (AClicked == true){
await addExample(0, bboxImg, net)
AClicked = false
} else if (BClicked === true){
await addExample(1, bboxImg, net)
BClicked = false
} else if (CClicked === true){
await addExample(2, bboxImg, net)
CClicked = false
}
}
async function setupCamera(videoElement) {
const constraints = {video: {width: 320,height: 240}, audio: false};
const stream = await navigator.mediaDevices.getUserMedia(constraints);
videoElement.srcObject = stream;
return new Promise(resolve => {
videoElement.onloadedmetadata = () => {
videoElement.play();
resolve();
};
});
}
var loaded = false
function startRender(input, output, output2, faceMeshModel, net) {
const ctx = output.getContext("2d");
const ctx2 = output2.getContext("2d");
// loop
async function renderFrame() {
//await sleep(16) // It does the trick but blinky!
requestAnimationFrame(renderFrame);
const faces = await faceMeshModel.estimateFaces(input, false, false);
ctx.clearRect(0, 0, output.width, output.height);
ctx2.clearRect(0, 0, output2.width, output2.height);
ctx2.beginPath();
ctx2.drawImage(input, 0, 0, output2.width, output2.height)
faces.forEach(async face => {
face.scaledMesh.forEach(xy => {
ctx.beginPath();
ctx.arc(xy[0], xy[1], 1, 0, 2 * Math.PI);
ctx.fill();
});
if (!loaded){
console.log(face)
loaded = true
}
var bboxImg = await bbox(face, ctx2, net);
// console.log(tf.browser.fromPixels(bboxImg))
// put on top left of canvas2:
// ctx2.putImageData(bboxImg, -2, -2);
await addExampleHandler(bboxImg, net)
});
}
renderFrame();
}
function loading(onoff, id) {
document.getElementById(id).style.display = onoff ? "inline" : "none";
}
function hideInit(){
document.getElementById('init').style.display = 'none'
document.getElementById('reg').style.display = 'block'
}
async function start() {
document.getElementById('init-desc').textContent = 'Fetching resources from internet. Takes 0.5-1 min based on your internet connection...';
const input = document.getElementById("input");
const output = document.getElementById("output");
const output2 = document.getElementById("output2");
loading(true, "loadingicon");
loading(true, "loadingicon2");
await setupCamera(input);
const faceMeshModel = await facemesh.load({ maxFaces: 1 });
const net = await mobilenet.load();
startRender(input, output, output2, faceMeshModel, net);
loading(false, "loadingicon");
loading(false, "loadingicon2");
hideInit()
}