manmohanai commited on
Commit
7608f4e
·
verified ·
1 Parent(s): dc1b818

still not working. lets start capturing expression on live video as we were doing before. - Follow Up Deployment

Browse files
Files changed (1) hide show
  1. index.html +70 -50
index.html CHANGED
@@ -29,6 +29,21 @@
29
  margin: 0 auto;
30
  }
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  @keyframes spin {
33
  0% { transform: rotate(0deg); }
34
  100% { transform: rotate(360deg); }
@@ -186,6 +201,9 @@
186
  stream.getTracks().forEach(track => track.stop());
187
  }
188
 
 
 
 
189
  try {
190
  stream = await navigator.mediaDevices.getUserMedia({
191
  video: {
@@ -195,6 +213,7 @@
195
  },
196
  audio: false
197
  });
 
198
 
199
  video.srcObject = stream;
200
  video.onloadedmetadata = () => {
@@ -209,39 +228,39 @@
209
 
210
  } catch (error) {
211
  console.error("Camera error:", error);
 
212
  statusElement.innerHTML = `
213
  <div class="bg-red-50 text-red-600 p-4 rounded-lg">
214
- <p>Camera access denied. Please allow camera permissions and refresh the page.</p>
 
 
 
 
 
 
 
 
 
215
  </div>
216
  `;
217
  }
218
  }
219
 
220
 
221
- // Capture photo and analyze emotions
222
- captureBtn.addEventListener('click', async () => {
223
- if (isCaptured) return;
224
-
225
- isCaptured = true;
226
- captureBtn.disabled = true;
227
 
228
- // Create canvas from video
229
- canvas.width = video.clientWidth;
230
- canvas.height = video.clientHeight;
231
- const context = canvas.getContext('2d');
232
- context.drawImage(video, 0, 0, canvas.width, canvas.height);
233
-
234
- // Hide video and show canvas
235
- video.classList.add('hidden');
236
- canvas.classList.remove('hidden');
237
-
238
- // Analyze the captured image
239
- const detection = await faceapi.detectSingleFace(canvas, new faceapi.TinyFaceDetectorOptions())
240
  .withFaceLandmarks()
241
  .withFaceExpressions();
242
 
243
- if (detection) {
244
- const expressions = detection.expressions;
 
 
 
 
245
  const primaryEmotion = Object.entries(expressions).reduce((a, b) => a[1] > b[1] ? a : b);
246
 
247
  // Display results
@@ -269,22 +288,30 @@
269
  });
270
 
271
  resultsElement.classList.remove('hidden');
272
- resetBtn.classList.remove('hidden');
273
 
274
- // Voice out the emotion tips
275
- const primaryEmotion = Object.entries(expressions).reduce((a, b) => a[1] > b[1] ? a : b)[0];
276
- const tips = emotionTips[primaryEmotion] || [
277
- "Your emotions are valid. Take a moment to acknowledge how you feel."
278
- ];
279
- const randomTip = tips[Math.floor(Math.random() * tips.length)];
280
- speak(`You seem ${primaryEmotion}. ${randomTip}. Here are some quick mood boosters: Take 3 deep breaths, drink some water, and stretch your body.`);
 
 
 
 
 
 
 
 
 
281
  } else {
282
- alert("No face detected in the captured image. Please try again.");
283
- resetApp();
284
  }
285
 
286
- captureBtn.disabled = false;
287
- });
288
 
289
  // Switch between front and back camera
290
  switchCameraBtn.addEventListener('click', () => {
@@ -292,22 +319,6 @@
292
  startCamera();
293
  });
294
 
295
- // Reset the app to initial state
296
- resetBtn.addEventListener('click', resetApp);
297
-
298
- function resetApp() {
299
- isCaptured = false;
300
- video.classList.remove('hidden');
301
- canvas.classList.add('hidden');
302
- resultsElement.classList.add('hidden');
303
- resetBtn.classList.add('hidden');
304
-
305
- // Clear canvas
306
- const context = canvas.getContext('2d');
307
- context.clearRect(0, 0, canvas.width, canvas.height);
308
-
309
- }
310
-
311
  // Speech synthesis
312
  function speak(text) {
313
  if ('speechSynthesis' in window && !isMuted) {
@@ -442,7 +453,16 @@
442
  });
443
 
444
  // Initialize the app
445
- document.addEventListener('DOMContentLoaded', init);
 
 
 
 
 
 
 
 
 
446
  </script>
447
  <p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=manmohanai/faceimotion" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
448
  </html>
 
29
  margin: 0 auto;
30
  }
31
 
32
+ #cameraStatus {
33
+ position: absolute;
34
+ top: 0;
35
+ left: 0;
36
+ right: 0;
37
+ bottom: 0;
38
+ display: flex;
39
+ flex-direction: column;
40
+ justify-content: center;
41
+ align-items: center;
42
+ background-color: rgba(0, 0, 0, 0.5);
43
+ z-index: 10;
44
+ color: white;
45
+ }
46
+
47
  @keyframes spin {
48
  0% { transform: rotate(0deg); }
49
  100% { transform: rotate(360deg); }
 
201
  stream.getTracks().forEach(track => track.stop());
202
  }
203
 
204
+ const cameraStatus = document.getElementById('cameraStatus');
205
+ cameraStatus.classList.remove('hidden');
206
+
207
  try {
208
  stream = await navigator.mediaDevices.getUserMedia({
209
  video: {
 
213
  },
214
  audio: false
215
  });
216
+ cameraStatus.classList.add('hidden');
217
 
218
  video.srcObject = stream;
219
  video.onloadedmetadata = () => {
 
228
 
229
  } catch (error) {
230
  console.error("Camera error:", error);
231
+ cameraStatus.classList.add('hidden');
232
  statusElement.innerHTML = `
233
  <div class="bg-red-50 text-red-600 p-4 rounded-lg">
234
+ <p>Camera error: ${error.message}</p>
235
+ <p class="mt-2">Please check:</p>
236
+ <ul class="list-disc pl-5 mt-1 space-y-1">
237
+ <li>Camera permissions are allowed</li>
238
+ <li>No other app is using the camera</li>
239
+ <li>Your device has a working camera</li>
240
+ </ul>
241
+ <button onclick="window.location.reload()" class="mt-3 bg-red-600 hover:bg-red-700 text-white px-4 py-2 rounded-md text-sm">
242
+ Try Again
243
+ </button>
244
  </div>
245
  `;
246
  }
247
  }
248
 
249
 
250
+ // Continuous face detection
251
+ async function detectFaces() {
252
+ if (video.paused || video.ended) return;
 
 
 
253
 
254
+ const detections = await faceapi.detectAllFaces(video, new faceapi.TinyFaceDetectorOptions())
 
 
 
 
 
 
 
 
 
 
 
255
  .withFaceLandmarks()
256
  .withFaceExpressions();
257
 
258
+ // Clear previous face boxes
259
+ document.querySelectorAll('.face-box').forEach(el => el.remove());
260
+
261
+ if (detections.length > 0) {
262
+ // Update results with first face detected
263
+ const expressions = detections[0].expressions;
264
  const primaryEmotion = Object.entries(expressions).reduce((a, b) => a[1] > b[1] ? a : b);
265
 
266
  // Display results
 
288
  });
289
 
290
  resultsElement.classList.remove('hidden');
291
+ updateEmotionSuggestions(expressions);
292
 
293
+ // Draw face boxes on video
294
+ const displaySize = { width: video.width, height: video.height };
295
+ const resizedDetections = faceapi.resizeResults(detections, displaySize);
296
+
297
+ resizedDetections.forEach(detection => {
298
+ const box = detection.detection.box;
299
+ const faceBox = document.createElement('div');
300
+ faceBox.className = 'face-box';
301
+ Object.assign(faceBox.style, {
302
+ width: `${box.width}px`,
303
+ height: `${box.height}px`,
304
+ left: `${box.x}px`,
305
+ top: `${box.y}px`
306
+ });
307
+ video.parentNode.appendChild(faceBox);
308
+ });
309
  } else {
310
+ resultsElement.classList.add('hidden');
 
311
  }
312
 
313
+ requestAnimationFrame(detectFaces);
314
+ }
315
 
316
  // Switch between front and back camera
317
  switchCameraBtn.addEventListener('click', () => {
 
319
  startCamera();
320
  });
321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
322
  // Speech synthesis
323
  function speak(text) {
324
  if ('speechSynthesis' in window && !isMuted) {
 
453
  });
454
 
455
  // Initialize the app
456
+ document.addEventListener('DOMContentLoaded', async () => {
457
+ await init();
458
+ // Start face detection loop once video is playing
459
+ video.addEventListener('play', () => {
460
+ // Set video dimensions for face detection
461
+ video.width = video.videoWidth;
462
+ video.height = video.videoHeight;
463
+ detectFaces();
464
+ });
465
+ });
466
  </script>
467
  <p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=manmohanai/faceimotion" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
468
  </html>