Anm5 commited on
Commit
c5a3e32
·
verified ·
1 Parent(s): 40a9506

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +49 -12
script.js CHANGED
@@ -1,20 +1,57 @@
1
- // Capture image from webcam and send to backend
2
- async function recognizePose() {
3
- const video = document.querySelector('video');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  const canvas = document.createElement('canvas');
5
  canvas.width = video.videoWidth;
6
  canvas.height = video.videoHeight;
7
  canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height);
8
  const image = canvas.toDataURL('image/jpeg');
9
 
10
- const response = await fetch('https://your-space-name.hf.space/api/recognize_pose', {
11
- method: 'POST',
12
- headers: {
13
- 'Content-Type': 'application/json',
14
- },
15
- body: JSON.stringify({ image: image })
16
- });
 
 
 
 
 
17
 
18
- const result = await response.json();
19
- document.getElementById('output').innerText = result.pose || 'Pose not recognized';
 
 
 
 
20
  }
 
1
+ let timerInterval;
2
+ let capturing = false;
3
+
4
+ // Access the webcam
5
+ navigator.mediaDevices.getUserMedia({ video: true })
6
+ .then(stream => {
7
+ document.getElementById('webcam').srcObject = stream;
8
+ })
9
+ .catch(error => {
10
+ console.error('Error accessing the webcam', error);
11
+ });
12
+
13
+ // Function to start the timer for automatic image capture
14
+ function startTimer() {
15
+ if (!capturing) {
16
+ capturing = true;
17
+ timerInterval = setInterval(captureAndSendImage, 5000); // Capture image every 5 seconds
18
+ }
19
+ }
20
+
21
+ // Function to stop the timer
22
+ function stopTimer() {
23
+ if (capturing) {
24
+ capturing = false;
25
+ clearInterval(timerInterval);
26
+ }
27
+ }
28
+
29
+ // Function to capture image and send to backend
30
+ async function captureAndSendImage() {
31
+ const video = document.getElementById('webcam');
32
  const canvas = document.createElement('canvas');
33
  canvas.width = video.videoWidth;
34
  canvas.height = video.videoHeight;
35
  canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height);
36
  const image = canvas.toDataURL('image/jpeg');
37
 
38
+ try {
39
+ const response = await fetch('https://your-space-name.hf.space/api/recognize_pose', {
40
+ method: 'POST',
41
+ headers: {
42
+ 'Content-Type': 'application/json',
43
+ },
44
+ body: JSON.stringify({ image: image })
45
+ });
46
+
47
+ if (!response.ok) {
48
+ throw new Error(`HTTP error! status: ${response.status}`);
49
+ }
50
 
51
+ const result = await response.json();
52
+ document.getElementById('output').innerText = result.pose || 'Pose not recognized';
53
+ } catch (error) {
54
+ console.error('Error recognizing pose:', error);
55
+ document.getElementById('output').innerText = 'Error recognizing pose';
56
+ }
57
  }