File size: 1,154 Bytes
135a067
 
 
 
 
 
 
1282fd8
135a067
1282fd8
135a067
 
 
 
 
 
 
 
1282fd8
135a067
 
 
 
 
 
 
 
 
 
1282fd8
135a067
 
 
 
 
 
 
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
<!DOCTYPE html>
<html>
  <head>
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
    <script src="https://cdn.jsdelivr.net/npm/@teachablemachine/image"></script>
  </head>
  <body>
    <h2>Posture Detector</h2>
    <video id="webcam" autoplay playsinline width="400" height="300"></video>
    <p id="label">Loading model...</p>
    <script>
      const URL = "./";
      let model, webcam;

      async function init() {
        model = await tmImage.load(URL + "model.json", URL + "metadata.json");
        const video = document.getElementById("webcam");
        const labelEl = document.getElementById("label");

        navigator.mediaDevices.getUserMedia({ video: true }).then(stream => {
          video.srcObject = stream;
        });

        video.addEventListener("loadeddata", () => {
          setInterval(async () => {
            const prediction = await model.predict(video);
            labelEl.innerHTML = prediction
              .map(p => `${p.className}: ${(p.probability * 100).toFixed(1)}%`)
              .join("<br>");
          }, 300);
        });
      }

      init();
    </script>
  </body>
</html>