eduardolundgren / tracking.js

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

Get the object type when using tracking.ObjectTracker #264

Open hay12396 opened 6 years ago

hay12396 commented 6 years ago

Hi guys,

So i just start using this great library and i can't find a way to identify which object am i identifying while looping trought the data.

For example, if i use

        var tracker = new tracking.ObjectTracker(['face', 'eye', 'mouth']);
        tracker.setStepSize(1.7);
        tracking.track('#img', tracker);

I am recieving an array of object of the following structure:

        height,
        total,
        width,
        x,
        y

and there is no type (face, eye, mouth). I was thinkin about doing this is steps (first track faces, than eyes, and than mouth) but was wondering if there is another way?

Best regards, Hay.

staus commented 6 years ago

I had the same issue. And on top of that I needed to know which element it was applied to as I ran it on multiple. I ended up modifying the script all the way through to pass out an object containing this info. Seems weird not to include it in the first place

murat-aka commented 6 years ago

The face has the largest area, then the mouth, then the eyes. Could help you identify

jesobreira commented 5 years ago

@staus perhaps you could ssend a PR? @murat-aka 's trick works in most of use-cases but not in pictures with multiple people in different distances from the camera. Of course it's possible to look for a big square with two smaller ones with most of their area inside it, but if the lib simply returns 'eye' or 'mouth' then that's obviously a lot easier.

nbpalomino commented 5 years ago

+1

staus commented 5 years ago

@jesobreira sorry, I'm not used to Githubs PR system and I'm a bit in a hurry, but I can drop the lines of code that should fix it here:

To run my version of the script you should call tracking.trackImg_(id, src, width, height, tracker)

NEW ID: I pass a unique ID through the tracking. In my case I ran the tracking on multiple images across a site simultaneously and I needed a way to identify which image the returned features were meant for so I put a random ID as a class on the image in the dom and passed the ID to the tracking script and could then target the exact image later when the tracking script returned. If you just need to track 1 image, just add some string here.

NEW src: This is just the source url of the image

NEW width / height: I also calculate the width and height in advance in the dom and feed it to the tracking script rather than letting the tracking script do it. (I believe) this was because that let me get "relative" widths to the size of the dom element rather than absolute width of the pictures real size (which sometimes is huge)

Changes to trackin.js

Line 187

-  tracking.trackCanvasInternal_ = function(canvas, element, tracker) {
+  tracking.trackCanvasInternal_ = function(canvas, id, tracker) {

Line 192

-    tracker.track(element, imageData.data, width, height);
+    tracker.track(id, imageData.data, width, height);

Line 205

-  tracking.trackImg_ = function(element, tracker) {
-    var width = element.width;
-    var height = element.height;
+  tracking.trackImg_ = function(id, src, width, height, tracker) {
+    // var width = element.width;
+    // var height = element.height;

Line 215

-      tracking.Canvas.loadImage(canvas, element.src, 0, 0, width, height, function() {
-        tracking.trackCanvasInternal_(canvas, element, tracker);
+      tracking.Canvas.loadImage(canvas, src, 0, 0, width, height, function() {
+        tracking.trackCanvasInternal_(canvas, id, tracker);

Line 2469

+      obj_classifiers = {}

Line 2473

-            opt_classifiers[i] = tracking.ViolaJones.classifiers[classifier];
+            obj_classifiers[classifier] = tracking.ViolaJones.classifiers[classifier];

Line 2482

-    this.setClassifiers(opt_classifiers);
+    this.setClassifiers(obj_classifiers);

Line 2563

-  tracking.ObjectTracker.prototype.track = function(element, pixels, width, height) {
+  tracking.ObjectTracker.prototype.track = function(id, pixels, width, height) {

Line 2571

-    var results = [];
-
-    classifiers.forEach(function(classifier) {
-      results = results.concat(tracking.ViolaJones.detect(pixels, width, height, self.getInitialScale(), self.getScaleFactor(), self.getStepSize(), self.getEdgesDensity(), classifier));
-    });
+    var results = {};
+    
+    for (var key in classifiers) {
+      results[key] = []
+      results[key] = results[key].concat(tracking.ViolaJones.detect(pixels, width, height, self.getInitialScale(), self.getScaleFactor(), self.getStepSize(), self.getEdgesDensity(), classifiers[key]));
+    };

Line 2579

-      element: element
+      element: id