Update Frontend/scripts.js
Browse files- Frontend/scripts.js +41 -31
Frontend/scripts.js
CHANGED
@@ -25,45 +25,55 @@ function toggleCamera() {
|
|
25 |
startCamera(currentFacingMode);
|
26 |
}
|
27 |
|
28 |
-
//
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
const
|
32 |
-
canvas.width
|
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 |
-
//
|
39 |
-
|
40 |
|
41 |
-
|
42 |
-
|
|
|
|
|
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 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
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();
|