Update Frontend/scripts.js
Browse files- Frontend/scripts.js +30 -12
Frontend/scripts.js
CHANGED
|
@@ -8,14 +8,36 @@ function startCamera(facingMode = "user") {
|
|
| 8 |
stream.getTracks().forEach(track => track.stop());
|
| 9 |
}
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
.then((newStream) => {
|
| 13 |
stream = newStream;
|
| 14 |
video.srcObject = stream;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
})
|
| 16 |
.catch((err) => {
|
| 17 |
-
console.
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
});
|
| 20 |
}
|
| 21 |
|
|
@@ -32,7 +54,6 @@ function captureAndSend() {
|
|
| 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;
|
|
@@ -40,13 +61,10 @@ function captureAndSend() {
|
|
| 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" },
|
|
@@ -54,16 +72,15 @@ function captureAndSend() {
|
|
| 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 |
const link = document.getElementById("downloadEmoji");
|
| 64 |
link.href = data.emoji;
|
| 65 |
link.style.display = "inline-block";
|
| 66 |
-
|
| 67 |
const sound = document.getElementById("emojiSound");
|
| 68 |
if (sound) sound.play();
|
| 69 |
})
|
|
@@ -72,5 +89,6 @@ function captureAndSend() {
|
|
| 72 |
alert("Failed to get emoji. Is your backend running?");
|
| 73 |
});
|
| 74 |
}
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
| 8 |
stream.getTracks().forEach(track => track.stop());
|
| 9 |
}
|
| 10 |
|
| 11 |
+
const constraints = {
|
| 12 |
+
video: {
|
| 13 |
+
facingMode: { exact: facingMode }
|
| 14 |
+
}
|
| 15 |
+
};
|
| 16 |
+
|
| 17 |
+
navigator.mediaDevices.getUserMedia(constraints)
|
| 18 |
.then((newStream) => {
|
| 19 |
stream = newStream;
|
| 20 |
video.srcObject = stream;
|
| 21 |
+
|
| 22 |
+
// ✅ iOS fix: playsinline and manual play
|
| 23 |
+
video.setAttribute("playsinline", true);
|
| 24 |
+
video.play().catch(err => console.warn("Autoplay issue:", err));
|
| 25 |
})
|
| 26 |
.catch((err) => {
|
| 27 |
+
console.warn("Exact facingMode failed, trying fallback. Error:", err);
|
| 28 |
+
|
| 29 |
+
// Fallback if iOS/older browser doesn’t support exact
|
| 30 |
+
navigator.mediaDevices.getUserMedia({ video: true })
|
| 31 |
+
.then((fallbackStream) => {
|
| 32 |
+
stream = fallbackStream;
|
| 33 |
+
video.srcObject = stream;
|
| 34 |
+
video.setAttribute("playsinline", true);
|
| 35 |
+
video.play().catch(err => console.warn("Autoplay issue:", err));
|
| 36 |
+
})
|
| 37 |
+
.catch((error) => {
|
| 38 |
+
console.error("Error accessing camera:", error);
|
| 39 |
+
alert("Camera access failed. Please allow camera permissions.");
|
| 40 |
+
});
|
| 41 |
});
|
| 42 |
}
|
| 43 |
|
|
|
|
| 54 |
return;
|
| 55 |
}
|
| 56 |
|
|
|
|
| 57 |
const canvas = document.createElement('canvas');
|
| 58 |
canvas.width = video.videoWidth;
|
| 59 |
canvas.height = video.videoHeight;
|
|
|
|
| 61 |
const context = canvas.getContext('2d');
|
| 62 |
context.drawImage(video, 0, 0, canvas.width, canvas.height);
|
| 63 |
|
|
|
|
| 64 |
const dataUrl = canvas.toDataURL("image/jpeg");
|
| 65 |
|
|
|
|
| 66 |
document.getElementById("snapshotImage").src = dataUrl;
|
| 67 |
|
|
|
|
| 68 |
fetch("https://prime810-facefeel.hf.space/process_image", {
|
| 69 |
method: "POST",
|
| 70 |
headers: { "Content-Type": "application/json" },
|
|
|
|
| 72 |
})
|
| 73 |
.then(response => response.json())
|
| 74 |
.then(data => {
|
|
|
|
| 75 |
document.getElementById("emojiDisplay").innerHTML =
|
| 76 |
`<img src="${data.emoji}" style="width:200px; border-radius:10px;">`;
|
| 77 |
document.getElementById("emotionText").textContent =
|
| 78 |
`Emotion: ${data.emotion}`;
|
| 79 |
+
|
| 80 |
const link = document.getElementById("downloadEmoji");
|
| 81 |
link.href = data.emoji;
|
| 82 |
link.style.display = "inline-block";
|
| 83 |
+
|
| 84 |
const sound = document.getElementById("emojiSound");
|
| 85 |
if (sound) sound.play();
|
| 86 |
})
|
|
|
|
| 89 |
alert("Failed to get emoji. Is your backend running?");
|
| 90 |
});
|
| 91 |
}
|
| 92 |
+
|
| 93 |
+
// ✅ Start camera automatically on page load
|
| 94 |
+
startCamera();
|