File size: 5,599 Bytes
4c8e01e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// JavaScript específico para el visualizador de datasets

let currentSession = "";

function loadSession() {
    const select = document.getElementById('sessionSelect');
    const selectedSession = select.value;
    
    if (selectedSession) {
        window.location.href = '/visualizer?session=' + selectedSession;
    } else {
        window.location.href = '/visualizer';
    }
}

function refreshData() {
    if (currentSession) {
        loadSession();
    }
}

// Función para inicializar el visualizador
function initializeVisualizer(session) {
    currentSession = session || '';
    
    // Cargar datos si hay sesión seleccionada
    if (currentSession) {
        loadSessionData(currentSession);
    }
}

async function loadSessionData(sessionName) {
    try {
        const response = await fetch('/api/session/' + sessionName + '/visualize');
        const data = await response.json();
        
        if (data.success) {
            displaySessionData(data);
        } else {
            showError('Error cargando sesión: ' + data.message);
        }
    } catch (error) {
        showError('Error de conexión: ' + error.message);
    }
}

function displaySessionData(data) {
    // Mostrar estadísticas
    document.getElementById('totalImages').textContent = data.total_images;
    document.getElementById('totalLabels').textContent = data.total_labels;
    const avgLabels = data.total_images > 0 ? (data.total_labels / data.total_images).toFixed(1) : '0.0';
    document.getElementById('avgLabels').textContent = avgLabels;
    document.getElementById('statsContainer').style.display = 'block';
    
    // Mostrar galería de imágenes
    let galleryHTML = '<div class="gallery">';
    
    if (data.images && data.images.length > 0) {
        data.images.forEach((image, index) => {
            galleryHTML += `
                <div class="image-card">
                    <div class="image-container">
                        <canvas id="canvas_${index}" width="${image.width}" height="${image.height}" 
                                style="max-width: 100%; height: 250px; object-fit: contain; border: 1px solid #ddd;">
                        </canvas>
                    </div>
                    <div class="image-info">
                        <h4>📄 ${image.name}</h4>
                        <p>🏷️ Labels: ${image.labels || 0}</p>
                        <p>📏 Resolución: ${image.width}x${image.height}</p>
                    </div>
                </div>
            `;
        });
    } else {
        galleryHTML += '<div style="grid-column: 1 / -1; text-align: center; padding: 40px;"><h3>📷 No hay imágenes en esta sesión</h3></div>';
    }
    
    galleryHTML += '</div>';
    document.getElementById('contentContainer').innerHTML = galleryHTML;
    
    // Dibujar las imágenes y las anotaciones en los canvas
    if (data.images && data.images.length > 0) {
        data.images.forEach((image, index) => {
            drawImageWithAnnotations(image, index);
        });
    }
}

function drawImageWithAnnotations(imageData, index) {
    const canvas = document.getElementById(`canvas_${index}`);
    if (!canvas) return;
    
    const ctx = canvas.getContext('2d');
    const img = new Image();
    
    img.onload = function() {
        // Dibujar la imagen
        ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
        
        // Dibujar las anotaciones
        if (imageData.annotations && imageData.annotations.length > 0) {
            imageData.annotations.forEach(annotation => {
                drawBoundingBox(ctx, annotation, canvas.width, canvas.height, imageData.width, imageData.height);
            });
        }
    };
    
    img.onerror = function() {
        // Si la imagen no se puede cargar, mostrar placeholder
        ctx.fillStyle = '#f8f9fa';
        ctx.fillRect(0, 0, canvas.width, canvas.height);
        ctx.fillStyle = '#6c757d';
        ctx.font = '16px Arial';
        ctx.textAlign = 'center';
        ctx.fillText('Imagen no encontrada', canvas.width/2, canvas.height/2);
    };
    
    img.src = `/image/${currentSession}/${imageData.name}`;
}

function drawBoundingBox(ctx, annotation, canvasWidth, canvasHeight, imageWidth, imageHeight) {
    // Escalar las coordenadas al tamaño del canvas
    const scaleX = canvasWidth / imageWidth;
    const scaleY = canvasHeight / imageHeight;
    
    const x1 = annotation.x1 * scaleX;
    const y1 = annotation.y1 * scaleY;
    const x2 = annotation.x2 * scaleX;
    const y2 = annotation.y2 * scaleY;
    
    // Colores por clase
    const colors = ['#ff0000', '#00ff00', '#0000ff', '#ffff00', '#ff00ff', '#00ffff'];
    const color = colors[annotation.class_id % colors.length] || '#ff0000';
    
    // Dibujar el rectángulo
    ctx.strokeStyle = color;
    ctx.lineWidth = 2;
    ctx.strokeRect(x1, y1, x2 - x1, y2 - y1);
    
    // Dibujar la etiqueta de clase
    ctx.fillStyle = color;
    ctx.font = '12px Arial';
    ctx.fillText(`Clase ${annotation.class_id}`, x1, y1 - 5);
    
    // Fondo semi-transparente para el texto
    ctx.globalAlpha = 0.7;
    ctx.fillRect(x1, y1 - 20, 60, 15);
    ctx.globalAlpha = 1.0;
    ctx.fillStyle = 'white';
    ctx.fillText(`Clase ${annotation.class_id}`, x1 + 2, y1 - 8);
}

function showError(message) {
    document.getElementById('contentContainer').innerHTML = `
        <div class="no-session">
            <h3>❌ Error</h3>
            <p>${message}</p>
            <button onclick="refreshData()" class="controls button">🔄 Intentar de nuevo</button>
        </div>
    `;
}