-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleAnnotationTool.html
More file actions
632 lines (548 loc) · 23.7 KB
/
SimpleAnnotationTool.html
File metadata and controls
632 lines (548 loc) · 23.7 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Image Annotation Tool</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f5f5f5;
}
.container {
display: flex;
height: 100vh;
width: 100%;
}
.main-panel {
flex: 3;
padding: 20px;
overflow: auto;
height: calc(100vh - 40px);
}
.sidebar {
flex: 1;
background-color: #f0f0f0;
padding: 20px;
border-left: 1px solid #ddd;
overflow-y: auto;
height: calc(100vh - 40px);
}
.canvas-wrapper {
position: relative;
border: 1px solid #ccc;
margin-bottom: 20px;
overflow: auto;
max-height: 70vh;
min-height: 300px;
background-color: #eee;
}
canvas {
display: block;
}
.btn {
background-color: #4CAF50;
border: none;
color: white;
padding: 8px 16px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 14px;
margin: 4px 2px;
cursor: pointer;
border-radius: 4px;
}
.btn-danger {
background-color: #f44336;
}
.btn-info {
background-color: #2196F3;
}
.btn-warning {
background-color: #ff9800;
}
.rect-item {
background-color: white;
border-left: 4px solid #4CAF50;
padding: 10px;
margin-bottom: 10px;
border-radius: 4px;
cursor: pointer;
}
.rect-item.selected {
background-color: #e7f3fe;
box-shadow: 0 0 5px rgba(0,0,0,0.2);
}
#status {
padding: 10px;
margin-bottom: 10px;
border-radius: 4px;
background-color: #e7f3fe;
border-left: 6px solid #2196F3;
}
.controls {
margin-bottom: 20px;
}
.multi-controls {
margin-top: 10px;
padding: 10px;
background-color: #f9f9f9;
border-radius: 4px;
display: none;
}
</style>
</head>
<body>
<div class="container">
<div class="main-panel">
<h1>Simple Image Annotation Tool</h1>
<div class="controls">
<input type="file" id="imageUpload" accept="image/*">
<button class="btn" id="drawMode">Draw Mode</button>
<button class="btn" id="selectMode">Select Mode</button>
</div>
<div id="status">Upload an image to start</div>
<div class="canvas-wrapper" id="canvasWrapper">
<canvas id="canvas"></canvas>
</div>
</div>
<div class="sidebar">
<h2>Annotations</h2>
<div class="multi-controls" id="multiControls">
<div id="selectionCount">0 rectangles selected</div>
<button class="btn btn-warning" id="duplicateSelected">Duplicate Selected</button>
<button class="btn btn-danger" id="deleteSelected">Delete Selected</button>
<button class="btn" id="deselectAll">Deselect All</button>
</div>
<div id="rectList"></div>
<button class="btn btn-info" id="downloadJSON">Download JSON</button>
</div>
</div>
<script>
// DOM Elements
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const canvasWrapper = document.getElementById('canvasWrapper');
const imageUpload = document.getElementById('imageUpload');
const statusEl = document.getElementById('status');
const rectListEl = document.getElementById('rectList');
const multiControls = document.getElementById('multiControls');
const selectionCount = document.getElementById('selectionCount');
// Buttons
const drawModeBtn = document.getElementById('drawMode');
const selectModeBtn = document.getElementById('selectMode');
const duplicateSelectedBtn = document.getElementById('duplicateSelected');
const deleteSelectedBtn = document.getElementById('deleteSelected');
const deselectAllBtn = document.getElementById('deselectAll');
const downloadJSONBtn = document.getElementById('downloadJSON');
// State variables
let image = null;
let rectangles = [];
let selectedRects = new Set();
let mode = 'draw';
let isDrawing = false;
let isDragging = false;
let startX = 0;
let startY = 0;
let draggedRect = null;
let dragOffsetX = 0;
let dragOffsetY = 0;
// Load image
imageUpload.addEventListener('change', function(e) {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(event) {
const img = new Image();
img.onload = function() {
image = img;
resizeCanvas();
drawAll();
statusEl.textContent = 'Image loaded. Draw rectangles by clicking and dragging.';
};
img.src = event.target.result;
};
reader.readAsDataURL(file);
}
});
// Resize canvas to match image
function resizeCanvas() {
if (image) {
canvas.width = image.width;
canvas.height = image.height;
}
}
// Draw everything
function drawAll() {
if (!image) return;
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw image
ctx.drawImage(image, 0, 0);
// Draw rectangles
rectangles.forEach(rect => {
ctx.strokeStyle = rect.color;
ctx.lineWidth = selectedRects.has(rect) ? 3 : 2;
if (selectedRects.has(rect)) {
ctx.setLineDash([5, 3]);
} else {
ctx.setLineDash([]);
}
ctx.strokeRect(rect.x, rect.y, rect.width, rect.height);
// Draw label
ctx.font = '14px Arial';
ctx.fillStyle = rect.color;
ctx.setLineDash([]);
ctx.fillText(rect.name, rect.x, rect.y - 5);
});
ctx.setLineDash([]);
}
// Generate random color
function getRandomColor() {
const colors = [
'#FF0000', '#00FF00', '#0000FF', '#FFFF00',
'#FF00FF', '#00FFFF', '#FF8000', '#8000FF'
];
return colors[Math.floor(Math.random() * colors.length)];
}
// Check if point is inside rectangle
function isPointInRect(x, y, rect) {
return (
x >= rect.x &&
x <= rect.x + rect.width &&
y >= rect.y &&
y <= rect.y + rect.height
);
}
// Update selection count
function updateSelectionUI() {
if (selectedRects.size > 0) {
multiControls.style.display = 'block';
selectionCount.textContent = `${selectedRects.size} rectangle${selectedRects.size !== 1 ? 's' : ''} selected`;
} else {
multiControls.style.display = 'none';
}
drawAll();
updateRectList();
}
// Update rectangle list in sidebar
function updateRectList() {
rectListEl.innerHTML = '';
if (rectangles.length === 0) {
rectListEl.innerHTML = '<div>No rectangles yet</div>';
return;
}
rectangles.forEach((rect, index) => {
const rectItem = document.createElement('div');
rectItem.className = 'rect-item';
if (selectedRects.has(rect)) {
rectItem.classList.add('selected');
}
rectItem.style.borderLeftColor = rect.color;
rectItem.innerHTML = `
<div><strong>${rect.name}</strong></div>
<div>Position: (${Math.round(rect.x)}, ${Math.round(rect.y)})</div>
<div>Size: ${Math.round(rect.width)} × ${Math.round(rect.height)}</div>
<button class="btn btn-warning duplicate-btn">Duplicate</button>
<button class="btn btn-danger delete-btn">Delete</button>
`;
// Rectangle selection
rectItem.addEventListener('click', function(e) {
if (e.target.tagName !== 'BUTTON') {
if (e.ctrlKey || e.metaKey) {
// Toggle selection
if (selectedRects.has(rect)) {
selectedRects.delete(rect);
} else {
selectedRects.add(rect);
}
} else {
// Single select
selectedRects.clear();
selectedRects.add(rect);
}
updateSelectionUI();
}
});
// Duplicate button
const duplicateBtn = rectItem.querySelector('.duplicate-btn');
duplicateBtn.addEventListener('click', function() {
const newRect = { ...rect };
newRect.name = `rect_${Math.floor(1000 + Math.random() * 9000)}`;
newRect.color = getRandomColor();
newRect.x += 20;
newRect.y += 20;
// Ensure within bounds
if (image) {
if (newRect.x + newRect.width > image.width) {
newRect.x = Math.max(0, image.width - newRect.width);
}
if (newRect.y + newRect.height > image.height) {
newRect.y = Math.max(0, image.height - newRect.height);
}
}
rectangles.push(newRect);
selectedRects.clear();
selectedRects.add(newRect);
// Switch to select mode
mode = 'select';
drawModeBtn.classList.remove('active');
selectModeBtn.classList.add('active');
updateSelectionUI();
statusEl.textContent = `Duplicated as ${newRect.name}`;
});
// Delete button
const deleteBtn = rectItem.querySelector('.delete-btn');
deleteBtn.addEventListener('click', function() {
rectangles.splice(index, 1);
if (selectedRects.has(rect)) {
selectedRects.delete(rect);
}
updateSelectionUI();
statusEl.textContent = `Deleted ${rect.name}`;
});
rectListEl.appendChild(rectItem);
});
}
// Mode control
drawModeBtn.addEventListener('click', function() {
mode = 'draw';
drawModeBtn.classList.add('active');
selectModeBtn.classList.remove('active');
canvas.style.cursor = 'crosshair';
statusEl.textContent = 'Draw Mode: Click and drag to create rectangles';
});
selectModeBtn.addEventListener('click', function() {
mode = 'select';
selectModeBtn.classList.add('active');
drawModeBtn.classList.remove('active');
canvas.style.cursor = 'default';
statusEl.textContent = 'Select Mode: Click to select, drag to move rectangles';
});
// Duplicate selected rectangles
duplicateSelectedBtn.addEventListener('click', function() {
if (selectedRects.size === 0) return;
const selectedArray = Array.from(selectedRects);
const newRects = [];
selectedArray.forEach(rect => {
const newRect = { ...rect };
newRect.name = `rect_${Math.floor(1000 + Math.random() * 9000)}`;
newRect.color = getRandomColor();
newRect.x += 20;
newRect.y += 20;
// Ensure within bounds
if (image) {
if (newRect.x + newRect.width > image.width) {
newRect.x = Math.max(0, image.width - newRect.width);
}
if (newRect.y + newRect.height > image.height) {
newRect.y = Math.max(0, image.height - newRect.height);
}
}
newRects.push(newRect);
});
// Add new rectangles to the array
rectangles = rectangles.concat(newRects);
// Select the new rectangles
selectedRects.clear();
newRects.forEach(rect => selectedRects.add(rect));
updateSelectionUI();
statusEl.textContent = `Duplicated ${newRects.length} rectangle${newRects.length !== 1 ? 's' : ''}`;
});
// Delete selected rectangles
deleteSelectedBtn.addEventListener('click', function() {
if (selectedRects.size === 0) return;
rectangles = rectangles.filter(rect => !selectedRects.has(rect));
selectedRects.clear();
updateSelectionUI();
statusEl.textContent = 'Deleted selected rectangles';
});
// Deselect all
deselectAllBtn.addEventListener('click', function() {
selectedRects.clear();
updateSelectionUI();
statusEl.textContent = 'Deselected all rectangles';
});
// Download JSON
downloadJSONBtn.addEventListener('click', function() {
if (rectangles.length === 0) {
alert('No rectangles to download');
return;
}
// Prepare JSON data
const jsonData = rectangles.map(rect => ({
name: rect.name,
coordinates: {
x1: Math.round(rect.x),
y1: Math.round(rect.y),
x2: Math.round(rect.x + rect.width),
y2: Math.round(rect.y + rect.height)
}
}));
try {
const dataStr = "data:text/json;charset=utf-8," +
encodeURIComponent(JSON.stringify(jsonData, null, 2));
const downloadAnchor = document.createElement('a');
downloadAnchor.setAttribute("href", dataStr);
downloadAnchor.setAttribute("download", "annotations.json");
document.body.appendChild(downloadAnchor);
downloadAnchor.click();
downloadAnchor.remove();
statusEl.textContent = `Downloaded ${rectangles.length} annotations as JSON`;
} catch (error) {
console.error("Download error:", error);
statusEl.textContent = "Error downloading JSON";
}
});
// Canvas event handlers
canvas.addEventListener('mousedown', function(e) {
if (!image) return;
const rect = canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
if (mode === 'draw') {
// Start drawing a rectangle
isDrawing = true;
startX = x;
startY = y;
statusEl.textContent = 'Release to complete the rectangle';
} else if (mode === 'select') {
// Check if clicked on a rectangle
let clickedRect = null;
// Check in reverse (top-most first)
for (let i = rectangles.length - 1; i >= 0; i--) {
if (isPointInRect(x, y, rectangles[i])) {
clickedRect = rectangles[i];
break;
}
}
if (clickedRect) {
// Handle selection
if (e.ctrlKey || e.metaKey) {
// Toggle selection
if (selectedRects.has(clickedRect)) {
selectedRects.delete(clickedRect);
} else {
selectedRects.add(clickedRect);
}
} else {
// Single select unless clicking already selected rect
if (!(selectedRects.size > 1 && selectedRects.has(clickedRect))) {
selectedRects.clear();
}
selectedRects.add(clickedRect);
}
// Set up dragging
isDragging = true;
draggedRect = clickedRect;
dragOffsetX = x - clickedRect.x;
dragOffsetY = y - clickedRect.y;
canvas.style.cursor = 'move';
} else if (!e.ctrlKey && !e.metaKey) {
// Clicked empty space - clear selection
selectedRects.clear();
}
updateSelectionUI();
}
});
canvas.addEventListener('mousemove', function(e) {
if (!image) return;
const rect = canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
if (mode === 'draw' && isDrawing) {
// Drawing temporary rectangle
const width = x - startX;
const height = y - startY;
drawAll();
// Draw current rectangle
ctx.strokeStyle = '#00FFFF';
ctx.lineWidth = 2;
ctx.strokeRect(
width >= 0 ? startX : x,
height >= 0 ? startY : y,
Math.abs(width),
Math.abs(height)
);
} else if (mode === 'select' && isDragging && draggedRect) {
// Move selected rectangles
const deltaX = (x - dragOffsetX) - draggedRect.x;
const deltaY = (y - dragOffsetY) - draggedRect.y;
selectedRects.forEach(rect => {
rect.x += deltaX;
rect.y += deltaY;
// Keep in bounds
if (rect.x < 0) rect.x = 0;
if (rect.y < 0) rect.y = 0;
if (image) {
if (rect.x + rect.width > image.width) {
rect.x = image.width - rect.width;
}
if (rect.y + rect.height > image.height) {
rect.y = image.height - rect.height;
}
}
});
// Update reference rect position for next move
draggedRect.x = x - dragOffsetX;
draggedRect.y = y - dragOffsetY;
// Keep in bounds
if (draggedRect.x < 0) draggedRect.x = 0;
if (draggedRect.y < 0) draggedRect.y = 0;
if (image) {
if (draggedRect.x + draggedRect.width > image.width) {
draggedRect.x = image.width - draggedRect.width;
}
if (draggedRect.y + draggedRect.height > image.height) {
draggedRect.y = image.height - draggedRect.height;
}
}
drawAll();
}
});
canvas.addEventListener('mouseup', function(e) {
if (!image) return;
const rect = canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
if (mode === 'draw' && isDrawing) {
// Finish rectangle
const width = x - startX;
const height = y - startY;
// Only add if big enough
if (Math.abs(width) > 5 && Math.abs(height) > 5) {
const newRect = {
x: width >= 0 ? startX : x,
y: height >= 0 ? startY : y,
width: Math.abs(width),
height: Math.abs(height),
name: `rect_${rectangles.length + 1}`,
color: getRandomColor()
};
rectangles.push(newRect);
selectedRects.clear();
selectedRects.add(newRect);
updateSelectionUI();
statusEl.textContent = `Added rectangle ${newRect.name}`;
} else {
statusEl.textContent = 'Rectangle too small, try again';
}
isDrawing = false;
} else if (mode === 'select' && isDragging) {
// Finish moving
isDragging = false;
draggedRect = null;
canvas.style.cursor = 'default';
updateRectList();
statusEl.textContent = `Moved ${selectedRects.size} rectangle${selectedRects.size !== 1 ? 's' : ''}`;
}
});
// Set initial state
drawModeBtn.classList.add('active');
updateRectList();
</script>
</body>
</html>