File size: 15,075 Bytes
47beacf
 
 
 
 
 
 
 
 
 
 
eb935e1
47beacf
 
eb935e1
 
 
 
 
 
 
47beacf
 
 
 
 
 
 
 
 
 
8e1bfbd
 
 
 
47beacf
 
f1aaf2f
47beacf
 
 
 
f1aaf2f
 
47beacf
f1aaf2f
47beacf
 
 
 
 
 
f1aaf2f
47beacf
 
 
 
 
f1aaf2f
 
47beacf
8e1bfbd
f1aaf2f
8e1bfbd
 
f1aaf2f
8e1bfbd
 
 
 
47beacf
f1aaf2f
47beacf
f1aaf2f
 
47beacf
 
 
 
 
 
f1aaf2f
 
47beacf
 
 
 
 
 
 
 
 
 
 
 
f1aaf2f
 
 
47beacf
f1aaf2f
47beacf
 
 
 
 
 
 
 
 
 
f1aaf2f
47beacf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1aaf2f
 
47beacf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e1bfbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb935e1
 
8e1bfbd
 
 
 
 
 
 
 
47beacf
 
 
 
 
 
 
 
 
 
 
 
 
 
eb935e1
 
47beacf
 
 
 
 
 
 
 
 
 
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI Vision Classifier</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        .prediction-bar {
            height: 24px;
            background: linear-gradient(90deg, #5b21b6 0%, #7e22ce 100%);
            border-radius: 12px;
            transition: width 0.3s ease;
            box-shadow: 0 0 10px rgba(124, 58, 237, 0.5);
        }
        .prediction-item {
            background-color: rgba(76, 29, 149, 0.2);
            padding: 12px;
            border-radius: 8px;
            border: 1px solid rgba(124, 58, 237, 0.3);
        }
        .webcam-feed {
            border-radius: 16px;
            box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
        }
        .webcam-feed:hover {
            transform: scale(1.02);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .webcam-feed img {
            max-width: 100%;
            max-height: 100%;
        }
    </style>
</head>
<body class="bg-gradient-to-br from-gray-900 to-purple-900 min-h-screen text-gray-100">
    <div class="container mx-auto px-4 py-12">
        <div class="max-w-4xl mx-auto">
            <!-- Header -->
            <div class="text-center mb-12">
                <h1 class="text-4xl font-bold text-purple-300 mb-2">
                    <i class="fas fa-robot text-purple-400 mr-2"></i> AI Vision Classifier
                </h1>
                <p class="text-gray-300 max-w-lg mx-auto">
                    A real-time image classification system powered by Teachable Machine.
                    Point your camera at objects to see the AI's predictions.
                </p>
            </div>

            <!-- Main Content -->
            <div class="bg-gray-800 rounded-xl shadow-lg overflow-hidden border border-purple-800">
                <div class="p-6 md:p-8">
                    <!-- Webcam Container -->
                    <div class="flex flex-col md:flex-row gap-8">
                        <div class="flex-1">
                            <div class="mb-4 flex justify-between items-center">
                                <h2 class="text-xl font-semibold text-purple-300">
                                    <i class="fas fa-camera text-purple-400 mr-2"></i> Live Feed
                                </h2>
                                <div class="flex gap-2">
                                    <button id="startBtn" onclick="init()" class="bg-purple-700 hover:bg-purple-600 text-white px-4 py-2 rounded-lg transition flex items-center">
                                        <i class="fas fa-play mr-2"></i> Start Camera
                                    </button>
                                    <label for="fileUpload" class="bg-purple-800 hover:bg-purple-700 text-white px-4 py-2 rounded-lg transition flex items-center cursor-pointer">
                                        <i class="fas fa-upload mr-2"></i> Upload Image
                                        <input id="fileUpload" type="file" accept="image/*" class="hidden" onchange="handleImageUpload(this)">
                                    </label>
                                </div>
                            </div>
                            <div id="webcam-container" class="webcam-feed bg-gray-700 w-full aspect-square flex items-center justify-center rounded-lg overflow-hidden border border-purple-800">
                                <div class="text-center p-4">
                                    <i class="fas fa-camera text-purple-400 text-4xl mb-2"></i>
                                    <p class="text-gray-300">Camera feed will appear here</p>
                                </div>
                            </div>
                        </div>

                        <!-- Predictions Container -->
                        <div class="flex-1">
                            <h2 class="text-xl font-semibold text-purple-300 mb-4">
                                <i class="fas fa-chart-bar text-purple-400 mr-2"></i> Predictions
                            </h2>
                            <div id="label-container" class="space-y-4">
                                <div class="bg-gray-100 p-6 rounded-lg text-center">
                                    <i class="fas fa-lightbulb text-yellow-400 text-3xl mb-3"></i>
                                    <p class="text-gray-600">Click "Start Camera" to begin classification</p>
                                    <p class="text-sm text-gray-500 mt-2">The AI will analyze objects in view and display confidence levels here</p>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Instructions -->
                    <div class="mt-8 bg-purple-900/30 p-4 rounded-lg border border-purple-800">
                        <h3 class="font-medium text-purple-300 mb-2 flex items-center">
                            <i class="fas fa-info-circle text-purple-400 mr-2"></i> How to use
                        </h3>
                        <ol class="list-decimal list-inside text-purple-200 space-y-1 text-sm">
                            <li>Click "Start Camera" and allow access to your webcam</li>
                            <li>Point your camera at objects you've trained the model to recognize</li>
                            <li>View real-time predictions with confidence percentages</li>
                            <li>For best results, ensure good lighting and clear focus</li>
                        </ol>
                    </div>
                </div>
            </div>

            <!-- Footer -->
            <div class="mt-8 text-center text-gray-400 text-sm">
                <p>Powered by Teachable Machine and TensorFlow.js</p>
            </div>
        </div>
    </div>

    <!-- Scripts -->
    <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">
        // the link to your model provided by Teachable Machine export panel
        const URL = "https://teachablemachine.withgoogle.com/models/5bQ38_H5n/";
        
        let model, webcam, labelContainer, maxPredictions;
        let isRunning = false;

        // Load the image model and setup the webcam
        async function init() {
            if (isRunning) return;
            
            const startBtn = document.getElementById('startBtn');
            startBtn.disabled = true;
            startBtn.innerHTML = '<i class="fas fa-spinner fa-spin mr-2"></i> Initializing...';
            
            try {
                const modelURL = URL + "model.json";
                const metadataURL = URL + "metadata.json";

                // load the model and metadata
                model = await tmImage.load(modelURL, metadataURL);
                maxPredictions = model.getTotalClasses();

                // Setup webcam
                const flip = true; // whether to flip the webcam
                webcam = new tmImage.Webcam(400, 400, flip); // increased resolution
                await webcam.setup(); // request access to the webcam
                await webcam.play();
                
                // Update UI
                const webcamContainer = document.getElementById('webcam-container');
                webcamContainer.innerHTML = '';
                webcamContainer.appendChild(webcam.canvas);
                webcam.canvas.classList.add('webcam-feed', 'w-full', 'h-full');
                
                // Setup predictions container
                labelContainer = document.getElementById('label-container');
                labelContainer.innerHTML = '';
                for (let i = 0; i < maxPredictions; i++) {
                    const predictionElement = document.createElement('div');
                    predictionElement.className = 'prediction-item';
                    labelContainer.appendChild(predictionElement);
                }
                
                startBtn.innerHTML = '<i class="fas fa-check-circle mr-2"></i> Running';
                startBtn.classList.remove('bg-purple-700', 'hover:bg-purple-600');
                startBtn.classList.add('bg-purple-600', 'hover:bg-purple-500');
                
                isRunning = true;
                window.requestAnimationFrame(loop);
            } catch (error) {
                console.error('Error initializing:', error);
                labelContainer.innerHTML = `
                    <div class="bg-red-50 p-4 rounded-lg text-red-700">
                        <i class="fas fa-exclamation-triangle mr-2"></i>
                        Error: ${error.message}
                    </div>
                `;
                startBtn.disabled = false;
                startBtn.innerHTML = '<i class="fas fa-play mr-2"></i> Try Again';
            }
        }

        async function loop() {
            if (!isRunning) return;
            
            webcam.update(); // update the webcam frame
            await predict();
            window.requestAnimationFrame(loop);
        }

        // run the webcam image through the image model
        async function handleImageUpload(input) {
            if (input.files && input.files[0]) {
                const reader = new FileReader();
                
                reader.onload = async function(e) {
                    const webcamContainer = document.getElementById('webcam-container');
                    webcamContainer.innerHTML = '';
                    
                    const img = document.createElement('img');
                    img.src = e.target.result;
                    img.className = 'webcam-feed w-full h-full object-contain';
                    webcamContainer.appendChild(img);
                    
                    // Stop webcam if running
                    if (isRunning) {
                        webcam.stop();
                        isRunning = false;
                        const startBtn = document.getElementById('startBtn');
                        startBtn.innerHTML = '<i class="fas fa-play mr-2"></i> Start Camera';
                        startBtn.classList.remove('bg-green-500', 'hover:bg-green-600');
                        startBtn.classList.add('bg-blue-500', 'hover:bg-blue-600');
                        startBtn.disabled = false;
                    }
                    
                    // Load model if not already loaded
                    if (!model) {
                        try {
                            const modelURL = URL + "model.json";
                            const metadataURL = URL + "metadata.json";
                            model = await tmImage.load(modelURL, metadataURL);
                            maxPredictions = model.getTotalClasses();
                        } catch (error) {
                            console.error('Error loading model:', error);
                            return;
                        }
                    }
                    
                    // Predict on the uploaded image
                    await predictOnImage(img);
                }
                
                reader.readAsDataURL(input.files[0]);
            }
        }

        async function predictOnImage(imageElement) {
            // Clear previous predictions
            labelContainer = document.getElementById('label-container');
            labelContainer.innerHTML = '';
            
            // Create prediction elements
            for (let i = 0; i < maxPredictions; i++) {
                const predictionElement = document.createElement('div');
                predictionElement.className = 'prediction-item';
                labelContainer.appendChild(predictionElement);
            }
            
            // Predict
            const prediction = await model.predict(imageElement);
            
            for (let i = 0; i < maxPredictions; i++) {
                const probability = prediction[i].probability.toFixed(2);
                const percentage = Math.round(probability * 100);
                
                const predictionElement = labelContainer.childNodes[i];
                predictionElement.className = 'prediction-item mb-4';
                predictionElement.innerHTML = `
                    <div class="flex justify-between items-center mb-1">
                        <span class="font-medium text-purple-300">${prediction[i].className}</span>
                        <span class="text-sm font-semibold text-purple-200">
                            ${percentage}%
                        </span>
                    </div>
                    <div class="prediction-bar" style="width: ${percentage}%"></div>
                `;
            }
        }

        async function predict() {
            if (!isRunning) return;
            
            // 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 probability = prediction[i].probability.toFixed(2);
                const percentage = Math.round(probability * 100);
                
                const predictionElement = labelContainer.childNodes[i];
                predictionElement.className = 'prediction-item mb-4';
                predictionElement.innerHTML = `
                    <div class="flex justify-between items-center mb-1">
                        <span class="font-medium text-purple-300">${prediction[i].className}</span>
                        <span class="text-sm font-semibold text-purple-200">
                            ${percentage}%
                        </span>
                    </div>
                    <div class="prediction-bar" style="width: ${percentage}%"></div>
                `;
            }
        }
    </script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=deniztas/model" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html>