eduardolundgren / tracking.js

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

Stop tracker using tracking.js - possible race condition? #171

Open pciazynski opened 7 years ago

pciazynski commented 7 years ago

It is possible to stop tracking in track event without timeout? I use colour tracker to control web application. When I move color over button I want to stop tracking, but unfortunately tracking.js is still running even when trackerTask.inRunning() shows false. When I add 100 ms timeout everything works fine and tracking.js stops tracking.

Tracker doesn't stop:

tracker.on('track', function (event) {
    event.data.forEach(function (rect) {
        console.log(rect);
        console.log(trackerTask.inRunning());
        trackerTask.stop();
    });
});

Work as expected - tracker successfully stops:

tracker.on('track', function (event) {
    event.data.forEach(function (rect) {
        console.log(rect);
        console.log(trackerTask.inRunning());
        setTimeout(function () {
            trackerTask.stop();
        }, 100);
    });
});

It is a bug? Is there a better solution than adding timeout? Any help appreciated :)

kudlohlavec commented 7 years ago

Very interesting, thanks for posting this - you've saved me hours of nerves :D . I was also calling stop method with no effect and continual tracking caused real performance drop of my website so it was very important for me to solve it... I assume that its bug, considering the fact that on official documentation page (https://trackingjs.com/docs.html#trackers) there's example for stopping of the tracker without timeout. What is more, even timeout of 1ms works for me so it is very strange.

tetreault commented 6 years ago

I know this is over a year old, but I'm using trackingJS for a project, calling trackerTask.stop(); does nothing for me. Furthermore, trying the timeout hack mentioned above yields no different results.

kudlohlavec commented 6 years ago

Just to make sure... are you assigning reference to tracking.track method into trackerTaskvariable during initialization of tracking?

tetreault commented 6 years ago

Thanks for the response, here's a snippet of the code @kudlohlavec

const tracker = new tracking.ObjectTracker('eye');
tracker.setInitialScale(4);
tracker.setStepSize(0.5);
tracker.setEdgesDensity(0);
const trackingTask = tracking.track('#video', tracker, { camera: true });
trackingTask.run();
// on tracker start, if we found eyes (event.data)
tracker.on('track', function(event) {
  if (debug) console.log(event);

  setTimeout(() => {
    trackingTask.stop();
  }, 5500);
});

Everything works except trackingTask.stop().

kudlohlavec commented 6 years ago

Ive tried your snippet and stopmethod works as it should be. However I was trying it with 'face' object tracker.

Try if the stop method is not working also in the face tracker for you. If it will be working it tells that the workaround does not work for eye tracker, otherwise there will be something wrong with the version of tracking.js that you are using (i was doing some changes to it but i dont recall that it was something crucial, also diff against official version from trackingjs.com does not yield anything regarding the stop method)

tetreault commented 6 years ago

thanks for looking into this @kudlohlavec i'll be sure to look into that later day when I have the time.

Just4Ease commented 6 years ago

From what I can see here:

tracker.on('track', ... // This line itself will never allow the timeout to initialise once. Because on every 'track' event the setTimeout is instantiated.