-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathteachableMachine.html
More file actions
180 lines (166 loc) · 5.71 KB
/
Copy pathteachableMachine.html
File metadata and controls
180 lines (166 loc) · 5.71 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>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Teachable Machine Image Model</title>
<style>
/* Modern, responsive styling */
body, html {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
background-color: #f4f7f6;
font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
width: 100%;
max-width: 500px;
padding: 20px;
box-sizing: border-box;
height: 100%;
}
h1 {
font-size: 1.4rem;
color: #333;
margin-bottom: 20px;
text-align: center;
}
button {
background-color: #4A90E2;
color: white;
border: none;
border-radius: 12px;
padding: 16px 32px;
font-size: 1.1rem;
font-weight: bold;
cursor: pointer;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
transition: background-color 0.2s, transform 0.1s;
width: 100%;
max-width: 300px;
}
button:active {
background-color: #357ABD;
transform: scale(0.98);
}
#webcam-container {
margin-top: 15px;
width: 100%;
display: flex;
justify-content: center;
}
/* Makes the webcam responsive */
#webcam-container canvas {
width: 100%;
max-width: 400px;
height: auto;
border-radius: 16px;
box-shadow: 0 6px 12px rgba(0,0,0,0.15);
background-color: #e0e0e0; /* Placeholder color before load */
}
#label-container {
margin-top: 25px;
width: 100%;
max-width: 400px;
display: flex;
flex-direction: column;
gap: 12px;
}
/* Clean UI cards for the predictions */
.label-row {
display: flex;
justify-content: space-between;
background: #ffffff;
padding: 16px 20px;
border-radius: 12px;
box-shadow: 0 2px 8px rgba(0,0,0,0.06);
font-weight: 600;
color: #444;
font-size: 1.1rem;
}
.probability {
color: #4A90E2;
}
</style>
</head>
<body>
<div class="container">
<h1>Image Classifier</h1>
<button type="button" id="start-btn" onclick="init()">Start Camera</button>
<div id="webcam-container"></div>
<div id="label-container"></div>
</div>
<script src="https://thunkable.github.io/webviewer-extension/thunkableWebviewerExtension.js" type="text/javascript"></script>
<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">
let URL = "https://teachablemachine.withgoogle.com/models/9f4pUP5L2/";
let model, webcam, labelContainer, maxPredictions;
ThunkableWebviewerExtension.receiveMessage(function(message) {
if (message.type === 'op-window-direct-response' || message.type === 'op-window-direct-request') {
return;
}
try {
const msgFromApp = JSON.parse(message);
if (msgFromApp.type === 'setUrl') {
URL = msgFromApp.url;
}
} catch (e) {
console.error(e);
}
});
async function init() {
// Hide the button to free up screen space
document.getElementById("start-btn").style.display = "none";
const modelURL = URL + "model.json";
const metadataURL = URL + "metadata.json";
model = await tmImage.load(modelURL, metadataURL);
maxPredictions = model.getTotalClasses();
const flip = true;
// We initialize at 200x200, but CSS scales it up smoothly
webcam = new tmImage.Webcam(200, 200, flip);
await webcam.setup();
await webcam.play();
window.requestAnimationFrame(loop);
document.getElementById("webcam-container").appendChild(webcam.canvas);
labelContainer = document.getElementById("label-container");
// Setup the UI cards for labels
for (let i = 0; i < maxPredictions; i++) {
let div = document.createElement("div");
div.className = "label-row";
labelContainer.appendChild(div);
}
}
async function loop() {
webcam.update();
await predict();
window.requestAnimationFrame(loop);
}
async function predict() {
const prediction = await model.predict(webcam.canvas);
const results = {};
for (let i = 0; i < maxPredictions; i++) {
const className = prediction[i].className;
// Convert decimal to a clean percentage (e.g., 0.98 -> 98%)
const probability = (prediction[i].probability * 100).toFixed(0) + "%";
labelContainer.childNodes[i].innerHTML = `
<span>${className}</span>
<span class="probability">${probability}</span>
`;
results[className] = probability;
}
ThunkableWebviewerExtension.postMessage(
JSON.stringify({ type: "prediction", results: results })
);
}
</script>
</body>
</html>