File size: 23,835 Bytes
6c001d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Premium AI Object Detector</title>
    <script src="https://cdn.jsdelivr.net/npm/onnxruntime-web/dist/ort.min.js"></script>
    <style>
        /* --- Base & Theming --- */
        :root {
            --bg-start: #f4f6f9;
            --bg-end: #e2e8f0;
            --card-bg: rgba(255, 255, 255, 0.7);
            --text-primary: #1f2937;
            --text-secondary: #4b5563;
            --border-color: rgba(0, 0, 0, 0.1);
            --shadow: 0 10px 30px rgba(0, 0, 0, 0.07);
            --primary-gradient: linear-gradient(90deg, #3b82f6, #8b5cf6);
            --primary-hover-gradient: linear-gradient(90deg, #2563eb, #7c3aed);
            --detail-bg: rgba(0, 0, 0, 0.04);
        }
        html.dark {
            --bg-start: #111827;
            --bg-end: #1f2937;
            --card-bg: rgba(31, 41, 55, 0.7);
            --text-primary: #f9fafb;
            --text-secondary: #d1d5db;
            --border-color: rgba(255, 255, 255, 0.1);
            --shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
            --primary-gradient: linear-gradient(90deg, #3b82f6, #8b5cf6);
            --primary-hover-gradient: linear-gradient(90deg, #2563eb, #7c3aed);
            --detail-bg: rgba(255, 255, 255, 0.06);
        }
        /* --- Static Background --- */
        body {
            background: linear-gradient(-45deg, var(--bg-start), var(--bg-end));
            transition: background 0.5s;
        }
        /* Helpers */
        .text-text-primary {
            color: var(--text-primary);
        }
        .text-text-secondary {
            color: var(--text-secondary);
        }
        .border-border-color {
            border-color: var(--border-color);
        }
        .bg-card-bg {
            background-color: var(--card-bg);
        }
        .bg-detail-bg {
            background-color: var(--detail-bg);
        }
        .backdrop-blur-xl {
            backdrop-filter: blur(16px);
            -webkit-backdrop-filter: blur(16px);
        }
        /* Accordion transition */
        .accordion-content {
            max-height: 0;
            overflow: hidden;
            transition: max-height 0.4s ease-out, padding 0.4s ease-out;
        }
    </style>
    <script>
        // Minimal Tailwind config for CDN
        tailwind.config = { darkMode: 'class' }
    </script>
    <script src="https://cdn.tailwindcss.com"></script>
</head>

<body class="min-h-screen flex items-center justify-center p-4 antialiased font-sans bg-gray-100 dark:bg-gray-900">

    <!-- Model Loading Overlay -->
    <div id="model-loading-overlay"
        class="fixed inset-0 bg-black/70 flex flex-col items-center justify-center z-50 transition-opacity duration-300">
        <div class="flex flex-col items-center gap-4">
            <div class="animate-spin rounded-full h-16 w-16 border-b-4 border-blue-400"></div>
            <span class="text-white text-2xl font-bold tracking-wide drop-shadow-lg">Loading AI Model...</span>
            <span class="text-blue-200 text-sm">This may take a few seconds on first load.</span>
        </div>
    </div>

    <!-- Main Application Card -->
    <div id="detector-card"
        class="w-full max-w-2xl bg-card-bg border border-border-color rounded-3xl shadow-2xl backdrop-blur-xl p-6 md:p-8 transition-all duration-500">

        <!-- Header -->
        <header class="flex justify-between items-center mb-6">
            <h1 class="text-2xl font-bold text-text-primary">AI Vision</h1>
            <button id="theme-switcher"
                class="w-10 h-10 flex items-center justify-center rounded-full hover:bg-black/5 dark:hover:bg-white/10 transition"
                aria-label="Switch theme" title="Switch theme">
                <svg id="sun-icon" class="w-6 h-6 text-text-secondary" fill="none" stroke="currentColor"
                    viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                        d="M12 3v1m0 16v1m9-9h-1M4 12H3m15.364 6.364l-.707-.707M6.343 6.343l-.707-.707m12.728 0l-.707.707M6.343 17.657l-.707.707M16 12a4 4 0 11-8 0 4 4 0 018 0z">
                    </path>
                </svg>
                <svg id="moon-icon" class="w-6 h-6 text-text-secondary hidden" fill="none" stroke="currentColor"
                    viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                        d="M20.354 15.354A9 9 0 018.646 3.646 9.003 9.003 0 0012 21a9.003 9.003 0 008.354-5.646z">
                    </path>
                </svg>
            </button>
        </header>

        <!-- Dynamic Content Area -->
        <div id="content-area">

            <!-- Uploader View -->
            <div id="uploader-view">
                <div id="drop-zone"
                    class="border-2 border-dashed border-gray-300 dark:border-gray-600 rounded-2xl p-8 text-center cursor-pointer hover:border-blue-500 dark:hover:border-blue-400 hover:scale-[1.02] transition-all duration-300"
                    tabindex="0" aria-label="Image upload drop zone">
                    <div class="flex flex-col items-center justify-center space-y-4 text-text-secondary">
                        <svg class="w-16 h-16 opacity-50" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"
                                d="M7 16a4 4 0 01-.88-7.903A5 5 0 1115.9 6L16 6a5 5 0 011 9.9M15 13l-3-3m0 0l-3 3m3-3v12">
                            </path>
                        </svg>
                        <p class="font-semibold text-text-primary">Drag & Drop Image Here</p>
                        <p class="text-sm">or</p>
                        <label for="image-upload"
                            class="px-6 py-2 rounded-lg font-semibold text-white cursor-pointer transition-all duration-300 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"
                            style="background: var(--primary-gradient);"
                            onmouseover="this.style.background= 'var(--primary-hover-gradient)'"
                            onmouseout="this.style.background= 'var(--primary-gradient)'">
                            Select File
                        </label>
                        <input type="file" id="image-upload" class="hidden" accept="image/*">
                    </div>
                </div>
            </div>

            <!-- Results View (hidden by default) -->
            <div id="results-view" class="hidden">
                <div class="relative mb-4 group">
                    <canvas id="output-canvas" class="w-full h-auto rounded-2xl shadow-md"></canvas>
                    <button id="reset-button"
                        class="absolute top-3 right-3 w-10 h-10 bg-black/40 hover:bg-black/60 backdrop-blur-sm rounded-full flex items-center justify-center transition opacity-0 group-hover:opacity-100"
                        title="Analyze another image" aria-label="Analyze another image">
                        <svg class="w-6 h-6 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                d="M19 12H5M12 19l-7-7 7-7"></path>
                        </svg>
                    </button>
                </div>
                <div id="results-summary" class="max-h-64 overflow-y-auto space-y-2 pr-2">
                    <!-- Detected objects will be listed here -->
                </div>
            </div>
        </div>

        <!-- Status Footer -->
        <footer id="status-footer" class="mt-6 text-center text-sm text-text-secondary h-6"></footer>
    </div>

    <!-- Toast Notification -->
    <div id="toast"
        class="fixed bottom-6 right-6 bg-red-600 text-white px-4 py-2 rounded-lg shadow-lg opacity-0 transform translate-y-4 pointer-events-none transition-all duration-300 z-50">
    </div>


    <script>
        // DOM Elements
        const uploaderView = document.getElementById('uploader-view');
        const resultsView = document.getElementById('results-view');
        const dropZone = document.getElementById('drop-zone');
        const imageUpload = document.getElementById('image-upload');
        const outputCanvas = document.getElementById('output-canvas');
        const resultsSummary = document.getElementById('results-summary');
        const statusFooter = document.getElementById('status-footer');
        const resetButton = document.getElementById('reset-button');
        const themeSwitcher = document.getElementById('theme-switcher');
        const sunIcon = document.getElementById('sun-icon');
        const moonIcon = document.getElementById('moon-icon');
        const modelLoadingOverlay = document.getElementById('model-loading-overlay');
        const toast = document.getElementById('toast');
        let session;
        // --- Constants ---
        const MODEL_URL = "https://huggingface.co/aaurelions/yolo11n.onnx/resolve/main/yolo11n.onnx";
        const COCO_CLASSES = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'];
        const COLORS = ["#3b82f6", "#ef4444", "#22c55e", "#f97316", "#8b5cf6", "#fde047", "#ec4899", "#14b8a6", "#64748b"];
        // --- UI Logic ---
        function showToast(message, duration = 3000) {
            toast.textContent = message;
            toast.classList.remove('opacity-0', 'translate-y-4');
            setTimeout(() => {
                toast.classList.add('opacity-0', 'translate-y-4');
            }, duration);
        }
        function showModelLoading(show) {
            modelLoadingOverlay.style.display = show ? 'flex' : 'none';
            modelLoadingOverlay.classList.toggle('opacity-0', !show);
        }
        function setDarkMode(isDark) {
            document.documentElement.classList.toggle('dark', isDark);
            sunIcon.classList.toggle('hidden', isDark);
            moonIcon.classList.toggle('hidden', !isDark);
            localStorage.setItem('theme', isDark ? 'dark' : 'light');
        }
        themeSwitcher.addEventListener('click', () => setDarkMode(!document.documentElement.classList.contains('dark')));
        resetButton.addEventListener('click', () => {
            resultsView.classList.add('hidden');
            uploaderView.classList.remove('hidden');
            statusFooter.textContent = 'Model is ready. Select an image.';
            imageUpload.value = ''; // Clear file input
        });
        // --- Drag & Drop Logic ---
        ['dragenter', 'dragover'].forEach(eventName => {
            dropZone.addEventListener(eventName, (e) => {
                e.preventDefault();
                dropZone.classList.add('ring-4', 'ring-blue-400', 'scale-105', 'border-solid');
            }, false);
        });
        ['dragleave', 'drop'].forEach(eventName => {
            dropZone.addEventListener(eventName, (e) => {
                e.preventDefault();
                dropZone.classList.remove('ring-4', 'ring-blue-400', 'scale-105', 'border-solid');
            }, false);
        });
        dropZone.addEventListener('drop', (e) => {
            const file = e.dataTransfer.files[0];
            if (file) handleFile(file);
        });
        imageUpload.addEventListener('change', (e) => { if (e.target.files[0]) handleFile(e.target.files[0]); });
        // --- Core AI & Rendering Logic ---
        async function handleFile(file) {
            if (!session) { showToast("AI Model is not loaded yet."); return; }
            if (!file.type.startsWith('image/')) { showToast("Please upload an image file."); return; }
            statusFooter.innerHTML = '<div class="flex items-center justify-center gap-2"><div class="animate-spin rounded-full h-4 w-4 border-b-2 border-text-primary"></div><span>Analyzing image...</span></div>';
            uploaderView.classList.add('hidden');
            resultsView.classList.remove('hidden');
            const image = new Image();
            image.src = URL.createObjectURL(file);
            image.onload = async () => {
                try {
                    const inputTensor = preprocess(image);
                    const results = await session.run({ 'images': inputTensor });
                    const boxes = postprocess(results.output0, image.width, image.height);
                    const finalBoxes = nonMaxSuppression(boxes, 0.5, 0.45);
                    drawDetections(image, finalBoxes);
                    displaySummary(finalBoxes);
                    statusFooter.textContent = `Found ${finalBoxes.length} unique object(s).`;
                } catch (e) {
                    showToast(`An error occurred: ${e.message}`);
                    statusFooter.textContent = "Error during analysis.";
                    resetButton.click(); // Go back to uploader
                } finally {
                    URL.revokeObjectURL(image.src);
                }
            };
            image.onerror = () => {
                showToast("Could not load the selected image file.");
                statusFooter.textContent = "Error: Invalid image.";
                URL.revokeObjectURL(image.src);
            }
        }
        function preprocess(img) {
            const canvas = document.createElement('canvas');
            const modelWidth = 640, modelHeight = 640;
            canvas.width = modelWidth;
            canvas.height = modelHeight;
            const ctx = canvas.getContext('2d');
            ctx.drawImage(img, 0, 0, modelWidth, modelHeight);
            const imageData = ctx.getImageData(0, 0, modelWidth, modelHeight);
            const float32Data = new Float32Array(1 * 3 * modelHeight * modelWidth);
            for (let i = 0, j = 0; i < imageData.data.length; i += 4, j++) {
                float32Data[j] = imageData.data[i] / 255.0; // R
                float32Data[modelWidth * modelHeight + j] = imageData.data[i + 1] / 255.0; // G
                float32Data[2 * modelWidth * modelHeight + j] = imageData.data[i + 2] / 255.0; // B
            }
            return new ort.Tensor('float32', float32Data, [1, 3, modelHeight, modelWidth]);
        }
        function postprocess(tensor, originalWidth, originalHeight) {
            const data = tensor.data;
            const boxes = [];
            const modelWidth = 640, modelHeight = 640;
            const xRatio = originalWidth / modelWidth;
            const yRatio = originalHeight / modelHeight;
            for (let i = 0; i < tensor.dims[2]; i++) {
                const classScores = [];
                for (let j = 0; j < COCO_CLASSES.length; j++) {
                    classScores.push(data[(4 + j) * tensor.dims[2] + i]);
                }
                const maxScore = Math.max(...classScores);
                if (maxScore > 0.25) { // Confidence threshold
                    const classId = classScores.indexOf(maxScore);
                    const [cx, cy, w, h] = [data[i], data[tensor.dims[2] + i], data[2 * tensor.dims[2] + i], data[3 * tensor.dims[2] + i]];
                    const x1 = (cx - w / 2) * xRatio;
                    const y1 = (cy - h / 2) * yRatio;
                    const x2 = (cx + w / 2) * xRatio;
                    const y2 = (cy + h / 2) * yRatio;
                    boxes.push({ classId, confidence: maxScore, box: [x1, y1, x2, y2] });
                }
            }
            return boxes;
        }
        function nonMaxSuppression(boxes, iouThreshold, scoreThreshold) {
            boxes.sort((a, b) => b.confidence - a.confidence);
            const finalBoxes = [];
            while (boxes.length > 0) {
                const current = boxes.shift();
                if (current.confidence < scoreThreshold) continue;
                finalBoxes.push(current);
                boxes = boxes.filter(box => {
                    if (box.classId !== current.classId) return true;
                    const iou = calculateIoU(current.box, box.box);
                    return iou <= iouThreshold;
                });
            }
            return finalBoxes;
        }
        function calculateIoU(box1, box2) {
            const [x1, y1, x2, y2] = box1;
            const [x1_b, y1_b, x2_b, y2_b] = box2;
            const interX1 = Math.max(x1, x1_b);
            const interY1 = Math.max(y1, y1_b);
            const interX2 = Math.min(x2, x2_b);
            const interY2 = Math.min(y2, y2_b);
            const interArea = Math.max(0, interX2 - interX1) * Math.max(0, interY2 - interY1);
            const box1Area = (x2 - x1) * (y2 - y1);
            const box2Area = (x2_b - x1_b) * (y2_b - y1_b);
            const unionArea = box1Area + box2Area - interArea;
            return unionArea > 0 ? interArea / unionArea : 0;
        }
        function drawDetections(img, boxes) {
            const ctx = outputCanvas.getContext('2d');
            outputCanvas.width = img.naturalWidth;
            outputCanvas.height = img.naturalHeight;
            ctx.drawImage(img, 0, 0, outputCanvas.width, outputCanvas.height);
            boxes.forEach(det => {
                const [x1, y1, x2, y2] = det.box;
                const w = x2 - x1;
                const h = y2 - y1;
                const color = COLORS[det.classId % COLORS.length];
                ctx.strokeStyle = color;
                ctx.lineWidth = 3;
                ctx.strokeRect(x1, y1, w, h);
                ctx.fillStyle = color;
                const label = `${COCO_CLASSES[det.classId]} (${(det.confidence * 100).toFixed(1)}%)`;
                ctx.font = 'bold 14px sans-serif';
                const textWidth = ctx.measureText(label).width;
                ctx.fillRect(x1 - 1, y1 - 18, textWidth + 8, 20);
                ctx.fillStyle = '#ffffff';
                ctx.fillText(label, x1 + 4, y1 - 4);
            });
        }
        function displaySummary(boxes) {
            resultsSummary.innerHTML = '';
            if (boxes.length === 0) {
                resultsSummary.innerHTML = '<p class="text-text-secondary text-center">No objects detected.</p>';
                return;
            }
            // Group by class
            const grouped = {};
            boxes.forEach(box => {
                const className = COCO_CLASSES[box.classId];
                if (!grouped[className]) {
                    grouped[className] = { boxes: [], color: COLORS[box.classId % COLORS.length] };
                }
                grouped[className].boxes.push(box);
            });
            const sortedClasses = Object.entries(grouped).sort(([, a], [, b]) => b.boxes.length - a.boxes.length);
            sortedClasses.forEach(([className, data]) => {
                const wrapper = document.createElement('div');
                wrapper.className = 'bg-gray-500/10 rounded-lg overflow-hidden';
                const header = document.createElement('button');
                header.className = 'w-full flex items-center justify-between p-3 text-left transition-colors hover:bg-gray-500/20';
                header.innerHTML = `
                    <div class="flex items-center gap-3">
                        <div class="w-2.5 h-8 rounded" style="background-color: ${data.color}"></div>
                        <span class="font-bold text-lg text-text-primary">${data.boxes.length} x</span>
                        <span class="font-medium text-text-primary text-lg">${className}</span>
                    </div>
                    <svg class="w-5 h-5 text-text-secondary transition-transform transform" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path></svg>
                `;
                const content = document.createElement('div');
                content.className = 'accordion-content bg-detail-bg';
                const detailList = document.createElement('ul');
                detailList.className = 'text-sm p-4 my-4';
                data.boxes.sort((a, b) => b.confidence - a.confidence).forEach((box, i) => {
                    const [x1, y1, x2, y2] = box.box;
                    const w = x2 - x1;
                    const h = y2 - y1;
                    const listItem = document.createElement('li');
                    listItem.className = 'flex justify-between items-center text-text-secondary';
                    listItem.innerHTML = `
                        <span class="font-mono text-xs">#${i + 1} Confidence: <b class="text-text-primary">${(box.confidence * 100).toFixed(1)}%</b></span>
                        <span class="font-mono text-xs">W:${Math.round(w)} H:${Math.round(h)} @ X:${Math.round(x1)},Y:${Math.round(y1)}</span>
                    `;
                    detailList.appendChild(listItem);
                });
                content.appendChild(detailList);
                wrapper.appendChild(header);
                wrapper.appendChild(content);
                resultsSummary.appendChild(wrapper);
                // Accordion click listener
                header.addEventListener('click', () => {
                    const icon = header.querySelector('svg');
                    const isExpanded = content.style.maxHeight && content.style.maxHeight !== '0px';
                    if (isExpanded) {
                        content.style.maxHeight = '0px';
                        content.style.paddingTop = '0px';
                        content.style.paddingBottom = '0px';
                        icon.classList.remove('rotate-180');
                    } else {
                        content.style.paddingTop = '0px';
                        content.style.paddingBottom = '1rem';
                        content.style.maxHeight = content.scrollHeight + "px";
                        icon.classList.add('rotate-180');
                    }
                });
            });
        }
        // --- Initialization ---
        async function initialize() {
            setDarkMode(localStorage.getItem('theme') === 'dark' || (window.matchMedia('(prefers-color-scheme: dark)').matches && !localStorage.getItem('theme')));
            statusFooter.textContent = 'Initializing AI engine...';
            showModelLoading(true);
            try {
                session = await ort.InferenceSession.create(MODEL_URL, { executionProviders: ['wasm'] });
                statusFooter.textContent = 'Model is ready. Select an image.';
            } catch (e) {
                statusFooter.textContent = `Error loading model: ${e.message}`;
                showToast(`Failed to load the AI model. Please refresh. ${e.message}`);
            } finally {
                showModelLoading(false);
            }
        }
        document.addEventListener('DOMContentLoaded', initialize);
    </script>
</body>

</html>