-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
180 lines (145 loc) · 6.54 KB
/
index.html
File metadata and controls
180 lines (145 loc) · 6.54 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<!DOCTYPE html>
<html lang="en">
<head>
<title>WaveSign Application</title>
<h1>WaveSign</h1>
</head>
<body>
<div>Teachable Machine Image Model</div>
<button id = "toggle-webcam" type="button" onclick="toggleWebcam()">Start/Stop</button>
<form id = "image-upload-form">
<label for="image-upload">Upload an image:</label>
<input type="file" id="image-upload" accept="image/*" onchange="handleImageUpload(event)">
<br><button id = "submit-button" type = "submit">Submit Image</button>
<br>
<img id = "uploaded-image" style = "display: none; max-width: 200px; margin-top: 10px;">
</form>
<a href = "aslData.html">
<button type = "button">View ASL Images</button>
</a>
<div id="webcam-container"></div>
<div id="label-container"></div>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest/dist/tf.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@teachablemachine/image@latest/dist/teachablemachine-image.min.js"></script>
<script type="text/javascript">
// 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 = "./My_Model/";
let model, webcam, labelContainer, maxPredictions;
// Load the image model and setup the webcam
async function loadModel() {
if (!model) {
const modelURL = URL + "model.json";
const metadataURL = URL + "metadata.json";
model = await tmImage.load(modelURL, metadataURL);
maxPredictions = model.getTotalClasses();
}
labelContainer = document.getElementById("label-container");
labelContainer.innerHTML = "";
for (let i = 0; i < maxPredictions; i++) {
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
const prediction = await model.predict(webcam.canvas);
for (let i = 0; i < maxPredictions; i++) {
const classPrediction =
prediction[i].className + ": " + prediction[i].probability.toFixed(2);
labelContainer.childNodes[i].innerHTML = classPrediction;
}
}
async function toggleWebcam() {
const button = document.getElementById("toggle-webcam");
if (!webcam) {
await startWebcam();
button.textContent = "Start/Stop Webcam";
} else {
stopWebcam();
button.textContent = "Start/Stop Webcam";
}
}
async function predictImage(imageElement) {
await loadModel();
const prediction = await model.predict(imageElement);
labelContainer.innerHTML = "";
for (let i = 0; i < maxPredictions; i++) {
const classPrediction = prediction[i].className + ": " + prediction[i].probability.toFixed(2);
const div = document.createElement("div");
div.innerHTML = classPrediction;
labelContainer.appendChild(div);
}
}
let uploadedImage = null;
function handleImageUpload(event) {
const file = event.target.files[0];
const imageElement = document.getElementById("uploaded-image");
if (!file) {
alert("Please upload an image.");
return;
}
const reader = new FileReader();
reader.onload = function(e) {
imageElement.src = e.target.result;
imageElement.style.display = "block";
// Stop and hide webcam if active
if (webcam) {
webcam.stop();
webcam.canvas.remove();
webcam = null;
}
imageElement.onload = async function() {
await loadModel();
await predictImage(imageElement);
};
};
reader.readAsDataURL(file);
}
document.getElementById("image-upload-form").addEventListener("submit", function(event) {
event.preventDefault();
const imageElement = document.getElementById("uploaded-image");
if (!imageElement || !imageElement.src || imageElement.style.display === "none") {
alert("Please upload an image.");
return;
}
predictImage(imageElement);
});
async function startWebcam() {
await loadModel();
const imageElement = document.getElementById("uploaded-image");
imageElement.style.display = "none";
if (webcam) return;
const flip = true;
webcam = new tmImage.Webcam(200, 200, flip);
await webcam.setup(); // request access to the webcam
await webcam.play();
document.getElementById("webcam-container").appendChild(webcam.canvas);
window.requestAnimationFrame(loop);
}
function stopWebcam() {
if (webcam) {
webcam.stop();
webcam.canvas.remove();
webcam = null;
}
}
</script>
<style>
#start-stop-button {
font-weight: bold;
font-size: 20px;
margin-bottom: 16px;
}
#stop-button, #webcam-button, #submit-button {
margin-bottom: 16px;
}
</style>
</body>
</html>