-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
90 lines (66 loc) · 2.62 KB
/
script.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
console.log('ALL SET LETZ GO!');
// the link of model file
const URL = "https://teachablemachine.withgoogle.com/models/K4JuZnTWX/";
let model, webcam, labelContainer, maxPredictions;
// wake up audio is the place where i have stored my audio file
var aud = document.getElementById("VOICE");
function playAud() {
aud.play();
}
function pauseAud() {
aud.pause();
}
async function init() {
const modelURL = URL + "model.json";
const metadataURL = URL + "metadata.json";
// load the model and metadata
// Refer to tmImage.loadFromFiles() in the API to support files from a file picker
// or files from your local hard drive
// Note: the pose library adds "tmImage" object to your window (window.tmImage)
model = await tmImage.load(modelURL, metadataURL);
maxPredictions = model.getTotalClasses();
// Convenience function to setup a webcam
const flip = true; // whether to flip the webcam
webcam = new tmImage.Webcam(600, 500, flip); // width, height, flip
await webcam.setup({ facingMode: "enviroment" }); // request access to the webcam
await webcam.play();
window.requestAnimationFrame(loop);
// append elements to the DOM
document.getElementById("webcam-container").appendChild(webcam.canvas);
labelContainer = document.getElementById("label-container");
labelContainer.innerHTML=''
for (let i = 0; i < maxPredictions; i++) { // and class labels
labelContainer.appendChild(document.createElement("div"));
}
}
async function loop() {
webcam.update(); // update the webcam frame
await predict();
window.requestAnimationFrame(loop);
}
// run the webcam image through the image model
async function predict() {
// predict can take in an image, video or canvas html element
// console.log('Taking a break...');
// await sleep(4000);
// console.log('Two seconds later, showing sleep in a loop...');
const prediction = await model.predict(webcam.canvas);
for (let i = 0; i < maxPredictions; i++) {
const classPrediction =
prediction[i].className + ": " + (prediction[i].probability.toFixed(2))*100+'%';
labelContainer.childNodes[i].innerHTML = classPrediction;
// don't play audio when head's neutral with probability >= 75%
// console.log(prediction)
// console.log(prediction[0].probability.toFixed(2));
if (prediction[0].probability.toFixed(2) >= 0.90 || prediction[1].probability.toFixed(2) >= 0.90)
playAud();
else
pauseAud();
}
}
async function stop() {
location.reload();
// await webcam.stop();
// document.getElementById("webcam-container").removeChild(webcam.canvas);
// document.getElementById("label-container").innerHTML = "";
}