File size: 26,022 Bytes
f33570b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf50f05
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SAM3 Object Detection Browser</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
            line-height: 1.5;
            color: #222;
            background: #fff;
            font-size: 15px;
        }

        .container {
            max-width: 1400px;
            margin: 0 auto;
            padding: 40px 20px;
        }

        header {
            border-bottom: 1px solid #ddd;
            padding-bottom: 20px;
            margin-bottom: 40px;
        }

        h1 {
            color: #222;
            font-weight: 400;
            font-size: 28px;
            margin-bottom: 8px;
        }

        .subtitle {
            color: #666;
            font-size: 15px;
            font-weight: 300;
            margin-bottom: 12px;
        }

        .header-links {
            font-size: 13px;
            color: #666;
        }

        .header-links a {
            color: #222;
            text-decoration: none;
            border-bottom: 1px solid #222;
        }

        .header-links a:hover {
            border-bottom-color: #666;
        }

        .example-datasets {
            margin-top: 8px;
            font-size: 12px;
            color: #666;
        }

        .example-datasets a {
            color: #666;
            text-decoration: none;
            border-bottom: 1px dotted #999;
            cursor: pointer;
            margin-right: 12px;
        }

        .example-datasets a:hover {
            color: #222;
            border-bottom-color: #222;
        }

        .controls {
            border: 1px solid #ddd;
            padding: 20px;
            margin-bottom: 40px;
            background: #fafafa;
        }

        .control-group {
            margin-bottom: 18px;
        }

        .control-group:last-child {
            margin-bottom: 0;
        }

        label {
            display: block;
            font-weight: 400;
            margin-bottom: 6px;
            color: #444;
            font-size: 14px;
        }

        input[type="text"],
        select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 2px;
            font-size: 14px;
            background: white;
        }

        input[type="text"]:focus,
        select:focus {
            outline: none;
            border-color: #888;
        }

        input[type="range"] {
            width: 100%;
            margin-right: 10px;
        }

        .slider-container {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .slider-value {
            min-width: 50px;
            font-weight: 400;
            color: #222;
        }

        .checkbox-container {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        input[type="checkbox"] {
            width: 18px;
            height: 18px;
            cursor: pointer;
        }

        .stats {
            border-top: 1px solid #ddd;
            border-bottom: 1px solid #ddd;
            padding: 25px 0;
            margin-bottom: 40px;
        }

        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 30px;
        }

        .stat-item {
            text-align: center;
            padding: 0;
        }

        .stat-value {
            font-size: 36px;
            font-weight: 300;
            color: #222;
            margin-bottom: 4px;
            letter-spacing: -0.5px;
        }

        .stat-label {
            font-size: 13px;
            color: #666;
            font-weight: 400;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .loading {
            text-align: center;
            padding: 40px;
            font-size: 14px;
            color: #666;
        }

        .error {
            background: #fff;
            border: 1px solid #d00;
            padding: 15px;
            margin: 20px 0;
            color: #d00;
            font-size: 14px;
        }

        .image-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(400px, 1fr));
            gap: 40px;
            margin-bottom: 40px;
        }

        .image-card {
            background: white;
            border: 1px solid #ddd;
            overflow: hidden;
        }

        .image-card:hover {
            border-color: #999;
        }

        .image-container {
            position: relative;
            width: 100%;
            background: #000;
        }

        .image-container img {
            width: 100%;
            height: auto;
            display: block;
        }

        .image-container canvas {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            pointer-events: none;
            z-index: 10;
        }

        .image-info {
            padding: 12px;
            border-top: 1px solid #eee;
        }

        .image-index {
            font-size: 11px;
            color: #999;
            margin-bottom: 6px;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .detections-count {
            font-weight: 400;
            color: #222;
            margin-bottom: 8px;
            font-size: 14px;
        }

        .detections-list {
            font-size: 13px;
            color: #666;
        }

        .detection-item {
            padding: 3px 0;
        }

        .confidence-high {
            color: #27ae60;
            font-weight: 600;
        }

        .confidence-medium {
            color: #f39c12;
            font-weight: 600;
        }

        .confidence-low {
            color: #e74c3c;
            font-weight: 600;
        }

        .pagination {
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 20px;
            margin: 40px 0;
            padding-top: 20px;
            border-top: 1px solid #ddd;
        }

        .pagination button {
            padding: 8px 16px;
            border: 1px solid #666;
            background: white;
            color: #222;
            cursor: pointer;
            font-size: 13px;
            transition: all 0.2s;
        }

        .pagination button:hover:not(:disabled) {
            background: #222;
            color: white;
        }

        .pagination button:disabled {
            border-color: #ddd;
            color: #ccc;
            cursor: not-allowed;
        }

        .pagination span {
            color: #666;
            font-size: 13px;
        }

        .load-button {
            padding: 10px 20px;
            border: 1px solid #666;
            background: white;
            color: #222;
            cursor: pointer;
            font-size: 14px;
            font-weight: 400;
            transition: all 0.2s;
            width: 100%;
            margin-top: 12px;
        }

        .load-button:hover {
            background: #222;
            color: white;
        }

        .load-button:disabled {
            border-color: #ddd;
            color: #ccc;
            cursor: not-allowed;
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>SAM3 Object Detection Browser</h1>
            <p class="subtitle">Browse and explore object detection results from Meta's SAM3 model</p>
            <p class="header-links">Create your own detection dataset with the <a href="https://huggingface.co/datasets/uv-scripts/sam3" target="_blank">SAM3 detection script</a></p>
        </header>

        <div class="controls">
            <div class="control-group">
                <label for="dataset-id">Dataset ID:</label>
                <input type="text" id="dataset-id" value="davanstrien/newspapers-image-predictions" placeholder="username/dataset-name">
                <div class="example-datasets">
                    Examples:
                    <a href="#" data-dataset="davanstrien/newspapers-image-predictions">photographs</a>
                    <a href="#" data-dataset="davanstrien/newspapers-illustration-predictions">illustrations</a>
                </div>
            </div>

            <div class="control-group">
                <label for="split">Split:</label>
                <select id="split">
                    <option value="train">train</option>
                    <option value="validation">validation</option>
                    <option value="test">test</option>
                </select>
            </div>

            <div class="control-group">
                <label for="confidence-threshold">Confidence Threshold: <span id="confidence-value" class="slider-value">0.50</span></label>
                <div class="slider-container">
                    <input type="range" id="confidence-threshold" min="0" max="1" step="0.05" value="0.5">
                </div>
            </div>

            <div class="control-group">
                <div class="checkbox-container">
                    <input type="checkbox" id="only-detections">
                    <label for="only-detections" style="margin-bottom: 0;">Show only images with detections</label>
                </div>
            </div>

            <button class="load-button" id="load-dataset">Load Dataset</button>
        </div>

        <div id="stats-container"></div>
        <div id="loading" class="loading" style="display: none;">Loading dataset...</div>
        <div id="error" class="error" style="display: none;"></div>
        <div id="image-grid" class="image-grid"></div>
        <div id="pagination" class="pagination" style="display: none;"></div>
    </div>

    <script>
        // Global state
        let dataset = [];
        let filteredDataset = [];
        let currentPage = 0;
        const itemsPerPage = 12;
        let confidenceThreshold = 0.5;
        let showOnlyDetections = false;

        // DOM elements
        const datasetIdInput = document.getElementById('dataset-id');
        const splitSelect = document.getElementById('split');
        const confidenceSlider = document.getElementById('confidence-threshold');
        const confidenceValue = document.getElementById('confidence-value');
        const onlyDetectionsCheckbox = document.getElementById('only-detections');
        const loadButton = document.getElementById('load-dataset');
        const statsContainer = document.getElementById('stats-container');
        const loadingDiv = document.getElementById('loading');
        const errorDiv = document.getElementById('error');
        const imageGrid = document.getElementById('image-grid');
        const paginationDiv = document.getElementById('pagination');

        // Initialize state from DOM (in case browser persists checkbox state)
        showOnlyDetections = onlyDetectionsCheckbox.checked;

        // Event listeners
        confidenceSlider.addEventListener('input', (e) => {
            confidenceThreshold = parseFloat(e.target.value);
            confidenceValue.textContent = confidenceThreshold.toFixed(2);
            if (dataset.length > 0) {
                filterAndRender();
            }
        });

        onlyDetectionsCheckbox.addEventListener('change', (e) => {
            showOnlyDetections = e.target.checked;
            if (dataset.length > 0) {
                filterAndRender();
            }
        });

        loadButton.addEventListener('click', loadDataset);

        // Handle example dataset links
        document.querySelectorAll('.example-datasets a').forEach(link => {
            link.addEventListener('click', (e) => {
                e.preventDefault();
                const datasetId = e.target.getAttribute('data-dataset');
                datasetIdInput.value = datasetId;
                loadDataset();
            });
        });

        // Load dataset from HuggingFace with progressive rendering
        async function loadDataset() {
            const datasetId = datasetIdInput.value.trim();
            const split = splitSelect.value;

            if (!datasetId) {
                showError('Please enter a dataset ID');
                return;
            }

            loadingDiv.style.display = 'block';
            errorDiv.style.display = 'none';
            statsContainer.innerHTML = '';
            imageGrid.innerHTML = '';
            paginationDiv.style.display = 'none';
            loadButton.disabled = true;

            dataset = [];
            let isLoadingComplete = false;

            try {
                // Fetch rows progressively and render after each batch
                let offset = 0;
                const batchSize = 50; // Smaller batches for faster initial display
                let hasMore = true;
                let isFirstBatch = true;

                while (hasMore) {
                    const url = `https://datasets-server.huggingface.co/rows?dataset=${encodeURIComponent(datasetId)}&config=default&split=${split}&offset=${offset}&length=${batchSize}`;
                    const response = await fetch(url);

                    if (!response.ok) {
                        if (offset === 0) {
                            throw new Error(`Failed to load dataset. Check dataset ID and split name.`);
                        }
                        break;
                    }

                    const data = await response.json();

                    if (!data.rows || data.rows.length === 0) {
                        hasMore = false;
                        break;
                    }

                    // Convert rows to dataset format
                    const newRows = data.rows.map(item => ({
                        index: offset + item.row_idx,
                        image: item.row.image, // Keep as object or string
                        objects: item.row.objects || { bbox: [], category: [], score: [] },
                        ...item.row
                    }));

                    dataset = dataset.concat(newRows);
                    offset += data.rows.length;

                    // Render immediately after first batch
                    if (isFirstBatch) {
                        filterAndRender();
                        loadingDiv.textContent = `Loading more... (${dataset.length} rows loaded)`;
                        isFirstBatch = false;
                    } else {
                        // Update view and stats as we load more
                        filterDataset();
                        renderStats();
                        // Only re-render current page if we're on page 1 (to avoid disrupting user)
                        if (currentPage === 0) {
                            renderPage();
                            renderPagination();
                        }
                        loadingDiv.textContent = `Loading more... (${dataset.length} rows loaded)`;
                    }

                    // Stop if we got fewer rows than requested (end of dataset)
                    if (data.rows.length < batchSize) {
                        hasMore = false;
                    }

                    // Limit to prevent overwhelming the browser
                    if (dataset.length >= 10000) {
                        console.log('Loaded 10,000 rows, stopping to prevent performance issues');
                        hasMore = false;
                    }
                }

                if (dataset.length === 0) {
                    throw new Error('No data found in dataset');
                }

                // Final update
                isLoadingComplete = true;
                loadingDiv.style.display = 'none';
                filterAndRender();

            } catch (error) {
                loadingDiv.style.display = 'none';
                showError(`Failed to load dataset: ${error.message}`);
            } finally {
                loadButton.disabled = false;
                if (isLoadingComplete) {
                    loadingDiv.textContent = 'Loading dataset...';
                }
            }
        }

        // Filter dataset based on current filters
        function filterDataset() {
            filteredDataset = dataset.filter(item => {
                const objects = item.objects || { bbox: [], category: [], score: [] };

                // Filter by confidence threshold
                const validDetections = objects.score.filter(score => score >= confidenceThreshold);

                // If "only detections" is checked, filter out images without detections
                if (showOnlyDetections && validDetections.length === 0) {
                    return false;
                }

                return true;
            });
        }

        // Filter and render
        function filterAndRender() {
            filterDataset();
            currentPage = 0;
            renderStats();
            renderPage();
            renderPagination();
        }

        // Render statistics
        function renderStats() {
            // Calculate stats from FILTERED dataset for consistency
            const totalImages = filteredDataset.length;
            const totalDetections = filteredDataset.reduce((sum, item) => {
                const objects = item.objects || { bbox: [], category: [], score: [] };
                return sum + objects.score.filter(score => score >= confidenceThreshold).length;
            }, 0);

            const imagesWithDetections = filteredDataset.filter(item => {
                const objects = item.objects || { bbox: [], category: [], score: [] };
                return objects.score.some(score => score >= confidenceThreshold);
            }).length;

            const avgDetections = totalImages > 0 ? (totalDetections / totalImages).toFixed(2) : 0;

            statsContainer.innerHTML = `
                <div class="stats">
                    <div class="stats-grid">
                        <div class="stat-item">
                            <div class="stat-value">${filteredDataset.length}</div>
                            <div class="stat-label">Filtered Images</div>
                        </div>
                        <div class="stat-item">
                            <div class="stat-value">${imagesWithDetections}</div>
                            <div class="stat-label">Images with Detections</div>
                        </div>
                        <div class="stat-item">
                            <div class="stat-value">${totalDetections}</div>
                            <div class="stat-label">Total Detections</div>
                        </div>
                        <div class="stat-item">
                            <div class="stat-value">${avgDetections}</div>
                            <div class="stat-label">Avg per Image</div>
                        </div>
                    </div>
                </div>
            `;
        }

        // Render current page
        function renderPage() {
            const start = currentPage * itemsPerPage;
            const end = start + itemsPerPage;
            const pageItems = filteredDataset.slice(start, end);

            imageGrid.innerHTML = pageItems.map(item => {
                const objects = item.objects || { bbox: [], category: [], score: [] };
                const validDetections = objects.score
                    .map((score, idx) => ({ score, idx }))
                    .filter(({ score }) => score >= confidenceThreshold);

                // Extract image URL properly (handle both object with src and direct string)
                const imageUrl = typeof item.image === 'object' ? item.image?.src : item.image;

                return `
                    <div class="image-card">
                        <div class="image-container" id="container-${item.index}">
                            <img src="${imageUrl}" alt="Image ${item.index}" crossorigin="anonymous" onload="drawBoundingBoxes(${item.index})">
                            <canvas id="canvas-${item.index}"></canvas>
                        </div>
                        <div class="image-info">
                            <div class="image-index">Image #${item.index}</div>
                            <div class="detections-count">${validDetections.length} detection(s)</div>
                            <div class="detections-list">
                                ${validDetections.map(({ score, idx }) => {
                                    const confidenceClass = score >= 0.7 ? 'confidence-high' : score >= 0.4 ? 'confidence-medium' : 'confidence-low';
                                    const category = objects.category && objects.category[idx] !== undefined ? objects.category[idx] : 0;
                                    return `
                                        <div class="detection-item">
                                            <span class="${confidenceClass}">${(score * 100).toFixed(1)}%</span>
                                            confidence
                                        </div>
                                    `;
                                }).join('')}
                            </div>
                        </div>
                    </div>
                `;
            }).join('');
        }

        // Draw bounding boxes on canvas
        window.drawBoundingBoxes = function(itemIndex) {
            const item = dataset.find(d => d.index === itemIndex);
            if (!item) return;

            const container = document.getElementById(`container-${itemIndex}`);
            const canvas = document.getElementById(`canvas-${itemIndex}`);
            const img = container.querySelector('img');

            if (!canvas || !img) return;

            // Set canvas size to match image
            canvas.width = img.naturalWidth;
            canvas.height = img.naturalHeight;

            const ctx = canvas.getContext('2d');

            // Clear canvas before drawing
            ctx.clearRect(0, 0, canvas.width, canvas.height);

            const objects = item.objects || { bbox: [], category: [], score: [] };

            // Debug: log drawing info
            const validBoxes = objects.bbox.filter((_, idx) => objects.score[idx] >= confidenceThreshold);
            if (validBoxes.length > 0) {
                console.log(`Drawing ${validBoxes.length} boxes for image #${itemIndex}`);
            }

            // Draw each bounding box
            objects.bbox.forEach((bbox, idx) => {
                const score = objects.score[idx];
                if (score < confidenceThreshold) return;

                const [x, y, width, height] = bbox;

                // Debug: log bbox coordinates
                console.log(`  Box ${idx}: [${x}, ${y}, ${width}, ${height}] score: ${score}`);

                // Choose color based on confidence
                let color;
                if (score >= 0.7) {
                    color = '#27ae60'; // Green
                } else if (score >= 0.4) {
                    color = '#f39c12'; // Orange
                } else {
                    color = '#e74c3c'; // Red
                }

                // Draw rectangle
                ctx.strokeStyle = color;
                ctx.lineWidth = 3;
                ctx.strokeRect(x, y, width, height);

                console.log(`  Drew box at [${x}, ${y}] size [${width}, ${height}] in ${color}`);

                // Draw label background
                const label = `${(score * 100).toFixed(1)}%`;
                ctx.font = 'bold 16px Arial';
                const textWidth = ctx.measureText(label).width;
                const textHeight = 20;

                ctx.fillStyle = color;
                ctx.fillRect(x, y - textHeight - 4, textWidth + 10, textHeight + 4);

                // Draw label text
                ctx.fillStyle = 'white';
                ctx.fillText(label, x + 5, y - 8);
            });
        };

        // Render pagination
        function renderPagination() {
            const totalPages = Math.ceil(filteredDataset.length / itemsPerPage);

            if (totalPages <= 1) {
                paginationDiv.style.display = 'none';
                return;
            }

            paginationDiv.style.display = 'flex';
            paginationDiv.innerHTML = `
                <button id="prev-btn" ${currentPage === 0 ? 'disabled' : ''}>Previous</button>
                <span>Page ${currentPage + 1} of ${totalPages}</span>
                <button id="next-btn" ${currentPage >= totalPages - 1 ? 'disabled' : ''}>Next</button>
            `;

            document.getElementById('prev-btn').addEventListener('click', () => {
                if (currentPage > 0) {
                    currentPage--;
                    renderPage();
                    renderPagination();
                    window.scrollTo({ top: 0, behavior: 'smooth' });
                }
            });

            document.getElementById('next-btn').addEventListener('click', () => {
                if (currentPage < totalPages - 1) {
                    currentPage++;
                    renderPage();
                    renderPagination();
                    window.scrollTo({ top: 0, behavior: 'smooth' });
                }
            });
        }

        // Show error message
        function showError(message) {
            errorDiv.textContent = message;
            errorDiv.style.display = 'block';
        }

        // Auto-load default dataset on page load
        window.addEventListener('load', () => {
            loadDataset();
        });
    </script>
</body>
</html>