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 can I add custom color to color tracker ?! #333

Closed buglessir closed 5 years ago

buglessir commented 5 years ago

I'm trying to use trackingJS with a simple and flat image.

This is my source image : http://s9.picofile.com/file/8333543400/circles.jpg

And this my code to add green color :

`window.onload = function() { var img = document.getElementById('img'); var demoContainer = document.querySelector('.demo-container');

  var tracker = new tracking.ColorTracker(['magenta', 'cyan', 'yellow', 'green']);

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

  tracker.on('track', function(event) {
    event.data.forEach(function(rect) {
      window.plot(rect.x, rect.y, rect.width, rect.height, rect.color);
    });
  });

  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';
  };
};`

After this config I get this error :

Error: Color not valid, trynew tracking.ColorTracker("magenta").

I followed the main doc to add custom color, but what's the right way to adding new colors ?!

Thank you

buglessir commented 5 years ago

@eduardolundgren Hi, do you have any idea ? thanks

buglessir commented 5 years ago

This problem solved, because my color code was not equal by image color codes.