Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions spec/Tracker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,21 @@ describe('Tracker', function () {
expect(Tracker.getJSONOfTrackedItems()[0].id).toBe(0);
});
});

describe('custom distance function', function() {
var distanceSpy;
beforeEach(function() {
distanceSpy = jasmine.createSpy('distance spy', Tracker.iouDistance);

Tracker.reset();
Tracker.setParams({ distanceFunc: distanceSpy });
detections.forEach((frame, frameNb) => {
Tracker.updateTrackedItemsWithNewFrame(frame, frameNb);
});
});

it('called the custom distance', function() {
expect(distanceSpy).toHaveBeenCalled();
});
});
});
44 changes: 24 additions & 20 deletions tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ var iouAreas = require('./utils').iouAreas

var DEBUG_MODE = false;

// Distance function
const iouDistance = function(item1, item2) {
// IOU distance, between 0 and 1
// The smaller the less overlap
var iou = iouAreas(item1, item2);

// Invert this as the KDTREESEARCH is looking for the smaller value
var distance = 1 - iou;

// If the overlap is iou < 0.95, exclude value
if(distance > (1 - params.iouLimit)) {
distance = KDTREESEARCH_LIMIT + 1;
}

return distance;
}

const params = {
// DEFAULT_UNMATCHEDFRAMES_TOLERANCE
// This the number of frame we wait when an object isn't matched before considering it gone
Expand All @@ -17,7 +34,9 @@ const params = {
// Setting this to false ensures the object will stick around at least
// unMatchedFramesTolerance frames, even if they could neven be matched in
// subsequent frames.
fastDelete: true
fastDelete: true,
// The function to use to determine the distance between to detected objects
distanceFunc: iouDistance
}

// A dictionary of itemTracked currently tracked
Expand All @@ -37,33 +56,18 @@ var keepAllHistoryInMemory = false;
var KDTREESEARCH_LIMIT = 10000;


// Distance function
const computeDistance = function(item1, item2) {
// IOU distance, between 0 and 1
// The smaller the less overlap
var iou = iouAreas(item1, item2);

// Invert this as the KDTREESEARCH is looking for the smaller value
var distance = 1 - iou;

// If the overlap is iou < 0.95, exclude value
if(distance > (1 - params.iouLimit)) {
distance = KDTREESEARCH_LIMIT + 1;
}

return distance;
}

exports.computeDistance = computeDistance;
exports.computeDistance = iouDistance;

exports.updateTrackedItemsWithNewFrame = function(detectionsOfThisFrame, frameNb) {

// A kd-tree containing all the itemtracked
// Need to rebuild on each frame, because itemTracked positions have changed
var treeItemsTracked = new kdTree(Array.from(mapOfItemsTracked.values()), computeDistance, ["x", "y", "w", "h"]);
var treeItemsTracked = new kdTree(Array.from(mapOfItemsTracked.values()), params.distanceFunc, ["x", "y", "w", "h"]);

// Contruct a kd tree for the detections of this frame
var treeDetectionsOfThisFrame = new kdTree(detectionsOfThisFrame, computeDistance, ["x", "y", "w", "h"]);
var treeDetectionsOfThisFrame = new kdTree(detectionsOfThisFrame, params.distanceFunc, ["x", "y", "w", "h"]);

// SCENARIO 1: itemsTracked map is empty
if(mapOfItemsTracked.size === 0) {
Expand Down Expand Up @@ -187,7 +191,7 @@ exports.updateTrackedItemsWithNewFrame = function(detectionsOfThisFrame, frameNb
// to existing trackedItems this avoids adding some double match of YOLO and bring down drasticly reassignments
if(mapOfItemsTracked.size > 0) { // Safety check to see if we still have object tracked (could have been deleted previously)
// Rebuild tracked item tree to take in account the new positions
treeItemsTracked = new kdTree(Array.from(mapOfItemsTracked.values()), computeDistance, ["x", "y", "w", "h"]);
treeItemsTracked = new kdTree(Array.from(mapOfItemsTracked.values()), params.distanceFunc, ["x", "y", "w", "h"]);
// console.log(`Nb new items Unmatched : ${matchedList.filter((isMatched) => isMatched === false).length}`)
matchedList.forEach(function(matched, index) {
// Iterate through unmatched new detections
Expand Down