Spaces:
Sleeping
Sleeping
File size: 1,723 Bytes
8549b9c |
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 51 52 53 54 55 56 57 58 59 |
const webcam = document.getElementById("webcam");
const startBtn = document.getElementById("startBtn");
const stopBtn = document.getElementById("stopBtn");
const statusText = document.getElementById("status");
let stream;
let interval;
async function startWebcam() {
try {
stream = await navigator.mediaDevices.getUserMedia({ video: true });
webcam.srcObject = stream;
startBtn.disabled = true;
stopBtn.disabled = false;
statusText.textContent = "🎥 Webcam started";
interval = setInterval(captureAndSend, 2000); // every 2 seconds
} catch (err) {
console.error("Error accessing webcam:", err);
statusText.textContent = "❌ Webcam access denied";
}
}
function stopWebcam() {
if (stream) {
stream.getTracks().forEach(track => track.stop());
}
clearInterval(interval);
webcam.srcObject = null;
startBtn.disabled = false;
stopBtn.disabled = true;
statusText.textContent = "⏹️ Webcam stopped";
}
async function captureAndSend() {
const canvas = document.createElement("canvas");
canvas.width = 224;
canvas.height = 224;
const ctx = canvas.getContext("2d");
ctx.drawImage(webcam, 0, 0, 224, 224);
const dataUrl = canvas.toDataURL("image/jpeg");
try {
const response = await fetch("/predict", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ image: dataUrl }),
});
const result = await response.json();
statusText.textContent = `Prediction: ${result.label || "Error"}`;
} catch (err) {
console.error(err);
statusText.textContent = "⚠️ Prediction error";
}
}
startBtn.addEventListener("click", startWebcam);
stopBtn.addEventListener("click", stopWebcam);
|