try1 / script.js
Anm5's picture
Update script.js
0c9d3a0 verified
Raw History Blame Contribute Delete
1.74 kB
let timerInterval;
let capturing = false;
// Access the webcam
navigator.mediaDevices.getUserMedia({ video: true })
.then(stream => {
document.getElementById('webcam').srcObject = stream;
})
.catch(error => {
console.error('Error accessing the webcam', error);
});
// Function to start the timer for automatic image capture
function startTimer() {
if (!capturing) {
capturing = true;
timerInterval = setInterval(captureAndSendImage, 5000); // Capture image every 5 seconds
}
}
// Function to stop the timer
function stopTimer() {
if (capturing) {
capturing = false;
clearInterval(timerInterval);
}
}
// Function to capture image and send to backend
async function captureAndSendImage() {
const video = document.getElementById('webcam');
const canvas = document.createElement('canvas');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height);
const image = canvas.toDataURL('image/jpeg');
try {
const response = await fetch('https://your-space-name.hf.space/api/recognize_pose', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ image: image })
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const result = await response.json();
document.getElementById('output').innerText = result.pose || 'Pose not recognized';
} catch (error) {
console.error('Error recognizing pose:', error);
document.getElementById('output').innerText = 'Error recognizing pose';
}
}