Update Frontend/scripts.js
Browse files- Frontend/scripts.js +75 -75
Frontend/scripts.js
CHANGED
@@ -1,76 +1,76 @@
|
|
1 |
-
const video = document.querySelector("#videoElement");
|
2 |
-
let currentFacingMode = "user"; // "user" = front, "environment" = back
|
3 |
-
let stream = null;
|
4 |
-
|
5 |
-
// Initialize camera with given facing mode
|
6 |
-
function startCamera(facingMode = "user") {
|
7 |
-
if (stream) {
|
8 |
-
stream.getTracks().forEach(track => track.stop());
|
9 |
-
}
|
10 |
-
|
11 |
-
navigator.mediaDevices.getUserMedia({ video: { facingMode } })
|
12 |
-
.then((newStream) => {
|
13 |
-
stream = newStream;
|
14 |
-
video.srcObject = stream;
|
15 |
-
})
|
16 |
-
.catch((err) => {
|
17 |
-
console.error("Error accessing camera: ", err);
|
18 |
-
alert("Camera access failed. Please allow camera permissions.");
|
19 |
-
});
|
20 |
-
}
|
21 |
-
|
22 |
-
// Flip between front and back camera
|
23 |
-
function toggleCamera() {
|
24 |
-
currentFacingMode = currentFacingMode === "user" ? "environment" : "user";
|
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 |
-
|
60 |
-
document.getElementById("emotionText").textContent =
|
61 |
-
Emotion: ${data.emotion}
|
62 |
-
// Show download button for emoji image
|
63 |
-
const link = document.getElementById("downloadEmoji");
|
64 |
-
link.href = data.emoji;
|
65 |
-
link.style.display = "inline-block";
|
66 |
-
// Play emoji sound if available
|
67 |
-
const sound = document.getElementById("emojiSound");
|
68 |
-
if (sound) sound.play();
|
69 |
-
})
|
70 |
-
.catch(error => {
|
71 |
-
console.error("Error:", error);
|
72 |
-
alert("Failed to get emoji. Is your backend running?");
|
73 |
-
});
|
74 |
-
}
|
75 |
-
// Start camera automatically on page load
|
76 |
startCamera();
|
|
|
1 |
+
const video = document.querySelector("#videoElement");
|
2 |
+
let currentFacingMode = "user"; // "user" = front, "environment" = back
|
3 |
+
let stream = null;
|
4 |
+
|
5 |
+
// Initialize camera with given facing mode
|
6 |
+
function startCamera(facingMode = "user") {
|
7 |
+
if (stream) {
|
8 |
+
stream.getTracks().forEach(track => track.stop());
|
9 |
+
}
|
10 |
+
|
11 |
+
navigator.mediaDevices.getUserMedia({ video: { facingMode } })
|
12 |
+
.then((newStream) => {
|
13 |
+
stream = newStream;
|
14 |
+
video.srcObject = stream;
|
15 |
+
})
|
16 |
+
.catch((err) => {
|
17 |
+
console.error("Error accessing camera: ", err);
|
18 |
+
alert("Camera access failed. Please allow camera permissions.");
|
19 |
+
});
|
20 |
+
}
|
21 |
+
|
22 |
+
// Flip between front and back camera
|
23 |
+
function toggleCamera() {
|
24 |
+
currentFacingMode = currentFacingMode === "user" ? "environment" : "user";
|
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 |
+
// Show download button for emoji image
|
63 |
+
const link = document.getElementById("downloadEmoji");
|
64 |
+
link.href = data.emoji;
|
65 |
+
link.style.display = "inline-block";
|
66 |
+
// Play emoji sound if available
|
67 |
+
const sound = document.getElementById("emojiSound");
|
68 |
+
if (sound) sound.play();
|
69 |
+
})
|
70 |
+
.catch(error => {
|
71 |
+
console.error("Error:", error);
|
72 |
+
alert("Failed to get emoji. Is your backend running?");
|
73 |
+
});
|
74 |
+
}
|
75 |
+
// Start camera automatically on page load
|
76 |
startCamera();
|