-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
50 lines (36 loc) · 1.16 KB
/
Copy pathscript.js
File metadata and controls
50 lines (36 loc) · 1.16 KB
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
const URL = "./model/";
let model, webcam;
const alarm = document.getElementById("alarm");
const statusText = document.getElementById("status");
// Unlock audio on first click
document.body.addEventListener("click", () => {
alarm.play().then(() => alarm.pause());
});
async function init() {
statusText.innerText = "Loading model...";
model = await tmImage.load(
URL + "model.json",
URL + "metadata.json"
);
webcam = new tmImage.Webcam(300, 300, true);
await webcam.setup();
await webcam.play();
document.getElementById("webcam").replaceWith(webcam.canvas);
statusText.innerText = "Model loaded. Stay focused 👀";
window.requestAnimationFrame(loop);
}
async function loop() {
webcam.update();
await predict();
window.requestAnimationFrame(loop);
}
async function predict() {
const predictions = await model.predict(webcam.canvas);
predictions.forEach(p => {
if (p.className === "Distracted" && p.probability > 0.9) { // at 90 percent distraction, alarm is triggered
statusText.innerText = "🚨 GET BACK TO WORK";
if (alarm.paused) alarm.play();
}
});
}
init();