deniztas commited on
Commit
8e1bfbd
·
verified ·
1 Parent(s): 47beacf

after uploading image predictions is not showing - Follow Up Deployment

Browse files
Files changed (1) hide show
  1. index.html +91 -3
index.html CHANGED
@@ -22,6 +22,10 @@
22
  transform: scale(1.02);
23
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
24
  }
 
 
 
 
25
  </style>
26
  </head>
27
  <body class="bg-gradient-to-br from-gray-50 to-gray-100 min-h-screen">
@@ -48,9 +52,15 @@
48
  <h2 class="text-xl font-semibold text-gray-800">
49
  <i class="fas fa-camera text-blue-500 mr-2"></i> Live Feed
50
  </h2>
51
- <button id="startBtn" onclick="init()" class="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded-lg transition flex items-center">
52
- <i class="fas fa-play mr-2"></i> Start Camera
53
- </button>
 
 
 
 
 
 
54
  </div>
55
  <div id="webcam-container" class="webcam-feed bg-gray-200 w-full aspect-square flex items-center justify-center rounded-lg overflow-hidden">
56
  <div class="text-center p-4">
@@ -172,6 +182,84 @@
172
  }
173
 
174
  // run the webcam image through the image model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175
  async function predict() {
176
  if (!isRunning) return;
177
 
 
22
  transform: scale(1.02);
23
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
24
  }
25
+ .webcam-feed img {
26
+ max-width: 100%;
27
+ max-height: 100%;
28
+ }
29
  </style>
30
  </head>
31
  <body class="bg-gradient-to-br from-gray-50 to-gray-100 min-h-screen">
 
52
  <h2 class="text-xl font-semibold text-gray-800">
53
  <i class="fas fa-camera text-blue-500 mr-2"></i> Live Feed
54
  </h2>
55
+ <div class="flex gap-2">
56
+ <button id="startBtn" onclick="init()" class="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded-lg transition flex items-center">
57
+ <i class="fas fa-play mr-2"></i> Start Camera
58
+ </button>
59
+ <label for="fileUpload" class="bg-purple-500 hover:bg-purple-600 text-white px-4 py-2 rounded-lg transition flex items-center cursor-pointer">
60
+ <i class="fas fa-upload mr-2"></i> Upload Image
61
+ <input id="fileUpload" type="file" accept="image/*" class="hidden" onchange="handleImageUpload(this)">
62
+ </label>
63
+ </div>
64
  </div>
65
  <div id="webcam-container" class="webcam-feed bg-gray-200 w-full aspect-square flex items-center justify-center rounded-lg overflow-hidden">
66
  <div class="text-center p-4">
 
182
  }
183
 
184
  // run the webcam image through the image model
185
+ async function handleImageUpload(input) {
186
+ if (input.files && input.files[0]) {
187
+ const reader = new FileReader();
188
+
189
+ reader.onload = async function(e) {
190
+ const webcamContainer = document.getElementById('webcam-container');
191
+ webcamContainer.innerHTML = '';
192
+
193
+ const img = document.createElement('img');
194
+ img.src = e.target.result;
195
+ img.className = 'webcam-feed w-full h-full object-contain';
196
+ webcamContainer.appendChild(img);
197
+
198
+ // Stop webcam if running
199
+ if (isRunning) {
200
+ webcam.stop();
201
+ isRunning = false;
202
+ const startBtn = document.getElementById('startBtn');
203
+ startBtn.innerHTML = '<i class="fas fa-play mr-2"></i> Start Camera';
204
+ startBtn.classList.remove('bg-green-500', 'hover:bg-green-600');
205
+ startBtn.classList.add('bg-blue-500', 'hover:bg-blue-600');
206
+ startBtn.disabled = false;
207
+ }
208
+
209
+ // Load model if not already loaded
210
+ if (!model) {
211
+ try {
212
+ const modelURL = URL + "model.json";
213
+ const metadataURL = URL + "metadata.json";
214
+ model = await tmImage.load(modelURL, metadataURL);
215
+ maxPredictions = model.getTotalClasses();
216
+ } catch (error) {
217
+ console.error('Error loading model:', error);
218
+ return;
219
+ }
220
+ }
221
+
222
+ // Predict on the uploaded image
223
+ await predictOnImage(img);
224
+ }
225
+
226
+ reader.readAsDataURL(input.files[0]);
227
+ }
228
+ }
229
+
230
+ async function predictOnImage(imageElement) {
231
+ // Clear previous predictions
232
+ labelContainer = document.getElementById('label-container');
233
+ labelContainer.innerHTML = '';
234
+
235
+ // Create prediction elements
236
+ for (let i = 0; i < maxPredictions; i++) {
237
+ const predictionElement = document.createElement('div');
238
+ predictionElement.className = 'prediction-item';
239
+ labelContainer.appendChild(predictionElement);
240
+ }
241
+
242
+ // Predict
243
+ const prediction = await model.predict(imageElement);
244
+
245
+ for (let i = 0; i < maxPredictions; i++) {
246
+ const probability = prediction[i].probability.toFixed(2);
247
+ const percentage = Math.round(probability * 100);
248
+
249
+ const predictionElement = labelContainer.childNodes[i];
250
+ predictionElement.className = 'prediction-item mb-4';
251
+ predictionElement.innerHTML = `
252
+ <div class="flex justify-between items-center mb-1">
253
+ <span class="font-medium text-gray-700">${prediction[i].className}</span>
254
+ <span class="text-sm font-semibold ${percentage > 70 ? 'text-green-600' : percentage > 40 ? 'text-yellow-600' : 'text-gray-600'}">
255
+ ${percentage}%
256
+ </span>
257
+ </div>
258
+ <div class="prediction-bar" style="width: ${percentage}%"></div>
259
+ `;
260
+ }
261
+ }
262
+
263
  async function predict() {
264
  if (!isRunning) return;
265