YOUSEF2434 commited on
Commit
2666ddd
·
verified ·
1 Parent(s): 84624f1

Upload make-model.html

Browse files
Files changed (1) hide show
  1. make-model.html +255 -0
make-model.html ADDED
@@ -0,0 +1,255 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <br><br>
2
+
3
+
4
+ <!-- Save as: teachable-machine-widget.html -->
5
+ <div class="teachable-machine-widget w-full max-w-2xl mx-auto my-8 font-sans bg-zinc-950 text-zinc-100 rounded-2xl overflow-hidden border border-zinc-800 shadow-2xl">
6
+
7
+ <!-- Dependencies (Load these in your main page <head> if possible) -->
8
+ <script src="https://cdn.tailwindcss.com"></script>
9
+ <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.22.0/dist/tf.min.js"></script>
10
+ <script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/mobilenet@2.1.1"></script>
11
+ <script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/knn-classifier@1.2.6"></script>
12
+ <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
13
+
14
+ <style>
15
+
16
+ body {
17
+ background-color: black;
18
+ }
19
+ /* Scoped styles for the widget */
20
+ .teachable-machine-widget { font-family: 'Inter', sans-serif; }
21
+ .teachable-machine-widget button { touch-action: manipulation; user-select: none; -webkit-user-select: none; }
22
+ .tm-pulse { animation: tm-pulse-ring 1.5s cubic-bezier(0.24, 0, 0.38, 1) infinite; }
23
+ @keyframes tm-pulse-ring {
24
+ 0% { box-shadow: 0 0 0 0 rgba(255, 255, 255, 0.2); }
25
+ 70% { box-shadow: 0 0 0 6px rgba(255, 255, 255, 0); }
26
+ 100% { box-shadow: 0 0 0 0 rgba(255, 255, 255, 0); }
27
+ }
28
+ </style>
29
+
30
+ <!-- Widget Header -->
31
+ <div class="px-5 py-4 bg-zinc-900 border-b border-zinc-800 flex items-center justify-between">
32
+ <div class="flex items-center gap-3">
33
+ <div class="w-3 h-3 rounded-full bg-indigo-500 shadow-[0_0_10px_rgba(99,102,241,0.5)]"></div>
34
+ <h2 class="text-sm font-semibold tracking-wide text-zinc-200 uppercase">Teachable Machine</h2>
35
+ </div>
36
+ <div id="tm-status-badge" class="flex items-center gap-2 px-2 py-1 bg-zinc-950 rounded text-[10px] font-medium text-zinc-500 border border-zinc-800">
37
+ <span class="w-1.5 h-1.5 rounded-full bg-zinc-600"></span>
38
+ <span id="tm-status-text">Loading...</span>
39
+ </div>
40
+ </div>
41
+
42
+ <!-- Main Content Area -->
43
+ <div class="p-5 bg-zinc-950/50">
44
+
45
+ <!-- Video & Prediction Overlay Wrapper -->
46
+ <div class="relative rounded-xl overflow-hidden bg-black border border-zinc-800 aspect-video shadow-inner">
47
+ <video id="tm-webcam" class="w-full h-full object-cover transform scale-x-[-1]" autoplay playsinline muted></video>
48
+
49
+ <!-- Prediction Overlay (Bottom of video) -->
50
+ <div class="absolute bottom-0 left-0 right-0 bg-gradient-to-t from-black/90 to-transparent pt-12 pb-3 px-4 flex items-end justify-between">
51
+ <div>
52
+ <div class="text-[10px] text-zinc-400 uppercase tracking-wider mb-0.5">I see</div>
53
+ <div id="tm-result" class="text-xl font-bold text-white leading-none">...</div>
54
+ </div>
55
+ <div class="text-right">
56
+ <div class="text-[10px] text-zinc-400 uppercase tracking-wider mb-1">Confidence</div>
57
+ <div class="w-24 h-1.5 bg-zinc-800 rounded-full overflow-hidden">
58
+ <div id="tm-conf-bar" class="h-full bg-indigo-500 w-0 transition-all duration-300"></div>
59
+ </div>
60
+ </div>
61
+ </div>
62
+ </div>
63
+
64
+ <!-- Controls Compact Grid -->
65
+ <div class="mt-4 grid grid-cols-3 gap-3">
66
+
67
+ <!-- Button A -->
68
+ <button id="tm-btn-a" class="group relative flex flex-col items-center justify-center p-3 rounded-xl bg-zinc-900 border border-zinc-800 hover:border-red-500/50 hover:bg-zinc-800 transition-all active:scale-[0.98]">
69
+ <div class="absolute inset-0 rounded-xl border-2 border-transparent group-hover:border-red-500/20 pointer-events-none"></div>
70
+ <div class="mb-1 text-xs font-medium text-red-400">Class A</div>
71
+ <div class="text-[10px] text-zinc-500">Red Object</div>
72
+ <div class="mt-2 px-2 py-0.5 bg-zinc-950 rounded text-[10px] text-zinc-300 font-mono border border-zinc-800">
73
+ <span id="tm-count-a">0</span> samples
74
+ </div>
75
+ </button>
76
+
77
+ <!-- Button B -->
78
+ <button id="tm-btn-b" class="group relative flex flex-col items-center justify-center p-3 rounded-xl bg-zinc-900 border border-zinc-800 hover:border-blue-500/50 hover:bg-zinc-800 transition-all active:scale-[0.98]">
79
+ <div class="absolute inset-0 rounded-xl border-2 border-transparent group-hover:border-blue-500/20 pointer-events-none"></div>
80
+ <div class="mb-1 text-xs font-medium text-blue-400">Class B</div>
81
+ <div class="text-[10px] text-zinc-500">Blue Object</div>
82
+ <div class="mt-2 px-2 py-0.5 bg-zinc-950 rounded text-[10px] text-zinc-300 font-mono border border-zinc-800">
83
+ <span id="tm-count-b">0</span> samples
84
+ </div>
85
+ </button>
86
+
87
+ <!-- Button Idle -->
88
+ <button id="tm-btn-i" class="group relative flex flex-col items-center justify-center p-3 rounded-xl bg-zinc-900 border border-zinc-800 hover:border-zinc-500/50 hover:bg-zinc-800 transition-all active:scale-[0.98]">
89
+ <div class="absolute inset-0 rounded-xl border-2 border-transparent group-hover:border-zinc-500/20 pointer-events-none"></div>
90
+ <div class="mb-1 text-xs font-medium text-zinc-400">Background</div>
91
+ <div class="text-[10px] text-zinc-500">Idle</div>
92
+ <div class="mt-2 px-2 py-0.5 bg-zinc-950 rounded text-[10px] text-zinc-300 font-mono border border-zinc-800">
93
+ <span id="tm-count-i">0</span> samples
94
+ </div>
95
+ </button>
96
+
97
+ </div>
98
+
99
+ <!-- Footer / Reset -->
100
+ <div class="mt-4 flex items-center justify-between text-[10px] text-zinc-500">
101
+ <p>Hold buttons to train</p>
102
+ <button id="tm-reset-btn" class="hover:text-red-400 transition-colors underline decoration-zinc-800 hover:decoration-red-900">
103
+ Reset Data
104
+ </button>
105
+ </div>
106
+ </div>
107
+
108
+ <!-- Logic -->
109
+ <script>
110
+ (function() {
111
+ // Namespace variables to avoid conflicts if you have multiple scripts on the page
112
+ const TM_LABELS = ["Class A", "Class B", "Background"];
113
+ const TM_TOPK = 10;
114
+ let tmNet = null;
115
+ let tmKnn = null;
116
+ let tmIsTraining = false;
117
+ let tmTrainingClass = -1;
118
+ let tmPredicting = false;
119
+
120
+ // Elements
121
+ const get = (id) => document.getElementById(id);
122
+ const els = {
123
+ webcam: get('tm-webcam'),
124
+ statusText: get('tm-status-text'),
125
+ statusBadge: get('tm-status-badge'),
126
+ result: get('tm-result'),
127
+ confBar: get('tm-conf-bar'),
128
+ counts: [get('tm-count-a'), get('tm-count-b'), get('tm-count-i')],
129
+ btns: [get('tm-btn-a'), get('tm-btn-b'), get('tm-btn-i')],
130
+ reset: get('tm-reset-btn')
131
+ };
132
+
133
+ function setStatus(msg, type="neutral") {
134
+ els.statusText.textContent = msg;
135
+ if(type === 'ready') els.statusBadge.querySelector('span').className = "w-1.5 h-1.5 rounded-full bg-emerald-500 shadow-[0_0_8px_rgba(16,185,129,0.6)]";
136
+ else if(type === 'loading') els.statusBadge.querySelector('span').className = "w-1.5 h-1.5 rounded-full bg-amber-500 animate-pulse";
137
+ else els.statusBadge.querySelector('span').className = "w-1.5 h-1.5 rounded-full bg-zinc-600";
138
+ }
139
+
140
+ function updateUI(label, conf) {
141
+ if (!label || label === "—") {
142
+ els.result.textContent = "...";
143
+ els.result.className = "text-xl font-bold text-zinc-500 leading-none";
144
+ els.confBar.style.width = "0%";
145
+ return;
146
+ }
147
+
148
+ els.result.textContent = label;
149
+ let color = "text-white";
150
+ let barColor = "bg-indigo-500";
151
+
152
+ if(label === TM_LABELS[0]) { color = "text-red-400"; barColor = "bg-red-500"; }
153
+ if(label === TM_LABELS[1]) { color = "text-blue-400"; barColor = "bg-blue-500"; }
154
+ if(label === TM_LABELS[2]) { color = "text-zinc-400"; barColor = "bg-zinc-500"; }
155
+
156
+ els.result.className = `text-xl font-bold ${color} leading-none transition-colors duration-200`;
157
+ els.confBar.className = `h-full ${barColor} transition-all duration-200`;
158
+ els.confBar.style.width = `${conf}%`;
159
+ }
160
+
161
+ function updateCounts() {
162
+ const counts = tmKnn ? tmKnn.getClassExampleCount() : {};
163
+ els.counts.forEach((el, idx) => el.textContent = counts[idx] || 0);
164
+ }
165
+
166
+ async function init() {
167
+ try {
168
+ setStatus("Camera...", "loading");
169
+
170
+ // 1. Setup Webcam
171
+ const stream = await navigator.mediaDevices.getUserMedia({ video: true, audio: false });
172
+ els.webcam.srcObject = stream;
173
+ await new Promise(r => els.webcam.onloadedmetadata = () => r());
174
+
175
+ // 2. Load Models
176
+ setStatus("Models...", "loading");
177
+ tmKnn = knnClassifier.create();
178
+ tmNet = await mobilenet.load({ version: 2, alpha: 0.50 });
179
+
180
+ // 3. Setup Events
181
+ els.btns.forEach((btn, idx) => {
182
+ const start = (e) => {
183
+ if (e.cancelable) e.preventDefault();
184
+ tmIsTraining = true;
185
+ tmTrainingClass = idx;
186
+ btn.classList.add("tm-pulse", "border-white");
187
+ };
188
+ const stop = (e) => {
189
+ if (e.cancelable) e.preventDefault();
190
+ tmIsTraining = false;
191
+ tmTrainingClass = -1;
192
+ btn.classList.remove("tm-pulse", "border-white");
193
+ };
194
+ btn.addEventListener("mousedown", start);
195
+ btn.addEventListener("mouseup", stop);
196
+ btn.addEventListener("mouseleave", stop);
197
+ btn.addEventListener("touchstart", start, {passive:false});
198
+ btn.addEventListener("touchend", stop, {passive:false});
199
+ });
200
+
201
+ els.reset.addEventListener('click', () => {
202
+ if(tmKnn) tmKnn.clearAllClasses();
203
+ updateCounts();
204
+ updateUI(null, 0);
205
+ });
206
+
207
+ setStatus("Ready", "ready");
208
+
209
+ // 4. Loops
210
+ trainLoop();
211
+ predictLoop();
212
+ tmPredicting = true;
213
+
214
+ } catch (e) {
215
+ console.error(e);
216
+ setStatus("Error", "error");
217
+ }
218
+ }
219
+
220
+ async function trainLoop() {
221
+ while (true) {
222
+ if (tmIsTraining && tmTrainingClass !== -1) {
223
+ const img = tf.browser.fromPixels(els.webcam);
224
+ const activation = tmNet.infer(img, true);
225
+ tmKnn.addExample(activation, tmTrainingClass);
226
+ img.dispose();
227
+ activation.dispose();
228
+ updateCounts();
229
+ }
230
+ await new Promise(r => setTimeout(r, 70)); // Throttle training
231
+ }
232
+ }
233
+
234
+ async function predictLoop() {
235
+ while (true) {
236
+ if (tmPredicting && tmKnn.getNumClasses() > 0) {
237
+ const img = tf.browser.fromPixels(els.webcam);
238
+ const activation = tmNet.infer(img, true);
239
+ try {
240
+ const res = await tmKnn.predictClass(activation, TM_TOPK);
241
+ const label = TM_LABELS[res.classIndex];
242
+ const conf = res.confidences[res.classIndex] * 100;
243
+ updateUI(label, conf);
244
+ } catch(e) {}
245
+ img.dispose();
246
+ activation.dispose();
247
+ }
248
+ await tf.nextFrame();
249
+ }
250
+ }
251
+
252
+ init();
253
+ })();
254
+ </script>
255
+ </div>