FaceFeel / Frontend /scripts.js
Prime810's picture
Update Frontend/scripts.js
0e704d0 verified
raw
history blame
1.93 kB
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);
}
// Call this on page load
startCamera();
function captureAndSend() {
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);
const dataUrl = canvas.toDataURL("image/jpeg");
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 => {
document.getElementById("emojiDisplay").innerHTML =
`<img src="${data.emoji}" style="width:200px; border-radius:10px;">`;
document.getElementById("emotionText").textContent =
`Emotion: ${data.emotion}`;
// Show download button
const link = document.getElementById("downloadEmoji");
link.href = data.emoji;
link.style.display = "inline-block";
})
.catch(error => {
console.error("Error:", error);
alert("Failed to get emoji. Is your backend running?");
});
}