Subh775 commited on
Commit
8c8b6d8
·
verified ·
1 Parent(s): 3224a0d

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +342 -332
index.html CHANGED
@@ -3,436 +3,446 @@
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <title>Leaf Segmentation with Precision</title>
 
7
  <style>
8
  :root {
9
  --primary: #4CAF50;
 
 
10
  --dark-bg: #121212;
11
- --surface-bg: #1E1E2E;
12
  --text-primary: #E0E0E0;
13
  --text-secondary: #9E9E9E;
14
- --border-color: rgba(255, 255, 255, 0.1);
 
 
 
 
 
 
15
  }
16
- * { margin: 0; padding: 0; box-sizing: border-box; }
17
  body {
18
- font-family: 'Segoe UI', sans-serif;
19
- background: var(--dark-bg);
20
  color: var(--text-primary);
 
21
  padding: 20px;
22
  }
23
  .container {
24
  max-width: 1400px;
25
  margin: 0 auto;
26
- background: var(--surface-bg);
27
- border-radius: 16px;
28
- box-shadow: 0 10px 30px rgba(0,0,0,0.3);
29
- border: 1px solid var(--border-color);
 
30
  }
31
- h1 {
 
 
32
  text-align: center;
33
- color: var(--primary);
34
- padding: 30px;
35
- font-size: 2.2em;
36
  font-weight: 300;
 
 
 
 
 
 
 
 
 
 
37
  }
38
  .main-content {
39
  display: grid;
40
  grid-template-columns: 1fr 1fr;
41
- gap: 30px;
42
- padding: 0 30px 30px 30px;
43
  }
44
  .column {
45
- background: rgba(0,0,0,0.2);
46
- border-radius: 12px;
47
- padding: 25px;
 
48
  }
49
  .column h2 {
50
- color: var(--primary);
51
- margin-bottom: 20px;
52
- font-size: 1.5em;
53
- font-weight: 500;
 
 
54
  }
55
  .upload-area {
56
- border: 2px dashed var(--primary);
57
- border-radius: 8px;
58
- padding: 30px;
59
  text-align: center;
60
- margin-bottom: 20px;
61
- transition: all 0.3s ease;
62
  cursor: pointer;
 
 
 
63
  }
64
- .upload-area:hover, .upload-area.dragover {
65
- background: rgba(76, 175, 80, 0.1);
66
- border-color: #66BB6A;
 
67
  }
68
- .canvas-container {
69
- background: #000;
70
- border-radius: 8px;
71
  margin-bottom: 20px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  display: flex;
73
- justify-content: center;
74
  align-items: center;
75
- height: 400px;
 
76
  border: 1px solid var(--border-color);
77
  }
78
- canvas {
79
  max-width: 100%;
80
- max-height: 100%;
81
  object-fit: contain;
82
- cursor: crosshair;
83
- }
84
- .tools-panel, .output-controls {
85
- background: rgba(0,0,0,0.2);
86
- border-radius: 8px;
87
- padding: 20px;
88
- margin-bottom: 20px;
89
  }
90
- .tool-group { margin-bottom: 15px; }
91
- .tool-group label {
92
- display: block;
93
- margin-bottom: 8px;
94
- font-weight: 500;
95
  color: var(--text-secondary);
 
 
 
96
  }
97
- .tool-buttons { display: flex; gap: 10px; }
98
- .tool-btn {
99
- background: rgba(76, 175, 80, 0.2);
100
- color: var(--primary);
101
- border: 1px solid var(--primary);
102
- padding: 10px 16px;
103
- border-radius: 6px;
 
104
  cursor: pointer;
105
  transition: all 0.3s ease;
106
- flex: 1;
107
- }
108
- .tool-btn:hover { background: rgba(76, 175, 80, 0.3); }
109
- .tool-btn.active { background: var(--primary); color: white; }
110
- .predict-btn {
111
- background: linear-gradient(135deg, #FF6B35, #F7931E);
112
- color: white; border: none;
113
- padding: 15px 30px; font-size: 18px; width: 100%;
114
- border-radius: 8px; cursor: pointer; transition: all 0.3s ease;
115
- }
116
- .predict-btn:hover:not(:disabled) { transform: translateY(-2px); }
117
- .predict-btn:disabled { background: #666; cursor: not-allowed; }
118
- .output-tabs { display: flex; gap: 10px; margin-bottom: 20px; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  .download-btn {
120
- background: linear-gradient(135deg, #9C27B0, #673AB7);
121
- color: white; border: none; padding: 12px 24px;
122
- border-radius: 8px; cursor: pointer; width: 100%;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
  }
124
- .download-btn:disabled { background: #666; cursor: not-allowed; }
125
- .hidden { display: none !important; }
126
- @media (max-width: 900px) {
127
- .main-content { grid-template-columns: 1fr; }
128
  }
129
  </style>
130
  </head>
131
  <body>
132
  <div class="container">
133
- <h1>Interactive Leaf Segmentation</h1>
 
 
 
 
134
  <div class="main-content">
135
  <div class="column">
136
- <h2>Input & Controls</h2>
137
- <div class="upload-area" id="upload-area">
138
- <p>Click or Drop Image Here</p>
 
 
 
139
  </div>
140
- <input type="file" id="file-input" class="hidden" accept="image/*">
141
 
142
- <div class="canvas-container">
143
- <canvas id="input-canvas"></canvas>
144
  </div>
145
- <div class="tools-panel">
146
- <div class="tool-group">
147
- <label>Annotation Tools</label>
148
- <div class="tool-buttons">
149
- <button class="tool-btn active" data-tool="point">Point (Dot)</button>
150
- <button class="tool-btn" data-tool="line">Line/Drag</button>
151
- </div>
152
- </div>
153
- <button class="tool-btn" id="clear-annotations" style="width:100%; margin-top:10px;">Clear Annotations</button>
154
  </div>
155
- <button class="predict-btn" id="predict-btn" disabled>Predict</button>
156
  </div>
 
157
  <div class="column">
158
- <h2>Output</h2>
159
- <div class="output-tabs">
160
- <button class="tool-btn active" id="view-filled">Filled</button>
161
- <button class="tool-btn" id="view-outline">Outline</button>
162
- <button class="tool-btn" id="view-binary">Binary Mask</button>
163
- </div>
164
- <div class="canvas-container" id="output-container">
165
- <canvas id="output-canvas"></canvas>
166
  </div>
167
- <div class="output-controls">
168
- <div class="tool-group" id="transparency-control">
169
- <label>Mask Transparency</label>
170
- <input type="range" id="transparency" min="0" max="100" value="40" style="width:100%;">
171
- </div>
172
- <button class="download-btn" id="download-btn" disabled>Download Mask</button>
173
  </div>
174
  </div>
175
  </div>
 
 
 
 
176
  </div>
177
 
178
- <script src="https://docs.opencv.org/4.8.0/opencv.js"></script>
179
  <script>
180
- // Wait for OpenCV to be ready
181
- cv['onRuntimeInitialized'] = () => {
182
- console.log('OpenCV.js is ready.');
183
- document.addEventListener('DOMContentLoaded', () => {
184
- new SegmentationApp();
185
- });
186
- };
187
-
188
- class SegmentationApp {
189
  constructor() {
190
- this.inputCanvas = document.getElementById('input-canvas');
191
- this.inputCtx = this.inputCanvas.getContext('2d');
192
- this.outputCanvas = document.getElementById('output-canvas');
193
- this.outputCtx = this.outputCanvas.getContext('2d');
194
- this.maskCanvas = document.createElement('canvas');
195
- this.maskCtx = this.maskCanvas.getContext('2d');
196
- this.currentTool = 'point';
197
- this.brushSize = 8;
198
- this.isDrawing = false;
199
- this.uploadedImage = null;
200
- this.predictedMask = null;
201
- this.annotations = [];
202
- this.currentView = 'filled';
203
- this.predictBtn = document.getElementById('predict-btn');
204
- this.downloadBtn = document.getElementById('download-btn');
205
- this.transparencySlider = document.getElementById('transparency');
206
- this.transparencyControl = document.getElementById('transparency-control');
207
- this.initEventListeners();
208
  }
209
 
210
- initEventListeners() {
211
- // FIX: Simplified event listeners for robust uploading
212
- const fileInput = document.getElementById('file-input');
213
- const uploadArea = document.getElementById('upload-area');
214
-
215
- uploadArea.onclick = () => fileInput.click();
216
- fileInput.onchange = e => this.handleFile(e.target.files[0]);
217
 
218
- uploadArea.ondragover = e => { e.preventDefault(); uploadArea.classList.add('dragover'); };
219
- uploadArea.ondragleave = () => uploadArea.classList.remove('dragover');
220
- uploadArea.ondrop = e => {
221
  e.preventDefault();
222
- uploadArea.classList.remove('dragover');
223
- this.handleFile(e.dataTransfer.files[0]);
224
- };
225
-
226
- this.inputCanvas.addEventListener('mousedown', e => this.startDrawing(e));
227
- this.inputCanvas.addEventListener('mousemove', e => this.draw(e));
228
- ['mouseup', 'mouseout'].forEach(evt => this.inputCanvas.addEventListener(evt, () => this.stopDrawing()));
229
-
230
- document.querySelectorAll('.tool-btn[data-tool]').forEach(btn => {
231
- btn.onclick = (e) => {
232
- document.querySelectorAll('.tool-btn[data-tool]').forEach(b => b.classList.remove('active'));
233
- e.target.classList.add('active');
234
- this.currentTool = e.target.dataset.tool;
235
- };
236
  });
237
-
238
- document.getElementById('view-filled').onclick = () => this.setView('filled');
239
- document.getElementById('view-outline').onclick = () => this.setView('outline');
240
- document.getElementById('view-binary').onclick = () => this.setView('binary');
241
 
242
- this.transparencySlider.oninput = () => this.renderOutput();
243
- document.getElementById('clear-annotations').onclick = () => this.clearAnnotations();
244
- this.predictBtn.onclick = () => this.predict();
245
- this.downloadBtn.onclick = () => this.downloadMask();
246
  }
247
 
248
- handleFile(file) {
249
- if (!file || !file.type.startsWith('image/')) return;
 
 
 
 
 
 
 
 
250
  const reader = new FileReader();
251
- reader.onload = e => {
252
- const img = new Image();
253
- img.onload = () => {
254
- this.uploadedImage = img;
255
- this.reset();
256
- this.drawImageScaled(this.inputCanvas, this.inputCtx, img);
257
- // Also set the size for other canvases
258
- this.outputCanvas.width = this.inputCanvas.width;
259
- this.outputCanvas.height = this.inputCanvas.height;
260
- this.maskCanvas.width = this.inputCanvas.width;
261
- this.maskCanvas.height = this.inputCanvas.height;
262
- };
263
- img.src = e.target.result;
264
  };
265
  reader.readAsDataURL(file);
266
  }
267
-
268
- reset() {
269
- this.annotations = [];
270
- this.predictedMask = null;
271
- this.predictBtn.disabled = true;
272
- this.downloadBtn.disabled = true;
273
- this.outputCtx.clearRect(0,0, this.outputCanvas.width, this.outputCanvas.height);
274
- }
275
-
276
- drawImageScaled(canvas, ctx, img) {
277
- const container = canvas.parentElement;
278
- const hRatio = container.clientWidth / img.width;
279
- const vRatio = container.clientHeight / img.height;
280
- const ratio = Math.min(hRatio, vRatio);
281
- canvas.width = img.width * ratio;
282
- canvas.height = img.height * ratio;
283
- ctx.clearRect(0, 0, canvas.width, canvas.height);
284
- ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
285
- }
286
-
287
- getCanvasCoordinates(e) {
288
- const rect = this.inputCanvas.getBoundingClientRect();
289
- return {
290
- x: (e.clientX - rect.left) * (this.inputCanvas.width / rect.width),
291
- y: (e.clientY - rect.top) * (this.inputCanvas.height / rect.height)
292
- };
293
- }
294
-
295
- startDrawing(e) {
296
- if (!this.uploadedImage) return;
297
- this.isDrawing = true;
298
- const coords = this.getCanvasCoordinates(e);
299
- if (this.currentTool === 'point') this.drawOnCanvas(coords.x, coords.y);
300
- else { this.lastX = coords.x; this.lastY = coords.y; }
301
- }
302
-
303
- draw(e) {
304
- if (!this.isDrawing || this.currentTool !== 'line') return;
305
- const coords = this.getCanvasCoordinates(e);
306
- this.drawOnCanvas(this.lastX, this.lastY, coords.x, coords.y);
307
- [this.lastX, this.lastY] = [coords.x, coords.y];
308
- }
309
-
310
- stopDrawing() { this.isDrawing = false; }
311
-
312
- drawOnCanvas(x1, y1, x2 = null, y2 = null) {
313
- [this.inputCtx, this.maskCtx].forEach((ctx, i) => {
314
- ctx.beginPath();
315
- ctx.lineWidth = this.brushSize * 2;
316
- ctx.strokeStyle = (i === 0) ? '#4CAF50' : 'white';
317
- ctx.fillStyle = (i === 0) ? '#4CAF50' : 'white';
318
- ctx.lineCap = 'round';
319
- if (x2 && y2) {
320
- ctx.moveTo(x1, y1); ctx.lineTo(x2, y2); ctx.stroke();
321
- } else {
322
- ctx.arc(x1, y1, this.brushSize, 0, Math.PI * 2); ctx.fill();
323
- }
324
- });
325
- this.annotations.push(true);
326
- this.predictBtn.disabled = false;
327
- }
328
-
329
- clearAnnotations() {
330
- if (!this.uploadedImage) return;
331
- this.reset();
332
- this.drawImageScaled(this.inputCanvas, this.inputCtx, this.uploadedImage);
333
- this.maskCtx.clearRect(0, 0, this.maskCanvas.width, this.maskCanvas.height);
334
  }
335
-
336
- async predict() {
337
- if (!this.uploadedImage || this.annotations.length === 0) return;
338
- this.predictBtn.disabled = true; this.predictBtn.innerText = "Processing...";
 
 
 
339
  try {
340
- const payload = {
341
- image: this.inputCanvas.toDataURL('image/jpeg'),
342
- scribble_mask: this.maskCanvas.toDataURL('image/png')
343
- };
344
  const response = await fetch('/predict', {
345
  method: 'POST',
346
  headers: { 'Content-Type': 'application/json' },
347
  body: JSON.stringify(payload)
348
  });
349
- if (!response.ok) throw new Error(`Server error: ${response.status}`);
350
  const result = await response.json();
351
- const maskImg = new Image();
352
- maskImg.onload = () => {
353
- this.predictedMask = maskImg;
354
- this.renderOutput();
355
  this.downloadBtn.disabled = false;
356
- };
357
- maskImg.src = result.predicted_mask;
 
 
 
358
  } catch (error) {
359
- console.error('Prediction failed:', error);
360
- alert('Prediction failed. Check the console for details.');
361
  } finally {
362
- this.predictBtn.innerText = "Predict";
363
- this.predictBtn.disabled = this.annotations.length === 0;
364
  }
365
  }
366
-
367
- setView(view) {
368
- this.currentView = view;
369
- document.querySelectorAll('.output-tabs .tool-btn').forEach(b => b.classList.remove('active'));
370
- document.getElementById(`view-${view}`).classList.add('active');
371
- this.transparencyControl.style.display = (view === 'filled') ? 'block' : 'none';
372
- this.renderOutput();
373
  }
374
-
375
- renderOutput() {
376
- if (!this.predictedMask) return;
377
-
378
- // Always draw the original image on the output canvas first
379
- this.outputCtx.clearRect(0,0, this.outputCanvas.width, this.outputCanvas.height);
380
- this.drawImageScaled(this.outputCanvas, this.outputCtx, this.uploadedImage);
381
 
382
- if (this.currentView === 'binary') {
383
- this.drawImageScaled(this.outputCanvas, this.outputCtx, this.predictedMask);
384
- return;
385
- }
386
-
387
- // Create a temporary canvas to work with the mask
388
- const tempCanvas = document.createElement('canvas');
389
- const tempCtx = tempCanvas.getContext('2d');
390
- tempCanvas.width = this.outputCanvas.width;
391
- tempCanvas.height = this.outputCanvas.height;
392
- tempCtx.drawImage(this.predictedMask, 0, 0, tempCanvas.width, tempCanvas.height);
393
-
394
- if (this.currentView === 'filled') {
395
- tempCtx.globalCompositeOperation = 'source-in';
396
- tempCtx.fillStyle = '#FF00FF'; // Magenta
397
- tempCtx.fillRect(0, 0, tempCanvas.width, tempCanvas.height);
398
-
399
- this.outputCtx.globalAlpha = this.transparencySlider.value / 100;
400
- this.outputCtx.drawImage(tempCanvas, 0, 0);
401
- this.outputCtx.globalAlpha = 1.0;
402
 
403
- } else if (this.currentView === 'outline') {
404
- const src = cv.imread(tempCanvas);
405
- const contours = new cv.MatVector();
406
- const hierarchy = new cv.Mat();
407
- // Convert to grayscale for findContours
408
- cv.cvtColor(src, src, cv.COLOR_RGBA2GRAY, 0);
409
- cv.findContours(src, contours, hierarchy, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE);
410
-
411
- // Draw contours directly onto the output canvas
412
- this.outputCtx.strokeStyle = 'white';
413
- this.outputCtx.lineWidth = 2;
414
- for (let i = 0; i < contours.size(); ++i) {
415
- const contour = contours.get(i).data32S; // Get contour points
416
- this.outputCtx.beginPath();
417
- this.outputCtx.moveTo(contour[0], contour[1]);
418
- for (let j = 2; j < contour.length; j += 2) {
419
- this.outputCtx.lineTo(contour[j], contour[j+1]);
420
- }
421
- this.outputCtx.closePath();
422
- this.outputCtx.stroke();
423
- }
424
- src.delete(); contours.delete(); hierarchy.delete();
425
  }
426
  }
427
-
428
- downloadMask() {
429
- if (!this.predictedMask) return;
430
- const link = document.createElement('a');
431
- link.download = 'binary_mask.png';
432
- link.href = this.predictedMask.src;
433
- link.click();
 
 
 
 
 
 
 
 
 
434
  }
435
  }
 
 
 
 
436
  </script>
437
  </body>
438
  </html>
 
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Leaf Disease Segmentation System</title>
7
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
8
  <style>
9
  :root {
10
  --primary: #4CAF50;
11
+ --primary-dark: #388E3C;
12
+ --secondary: #607D8B;
13
  --dark-bg: #121212;
14
+ --dark-surface: #1E1E1E;
15
  --text-primary: #E0E0E0;
16
  --text-secondary: #9E9E9E;
17
+ --border-color: #333;
18
+ --error: #F44336;
19
+ }
20
+ * {
21
+ margin: 0;
22
+ padding: 0;
23
+ box-sizing: border-box;
24
  }
 
25
  body {
26
+ font-family: 'Segoe UI', 'Roboto', sans-serif;
27
+ background: linear-gradient(135deg, #121212 0%, #1e1e2e 100%);
28
  color: var(--text-primary);
29
+ min-height: 100vh;
30
  padding: 20px;
31
  }
32
  .container {
33
  max-width: 1400px;
34
  margin: 0 auto;
35
+ background: rgba(30, 30, 46, 0.9);
36
+ border-radius: 24px;
37
+ box-shadow: 0 25px 50px rgba(0, 0, 0, 0.4);
38
+ overflow: hidden;
39
+ border: 1px solid rgba(255, 255, 255, 0.05);
40
  }
41
+ .header {
42
+ background: linear-gradient(90deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
43
+ padding: 40px;
44
  text-align: center;
45
+ }
46
+ .header h1 {
47
+ font-size: 2.8rem;
48
  font-weight: 300;
49
+ margin-bottom: 12px;
50
+ background: linear-gradient(90deg, #4c956c, #2f9b69);
51
+ -webkit-background-clip: text;
52
+ -webkit-text-fill-color: transparent;
53
+ }
54
+ .header p {
55
+ font-size: 1.3rem;
56
+ opacity: 0.85;
57
+ max-width: 800px;
58
+ margin: 0 auto;
59
  }
60
  .main-content {
61
  display: grid;
62
  grid-template-columns: 1fr 1fr;
63
+ gap: 40px;
64
+ padding: 50px;
65
  }
66
  .column {
67
+ background: rgba(42, 45, 71, 0.6);
68
+ border-radius: 20px;
69
+ padding: 35px;
70
+ border: 1px solid rgba(255, 255, 255, 0.08);
71
  }
72
  .column h2 {
73
+ color: #4c956c;
74
+ margin-bottom: 30px;
75
+ font-size: 1.8rem;
76
+ text-align: center;
77
+ padding-bottom: 15px;
78
+ border-bottom: 2px solid #4c956c;
79
  }
80
  .upload-area {
81
+ border: 2px dashed rgba(76, 149, 108, 0.5);
82
+ border-radius: 18px;
83
+ padding: 50px 30px;
84
  text-align: center;
 
 
85
  cursor: pointer;
86
+ transition: all 0.4s ease;
87
+ background: rgba(76, 149, 108, 0.05);
88
+ margin-bottom: 30px;
89
  }
90
+ .upload-area.dragover {
91
+ border-color: var(--primary);
92
+ background: rgba(76, 149, 108, 0.15);
93
+ transform: scale(1.02);
94
  }
95
+ .upload-icon {
96
+ font-size: 3.5rem;
97
+ color: var(--primary);
98
  margin-bottom: 20px;
99
+ }
100
+ .upload-text {
101
+ font-size: 1.4rem;
102
+ margin-bottom: 8px;
103
+ font-weight: 500;
104
+ }
105
+ .upload-subtext {
106
+ font-size: 1rem;
107
+ opacity: 0.7;
108
+ color: var(--text-secondary);
109
+ }
110
+ .file-input { display: none; }
111
+
112
+ /* FIX: Corrected image container and image scaling */
113
+ .image-display {
114
+ margin-top: 25px;
115
+ border-radius: 15px;
116
+ overflow: hidden;
117
+ height: 350px; /* Fixed height */
118
  display: flex;
 
119
  align-items: center;
120
+ justify-content: center;
121
+ background: rgba(0, 0, 0, 0.2);
122
  border: 1px solid var(--border-color);
123
  }
124
+ .image-display img {
125
  max-width: 100%;
126
+ max-height: 100%; /* Scales down to fit container */
127
  object-fit: contain;
128
+ border-radius: 12px;
 
 
 
 
 
 
129
  }
130
+
131
+ .placeholder {
 
 
 
132
  color: var(--text-secondary);
133
+ font-style: italic;
134
+ text-align: center;
135
+ padding: 40px 20px;
136
  }
137
+ .btn {
138
+ background: linear-gradient(45deg, #4c956c 0%, #2f9b69 100%);
139
+ color: white;
140
+ border: none;
141
+ padding: 16px 40px;
142
+ border-radius: 30px;
143
+ font-size: 1.1rem;
144
+ font-weight: 600;
145
  cursor: pointer;
146
  transition: all 0.3s ease;
147
+ margin-top: 25px;
148
+ width: 100%;
149
+ text-transform: uppercase;
150
+ letter-spacing: 1.2px;
151
+ box-shadow: 0 8px 25px rgba(76, 149, 108, 0.2);
152
+ }
153
+ .btn:hover:not(:disabled) {
154
+ transform: translateY(-3px);
155
+ box-shadow: 0 15px 40px rgba(76, 149, 108, 0.4);
156
+ }
157
+ .btn:disabled {
158
+ background: #555;
159
+ cursor: not-allowed;
160
+ box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
161
+ }
162
+ .loading {
163
+ display: none;
164
+ align-items: center;
165
+ justify-content: center;
166
+ gap: 15px;
167
+ margin-top: 25px;
168
+ color: var(--primary);
169
+ font-size: 1.1rem;
170
+ }
171
+ .spinner {
172
+ width: 24px;
173
+ height: 24px;
174
+ border: 2px solid rgba(76, 149, 108, 0.3);
175
+ border-top: 2px solid var(--primary);
176
+ border-radius: 50%;
177
+ animation: spin 1.2s linear infinite;
178
+ }
179
+ @keyframes spin {
180
+ 0% { transform: rotate(0deg); }
181
+ 100% { transform: rotate(360deg); }
182
+ }
183
  .download-btn {
184
+ background: linear-gradient(45deg, #ff6b6b 0%, #ee5a52 100%);
185
+ margin-top: 20px;
186
+ box-shadow: 0 8px 25px rgba(255, 107, 107, 0.25);
187
+ }
188
+ .download-btn:hover:not(:disabled) {
189
+ background: linear-gradient(45deg, #ee5a52 0%, #ff6b6b 100%);
190
+ box-shadow: 0 15px 40px rgba(255, 107, 107, 0.4);
191
+ }
192
+ .result-stats {
193
+ margin-top: 30px;
194
+ padding: 25px;
195
+ background: rgba(76, 149, 108, 0.1);
196
+ border-radius: 16px;
197
+ border-left: 4px solid var(--primary);
198
+ display: none;
199
+ animation: fadeIn 0.5s ease;
200
+ }
201
+ .result-stats h3 {
202
+ color: var(--primary);
203
+ margin-bottom: 12px;
204
+ font-weight: 500;
205
+ font-size: 1.3rem;
206
+ }
207
+ .result-stats p {
208
+ color: #c0c0c0;
209
+ }
210
+ .alert {
211
+ padding: 15px;
212
+ border-radius: 8px;
213
+ margin: 20px 0;
214
+ display: none;
215
+ animation: fadeIn 0.5s ease;
216
+ text-align: center;
217
+ }
218
+ .alert-success {
219
+ background-color: rgba(76, 175, 80, 0.2);
220
+ border: 1px solid var(--primary);
221
+ }
222
+ .alert-error {
223
+ background-color: rgba(244, 67, 54, 0.2);
224
+ border: 1px solid var(--error);
225
+ }
226
+ .footer {
227
+ padding: 25px;
228
+ text-align: center;
229
+ background: rgba(0, 0, 0, 0.2);
230
+ }
231
+ .footer a {
232
+ color: var(--primary);
233
+ text-decoration: none;
234
+ }
235
+ @keyframes fadeIn {
236
+ from { opacity: 0; transform: translateY(10px); }
237
+ to { opacity: 1; transform: translateY(0); }
238
  }
239
+ @media (max-width: 768px) {
240
+ .container { margin: 10px; }
241
+ .main-content { grid-template-columns: 1fr; padding: 20px; }
 
242
  }
243
  </style>
244
  </head>
245
  <body>
246
  <div class="container">
247
+ <div class="header">
248
+ <h1>Leaf Disease Segmentation System</h1>
249
+ <p>Advanced AI-powered analysis to detect and segment diseased areas in plant leaves with high precision</p>
250
+ </div>
251
+
252
  <div class="main-content">
253
  <div class="column">
254
+ <h2>Input Image</h2>
255
+
256
+ <div class="upload-area" id="uploadArea">
257
+ <div class="upload-icon">📷</div>
258
+ <div class="upload-text">Upload Leaf Image</div>
259
+ <div class="upload-subtext">Or drop an image here</div>
260
  </div>
261
+ <input type="file" id="fileInput" class="file-input" accept="image/*">
262
 
263
+ <div class="image-display" id="inputImageDisplay">
264
+ <div class="placeholder">No image selected. Upload an image to get started.</div>
265
  </div>
266
+
267
+ <div class="alert alert-success" id="successMessage"></div>
268
+ <div class="alert alert-error" id="errorMessage"></div>
269
+
270
+ <button class="btn" id="detectBtn" disabled>🔍 Analyze Image</button>
271
+
272
+ <div class="loading" id="loadingIndicator">
273
+ <div class="spinner"></div>
274
+ <span>Analyzing leaf image...</span>
275
  </div>
 
276
  </div>
277
+
278
  <div class="column">
279
+ <h2>Segmentation Results</h2>
280
+
281
+ <div class="image-display" id="outputImageDisplay">
282
+ <div class="placeholder">Segmentation results will appear here after analysis.</div>
 
 
 
 
283
  </div>
284
+
285
+ <button class="btn download-btn" id="downloadBtn" disabled>💾 Download Result</button>
286
+
287
+ <div class="result-stats" id="resultStats">
288
+ <h3>Analysis Summary</h3>
289
+ <p>Diseased areas have been identified and highlighted with colored overlays.</p>
290
  </div>
291
  </div>
292
  </div>
293
+
294
+ <div class="footer">
295
+ Built with ❤️ and AI • A project by Subh775
296
+ </div>
297
  </div>
298
 
 
299
  <script>
300
+ class LeafDiseaseSegmentation {
 
 
 
 
 
 
 
 
301
  constructor() {
302
+ this.uploadArea = document.getElementById('uploadArea');
303
+ this.fileInput = document.getElementById('fileInput');
304
+ this.inputImageDisplay = document.getElementById('inputImageDisplay');
305
+ this.outputImageDisplay = document.getElementById('outputImageDisplay');
306
+ this.detectBtn = document.getElementById('detectBtn');
307
+ this.downloadBtn = document.getElementById('downloadBtn');
308
+ this.loadingIndicator = document.getElementById('loadingIndicator');
309
+ this.successMessage = document.getElementById('successMessage');
310
+ this.errorMessage = document.getElementById('errorMessage');
311
+ this.resultStats = document.getElementById('resultStats');
312
+ this.currentImage = null;
313
+ this.resultImage = null;
314
+ this.initializeEventListeners();
 
 
 
 
 
315
  }
316
 
317
+ initializeEventListeners() {
318
+ // FIX: Simplified listeners for robust uploads
319
+ this.uploadArea.addEventListener('click', () => this.fileInput.click());
320
+ this.fileInput.addEventListener('change', (e) => this.handleFileSelect(e.target.files[0]));
 
 
 
321
 
322
+ this.uploadArea.addEventListener('dragover', (e) => {
 
 
323
  e.preventDefault();
324
+ this.uploadArea.classList.add('dragover');
325
+ });
326
+ this.uploadArea.addEventListener('dragleave', (e) => {
327
+ e.preventDefault();
328
+ this.uploadArea.classList.remove('dragover');
329
+ });
330
+ this.uploadArea.addEventListener('drop', (e) => {
331
+ e.preventDefault();
332
+ this.uploadArea.classList.remove('dragover');
333
+ const file = e.dataTransfer.files[0];
334
+ if (file && file.type.startsWith('image/')) {
335
+ this.handleFileSelect(file);
336
+ }
 
337
  });
 
 
 
 
338
 
339
+ this.detectBtn.addEventListener('click', () => this.processImage());
340
+ this.downloadBtn.addEventListener('click', () => this.downloadResult());
 
 
341
  }
342
 
343
+ handleFileSelect(file) {
344
+ if (!file || !file.type.startsWith('image/')) {
345
+ this.showError('Please select a valid image file (JPEG, PNG, etc.).');
346
+ return;
347
+ }
348
+ if (file.size > 10 * 1024 * 1024) { // 10MB limit
349
+ this.showError('File size exceeds 10MB. Please select a smaller image.');
350
+ return;
351
+ }
352
+
353
  const reader = new FileReader();
354
+ reader.onload = (e) => {
355
+ this.currentImage = e.target.result;
356
+ this.displayInputImage(this.currentImage);
357
+ this.detectBtn.disabled = false;
358
+ this.showSuccess('Image uploaded successfully!');
359
+ this.hideError();
 
 
 
 
 
 
 
360
  };
361
  reader.readAsDataURL(file);
362
  }
363
+
364
+ displayInputImage(imageSrc) {
365
+ this.inputImageDisplay.innerHTML = `<img src="${imageSrc}" alt="Input leaf image" />`;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
366
  }
367
+
368
+ async processImage() {
369
+ if (!this.currentImage) return;
370
+
371
+ this.setLoadingState(true);
372
+ this.hideMessages();
373
+
374
  try {
375
+ const payload = { image: this.currentImage };
 
 
 
376
  const response = await fetch('/predict', {
377
  method: 'POST',
378
  headers: { 'Content-Type': 'application/json' },
379
  body: JSON.stringify(payload)
380
  });
381
+ if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); }
382
  const result = await response.json();
383
+
384
+ if (result.result_image) {
385
+ this.resultImage = result.result_image;
386
+ this.displayOutputImage(this.resultImage);
387
  this.downloadBtn.disabled = false;
388
+ this.resultStats.style.display = 'block';
389
+ this.showSuccess('Disease detection completed successfully!');
390
+ } else {
391
+ throw new Error('No result image received from server');
392
+ }
393
  } catch (error) {
394
+ console.error('Error processing image:', error);
395
+ this.showError('Failed to process image. Please check your connection and try again.');
396
  } finally {
397
+ this.setLoadingState(false);
 
398
  }
399
  }
400
+
401
+ displayOutputImage(imageSrc) {
402
+ this.outputImageDisplay.innerHTML = `<img src="${imageSrc}" alt="Disease segmentation result" />`;
 
 
 
 
403
  }
 
 
 
 
 
 
 
404
 
405
+ downloadResult() {
406
+ if (!this.resultImage) return;
407
+ const link = document.createElement('a');
408
+ link.href = this.resultImage;
409
+ link.download = `leaf_disease_segmentation_${Date.now()}.png`;
410
+ document.body.appendChild(link);
411
+ link.click();
412
+ document.body.removeChild(link);
413
+ }
 
 
 
 
 
 
 
 
 
 
 
414
 
415
+ setLoadingState(isLoading) {
416
+ if (isLoading) {
417
+ this.loadingIndicator.style.display = 'flex';
418
+ this.detectBtn.disabled = true;
419
+ } else {
420
+ this.loadingIndicator.style.display = 'none';
421
+ this.detectBtn.disabled = false;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
422
  }
423
  }
424
+
425
+ showSuccess(message) {
426
+ this.successMessage.textContent = `✅ ${message}`;
427
+ this.successMessage.style.display = 'block';
428
+ setTimeout(() => { this.successMessage.style.display = 'none'; }, 3000);
429
+ }
430
+
431
+ showError(message) {
432
+ this.errorMessage.textContent = `❌ ${message}`;
433
+ this.errorMessage.style.display = 'block';
434
+ }
435
+
436
+ hideError() { this.errorMessage.style.display = 'none'; }
437
+ hideMessages() {
438
+ this.successMessage.style.display = 'none';
439
+ this.errorMessage.style.display = 'none';
440
  }
441
  }
442
+
443
+ document.addEventListener('DOMContentLoaded', () => {
444
+ new LeafDiseaseSegmentation();
445
+ });
446
  </script>
447
  </body>
448
  </html>