Skip to content

Commit

Permalink
Merge pull request #41 from opendatacam/release-0.9.1
Browse files Browse the repository at this point in the history
Release 0.9.1
  • Loading branch information
vsaw authored Mar 1, 2022
2 parents 96a77c0 + 9c8db12 commit f5bab69
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 22 deletions.
27 changes: 23 additions & 4 deletions ItemTracked.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ var computeVelocityVector = require('./utils').computeVelocityVector
// "name": "car"
// }

/** The maximum length of the item history. */
exports.ITEM_HISTORY_MAX_LENGTH = 15;

// Use a simple incremental unique id for the display
var idDisplay = 0;

Expand Down Expand Up @@ -48,6 +51,9 @@ exports.ItemTracked = function(properties, frameNb, unMatchedFramesTolerance, fa
h: properties.h,
confidence: properties.confidence
});
if(itemTracked.itemHistory.length >= exports.ITEM_HISTORY_MAX_LENGTH) {
itemTracked.itemHistory.shift();
}
itemTracked.velocity = {
dx: 0,
dy: 0
Expand Down Expand Up @@ -79,6 +85,9 @@ exports.ItemTracked = function(properties, frameNb, unMatchedFramesTolerance, fa
h: this.h,
confidence: this.confidence
});
if(itemTracked.itemHistory.length >= exports.ITEM_HISTORY_MAX_LENGTH) {
itemTracked.itemHistory.shift();
}
this.name = properties.name;
if(this.nameCount[properties.name]) {
this.nameCount[properties.name]++;
Expand Down Expand Up @@ -124,6 +133,9 @@ exports.ItemTracked = function(properties, frameNb, unMatchedFramesTolerance, fa
h: this.h,
confidence: this.confidence
});
if(itemTracked.itemHistory.length >= exports.ITEM_HISTORY_MAX_LENGTH) {
itemTracked.itemHistory.shift();
}
this.x = this.x + this.velocity.dx
this.y = this.y + this.velocity.dy
}
Expand All @@ -142,11 +154,18 @@ exports.ItemTracked = function(properties, frameNb, unMatchedFramesTolerance, fa
}
// Velocity vector based on the last 15 frames
itemTracked.updateVelocityVector = function() {
var AVERAGE_NBFRAME = 15;
if(this.itemHistory.length <= AVERAGE_NBFRAME) {
return computeVelocityVector(this.itemHistory[0], this.itemHistory[this.itemHistory.length - 1], this.itemHistory.length);
if(exports.ITEM_HISTORY_MAX_LENGTH <= 2) {
return { dx: undefined, dy: undefined, }
}

if(this.itemHistory.length <= exports.ITEM_HISTORY_MAX_LENGTH) {
const start = this.itemHistory[0];
const end = this.itemHistory[this.itemHistory.length - 1];
return computeVelocityVector(start, end, this.itemHistory.length);
} else {
return computeVelocityVector(this.itemHistory[this.itemHistory.length - AVERAGE_NBFRAME], this.itemHistory[this.itemHistory.length - 1], AVERAGE_NBFRAME);
const start = this.itemHistory[this.itemHistory.length - exports.ITEM_HISTORY_MAX_LENGTH];
const end = this.itemHistory[this.itemHistory.length - 1];
return computeVelocityVector(start, end, exports.ITEM_HISTORY_MAX_LENGTH);
}
}

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-moving-things-tracker",
"version": "0.9.0",
"version": "0.9.1",
"description": "Tracker by detections in javascript for node.js / browsers",
"url": "https://github.com/opendatacam/node-moving-things-tracker",
"main": "main.js",
Expand Down
54 changes: 39 additions & 15 deletions spec/ItemTracked.spec.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,54 @@
const ItemTracked = require('../ItemTracked');

describe("ItemTracked", function () {
const properties = {
x: null,
y: null,
w: null,
h: null,
name: "dummy",
confidence: null
};

describe("idDisplay", function () {
const properties = {
x: null,
y: null,
w: null,
h: null,
name: "dummy",
confidence: null
};
const item1 = ItemTracked.ItemTracked(properties);
const item2 = ItemTracked.ItemTracked(properties);

it('starts at 0', function() {
expect(item1.idDisplay).toBe(0);
it('starts at 0', function () {
expect(item1.idDisplay).toBe(0);
});

it('icrements IDs', function() {
it('icrements IDs', function () {
expect(item1.idDisplay).toBeLessThan(item2.idDisplay);
});

it('resets IDs', function() {
ItemTracked.reset();
const item3 = ItemTracked.ItemTracked(properties);
expect(item3.idDisplay).toBe(0);
it('resets IDs', function () {
ItemTracked.reset();
const item3 = ItemTracked.ItemTracked(properties);
expect(item3.idDisplay).toBe(0);
});
});

describe('itemHistory', () => {
it('stops at max length', () => {
const item1 = ItemTracked.ItemTracked(properties);
for (var i = 0; i < ItemTracked.ITEM_HISTORY_MAX_LENGTH + 2; i++) {
item1.update(properties, i);
expect(item1.itemHistory.length).toBeLessThanOrEqual(ItemTracked.ITEM_HISTORY_MAX_LENGTH);
}
});

it('does not store history', () => {
const originalItemHistoryMaxLen = ItemTracked.ITEM_HISTORY_MAX_LENGTH;
ItemTracked.ITEM_HISTORY_MAX_LENGTH = 0;
const item1 = ItemTracked.ItemTracked(properties);

for (var i = 0; i < ItemTracked.ITEM_HISTORY_MAX_LENGTH + 2; i++) {
item1.update(properties, i);
expect(item1.itemHistory.length).toBe(ItemTracked.ITEM_HISTORY_MAX_LENGTH);
}

ItemTracked.ITEM_HISTORY_MAX_LENGTH = originalItemHistoryMaxLen;
});
});
});

0 comments on commit f5bab69

Please sign in to comment.