Spaces:
Runtime error
Runtime error
<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'); | |
let frameCount = 0; | |
// Access the webcam | |
navigator.mediaDevices.getUserMedia({ video: true }) | |
.then(stream => { | |
video.srcObject = stream; | |
setInterval(() => { | |
frameCount++; | |
// Only process every 3rd frame | |
if (frameCount % 3 === 0) { | |
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); // Adjust interval as needed | |
}) | |
.catch(error => { | |
console.error("Error accessing webcam: ", error); | |
}); | |
</script> | |
</body> | |
</html> | |