obniz developer team
Published

Buzzer alerts when a cat is caught on camera

It detects the object on the webcam and notifies you if it is a cat.

IntermediateFull instructions provided2 hours319
Buzzer alerts when a cat is caught on camera

Things used in this project

Hardware components

obniz
Cambrian Robotics obniz
×1
Buzzer
Buzzer
×1

Software apps and online services

TensorFlow
TensorFlow

Story

Read more

Code

Untitled file

HTML
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link
      rel="stylesheet"
      href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
    />
    <link rel="stylesheet" href="/css/starter-sample.css" />
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/coco-ssd"></script>
    <script src="https://obniz.io/js/jquery-3.2.1.min.js"></script>
    <script
      src="https://unpkg.com/obniz@3.x/obniz.js"
      crossorigin="anonymous"
    ></script>
    <style>
      #canvas {
        position: absolute;
        top: 0;
        left: 0;
        pointer-events: none;
      }

      #wrapper {
        position: relative;
      }
    </style>
  </head>
  <body>
    <div id="wrapper">
      <video id="video" width="800px" height="450px" autoplay="1"></video>
      <canvas id="canvas" width="800px" height="450px"></canvas>
    </div>

    <script>
      let canvas = document.getElementById("canvas");
      let ctx = canvas.getContext("2d");
      ctx.strokeStyle = "#00ff33";
      ctx.font = "18px 'serif'";
      const obniz = new Obniz("OBNIZ_ID_HERE");
      obniz.onconnect = async function() {
        let speaker = obniz.wired("Speaker", { signal: 0, gnd: 4 });
      
        bindPage();
      
        async function bindPage() {
          let video;
          try {
            video = await loadVideo();
          } catch (e) {
            console.error(e);
            return;
          }
          detection(video);
        }
      
        async function loadVideo() {
          const video = await setupCamera();
          video.play();
          return video;
        }
      
        async function setupCamera() {
          const video = document.getElementById("video");
          if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
            const stream = await navigator.mediaDevices.getUserMedia({
              audio: false,
              video: true
            });
            video.srcObject = stream;
      
            return new Promise(resolve => {
              video.onloadedmetadata = () => {
                resolve(video);
              };
            });
          } else {
            const errorMessage =
              "This browser does not support video capture, or this device does not have a camera";
            alert(errorMessage);
            return Promise.reject(errorMessage);
          }
        }
      
        function detection(video) {
          cocoSsd.load().then(model => {
            function judgeCat() {
              ctx.clearRect(0, 0, canvas.width, canvas.height);
              model.detect(video).then(predictions => {
                predictions.forEach(async element => {
                  draw(element);
                  console.log(element.class, element.score);
                  if (element.class == "cat") {
                    speaker.play(1000);
                    await obniz.wait(1000);
                    speaker.stop();
                  }
                });
              });
      
              setTimeout(arguments.callee, 1000);
            }
      
            judgeCat();
          });
        }
      
        function draw(element) {
          ctx.fillStyle = "#00ff33";
          ctx.strokeRect(
            element.bbox[0],
            element.bbox[1],
            element.bbox[2] / 2,
            element.bbox[3] / 2
          );
          ctx.fillRect(element.bbox[0], element.bbox[1] - 20, 100, 20);
          ctx.fillStyle = "#ff0000";
          ctx.fillText(element.class, element.bbox[0] + 5, element.bbox[1] - 5);
        }
      };

    </script>
  </body>
</html>

Github file

https://github.com/tensorflow/tfjs-models/tree/master/coco-ssd

Credits

obniz developer team
80 projects • 37 followers
Development board "obniz" is controlled via the internet.

Comments