| <!DOCTYPE html>
|
| <html lang="en">
|
|
|
| <head>
|
| <meta charset="UTF-8">
|
| <meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| <title>TPC-268 Annotator</title>
|
| <style>
|
| :root {
|
| --bg-color: #1e1e1e;
|
| --panel-bg: #252526;
|
| --border-color: #3e3e42;
|
| --text-main: #cccccc;
|
| --text-muted: #858585;
|
| --accent-color: #007acc;
|
| --accent-hover: #005f9e;
|
| --danger-color: #d16969;
|
| --box-color: #f44336;
|
| --point-color: #ffeb3b;
|
| }
|
|
|
| * {
|
| margin: 0;
|
| padding: 0;
|
| box-sizing: border-box;
|
| }
|
|
|
| body {
|
| font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| background-color: var(--bg-color);
|
| color: var(--text-main);
|
| height: 100vh;
|
| display: flex;
|
| flex-direction: column;
|
| overflow: hidden;
|
| }
|
|
|
|
|
| .navbar {
|
| height: 50px;
|
| background-color: var(--panel-bg);
|
| border-bottom: 1px solid var(--border-color);
|
| display: flex;
|
| justify-content: space-between;
|
| align-items: center;
|
| padding: 0 20px;
|
| }
|
|
|
| .nav-brand {
|
| font-size: 1.2rem;
|
| font-weight: 600;
|
| color: #fff;
|
| }
|
|
|
| .nav-controls {
|
| display: flex;
|
| gap: 10px;
|
| align-items: center;
|
| }
|
|
|
| .btn {
|
| background-color: var(--border-color);
|
| color: var(--text-main);
|
| border: none;
|
| padding: 6px 12px;
|
| border-radius: 4px;
|
| cursor: pointer;
|
| font-size: 0.85rem;
|
| transition: background 0.2s;
|
| display: flex;
|
| align-items: center;
|
| gap: 5px;
|
| }
|
|
|
| .btn:hover {
|
| background-color: #4a4a4d;
|
| }
|
|
|
| .btn-primary {
|
| background-color: var(--accent-color);
|
| color: #fff;
|
| }
|
|
|
| .btn-primary:hover {
|
| background-color: var(--accent-hover);
|
| }
|
|
|
|
|
| .workspace {
|
| display: flex;
|
| flex: 1;
|
| height: calc(100vh - 50px);
|
| }
|
|
|
|
|
| .toolbar {
|
| width: 200px;
|
| background-color: var(--panel-bg);
|
| border-right: 1px solid var(--border-color);
|
| padding: 15px;
|
| display: flex;
|
| flex-direction: column;
|
| gap: 20px;
|
| }
|
|
|
| .tool-group-title {
|
| font-size: 0.75rem;
|
| text-transform: uppercase;
|
| color: var(--text-muted);
|
| margin-bottom: 8px;
|
| letter-spacing: 1px;
|
| }
|
|
|
| .tool-btn {
|
| width: 100%;
|
| padding: 8px 10px;
|
| background: transparent;
|
| border: 1px solid transparent;
|
| color: var(--text-main);
|
| text-align: left;
|
| cursor: pointer;
|
| border-radius: 4px;
|
| margin-bottom: 5px;
|
| transition: all 0.2s;
|
| }
|
|
|
| .tool-btn:hover {
|
| background-color: rgba(255, 255, 255, 0.05);
|
| }
|
|
|
| .tool-btn.active {
|
| background-color: rgba(0, 122, 204, 0.2);
|
| border-color: var(--accent-color);
|
| color: #fff;
|
| }
|
|
|
| .stats-box {
|
| background-color: rgba(0, 0, 0, 0.2);
|
| padding: 10px;
|
| border-radius: 4px;
|
| font-size: 0.85rem;
|
| }
|
|
|
| .stats-row {
|
| display: flex;
|
| justify-content: space-between;
|
| margin-bottom: 5px;
|
| }
|
|
|
|
|
| .canvas-container {
|
| flex: 1;
|
| position: relative;
|
| background-color: #1a1a1a;
|
| display: flex;
|
| justify-content: center;
|
| align-items: center;
|
| overflow: hidden;
|
| background-image: linear-gradient(45deg, #222 25%, transparent 25%, transparent 75%, #222 75%, #222),
|
| linear-gradient(45deg, #222 25%, transparent 25%, transparent 75%, #222 75%, #222);
|
| background-size: 20px 20px;
|
| background-position: 0 0, 10px 10px;
|
| }
|
|
|
| canvas {
|
| box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
|
| cursor: crosshair;
|
| max-width: 95%;
|
| max-height: 95%;
|
| object-fit: contain;
|
| }
|
|
|
|
|
| .file-sidebar {
|
| width: 250px;
|
| background-color: var(--panel-bg);
|
| border-left: 1px solid var(--border-color);
|
| display: flex;
|
| flex-direction: column;
|
| }
|
|
|
| .file-header {
|
| padding: 15px;
|
| border-bottom: 1px solid var(--border-color);
|
| font-size: 0.85rem;
|
| color: var(--text-muted);
|
| }
|
|
|
| .file-list {
|
| flex: 1;
|
| overflow-y: auto;
|
| padding: 10px;
|
| }
|
|
|
| .file-item {
|
| padding: 8px 10px;
|
| cursor: pointer;
|
| font-size: 0.85rem;
|
| border-radius: 4px;
|
| margin-bottom: 2px;
|
| white-space: nowrap;
|
| overflow: hidden;
|
| text-overflow: ellipsis;
|
| }
|
|
|
| .file-item:hover {
|
| background-color: rgba(255, 255, 255, 0.05);
|
| }
|
|
|
| .file-item.active {
|
| background-color: var(--accent-color);
|
| color: #fff;
|
| }
|
|
|
|
|
| input[type="file"] {
|
| display: none;
|
| }
|
|
|
|
|
| #loadingOverlay {
|
| position: absolute;
|
| top: 0;
|
| left: 0;
|
| right: 0;
|
| bottom: 0;
|
| background: rgba(0, 0, 0, 0.7);
|
| color: #fff;
|
| display: flex;
|
| justify-content: center;
|
| align-items: center;
|
| z-index: 100;
|
| font-size: 1.2rem;
|
| display: none;
|
| }
|
| </style>
|
| </head>
|
|
|
| <body>
|
|
|
| <div class="navbar">
|
| <div class="nav-brand">TPC-268 Annotator</div>
|
| <div class="nav-controls">
|
| <span id="imgCounter" style="color: var(--text-muted); font-size: 0.85rem; margin-right: 15px;">0 / 0</span>
|
| <button class="btn" onclick="document.getElementById('imgUpload').click()">📁 Load Images</button>
|
| <input type="file" id="imgUpload" multiple accept="image/*" onchange="handleImageUpload(event)">
|
|
|
| <button class="btn" onclick="document.getElementById('jsonUpload').click()">📄 Load JSON</button>
|
| <input type="file" id="jsonUpload" accept=".json" onchange="handleJsonUpload(event)">
|
|
|
| <button class="btn btn-primary" onclick="exportAnnotations()">💾 Export JSON</button>
|
| </div>
|
| </div>
|
|
|
| <div class="workspace">
|
|
|
| <div class="toolbar">
|
| <div>
|
| <div class="tool-group-title">Modes</div>
|
| <button class="tool-btn active" id="modePoint" onclick="setMode('point')">⊙ Add Point
|
| (Instance)</button>
|
| <button class="tool-btn" id="modeBox" onclick="setMode('box')">□ Draw Box (Exemplar)</button>
|
| <button class="tool-btn" id="modeEdit" onclick="setMode('edit')">↖ Edit / Delete</button>
|
| </div>
|
|
|
| <div>
|
| <div class="tool-group-title">Statistics</div>
|
| <div class="stats-box">
|
| <div class="stats-row"><span>Points:</span> <strong id="statPoints"
|
| style="color: var(--point-color)">0</strong></div>
|
| <div class="stats-row"><span>Boxes:</span> <strong id="statBoxes"
|
| style="color: var(--box-color)">0</strong></div>
|
| </div>
|
| </div>
|
|
|
| <div style="margin-top: auto; font-size: 0.75rem; color: var(--text-muted); line-height: 1.5;">
|
| <strong>Shortcuts:</strong><br>
|
| [W] / [S] : Prev / Next Image<br>
|
| [1] : Point Mode<br>
|
| [2] : Box Mode<br>
|
| [3] : Edit Mode<br>
|
| [Right-Click] : Delete (in Edit Mode)
|
| </div>
|
| </div>
|
|
|
| <div class="canvas-container" id="canvasContainer">
|
| <div id="loadingOverlay">Loading...</div>
|
| <canvas id="mainCanvas"></canvas>
|
| </div>
|
|
|
| <div class="file-sidebar">
|
| <div class="file-header">IMAGE LIST</div>
|
| <div class="file-list" id="fileList">
|
| <div style="color: var(--text-muted); font-size: 0.8rem; text-align: center; margin-top: 20px;">
|
| Please load images to start.
|
| </div>
|
| </div>
|
| </div>
|
|
|
| </div>
|
|
|
| <script>
|
|
|
| let annotations = {};
|
| let uploadedImages = {};
|
| let imageNames = [];
|
| let currentIndex = -1;
|
|
|
| let currentMode = 'point';
|
| let isDrawingBox = false;
|
| let startX = 0, startY = 0;
|
| let tempBox = null;
|
|
|
| let isDraggingPoint = false;
|
| let draggingPointIdx = -1;
|
|
|
|
|
| const canvas = document.getElementById('mainCanvas');
|
| const ctx = canvas.getContext('2d');
|
| let currentImageObj = null;
|
|
|
|
|
|
|
| function setMode(mode) {
|
| currentMode = mode;
|
| document.getElementById('modePoint').classList.remove('active');
|
| document.getElementById('modeBox').classList.remove('active');
|
| document.getElementById('modeEdit').classList.remove('active');
|
|
|
| if (mode === 'point') document.getElementById('modePoint').classList.add('active');
|
| if (mode === 'box') document.getElementById('modeBox').classList.add('active');
|
| if (mode === 'edit') document.getElementById('modeEdit').classList.add('active');
|
|
|
| canvas.style.cursor = mode === 'edit' ? 'default' : 'crosshair';
|
| }
|
|
|
| async function handleImageUpload(e) {
|
| const files = Array.from(e.target.files);
|
| if (files.length === 0) return;
|
|
|
| showLoading(`Loading ${files.length} images...`);
|
|
|
| for (let file of files) {
|
| const dataUrl = await readFileAsDataURL(file);
|
| uploadedImages[file.name] = dataUrl;
|
| if (!annotations[file.name]) {
|
| annotations[file.name] = { box_examples_coordinates: [], points: [] };
|
| }
|
| if (!imageNames.includes(file.name)) {
|
| imageNames.push(file.name);
|
| }
|
| }
|
|
|
| updateFileList();
|
| if (currentIndex === -1 && imageNames.length > 0) {
|
| loadImage(0);
|
| }
|
| hideLoading();
|
| e.target.value = '';
|
| }
|
|
|
| function handleJsonUpload(e) {
|
| const file = e.target.files[0];
|
| if (!file) return;
|
|
|
| const reader = new FileReader();
|
| reader.onload = function (evt) {
|
| try {
|
| const parsed = JSON.parse(evt.target.result);
|
|
|
| for (let key in parsed) {
|
| annotations[key] = parsed[key];
|
|
|
| if (!imageNames.includes(key) && !uploadedImages[key]) {
|
| imageNames.push(key);
|
| uploadedImages[key] = null;
|
| }
|
| }
|
| updateFileList();
|
| if (currentIndex !== -1) renderCanvas();
|
| alert("Annotations loaded successfully!");
|
| } catch (err) {
|
| alert("Invalid JSON format.");
|
| }
|
| };
|
| reader.readAsText(file);
|
| e.target.value = '';
|
| }
|
|
|
| function exportAnnotations() {
|
|
|
| const cleanAnnotations = {};
|
| for (let key in annotations) {
|
| const hasPoints = annotations[key].points && annotations[key].points.length > 0;
|
| const hasBoxes = annotations[key].box_examples_coordinates && annotations[key].box_examples_coordinates.length > 0;
|
| if (hasPoints || hasBoxes) {
|
| cleanAnnotations[key] = annotations[key];
|
| }
|
| }
|
|
|
| const dataStr = JSON.stringify(cleanAnnotations, null, 4);
|
| const blob = new Blob([dataStr], { type: 'application/json' });
|
| const url = URL.createObjectURL(blob);
|
| const a = document.createElement('a');
|
| a.href = url;
|
| a.download = 'tpc268_annotations.json';
|
| a.click();
|
| URL.revokeObjectURL(url);
|
| }
|
|
|
| function loadImage(index) {
|
| if (index < 0 || index >= imageNames.length) return;
|
| currentIndex = index;
|
|
|
| const filename = imageNames[index];
|
| const imgData = uploadedImages[filename];
|
|
|
| updateFileListUI();
|
| document.getElementById('imgCounter').innerText = `${index + 1} / ${imageNames.length}`;
|
|
|
| if (!imgData) {
|
|
|
| ctx.clearRect(0, 0, canvas.width, canvas.height);
|
| currentImageObj = null;
|
| updateStats();
|
| return;
|
| }
|
|
|
| const img = new Image();
|
| img.onload = () => {
|
| currentImageObj = img;
|
|
|
| const container = document.getElementById('canvasContainer');
|
| const ratio = Math.min((container.clientWidth - 40) / img.width, (container.clientHeight - 40) / img.height, 1);
|
|
|
| canvas.width = img.width * ratio;
|
| canvas.height = img.height * ratio;
|
|
|
|
|
| if (!annotations[filename]) {
|
| annotations[filename] = { box_examples_coordinates: [], points: [] };
|
| }
|
|
|
| renderCanvas();
|
| };
|
| img.src = imgData;
|
| }
|
|
|
| function renderCanvas() {
|
| if (!currentImageObj) return;
|
|
|
| ctx.clearRect(0, 0, canvas.width, canvas.height);
|
| ctx.drawImage(currentImageObj, 0, 0, canvas.width, canvas.height);
|
|
|
| const filename = imageNames[currentIndex];
|
| const anno = annotations[filename];
|
| if (!anno) return;
|
|
|
| const scaleX = canvas.width / currentImageObj.width;
|
| const scaleY = canvas.height / currentImageObj.height;
|
|
|
|
|
| if (anno.box_examples_coordinates) {
|
| ctx.strokeStyle = '#f44336';
|
| ctx.lineWidth = 2;
|
| anno.box_examples_coordinates.forEach(box => {
|
| ctx.beginPath();
|
| ctx.moveTo(box[0][0] * scaleX, box[0][1] * scaleY);
|
| ctx.lineTo(box[1][0] * scaleX, box[1][1] * scaleY);
|
| ctx.lineTo(box[2][0] * scaleX, box[2][1] * scaleY);
|
| ctx.lineTo(box[3][0] * scaleX, box[3][1] * scaleY);
|
| ctx.closePath();
|
| ctx.stroke();
|
| });
|
| }
|
|
|
|
|
| if (anno.points) {
|
| anno.points.forEach((pt, idx) => {
|
| const cx = pt[0] * scaleX;
|
| const cy = pt[1] * scaleY;
|
| ctx.beginPath();
|
| ctx.arc(cx, cy, 3, 0, 2 * Math.PI);
|
| ctx.fillStyle = (isDraggingPoint && draggingPointIdx === idx) ? '#ffffff' : '#ffeb3b';
|
| ctx.fill();
|
| ctx.strokeStyle = '#000';
|
| ctx.lineWidth = 1;
|
| ctx.stroke();
|
| });
|
| }
|
|
|
|
|
| if (isDrawingBox && tempBox) {
|
| ctx.strokeStyle = '#4CAF50';
|
| ctx.setLineDash([5, 5]);
|
| ctx.lineWidth = 2;
|
| ctx.strokeRect(tempBox.x, tempBox.y, tempBox.w, tempBox.h);
|
| ctx.setLineDash([]);
|
| }
|
|
|
| updateStats();
|
| }
|
|
|
|
|
|
|
| function getMousePos(evt) {
|
| const rect = canvas.getBoundingClientRect();
|
| return {
|
| x: evt.clientX - rect.left,
|
| y: evt.clientY - rect.top
|
| };
|
| }
|
|
|
| function getOriginalCoords(canvasX, canvasY) {
|
| const scaleX = currentImageObj.width / canvas.width;
|
| const scaleY = currentImageObj.height / canvas.height;
|
| return {
|
| x: Math.round(canvasX * scaleX),
|
| y: Math.round(canvasY * scaleY)
|
| };
|
| }
|
|
|
| canvas.addEventListener('mousedown', (e) => {
|
| if (!currentImageObj) return;
|
| const pos = getMousePos(e);
|
| const orig = getOriginalCoords(pos.x, pos.y);
|
| const filename = imageNames[currentIndex];
|
| const anno = annotations[filename];
|
|
|
|
|
| if (e.button === 2 && currentMode === 'edit') {
|
|
|
| if (anno.points) {
|
| for (let i = anno.points.length - 1; i >= 0; i--) {
|
| const pt = anno.points[i];
|
| const dist = Math.hypot(pt[0] - orig.x, pt[1] - orig.y);
|
| if (dist < 10) {
|
| anno.points.splice(i, 1);
|
| renderCanvas();
|
| return;
|
| }
|
| }
|
| }
|
|
|
| if (anno.box_examples_coordinates) {
|
| for (let i = anno.box_examples_coordinates.length - 1; i >= 0; i--) {
|
| const box = anno.box_examples_coordinates[i];
|
| const minX = Math.min(box[0][0], box[2][0]);
|
| const maxX = Math.max(box[0][0], box[2][0]);
|
| const minY = Math.min(box[0][1], box[2][1]);
|
| const maxY = Math.max(box[0][1], box[2][1]);
|
| if (orig.x >= minX && orig.x <= maxX && orig.y >= minY && orig.y <= maxY) {
|
| anno.box_examples_coordinates.splice(i, 1);
|
| renderCanvas();
|
| return;
|
| }
|
| }
|
| }
|
| return;
|
| }
|
|
|
|
|
| if (e.button === 0) {
|
| if (currentMode === 'point') {
|
| if (!anno.points) anno.points = [];
|
| anno.points.push([orig.x, orig.y]);
|
| renderCanvas();
|
| }
|
| else if (currentMode === 'box') {
|
| isDrawingBox = true;
|
| startX = pos.x;
|
| startY = pos.y;
|
| }
|
| else if (currentMode === 'edit') {
|
|
|
| if (anno.points) {
|
| for (let i = 0; i < anno.points.length; i++) {
|
| const pt = anno.points[i];
|
| const dist = Math.hypot(pt[0] - orig.x, pt[1] - orig.y);
|
| if (dist < 10) {
|
| isDraggingPoint = true;
|
| draggingPointIdx = i;
|
| return;
|
| }
|
| }
|
| }
|
| }
|
| }
|
| });
|
|
|
| canvas.addEventListener('mousemove', (e) => {
|
| if (!currentImageObj) return;
|
| const pos = getMousePos(e);
|
|
|
| if (isDrawingBox) {
|
| tempBox = {
|
| x: Math.min(startX, pos.x),
|
| y: Math.min(startY, pos.y),
|
| w: Math.abs(pos.x - startX),
|
| h: Math.abs(pos.y - startY)
|
| };
|
| renderCanvas();
|
| }
|
|
|
| if (isDraggingPoint) {
|
| const orig = getOriginalCoords(pos.x, pos.y);
|
| annotations[imageNames[currentIndex]].points[draggingPointIdx] = [orig.x, orig.y];
|
| renderCanvas();
|
| }
|
|
|
|
|
| if (currentMode === 'edit' && !isDraggingPoint) {
|
| const orig = getOriginalCoords(pos.x, pos.y);
|
| let hover = false;
|
| const pts = annotations[imageNames[currentIndex]].points || [];
|
| for (let pt of pts) {
|
| if (Math.hypot(pt[0] - orig.x, pt[1] - orig.y) < 10) {
|
| hover = true; break;
|
| }
|
| }
|
| canvas.style.cursor = hover ? 'move' : 'default';
|
| }
|
| });
|
|
|
| canvas.addEventListener('mouseup', (e) => {
|
| if (e.button !== 0) return;
|
|
|
| if (isDrawingBox && tempBox) {
|
| if (tempBox.w > 5 && tempBox.h > 5) {
|
| const tl = getOriginalCoords(tempBox.x, tempBox.y);
|
| const br = getOriginalCoords(tempBox.x + tempBox.w, tempBox.y + tempBox.h);
|
|
|
| const filename = imageNames[currentIndex];
|
| if (!annotations[filename].box_examples_coordinates) {
|
| annotations[filename].box_examples_coordinates = [];
|
| }
|
|
|
|
|
| annotations[filename].box_examples_coordinates.push([
|
| [tl.x, tl.y],
|
| [br.x, tl.y],
|
| [br.x, br.y],
|
| [tl.x, br.y]
|
| ]);
|
| }
|
| isDrawingBox = false;
|
| tempBox = null;
|
| renderCanvas();
|
| }
|
|
|
| if (isDraggingPoint) {
|
| isDraggingPoint = false;
|
| draggingPointIdx = -1;
|
| renderCanvas();
|
| }
|
| });
|
|
|
|
|
| canvas.addEventListener('contextmenu', e => e.preventDefault());
|
|
|
|
|
| document.addEventListener('keydown', (e) => {
|
| if (e.key.toLowerCase() === 'w') { loadImage(currentIndex - 1); }
|
| if (e.key.toLowerCase() === 's') { loadImage(currentIndex + 1); }
|
| if (e.key === '1') setMode('point');
|
| if (e.key === '2') setMode('box');
|
| if (e.key === '3') setMode('edit');
|
| });
|
|
|
|
|
|
|
| function updateFileList() {
|
| const listDiv = document.getElementById('fileList');
|
| listDiv.innerHTML = '';
|
| imageNames.forEach((name, idx) => {
|
| const div = document.createElement('div');
|
| div.className = 'file-item';
|
| div.innerText = name;
|
| div.onclick = () => loadImage(idx);
|
|
|
| if (!uploadedImages[name]) div.style.color = 'var(--danger-color)';
|
| listDiv.appendChild(div);
|
| });
|
| updateFileListUI();
|
| }
|
|
|
| function updateFileListUI() {
|
| const items = document.querySelectorAll('.file-item');
|
| items.forEach((item, idx) => {
|
| if (idx === currentIndex) item.classList.add('active');
|
| else item.classList.remove('active');
|
|
|
| if (idx === currentIndex) item.scrollIntoView({ block: 'nearest' });
|
| });
|
| }
|
|
|
| function updateStats() {
|
| if (currentIndex === -1) return;
|
| const anno = annotations[imageNames[currentIndex]];
|
| const pts = anno?.points?.length || 0;
|
| const boxes = anno?.box_examples_coordinates?.length || 0;
|
| document.getElementById('statPoints').innerText = pts;
|
| document.getElementById('statBoxes').innerText = boxes;
|
| }
|
|
|
| function readFileAsDataURL(file) {
|
| return new Promise((resolve) => {
|
| const reader = new FileReader();
|
| reader.onload = e => resolve(e.target.result);
|
| reader.readAsDataURL(file);
|
| });
|
| }
|
|
|
| function showLoading(msg) {
|
| const loader = document.getElementById('loadingOverlay');
|
| loader.innerText = msg;
|
| loader.style.display = 'flex';
|
| }
|
| function hideLoading() {
|
| document.getElementById('loadingOverlay').style.display = 'none';
|
| }
|
|
|
|
|
| window.addEventListener('resize', () => {
|
| if (currentIndex !== -1) loadImage(currentIndex);
|
| });
|
|
|
| </script>
|
| </body>
|
|
|
| </html> |