File size: 27,871 Bytes
fad65be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e09776b
 
 
 
 
 
 
 
 
 
 
 
fad65be
 
 
 
 
 
 
e09776b
 
 
fad65be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e09776b
 
fad65be
 
 
 
 
 
 
 
 
 
 
 
 
e09776b
 
 
 
 
 
 
 
 
fad65be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e09776b
 
fad65be
e09776b
fad65be
 
 
 
 
 
 
 
 
 
 
 
 
e09776b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fad65be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e09776b
 
 
 
 
 
 
 
 
 
 
 
 
fad65be
 
e09776b
 
 
 
 
fad65be
e09776b
 
fad65be
 
 
 
 
b789e90
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AssetSnap | School Equipment Scanner</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://unpkg.com/feather-icons"></script>
    <script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest"></script>
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/coco-ssd@latest"></script>
    <style>
        #cameraFeed {
            transform: scaleX(-1);
        }
        .object-tag {
            position: absolute;
            background: rgba(59, 130, 246, 0.7);
            color: white;
            padding: 2px 6px;
            border-radius: 4px;
            font-size: 12px;
            pointer-events: none;
        }
        .progress-ring__circle {
            transition: stroke-dashoffset 0.35s;
            transform: rotate(-90deg);
            transform-origin: 50% 50%;
        }
    </style>
</head>
<body class="bg-gray-100 min-h-screen">
    <div class="container mx-auto px-4 py-8">
        <!-- Scanner Mode Selection -->
        <div class="flex justify-center mb-8">
            <div class="inline-flex rounded-md shadow-sm">
                <button id="liveScannerBtn" class="px-4 py-2 text-sm font-medium rounded-l-lg bg-blue-600 text-white">
                    <i data-feather="video"></i> Live Scanner
                </button>
                <button id="uploadScannerBtn" class="px-4 py-2 text-sm font-medium rounded-r-lg bg-gray-100 text-gray-700 hover:bg-gray-200">
                    <i data-feather="upload"></i> Upload Scanner
                </button>
            </div>
        </div>
<!-- Header -->
        <header class="mb-12 text-center">
            <h1 class="text-4xl font-bold text-blue-600 mb-2">EduSight Scanner</h1>
            <p class="text-lg text-gray-600">Count school equipment from photos or live camera</p>
</header>

        <!-- Main Content -->
        <div class="flex flex-col lg:flex-row gap-8">
            <!-- Scanner Section -->
            <div id="scannerSection" class="lg:w-2/3 bg-white rounded-xl shadow-lg overflow-hidden">
<div class="p-4 bg-blue-600 text-white">
                    <h2 class="text-xl font-semibold flex items-center gap-2">
                        <i data-feather="camera"></i> Live Detection
                    </h2>
                </div>
                <div class="relative bg-black aspect-video flex items-center justify-center">
                    <video id="cameraFeed" autoplay playsinline class="w-full h-full object-cover"></video>
                    <div id="detectionOverlay" class="absolute inset-0"></div>
                    <div id="noCamera" class="text-white text-center p-4 hidden">
                        <i data-feather="video-off" class="w-12 h-12 mx-auto mb-4"></i>
                        <p>Camera access required for live scanning</p>
                        <button id="enableCameraBtn" class="mt-4 bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded-lg">
                            Enable Camera
                        </button>
                    </div>
                </div>
                <div class="p-4 flex flex-col gap-4">
                    <div class="flex justify-between items-center">
                        <button id="captureBtn" class="bg-blue-600 hover:bg-blue-700 text-white px-6 py-2 rounded-lg flex items-center gap-2">
                        <i data-feather="scan"></i> Scan Now
</button>
                        <button id="uploadBtn" class="bg-purple-600 hover:bg-purple-700 text-white px-6 py-2 rounded-lg flex items-center gap-2">
                            <i data-feather="upload"></i> Upload Photo
                        </button>
                        <input type="file" id="fileInput" accept="image/*" class="hidden">
                    </div>
                    <div class="flex items-center gap-2 text-gray-600">
                        <i data-feather="info"></i>
                        <span>Point camera or upload photo of school equipment</span>
                    </div>
                </div>
</div>
            <!-- Results Section -->
            <div class="lg:w-1/3 bg-white rounded-xl shadow-lg">
                <div class="p-4 bg-blue-600 text-white flex justify-between items-center">
                    <h2 class="text-xl font-semibold flex items-center gap-2">
                        <i data-feather="clipboard"></i> Scan Results
                    </h2>
                    <button id="clearResultsBtn" class="text-sm bg-white text-blue-600 px-2 py-1 rounded flex items-center gap-1">
                        <i data-feather="trash-2" class="w-4 h-4"></i> Clear
                    </button>
                </div>
<div class="p-4 bg-blue-600 text-white">
                    <h2 class="text-xl font-semibold flex items-center gap-2">
                        <i data-feather="clipboard"></i> Scan Results
                    </h2>
                </div>
                <div class="p-4 h-96 overflow-y-auto">
                    <div id="loadingSection" class="flex flex-col items-center justify-center h-full">
                        <div class="relative w-20 h-20 mb-4">
                            <svg class="progress-ring w-full h-full" viewBox="0 0 100 100">
                                <circle class="progress-ring__circle stroke-blue-200" stroke-width="6" fill="transparent" r="40" cx="50" cy="50"></circle>
                                <circle class="progress-ring__circle stroke-blue-600" stroke-width="6" fill="transparent" r="40" cx="50" cy="50" stroke-dasharray="251.2" stroke-dashoffset="251.2"></circle>
                            </svg>
                        </div>
                        <p class="text-gray-600">Waiting for scan...</p>
                    </div>
                    <div id="resultsSection" class="hidden">
                        <div class="mb-4">
                            <div class="flex justify-between items-center mb-2">
                                <h3 class="font-semibold text-gray-700">Detected Items</h3>
                                <span id="totalCount" class="bg-blue-100 text-blue-800 px-2 py-1 rounded-full text-sm font-medium">0</span>
                            </div>
                            <div id="detectedItems" class="space-y-2"></div>
                        </div>
                        <div class="border-t pt-4">
                            <h3 class="font-semibold text-gray-700 mb-2">Summary</h3>
                            <div id="summaryChart" class="h-40"></div>
                        </div>
                    </div>
                </div>
                <div class="p-4 border-t flex justify-end">
                    <button id="exportBtn" class="bg-green-600 hover:bg-green-700 text-white px-4 py-2 rounded-lg flex items-center gap-2 hidden">
                        <i data-feather="download"></i> Export Report
                    </button>
                </div>
            </div>
        </div>
        <!-- Supported Equipment Section -->
        <div class="mt-12 bg-white rounded-xl shadow-lg overflow-hidden">
            <div class="p-4 bg-blue-600 text-white">
                <h2 class="text-xl font-semibold flex items-center gap-2">
                    <i data-feather="check-circle"></i> Supported School Equipment
                </h2>
            </div>
            <div class="p-6 grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-6 gap-4">
                <div class="bg-blue-50 p-4 rounded-lg text-center">
                    <i data-feather="monitor" class="w-8 h-8 mx-auto text-blue-600 mb-2"></i>
                    <span class="text-gray-700">Monitors</span>
                </div>
                <div class="bg-blue-50 p-4 rounded-lg text-center">
                    <i data-feather="printer" class="w-8 h-8 mx-auto text-blue-600 mb-2"></i>
                    <span class="text-gray-700">Printers</span>
                </div>
                <div class="bg-blue-50 p-4 rounded-lg text-center">
                    <i data-feather="chair" class="w-8 h-8 mx-auto text-blue-600 mb-2"></i>
                    <span class="text-gray-700">Chairs</span>
                </div>
                <div class="bg-blue-50 p-4 rounded-lg text-center">
                    <i data-feather="table" class="w-8 h-8 mx-auto text-blue-600 mb-2"></i>
                    <span class="text-gray-700">Tables</span>
                </div>
                <div class="bg-blue-50 p-4 rounded-lg text-center">
                    <i data-feather="book" class="w-8 h-8 mx-auto text-blue-600 mb-2"></i>
                    <span class="text-gray-700">Books</span>
                </div>
                <div class="bg-blue-50 p-4 rounded-lg text-center">
                    <i data-feather="cpu" class="w-8 h-8 mx-auto text-blue-600 mb-2"></i>
                    <span class="text-gray-700">Computers</span>
                </div>
                <div class="bg-blue-50 p-4 rounded-lg text-center">
                    <i data-feather="keyboard" class="w-8 h-8 mx-auto text-blue-600 mb-2"></i>
                    <span class="text-gray-700">Keyboards</span>
                </div>
                <div class="bg-blue-50 p-4 rounded-lg text-center">
                    <i data-feather="mouse-pointer" class="w-8 h-8 mx-auto text-blue-600 mb-2"></i>
                    <span class="text-gray-700">Mice</span>
                </div>
                <div class="bg-blue-50 p-4 rounded-lg text-center">
                    <i data-feather="box" class="w-8 h-8 mx-auto text-blue-600 mb-2"></i>
                    <span class="text-gray-700">Desktops</span>
                </div>
                <div class="bg-blue-50 p-4 rounded-lg text-center">
                    <i data-feather="clock" class="w-8 h-8 mx-auto text-blue-600 mb-2"></i>
                    <span class="text-gray-700">Clocks</span>
                </div>
                <div class="bg-blue-50 p-4 rounded-lg text-center">
                    <i data-feather="battery" class="w-8 h-8 mx-auto text-blue-600 mb-2"></i>
                    <span class="text-gray-700">Projectors</span>
                </div>
                <div class="bg-blue-50 p-4 rounded-lg text-center">
                    <i data-feather="speaker" class="w-8 h-8 mx-auto text-blue-600 mb-2"></i>
                    <span class="text-gray-700">Speakers</span>
                </div>
            </div>
        </div>
</div>

    <script>
        document.addEventListener('DOMContentLoaded', async () => {
            feather.replace();
            // DOM Elements
            const cameraFeed = document.getElementById('cameraFeed');
            const detectionOverlay = document.getElementById('detectionOverlay');
            const noCameraSection = document.getElementById('noCamera');
            const enableCameraBtn = document.getElementById('enableCameraBtn');
            const captureBtn = document.getElementById('captureBtn');
            const uploadBtn = document.getElementById('uploadBtn');
            const fileInput = document.getElementById('fileInput');
            const loadingSection = document.getElementById('loadingSection');
            const resultsSection = document.getElementById('resultsSection');
            const detectedItems = document.getElementById('detectedItems');
            const totalCount = document.getElementById('totalCount');
            const exportBtn = document.getElementById('exportBtn');
            // Model and state
            let model;
            let stream;
            let isScanning = false;
            const detectedObjects = {};
            const targetClasses = [
                'chair', 'dining table', 'tv', 'laptop', 'book', 
                'mouse', 'keyboard', 'cell phone', 'monitor', 'printer',
                'backpack', 'scissors', 'bottle', 'cup', 'clock',
                'vase', 'microwave', 'refrigerator', 'desk', 'computer',
                'projector', 'speaker', 'calculator', 'whiteboard', 'bookshelf'
            ];
// Initialize COCO-SSD model
            async function initModel() {
                loadingSection.querySelector('p').textContent = 'Loading AI model...';
                try {
                    model = await cocoSsd.load();
                    loadingSection.querySelector('p').textContent = 'Model loaded successfully!';
                    setTimeout(() => {
                        loadingSection.querySelector('p').textContent = 'Ready to scan!';
                    }, 2000);
                } catch (error) {
                    loadingSection.querySelector('p').textContent = 'Failed to load model';
                    console.error('Model loading error:', error);
                }
            }
            // Scanner mode toggle
            function toggleScannerMode(isLive) {
                if (isLive) {
                    document.getElementById('liveScannerBtn').classList.add('bg-blue-600', 'text-white');
                    document.getElementById('liveScannerBtn').classList.remove('bg-gray-100', 'text-gray-700');
                    document.getElementById('uploadScannerBtn').classList.add('bg-gray-100', 'text-gray-700');
                    document.getElementById('uploadScannerBtn').classList.remove('bg-blue-600', 'text-white');
                    startCamera();
                } else {
                    document.getElementById('uploadScannerBtn').classList.add('bg-blue-600', 'text-white');
                    document.getElementById('uploadScannerBtn').classList.remove('bg-gray-100', 'text-gray-700');
                    document.getElementById('liveScannerBtn').classList.add('bg-gray-100', 'text-gray-700');
                    document.getElementById('liveScannerBtn').classList.remove('bg-blue-600', 'text-white');
                    if (stream) {
                        stream.getTracks().forEach(track => track.stop());
                    }
                    cameraFeed.srcObject = null;
                    detectionOverlay.innerHTML = '';
                    noCameraSection.classList.add('hidden');
                }
            }

            // Start camera
async function startCamera() {
                try {
                    stream = await navigator.mediaDevices.getUserMedia({ video: true });
                    cameraFeed.srcObject = stream;
                    noCameraSection.classList.add('hidden');
                    
                    // Start object detection when camera is ready
                    cameraFeed.onloadedmetadata = () => {
                        detectObjects();
                    };
                } catch (error) {
                    console.error('Error accessing camera:', error);
                    noCameraSection.classList.remove('hidden');
                }
            }
            
            // Detect objects in video feed
            async function detectObjects() {
                if (!model || isScanning) return;
                
                const predictions = await model.detect(cameraFeed);
                renderDetections(predictions);
                
                requestAnimationFrame(detectObjects);
            }
            
            // Render detection boxes
            function renderDetections(predictions) {
                detectionOverlay.innerHTML = '';
                
                const videoWidth = cameraFeed.videoWidth;
                const videoHeight = cameraFeed.videoHeight;
                const overlayWidth = detectionOverlay.offsetWidth;
                const overlayHeight = detectionOverlay.offsetHeight;
                
                predictions.forEach(prediction => {
                    if (!targetClasses.includes(prediction.class)) return;
                    
                    // Calculate scaled coordinates
                    const x = (prediction.bbox[0] / videoWidth) * overlayWidth;
                    const y = (prediction.bbox[1] / videoHeight) * overlayHeight;
                    const width = (prediction.bbox[2] / videoWidth) * overlayWidth;
                    const height = (prediction.bbox[3] / videoHeight) * overlayHeight;
                    
                    // Create detection box
                    const box = document.createElement('div');
                    box.style.position = 'absolute';
                    box.style.left = `${x}px`;
                    box.style.top = `${y}px`;
                    box.style.width = `${width}px`;
                    box.style.height = `${height}px`;
                    box.style.border = '2px solid rgba(59, 130, 246, 0.8)';
                    box.style.borderRadius = '4px';
                    box.style.pointerEvents = 'none';
                    
                    // Create label
                    const label = document.createElement('div');
                    label.className = 'object-tag';
                    label.style.left = `${x}px`;
                    label.style.top = `${y - 24}px`;
                    label.textContent = `${prediction.class} (${Math.round(prediction.score * 100)}%)`;
                    
                    detectionOverlay.appendChild(box);
                    detectionOverlay.appendChild(label);
                });
            }
            
            // Capture and analyze scene
            async function captureAndAnalyze() {
                if (isScanning) return;
                isScanning = true;
                
                // Show loading state
                loadingSection.classList.remove('hidden');
                resultsSection.classList.add('hidden');
                exportBtn.classList.add('hidden');
                
                // Clear previous results
                detectedItems.innerHTML = '';
                Object.keys(detectedObjects).forEach(key => delete detectedObjects[key]);
                
                // Run detection on current frame
                const predictions = await model.detect(cameraFeed);
                
                // Count objects
                predictions.forEach(prediction => {
                    if (targetClasses.includes(prediction.class)) {
                        const className = formatClassName(prediction.class);
                        detectedObjects[className] = (detectedObjects[className] || 0) + 1;
                    }
                });
                
                // Display results
                displayResults();
                isScanning = false;
            }
            
            // Format class names for display
            function formatClassName(className) {
                return className
                    .split(' ')
                    .map(word => word.charAt(0).toUpperCase() + word.slice(1))
                    .join(' ');
            }
            
            // Display detection results
            function displayResults() {
                // Update total count
                const total = Object.values(detectedObjects).reduce((sum, count) => sum + count, 0);
                totalCount.textContent = total;
                
                // Populate detected items
                Object.entries(detectedObjects).forEach(([item, count]) => {
                    const itemElement = document.createElement('div');
                    itemElement.className = 'flex justify-between items-center bg-gray-50 p-3 rounded-lg';
                    itemElement.innerHTML = `
                        <span class="text-gray-700">${item}</span>
                        <span class="bg-blue-100 text-blue-800 px-2 py-1 rounded-full text-sm font-medium">${count}</span>
                    `;
                    detectedItems.appendChild(itemElement);
                });
                
                // Show results and export button
                loadingSection.classList.add('hidden');
                resultsSection.classList.remove('hidden');
                if (total > 0) {
                    exportBtn.classList.remove('hidden');
                }
                
                // Simple chart animation
                animateChart();
            }
            
            // Simple chart animation
            function animateChart() {
                const chart = document.getElementById('summaryChart');
                chart.innerHTML = '';
                
                if (Object.keys(detectedObjects).length === 0) {
                    chart.innerHTML = '<p class="text-gray-500 text-center mt-12">No items detected</p>';
                    return;
                }
                
                const maxCount = Math.max(...Object.values(detectedObjects));
                const itemHeight = 20;
                const gap = 10;
                const width = chart.offsetWidth;
                const height = chart.offsetHeight;
                const availableHeight = height - (Object.keys(detectedObjects).length * (itemHeight + gap));
                
                let yPos = 0;
                Object.entries(detectedObjects).forEach(([item, count]) => {
                    const barGroup = document.createElement('div');
                    barGroup.className = 'mb-2';
                    
                    const label = document.createElement('div');
                    label.className = 'flex justify-between text-xs text-gray-600 mb-1';
                    label.innerHTML = `
                        <span>${item}</span>
                        <span>${count}</span>
                    `;
                    
                    const barContainer = document.createElement('div');
                    barContainer.className = 'h-2 bg-gray-200 rounded-full overflow-hidden';
                    
                    const bar = document.createElement('div');
                    bar.className = 'h-full bg-blue-600 rounded-full';
                    bar.style.width = '0';
                    
                    setTimeout(() => {
                        bar.style.width = `${(count / maxCount) * 100}%`;
                        bar.style.transition = 'width 0.6s ease-out';
                    }, 100);
                    
                    barContainer.appendChild(bar);
                    barGroup.appendChild(label);
                    barGroup.appendChild(barContainer);
                    chart.appendChild(barGroup);
                    
                    yPos += itemHeight + gap;
                });
            }
            
            // Export report
            function exportReport() {
                // In a real app, this would generate a PDF or CSV
                alert('Report exported (this would generate a PDF/CSV in a real application)');
            }
            // Analyze uploaded image
            async function analyzeUploadedImage(file) {
                if (!model || isScanning) return;
                isScanning = true;
                
                // Show loading state
                loadingSection.classList.remove('hidden');
                resultsSection.classList.add('hidden');
                exportBtn.classList.add('hidden');
                detectedItems.innerHTML = '';
                Object.keys(detectedObjects).forEach(key => delete detectedObjects[key]);
                
                // Create image element
                const img = new Image();
                img.src = URL.createObjectURL(file);
                
                img.onload = async () => {
                    // Display the uploaded image
                    cameraFeed.srcObject = null;
                    detectionOverlay.innerHTML = '';
                    const imgElement = document.createElement('img');
                    imgElement.src = img.src;
                    imgElement.style.width = '100%';
                    imgElement.style.height = '100%';
                    imgElement.style.objectFit = 'contain';
                    detectionOverlay.appendChild(imgElement);
                    
                    // Run detection
                    const predictions = await model.detect(img);
                    
                    // Count objects
                    predictions.forEach(prediction => {
                        if (targetClasses.includes(prediction.class)) {
                            const className = formatClassName(prediction.class);
                            detectedObjects[className] = (detectedObjects[className] || 0) + 1;
                            
                            // Draw detection boxes on image
                            const box = document.createElement('div');
                            box.style.position = 'absolute';
                            box.style.left = `${prediction.bbox[0]}px`;
                            box.style.top = `${prediction.bbox[1]}px`;
                            box.style.width = `${prediction.bbox[2]}px`;
                            box.style.height = `${prediction.bbox[3]}px`;
                            box.style.border = '2px solid rgba(59, 130, 246, 0.8)';
                            box.style.pointerEvents = 'none';
                            
                            const label = document.createElement('div');
                            label.className = 'object-tag';
                            label.style.left = `${prediction.bbox[0]}px`;
                            label.style.top = `${prediction.bbox[1] - 24}px`;
                            label.textContent = `${prediction.class} (${Math.round(prediction.score * 100)}%)`;
                            
                            detectionOverlay.appendChild(box);
                            detectionOverlay.appendChild(label);
                        }
                    });
                    
                    // Display results
                    displayResults();
                    isScanning = false;
                };
            }

            // Event listeners
            enableCameraBtn.addEventListener('click', startCamera);
            captureBtn.addEventListener('click', captureAndAnalyze);
            uploadBtn.addEventListener('click', () => fileInput.click());
            fileInput.addEventListener('change', (e) => {
                if (e.target.files.length > 0) {
                    analyzeUploadedImage(e.target.files[0]);
                }
            });
            exportBtn.addEventListener('click', exportReport);
            // Clear results
            function clearResults() {
                detectedItems.innerHTML = '';
                totalCount.textContent = '0';
                Object.keys(detectedObjects).forEach(key => delete detectedObjects[key]);
                loadingSection.classList.remove('hidden');
                resultsSection.classList.add('hidden');
                exportBtn.classList.add('hidden');
                document.getElementById('summaryChart').innerHTML = '';
                loadingSection.querySelector('p').textContent = 'Ready to scan!';
            }

            // Initialize
            initModel();
            
            // Event listeners for mode toggle
            document.getElementById('liveScannerBtn').addEventListener('click', () => toggleScannerMode(true));
            document.getElementById('uploadScannerBtn').addEventListener('click', () => toggleScannerMode(false));
            document.getElementById('clearResultsBtn').addEventListener('click', clearResults);
            
            // Start with camera disabled to respect privacy
            toggleScannerMode(true);
});
    </script>
    <script>
        feather.replace();
    </script>
</body>
</html>