File size: 1,821 Bytes
2328c21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
45
46
47
48
49
50
<!-- templates/index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Webcam Stream</title>
</head>
<body>
    <h1>Webcam Stream</h1>
    <video id="video" width="640" height="480" autoplay></video>
    <img id="output" src="" alt="Processed Frame" />
    <script>
        const video = document.getElementById('video');
        const output = document.getElementById('output');

        // Access the webcam
        navigator.mediaDevices.getUserMedia({ video: true })
            .then(stream => {
                video.srcObject = stream;

                setInterval(() => {
                    const canvas = document.createElement('canvas');
                    canvas.width = 640;
                    canvas.height = 480;
                    const ctx = canvas.getContext('2d');
                    ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
                    canvas.toBlob(blob => {
                        const formData = new FormData();
                        formData.append('frame', blob, 'frame.jpg');

                        // Send the frame to the Flask server
                        fetch('/video_feed', {
                            method: 'POST',
                            body: formData
                        })
                        .then(response => response.blob())
                        .then(imageBlob => {
                            output.src = URL.createObjectURL(imageBlob);
                        });
                    }, 'image/jpeg', 0.95);
                }, 100); // Send every 100ms
            })
            .catch(error => {
                console.error("Error accessing webcam: ", error);
            });
    </script>
</body>
</html>