eduardolundgren / tracking.js

A modern approach for Computer Vision on the web
http://trackingjs.com
Other
9.43k stars 1.44k forks source link

How to count the number of times a color appear in a video? #305

Open FritzAgency opened 6 years ago

FritzAgency commented 6 years ago

Hello, nice work on the tracking.js library. I'm currently using the color camera for a project to detect 2 colors in a video and show the number of times they appear after the video stopped in a specific time frame. I'm in need of a guide to make this happen using this library. Thank you.

murat-aka commented 6 years ago

Need to explain bit more mate.

a. What will you detect b. Have you experienced with the library yet.

FritzAgency commented 6 years ago

@murat-aka

a. I want to calculate the total surface covered by a color(I'm experiencing with red color) throughout a period of time.

b. I have experienced with the library.

I hope you get what am trying to achieve.

murat-aka commented 6 years ago

You can get the height and width of the rectangle As below

event.data.forEach(function(rect) {
        console.log(rect.x, rect.y, rect.height, rect.width, rect.color);
      });
    }
  });

Set a global variable for count and area. When you detect an area count should go up

FritzAgency commented 6 years ago

@murat-aka Can you please give me a view on how to achieve your last sentence

Set a global variable for count and area. When you detect an area count should go up

  1. How do I set a global variable for count and area?

  2. How to show the real-time area count of the color?

  3. Will I change rect.color to my preferred color. If yes, how?

I will appreciate it If you can put me through.

Thank you!

murat-aka commented 6 years ago

Rect colour tells you what color is detected Try below. Though I haven't tested it.

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>tracking.js - color hello world</title>
  <link rel="stylesheet" href="assets/demo.css">

  <script src="../build/tracking-min.js"></script>

  <style>
  .rect {
    width: 80px;
    height: 80px;
    position: absolute;
    left: -1000px;
    top: -1000px;
  }
  </style>
</head>
<body>
  <div class="demo-title">
    <p><a href="http://trackingjs.com" target="_parent">tracking.js</a> - detect certain colors in a image</p>
  </div>

  <div class="demo-frame">
    <div id="counter_div">
        Area
    </div>
    <div class="demo-container">
      <img id="img" src="assets/psmove.png" />
    </div>

  </div>

  <script>
    window.onload = function() {
      var img = document.getElementById('img');
      var demoContainer = document.querySelector('.demo-container');
      var tracker = new tracking.ColorTracker(['magenta', 'cyan', 'yellow']);
      var count =0;
      var div_counter = document.getElementById('counter_div');

      tracker.on('track', function(event) {
         if (event.data.length === 0) {

             // No colors were detected in this frame
             div_counter.innerHTML= count;

         } else {
              count++;
              div_counter.innerHTML= count;
              event.data.forEach(function(rect) {
                  window.plot(rect.x, rect.y, rect.width, rect.height, rect.color);
              });
         }
      });

      //   });  Update, now tested, this code works, this line needed comment out, as it was preventing code running

      tracking.track('#img', tracker);
      window.plot = function(x, y, w, h, color) {
        var rect = document.createElement('div');
        document.querySelector('.demo-container').appendChild(rect);
        rect.classList.add('rect');
        rect.style.border = '2px solid ' + color;
        rect.style.width = w + 'px';
        rect.style.height = h + 'px';
        rect.style.left = (img.offsetLeft + x) + 'px';
        rect.style.top = (img.offsetTop + y) + 'px';
        // Real-time area;
        rect.innerHTML = h*w;
      };
    };
  </script>

</body>
</html>
FritzAgency commented 6 years ago

Here is the code. I'm currently using.

How can I apply it to what you posted above?

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>tracking.js - color with camera</title>
  <link rel="stylesheet" href="assets/demo.css">

  <script src="../build/tracking-min.js"></script>

  <script src="../build/build2/dat.gui.min.js"></script>
  <!--script src="../node_modules/dat.gui/build2/dat.gui.min.js"></script-->
  <script src="assets/stats.min.js"></script>
  <script src="assets/color_camera_gui.js"></script>

  <style>
  video, canvas {
    margin-left: 100px;
    margin-top: 35px;
    position: absolute;
  }
  </style>
</head>
<body>
  <div class="demo-title">
    <p><a href="http://trackingjs.com" target="_parent">tracking.js</a> - choose the colors you want to detect through the controls on the right</p>
  </div>

  <div class="demo-frame">
    <div class="demo-container">
      <video id="video" width="600" height="450" camera="true" preload autoplay loop muted controls></video>
      <canvas id="canvas" width="600" height="450"></canvas>
    </div>
  </div>

  <script>
    window.onload = function() {
      var video = document.getElementById('video');
      var canvas = document.getElementById('canvas');
      var context = canvas.getContext('2d');

      var tracker = new tracking.ColorTracker();

      tracking.track('#video', tracker, { camera: true });

      tracking.ColorTracker.registerColor('green', function(r, g, b) {
  if (r < 50 && g > 200 && b < 50) {
    return true;
  }
  return false;
});

   tracking.ColorTracker.registerColor('red', function(r, g, b) {
  if (r < 255 && g > 0 && b < 0) {
    return true;
  }
  return false;
});

      tracker.on('track', function(event) {
        context.clearRect(0, 0, canvas.width, canvas.height);

        event.data.forEach(function(rect) {
          if (rect.color === 'custom') {
            rect.color = tracker.customColor;
          }

          context.strokeStyle = rect.color;
          context.strokeRect(rect.x, rect.y, rect.width, rect.height);
          context.font = '11px Helvetica';
          context.fillStyle = "#fff";
          context.fillText('x: ' + rect.x + 'px', rect.x + rect.width + 5, rect.y + 11);
          context.fillText('y: ' + rect.y + 'px', rect.x + rect.width + 5, rect.y + 22);
        });
      });

     initGUIControllers(tracker);
    };
  </script>

</body>
</html>

@murat-aka Thanks for the help so far.

FritzAgency commented 6 years ago

@murat-aka Your example is using image but what I wanted to use is a video. I'm using color camera https://trackingjs.com/examples/color_camera.html