Prime810 commited on
Commit
14f75be
·
verified ·
1 Parent(s): 32fd80a

Update Frontend/scripts.js

Browse files
Files changed (1) hide show
  1. Frontend/scripts.js +41 -31
Frontend/scripts.js CHANGED
@@ -25,45 +25,55 @@ function toggleCamera() {
25
  startCamera(currentFacingMode);
26
  }
27
 
28
- // Call this on page load
29
- startCamera();
 
 
 
 
 
 
 
 
 
30
 
31
- const canvas = document.createElement('canvas');
32
- canvas.width = video.videoWidth;
33
- canvas.height = video.videoHeight;
34
- const context = canvas.getContext('2d');
35
- context.drawImage(video, 0, 0, canvas.width, canvas.height);
36
- const dataUrl = canvas.toDataURL("image/jpeg");
37
 
38
- // Show captured image preview in <img>
39
- document.getElementById("snapshotImage").src = dataUrl;
40
 
41
-
42
- // Send to backend
 
 
43
  fetch("https://prime810-facefeel.hf.space/process_image", {
44
  method: "POST",
45
  headers: { "Content-Type": "application/json" },
46
  body: JSON.stringify({ image: dataUrl })
47
  })
48
- .then(response => response.json())
49
- .then(data => {
50
- document.getElementById("emojiDisplay").innerHTML =
51
- `<img src="${data.emoji}" style="width:200px; border-radius:10px;">`;
52
- document.getElementById("emotionText").textContent =
53
- `Emotion: ${data.emotion}`;
54
-
55
 
56
- // Show download button
57
- const link = document.getElementById("downloadEmoji");
58
- link.href = data.emoji;
59
- link.style.display = "inline-block";
60
 
61
- // Play emoji sound
62
- const sound = document.getElementById("emojiSound");
63
- if (sound) sound.play();
64
- })
65
- .catch(error => {
66
- console.error("Error:", error);
67
- alert("Failed to get emoji. Is your backend running?");
68
- });
69
  }
 
 
 
 
25
  startCamera(currentFacingMode);
26
  }
27
 
28
+ // Capture current frame from video, show it, and send to backend
29
+ function captureAndSend() {
30
+ if (!stream) {
31
+ alert("Camera is not started yet!");
32
+ return;
33
+ }
34
+
35
+ // Create a canvas to capture the frame
36
+ const canvas = document.createElement('canvas');
37
+ canvas.width = video.videoWidth;
38
+ canvas.height = video.videoHeight;
39
 
40
+ const context = canvas.getContext('2d');
41
+ context.drawImage(video, 0, 0, canvas.width, canvas.height);
 
 
 
 
42
 
43
+ // Convert canvas to data URL (JPEG)
44
+ const dataUrl = canvas.toDataURL("image/jpeg");
45
 
46
+ // Show captured image preview in <img>
47
+ document.getElementById("snapshotImage").src = dataUrl;
48
+
49
+ // Send captured image to backend
50
  fetch("https://prime810-facefeel.hf.space/process_image", {
51
  method: "POST",
52
  headers: { "Content-Type": "application/json" },
53
  body: JSON.stringify({ image: dataUrl })
54
  })
55
+ .then(response => response.json())
56
+ .then(data => {
57
+ // Display returned emoji and emotion
58
+ document.getElementById("emojiDisplay").innerHTML =
59
+ `<img src="${data.emoji}" style="width:200px; border-radius:10px;">`;
60
+ document.getElementById("emotionText").textContent =
61
+ `Emotion: ${data.emotion}`;
62
 
63
+ // Show download button for emoji image
64
+ const link = document.getElementById("downloadEmoji");
65
+ link.href = data.emoji;
66
+ link.style.display = "inline-block";
67
 
68
+ // Play emoji sound if available
69
+ const sound = document.getElementById("emojiSound");
70
+ if (sound) sound.play();
71
+ })
72
+ .catch(error => {
73
+ console.error("Error:", error);
74
+ alert("Failed to get emoji. Is your backend running?");
75
+ });
76
  }
77
+
78
+ // Start camera automatically on page load
79
+ startCamera();