File size: 1,740 Bytes
533bcaa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c9d3a0
 
 
 
 
 
 
533bcaa
 
 
 
 
 
 
 
 
 
 
 
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
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';
    }
}