File size: 5,014 Bytes
9eabb80 |
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 |
// CIFAR-10 Classifier JavaScript
let selectedFile = null;
// DOM Elements
const uploadArea = document.getElementById('uploadArea');
const fileInput = document.getElementById('fileInput');
const previewCard = document.getElementById('previewCard');
const previewImage = document.getElementById('previewImage');
const classifyBtn = document.getElementById('classifyBtn');
const randomBtn = document.getElementById('randomBtn');
const resultsSection = document.getElementById('resultsSection');
const loadingOverlay = document.getElementById('loadingOverlay');
// Upload area click handler
uploadArea.addEventListener('click', () => {
fileInput.click();
});
// File input change handler
fileInput.addEventListener('change', (e) => {
const file = e.target.files[0];
if (file) {
handleFile(file);
}
});
// Drag and drop handlers
uploadArea.addEventListener('dragover', (e) => {
e.preventDefault();
uploadArea.classList.add('drag-over');
});
uploadArea.addEventListener('dragleave', () => {
uploadArea.classList.remove('drag-over');
});
uploadArea.addEventListener('drop', (e) => {
e.preventDefault();
uploadArea.classList.remove('drag-over');
const file = e.dataTransfer.files[0];
if (file && file.type.startsWith('image/')) {
handleFile(file);
}
});
// Handle file selection
function handleFile(file) {
selectedFile = file;
const reader = new FileReader();
reader.onload = (e) => {
previewImage.src = e.target.result;
previewCard.style.display = 'block';
resultsSection.style.display = 'none';
};
reader.readAsDataURL(file);
}
// Classify button handler
classifyBtn.addEventListener('click', async () => {
if (!selectedFile) return;
const formData = new FormData();
formData.append('file', selectedFile);
try {
loadingOverlay.style.display = 'flex';
const response = await fetch('/predict', {
method: 'POST',
body: formData
});
const data = await response.json();
if (data.error) {
alert('Error: ' + data.error);
return;
}
displayResults(data);
} catch (error) {
alert('Error: ' + error.message);
} finally {
loadingOverlay.style.display = 'none';
}
});
// Random sample button handler
randomBtn.addEventListener('click', async () => {
try {
loadingOverlay.style.display = 'flex';
const response = await fetch('/random_sample');
const data = await response.json();
if (data.error) {
alert('Error: ' + data.error);
return;
}
// Convert base64 to blob
const blob = await fetch(data.image).then(r => r.blob());
const file = new File([blob], 'random_sample.png', { type: 'image/png' });
handleFile(file);
} catch (error) {
alert('Error: ' + error.message);
} finally {
loadingOverlay.style.display = 'none';
}
});
// Display classification results
function displayResults(data) {
document.getElementById('predictedClass').textContent = data.predicted_class;
document.getElementById('confidenceValue').textContent = data.confidence.toFixed(2) + '%';
// Update confidence badge color based on confidence level
const badge = document.getElementById('confidenceBadge');
if (data.confidence >= 80) {
badge.style.background = 'rgba(79, 172, 254, 0.2)';
badge.style.borderColor = 'rgba(79, 172, 254, 0.4)';
badge.style.color = '#4facfe';
} else if (data.confidence >= 60) {
badge.style.background = 'rgba(240, 147, 251, 0.2)';
badge.style.borderColor = 'rgba(240, 147, 251, 0.4)';
badge.style.color = '#f093fb';
} else {
badge.style.background = 'rgba(245, 87, 108, 0.2)';
badge.style.borderColor = 'rgba(245, 87, 108, 0.4)';
badge.style.color = '#f5576c';
}
// Display top 5 predictions
const top5Container = document.getElementById('top5Container');
top5Container.innerHTML = '';
data.top5_predictions.forEach((pred, index) => {
const item = document.createElement('div');
item.className = 'prediction-item';
item.style.animationDelay = `${index * 0.1}s`;
item.innerHTML = `
<span class="prediction-item-name">${pred.class}</span>
<div class="prediction-item-bar">
<div class="prediction-item-fill" style="width: ${pred.probability}%"></div>
</div>
<span class="prediction-item-value">${pred.probability.toFixed(2)}%</span>
`;
top5Container.appendChild(item);
});
resultsSection.style.display = 'grid';
// Scroll to results
resultsSection.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}
|