-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.js
154 lines (130 loc) · 5.1 KB
/
camera.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/** TEACHABLE MACHINE **/
// More API functions here:
// https://github.com/googlecreativelab/teachablemachine-community/tree/master/libraries/image
// the link to your model provided by Teachable Machine export panel
const URL = "https://teachablemachine.withgoogle.com/models/hKgnHgNcc/";
// https://teachablemachine.withgoogle.com/models/bFFK0prYq/
let model, webcam, labelContainer, maxPredictions, lastPrediction;
// Load the image model and setup the webcam
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(200, 200, flip); // width, height, flip
await webcam.setup(); // 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");
for (let i = 0; i < maxPredictions; i++) { // and class labels
labelContainer.appendChild(document.createElement("div"));
}
submitBtn.style.display = "block";
}
async function loop() {
webcam.update(); // update the webcam frame
await predict();
window.requestAnimationFrame(loop);
}
async function stop() {
await webcam.stop();
document.getElementById("webcam-container").removeChild(webcam.canvas);
labelContainer = document.getElementById("label-container");
labelContainer.removeChild(labelContainer.children[0]);
//Hiding containers
document.getElementById("webcam-container").className = "d-none";
document.getElementById("label-container").className = "d-none";
}
// run the webcam image through the image model
async function predict() {
if (!startCamFlag) {
let highestProbability;
let lastProbability = 0;
// predict can take in an image, video or canvas html element
// predict can take in an image, video or canvas html element
const prediction = await model.predict(webcam.canvas);
for (let i = 0; i < maxPredictions; i++) {
if (prediction[i].probability.toFixed(2) > lastProbability)
highestProbability = i;
lastProbability = prediction[i].probability.toFixed(2);
if (prediction[highestProbability] != null) {
const predictionName = prediction[highestProbability].className;
labelContainer.childNodes[0].innerHTML = "Current response: "+predictionName;
lastPrediction = predictionName;
}
}
} else {
labelContainer.childNodes[0].innerHTML = "Your response was: "+lastPrediction;
switch(lastPrediction) {
case "nothing":
invalidResponse.style.display = "block";
break;
case "engage":
validResponse.style.display = "block";
break;
case "avoid":
validResponse.style.display = "block";
break;
}
if (document.getElementById("no-more-avoids").style.display == "block") {
document.getElementById("no-more-avoids").style.display = "none"
}
}
}
/** HANDLING EVENTS **/
let startCamFlag = true;
let validResponse = document.getElementById("valid-response");
let invalidResponse = document.getElementById("invalid-response");
let submitBtn = document.getElementById("submit-btn");
function startCamHandler() {
invalidResponse.style.display = "none";
if (startCamFlag) {
init();
} else {
stop();
submitBtn.style.display = "none";
}
startCamFlag = !startCamFlag;
}
function submitHandler() {
stop();
submitBtn.style.display = "none";
document.getElementById("cam-checkbox").checked = false;
startCamFlag = !startCamFlag;
}
function handleValidYes() {
if (lastPrediction == "avoid" && currStars.length == 0) {
handleNoMoreAvoids()
} else {
if (i >= opponents.size - 1) {
nextOpponent(lastPrediction)
} else {
nextOpponent(lastPrediction)
document.getElementById("cam-checkbox").checked = true;
init()
startCamFlag = !startCamFlag;
validResponse.style.display = "none";
}
}
}
function handleValidNo() {
document.getElementById("cam-checkbox").checked = true;
init()
startCamFlag = !startCamFlag;
validResponse.style.display = "none";
}
function handleNoMoreAvoids() {
document.getElementById("no-more-avoids").style.display = "block";
document.getElementById("cam-checkbox").checked = true;
init()
startCamFlag = !startCamFlag;
validResponse.style.display = "none";
}