File size: 11,816 Bytes
bd99505
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Multi-Model Vision App</title>
    <style>
        body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
        h1 { color: #3f51b5; text-align: center; }
        .container { display: flex; flex-direction: column; gap: 20px; }
        .card { border: 1px solid #ddd; border-radius: 8px; padding: 20px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
        .model-selection { display: flex; gap: 10px; margin: 20px 0; }
        .model-btn { padding: 10px 15px; border: none; border-radius: 4px; cursor: pointer; background-color: #e0e0e0; }
        .model-btn.active { background-color: #3f51b5; color: white; }
        .model-btn:disabled { opacity: 0.5; cursor: not-allowed; }
        .upload-container { display: flex; flex-direction: column; gap: 10px; }
        #file-input { margin-bottom: 10px; }
        #process-btn { padding: 10px 15px; background-color: #3f51b5; color: white; border: none; border-radius: 4px; cursor: pointer; }
        #process-btn:disabled { background-color: #9e9e9e; cursor: not-allowed; }
        .preview-container { display: flex; justify-content: center; margin: 20px 0; }
        .preview-image { max-width: 100%; max-height: 300px; border-radius: 4px; }
        .result-container { margin-top: 20px; }
        .result-image { max-width: 100%; max-height: 400px; border-radius: 4px; }
        .detection-list { list-style-type: none; padding: 0; }
        .detection-item { padding: 8px; border-bottom: 1px solid #eee; }
        .detection-item:last-child { border-bottom: none; }
        .confidence { display: inline-block; padding: 2px 6px; background-color: #3f51b5; color: white; border-radius: 10px; font-size: 0.8em; margin-left: 8px; }
        .performance { margin-top: 20px; font-size: 0.9em; color: #666; }
        .error { color: #f44336; font-weight: bold; }
        .loading { text-align: center; margin: 20px 0; }
    </style>
</head>
<body>
    <h1>Multi-Model Vision App</h1>
    
    <div class="container">
        <div class="card">
            <h2>Select Model</h2>
            <div class="model-selection">
                <button id="yolo-btn" class="model-btn">YOLOv8 (Detection)</button>
                <button id="detr-btn" class="model-btn">DETR (Detection)</button>
                <button id="vit-btn" class="model-btn">ViT (Classification)</button>
            </div>
        </div>
        
        <div class="card upload-container">
            <h2>Upload Image</h2>
            <input type="file" id="file-input" accept="image/*">
            <div class="preview-container" id="preview-container"></div>
            <button id="process-btn" disabled>Process Image</button>
        </div>
        
        <div class="card result-container" id="result-container" style="display: none;">
            <h2 id="result-title">Results</h2>
            <div id="result-content"></div>
        </div>
    </div>

    <script>
        // Model selection
        const yoloBtn = document.getElementById('yolo-btn');
        const detrBtn = document.getElementById('detr-btn');
        const vitBtn = document.getElementById('vit-btn');
        const fileInput = document.getElementById('file-input');
        const processBtn = document.getElementById('process-btn');
        const previewContainer = document.getElementById('preview-container');
        const resultContainer = document.getElementById('result-container');
        const resultTitle = document.getElementById('result-title');
        const resultContent = document.getElementById('result-content');
        
        let selectedModel = null;
        let selectedFile = null;
        let modelsStatus = { yolo: false, detr: false, vit: false };
        
        // Check API status
        async function checkApiStatus() {
            try {
                const response = await fetch('/api/status');
                const data = await response.json();
                modelsStatus = data.models;
                
                // Update UI based on model availability
                yoloBtn.disabled = !modelsStatus.yolo;
                detrBtn.disabled = !modelsStatus.detr;
                vitBtn.disabled = !modelsStatus.vit;
                
                if (!modelsStatus.yolo) yoloBtn.title = "YOLOv8 model not available";
                if (!modelsStatus.detr) detrBtn.title = "DETR model not available";
                if (!modelsStatus.vit) vitBtn.title = "ViT model not available";
            } catch (error) {
                console.error('Error checking API status:', error);
                alert('Error connecting to the API. Please make sure the server is running.');
            }
        }
        
        // Format time for display
        function formatTime(ms) {
            if (ms < 1000) return `${ms.toFixed(2)} ms`;
            return `${(ms / 1000).toFixed(2)} s`;
        }
        
        // Select model
        function selectModel(model) {
            selectedModel = model;
            yoloBtn.classList.remove('active');
            detrBtn.classList.remove('active');
            vitBtn.classList.remove('active');
            
            switch(model) {
                case 'yolo':
                    yoloBtn.classList.add('active');
                    break;
                case 'detr':
                    detrBtn.classList.add('active');
                    break;
                case 'vit':
                    vitBtn.classList.add('active');
                    break;
            }
            
            updateProcessButton();
        }
        
        // Update process button state
        function updateProcessButton() {
            processBtn.disabled = !selectedModel || !selectedFile;
        }
        
        // Handle file selection
        fileInput.addEventListener('change', (e) => {
            const file = e.target.files[0];
            if (file) {
                selectedFile = file;
                const reader = new FileReader();
                reader.onload = (e) => {
                    previewContainer.innerHTML = `<img src="${e.target.result}" class="preview-image" alt="Preview">`;
                };
                reader.readAsDataURL(file);
                updateProcessButton();
            } else {
                selectedFile = null;
                previewContainer.innerHTML = '';
                updateProcessButton();
            }
        });
        
        // Model selection event listeners
        yoloBtn.addEventListener('click', () => selectModel('yolo'));
        detrBtn.addEventListener('click', () => selectModel('detr'));
        vitBtn.addEventListener('click', () => selectModel('vit'));
        
        // Process image
        processBtn.addEventListener('click', async () => {
            if (!selectedModel || !selectedFile) return;
            
            resultContainer.style.display = 'block';
            resultTitle.textContent = `Processing with ${selectedModel.toUpperCase()}...`;
            resultContent.innerHTML = '<div class="loading">Processing image...</div>';
            
            const formData = new FormData();
            formData.append('image', selectedFile);
            
            let endpoint = '';
            switch(selectedModel) {
                case 'yolo':
                    endpoint = '/api/detect/yolo';
                    break;
                case 'detr':
                    endpoint = '/api/detect/detr';
                    break;
                case 'vit':
                    endpoint = '/api/classify/vit';
                    break;
            }
            
            try {
                const response = await fetch(endpoint, {
                    method: 'POST',
                    body: formData
                });
                
                if (!response.ok) {
                    throw new Error(`HTTP error! Status: ${response.status}`);
                }
                
                const data = await response.json();
                displayResults(selectedModel, data);
            } catch (error) {
                console.error('Error processing image:', error);
                resultTitle.textContent = 'Error';
                resultContent.innerHTML = `<div class="error">Error processing image: ${error.message}</div>`;
            }
        });
        
        // Display results
        function displayResults(model, data) {
            resultTitle.textContent = `${model.toUpperCase()} Results`;
            let html = '';
            
            if (model === 'yolo' || model === 'detr') {
                // Detection results
                html += `
                    <div style="display: flex; flex-direction: column; gap: 20px;">
                        <div>
                            <h3>Processed Image</h3>
                            <img src="data:image/jpeg;base64,${data.image_with_boxes}" class="result-image" alt="Detection Result">
                        </div>
                        <div>
                            <h3>Detected Objects</h3>
                `;
                            
                if (data.detections && data.detections.length > 0) {
                    html += '<ul class="detection-list">';
                    data.detections.forEach(item => {
                        html += `
                            <li class="detection-item">
                                ${item.label}
                                <span class="confidence">${(item.confidence * 100).toFixed(0)}%</span>
                            </li>
                        `;
                    });
                    html += '</ul>';
                } else {
                    html += '<p>No objects detected</p>';
                }
                
                html += `
                        </div>
                        <div class="performance">
                            <p>Inference Time: ${formatTime(data.inference_time)}</p>
                            <p>Total Processing Time: ${formatTime(data.total_time)}</p>
                        </div>
                    </div>
                `;
            } else if (model === 'vit') {
                // Classification results
                html += `
                    <div style="display: flex; flex-direction: column; gap: 20px;">
                        <div>
                            <h3>Classification Results</h3>
                `;
                            
                if (data.classifications && data.classifications.length > 0) {
                    html += '<ul class="detection-list">';
                    data.classifications.forEach(item => {
                        html += `
                            <li class="detection-item">
                                ${item.label}
                                <span class="confidence">${(item.confidence * 100).toFixed(1)}%</span>
                            </li>
                        `;
                    });
                    html += '</ul>';
                } else {
                    html += '<p>No classifications found</p>';
                }
                
                html += `
                        </div>
                        <div class="performance">
                            <p>Inference Time: ${formatTime(data.inference_time)}</p>
                            <p>Total Processing Time: ${formatTime(data.total_time)}</p>
                        </div>
                    </div>
                `;
            }
            
            resultContent.innerHTML = html;
        }
        
        // Initialize
        checkApiStatus();
    </script>
</body>
</html>