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3c8c6c
 
 
 
 
5e7efb7
 
b3c8c6c
 
 
5e7efb7
 
 
 
b3c8c6c
5e7efb7
b3c8c6c
 
 
5e7efb7
 
b3c8c6c
 
5e7efb7
 
 
 
 
 
 
 
b3c8c6c
5e7efb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9379e4
b3c8c6c
5e7efb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3c8c6c
 
5e7efb7
 
 
 
 
 
 
 
b3c8c6c
 
 
 
 
 
5e7efb7
a9379e4
5e7efb7
 
 
b3c8c6c
 
5e7efb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3c8c6c
5e7efb7
 
 
 
b3c8c6c
 
 
 
5e7efb7
 
 
 
 
 
 
 
 
 
 
b3c8c6c
 
 
 
5e7efb7
 
 
 
 
 
 
 
 
b3c8c6c
5e7efb7
 
 
 
a9379e4
5e7efb7
 
 
 
 
 
 
 
 
 
b3c8c6c
5e7efb7
b3c8c6c
 
5e7efb7
 
 
 
 
dc804ea
b3c8c6c
5e7efb7
 
 
 
 
 
 
 
 
 
b3c8c6c
 
5e7efb7
 
b3c8c6c
 
5e7efb7
 
b3c8c6c
 
5e7efb7
 
 
 
 
 
 
 
 
 
 
 
 
 
b3c8c6c
 
dc804ea
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>Tulsi Leaf Segmentation — RF-DETR</title>
    <style>
        :root {
            --primary: #E73562;
            --secondary: #EA782D;


            --dark-bg: #121212;
            --surface-bg: #1E1E2E;
            --text-primary: #E0E0E0;
            --text-secondary: #BDB2C6;
            --border-color: rgba(255, 255, 255, 0.06);
            --accent-green: #33ff99;
        }
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            min-height: 100vh;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: var(--surface-bg);
            border-radius: 14px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.35);
            border: 1px solid var(--border-color);
            overflow: hidden;



        }
        header {
            text-align: center;
            padding: 30px;
            background: rgba(0,0,0,0.18);
            border-bottom: 1px solid var(--border-color);
        }
        h1 {
            color: var(--primary);
            font-size: 2.2em;
            font-weight: 600;
            margin-bottom: 8px;


        }
        .header-note {
            font-size: 0.92rem;
            background-color: rgba(255, 59, 48, 0.06);
            color: var(--secondary);
            padding: 8px 12px;
            border-radius: 6px;
            display: inline-block;
            border: 1px solid rgba(255,59,48,0.08);
        }

        .main-content {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 26px;
            padding: 30px;
        }
        .column {




            background: rgba(0,0,0,0.15);
            border-radius: 12px;
            padding: 22px;
            display: flex;
            flex-direction: column;


        }
        .column h2 {
            color: var(--primary);
            margin-bottom: 16px;
            font-size: 1.4em;
            font-weight: 500;
            text-align: center;

        }

        .upload-area {
            border: 2px dashed var(--primary);
            border-radius: 8px;
            padding: 28px;
            text-align: center;
            margin-bottom: 18px;
            transition: all 0.2s ease;
            cursor: pointer;
            user-select: none;
        }
        .upload-area.dragover {
            background: rgba(255, 77, 166, 0.06);
            transform: translateY(-2px);
        }
        .upload-area p { color: var(--text-secondary); font-size: 1.05em; }
        .canvas-container {



            background: #000;
            border-radius: 8px;
            margin-bottom: 18px;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 420px;
            border: 1px solid var(--border-color);
            position: relative;
            overflow: hidden;
        }
        canvas, img {
            max-width: 100%;
            max-height: 520px;
            object-fit: contain;
            border-radius: 4px;
        }

        .controls-panel {
            background: rgba(0,0,0,0.18);
            border-radius: 8px;
            padding: 14px;
            margin-top: 6px;





        }
        .control-group { margin-bottom: 14px; }
        label { display:block; margin-bottom:6px; color:var(--text-secondary); font-size:0.95em; }
        .confidence-display { display:flex; justify-content:space-between; align-items:center; margin-bottom:8px; }
        .confidence-value { color: var(--primary); font-weight:700; font-size:1.05em; }

        input[type="range"] { width:100%; height:8px; border-radius:4px; background: rgba(255,255,255,0.06); outline:none; }
        input[type="range"]::-webkit-slider-thumb { -webkit-appearance:none; width:18px; height:18px; border-radius:50%; background:var(--primary); cursor:pointer; }
        input[type="range"]::-moz-range-thumb { width:18px; height:18px; border-radius:50%; background:var(--primary); cursor:pointer; }







        /* Toggle switches */
        .toggle-group {
            display: flex;
            gap: 16px;
            margin-bottom: 14px;
            padding: 10px;
            background: rgba(0,0,0,0.2);
            border-radius: 6px;




        }
        .toggle-item {
            flex: 1;



            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .toggle-label {
            color: var(--text-secondary);
            font-size: 0.9em;
            font-weight: 500;
        }
        .toggle-switch {






            position: relative;
            display: inline-block;
            width: 44px;
            height: 24px;




        }
        .toggle-switch input {
            opacity: 0;
            width: 0;
            height: 0;


        }
        .slider {

            position: absolute;
            cursor: pointer;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-color: rgba(255, 255, 255, 0.1);
            transition: .3s;
            border-radius: 24px;
        }
        .slider:before {
            position: absolute;
            content: "";
            height: 18px;
            width: 18px;
            left: 3px;
            bottom: 3px;
            background-color: white;
            transition: .3s;
            border-radius: 50%;


        }
        input:checked + .slider {
            background-color: var(--accent-green);
        }
        input:checked + .slider:before {
            transform: translateX(20px);
        }

        .action-btn {
            background: linear-gradient(135deg, var(--secondary), #FF7A5A);
            color: white;
            border: none;
            padding: 12px 20px;
            font-size: 1.02em;
            width: 100%;
            border-radius: 8px;
            cursor: pointer;
            transition: all 0.22s ease;
            margin-top: 6px;
            font-weight:600;
        }
        .action-btn:hover:not(:disabled) {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(234, 120, 45, 0.4);
        }
        .action-btn:disabled { background: linear-gradient(135deg,#444,#666); cursor:not-allowed; opacity:0.6; }

        .info-panel { background: rgba(0,0,0,0.14); border-radius:8px; padding:14px; margin-top:10px; }
        .detection-info { color: var(--text-secondary); font-size:0.95em; }
        .detection-count { color: var(--primary); font-weight:700; font-size:1.1em; margin-top:8px; }

        .download-btn {
            background: linear-gradient(135deg,#D63384,#8E2B57);
            color: white;
            border: none;
            padding: 10px 16px;
            border-radius: 8px;
            cursor: pointer;
            width: 100%;
            font-weight:600;
            transition: all 0.22s ease;
        }
        .download-btn:hover:not(:disabled) {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(214, 51, 132, 0.4);
        }
        .download-btn:disabled { opacity: 0.5; cursor: not-allowed; }
        
        @media (max-width: 900px) {
            .main-content { grid-template-columns: 1fr; }
            h1 { font-size: 1.8em; }
            .toggle-group { flex-direction: column; gap: 10px; }

        }
        .visually-hidden { position:absolute; left:-9999px; width:1px; height:1px; overflow:hidden; }

    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>Tulsi Leaf Segmentation — RF-DETR</h1>
            <p class="header-note">The model predicts leaves that are in focus and clearly visible in the input. <strong>Recommended:</strong>For best performance, provide a high-quality image</p>
        </header>

        <div class="main-content">
            <!-- Left column: upload & controls -->
            <div class="column">
                <h2>Input & Controls</h2>

                <div id="upload-area" class="upload-area">
                    <p>Click or drop an image here (jpg, png)</p>
                </div>
                <input id="file-input" class="visually-hidden" type="file" accept="image/*" />

                <div class="canvas-container">
                    <canvas id="input-canvas"></canvas>
                </div>

                <div class="controls-panel">
                    <div class="control-group">
                        <div class="confidence-display">
                            <label>Confidence Threshold</label>
                            <span class="confidence-value" id="conf-display">0.05</span>
                        </div>
                        <input id="conf-slider" type="range" min="0" max="100" value="5" step="1">
                    </div>

                    <div class="toggle-group">
                        <div class="toggle-item">
                            <span class="toggle-label">Show Labels</span>
                            <label class="toggle-switch">
                                <input type="checkbox" id="show-labels" checked>
                                <span class="slider"></span>
                            </label>
                        </div>
                        <div class="toggle-item">
                            <span class="toggle-label">Show Confidence</span>
                            <label class="toggle-switch">
                                <input type="checkbox" id="show-confidence" checked>
                                <span class="slider"></span>
                            </label>
                        </div>
                    </div>

                    <button id="predict-btn" class="action-btn" disabled>Detect Leaves</button>
                </div>
            </div>

            <!-- Right column: output & info -->
            <div class="column">
                <h2>Detection Results</h2>

                <div class="canvas-container">
                    <img id="annotated-img" src="" />









                </div>

                <div class="info-panel">
                    <div class="detection-info">
                        <p><strong>Status:</strong> <span id="status-text">Ready</span></p>
                        <p class="detection-count">Leaves Detected: <span id="leaf-count">0</span></p>
                        <p class="detection-info">Highest confidence: <strong id="best-conf">0.00</strong></p>
                        <button id="download-btn" class="download-btn" disabled>Download Annotated Image</button>


                    </div>
                </div>
            </div>
    </div>

    <script>
    (function(){
        const uploadArea = document.getElementById('upload-area');
        const fileInput = document.getElementById('file-input');
        const inputCanvas = document.getElementById('input-canvas');
        const inputCtx = inputCanvas.getContext('2d');
        const predictBtn = document.getElementById('predict-btn');
        const confSlider = document.getElementById('conf-slider');
        const confDisplay = document.getElementById('conf-display');
        const showLabelsToggle = document.getElementById('show-labels');
        const showConfidenceToggle = document.getElementById('show-confidence');
        const annotatedImg = document.getElementById('annotated-img');
        const statusText = document.getElementById('status-text');
        const leafCountEl = document.getElementById('leaf-count');
        const bestConfEl = document.getElementById('best-conf');
        const downloadBtn = document.getElementById('download-btn');

        let currentImage = null;
        let lastAnnotatedDataUrl = null;

        function setStatus(text) { statusText.textContent = text; }


        uploadArea.addEventListener('click', ()=> fileInput.click());
        uploadArea.addEventListener('dragover', (e)=>{ e.preventDefault(); uploadArea.classList.add('dragover'); });
        uploadArea.addEventListener('dragleave', ()=> uploadArea.classList.remove('dragover'));
        uploadArea.addEventListener('drop', (e)=>{
            e.preventDefault(); uploadArea.classList.remove('dragover');
            const f = e.dataTransfer.files && e.dataTransfer.files[0];
            if (f) handleFile(f);
        });

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

        function handleFile(file) {
            const reader = new FileReader();
            reader.onload = (ev) => {
                const img = new Image();
                img.onload = () => {
                    currentImage = img;
                    drawToCanvas(img);
                    predictBtn.disabled = false;
                    annotatedImg.src = "";
                    downloadBtn.disabled = true;
                    setStatus("Image ready");
                };
                img.src = ev.target.result;
            };
            reader.readAsDataURL(file);
        }

        function drawToCanvas(img) {
            const maxW = 600, maxH = 520;
            let w = img.width, h = img.height;
            const ratio = Math.min(maxW/w, maxH/h, 1);
            w = Math.round(w*ratio); h = Math.round(h*ratio);
            inputCanvas.width = w; inputCanvas.height = h;
            inputCtx.clearRect(0,0,w,h);
            inputCtx.drawImage(img, 0,0,w,h);
        }

        confSlider.addEventListener('input', ()=> {
            const val = confSlider.value / 100;
            confDisplay.textContent = val.toFixed(2);
        });

        predictBtn.addEventListener('click', async ()=>{
            if (!currentImage) return;
            predictBtn.disabled = true;
            setStatus("Processing...");
            
            const dataUrl = inputCanvas.toDataURL('image/jpeg', 0.9);
            const conf = confSlider.value / 100;
            const showLabels = showLabelsToggle.checked;
            const showConfidence = showConfidenceToggle.checked;
            
            try {
                const res = await fetch('/predict', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ 
                        image: dataUrl, 
                        conf: conf,
                        show_labels: showLabels,
                        show_confidence: showConfidence
                    })
                });
                if (!res.ok) throw new Error('Server error: ' + res.status);
                const j = await res.json();
                
                lastAnnotatedDataUrl = j.annotated;
                annotatedImg.src = lastAnnotatedDataUrl;
                leafCountEl.textContent = j.count || 0;
                const best = j.confidences && j.confidences.length ? Math.max(...j.confidences) : 0;
                bestConfEl.textContent = (best).toFixed(2);
                downloadBtn.disabled = false;
                setStatus("Done");
            } catch (err) {
                console.error(err);
                setStatus("Error: " + (err.message || err));
                alert('Prediction failed. See console for details.');
            } finally {
                predictBtn.disabled = false;


            }
        });

        downloadBtn.addEventListener('click', ()=>{
            if (!lastAnnotatedDataUrl) return;
            const a = document.createElement('a');
            a.href = lastAnnotatedDataUrl;
            a.download = `tulsi_segment_${Date.now()}.png`;
            document.body.appendChild(a);
            a.click();
            a.remove();
        });

        // Initialize
        setStatus('Ready');
    })();
    </script>
</body>
</html>