File size: 1,074 Bytes
254d321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const video = document.getElementById("webcam");
const canvas = document.getElementById("canvas");
const emotionText = document.getElementById("emotion");

async function setupCamera() {
    const stream = await navigator.mediaDevices.getUserMedia({ video: true });
    video.srcObject = stream;
}

setupCamera();

async function captureFrame() {
    const ctx = canvas.getContext("2d");
    ctx.drawImage(video, 0, 0, canvas.width, canvas.height);

    canvas.toBlob(async (blob) => {
        const formData = new FormData();
        formData.append("frame", blob, "frame.jpg");

        try {
            const res = await fetch("/detect_live", {
                method: "POST",
                body: formData
            });

            const data = await res.json();

            if (data.emotion) {
                emotionText.innerText = `Emotion: ${data.emotion}`;
            }
        } catch (err) {
            console.error(err);
        }
    }, "image/jpeg");
}

// send frame every 400ms
setInterval(captureFrame, 400);