File size: 20,948 Bytes
e9ff155
5cf0578
55cae17
e9ff155
 
 
 
 
 
 
 
 
 
 
 
55cae17
e9ff155
 
 
 
 
 
55cae17
e9ff155
 
 
 
 
 
 
 
 
 
 
55cae17
 
e9ff155
 
 
 
55cae17
e9ff155
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55cae17
 
e9ff155
 
 
 
 
 
55cae17
e9ff155
 
 
 
 
 
 
 
 
 
 
 
55cae17
e9ff155
 
 
 
55cae17
e9ff155
 
 
 
 
 
55cae17
e9ff155
 
55cae17
e9ff155
 
 
 
 
55cae17
e9ff155
 
 
 
 
 
55cae17
e9ff155
 
 
 
 
 
 
 
 
 
 
 
 
 
55cae17
e9ff155
55cae17
e9ff155
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55cae17
e9ff155
 
55cae17
e9ff155
 
55cae17
e9ff155
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55cae17
e9ff155
 
 
 
 
 
 
 
 
 
 
 
55cae17
e9ff155
 
55cae17
e9ff155
 
 
 
 
 
55cae17
 
 
e9ff155
 
 
55cae17
 
e9ff155
55cae17
e9ff155
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55cae17
 
e9ff155
 
55cae17
e9ff155
55cae17
 
e9ff155
 
55cae17
e9ff155
fcb9cb0
e9ff155
 
55cae17
e9ff155
55cae17
 
e9ff155
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55cae17
 
e9ff155
 
 
 
55cae17
 
8b5581d
e9ff155
 
 
55cae17
 
e9ff155
 
 
 
55cae17
 
e9ff155
 
 
 
55cae17
 
8b5581d
e9ff155
 
 
 
 
 
 
 
 
 
 
 
55cae17
e9ff155
 
 
 
 
 
 
 
 
 
 
 
 
55cae17
e9ff155
55cae17
e9ff155
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55cae17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9ff155
 
 
 
 
 
 
55cae17
e9ff155
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55cae17
 
 
 
 
 
 
 
 
 
 
 
e9ff155
 
 
 
 
 
 
 
 
 
55cae17
e9ff155
55cae17
e9ff155
 
 
 
 
55cae17
e9ff155
 
 
55cae17
e9ff155
 
55cae17
e9ff155
 
 
 
 
 
 
 
55cae17
e9ff155
 
 
 
 
 
 
 
55cae17
e9ff155
 
 
 
 
 
 
 
55cae17
e9ff155
 
 
 
 
 
 
 
 
55cae17
e9ff155
 
 
 
 
 
 
 
 
 
 
55cae17
 
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
<!DOCTYPE html>
<html>

<head>
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            font-family: system-ui;
            background-color: #f5f5f5;
            min-height: 100vh;
            padding: 1rem;
        }

        .page-title {
            text-align: center;
            padding: 0.5rem 0;
            color: #333;
            margin-bottom: 1.5rem;
            font-size: 1.8rem;
        }

        .main-container {
            max-width: 1200px;
            margin: 0 auto;
        }

        /* Top Section: Image and Controls Side by Side */
        .top-section {
            display: flex;
            gap: 20px;
            margin-bottom: 30px;
        }

        /* Image Section */
        .image-section {
            flex: 0 0 640px;
            display: flex;
            flex-direction: column;
            align-items: center;
            background: #fff;
            padding: 15px;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
        }

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

        #image {
            display: block;
            width: 100%;
            height: auto;
            border: 1px solid #ddd;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
        }

        #canvas {
            position: absolute;
            top: 0;
            left: 0;
            cursor: crosshair;
        }

        .labels-container {
            position: absolute;
            top: 0;
            left: 0;
            pointer-events: none;
            width: 100%;
            height: 100%;
        }

        /* Controls Section */
        .controls-section {
            flex: 1;
            background: #fff;
            padding: 15px;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            display: flex;
            flex-direction: column;
        }

        .coordinates {
            display: flex;
            flex-direction: column;
            flex-grow: 1;
        }

        .format-controls {
            margin-bottom: 20px;
        }

        .format-controls h2 {
            margin-bottom: 10px;
            color: #333;
            font-size: 1.2rem;
        }

        .radio-group {
            margin-bottom: 15px;
            padding: 10px;
            background: #f9f9f9;
            border-radius: 4px;
            border: 1px solid #ddd;
        }

        .radio-option {
            margin-bottom: 8px;
        }

        .radio-option label {
            margin-left: 5px;
            color: #555;
        }

        .format-controls h2:last-of-type {
            margin-top: 20px;
        }

        .format {
            margin-bottom: 15px;
        }

        .format-title {
            font-weight: bold;
            margin-bottom: 5px;
            color: #333;
        }

        .coords {
            font-family: monospace;
            background: #f9f9f9;
            padding: 8px;
            border-radius: 4px;
            border: 1px solid #ddd;
            min-height: 20px;
        }

        .clear-button-container {
            display: flex;
            justify-content: center;
            padding-top: 10px;
        }

        .clear-button {
            background-color: #f44336;
            color: white;
            border: none;
            padding: 8px 16px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            transition: background-color 0.2s;
        }

        .clear-button:hover {
            background-color: #d32f2f;
        }

        .label {
            position: absolute;
            background: rgba(0, 0, 0, 0.7);
            color: #fff;
            padding: 3px 5px;
            border-radius: 3px;
            font-size: 11px;
            pointer-events: none;
            white-space: nowrap;
        }

        /* Bottom Section: Model Explanation Table */
        .bottom-section {
            background: #fff;
            padding: 15px;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            overflow-x: auto;
        }

        .model-explanation table {
            width: 100%;
            border-collapse: collapse;
            background: white;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
            overflow: hidden;
            font-size: 0.9rem;
        }

        .model-explanation th,
        .model-explanation td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }

        .model-explanation th {
            background-color: #f2f2f2;
            position: sticky;
            top: 0;
            z-index: 1;
        }

        /* Responsive Design */
        @media (max-width: 1200px) {
            .top-section {
                flex-direction: column;
            }

            .image-section,
            .controls-section,
            .bottom-section {
                width: 100%;
                max-width: 100%;
                margin: 0 auto;
            }

            .image-section,
            .controls-section {
                flex: none;
            }
        }

        @media (max-height: 900px) {
            body {
                overflow: auto;
            }
        }
    </style>
</head>

<body>
    <h1 class="page-title">Bounding Box Coordinate Tool</h1>

    <div class="main-container">
        <!-- Top Section: Image and Controls -->
        <div class="top-section">
            <!-- Image Section -->
            <div class="image-section">
                <div class="image-container">
                    <img id="image" src="demo-image.jpg"
                        alt="Image created by Flux Pro with prompt 'an image of a living room with plenty of different furniture objects'"
                        width="640">
                    <canvas id="canvas"></canvas>
                    <div id="labels-container" class="labels-container"></div>
                </div>
                <p>Try drawing a box on the image by left clicking and dragging.</p>
                <p id="image-dimensions"></p>
            </div>

            <!-- Controls Section -->
            <div class="controls-section">
                <div class="coordinates">
                    <div class="format-controls">
                        <h2>Display Format</h2>
                        <div class="radio-group">
                            <div class="radio-option">
                                <input type="radio" id="xyxy-radio" name="format" value="xyxy" checked>
                                <label for="xyxy-radio">XYXY (x1, y1, x2, y2)</label>
                            </div>
                            <div class="radio-option">
                                <input type="radio" id="xywh-radio" name="format" value="xywh">
                                <label for="xywh-radio">XYWH (x, y, width, height)</label>
                            </div>
                            <div class="radio-option">
                                <input type="radio" id="normalized-radio" name="format" value="normalized">
                                <label for="normalized-radio">Normalized XYWH (0-1)</label>
                            </div>
                            <div class="radio-option">
                                <input type="radio" id="center-radio" name="format" value="center">
                                <label for="center-radio">Center XYWH (cx, cy, w, h)</label>
                            </div>
                        </div>

                        <h2>Bounding Box Coordinates</h2>
                        <div class="format">
                            <div class="format-title">XYXY (x1, y1, x2, y2) β†’ top left: (x1, y1), bottom right: (x2, y2)
                            </div>
                            <div id="xyxy" class="coords">No box drawn</div>
                        </div>

                        <div class="format">
                            <div class="format-title">XYWH (x, y, width, height) β†’ top left: (x, y), width and height of
                                box</div>
                            <div id="xywh" class="coords">No box drawn</div>
                        </div>

                        <div class="format">
                            <div class="format-title">Normalized XYWH (pixel values in range of 0-1)</div>
                            <div id="normalized" class="coords">No box drawn</div>
                        </div>

                        <div class="format">
                            <div class="format-title">Center XYWH (cx, cy, w, h) β†’ (center x, center y, width, height)
                            </div>
                            <div id="center" class="coords">No box drawn</div>
                        </div>
                    </div>

                    <div class="clear-button-container">
                        <button id="clear-button" class="clear-button">Clear Box</button>
                    </div>
                </div>
            </div>
        </div>

        <!-- Bottom Section: Model Explanation Table -->
        <div class="bottom-section">
            <div class="model-explanation">
                <table>
                    <thead>
                        <tr>
                            <th>Model</th>
                            <th>Summary</th>
                            <th>Bounding Box Format</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td><strong>YOLO</strong></td>
                            <td>Real-time object detection system. Training format uses normalized coordinates for
                                better generalization across different image sizes.</td>
                            <td><strong>Normalized XYWH</strong> (x, y, w, h) in range [0,1]</td>
                        </tr>
                        <tr>
                            <td><strong>Faster R-CNN</strong></td>
                            <td>Region proposal network based detection. Typically uses absolute coordinates during
                                inference, but may use normalized coordinates during training.</td>
                            <td><strong>XYXY</strong> (x1, y1, x2, y2) or normalized coordinates</td>
                        </tr>
                        <tr>
                            <td><strong>SSD</strong></td>
                            <td>Single Shot MultiBox Detector uses normalized coordinates for anchor boxes and
                                predictions to handle multiple scales efficiently.</td>
                            <td><strong>Normalized XYWH</strong> (x, y, w, h) in range [0,1]</td>
                        </tr>
                        <tr>
                            <td><strong>RetinaNet</strong></td>
                            <td>Dense object detector with focal loss. Uses normalized coordinates for anchor boxes and
                                predictions.</td>
                            <td><strong>Normalized XYWH</strong> (x, y, w, h) in range [0,1]</td>
                        </tr>
                        <tr>
                            <td><strong>CornerNet</strong></td>
                            <td>Anchor-free detector that predicts keypoint heatmaps. Uses normalized coordinates for
                                better scale invariance.</td>
                            <td><strong>Normalized Corners</strong> (x1, y1, x2, y2) in range [0,1]</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <script>
        const image = document.getElementById('image');
        const canvas = document.getElementById('canvas');
        const ctx = canvas.getContext('2d');
        const labelsContainer = document.getElementById('labels-container');

        let isDrawing = false;
        let startX = 0;
        let startY = 0;
        let lastCoords = null;
        let labelElements = [];

        function initCanvas() {
            canvas.width = image.width;
            canvas.height = image.height;
            canvas.style.width = image.width + 'px';
            canvas.style.height = image.height + 'px';
            labelsContainer.style.width = image.width + 'px';
            labelsContainer.style.height = image.height + 'px';
            document.getElementById('image-dimensions').textContent = 'Image dimensions: ' + image.width + ' x ' + image.height;
        }

        if (image.complete) {
            initCanvas();
        } else {
            image.onload = initCanvas;
        }

        function clearCanvas() {
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            clearLabels();
        }

        function clearLabels() {
            labelElements.forEach(label => labelsContainer.removeChild(label));
            labelElements = [];
        }

        function createLabel(text, x, y, positionClass) {
            const label = document.createElement('div');
            label.classList.add('label', positionClass);
            label.textContent = text;
            label.style.left = `${x}px`;
            label.style.top = `${y}px`;
            const containerRect = labelsContainer.getBoundingClientRect();
            label.style.visibility = 'hidden';
            labelsContainer.appendChild(label);
            const tempRect = label.getBoundingClientRect();
            label.style.visibility = 'visible';
            if (x + tempRect.width > containerRect.width) {
                label.style.left = `${containerRect.width - tempRect.width - 5}px`;
            }
            if (y + tempRect.height > containerRect.height) {
                label.style.top = `${containerRect.height - tempRect.height - 5}px`;
            }
            labelsContainer.appendChild(label);
            labelElements.push(label);
        }

        function drawDot(x, y) {
            ctx.beginPath();
            ctx.arc(x, y, 4, 0, 2 * Math.PI);
            ctx.fillStyle = 'black';
            ctx.fill();
        }

        // Draw dots at the positions corresponding to the selected format.
        function drawDots(x1, y1, x2, y2) {
            const format = document.querySelector('input[name="format"]:checked').value;
            if (format === 'center') {
                const centerX = x1 + (x2 - x1) / 2;
                const centerY = y1 + (y2 - y1) / 2;
                drawDot(centerX, centerY);
                drawDot(x1, y1);
            } else {
                drawDot(x1, y1);
                drawDot(x2, y2);
            }
        }

        function createLabels(x1, y1, x2, y2) {
            const width = x2 - x1;
            const height = y2 - y1;
            const format = document.querySelector('input[name="format"]:checked').value;
            let labels = [];
            switch (format) {
                case 'xyxy':
                    labels = [
                        { text: `(${Math.round(x1)}, ${Math.round(y1)})`, x: x1, y: y1, position: 'top-left' },
                        { text: `(${Math.round(x2)}, ${Math.round(y2)})`, x: x2, y: y2, position: 'bottom-right' }
                    ];
                    break;
                case 'xywh':
                    labels = [
                        { text: `x: ${Math.round(x1)}, y: ${Math.round(y1)}`, x: x1, y: y1, position: 'top-left' },
                        { text: `w: ${Math.round(width)}, h: ${Math.round(height)}`, x: x1, y: y1 - 15, position: 'top-left' }
                    ];
                    break;
                case 'normalized':
                    const normalizedX = (x1 / canvas.width).toFixed(3);
                    const normalizedY = (y1 / canvas.height).toFixed(3);
                    const normalizedW = (width / canvas.width).toFixed(3);
                    const normalizedH = (height / canvas.height).toFixed(3);
                    labels = [
                        { text: `x: ${normalizedX}, y: ${normalizedY}`, x: x1, y: y1, position: 'top-left' },
                        { text: `w: ${normalizedW}, h: ${normalizedH}`, x: x1, y: y1 - 15, position: 'top-left' }
                    ];
                    break;
                case 'center':
                    const centerX = Math.round(x1 + width / 2);
                    const centerY = Math.round(y1 + height / 2);
                    labels = [
                        { text: `cx: ${centerX}, cy: ${centerY}`, x: centerX, y: centerY, position: 'top-left' },
                        { text: `w: ${Math.round(width)}, h: ${Math.round(height)}`, x: x1, y: y1 - 15, position: 'top-left' }
                    ];
                    break;
            }
            labels.forEach(label => {
                createLabel(label.text, label.x, label.y, label.position);
            });
        }

        function drawBox(x1, y1, x2, y2) {
            clearCanvas();
            ctx.strokeStyle = '#00ff00';
            ctx.lineWidth = 2;
            const width = x2 - x1;
            const height = y2 - y1;
            ctx.strokeRect(x1, y1, width, height);
            drawDots(x1, y1, x2, y2);
            createLabels(x1, y1, x2, y2);
            lastCoords = { x1, y1, x2, y2 };
        }

        function updateCoordinates(x1, y1, x2, y2) {
            if (x1 === null || y1 === null || x2 === null || y2 === null) {
                document.getElementById('xyxy').textContent = 'No box drawn';
                document.getElementById('xywh').textContent = 'No box drawn';
                document.getElementById('normalized').textContent = 'No box drawn';
                document.getElementById('center').textContent = 'No box drawn';
                return;
            }
            const width = x2 - x1;
            const height = y2 - y1;
            document.getElementById('xyxy').textContent =
                `[${Math.round(x1)}, ${Math.round(y1)}, ${Math.round(x2)}, ${Math.round(y2)}]`;
            document.getElementById('xywh').textContent =
                `[${Math.round(x1)}, ${Math.round(y1)}, ${Math.round(width)}, ${Math.round(height)}]`;
            const normalizedX = (x1 / canvas.width).toFixed(3);
            const normalizedY = (y1 / canvas.height).toFixed(3);
            const normalizedW = (width / canvas.width).toFixed(3);
            const normalizedH = (height / canvas.height).toFixed(3);
            document.getElementById('normalized').textContent =
                `[${normalizedX}, ${normalizedY}, ${normalizedW}, ${normalizedH}]`;
            const centerX = x1 + width / 2;
            const centerY = y1 + height / 2;
            document.getElementById('center').textContent =
                `[${Math.round(centerX)}, ${Math.round(centerY)}, ${Math.round(width)}, ${Math.round(height)}]`;
        }

        document.querySelectorAll('input[name="format"]').forEach(radio => {
            radio.addEventListener('change', () => {
                if (lastCoords) {
                    drawBox(lastCoords.x1, lastCoords.y1, lastCoords.x2, lastCoords.y2);
                    updateCoordinates(lastCoords.x1, lastCoords.y1, lastCoords.x2, lastCoords.y2);
                }
            });
        });

        canvas.addEventListener('mousedown', (e) => {
            isDrawing = true;
            const rect = canvas.getBoundingClientRect();
            startX = e.clientX - rect.left;
            startY = e.clientY - rect.top;
            clearCanvas();
            updateCoordinates(null, null, null, null);
        });

        canvas.addEventListener('mousemove', (e) => {
            if (!isDrawing) return;
            const rect = canvas.getBoundingClientRect();
            const currentX = e.clientX - rect.left;
            const currentY = e.clientY - rect.top;
            drawBox(startX, startY, currentX, currentY);
            updateCoordinates(startX, startY, currentX, currentY);
        });

        canvas.addEventListener('mouseup', (e) => {
            if (!isDrawing) return;
            const rect = canvas.getBoundingClientRect();
            const endX = e.clientX - rect.left;
            const endY = e.clientY - rect.top;
            drawBox(startX, startY, endX, endY);
            updateCoordinates(startX, startY, endX, endY);
            isDrawing = false;
        });

        canvas.addEventListener('mouseleave', () => {
            isDrawing = false;
        });

        document.getElementById('clear-button').addEventListener('click', () => {
            clearCanvas();
            lastCoords = null;
            updateCoordinates(null, null, null, null);
        });
    </script>
</body>

</html>