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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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Certificate Verifier - with Annotated Images</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
        }
        
        .header {
            text-align: center;
            color: white;
            margin-bottom: 30px;
        }
        
        .header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
        }
        
        .card {
            background: white;
            border-radius: 15px;
            padding: 30px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
            margin-bottom: 20px;
        }
        
        .upload-area {
            border: 3px dashed #667eea;
            border-radius: 10px;
            padding: 40px;
            text-align: center;
            cursor: pointer;
            transition: all 0.3s;
        }
        
        .upload-area:hover {
            border-color: #764ba2;
            background: #f8f9ff;
        }
        
        .upload-area.dragover {
            background: #e8ebff;
            border-color: #764ba2;
        }
        
        .btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            padding: 12px 30px;
            border-radius: 25px;
            font-size: 16px;
            cursor: pointer;
            transition: transform 0.2s;
            margin: 10px 5px;
        }
        
        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0,0,0,0.2);
        }
        
        .btn:disabled {
            opacity: 0.5;
            cursor: not-allowed;
        }
        
        .checkbox-group {
            margin: 20px 0;
            padding: 15px;
            background: #f8f9ff;
            border-radius: 10px;
        }
        
        .checkbox-group label {
            display: flex;
            align-items: center;
            font-size: 16px;
            cursor: pointer;
            margin: 10px 0;
        }
        
        .checkbox-group input[type="checkbox"] {
            width: 20px;
            height: 20px;
            margin-right: 10px;
            cursor: pointer;
        }
        
        .loading {
            display: none;
            text-align: center;
            padding: 20px;
        }
        
        .spinner {
            border: 4px solid #f3f3f3;
            border-top: 4px solid #667eea;
            border-radius: 50%;
            width: 50px;
            height: 50px;
            animation: spin 1s linear infinite;
            margin: 0 auto;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        .result {
            display: none;
            margin-top: 20px;
        }
        
        .result-authentic {
            background: #d4edda;
            border-left: 5px solid #28a745;
            padding: 20px;
            border-radius: 5px;
        }
        
        .result-fake {
            background: #f8d7da;
            border-left: 5px solid #dc3545;
            padding: 20px;
            border-radius: 5px;
        }
        
        .result-suspicious {
            background: #fff3cd;
            border-left: 5px solid #ffc107;
            padding: 20px;
            border-radius: 5px;
        }
        
        .image-container {
            margin-top: 20px;
        }
        
        .image-comparison {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
            margin-top: 20px;
        }
        
        .image-box {
            text-align: center;
        }
        
        .image-box h3 {
            margin-bottom: 10px;
            color: #333;
        }
        
        .image-box img {
            max-width: 100%;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
        }
        
        .details {
            margin-top: 20px;
            padding: 15px;
            background: #f8f9fa;
            border-radius: 5px;
        }
        
        .detail-row {
            display: flex;
            justify-content: space-between;
            padding: 8px 0;
            border-bottom: 1px solid #dee2e6;
        }
        
        .detail-row:last-child {
            border-bottom: none;
        }
        
        .legend {
            background: #f8f9ff;
            padding: 15px;
            border-radius: 10px;
            margin-top: 20px;
        }
        
        .legend h4 {
            margin-bottom: 10px;
            color: #667eea;
        }
        
        .legend-item {
            display: flex;
            align-items: center;
            margin: 8px 0;
        }
        
        .legend-color {
            width: 30px;
            height: 20px;
            border-radius: 3px;
            margin-right: 10px;
            border: 2px solid #333;
        }
        
        @media (max-width: 768px) {
            .image-comparison {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>πŸŽ“ Certificate Verifier</h1>
            <p>AI-Powered Certificate Authentication with Visual Seal Detection</p>
        </div>
        
        <div class="card">
            <div class="upload-area" id="uploadArea">
                <h2>πŸ“ Upload Certificate</h2>
                <p>Click to select or drag & drop your certificate image</p>
                <input type="file" id="fileInput" accept="image/*" style="display: none;">
            </div>
            
            <div class="checkbox-group">
                <label>
                    <input type="checkbox" id="returnImageCheck" checked>
                    <strong>Show Annotated Image</strong> - Display certificate with colored boxes around seals (🟒 Authentic / πŸ”΄ Fake)
                </label>
                <label>
                    <input type="checkbox" id="sealVerificationCheck" checked>
                    <strong>Enable Seal Verification</strong> - Use AI to detect and verify seals
                </label>
            </div>
            
            <div style="text-align: center;">
                <button class="btn" id="verifyBtn" disabled>πŸ” Verify Certificate</button>
                <button class="btn" id="clearBtn" style="background: #6c757d;">πŸ—‘οΈ Clear</button>
            </div>
            
            <div class="loading" id="loading">
                <div class="spinner"></div>
                <p style="margin-top: 15px;">Analyzing certificate...</p>
            </div>
            
            <div class="result" id="result"></div>
        </div>
        
        <div class="legend">
            <h4>πŸ“Š How to Read the Annotated Image:</h4>
            <div class="legend-item">
                <div class="legend-color" style="background-color: rgb(0, 255, 0);"></div>
                <span><strong>Green Box:</strong> Authentic seal detected with high confidence</span>
            </div>
            <div class="legend-item">
                <div class="legend-color" style="background-color: rgb(255, 0, 0);"></div>
                <span><strong>Red Box:</strong> Fake seal detected - certificate may be fraudulent</span>
            </div>
            <div class="legend-item">
                <div class="legend-color" style="background-color: rgb(255, 255, 0);"></div>
                <span><strong>Yellow Box:</strong> Suspicious seal - requires manual review</span>
            </div>
        </div>
    </div>
    
    <script>
        const uploadArea = document.getElementById('uploadArea');
        const fileInput = document.getElementById('fileInput');
        const verifyBtn = document.getElementById('verifyBtn');
        const clearBtn = document.getElementById('clearBtn');
        const loading = document.getElementById('loading');
        const result = document.getElementById('result');
        const returnImageCheck = document.getElementById('returnImageCheck');
        const sealVerificationCheck = document.getElementById('sealVerificationCheck');
        
        let selectedFile = null;
        let originalImageUrl = null;
        
        // Upload area click
        uploadArea.addEventListener('click', () => fileInput.click());
        
        // File selection
        fileInput.addEventListener('change', (e) => {
            if (e.target.files.length > 0) {
                selectedFile = e.target.files[0];
                originalImageUrl = URL.createObjectURL(selectedFile);
                uploadArea.innerHTML = `<p>βœ… Selected: ${selectedFile.name}</p>`;
                verifyBtn.disabled = false;
            }
        });
        
        // Drag and drop
        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');
            
            if (e.dataTransfer.files.length > 0) {
                selectedFile = e.dataTransfer.files[0];
                originalImageUrl = URL.createObjectURL(selectedFile);
                uploadArea.innerHTML = `<p>βœ… Selected: ${selectedFile.name}</p>`;
                verifyBtn.disabled = false;
            }
        });
        
        // Clear button
        clearBtn.addEventListener('click', () => {
            selectedFile = null;
            originalImageUrl = null;
            fileInput.value = '';
            uploadArea.innerHTML = `
                <h2>πŸ“ Upload Certificate</h2>
                <p>Click to select or drag & drop your certificate image</p>
            `;
            result.style.display = 'none';
            verifyBtn.disabled = true;
        });
        
        // Verify button
        verifyBtn.addEventListener('click', async () => {
            if (!selectedFile) return;
            
            // Show loading
            loading.style.display = 'block';
            result.style.display = 'none';
            verifyBtn.disabled = true;
            
            // Prepare form data
            const formData = new FormData();
            formData.append('files', selectedFile);
            
            // Build URL with parameters
            const returnImage = returnImageCheck.checked;
            const sealVerification = sealVerificationCheck.checked;
            const url = `http://localhost:8000/api/verify?return_image=${returnImage}&enable_seal_verification=${sealVerification}`;
            
            try {
                const response = await fetch(url, {
                    method: 'POST',
                    body: formData
                });
                
                const data = await response.json();
                
                // Hide loading
                loading.style.display = 'none';
                
                // Display results
                displayResults(data);
                
            } catch (error) {
                loading.style.display = 'none';
                result.innerHTML = `
                    <div class="result-fake">
                        <h3>❌ Error</h3>
                        <p>${error.message}</p>
                    </div>
                `;
                result.style.display = 'block';
            }
            
            verifyBtn.disabled = false;
        });
        
        function displayResults(data) {
            const decision = data.decision;
            let resultClass = 'result-suspicious';
            let icon = '⚠️';
            
            if (decision === 'AUTHENTIC') {
                resultClass = 'result-authentic';
                icon = 'βœ…';
            } else if (decision === 'FAKE') {
                resultClass = 'result-fake';
                icon = '❌';
            }
            
            let html = `
                <div class="${resultClass}">
                    <h2>${icon} ${decision}</h2>
                    <p><strong>Confidence:</strong> ${(data.confidence * 100).toFixed(1)}%</p>
                    <p><strong>Reason:</strong> ${data.reason}</p>
                    <p><strong>Processing Time:</strong> ${data.processing_time_seconds}s</p>
                </div>
            `;
            
            // Show image comparison if annotated image is available
            if (data.annotated_image_url) {
                html += `
                    <div class="image-comparison">
                        <div class="image-box">
                            <h3>Original Certificate</h3>
                            <img src="${originalImageUrl}" alt="Original">
                        </div>
                        <div class="image-box">
                            <h3>🎨 Annotated (Seal Detection)</h3>
                            <img src="${data.annotated_image_url}" alt="Annotated">
                        </div>
                    </div>
                `;
            } else if (originalImageUrl) {
                html += `
                    <div class="image-container">
                        <h3>Certificate Image</h3>
                        <img src="${originalImageUrl}" alt="Certificate" style="max-width: 100%; border-radius: 10px;">
                    </div>
                `;
            }
            
            // Show seal details
            const sealInfo = data.details?.seal_verification;
            if (sealInfo && sealInfo.total_seals > 0) {
                html += `
                    <div class="details">
                        <h3>πŸ” Seal Detection Details</h3>
                        <div class="detail-row">
                            <span>Total Seals:</span>
                            <strong>${sealInfo.total_seals}</strong>
                        </div>
                        <div class="detail-row">
                            <span>βœ… Authentic:</span>
                            <strong>${sealInfo.authentic_seals}</strong>
                        </div>
                        <div class="detail-row">
                            <span>❌ Fake:</span>
                            <strong>${sealInfo.fake_seals}</strong>
                        </div>
                        <div class="detail-row">
                            <span>Detection Method:</span>
                            <strong>${sealInfo.detection_method}</strong>
                        </div>
                    </div>
                `;
            }
            
            // Show cropped seals if available
            if (data.cropped_seals && data.cropped_seals.length > 0) {
                html += `
                    <div style="margin-top: 20px;">
                        <h3>πŸ“Έ Cropped Seals</h3>
                        <div style="display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 15px; margin-top: 15px;">
                `;
                
                data.cropped_seals.forEach(seal => {
                    const statusColor = seal.class === 'true' ? '#28a745' : seal.class === 'fake' ? '#dc3545' : '#ffc107';
                    const statusIcon = seal.class === 'true' ? 'βœ…' : seal.class === 'fake' ? '❌' : '⚠️';
                    const statusText = seal.class === 'true' ? 'Authentic' : seal.class === 'fake' ? 'Fake' : 'Suspicious';
                    
                    html += `
                        <div style="border: 3px solid ${statusColor}; border-radius: 10px; padding: 10px; text-align: center; background: white;">
                            <img src="${seal.image_url}" alt="Seal ${seal.seal_number}" style="max-width: 100%; border-radius: 5px;">
                            <div style="margin-top: 8px; font-weight: bold; color: ${statusColor};">
                                ${statusIcon} Seal ${seal.seal_number}
                            </div>
                            <div style="font-size: 12px; color: #666;">
                                ${statusText}<br>
                                ${(seal.confidence * 100).toFixed(1)}%
                            </div>
                        </div>
                    `;
                });
                
                html += `
                        </div>
                    </div>
                `;
            }
            
            result.innerHTML = html;
            result.style.display = 'block';
        }
    </script>
</body>
</html>