File size: 2,405 Bytes
908ca9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const imageInput = document.getElementById('imageInput');
const previewImg = document.getElementById('previewImg');
const classifyBtn = document.getElementById('classifyBtn');
const resultDiv = document.getElementById('result');

imageInput.addEventListener('change', (event) => {
    const file = event.target.files[0];
    if (file) {
        const reader = new FileReader();
        reader.onload = function(e) {
            previewImg.src = e.target.result;
            previewImg.style.display = 'block';
            classifyBtn.disabled = false;
            resultDiv.innerHTML = '';
            resultDiv.className = 'result';
        };
        reader.readAsDataURL(file);
    } else {
        previewImg.style.display = 'none';
        classifyBtn.disabled = true;
    }
});

classifyBtn.addEventListener('click', async () => {
    const file = imageInput.files[0];
    if (!file) return;

    classifyBtn.disabled = true;
    classifyBtn.innerText = '🔍 Analizando...';
    resultDiv.innerHTML = '<em>Procesando imagen...</em>';

    const formData = new FormData();
    formData.append('image', file);

    try {
        const response = await fetch('/predict', {
            method: 'POST',
            body: formData
        });

        if (!response.ok) {
            const errorData = await response.json();
            throw new Error(errorData.error || 'Error del servidor');
        }

        const data = await response.json();
        const genero = data.genero;
        const confianza = data.confianza; // Capturamos la nueva variable

        // Actualizamos el HTML para mostrar tanto el género como la confianza
        resultDiv.innerHTML = `
            🧬 La persona es <strong>${genero}</strong>.<br>
            🎯 Precisión: <strong>${confianza}</strong>
        `;
        
        // Removemos clases anteriores por si acaso
        resultDiv.classList.remove('mujer', 'hombre', 'error');
        // Añadimos la clase correspondiente
        resultDiv.classList.add(genero === 'mujer' ? 'mujer' : 'hombre');

    } catch (error) {
        console.error(error);
        resultDiv.innerHTML = `❌ Error: ${error.message}`;
        resultDiv.classList.remove('mujer', 'hombre'); // Limpiar estilos previos
        resultDiv.classList.add('error');
    } finally {
        classifyBtn.disabled = false;
        classifyBtn.innerText = '🔍 Clasificar';
    }
});