forked from opendatacam/node-moving-things-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
executable file
·164 lines (133 loc) · 5.03 KB
/
Copy pathmain.js
File metadata and controls
executable file
·164 lines (133 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#! /usr/bin/env node
var fs = require("fs");
var Tracker = require('./tracker');
var parseArgs = require('minimist')
// Utilities for cleaning up detections input
var isInsideSomeAreas = require('./utils').isInsideSomeAreas;
var ignoreObjectsNotToDetect = require('./utils').ignoreObjectsNotToDetect;
var isDetectionTooLarge = require('./utils').isDetectionTooLarge;
// Export Tracker API to use as a node module
exports.Tracker = Tracker
var debugOutput = false;
// Parse CLI args
var args = parseArgs(process.argv.slice(2));
// Path to raw detections input
var pathRawDetectionsInput = args.input;
if(args.debug) {
console.log('debug mode');
// Running in debug mode output full json
debugOutput = true;
}
// If input path not specified abort
if(!pathRawDetectionsInput) {
console.error('Please specify the path to the raw detections file');
return;
}
// Compute the output file path
// In the same folder with the name tracker.json
var arrayTemp = pathRawDetectionsInput.split('/')
arrayTemp.pop()
var pathToTrackerOutput = `${arrayTemp.join('/')}/tracker.json`
console.log(`Tracker data will be written here: ${pathToTrackerOutput}`);
// Specific mode for beat the traffic game
var MODE_BEATTHETRAFFIC = args.mode === "beatthetraffic";
var BUS_AS_TRUCKS = args.busastruck;
var PERSON_AS_MOTORBIKE = args.personasmotorbike;
var LARGEST_DETECTION_ALLOWED = 1920 * 25 / 100;
var DETECT_LIST = ["bicycle", "car", "motorbike", "bus", "truck", "person"];
var TRACKED_LIST = ["car", "motorbike", "truck"]
var IGNORED_AREAS = []; // example: [{"x":634,"y":1022,"w":192,"h":60},{"x":1240,"y":355,"w":68,"h":68}
// Store detections input
var detections = {}
// Store tracker output
var tracker = {}
// If MODE is BEATTHETRAFFIC keep all tracker history in memory
if(MODE_BEATTHETRAFFIC) {
Tracker.enableKeepInMemory();
}
// Parse detections input
fs.readFile(`${pathRawDetectionsInput}`, function(err, f){
var lines = f.toString().split('\n');
lines.forEach(function(l) {
try {
var detection = JSON.parse(l);
detections[detection.frame] = detection.detections;
} catch (e) {
console.log('Error parsing line');
console.log(l);
}
});
Object.keys(detections).forEach(function(frameNb) {
let detectionsForThisFrame = detections[frameNb]
if(MODE_BEATTHETRAFFIC) {
// Remove unwanted areas
detectionsForThisFrame = detectionsForThisFrame.filter((detection) => !isInsideSomeAreas(IGNORED_AREAS, detection));
// Remove unwanted items
detectionsForThisFrame = ignoreObjectsNotToDetect(detectionsForThisFrame, DETECT_LIST);
// Remove objects too big
detectionsForThisFrame = detectionsForThisFrame.filter((detection) => !isDetectionTooLarge(detection, LARGEST_DETECTION_ALLOWED));
}
Tracker.updateTrackedItemsWithNewFrame(detectionsForThisFrame, parseInt(frameNb, 10))
tracker[frameNb] = Tracker.getJSONOfTrackedItems();
});
var allTrackedItems = Tracker.getAllTrackedItems();
if(MODE_BEATTHETRAFFIC) {
// Overwrite name for each id with mostly matched name to avoid having
// tracked item that change types
// For each frame
Object.keys(tracker).map((frameNb) => {
tracker[frameNb] = tracker[frameNb].map((trackedItem) => {
// Find
var item = allTrackedItems.get(trackedItem.id)
var mostlyMatchedName = trackedItem.name;
if(item) {
mostlyMatchedName = item.getMostlyMatchedName()
}
if(BUS_AS_TRUCKS) {
if(mostlyMatchedName === "bus") {
// console.log('Change bus to truck');
mostlyMatchedName = "truck"
}
}
if(PERSON_AS_MOTORBIKE) {
if(mostlyMatchedName === "person") {
// console.log('Change person to motorbike');
mostlyMatchedName = "motorbike"
}
}
if(debugOutput) {
// console.log(trackedItem);
return {
...trackedItem,
name : mostlyMatchedName
}
} else {
// If not debug, excude some fields
// Ugly, would need an getJSON() as a utility function to avoid quote duplication
return {
id: trackedItem.idDisplay,
x: trackedItem.x,
y: trackedItem.y,
w: trackedItem.w,
h: trackedItem.h,
name: mostlyMatchedName,
bearing: trackedItem.bearing
}
}
})
})
// Remove the tracked item with the name that we won't render as clickable
Object.keys(tracker).map((frameNb) => {
tracker[frameNb] = tracker[frameNb].filter((trackedItem) => {
if(TRACKED_LIST.indexOf(trackedItem.name) > -1) {
return true
} else {
return false
}
});
});
}
fs.writeFile(`${pathToTrackerOutput}`, JSON.stringify(tracker), function() {
console.log('Output tracker data wrote');
});
});