File size: 2,463 Bytes
8a35d1e
52841d3
 
8a35d1e
52841d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14f75be
 
 
 
 
 
 
 
 
 
 
8a35d1e
14f75be
 
38dfc37
14f75be
 
8a35d1e
14f75be
 
 
 
2a21aec
52841d3
 
 
8a35d1e
14f75be
 
 
 
 
 
 
8a35d1e
14f75be
 
 
 
bf9b284
14f75be
 
 
 
 
 
 
 
8a35d1e
14f75be
 
 
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
const video = document.querySelector("#videoElement");
let currentFacingMode = "user"; // "user" = front, "environment" = back
let stream = null;

// Initialize camera with given facing mode
function startCamera(facingMode = "user") {
  if (stream) {
    stream.getTracks().forEach(track => track.stop());
  }

  navigator.mediaDevices.getUserMedia({ video: { facingMode } })
    .then((newStream) => {
      stream = newStream;
      video.srcObject = stream;
    })
    .catch((err) => {
      console.error("Error accessing camera: ", err);
      alert("Camera access failed. Please allow camera permissions.");
    });
}

// Flip between front and back camera
function toggleCamera() {
  currentFacingMode = currentFacingMode === "user" ? "environment" : "user";
  startCamera(currentFacingMode);
}

// Capture current frame from video, show it, and send to backend
function captureAndSend() {
  if (!stream) {
    alert("Camera is not started yet!");
    return;
  }

  // Create a canvas to capture the frame
  const canvas = document.createElement('canvas');
  canvas.width = video.videoWidth;
  canvas.height = video.videoHeight;

  const context = canvas.getContext('2d');
  context.drawImage(video, 0, 0, canvas.width, canvas.height);

  // Convert canvas to data URL (JPEG)
  const dataUrl = canvas.toDataURL("image/jpeg");

  // Show captured image preview in <img>
  document.getElementById("snapshotImage").src = dataUrl;

  // Send captured image to backend
  fetch("https://prime810-facefeel.hf.space/process_image", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ image: dataUrl })
  })
  .then(response => response.json())
  .then(data => {
    // Display returned emoji and emotion
    document.getElementById("emojiDisplay").innerHTML =
      `<img src="${data.emoji}" style="width:200px; border-radius:10px;">`;
    document.getElementById("emotionText").textContent =
      `Emotion: ${data.emotion}`;

    // Show download button for emoji image
    const link = document.getElementById("downloadEmoji");
    link.href = data.emoji;
    link.style.display = "inline-block";

    // Play emoji sound if available
    const sound = document.getElementById("emojiSound");
    if (sound) sound.play();
  })
  .catch(error => {
    console.error("Error:", error);
    alert("Failed to get emoji. Is your backend running?");
  });
}

// Start camera automatically on page load
startCamera();