File size: 5,600 Bytes
0dcc39c
 
 
 
 
 
 
 
 
02facdb
0dcc39c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
02facdb
0dcc39c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
02facdb
 
 
 
 
 
 
0dcc39c
 
 
 
 
 
 
 
 
 
02facdb
0dcc39c
 
 
 
02facdb
126419e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
02facdb
 
 
 
 
 
 
 
 
 
 
126419e
 
02facdb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0dcc39c
 
 
 
 
 
 
 
 
 
02facdb
0dcc39c
 
 
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
const dropZone = document.getElementById('drop-zone');
const fileInput = document.getElementById('file-input');
const loadingState = document.getElementById('loading');
const resultSection = document.getElementById('result-section');
const previewImg = document.getElementById('preview-img');
const diagnosisBadge = document.getElementById('diagnosis-badge');
const confidenceText = document.getElementById('confidence-text');
const confidenceBar = document.getElementById('confidence-bar');
const resetBtn = document.getElementById('reset-btn');
const bboxOverlay = document.getElementById('bbox-overlay'); // YOLO Box layer

dropZone.addEventListener('click', () => fileInput.click());

dropZone.addEventListener('dragover', (e) => {
    e.preventDefault();
    dropZone.classList.add('dragover');
});

dropZone.addEventListener('dragleave', () => {
    dropZone.classList.remove('dragover');
});

dropZone.addEventListener('drop', (e) => {
    e.preventDefault();
    dropZone.classList.remove('dragover');
    if (e.dataTransfer.files.length) {
        handleFile(e.dataTransfer.files[0]);
    }
});

fileInput.addEventListener('change', (e) => {
    if (e.target.files.length) {
        handleFile(e.target.files[0]);
    }
});

function handleFile(file) {
    if (!file.type.startsWith('image/')) return alert('Please upload an image file.');

    const reader = new FileReader();
    reader.onload = (e) => {
        previewImg.src = e.target.result;
    };
    reader.readAsDataURL(file);

    dropZone.style.display = 'none';
    loadingState.style.display = 'block';

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

    fetch('/api/predict', {
        method: 'POST',
        body: formData
    })
    .then(res => {
        if (!res.ok) throw new Error("API Error");
        return res.json();
    })
    .then(data => {
        showResults(data);
    })
    .catch(err => {
        alert("Error analyzing image.");
        resetUI();
    });
}

function showResults(data) {
    loadingState.style.display = 'none';
    resultSection.style.display = 'block';
    
    // Clear old YOLO boxes
    bboxOverlay.innerHTML = '';
    
    // Required to scale the coordinates from the physical image to the CSS display size
    const imgWidth = data.image_width;
    const imgHeight = data.image_height;

    setTimeout(() => {
        confidenceBar.style.width = data.parasite_probability;
        confidenceText.innerText = data.parasite_probability;
        
        if (data.parasite_detected) {
            diagnosisBadge.innerText = 'Parasite Detected';
            diagnosisBadge.className = 'badge danger';
            confidenceBar.style.background = 'var(--danger)';
            document.querySelector('.orb1').style.background = 'var(--danger)';
        } else {
            diagnosisBadge.innerText = 'Healthy Region';
            diagnosisBadge.className = 'badge success';
            confidenceBar.style.background = 'var(--success)';
            document.querySelector('.orb1').style.background = 'var(--success)';
        }
        
        // Calculate precisely where the image actually rendered inside the object-fit: contain CSS box
        const containerWidth = previewImg.clientWidth;
        const containerHeight = previewImg.clientHeight;
        const imgRatio = imgWidth / imgHeight;
        const containerRatio = containerWidth / containerHeight;
        
        let displayWidth, displayHeight, offsetX, offsetY;
        
        if (containerRatio > imgRatio) {
            displayHeight = containerHeight;
            displayWidth = displayHeight * imgRatio;
            offsetX = (containerWidth - displayWidth) / 2;
            offsetY = 0;
        } else {
            displayWidth = containerWidth;
            displayHeight = displayWidth / imgRatio;
            offsetX = 0;
            offsetY = (containerHeight - displayHeight) / 2;
        }

        const scaleX = displayWidth / imgWidth;
        const scaleY = displayHeight / imgHeight;
        
        if (data.detections && data.detections.length > 0) {
            data.detections.forEach(det => {
                const box = det.box;
                
                const boxEl = document.createElement('div');
                boxEl.className = 'bounding-box';
                boxEl.classList.add(det.label.toLowerCase().includes('parasite') ? 'box-danger' : 'box-safe');
                
                const left = (box.x1 * scaleX) + offsetX;
                const top = (box.y1 * scaleY) + offsetY;
                const width = (box.x2 - box.x1) * scaleX;
                const height = (box.y2 - box.y1) * scaleY;
                
                boxEl.style.left = `${left}px`;
                boxEl.style.top = `${top}px`;
                boxEl.style.width = `${width}px`;
                boxEl.style.height = `${height}px`;
                
                const labelEl = document.createElement('div');
                labelEl.className = 'box-label';
                labelEl.innerText = `${det.label} ${(det.confidence * 100).toFixed(0)}%`;
                
                boxEl.appendChild(labelEl);
                bboxOverlay.appendChild(boxEl);
            });
        }
        
    }, 100);
}

function resetUI() {
    resultSection.style.display = 'none';
    loadingState.style.display = 'none';
    dropZone.style.display = 'block';
    fileInput.value = '';
    confidenceBar.style.width = '0%';
    document.querySelector('.orb1').style.background = '#3b82f6';
    bboxOverlay.innerHTML = '';
}

resetBtn.addEventListener('click', resetUI);