Spaces:
Running
Running
File size: 12,507 Bytes
4874e86 |
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Segmentation Tool</title>
<style>
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
/* Additional styles for the area table */
.area-table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
.area-table th, .area-table td {
border: 1px solid #ddd;
padding: 8px;
text-align: center;
}
.area-table th {
background-color: #f2f2f2;
font-weight: bold;
}
.controls {
margin-bottom: 20px;
position: sticky;
top: 0;
background: white;
z-index: 100;
padding: 10px 0;
}
.button {
padding: 8px 16px;
margin-right: 10px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
.button.active {
background-color: #45a049;
}
.button:disabled {
background-color: #cccccc;
cursor: not-allowed;
}
.image-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 20px;
}
.image-wrapper {
position: relative;
border: 1px solid #ddd;
padding: 10px;
}
.canvas-wrapper {
position: relative;
overflow: hidden;
width: 100%;
height: 100%;
}
canvas {
position: absolute;
top: 0;
left: 0;
pointer-events: none;
width: 100%;
height: 100%;
}
img {
max-width: 100%;
height: auto;
display: block;
}
.status {
margin-top: 10px;
padding: 10px;
border-radius: 4px;
display: none;
position: fixed;
bottom: 20px;
right: 20px;
z-index: 1000;
}
.status.error {
background-color: #ffebee;
color: #c62828;
display: block;
}
.status.success {
background-color: #e8f5e9;
color: #2e7d32;
display: block;
}
.status.wait {
background-color: #fff3e0;
color: #f57c00;
display: block;
}
.top-right-buttons {
position: absolute;
top: 20px;
right: 20px;
display: flex;
gap: 10px;
}
</style>
</head>
<body>
<div class="container">
<h1>Voids and Components Classification</h1>
<div class="controls">
<input type="file" id="fileInput" accept="image/*">
<button id="classifyButton" class="button" disabled>Classify</button>
</div>
<div id="status" class="status"></div>
<div class="image-container">
<div class="image-wrapper">
<h2>Original Image</h2>
<div class="canvas-wrapper" id="canvasWrapper">
<img id="image" src="" alt="Upload an image" draggable="false">
<canvas id="overlayCanvas"></canvas>
</div>
</div>
<div class="image-wrapper">
<h2>Segmentation Prediction</h2>
<img id="classifiedImage" src="" alt="Classified image will appear here">
</div>
</div>
<table class="area-table" id="areaTable">
<caption><strong>Segmentation Area Table</strong></caption>
<thead>
<tr>
<th>Image</th>
<th>Component</th>
<th>Area</th>
<th>Void Area %</th>
<th>Max Void Area %</th>
</tr>
</thead>
<tbody>
<!-- Rows will be dynamically added here using JavaScript -->
</tbody>
</table>
<button id="exportButton" class="button" disabled>Export to CSV</button>
</div>
<div class="top-right-buttons">
<a href="{{ url_for('index') }}" class="button">Go to SAM</a>
<button class="button" disabled>Go to Yolo</button>
</div>
<script>
class ClassificationTool {
constructor() {
this.initializeElements();
this.initializeState();
this.setupEventListeners();
}
initializeElements() {
this.fileInput = document.getElementById('fileInput');
this.image = document.getElementById('image');
this.overlayCanvas = document.getElementById('overlayCanvas');
this.overlayCtx = this.overlayCanvas.getContext('2d');
this.classifiedImage = document.getElementById('classifiedImage');
this.status = document.getElementById('status');
this.canvasWrapper = document.getElementById('canvasWrapper');
this.buttons = {
classify: document.getElementById('classifyButton'),
export: document.getElementById('exportButton')
};
}
initializeState() {
this.currentMode = null;
this.uploadedFilename = '';
}
setupEventListeners() {
this.fileInput.addEventListener('change', (e) => this.handleFileUpload(e));
this.image.addEventListener('load', () => this.handleImageLoad());
this.buttons.classify.addEventListener('click', () => this.classify());
this.buttons.export.addEventListener('click', () => this.exportTableToCSV());
}
setMode(mode) {
this.currentMode = this.currentMode === mode ? null : mode;
Object.values(this.buttons).forEach(button => button.classList.remove('active'));
if (this.currentMode) {
this.buttons[this.currentMode].classList.add('active');
}
this.canvasWrapper.style.cursor = this.currentMode ? 'crosshair' : 'default';
}
updateAreaTable(areaData) {
const areaTableBody = document.getElementById('areaTable').getElementsByTagName('tbody')[0];
areaTableBody.innerHTML = ''; // Clear existing rows
areaData.forEach(item => {
const row = areaTableBody.insertRow();
const cellImage = row.insertCell(0);
const cellComponent = row.insertCell(1);
const cellArea = row.insertCell(2);
const cellVoidArea = row.insertCell(3);
const cellMaxVoidArea = row.insertCell(4);
cellImage.textContent = item['Image'];
cellComponent.textContent = item.Component;
cellArea.textContent = item['Area'];
cellVoidArea.textContent = item['Void Area %'].toFixed(2) + '%'; // Format as percentage
cellMaxVoidArea.textContent = item['Max Void Area %'].toFixed(2) + '%'; // Format as percentage
});
}
async exportTableToCSV() {
const table = document.getElementById('areaTable');
let csvContent = '';
const rows = table.querySelectorAll('tr');
rows.forEach(row => {
const cols = row.querySelectorAll('th, td');
const rowData = Array.from(cols).map(col => col.textContent).join(',');
csvContent += rowData + '\n';
});
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'report.csv');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
async handleFileUpload(event) {
const file = event.target.files[0];
if (!file) return;
// Show "please wait" message
this.showStatus('Uploading the image, please wait...', 'wait');
try {
const formData = new FormData();
formData.append('file', file);
const response = await fetch('/upload_yolo', {
method: 'POST',
body: formData
});
const result = await response.json();
if (result.error) throw new Error(result.error);
this.image.src = result.image_url;
this.uploadedFilename = result.filename;
this.originalDimensions = result.dimensions;
this.buttons.classify.disabled = false;
// Show success message after upload is complete
this.showStatus('Image uploaded successfully', 'success');
} catch (error) {
this.showStatus(`Upload failed: ${error.message}`, 'error');
}
}
async classify() {
if (!this.uploadedFilename) {
this.showStatus('Please upload an image first', 'error');
return;
}
try {
this.buttons.classify.disabled = true;
const requestData = {
filename: this.uploadedFilename
};
console.log('Sending data to backend:', requestData); // Debug logging
const response = await fetch('/classify', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(requestData),
});
const result = await response.json();
if (result.error) throw new Error(result.error);
this.classifiedImage.src = result.result_path + '?t=' + new Date().getTime();
this.showStatus('Classification completed successfully', 'success');
// Check if area_data is defined and is an array before updating the table
if (Array.isArray(result.area_data)) {
this.updateAreaTable(result.area_data);
this.buttons.export.disabled = false;
} else {
throw new Error('Area data is not available or is not an array.');
}
} catch (error) {
this.showStatus(`Failed to classify: ${error.message}`, 'error');
console.error('Classification error:', error); // Debug logging
} finally {
this.buttons.classify.disabled = false;
}
}
showStatus(message, type) {
this.status.className = `status ${type}`;
this.status.textContent = message;
this.status.style.display = 'block';
if (type === 'success' || type === 'error') {
setTimeout(() => {
this.status.style.display = 'none';
}, 3000);
}
}
}
// Initialize the tool when the page loads
window.addEventListener('load', () => {
new ClassificationTool();
});
</script>
</body>
</html> |