Update Frontend/scripts.js
Browse files- Frontend/scripts.js +31 -11
Frontend/scripts.js
CHANGED
@@ -1,12 +1,32 @@
|
|
1 |
const video = document.querySelector("#videoElement");
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
function captureAndSend() {
|
12 |
const canvas = document.createElement('canvas');
|
@@ -16,10 +36,10 @@ function captureAndSend() {
|
|
16 |
context.drawImage(video, 0, 0, canvas.width, canvas.height);
|
17 |
const dataUrl = canvas.toDataURL("image/jpeg");
|
18 |
|
19 |
-
fetch("https://prime810
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
})
|
24 |
.then(response => response.json())
|
25 |
.then(data => {
|
|
|
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 |
+
// Call this on page load
|
29 |
+
startCamera();
|
30 |
|
31 |
function captureAndSend() {
|
32 |
const canvas = document.createElement('canvas');
|
|
|
36 |
context.drawImage(video, 0, 0, canvas.width, canvas.height);
|
37 |
const dataUrl = canvas.toDataURL("image/jpeg");
|
38 |
|
39 |
+
fetch("https://prime810--facefeel.hf.space/process_image", {
|
40 |
+
method: "POST",
|
41 |
+
headers: { "Content-Type": "application/json" },
|
42 |
+
body: JSON.stringify({ image: dataUrl })
|
43 |
})
|
44 |
.then(response => response.json())
|
45 |
.then(data => {
|