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

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +330 -341
index.html CHANGED
@@ -3,446 +3,435 @@
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>
 
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Interactive Leaf Segmentation</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
+ display: flex;
49
+ flex-direction: column;
50
  }
51
  .column h2 {
52
+ color: var(--primary);
53
+ margin-bottom: 20px;
54
+ font-size: 1.5em;
55
+ font-weight: 500;
56
  text-align: center;
 
 
57
  }
58
  .upload-area {
59
+ border: 2px dashed var(--primary);
60
+ border-radius: 8px;
61
+ padding: 30px;
62
  text-align: center;
63
+ margin-bottom: 20px;
64
+ transition: all 0.3s ease;
65
  cursor: pointer;
 
 
 
66
  }
67
+ .upload-area:hover, .upload-area.dragover {
68
+ background: rgba(76, 175, 80, 0.1);
69
+ border-color: #66BB6A;
 
70
  }
71
+ .canvas-container {
72
+ background: #000;
73
+ border-radius: 8px;
74
  margin-bottom: 20px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  display: flex;
 
76
  justify-content: center;
77
+ align-items: center;
78
+ height: 400px;
79
  border: 1px solid var(--border-color);
80
+ flex-shrink: 0;
81
  }
82
+ canvas {
83
  max-width: 100%;
84
+ max-height: 100%;
85
  object-fit: contain;
86
+ cursor: crosshair;
87
  }
88
+ .tools-panel, .output-controls {
89
+ background: rgba(0,0,0,0.2);
90
+ border-radius: 8px;
91
+ padding: 20px;
92
+ margin-bottom: 20px;
93
+ }
94
+ .tool-group { margin-bottom: 15px; }
95
+ .tool-group label {
96
+ display: block;
97
+ margin-bottom: 8px;
98
+ font-weight: 500;
99
  color: var(--text-secondary);
 
 
 
100
  }
101
+ .tool-buttons { display: flex; gap: 10px; }
102
+ .tool-btn {
103
+ background: rgba(76, 175, 80, 0.2);
104
+ color: var(--primary);
105
+ border: 1px solid var(--primary);
106
+ padding: 10px 16px;
107
+ border-radius: 6px;
 
108
  cursor: pointer;
109
  transition: all 0.3s ease;
110
+ flex: 1;
111
+ }
112
+ .tool-btn:hover { background: rgba(76, 175, 80, 0.3); }
113
+ .tool-btn.active { background: var(--primary); color: white; }
114
+ .predict-btn {
115
+ background: linear-gradient(135deg, #FF6B35, #F7931E);
116
+ color: white; border: none;
117
+ padding: 15px 30px; font-size: 18px; width: 100%;
118
+ border-radius: 8px; cursor: pointer; transition: all 0.3s ease;
119
+ }
120
+ .predict-btn:hover:not(:disabled) { transform: translateY(-2px); }
121
+ .predict-btn:disabled { background: #666; cursor: not-allowed; }
122
+ .output-tabs { display: flex; gap: 10px; margin-bottom: 20px; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
  .download-btn {
124
+ background: linear-gradient(135deg, #9C27B0, #673AB7);
125
+ color: white; border: none; padding: 12px 24px;
126
+ border-radius: 8px; cursor: pointer; width: 100%;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  }
128
+ .download-btn:disabled { background: #666; cursor: not-allowed; }
129
+ .hidden { display: none !important; }
130
+ @media (max-width: 900px) {
131
+ .main-content { grid-template-columns: 1fr; }
132
  }
133
  </style>
134
  </head>
135
  <body>
136
  <div class="container">
137
+ <h1>Interactive Leaf Segmentation</h1>
 
 
 
 
138
  <div class="main-content">
139
  <div class="column">
140
+ <h2>Input & Controls</h2>
141
+ <div class="upload-area" id="upload-area">
142
+ <p>Click or Drop Image Here</p>
 
 
 
143
  </div>
144
+ <input type="file" id="file-input" class="hidden" accept="image/*">
145
 
146
+ <div class="canvas-container">
147
+ <canvas id="input-canvas"></canvas>
148
  </div>
149
+ <div class="tools-panel">
150
+ <div class="tool-group">
151
+ <label>Annotation Tools</label>
152
+ <div class="tool-buttons">
153
+ <button class="tool-btn active" data-tool="point">Point (Dot)</button>
154
+ <button class="tool-btn" data-tool="line">Line/Drag</button>
155
+ </div>
156
+ </div>
157
+ <button class="tool-btn" id="clear-annotations" style="width:100%; margin-top:10px;">Clear Annotations</button>
158
  </div>
159
+ <button class="predict-btn" id="predict-btn" disabled>Predict</button>
160
  </div>
 
161
  <div class="column">
162
+ <h2>Output</h2>
163
+ <div class="output-tabs">
164
+ <button class="tool-btn active" id="view-filled">Filled</button>
165
+ <button class="tool-btn" id="view-outline">Outline</button>
166
+ <button class="tool-btn" id="view-binary">Binary Mask</button>
167
  </div>
168
+ <div class="canvas-container" id="output-container">
169
+ <canvas id="output-canvas"></canvas>
170
+ </div>
171
+ <div class="output-controls">
172
+ <div class="tool-group" id="transparency-control">
173
+ <label>Mask Transparency</label>
174
+ <input type="range" id="transparency" min="0" max="100" value="40" style="width:100%;">
175
+ </div>
176
+ <button class="download-btn" id="download-btn" disabled>Download Mask</button>
177
  </div>
178
  </div>
179
  </div>
 
 
 
 
180
  </div>
181
 
182
+ <script src="https://docs.opencv.org/4.8.0/opencv.js"></script>
183
  <script>
184
+ cv['onRuntimeInitialized'] = () => {
185
+ console.log('OpenCV.js is ready.');
186
+ document.addEventListener('DOMContentLoaded', () => {
187
+ new SegmentationApp();
188
+ });
189
+ };
190
+
191
+ class SegmentationApp {
192
  constructor() {
193
+ this.inputCanvas = document.getElementById('input-canvas');
194
+ this.inputCtx = this.inputCanvas.getContext('2d');
195
+ this.outputCanvas = document.getElementById('output-canvas');
196
+ this.outputCtx = this.outputCanvas.getContext('2d');
197
+ this.maskCanvas = document.createElement('canvas');
198
+ this.maskCtx = this.maskCanvas.getContext('2d');
199
+ this.currentTool = 'point';
200
+ this.brushSize = 8;
201
+ this.isDrawing = false;
202
+ this.uploadedImage = null;
203
+ this.predictedMask = null;
204
+ this.annotations = [];
205
+ this.currentView = 'filled';
206
+ this.predictBtn = document.getElementById('predict-btn');
207
+ this.downloadBtn = document.getElementById('download-btn');
208
+ this.transparencySlider = document.getElementById('transparency');
209
+ this.transparencyControl = document.getElementById('transparency-control');
210
+ this.initEventListeners();
211
  }
212
 
213
+ initEventListeners() {
214
+ const fileInput = document.getElementById('file-input');
215
+ const uploadArea = document.getElementById('upload-area');
216
+
217
+ uploadArea.onclick = () => fileInput.click();
218
+ fileInput.onchange = e => this.handleFile(e.target.files[0]);
219
 
220
+ uploadArea.ondragover = e => { e.preventDefault(); uploadArea.classList.add('dragover'); };
221
+ uploadArea.ondragleave = () => uploadArea.classList.remove('dragover');
222
+ uploadArea.ondrop = e => {
 
 
223
  e.preventDefault();
224
+ uploadArea.classList.remove('dragover');
225
+ this.handleFile(e.dataTransfer.files[0]);
226
+ };
227
+
228
+ this.inputCanvas.addEventListener('mousedown', e => this.startDrawing(e));
229
+ this.inputCanvas.addEventListener('mousemove', e => this.draw(e));
230
+ ['mouseup', 'mouseout'].forEach(evt => this.inputCanvas.addEventListener(evt, () => this.stopDrawing()));
231
+
232
+ document.querySelectorAll('.tool-btn[data-tool]').forEach(btn => {
233
+ btn.onclick = (e) => {
234
+ document.querySelectorAll('.tool-btn[data-tool]').forEach(b => b.classList.remove('active'));
235
+ e.target.classList.add('active');
236
+ this.currentTool = e.target.dataset.tool;
237
+ };
238
  });
239
+
240
+ document.getElementById('view-filled').onclick = () => this.setView('filled');
241
+ document.getElementById('view-outline').onclick = () => this.setView('outline');
242
+ document.getElementById('view-binary').onclick = () => this.setView('binary');
243
 
244
+ this.transparencySlider.oninput = () => this.renderOutput();
245
+ document.getElementById('clear-annotations').onclick = () => this.clearAnnotations();
246
+ this.predictBtn.onclick = () => this.predict();
247
+ this.downloadBtn.onclick = () => this.downloadMask();
248
  }
249
 
250
+ handleFile(file) {
251
+ if (!file || !file.type.startsWith('image/')) return;
 
 
 
 
 
 
 
 
252
  const reader = new FileReader();
253
+ reader.onload = e => {
254
+ const img = new Image();
255
+ img.onload = () => {
256
+ this.uploadedImage = img;
257
+ this.reset();
258
+ this.drawImageScaled(this.inputCanvas, this.inputCtx, img);
259
+ this.outputCanvas.width = this.inputCanvas.width;
260
+ this.outputCanvas.height = this.inputCanvas.height;
261
+ this.maskCanvas.width = this.inputCanvas.width;
262
+ this.maskCanvas.height = this.inputCanvas.height;
263
+ };
264
+ img.src = e.target.result;
265
  };
266
  reader.readAsDataURL(file);
267
  }
268
+
269
+ reset() {
270
+ this.annotations = [];
271
+ this.predictedMask = null;
272
+ this.predictBtn.disabled = true;
273
+ this.downloadBtn.disabled = true;
274
+ this.outputCtx.clearRect(0,0, this.outputCanvas.width, this.outputCanvas.height);
275
  }
276
+
277
+ drawImageScaled(canvas, ctx, img) {
278
+ const container = canvas.parentElement;
279
+ const hRatio = container.clientWidth / img.width;
280
+ const vRatio = container.clientHeight / img.height;
281
+ const ratio = Math.min(hRatio, vRatio);
282
+ canvas.width = img.width * ratio;
283
+ canvas.height = img.height * ratio;
284
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
285
+ ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
286
+ }
287
+
288
+ getCanvasCoordinates(e) {
289
+ const rect = this.inputCanvas.getBoundingClientRect();
290
+ return {
291
+ x: (e.clientX - rect.left) * (this.inputCanvas.width / rect.width),
292
+ y: (e.clientY - rect.top) * (this.inputCanvas.height / rect.height)
293
+ };
294
+ }
295
+
296
+ startDrawing(e) {
297
+ if (!this.uploadedImage) return;
298
+ this.isDrawing = true;
299
+ const coords = this.getCanvasCoordinates(e);
300
+ if (this.currentTool === 'point') this.drawOnCanvas(coords.x, coords.y);
301
+ else { this.lastX = coords.x; this.lastY = coords.y; }
302
+ }
303
+
304
+ draw(e) {
305
+ if (!this.isDrawing || this.currentTool !== 'line') return;
306
+ const coords = this.getCanvasCoordinates(e);
307
+ this.drawOnCanvas(this.lastX, this.lastY, coords.x, coords.y);
308
+ [this.lastX, this.lastY] = [coords.x, coords.y];
309
+ }
310
+
311
+ stopDrawing() { this.isDrawing = false; }
312
+
313
+ drawOnCanvas(x1, y1, x2 = null, y2 = null) {
314
+ [this.inputCtx, this.maskCtx].forEach((ctx, i) => {
315
+ ctx.beginPath();
316
+ ctx.lineWidth = this.brushSize * 2;
317
+ ctx.strokeStyle = (i === 0) ? '#4CAF50' : 'white';
318
+ ctx.fillStyle = (i === 0) ? '#4CAF50' : 'white';
319
+ ctx.lineCap = 'round';
320
+ if (x2 && y2) {
321
+ ctx.moveTo(x1, y1); ctx.lineTo(x2, y2); ctx.stroke();
322
+ } else {
323
+ ctx.arc(x1, y1, this.brushSize, 0, Math.PI * 2); ctx.fill();
324
+ }
325
+ });
326
+ this.annotations.push(true);
327
+ this.predictBtn.disabled = false;
328
+ }
329
+
330
+ clearAnnotations() {
331
+ if (!this.uploadedImage) return;
332
+ this.reset();
333
+ this.drawImageScaled(this.inputCanvas, this.inputCtx, this.uploadedImage);
334
+ this.maskCtx.clearRect(0, 0, this.maskCanvas.width, this.maskCanvas.height);
335
+ }
336
+
337
+ async predict() {
338
+ if (!this.uploadedImage || this.annotations.length === 0) return;
339
+ this.predictBtn.disabled = true; this.predictBtn.innerText = "Processing...";
340
  try {
341
+ const payload = {
342
+ image: this.inputCanvas.toDataURL('image/jpeg'),
343
+ scribble_mask: this.maskCanvas.toDataURL('image/png')
344
+ };
345
  const response = await fetch('/predict', {
346
  method: 'POST',
347
  headers: { 'Content-Type': 'application/json' },
348
  body: JSON.stringify(payload)
349
  });
350
+ if (!response.ok) throw new Error(`Server error: ${response.status}`);
351
  const result = await response.json();
352
+ const maskImg = new Image();
353
+ maskImg.onload = () => {
354
+ this.predictedMask = maskImg;
355
+ this.renderOutput();
356
  this.downloadBtn.disabled = false;
357
+ };
358
+ maskImg.src = result.predicted_mask;
 
 
 
359
  } catch (error) {
360
+ console.error('Prediction failed:', error);
361
+ alert('Prediction failed. Check the console for details.');
362
  } finally {
363
+ this.predictBtn.innerText = "Predict";
364
+ this.predictBtn.disabled = this.annotations.length === 0;
365
  }
366
  }
367
+
368
+ setView(view) {
369
+ this.currentView = view;
370
+ document.querySelectorAll('.output-tabs .tool-btn').forEach(b => b.classList.remove('active'));
371
+ document.getElementById(`view-${view}`).classList.add('active');
372
+ this.transparencyControl.style.display = (view === 'filled' || view === 'outline') ? 'block' : 'none';
373
+ this.renderOutput();
 
 
 
 
 
 
374
  }
375
+
376
+ renderOutput() {
377
+ if (!this.predictedMask) return;
378
+
379
+ // Always draw the original image on the output canvas first
380
+ this.outputCtx.clearRect(0,0, this.outputCanvas.width, this.outputCanvas.height);
381
+ this.drawImageScaled(this.outputCanvas, this.outputCtx, this.uploadedImage);
382
 
383
+ if (this.currentView === 'binary') {
384
+ this.drawImageScaled(this.outputCanvas, this.outputCtx, this.predictedMask);
385
+ return;
 
 
 
 
386
  }
387
+
388
+ // Create a temporary canvas to work with the mask for filled/outline views
389
+ const tempCanvas = document.createElement('canvas');
390
+ const tempCtx = tempCanvas.getContext('2d');
391
+ tempCanvas.width = this.outputCanvas.width;
392
+ tempCanvas.height = this.outputCanvas.height;
393
+ tempCtx.drawImage(this.predictedMask, 0, 0, tempCanvas.width, tempCanvas.height);
394
+
395
+ if (this.currentView === 'filled') {
396
+ tempCtx.globalCompositeOperation = 'source-in';
397
+ tempCtx.fillStyle = '#FF00FF'; // Magenta
398
+ tempCtx.fillRect(0, 0, tempCanvas.width, tempCanvas.height);
399
+
400
+ this.outputCtx.globalAlpha = this.transparencySlider.value / 100;
401
+ this.outputCtx.drawImage(tempCanvas, 0, 0);
402
+ this.outputCtx.globalAlpha = 1.0;
403
 
404
+ } else if (this.currentView === 'outline') {
405
+ const src = cv.imread(tempCanvas);
406
+ const contours = new cv.MatVector();
407
+ const hierarchy = new cv.Mat();
408
+ cv.cvtColor(src, src, cv.COLOR_RGBA2GRAY, 0);
409
+ cv.findContours(src, contours, hierarchy, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE);
410
+
411
+ this.outputCtx.strokeStyle = 'white';
412
+ this.outputCtx.lineWidth = 2;
413
+ for (let i = 0; i < contours.size(); ++i) {
414
+ const contour = contours.get(i).data32S;
415
+ this.outputCtx.beginPath();
416
+ this.outputCtx.moveTo(contour[0], contour[1]);
417
+ for (let j = 2; j < contour.length; j += 2) {
418
+ this.outputCtx.lineTo(contour[j], contour[j+1]);
419
+ }
420
+ this.outputCtx.closePath();
421
+ this.outputCtx.stroke();
422
+ }
423
+ src.delete(); contours.delete(); hierarchy.delete();
424
+ }
425
  }
426
+
427
+ downloadMask() {
428
+ if (!this.predictedMask) return;
429
+ const link = document.createElement('a');
430
+ link.download = 'binary_mask.png';
431
+ link.href = this.predictedMask.src;
432
+ link.click();
433
  }
434
  }
 
 
 
 
435
  </script>
436
  </body>
437
  </html>