supervision 0.1.4 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +12 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +864 -127
- package/dist/index.js.map +1 -1
- package/dist/mask-preparation.worker.js +91 -59
- package/dist/mask-preparation.worker.js.map +1 -1
- package/dist/media/media-errors.d.ts +32 -0
- package/dist/media/media-errors.d.ts.map +1 -0
- package/dist/media/media-source-state.d.ts.map +1 -1
- package/dist/media/media-stream-media-source.d.ts +31 -0
- package/dist/media/media-stream-media-source.d.ts.map +1 -1
- package/dist/media/mediabunny-media-source.d.ts.map +1 -1
- package/dist/media/presentation-timeline-media-source.d.ts +17 -0
- package/dist/media/presentation-timeline-media-source.d.ts.map +1 -0
- package/dist/media/static-image-media-source.d.ts.map +1 -1
- package/dist/post-processing/default-tracking-worker.d.ts +3 -0
- package/dist/post-processing/default-tracking-worker.d.ts.map +1 -0
- package/dist/post-processing/detection-post-processing-pipeline.d.ts +7 -0
- package/dist/post-processing/detection-post-processing-pipeline.d.ts.map +1 -0
- package/dist/post-processing/embedded-tracking-worker.d.ts +2 -0
- package/dist/post-processing/embedded-tracking-worker.d.ts.map +1 -0
- package/dist/post-processing/tracking-worker-protocol.d.ts +29 -0
- package/dist/post-processing/tracking-worker-protocol.d.ts.map +1 -0
- package/dist/post-processing/tracking.worker.d.ts +2 -0
- package/dist/post-processing/tracking.worker.d.ts.map +1 -0
- package/dist/renderers/media-renderer-core.d.ts.map +1 -1
- package/dist/renderers/media-renderer-state.d.ts.map +1 -1
- package/dist/sessions/media-session-detections.d.ts.map +1 -1
- package/dist/sessions/media-session.d.ts +2 -2
- package/dist/sessions/media-session.d.ts.map +1 -1
- package/dist/tracking.worker.js +1509 -0
- package/dist/tracking.worker.js.map +1 -0
- package/dist/types/detection-post-processing.d.ts +62 -0
- package/dist/types/detection-post-processing.d.ts.map +1 -0
- package/dist/types/media-session.d.ts +57 -1
- package/dist/types/media-session.d.ts.map +1 -1
- package/dist/workers/worker-rpc-client.d.ts +2 -1
- package/dist/workers/worker-rpc-client.d.ts.map +1 -1
- package/node_modules/supervision-js-core/dist/detections/composite-detection-frame-source.d.ts.map +1 -1
- package/node_modules/supervision-js-core/dist/detections/memory-cold-detection-frame-store.d.ts.map +1 -1
- package/node_modules/supervision-js-core/dist/detections/projected-detection-frame-source.d.ts +22 -0
- package/node_modules/supervision-js-core/dist/detections/projected-detection-frame-source.d.ts.map +1 -0
- package/node_modules/supervision-js-core/dist/detections/writable-detection-frame-source.d.ts +2 -7
- package/node_modules/supervision-js-core/dist/detections/writable-detection-frame-source.d.ts.map +1 -1
- package/node_modules/supervision-js-core/dist/index.d.ts +11 -3
- package/node_modules/supervision-js-core/dist/index.d.ts.map +1 -1
- package/node_modules/supervision-js-core/dist/index.js +2289 -329
- package/node_modules/supervision-js-core/dist/index.js.map +1 -1
- package/node_modules/supervision-js-core/dist/post-processing/byte-track-tracker.d.ts +4 -0
- package/node_modules/supervision-js-core/dist/post-processing/byte-track-tracker.d.ts.map +1 -0
- package/node_modules/supervision-js-core/dist/post-processing/cbiou-tracker.d.ts +4 -0
- package/node_modules/supervision-js-core/dist/post-processing/cbiou-tracker.d.ts.map +1 -0
- package/node_modules/supervision-js-core/dist/post-processing/oc-sort-tracker.d.ts +4 -0
- package/node_modules/supervision-js-core/dist/post-processing/oc-sort-tracker.d.ts.map +1 -0
- package/node_modules/supervision-js-core/dist/post-processing/sort-tracker.d.ts +4 -0
- package/node_modules/supervision-js-core/dist/post-processing/sort-tracker.d.ts.map +1 -0
- package/node_modules/supervision-js-core/dist/post-processing/tracking.d.ts +9 -0
- package/node_modules/supervision-js-core/dist/post-processing/tracking.d.ts.map +1 -0
- package/node_modules/supervision-js-core/dist/types/detection-timeline.d.ts +113 -3
- package/node_modules/supervision-js-core/dist/types/detection-timeline.d.ts.map +1 -1
- package/node_modules/supervision-js-core/dist/types/detections.d.ts +33 -3
- package/node_modules/supervision-js-core/dist/types/detections.d.ts.map +1 -1
- package/node_modules/supervision-js-core/dist/types/media-rendering.d.ts +31 -0
- package/node_modules/supervision-js-core/dist/types/media-rendering.d.ts.map +1 -1
- package/node_modules/supervision-js-core/dist/types/post-processing.d.ts +148 -0
- package/node_modules/supervision-js-core/dist/types/post-processing.d.ts.map +1 -0
- package/node_modules/supervision-js-core/dist/utils/detection-frames.d.ts.map +1 -1
- package/node_modules/supervision-js-core/dist/utils/detection-projection.d.ts +20 -0
- package/node_modules/supervision-js-core/dist/utils/detection-projection.d.ts.map +1 -0
- package/node_modules/supervision-js-core/package.json +12 -5
- package/package.json +7 -2
|
@@ -0,0 +1,1509 @@
|
|
|
1
|
+
(function () {
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
/** COCO-compatible keypoint visibility values. */
|
|
5
|
+
var KeypointVisibility;
|
|
6
|
+
(function (KeypointVisibility) {
|
|
7
|
+
KeypointVisibility[KeypointVisibility["NotLabeled"] = 0] = "NotLabeled";
|
|
8
|
+
KeypointVisibility[KeypointVisibility["Occluded"] = 1] = "Occluded";
|
|
9
|
+
KeypointVisibility[KeypointVisibility["Visible"] = 2] = "Visible";
|
|
10
|
+
})(KeypointVisibility || (KeypointVisibility = {}));
|
|
11
|
+
var DetectionMaskEncoding;
|
|
12
|
+
(function (DetectionMaskEncoding) {
|
|
13
|
+
DetectionMaskEncoding["CompressedRle"] = "compressedRle";
|
|
14
|
+
})(DetectionMaskEncoding || (DetectionMaskEncoding = {}));
|
|
15
|
+
|
|
16
|
+
var DetectionBufferStatus;
|
|
17
|
+
(function (DetectionBufferStatus) {
|
|
18
|
+
DetectionBufferStatus["Idle"] = "idle";
|
|
19
|
+
DetectionBufferStatus["Loading"] = "loading";
|
|
20
|
+
DetectionBufferStatus["Ready"] = "ready";
|
|
21
|
+
DetectionBufferStatus["Error"] = "error";
|
|
22
|
+
DetectionBufferStatus["Destroyed"] = "destroyed";
|
|
23
|
+
})(DetectionBufferStatus || (DetectionBufferStatus = {}));
|
|
24
|
+
/**
|
|
25
|
+
* How the renderer selects the active detection frame for a media timestamp.
|
|
26
|
+
*/
|
|
27
|
+
var DetectionFrameSelectionMode;
|
|
28
|
+
(function (DetectionFrameSelectionMode) {
|
|
29
|
+
/**
|
|
30
|
+
* Select the frame whose `[mediaTime, endTime)` interval contains the media
|
|
31
|
+
* time. This is the default for interval annotations and timestamped sources.
|
|
32
|
+
*/
|
|
33
|
+
DetectionFrameSelectionMode["Interval"] = "interval";
|
|
34
|
+
/**
|
|
35
|
+
* Select from a known inference frame grid using `frameIndex`, `frameRate`,
|
|
36
|
+
* and optional `frameIndexOriginTime`. This is useful when inference was run
|
|
37
|
+
* on normalized frames and playback should snap detections to that grid.
|
|
38
|
+
*/
|
|
39
|
+
DetectionFrameSelectionMode["NearestFrameIndex"] = "nearestFrameIndex";
|
|
40
|
+
})(DetectionFrameSelectionMode || (DetectionFrameSelectionMode = {}));
|
|
41
|
+
var DetectionFrameRetentionMode;
|
|
42
|
+
(function (DetectionFrameRetentionMode) {
|
|
43
|
+
/**
|
|
44
|
+
* Keep writable detections only in an in-memory store. Useful for ephemeral
|
|
45
|
+
* live streams where old predictions should disappear when evicted.
|
|
46
|
+
*/
|
|
47
|
+
DetectionFrameRetentionMode["MemoryOnly"] = "memoryOnly";
|
|
48
|
+
/**
|
|
49
|
+
* Persist every written detection frame. Useful for finite media where seek
|
|
50
|
+
* and replay should not require recomputing detections.
|
|
51
|
+
*/
|
|
52
|
+
DetectionFrameRetentionMode["PersistAll"] = "persistAll";
|
|
53
|
+
/**
|
|
54
|
+
* Persist only the most recent retention window. Useful for long-running
|
|
55
|
+
* streams where replay is bounded to a recent time horizon.
|
|
56
|
+
*/
|
|
57
|
+
DetectionFrameRetentionMode["PersistWindow"] = "persistWindow";
|
|
58
|
+
})(DetectionFrameRetentionMode || (DetectionFrameRetentionMode = {}));
|
|
59
|
+
|
|
60
|
+
var AnnotationFrameMutationKind;
|
|
61
|
+
(function (AnnotationFrameMutationKind) {
|
|
62
|
+
AnnotationFrameMutationKind["Add"] = "add";
|
|
63
|
+
AnnotationFrameMutationKind["Remove"] = "remove";
|
|
64
|
+
AnnotationFrameMutationKind["Replace"] = "replace";
|
|
65
|
+
AnnotationFrameMutationKind["Transact"] = "transact";
|
|
66
|
+
AnnotationFrameMutationKind["Update"] = "update";
|
|
67
|
+
})(AnnotationFrameMutationKind || (AnnotationFrameMutationKind = {}));
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Creates one stateful SORT tracker for a single ordered media sequence.
|
|
71
|
+
*
|
|
72
|
+
* Defaults, lifecycle semantics, and observation-only output mirror
|
|
73
|
+
* roboflow/trackers SORT. Motion predictions remain internal to association.
|
|
74
|
+
*/
|
|
75
|
+
function createSortTracker$1(options = {}) {
|
|
76
|
+
const lostTrackBuffer = normalizeNonNegativeInteger$1(options.lostTrackBuffer ?? 30, "lostTrackBuffer");
|
|
77
|
+
const frameRate = options.frameRate ?? 30;
|
|
78
|
+
const trackActivationThreshold = options.trackActivationThreshold ?? 0.25;
|
|
79
|
+
const minimumConsecutiveFrames = normalizePositiveInteger$1(options.minimumConsecutiveFrames ?? 3, "minimumConsecutiveFrames");
|
|
80
|
+
const minimumIouThreshold = options.minimumIouThreshold ?? 0.3;
|
|
81
|
+
if (!Number.isFinite(frameRate) || frameRate <= 0) {
|
|
82
|
+
throw new Error("frameRate must be a finite positive value.");
|
|
83
|
+
}
|
|
84
|
+
normalizeUnitInterval$1(trackActivationThreshold, "trackActivationThreshold");
|
|
85
|
+
normalizeUnitInterval$1(minimumIouThreshold, "minimumIouThreshold");
|
|
86
|
+
const scaledLostTrackBuffer = (frameRate / 30) * lostTrackBuffer;
|
|
87
|
+
if (!Number.isFinite(scaledLostTrackBuffer)) {
|
|
88
|
+
throw new Error("Scaled lostTrackBuffer overflows: frameRate / 30 * lostTrackBuffer must be finite.");
|
|
89
|
+
}
|
|
90
|
+
const maximumFramesWithoutUpdate = lostTrackBuffer === 0 ? 0 : Math.max(1, Math.ceil(scaledLostTrackBuffer));
|
|
91
|
+
let tracks = [];
|
|
92
|
+
let nextTrackerId = 0;
|
|
93
|
+
let previousFrameIndex;
|
|
94
|
+
return {
|
|
95
|
+
reset() {
|
|
96
|
+
tracks = [];
|
|
97
|
+
nextTrackerId = 0;
|
|
98
|
+
previousFrameIndex = undefined;
|
|
99
|
+
},
|
|
100
|
+
update(detections, frameIndex) {
|
|
101
|
+
const frameStep = resolveFrameStep(frameIndex, previousFrameIndex);
|
|
102
|
+
previousFrameIndex = frameIndex ?? previousFrameIndex;
|
|
103
|
+
const predicted = tracks.map((track) => track.predict(frameStep, frameRate));
|
|
104
|
+
const { matches, unmatchedDetections } = associateDetectionsToTracks(detections, predicted, minimumIouThreshold);
|
|
105
|
+
const trackerIds = new Map();
|
|
106
|
+
for (const match of matches) {
|
|
107
|
+
const track = tracks[match.trackIndex];
|
|
108
|
+
const detection = detections[match.detectionIndex];
|
|
109
|
+
track.update(detection);
|
|
110
|
+
if (track.trackerId === undefined &&
|
|
111
|
+
track.successfulUpdates >= minimumConsecutiveFrames) {
|
|
112
|
+
track.trackerId = nextTrackerId;
|
|
113
|
+
nextTrackerId += 1;
|
|
114
|
+
}
|
|
115
|
+
if (track.trackerId !== undefined) {
|
|
116
|
+
trackerIds.set(detection.detectionIndex, track.trackerId);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
for (const detectionIndex of unmatchedDetections) {
|
|
120
|
+
const detection = detections[detectionIndex];
|
|
121
|
+
if ((detection.confidence ?? 1) >= trackActivationThreshold) {
|
|
122
|
+
tracks.push(new KalmanBoxTrack(detection));
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
tracks = tracks.filter((track) => track.timeSinceUpdate <= maximumFramesWithoutUpdate &&
|
|
126
|
+
(track.successfulUpdates >= minimumConsecutiveFrames ||
|
|
127
|
+
track.timeSinceUpdate === 0));
|
|
128
|
+
return {
|
|
129
|
+
activeTrackCount: tracks.length,
|
|
130
|
+
assignments: detections.flatMap((detection) => {
|
|
131
|
+
const trackerId = trackerIds.get(detection.detectionIndex);
|
|
132
|
+
return trackerId === undefined
|
|
133
|
+
? []
|
|
134
|
+
: [{ detectionIndex: detection.detectionIndex, trackerId }];
|
|
135
|
+
}),
|
|
136
|
+
confirmedTrackCount: tracks.filter((track) => track.trackerId !== undefined).length,
|
|
137
|
+
};
|
|
138
|
+
},
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
class KalmanBoxTrack {
|
|
142
|
+
consecutiveUpdates;
|
|
143
|
+
trackerId;
|
|
144
|
+
successfulUpdates = 1;
|
|
145
|
+
timeSinceUpdate = 0;
|
|
146
|
+
state;
|
|
147
|
+
covariance = identity(8);
|
|
148
|
+
constructor(detection, consecutiveUpdates = false) {
|
|
149
|
+
this.consecutiveUpdates = consecutiveUpdates;
|
|
150
|
+
this.state = [...rectToXyxy(detection.rect), 0, 0, 0, 0].map((value) => [
|
|
151
|
+
value,
|
|
152
|
+
]);
|
|
153
|
+
}
|
|
154
|
+
predict(frameStep, frameRate) {
|
|
155
|
+
if (this.consecutiveUpdates && this.timeSinceUpdate > 0) {
|
|
156
|
+
this.successfulUpdates = 0;
|
|
157
|
+
}
|
|
158
|
+
const transition = identity(8);
|
|
159
|
+
for (let index = 0; index < 4; index += 1) {
|
|
160
|
+
transition[index][index + 4] = frameStep;
|
|
161
|
+
}
|
|
162
|
+
this.state = multiply(transition, this.state);
|
|
163
|
+
this.covariance = add(multiply(multiply(transition, this.covariance), transpose(transition)), createProcessNoise(frameStep, frameRate));
|
|
164
|
+
// Python SORT counts update calls for the fixed-rate lost-track budget.
|
|
165
|
+
this.timeSinceUpdate += 1;
|
|
166
|
+
return stateToRect(this.state);
|
|
167
|
+
}
|
|
168
|
+
update(detection) {
|
|
169
|
+
const measurement = rectToXyxy(detection.rect).map((value) => [value]);
|
|
170
|
+
const observation = [
|
|
171
|
+
[1, 0, 0, 0, 0, 0, 0, 0],
|
|
172
|
+
[0, 1, 0, 0, 0, 0, 0, 0],
|
|
173
|
+
[0, 0, 1, 0, 0, 0, 0, 0],
|
|
174
|
+
[0, 0, 0, 1, 0, 0, 0, 0],
|
|
175
|
+
];
|
|
176
|
+
const measurementNoise = scale(identity(4), 0.1);
|
|
177
|
+
const innovation = subtract(measurement, multiply(observation, this.state));
|
|
178
|
+
const covarianceObservationTranspose = multiply(this.covariance, transpose(observation));
|
|
179
|
+
const innovationCovariance = add(multiply(observation, covarianceObservationTranspose), measurementNoise);
|
|
180
|
+
const gain = multiply(covarianceObservationTranspose, inverse(innovationCovariance));
|
|
181
|
+
this.state = add(this.state, multiply(gain, innovation));
|
|
182
|
+
const identityMinusGainObservation = subtract(identity(8), multiply(gain, observation));
|
|
183
|
+
// Joseph form matches the Python implementation and is more stable.
|
|
184
|
+
this.covariance = add(multiply(multiply(identityMinusGainObservation, this.covariance), transpose(identityMinusGainObservation)), multiply(multiply(gain, measurementNoise), transpose(gain)));
|
|
185
|
+
this.timeSinceUpdate = 0;
|
|
186
|
+
this.successfulUpdates += 1;
|
|
187
|
+
}
|
|
188
|
+
getStateRect() {
|
|
189
|
+
return stateToRect(this.state);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
function associateDetectionsToTracks(detections, predicted, minimumIouThreshold) {
|
|
193
|
+
if (predicted.length === 0 || detections.length === 0) {
|
|
194
|
+
return {
|
|
195
|
+
matches: [],
|
|
196
|
+
unmatchedDetections: detections.map((_, index) => index),
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
// Python SORT associates all detections class-agnostically using standard IoU.
|
|
200
|
+
const scores = predicted.map((rect) => detections.map((detection) => intersectionOverUnion(rect, detection.rect)));
|
|
201
|
+
const candidateMatches = maximizeAssignment(scores);
|
|
202
|
+
const matches = [];
|
|
203
|
+
const matchedDetections = new Set();
|
|
204
|
+
for (const [trackIndex, detectionIndex] of candidateMatches) {
|
|
205
|
+
if (scores[trackIndex][detectionIndex] < minimumIouThreshold)
|
|
206
|
+
continue;
|
|
207
|
+
matches.push({ detectionIndex, trackIndex });
|
|
208
|
+
matchedDetections.add(detectionIndex);
|
|
209
|
+
}
|
|
210
|
+
matches.sort((left, right) => left.trackIndex - right.trackIndex);
|
|
211
|
+
return {
|
|
212
|
+
matches,
|
|
213
|
+
unmatchedDetections: detections.flatMap((_, index) => matchedDetections.has(index) ? [] : [index]),
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
/** Hungarian assignment for a rectangular score matrix. */
|
|
217
|
+
function maximizeAssignment(scores) {
|
|
218
|
+
if (scores.length === 0 || scores[0]?.length === 0) {
|
|
219
|
+
return [];
|
|
220
|
+
}
|
|
221
|
+
const rowCount = scores.length;
|
|
222
|
+
const columnCount = scores[0].length;
|
|
223
|
+
const transposed = rowCount > columnCount;
|
|
224
|
+
const costs = transposed
|
|
225
|
+
? Array.from({ length: columnCount }, (_, row) => Array.from({ length: rowCount }, (_, column) => 1 - scores[column][row]))
|
|
226
|
+
: scores.map((row) => row.map((score) => 1 - score));
|
|
227
|
+
const rows = costs.length;
|
|
228
|
+
const columns = costs[0].length;
|
|
229
|
+
const u = new Array(rows + 1).fill(0);
|
|
230
|
+
const v = new Array(columns + 1).fill(0);
|
|
231
|
+
const p = new Array(columns + 1).fill(0);
|
|
232
|
+
const way = new Array(columns + 1).fill(0);
|
|
233
|
+
for (let row = 1; row <= rows; row += 1) {
|
|
234
|
+
p[0] = row;
|
|
235
|
+
let column0 = 0;
|
|
236
|
+
const minValue = new Array(columns + 1).fill(Number.POSITIVE_INFINITY);
|
|
237
|
+
const used = new Array(columns + 1).fill(false);
|
|
238
|
+
do {
|
|
239
|
+
used[column0] = true;
|
|
240
|
+
const row0 = p[column0];
|
|
241
|
+
let delta = Number.POSITIVE_INFINITY;
|
|
242
|
+
let column1 = 0;
|
|
243
|
+
for (let column = 1; column <= columns; column += 1) {
|
|
244
|
+
if (used[column])
|
|
245
|
+
continue;
|
|
246
|
+
const current = costs[row0 - 1][column - 1] - u[row0] - v[column];
|
|
247
|
+
if (current < minValue[column]) {
|
|
248
|
+
minValue[column] = current;
|
|
249
|
+
way[column] = column0;
|
|
250
|
+
}
|
|
251
|
+
if (minValue[column] < delta) {
|
|
252
|
+
delta = minValue[column];
|
|
253
|
+
column1 = column;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
for (let column = 0; column <= columns; column += 1) {
|
|
257
|
+
if (used[column]) {
|
|
258
|
+
u[p[column]] += delta;
|
|
259
|
+
v[column] -= delta;
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
minValue[column] -= delta;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
column0 = column1;
|
|
266
|
+
} while (p[column0] !== 0);
|
|
267
|
+
do {
|
|
268
|
+
const column1 = way[column0];
|
|
269
|
+
p[column0] = p[column1];
|
|
270
|
+
column0 = column1;
|
|
271
|
+
} while (column0 !== 0);
|
|
272
|
+
}
|
|
273
|
+
const assignments = [];
|
|
274
|
+
for (let column = 1; column <= columns; column += 1) {
|
|
275
|
+
if (p[column] === 0)
|
|
276
|
+
continue;
|
|
277
|
+
const row = p[column] - 1;
|
|
278
|
+
assignments.push(transposed ? [column - 1, row] : [row, column - 1]);
|
|
279
|
+
}
|
|
280
|
+
return assignments;
|
|
281
|
+
}
|
|
282
|
+
function rectToXyxy(rect) {
|
|
283
|
+
return [
|
|
284
|
+
rect.x - rect.width / 2,
|
|
285
|
+
rect.y - rect.height / 2,
|
|
286
|
+
rect.x + rect.width / 2,
|
|
287
|
+
rect.y + rect.height / 2,
|
|
288
|
+
];
|
|
289
|
+
}
|
|
290
|
+
function stateToRect(state) {
|
|
291
|
+
// The Python XYXY estimator intentionally leaves corner velocities
|
|
292
|
+
// unconstrained. Normalize only at the browser Rect boundary so a crossing
|
|
293
|
+
// prediction cannot violate the positive-width/height storage contract.
|
|
294
|
+
const x1 = Math.min(state[0][0], state[2][0]);
|
|
295
|
+
const y1 = Math.min(state[1][0], state[3][0]);
|
|
296
|
+
const x2 = Math.max(state[0][0], state[2][0]);
|
|
297
|
+
const y2 = Math.max(state[1][0], state[3][0]);
|
|
298
|
+
return {
|
|
299
|
+
height: Math.max(Number.EPSILON, y2 - y1),
|
|
300
|
+
width: Math.max(Number.EPSILON, x2 - x1),
|
|
301
|
+
x: (x1 + x2) / 2,
|
|
302
|
+
y: (y1 + y2) / 2,
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
function createProcessNoise(frameStep, frameRate) {
|
|
306
|
+
if (Math.abs(frameStep - 1) <= 0.004 * frameRate) {
|
|
307
|
+
return scale(identity(8), 0.01);
|
|
308
|
+
}
|
|
309
|
+
const result = Array.from({ length: 8 }, () => new Array(8).fill(0));
|
|
310
|
+
const dt2 = frameStep * frameStep;
|
|
311
|
+
const dt3 = dt2 * frameStep;
|
|
312
|
+
const dt4 = dt2 * dt2;
|
|
313
|
+
for (let index = 0; index < 4; index += 1) {
|
|
314
|
+
const velocityIndex = index + 4;
|
|
315
|
+
result[index][index] = (0.01 * dt4) / 4;
|
|
316
|
+
result[index][velocityIndex] = (0.01 * dt3) / 2;
|
|
317
|
+
result[velocityIndex][index] = (0.01 * dt3) / 2;
|
|
318
|
+
result[velocityIndex][velocityIndex] = 0.01 * dt2;
|
|
319
|
+
}
|
|
320
|
+
return result;
|
|
321
|
+
}
|
|
322
|
+
function intersectionOverUnion(left, right) {
|
|
323
|
+
const leftX = left.x - left.width / 2;
|
|
324
|
+
const leftY = left.y - left.height / 2;
|
|
325
|
+
const rightX = right.x - right.width / 2;
|
|
326
|
+
const rightY = right.y - right.height / 2;
|
|
327
|
+
const intersectionWidth = Math.max(0, Math.min(leftX + left.width, rightX + right.width) -
|
|
328
|
+
Math.max(leftX, rightX));
|
|
329
|
+
const intersectionHeight = Math.max(0, Math.min(leftY + left.height, rightY + right.height) -
|
|
330
|
+
Math.max(leftY, rightY));
|
|
331
|
+
const intersection = intersectionWidth * intersectionHeight;
|
|
332
|
+
const union = left.width * left.height + right.width * right.height - intersection;
|
|
333
|
+
return union <= 0 ? 0 : intersection / union;
|
|
334
|
+
}
|
|
335
|
+
function resolveFrameStep(current, previous) {
|
|
336
|
+
if (current === undefined || previous === undefined)
|
|
337
|
+
return 1;
|
|
338
|
+
return Math.max(1, current - previous);
|
|
339
|
+
}
|
|
340
|
+
function normalizePositiveInteger$1(value, label) {
|
|
341
|
+
if (!Number.isInteger(value) || value < 1) {
|
|
342
|
+
throw new Error(`${label} must be a positive integer.`);
|
|
343
|
+
}
|
|
344
|
+
return value;
|
|
345
|
+
}
|
|
346
|
+
function normalizeNonNegativeInteger$1(value, label) {
|
|
347
|
+
if (!Number.isInteger(value) || value < 0) {
|
|
348
|
+
throw new Error(`${label} must be a non-negative integer.`);
|
|
349
|
+
}
|
|
350
|
+
return value;
|
|
351
|
+
}
|
|
352
|
+
function normalizeUnitInterval$1(value, label) {
|
|
353
|
+
if (!Number.isFinite(value) || value < 0 || value > 1) {
|
|
354
|
+
throw new Error(`${label} must be between 0 and 1.`);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
function identity(size) {
|
|
358
|
+
return Array.from({ length: size }, (_, row) => Array.from({ length: size }, (_, column) => (row === column ? 1 : 0)));
|
|
359
|
+
}
|
|
360
|
+
function scale(matrix, factor) {
|
|
361
|
+
return matrix.map((row) => row.map((value) => value * factor));
|
|
362
|
+
}
|
|
363
|
+
function transpose(matrix) {
|
|
364
|
+
return matrix[0].map((_, column) => matrix.map((row) => row[column]));
|
|
365
|
+
}
|
|
366
|
+
function add(left, right) {
|
|
367
|
+
return left.map((row, rowIndex) => row.map((value, columnIndex) => value + right[rowIndex][columnIndex]));
|
|
368
|
+
}
|
|
369
|
+
function subtract(left, right) {
|
|
370
|
+
return left.map((row, rowIndex) => row.map((value, columnIndex) => value - right[rowIndex][columnIndex]));
|
|
371
|
+
}
|
|
372
|
+
function multiply(left, right) {
|
|
373
|
+
return left.map((row) => right[0].map((_, column) => row.reduce((sum, value, index) => sum + value * right[index][column], 0)));
|
|
374
|
+
}
|
|
375
|
+
function inverse(matrix) {
|
|
376
|
+
const size = matrix.length;
|
|
377
|
+
const augmented = matrix.map((row, index) => [
|
|
378
|
+
...row,
|
|
379
|
+
...identity(size)[index],
|
|
380
|
+
]);
|
|
381
|
+
for (let column = 0; column < size; column += 1) {
|
|
382
|
+
let pivot = column;
|
|
383
|
+
for (let row = column + 1; row < size; row += 1) {
|
|
384
|
+
if (Math.abs(augmented[row][column]) >
|
|
385
|
+
Math.abs(augmented[pivot][column])) {
|
|
386
|
+
pivot = row;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
if (Math.abs(augmented[pivot][column]) < 1e-12) {
|
|
390
|
+
throw new Error("SORT Kalman covariance is singular.");
|
|
391
|
+
}
|
|
392
|
+
[augmented[column], augmented[pivot]] = [
|
|
393
|
+
augmented[pivot],
|
|
394
|
+
augmented[column],
|
|
395
|
+
];
|
|
396
|
+
const divisor = augmented[column][column];
|
|
397
|
+
augmented[column] = augmented[column].map((value) => value / divisor);
|
|
398
|
+
for (let row = 0; row < size; row += 1) {
|
|
399
|
+
if (row === column)
|
|
400
|
+
continue;
|
|
401
|
+
const factor = augmented[row][column];
|
|
402
|
+
augmented[row] = augmented[row].map((value, index) => value - factor * augmented[column][index]);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
return augmented.map((row) => row.slice(size));
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Creates one stateful ByteTrack tracker for a single ordered media sequence.
|
|
410
|
+
*
|
|
411
|
+
* Defaults and two-stage association mirror roboflow/trackers ByteTrack at
|
|
412
|
+
* source commit 60b21c8a48676784085fbee455559f16b75a7c9a.
|
|
413
|
+
* Motion predictions remain internal; only observed detections receive IDs.
|
|
414
|
+
*/
|
|
415
|
+
function createByteTrackTracker$1(options = {}) {
|
|
416
|
+
const lostTrackBuffer = normalizeNonNegativeInteger$1(options.lostTrackBuffer ?? 30, "lostTrackBuffer");
|
|
417
|
+
const frameRate = options.frameRate ?? 30;
|
|
418
|
+
const trackActivationThreshold = options.trackActivationThreshold ?? 0.7;
|
|
419
|
+
const minimumConsecutiveFrames = normalizePositiveInteger$1(options.minimumConsecutiveFrames ?? 2, "minimumConsecutiveFrames");
|
|
420
|
+
const minimumIouThreshold = options.minimumIouThreshold ?? 0.1;
|
|
421
|
+
const highConfidenceDetectionThreshold = options.highConfidenceDetectionThreshold ?? 0.6;
|
|
422
|
+
if (!Number.isFinite(frameRate) || frameRate <= 0) {
|
|
423
|
+
throw new Error("frameRate must be a finite positive value.");
|
|
424
|
+
}
|
|
425
|
+
normalizeUnitInterval$1(trackActivationThreshold, "trackActivationThreshold");
|
|
426
|
+
normalizeUnitInterval$1(minimumIouThreshold, "minimumIouThreshold");
|
|
427
|
+
normalizeUnitInterval$1(highConfidenceDetectionThreshold, "highConfidenceDetectionThreshold");
|
|
428
|
+
const scaledLostTrackBuffer = (frameRate / 30) * lostTrackBuffer;
|
|
429
|
+
if (!Number.isFinite(scaledLostTrackBuffer)) {
|
|
430
|
+
throw new Error("Scaled lostTrackBuffer overflows: frameRate / 30 * lostTrackBuffer must be finite.");
|
|
431
|
+
}
|
|
432
|
+
const maximumFramesWithoutUpdate = lostTrackBuffer === 0 ? 0 : Math.max(1, Math.ceil(scaledLostTrackBuffer));
|
|
433
|
+
let tracks = [];
|
|
434
|
+
let nextTrackerId = 0;
|
|
435
|
+
let previousFrameIndex;
|
|
436
|
+
return {
|
|
437
|
+
reset() {
|
|
438
|
+
tracks = [];
|
|
439
|
+
nextTrackerId = 0;
|
|
440
|
+
previousFrameIndex = undefined;
|
|
441
|
+
},
|
|
442
|
+
update(detections, frameIndex) {
|
|
443
|
+
const frameStep = resolveFrameStep(frameIndex, previousFrameIndex);
|
|
444
|
+
previousFrameIndex = frameIndex ?? previousFrameIndex;
|
|
445
|
+
tracks.forEach((track) => track.predict(frameStep, frameRate));
|
|
446
|
+
const highDetectionIndexes = [];
|
|
447
|
+
const lowDetectionIndexes = [];
|
|
448
|
+
detections.forEach((detection, index) => {
|
|
449
|
+
if ((detection.confidence ?? 1) >= highConfidenceDetectionThreshold) {
|
|
450
|
+
highDetectionIndexes.push(index);
|
|
451
|
+
}
|
|
452
|
+
else {
|
|
453
|
+
lowDetectionIndexes.push(index);
|
|
454
|
+
}
|
|
455
|
+
});
|
|
456
|
+
const assignments = new Map();
|
|
457
|
+
const firstStage = associate(tracks, detections, highDetectionIndexes, minimumIouThreshold);
|
|
458
|
+
for (const match of firstStage.matches) {
|
|
459
|
+
updateMatchedTrack(tracks[match.trackIndex], detections[match.detectionIndex], assignments);
|
|
460
|
+
}
|
|
461
|
+
const remainingTracks = firstStage.unmatchedTrackIndexes.map((index) => tracks[index]);
|
|
462
|
+
const secondStage = associate(remainingTracks, detections, lowDetectionIndexes, minimumIouThreshold);
|
|
463
|
+
for (const match of secondStage.matches) {
|
|
464
|
+
updateMatchedTrack(remainingTracks[match.trackIndex], detections[match.detectionIndex], assignments);
|
|
465
|
+
}
|
|
466
|
+
// Only unmatched high-confidence observations can start a track.
|
|
467
|
+
for (const detectionIndex of firstStage.unmatchedDetectionIndexes) {
|
|
468
|
+
const detection = detections[detectionIndex];
|
|
469
|
+
if ((detection.confidence ?? 1) >= trackActivationThreshold) {
|
|
470
|
+
tracks.push(new KalmanBoxTrack(detection, true));
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
// Confirmation is sticky once an ID has been allocated. An unmatched
|
|
474
|
+
// unconfirmed track is discarded immediately, as in Python ByteTrack.
|
|
475
|
+
tracks = tracks.filter((track) => track.timeSinceUpdate <= maximumFramesWithoutUpdate &&
|
|
476
|
+
(track.trackerId !== undefined ||
|
|
477
|
+
track.successfulUpdates >= minimumConsecutiveFrames ||
|
|
478
|
+
track.timeSinceUpdate === 0));
|
|
479
|
+
return {
|
|
480
|
+
activeTrackCount: tracks.length,
|
|
481
|
+
assignments: detections.flatMap((detection) => {
|
|
482
|
+
const trackerId = assignments.get(detection.detectionIndex);
|
|
483
|
+
return trackerId === undefined
|
|
484
|
+
? []
|
|
485
|
+
: [{ detectionIndex: detection.detectionIndex, trackerId }];
|
|
486
|
+
}),
|
|
487
|
+
confirmedTrackCount: tracks.filter((track) => track.trackerId !== undefined).length,
|
|
488
|
+
};
|
|
489
|
+
},
|
|
490
|
+
};
|
|
491
|
+
function updateMatchedTrack(track, detection, assignments) {
|
|
492
|
+
track.update(detection);
|
|
493
|
+
if (track.trackerId === undefined &&
|
|
494
|
+
track.successfulUpdates >= minimumConsecutiveFrames) {
|
|
495
|
+
track.trackerId = nextTrackerId;
|
|
496
|
+
nextTrackerId += 1;
|
|
497
|
+
}
|
|
498
|
+
if (track.trackerId !== undefined) {
|
|
499
|
+
assignments.set(detection.detectionIndex, track.trackerId);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
function associate(tracks, detections, detectionIndexes, minimumIouThreshold) {
|
|
504
|
+
if (tracks.length === 0 || detectionIndexes.length === 0) {
|
|
505
|
+
return {
|
|
506
|
+
matches: [],
|
|
507
|
+
unmatchedDetectionIndexes: [...detectionIndexes],
|
|
508
|
+
unmatchedTrackIndexes: tracks.map((_, index) => index),
|
|
509
|
+
};
|
|
510
|
+
}
|
|
511
|
+
const scores = tracks.map((track) => {
|
|
512
|
+
const predictedRect = track.getStateRect();
|
|
513
|
+
return detectionIndexes.map((detectionIndex) => intersectionOverUnion(predictedRect, detections[detectionIndex].rect));
|
|
514
|
+
});
|
|
515
|
+
const matches = [];
|
|
516
|
+
const matchedTracks = new Set();
|
|
517
|
+
const matchedDetections = new Set();
|
|
518
|
+
for (const [trackIndex, localDetectionIndex] of maximizeAssignment(scores)) {
|
|
519
|
+
if (scores[trackIndex][localDetectionIndex] < minimumIouThreshold) {
|
|
520
|
+
continue;
|
|
521
|
+
}
|
|
522
|
+
const detectionIndex = detectionIndexes[localDetectionIndex];
|
|
523
|
+
matches.push({ detectionIndex, trackIndex });
|
|
524
|
+
matchedTracks.add(trackIndex);
|
|
525
|
+
matchedDetections.add(detectionIndex);
|
|
526
|
+
}
|
|
527
|
+
matches.sort((left, right) => left.trackIndex - right.trackIndex);
|
|
528
|
+
return {
|
|
529
|
+
matches,
|
|
530
|
+
unmatchedDetectionIndexes: detectionIndexes.filter((index) => !matchedDetections.has(index)),
|
|
531
|
+
unmatchedTrackIndexes: tracks.flatMap((_, index) => matchedTracks.has(index) ? [] : [index]),
|
|
532
|
+
};
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
function associateTrackingScores(scores, trackCount, detectionCount, minimumScore, acceptanceScores = scores) {
|
|
536
|
+
const matches = [];
|
|
537
|
+
const matchedTracks = new Set();
|
|
538
|
+
const matchedDetections = new Set();
|
|
539
|
+
for (const [trackIndex, detectionIndex] of maximizeAssignment(scores)) {
|
|
540
|
+
if (acceptanceScores[trackIndex][detectionIndex] < minimumScore)
|
|
541
|
+
continue;
|
|
542
|
+
matches.push({ detectionIndex, trackIndex });
|
|
543
|
+
matchedTracks.add(trackIndex);
|
|
544
|
+
matchedDetections.add(detectionIndex);
|
|
545
|
+
}
|
|
546
|
+
matches.sort((left, right) => left.trackIndex - right.trackIndex);
|
|
547
|
+
return {
|
|
548
|
+
matches,
|
|
549
|
+
unmatchedDetectionIndexes: Array.from({ length: detectionCount }, (_, index) => index).filter((index) => !matchedDetections.has(index)),
|
|
550
|
+
unmatchedTrackIndexes: Array.from({ length: trackCount }, (_, index) => index).filter((index) => !matchedTracks.has(index)),
|
|
551
|
+
};
|
|
552
|
+
}
|
|
553
|
+
function pairwiseIou(tracks, detections, bufferRatio = 0) {
|
|
554
|
+
return tracks.map((track) => detections.map((detection) => bufferedIntersectionOverUnion(track, detection, bufferRatio)));
|
|
555
|
+
}
|
|
556
|
+
function bufferedIntersectionOverUnion(left, right, bufferRatio) {
|
|
557
|
+
const leftWidth = left.width * (1 + 2 * bufferRatio);
|
|
558
|
+
const leftHeight = left.height * (1 + 2 * bufferRatio);
|
|
559
|
+
const rightWidth = right.width * (1 + 2 * bufferRatio);
|
|
560
|
+
const rightHeight = right.height * (1 + 2 * bufferRatio);
|
|
561
|
+
const leftX = left.x - leftWidth / 2;
|
|
562
|
+
const leftY = left.y - leftHeight / 2;
|
|
563
|
+
const rightX = right.x - rightWidth / 2;
|
|
564
|
+
const rightY = right.y - rightHeight / 2;
|
|
565
|
+
const intersectionWidth = Math.max(0, Math.min(leftX + leftWidth, rightX + rightWidth) - Math.max(leftX, rightX));
|
|
566
|
+
const intersectionHeight = Math.max(0, Math.min(leftY + leftHeight, rightY + rightHeight) -
|
|
567
|
+
Math.max(leftY, rightY));
|
|
568
|
+
const intersection = intersectionWidth * intersectionHeight;
|
|
569
|
+
const union = leftWidth * leftHeight + rightWidth * rightHeight - intersection;
|
|
570
|
+
return union <= 0 ? 0 : intersection / union;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
/**
|
|
574
|
+
* Kalman state estimator shared by the browser C-BIoU and OC-SORT ports.
|
|
575
|
+
* Its layouts, covariance update, and gap-scaled constant-velocity model
|
|
576
|
+
* mirror roboflow/trackers at 60b21c8.
|
|
577
|
+
*/
|
|
578
|
+
class TrackingKalmanEstimator {
|
|
579
|
+
representation;
|
|
580
|
+
dimension;
|
|
581
|
+
measurementDimension = 4;
|
|
582
|
+
covariance;
|
|
583
|
+
measurementNoise = identity(4);
|
|
584
|
+
processNoise;
|
|
585
|
+
state;
|
|
586
|
+
baselineProcessNoise;
|
|
587
|
+
positionIndexes;
|
|
588
|
+
velocityIndexes;
|
|
589
|
+
constructor(initialRect, representation) {
|
|
590
|
+
this.representation = representation;
|
|
591
|
+
const measurement = rectToMeasurement(initialRect, representation);
|
|
592
|
+
this.dimension = representation === "xcycsr" ? 7 : 8;
|
|
593
|
+
this.positionIndexes =
|
|
594
|
+
representation === "xcycsr" ? [0, 1, 2] : [0, 1, 2, 3];
|
|
595
|
+
this.velocityIndexes =
|
|
596
|
+
representation === "xcycsr" ? [4, 5, 6] : [4, 5, 6, 7];
|
|
597
|
+
this.state = Array.from({ length: this.dimension }, (_, index) => [
|
|
598
|
+
measurement[index] ?? 0,
|
|
599
|
+
]);
|
|
600
|
+
this.covariance = identity(this.dimension);
|
|
601
|
+
this.processNoise = identity(this.dimension);
|
|
602
|
+
this.baselineProcessNoise = identity(this.dimension);
|
|
603
|
+
}
|
|
604
|
+
predict(frameStep, frameRate) {
|
|
605
|
+
if (this.representation === "xcycsr" &&
|
|
606
|
+
this.state[2][0] + frameStep * this.state[6][0] <= 0) {
|
|
607
|
+
this.state[6][0] = 0;
|
|
608
|
+
}
|
|
609
|
+
const transition = identity(this.dimension);
|
|
610
|
+
this.positionIndexes.forEach((positionIndex, index) => {
|
|
611
|
+
transition[positionIndex][this.velocityIndexes[index]] = frameStep;
|
|
612
|
+
});
|
|
613
|
+
this.processNoise = this.createProcessNoise(frameStep, frameRate);
|
|
614
|
+
this.state = multiply(transition, this.state);
|
|
615
|
+
this.covariance = add(multiply(multiply(transition, this.covariance), transpose(transition)), this.processNoise);
|
|
616
|
+
}
|
|
617
|
+
update(rect) {
|
|
618
|
+
this.updateMeasurement(rectToMeasurement(rect, this.representation));
|
|
619
|
+
}
|
|
620
|
+
updateMeasurement(measurement) {
|
|
621
|
+
const observation = Array.from({ length: 4 }, (_, row) => Array.from({ length: this.dimension }, (_, column) => row === column ? 1 : 0));
|
|
622
|
+
const measurementColumn = measurement.map((value) => [value]);
|
|
623
|
+
const innovation = subtract(measurementColumn, multiply(observation, this.state));
|
|
624
|
+
const covarianceObservationTranspose = multiply(this.covariance, transpose(observation));
|
|
625
|
+
const innovationCovariance = add(multiply(observation, covarianceObservationTranspose), this.measurementNoise);
|
|
626
|
+
const gain = multiply(covarianceObservationTranspose, inverse(innovationCovariance));
|
|
627
|
+
this.state = add(this.state, multiply(gain, innovation));
|
|
628
|
+
const identityMinusGainObservation = subtract(identity(this.dimension), multiply(gain, observation));
|
|
629
|
+
this.covariance = add(multiply(multiply(identityMinusGainObservation, this.covariance), transpose(identityMinusGainObservation)), multiply(multiply(gain, this.measurementNoise), transpose(gain)));
|
|
630
|
+
}
|
|
631
|
+
getRect() {
|
|
632
|
+
return measurementToRect(this.state.slice(0, 4).map((row) => row[0]), this.representation);
|
|
633
|
+
}
|
|
634
|
+
setCovariances(options) {
|
|
635
|
+
if (options.covariance)
|
|
636
|
+
this.covariance = clone(options.covariance);
|
|
637
|
+
if (options.measurementNoise) {
|
|
638
|
+
this.measurementNoise = clone(options.measurementNoise);
|
|
639
|
+
}
|
|
640
|
+
if (options.processNoise) {
|
|
641
|
+
this.processNoise = clone(options.processNoise);
|
|
642
|
+
this.baselineProcessNoise = clone(options.processNoise);
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
snapshot() {
|
|
646
|
+
return {
|
|
647
|
+
covariance: clone(this.covariance),
|
|
648
|
+
measurementNoise: clone(this.measurementNoise),
|
|
649
|
+
processNoise: clone(this.processNoise),
|
|
650
|
+
state: clone(this.state),
|
|
651
|
+
};
|
|
652
|
+
}
|
|
653
|
+
restore(snapshot) {
|
|
654
|
+
this.covariance = clone(snapshot.covariance);
|
|
655
|
+
this.measurementNoise = clone(snapshot.measurementNoise);
|
|
656
|
+
this.processNoise = clone(snapshot.processNoise);
|
|
657
|
+
this.state = clone(snapshot.state);
|
|
658
|
+
}
|
|
659
|
+
createProcessNoise(frameStep, frameRate) {
|
|
660
|
+
if (Math.abs(frameStep - 1) <= 0.004 * frameRate) {
|
|
661
|
+
return clone(this.baselineProcessNoise);
|
|
662
|
+
}
|
|
663
|
+
const result = Array.from({ length: this.dimension }, () => new Array(this.dimension).fill(0));
|
|
664
|
+
const dt2 = frameStep * frameStep;
|
|
665
|
+
const dt3 = dt2 * frameStep;
|
|
666
|
+
const dt4 = dt2 * dt2;
|
|
667
|
+
const kinematicIndexes = new Set([
|
|
668
|
+
...this.positionIndexes,
|
|
669
|
+
...this.velocityIndexes,
|
|
670
|
+
]);
|
|
671
|
+
this.positionIndexes.forEach((positionIndex, index) => {
|
|
672
|
+
const velocityIndex = this.velocityIndexes[index];
|
|
673
|
+
const accelerationVariance = this.baselineProcessNoise[velocityIndex][velocityIndex];
|
|
674
|
+
result[positionIndex][positionIndex] = (accelerationVariance * dt4) / 4;
|
|
675
|
+
result[positionIndex][velocityIndex] = (accelerationVariance * dt3) / 2;
|
|
676
|
+
result[velocityIndex][positionIndex] = (accelerationVariance * dt3) / 2;
|
|
677
|
+
result[velocityIndex][velocityIndex] = accelerationVariance * dt2;
|
|
678
|
+
});
|
|
679
|
+
for (let index = 0; index < this.dimension; index += 1) {
|
|
680
|
+
if (!kinematicIndexes.has(index)) {
|
|
681
|
+
result[index][index] = this.baselineProcessNoise[index][index];
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
return result;
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
function rectToMeasurement(rect, representation) {
|
|
688
|
+
if (representation === "xcycwh") {
|
|
689
|
+
return [rect.x, rect.y, rect.width, rect.height];
|
|
690
|
+
}
|
|
691
|
+
return [
|
|
692
|
+
rect.x,
|
|
693
|
+
rect.y,
|
|
694
|
+
rect.width * rect.height,
|
|
695
|
+
rect.width / (rect.height + 1e-6),
|
|
696
|
+
];
|
|
697
|
+
}
|
|
698
|
+
function measurementToRect(measurement, representation) {
|
|
699
|
+
const [x, y, third, fourth] = measurement;
|
|
700
|
+
if (representation === "xcycwh") {
|
|
701
|
+
return {
|
|
702
|
+
height: Math.max(1e-3, fourth),
|
|
703
|
+
width: Math.max(1e-3, third),
|
|
704
|
+
x,
|
|
705
|
+
y,
|
|
706
|
+
};
|
|
707
|
+
}
|
|
708
|
+
const width = Math.sqrt(third * fourth);
|
|
709
|
+
const height = width === 0 ? 0 : third / width;
|
|
710
|
+
return {
|
|
711
|
+
height: Math.max(Number.EPSILON, height),
|
|
712
|
+
width: Math.max(Number.EPSILON, width),
|
|
713
|
+
x,
|
|
714
|
+
y,
|
|
715
|
+
};
|
|
716
|
+
}
|
|
717
|
+
function diagonal(values) {
|
|
718
|
+
return values.map((value, row) => values.map((_, column) => (row === column ? value : 0)));
|
|
719
|
+
}
|
|
720
|
+
function scaleMatrix(matrix, factor) {
|
|
721
|
+
return scale(matrix, factor);
|
|
722
|
+
}
|
|
723
|
+
function clone(matrix) {
|
|
724
|
+
return matrix.map((row) => [...row]);
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
const MINIMUM_DETECTION_CONFIDENCE = 0.1;
|
|
728
|
+
class CBIoUTrack {
|
|
729
|
+
trackerId;
|
|
730
|
+
successfulUpdates = 1;
|
|
731
|
+
timeSinceUpdate = 0;
|
|
732
|
+
estimator;
|
|
733
|
+
constructor(initial) {
|
|
734
|
+
this.estimator = new TrackingKalmanEstimator(initial.rect, "xcycwh");
|
|
735
|
+
this.setInitialNoise(initial.rect.width, initial.rect.height);
|
|
736
|
+
}
|
|
737
|
+
predict(frameStep, frameRate) {
|
|
738
|
+
const current = this.estimator.getRect();
|
|
739
|
+
this.estimator.setCovariances({
|
|
740
|
+
processNoise: this.buildProcessNoise(Math.max(current.width, 1e-3), Math.max(current.height, 1e-3)),
|
|
741
|
+
});
|
|
742
|
+
this.estimator.predict(frameStep, frameRate);
|
|
743
|
+
this.clampState();
|
|
744
|
+
this.timeSinceUpdate += 1;
|
|
745
|
+
}
|
|
746
|
+
update(detection) {
|
|
747
|
+
const current = this.estimator.getRect();
|
|
748
|
+
this.estimator.setCovariances({
|
|
749
|
+
measurementNoise: this.buildMeasurementNoise(Math.max(current.width, 1e-3), Math.max(current.height, 1e-3)),
|
|
750
|
+
});
|
|
751
|
+
this.estimator.update(detection.rect);
|
|
752
|
+
this.clampState();
|
|
753
|
+
this.timeSinceUpdate = 0;
|
|
754
|
+
this.successfulUpdates += 1;
|
|
755
|
+
}
|
|
756
|
+
getRect() {
|
|
757
|
+
return this.estimator.getRect();
|
|
758
|
+
}
|
|
759
|
+
setInitialNoise(width, height) {
|
|
760
|
+
const sigmaPosition = 0.05;
|
|
761
|
+
const sigmaVelocity = 0.00625;
|
|
762
|
+
this.estimator.setCovariances({
|
|
763
|
+
covariance: diagonal([
|
|
764
|
+
(2 * sigmaPosition * width) ** 2,
|
|
765
|
+
(2 * sigmaPosition * height) ** 2,
|
|
766
|
+
(2 * sigmaPosition * width) ** 2,
|
|
767
|
+
(2 * sigmaPosition * height) ** 2,
|
|
768
|
+
(10 * sigmaVelocity * width) ** 2,
|
|
769
|
+
(10 * sigmaVelocity * height) ** 2,
|
|
770
|
+
(10 * sigmaVelocity * width) ** 2,
|
|
771
|
+
(10 * sigmaVelocity * height) ** 2,
|
|
772
|
+
]),
|
|
773
|
+
measurementNoise: this.buildMeasurementNoise(width, height),
|
|
774
|
+
processNoise: this.buildProcessNoise(width, height),
|
|
775
|
+
});
|
|
776
|
+
}
|
|
777
|
+
buildProcessNoise(width, height) {
|
|
778
|
+
const sigmaPosition = 0.05;
|
|
779
|
+
const sigmaVelocity = 0.00625;
|
|
780
|
+
return diagonal([
|
|
781
|
+
(sigmaPosition * width) ** 2,
|
|
782
|
+
(sigmaPosition * height) ** 2,
|
|
783
|
+
(sigmaPosition * width) ** 2,
|
|
784
|
+
(sigmaPosition * height) ** 2,
|
|
785
|
+
(sigmaVelocity * width) ** 2,
|
|
786
|
+
(sigmaVelocity * height) ** 2,
|
|
787
|
+
(sigmaVelocity * width) ** 2,
|
|
788
|
+
(sigmaVelocity * height) ** 2,
|
|
789
|
+
]);
|
|
790
|
+
}
|
|
791
|
+
buildMeasurementNoise(width, height) {
|
|
792
|
+
const sigmaMeasurement = 0.05;
|
|
793
|
+
return diagonal([
|
|
794
|
+
(sigmaMeasurement * width) ** 2,
|
|
795
|
+
(sigmaMeasurement * height) ** 2,
|
|
796
|
+
(sigmaMeasurement * width) ** 2,
|
|
797
|
+
(sigmaMeasurement * height) ** 2,
|
|
798
|
+
]);
|
|
799
|
+
}
|
|
800
|
+
clampState() {
|
|
801
|
+
this.estimator.state[2][0] = Math.max(this.estimator.state[2][0], 1e-3);
|
|
802
|
+
this.estimator.state[3][0] = Math.max(this.estimator.state[3][0], 1e-3);
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
/** Creates the detection-only C-BIoU implementation from roboflow/trackers. */
|
|
806
|
+
function createCBIoUTracker$1(options = {}) {
|
|
807
|
+
const lostTrackBuffer = normalizeNonNegativeInteger$1(options.lostTrackBuffer ?? 30, "lostTrackBuffer");
|
|
808
|
+
const frameRate = options.frameRate ?? 30;
|
|
809
|
+
const trackActivationThreshold = options.trackActivationThreshold ?? 0.7;
|
|
810
|
+
const minimumConsecutiveFrames = normalizePositiveInteger$1(options.minimumConsecutiveFrames ?? 2, "minimumConsecutiveFrames");
|
|
811
|
+
const minimumIouThresholdFirstAssociation = options.minimumIouThresholdFirstAssociation ?? 0.2;
|
|
812
|
+
const minimumIouThresholdSecondAssociation = options.minimumIouThresholdSecondAssociation ?? 0.5;
|
|
813
|
+
const minimumIouThresholdUnconfirmedAssociation = options.minimumIouThresholdUnconfirmedAssociation ?? 0.3;
|
|
814
|
+
const highConfidenceDetectionThreshold = options.highConfidenceDetectionThreshold ?? 0.6;
|
|
815
|
+
const instantFirstFrameActivation = options.instantFirstFrameActivation ?? true;
|
|
816
|
+
const bufferRatioFirst = options.bufferRatioFirst ?? 0.3;
|
|
817
|
+
const bufferRatioSecond = options.bufferRatioSecond ?? 0.5;
|
|
818
|
+
if (!Number.isFinite(frameRate) || frameRate <= 0) {
|
|
819
|
+
throw new Error("frameRate must be a finite positive value.");
|
|
820
|
+
}
|
|
821
|
+
normalizeUnitInterval$1(trackActivationThreshold, "trackActivationThreshold");
|
|
822
|
+
normalizeUnitInterval$1(minimumIouThresholdFirstAssociation, "minimumIouThresholdFirstAssociation");
|
|
823
|
+
normalizeUnitInterval$1(minimumIouThresholdSecondAssociation, "minimumIouThresholdSecondAssociation");
|
|
824
|
+
normalizeUnitInterval$1(minimumIouThresholdUnconfirmedAssociation, "minimumIouThresholdUnconfirmedAssociation");
|
|
825
|
+
normalizeUnitInterval$1(highConfidenceDetectionThreshold, "highConfidenceDetectionThreshold");
|
|
826
|
+
if (!Number.isFinite(bufferRatioFirst) || bufferRatioFirst < 0) {
|
|
827
|
+
throw new Error("bufferRatioFirst must be a finite non-negative value.");
|
|
828
|
+
}
|
|
829
|
+
if (!Number.isFinite(bufferRatioSecond) || bufferRatioSecond < 0) {
|
|
830
|
+
throw new Error("bufferRatioSecond must be a finite non-negative value.");
|
|
831
|
+
}
|
|
832
|
+
const scaledLostTrackBuffer = (frameRate / 30) * lostTrackBuffer;
|
|
833
|
+
if (!Number.isFinite(scaledLostTrackBuffer)) {
|
|
834
|
+
throw new Error("Scaled lostTrackBuffer overflows: frameRate / 30 * lostTrackBuffer must be finite.");
|
|
835
|
+
}
|
|
836
|
+
const maximumFramesWithoutUpdate = lostTrackBuffer === 0 ? 0 : Math.max(1, Math.ceil(scaledLostTrackBuffer));
|
|
837
|
+
let tracks = [];
|
|
838
|
+
let nextTrackerId = 0;
|
|
839
|
+
let frameId = 0;
|
|
840
|
+
let previousFrameIndex;
|
|
841
|
+
return {
|
|
842
|
+
reset() {
|
|
843
|
+
tracks = [];
|
|
844
|
+
nextTrackerId = 0;
|
|
845
|
+
frameId = 0;
|
|
846
|
+
previousFrameIndex = undefined;
|
|
847
|
+
},
|
|
848
|
+
update(detections, frameIndex) {
|
|
849
|
+
const frameStep = resolveFrameStep(frameIndex, previousFrameIndex);
|
|
850
|
+
previousFrameIndex = frameIndex ?? previousFrameIndex;
|
|
851
|
+
frameId += 1;
|
|
852
|
+
tracks.forEach((track) => track.predict(frameStep, frameRate));
|
|
853
|
+
const highDetectionIndexes = [];
|
|
854
|
+
const lowDetectionIndexes = [];
|
|
855
|
+
detections.forEach((detection, index) => {
|
|
856
|
+
const confidence = detection.confidence ?? 1;
|
|
857
|
+
if (confidence >= highConfidenceDetectionThreshold) {
|
|
858
|
+
highDetectionIndexes.push(index);
|
|
859
|
+
}
|
|
860
|
+
else if (confidence > MINIMUM_DETECTION_CONFIDENCE) {
|
|
861
|
+
lowDetectionIndexes.push(index);
|
|
862
|
+
}
|
|
863
|
+
});
|
|
864
|
+
const confirmed = [];
|
|
865
|
+
const unconfirmed = [];
|
|
866
|
+
const lost = [];
|
|
867
|
+
for (const track of tracks) {
|
|
868
|
+
if (track.timeSinceUpdate > 1)
|
|
869
|
+
lost.push(track);
|
|
870
|
+
else if (track.trackerId !== undefined ||
|
|
871
|
+
track.successfulUpdates >= minimumConsecutiveFrames) {
|
|
872
|
+
confirmed.push(track);
|
|
873
|
+
}
|
|
874
|
+
else
|
|
875
|
+
unconfirmed.push(track);
|
|
876
|
+
}
|
|
877
|
+
const assignments = new Map();
|
|
878
|
+
const pool = [...confirmed, ...lost];
|
|
879
|
+
const highDetections = highDetectionIndexes.map((index) => detections[index]);
|
|
880
|
+
const firstScores = pairwiseIou(pool.map((track) => track.getRect()), highDetections.map((detection) => detection.rect), bufferRatioFirst).map((row) => row.map((score, index) => score * (highDetections[index].confidence ?? 1)));
|
|
881
|
+
const firstAssociation = associateTrackingScores(firstScores, pool.length, highDetections.length, minimumIouThresholdFirstAssociation);
|
|
882
|
+
for (const match of firstAssociation.matches) {
|
|
883
|
+
updateMatchedTrack(pool[match.trackIndex], highDetections[match.detectionIndex], assignments);
|
|
884
|
+
}
|
|
885
|
+
const remainingTracked = firstAssociation.unmatchedTrackIndexes
|
|
886
|
+
.map((index) => pool[index])
|
|
887
|
+
.filter((track) => track.timeSinceUpdate === 1);
|
|
888
|
+
const lowDetections = lowDetectionIndexes.map((index) => detections[index]);
|
|
889
|
+
const secondScores = pairwiseIou(remainingTracked.map((track) => track.getRect()), lowDetections.map((detection) => detection.rect), bufferRatioSecond);
|
|
890
|
+
const secondAssociation = associateTrackingScores(secondScores, remainingTracked.length, lowDetections.length, minimumIouThresholdSecondAssociation);
|
|
891
|
+
for (const match of secondAssociation.matches) {
|
|
892
|
+
updateMatchedTrack(remainingTracked[match.trackIndex], lowDetections[match.detectionIndex], assignments);
|
|
893
|
+
}
|
|
894
|
+
let unmatchedHighLocal = [...firstAssociation.unmatchedDetectionIndexes];
|
|
895
|
+
let unmatchedUnconfirmed = unconfirmed.map((_, index) => index);
|
|
896
|
+
if (unconfirmed.length > 0 && unmatchedHighLocal.length > 0) {
|
|
897
|
+
const remainingHigh = unmatchedHighLocal.map((index) => highDetections[index]);
|
|
898
|
+
const unconfirmedScores = pairwiseIou(unconfirmed.map((track) => track.getRect()), remainingHigh.map((detection) => detection.rect), bufferRatioFirst).map((row) => row.map((score, index) => score * (remainingHigh[index].confidence ?? 1)));
|
|
899
|
+
const unconfirmedAssociation = associateTrackingScores(unconfirmedScores, unconfirmed.length, remainingHigh.length, minimumIouThresholdUnconfirmedAssociation);
|
|
900
|
+
unmatchedUnconfirmed = unconfirmedAssociation.unmatchedTrackIndexes;
|
|
901
|
+
for (const match of unconfirmedAssociation.matches) {
|
|
902
|
+
updateMatchedTrack(unconfirmed[match.trackIndex], remainingHigh[match.detectionIndex], assignments);
|
|
903
|
+
}
|
|
904
|
+
unmatchedHighLocal =
|
|
905
|
+
unconfirmedAssociation.unmatchedDetectionIndexes.map((index) => unmatchedHighLocal[index]);
|
|
906
|
+
}
|
|
907
|
+
const unmatchedUnconfirmedTracks = new Set(unmatchedUnconfirmed.map((index) => unconfirmed[index]));
|
|
908
|
+
tracks = tracks.filter((track) => !unmatchedUnconfirmedTracks.has(track));
|
|
909
|
+
for (const localIndex of unmatchedHighLocal) {
|
|
910
|
+
const detection = highDetections[localIndex];
|
|
911
|
+
if ((detection.confidence ?? 1) < trackActivationThreshold)
|
|
912
|
+
continue;
|
|
913
|
+
const track = new CBIoUTrack(detection);
|
|
914
|
+
if (frameId === 1 && instantFirstFrameActivation) {
|
|
915
|
+
track.trackerId = nextTrackerId++;
|
|
916
|
+
assignments.set(detection.detectionIndex, track.trackerId);
|
|
917
|
+
}
|
|
918
|
+
tracks.push(track);
|
|
919
|
+
}
|
|
920
|
+
tracks = tracks.filter((track) => track.timeSinceUpdate <= maximumFramesWithoutUpdate &&
|
|
921
|
+
(track.timeSinceUpdate === 0 ||
|
|
922
|
+
track.trackerId !== undefined ||
|
|
923
|
+
track.successfulUpdates >= minimumConsecutiveFrames));
|
|
924
|
+
return createUpdate(detections, assignments, tracks);
|
|
925
|
+
},
|
|
926
|
+
};
|
|
927
|
+
function updateMatchedTrack(track, detection, assignments) {
|
|
928
|
+
track.update(detection);
|
|
929
|
+
if (track.trackerId === undefined &&
|
|
930
|
+
track.successfulUpdates >= minimumConsecutiveFrames) {
|
|
931
|
+
track.trackerId = nextTrackerId++;
|
|
932
|
+
}
|
|
933
|
+
if (track.trackerId !== undefined) {
|
|
934
|
+
assignments.set(detection.detectionIndex, track.trackerId);
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
function createUpdate(detections, assignments, tracks) {
|
|
939
|
+
const resolvedAssignments = detections.flatMap((detection) => {
|
|
940
|
+
const trackerId = assignments.get(detection.detectionIndex);
|
|
941
|
+
return trackerId === undefined
|
|
942
|
+
? []
|
|
943
|
+
: [{ detectionIndex: detection.detectionIndex, trackerId }];
|
|
944
|
+
});
|
|
945
|
+
return {
|
|
946
|
+
activeTrackCount: tracks.length,
|
|
947
|
+
assignments: resolvedAssignments,
|
|
948
|
+
confirmedTrackCount: tracks.filter((track) => track.trackerId !== undefined)
|
|
949
|
+
.length,
|
|
950
|
+
};
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
class OCSortTrack {
|
|
954
|
+
deltaT;
|
|
955
|
+
age = 0;
|
|
956
|
+
lastObservation;
|
|
957
|
+
trackerId;
|
|
958
|
+
successfulConsecutiveUpdates = 0;
|
|
959
|
+
timeSinceUpdate = 0;
|
|
960
|
+
velocity;
|
|
961
|
+
frozenState;
|
|
962
|
+
observed = true;
|
|
963
|
+
observations = new Map();
|
|
964
|
+
estimator;
|
|
965
|
+
constructor(initial, deltaT) {
|
|
966
|
+
this.deltaT = deltaT;
|
|
967
|
+
this.lastObservation = initial;
|
|
968
|
+
this.estimator = new TrackingKalmanEstimator(initial.rect, "xcycsr");
|
|
969
|
+
this.configureNoise();
|
|
970
|
+
}
|
|
971
|
+
predict(frameStep, frameRate) {
|
|
972
|
+
if (this.observed && this.timeSinceUpdate > 0) {
|
|
973
|
+
this.frozenState = this.estimator.snapshot();
|
|
974
|
+
this.observed = false;
|
|
975
|
+
}
|
|
976
|
+
this.estimator.predict(frameStep, frameRate);
|
|
977
|
+
if (this.timeSinceUpdate > 0) {
|
|
978
|
+
this.successfulConsecutiveUpdates = 0;
|
|
979
|
+
}
|
|
980
|
+
this.timeSinceUpdate += 1;
|
|
981
|
+
this.age += 1;
|
|
982
|
+
}
|
|
983
|
+
update(detection, frameStep, frameRate) {
|
|
984
|
+
const previous = this.getPreviousObservation();
|
|
985
|
+
if (previous) {
|
|
986
|
+
this.velocity = computeVelocity(previous, detection);
|
|
987
|
+
}
|
|
988
|
+
if (!this.observed && this.frozenState) {
|
|
989
|
+
this.unfreeze(detection, frameStep, frameRate);
|
|
990
|
+
}
|
|
991
|
+
this.estimator.update(detection.rect);
|
|
992
|
+
this.observed = true;
|
|
993
|
+
this.timeSinceUpdate = 0;
|
|
994
|
+
this.successfulConsecutiveUpdates += 1;
|
|
995
|
+
this.lastObservation = detection;
|
|
996
|
+
this.observations.set(this.age, detection);
|
|
997
|
+
const cutoff = this.age - this.deltaT;
|
|
998
|
+
for (const age of this.observations.keys()) {
|
|
999
|
+
if (age < cutoff)
|
|
1000
|
+
this.observations.delete(age);
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
getRect() {
|
|
1004
|
+
return this.estimator.getRect();
|
|
1005
|
+
}
|
|
1006
|
+
getPreviousObservation() {
|
|
1007
|
+
if (this.observations.size === 0)
|
|
1008
|
+
return undefined;
|
|
1009
|
+
for (let index = 0; index < this.deltaT; index += 1) {
|
|
1010
|
+
const delta = this.deltaT - index;
|
|
1011
|
+
const observation = this.observations.get(this.age - delta);
|
|
1012
|
+
if (observation)
|
|
1013
|
+
return observation;
|
|
1014
|
+
}
|
|
1015
|
+
const latestAge = Math.max(...this.observations.keys());
|
|
1016
|
+
return this.observations.get(latestAge);
|
|
1017
|
+
}
|
|
1018
|
+
configureNoise() {
|
|
1019
|
+
const measurementNoise = identity(4);
|
|
1020
|
+
for (let index = 2; index < 4; index += 1) {
|
|
1021
|
+
measurementNoise[index][index] *= 10;
|
|
1022
|
+
}
|
|
1023
|
+
const covariance = scaleMatrix(identity(7), 10);
|
|
1024
|
+
for (let index = 4; index < 7; index += 1) {
|
|
1025
|
+
covariance[index][index] *= 1000;
|
|
1026
|
+
}
|
|
1027
|
+
const processNoise = identity(7);
|
|
1028
|
+
processNoise[6][6] *= 0.01;
|
|
1029
|
+
for (let index = 4; index < 7; index += 1) {
|
|
1030
|
+
processNoise[index][index] *= 0.01;
|
|
1031
|
+
}
|
|
1032
|
+
this.estimator.setCovariances({
|
|
1033
|
+
covariance,
|
|
1034
|
+
measurementNoise,
|
|
1035
|
+
processNoise,
|
|
1036
|
+
});
|
|
1037
|
+
}
|
|
1038
|
+
unfreeze(detection, frameStep, frameRate) {
|
|
1039
|
+
if (!this.frozenState || this.timeSinceUpdate === 0)
|
|
1040
|
+
return;
|
|
1041
|
+
this.estimator.restore(this.frozenState);
|
|
1042
|
+
const timeGap = this.timeSinceUpdate;
|
|
1043
|
+
const start = rectToMeasurement(this.lastObservation.rect, "xcycsr");
|
|
1044
|
+
const end = rectToMeasurement(detection.rect, "xcycsr");
|
|
1045
|
+
const startWidth = Math.sqrt(start[2] * start[3]);
|
|
1046
|
+
const startHeight = start[3] === 0 ? 0 : Math.sqrt(start[2] / start[3]);
|
|
1047
|
+
const endWidth = Math.sqrt(end[2] * end[3]);
|
|
1048
|
+
const endHeight = end[3] === 0 ? 0 : Math.sqrt(end[2] / end[3]);
|
|
1049
|
+
for (let index = 0; index < timeGap; index += 1) {
|
|
1050
|
+
const progress = (index + 1) / timeGap;
|
|
1051
|
+
const x = start[0] + progress * (end[0] - start[0]);
|
|
1052
|
+
const y = start[1] + progress * (end[1] - start[1]);
|
|
1053
|
+
const width = startWidth + progress * (endWidth - startWidth);
|
|
1054
|
+
const height = startHeight + progress * (endHeight - startHeight);
|
|
1055
|
+
this.estimator.updateMeasurement([x, y, width * height, width / height]);
|
|
1056
|
+
if (index < timeGap - 1) {
|
|
1057
|
+
this.estimator.predict(frameStep, frameRate);
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
this.frozenState = undefined;
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
/** Creates the observation-centric SORT implementation from roboflow/trackers. */
|
|
1064
|
+
function createOCSortTracker$1(options = {}) {
|
|
1065
|
+
const lostTrackBuffer = normalizeNonNegativeInteger$1(options.lostTrackBuffer ?? 30, "lostTrackBuffer");
|
|
1066
|
+
const frameRate = options.frameRate ?? 30;
|
|
1067
|
+
const minimumConsecutiveFrames = normalizePositiveInteger$1(options.minimumConsecutiveFrames ?? 3, "minimumConsecutiveFrames");
|
|
1068
|
+
const minimumIouThreshold = options.minimumIouThreshold ?? 0.3;
|
|
1069
|
+
const directionConsistencyWeight = options.directionConsistencyWeight ?? 0.2;
|
|
1070
|
+
const highConfidenceDetectionThreshold = options.highConfidenceDetectionThreshold ?? 0.6;
|
|
1071
|
+
const deltaT = normalizePositiveInteger$1(options.deltaT ?? 3, "deltaT");
|
|
1072
|
+
if (!Number.isFinite(frameRate) || frameRate <= 0) {
|
|
1073
|
+
throw new Error("frameRate must be a finite positive value.");
|
|
1074
|
+
}
|
|
1075
|
+
normalizeUnitInterval$1(minimumIouThreshold, "minimumIouThreshold");
|
|
1076
|
+
normalizeUnitInterval$1(directionConsistencyWeight, "directionConsistencyWeight");
|
|
1077
|
+
normalizeUnitInterval$1(highConfidenceDetectionThreshold, "highConfidenceDetectionThreshold");
|
|
1078
|
+
const scaledLostTrackBuffer = (frameRate / 30) * lostTrackBuffer;
|
|
1079
|
+
if (!Number.isFinite(scaledLostTrackBuffer)) {
|
|
1080
|
+
throw new Error("Scaled lostTrackBuffer overflows: frameRate / 30 * lostTrackBuffer must be finite.");
|
|
1081
|
+
}
|
|
1082
|
+
const maximumFramesWithoutUpdate = lostTrackBuffer === 0 ? 0 : Math.max(1, Math.ceil(scaledLostTrackBuffer));
|
|
1083
|
+
let tracks = [];
|
|
1084
|
+
let frameCount = 0;
|
|
1085
|
+
let nextTrackerId = 0;
|
|
1086
|
+
let previousFrameIndex;
|
|
1087
|
+
return {
|
|
1088
|
+
reset() {
|
|
1089
|
+
tracks = [];
|
|
1090
|
+
frameCount = 0;
|
|
1091
|
+
nextTrackerId = 0;
|
|
1092
|
+
previousFrameIndex = undefined;
|
|
1093
|
+
},
|
|
1094
|
+
update(detections, frameIndex) {
|
|
1095
|
+
const frameStep = resolveFrameStep(frameIndex, previousFrameIndex);
|
|
1096
|
+
previousFrameIndex = frameIndex ?? previousFrameIndex;
|
|
1097
|
+
// Match roboflow/trackers: an empty stream before any track exists is
|
|
1098
|
+
// not part of OC-SORT's early-sequence activation window.
|
|
1099
|
+
if (tracks.length === 0 && detections.length === 0) {
|
|
1100
|
+
return {
|
|
1101
|
+
activeTrackCount: 0,
|
|
1102
|
+
assignments: [],
|
|
1103
|
+
confirmedTrackCount: 0,
|
|
1104
|
+
};
|
|
1105
|
+
}
|
|
1106
|
+
tracks.forEach((track) => track.predict(frameStep, frameRate));
|
|
1107
|
+
const highDetectionIndexes = detections.flatMap((detection, index) => detection.confidence === undefined ||
|
|
1108
|
+
detection.confidence >= highConfidenceDetectionThreshold
|
|
1109
|
+
? [index]
|
|
1110
|
+
: []);
|
|
1111
|
+
const highDetections = highDetectionIndexes.map((index) => detections[index]);
|
|
1112
|
+
const iouScores = pairwiseIou(tracks.map((track) => track.getRect()), highDetections.map((detection) => detection.rect));
|
|
1113
|
+
const combinedScores = iouScores.map((row, trackIndex) => row.map((iou, detectionIndex) => {
|
|
1114
|
+
if (directionConsistencyWeight === 0)
|
|
1115
|
+
return iou;
|
|
1116
|
+
return (iou +
|
|
1117
|
+
directionConsistencyWeight *
|
|
1118
|
+
directionConsistency(tracks[trackIndex], highDetections[detectionIndex]) *
|
|
1119
|
+
(highDetections[detectionIndex].confidence ?? 1));
|
|
1120
|
+
}));
|
|
1121
|
+
const primary = associateTrackingScores(combinedScores, tracks.length, highDetections.length, minimumIouThreshold, iouScores);
|
|
1122
|
+
const assignments = new Map();
|
|
1123
|
+
for (const match of primary.matches) {
|
|
1124
|
+
updateMatchedTrack(tracks[match.trackIndex], highDetections[match.detectionIndex], assignments, frameStep);
|
|
1125
|
+
}
|
|
1126
|
+
let remainingHigh = [...primary.unmatchedDetectionIndexes];
|
|
1127
|
+
if (primary.unmatchedTrackIndexes.length > 0 &&
|
|
1128
|
+
remainingHigh.length > 0) {
|
|
1129
|
+
const unmatchedTracks = primary.unmatchedTrackIndexes.map((index) => tracks[index]);
|
|
1130
|
+
const unmatchedDetections = remainingHigh.map((index) => highDetections[index]);
|
|
1131
|
+
const recoveryScores = pairwiseIou(unmatchedTracks.map((track) => track.lastObservation.rect), unmatchedDetections.map((detection) => detection.rect));
|
|
1132
|
+
const recovery = associateTrackingScores(recoveryScores, unmatchedTracks.length, unmatchedDetections.length, minimumIouThreshold);
|
|
1133
|
+
for (const match of recovery.matches) {
|
|
1134
|
+
updateMatchedTrack(unmatchedTracks[match.trackIndex], unmatchedDetections[match.detectionIndex], assignments, frameStep);
|
|
1135
|
+
}
|
|
1136
|
+
remainingHigh = recovery.unmatchedDetectionIndexes.map((index) => remainingHigh[index]);
|
|
1137
|
+
}
|
|
1138
|
+
for (const localIndex of remainingHigh) {
|
|
1139
|
+
tracks.push(new OCSortTrack(highDetections[localIndex], deltaT));
|
|
1140
|
+
}
|
|
1141
|
+
tracks = tracks.filter((track) => track.timeSinceUpdate <= maximumFramesWithoutUpdate);
|
|
1142
|
+
frameCount += 1;
|
|
1143
|
+
const resolvedAssignments = detections.flatMap((detection) => {
|
|
1144
|
+
const trackerId = assignments.get(detection.detectionIndex);
|
|
1145
|
+
return trackerId === undefined
|
|
1146
|
+
? []
|
|
1147
|
+
: [{ detectionIndex: detection.detectionIndex, trackerId }];
|
|
1148
|
+
});
|
|
1149
|
+
return {
|
|
1150
|
+
activeTrackCount: tracks.length,
|
|
1151
|
+
assignments: resolvedAssignments,
|
|
1152
|
+
confirmedTrackCount: tracks.filter((track) => track.trackerId !== undefined).length,
|
|
1153
|
+
};
|
|
1154
|
+
},
|
|
1155
|
+
};
|
|
1156
|
+
function updateMatchedTrack(track, detection, assignments, frameStep) {
|
|
1157
|
+
track.update(detection, frameStep, frameRate);
|
|
1158
|
+
const earlySequence = frameCount <= minimumConsecutiveFrames;
|
|
1159
|
+
const shouldEmit = (earlySequence && track.timeSinceUpdate === 0) ||
|
|
1160
|
+
track.successfulConsecutiveUpdates >= minimumConsecutiveFrames;
|
|
1161
|
+
if (shouldEmit) {
|
|
1162
|
+
if (track.trackerId === undefined) {
|
|
1163
|
+
track.trackerId = nextTrackerId++;
|
|
1164
|
+
}
|
|
1165
|
+
assignments.set(detection.detectionIndex, track.trackerId);
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
function computeVelocity(previous, current) {
|
|
1170
|
+
const deltaX = current.rect.x - previous.rect.x;
|
|
1171
|
+
const deltaY = current.rect.y - previous.rect.y;
|
|
1172
|
+
const norm = Math.sqrt(deltaX * deltaX + deltaY * deltaY) + 1e-6;
|
|
1173
|
+
return { x: deltaX / norm, y: deltaY / norm };
|
|
1174
|
+
}
|
|
1175
|
+
function directionConsistency(track, detection) {
|
|
1176
|
+
if (!track.velocity)
|
|
1177
|
+
return 0;
|
|
1178
|
+
const reference = track.getPreviousObservation() ?? track.lastObservation;
|
|
1179
|
+
const deltaX = detection.rect.x - reference.rect.x;
|
|
1180
|
+
const deltaY = detection.rect.y - reference.rect.y;
|
|
1181
|
+
const norm = Math.sqrt(deltaX * deltaX + deltaY * deltaY) + 1e-6;
|
|
1182
|
+
const cosine = Math.max(-1, Math.min(1, track.velocity.x * (deltaX / norm) + track.velocity.y * (deltaY / norm)));
|
|
1183
|
+
const angle = Math.acos(cosine);
|
|
1184
|
+
return (Math.PI / 2 - Math.abs(angle)) / Math.PI;
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
/** Public core facade for the internal tracker engine workspace. */
|
|
1188
|
+
function createSortTracker(options = {}) {
|
|
1189
|
+
return createSortTracker$1(options);
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
/** Public core facade for the internal tracker engine workspace. */
|
|
1193
|
+
function createByteTrackTracker(options = {}) {
|
|
1194
|
+
return createByteTrackTracker$1(options);
|
|
1195
|
+
}
|
|
1196
|
+
|
|
1197
|
+
/** Public core facade for the internal tracker engine workspace. */
|
|
1198
|
+
function createCBIoUTracker(options = {}) {
|
|
1199
|
+
return createCBIoUTracker$1(options);
|
|
1200
|
+
}
|
|
1201
|
+
|
|
1202
|
+
/** Public core facade for the internal tracker engine workspace. */
|
|
1203
|
+
function createOCSortTracker(options = {}) {
|
|
1204
|
+
return createOCSortTracker$1(options);
|
|
1205
|
+
}
|
|
1206
|
+
|
|
1207
|
+
/** Geometry projected into a tracker. The original detection payload is retained. */
|
|
1208
|
+
var TrackingGeometry;
|
|
1209
|
+
(function (TrackingGeometry) {
|
|
1210
|
+
TrackingGeometry["Box"] = "box";
|
|
1211
|
+
TrackingGeometry["Mask"] = "mask";
|
|
1212
|
+
TrackingGeometry["Keypoints"] = "keypoints";
|
|
1213
|
+
})(TrackingGeometry || (TrackingGeometry = {}));
|
|
1214
|
+
|
|
1215
|
+
var DetectionMaskPayloadFormat;
|
|
1216
|
+
(function (DetectionMaskPayloadFormat) {
|
|
1217
|
+
DetectionMaskPayloadFormat["RawCocoRle"] = "rawCocoRle";
|
|
1218
|
+
DetectionMaskPayloadFormat["DeflatedBase64"] = "deflatedBase64";
|
|
1219
|
+
})(DetectionMaskPayloadFormat || (DetectionMaskPayloadFormat = {}));
|
|
1220
|
+
|
|
1221
|
+
var DetectionPickTarget;
|
|
1222
|
+
(function (DetectionPickTarget) {
|
|
1223
|
+
DetectionPickTarget["Box"] = "box";
|
|
1224
|
+
DetectionPickTarget["Edge"] = "edge";
|
|
1225
|
+
DetectionPickTarget["Keypoint"] = "keypoint";
|
|
1226
|
+
DetectionPickTarget["Label"] = "label";
|
|
1227
|
+
DetectionPickTarget["Mask"] = "mask";
|
|
1228
|
+
DetectionPickTarget["Polygon"] = "polygon";
|
|
1229
|
+
DetectionPickTarget["Polyline"] = "polyline";
|
|
1230
|
+
})(DetectionPickTarget || (DetectionPickTarget = {}));
|
|
1231
|
+
var MediaInteractionMode;
|
|
1232
|
+
(function (MediaInteractionMode) {
|
|
1233
|
+
MediaInteractionMode["Always"] = "always";
|
|
1234
|
+
MediaInteractionMode["Disabled"] = "disabled";
|
|
1235
|
+
MediaInteractionMode["PausedOnly"] = "pausedOnly";
|
|
1236
|
+
})(MediaInteractionMode || (MediaInteractionMode = {}));
|
|
1237
|
+
|
|
1238
|
+
var AnnotationGeometryKind;
|
|
1239
|
+
(function (AnnotationGeometryKind) {
|
|
1240
|
+
AnnotationGeometryKind["Box"] = "box";
|
|
1241
|
+
AnnotationGeometryKind["Polygon"] = "polygon";
|
|
1242
|
+
AnnotationGeometryKind["Polyline"] = "polyline";
|
|
1243
|
+
AnnotationGeometryKind["Keypoints"] = "keypoints";
|
|
1244
|
+
AnnotationGeometryKind["Mask"] = "mask";
|
|
1245
|
+
})(AnnotationGeometryKind || (AnnotationGeometryKind = {}));
|
|
1246
|
+
var AnnotationGestureStateKind;
|
|
1247
|
+
(function (AnnotationGestureStateKind) {
|
|
1248
|
+
AnnotationGestureStateKind["Idle"] = "idle";
|
|
1249
|
+
AnnotationGestureStateKind["Creating"] = "creating";
|
|
1250
|
+
AnnotationGestureStateKind["Moving"] = "moving";
|
|
1251
|
+
AnnotationGestureStateKind["Resizing"] = "resizing";
|
|
1252
|
+
AnnotationGestureStateKind["DragSelecting"] = "dragSelecting";
|
|
1253
|
+
})(AnnotationGestureStateKind || (AnnotationGestureStateKind = {}));
|
|
1254
|
+
var AnnotationHandleKind;
|
|
1255
|
+
(function (AnnotationHandleKind) {
|
|
1256
|
+
AnnotationHandleKind["Resize"] = "resize";
|
|
1257
|
+
AnnotationHandleKind["Vertex"] = "vertex";
|
|
1258
|
+
AnnotationHandleKind["AddVertex"] = "addVertex";
|
|
1259
|
+
AnnotationHandleKind["Keypoint"] = "keypoint";
|
|
1260
|
+
})(AnnotationHandleKind || (AnnotationHandleKind = {}));
|
|
1261
|
+
|
|
1262
|
+
/** Alignment for strokes applied to closed rendered geometry. */
|
|
1263
|
+
var StrokeAlignment;
|
|
1264
|
+
(function (StrokeAlignment) {
|
|
1265
|
+
StrokeAlignment["Inside"] = "inside";
|
|
1266
|
+
StrokeAlignment["Center"] = "center";
|
|
1267
|
+
StrokeAlignment["Outside"] = "outside";
|
|
1268
|
+
})(StrokeAlignment || (StrokeAlignment = {}));
|
|
1269
|
+
|
|
1270
|
+
var BoxShape;
|
|
1271
|
+
(function (BoxShape) {
|
|
1272
|
+
BoxShape["Rect"] = "rect";
|
|
1273
|
+
BoxShape["RoundedRect"] = "roundedRect";
|
|
1274
|
+
})(BoxShape || (BoxShape = {}));
|
|
1275
|
+
|
|
1276
|
+
var FocusTargetMode;
|
|
1277
|
+
(function (FocusTargetMode) {
|
|
1278
|
+
FocusTargetMode["Hovered"] = "hovered";
|
|
1279
|
+
FocusTargetMode["Selected"] = "selected";
|
|
1280
|
+
FocusTargetMode["HoveredAndSelected"] = "hoveredAndSelected";
|
|
1281
|
+
FocusTargetMode["Ambient"] = "ambient";
|
|
1282
|
+
})(FocusTargetMode || (FocusTargetMode = {}));
|
|
1283
|
+
|
|
1284
|
+
var DetectionInteractionState;
|
|
1285
|
+
(function (DetectionInteractionState) {
|
|
1286
|
+
DetectionInteractionState["Hovered"] = "hovered";
|
|
1287
|
+
DetectionInteractionState["Selected"] = "selected";
|
|
1288
|
+
})(DetectionInteractionState || (DetectionInteractionState = {}));
|
|
1289
|
+
|
|
1290
|
+
var LabelPlacement;
|
|
1291
|
+
(function (LabelPlacement) {
|
|
1292
|
+
LabelPlacement["Top"] = "top";
|
|
1293
|
+
LabelPlacement["Bottom"] = "bottom";
|
|
1294
|
+
LabelPlacement["InsideTop"] = "insideTop";
|
|
1295
|
+
LabelPlacement["InsideBottom"] = "insideBottom";
|
|
1296
|
+
LabelPlacement["Center"] = "center";
|
|
1297
|
+
})(LabelPlacement || (LabelPlacement = {}));
|
|
1298
|
+
var LabelVisibilityMode;
|
|
1299
|
+
(function (LabelVisibilityMode) {
|
|
1300
|
+
LabelVisibilityMode["Always"] = "always";
|
|
1301
|
+
LabelVisibilityMode["HoveredOnly"] = "hoveredOnly";
|
|
1302
|
+
})(LabelVisibilityMode || (LabelVisibilityMode = {}));
|
|
1303
|
+
|
|
1304
|
+
var MaskRenderMode;
|
|
1305
|
+
(function (MaskRenderMode) {
|
|
1306
|
+
MaskRenderMode["FillAndStroke"] = "fillAndStroke";
|
|
1307
|
+
MaskRenderMode["FillOnly"] = "fillOnly";
|
|
1308
|
+
MaskRenderMode["StrokeOnly"] = "strokeOnly";
|
|
1309
|
+
})(MaskRenderMode || (MaskRenderMode = {}));
|
|
1310
|
+
|
|
1311
|
+
/**
|
|
1312
|
+
* Discriminator for renderer-neutral shape draw instructions.
|
|
1313
|
+
*/
|
|
1314
|
+
var ShapeInstructionKind;
|
|
1315
|
+
(function (ShapeInstructionKind) {
|
|
1316
|
+
ShapeInstructionKind["Ellipse"] = "ellipse";
|
|
1317
|
+
ShapeInstructionKind["Marker"] = "marker";
|
|
1318
|
+
ShapeInstructionKind["Path"] = "path";
|
|
1319
|
+
})(ShapeInstructionKind || (ShapeInstructionKind = {}));
|
|
1320
|
+
var MarkerShape;
|
|
1321
|
+
(function (MarkerShape) {
|
|
1322
|
+
MarkerShape["Circle"] = "circle";
|
|
1323
|
+
MarkerShape["Cross"] = "cross";
|
|
1324
|
+
MarkerShape["Square"] = "square";
|
|
1325
|
+
MarkerShape["Triangle"] = "triangle";
|
|
1326
|
+
})(MarkerShape || (MarkerShape = {}));
|
|
1327
|
+
/**
|
|
1328
|
+
* Coordinate space for marker sizing. Media sizes scale with the viewport;
|
|
1329
|
+
* screen sizes stay constant on screen, matching stroke-width semantics.
|
|
1330
|
+
*/
|
|
1331
|
+
var MarkerSizeSpace;
|
|
1332
|
+
(function (MarkerSizeSpace) {
|
|
1333
|
+
MarkerSizeSpace["Media"] = "media";
|
|
1334
|
+
MarkerSizeSpace["Screen"] = "screen";
|
|
1335
|
+
})(MarkerSizeSpace || (MarkerSizeSpace = {}));
|
|
1336
|
+
|
|
1337
|
+
var KeypointMarkerShape;
|
|
1338
|
+
(function (KeypointMarkerShape) {
|
|
1339
|
+
KeypointMarkerShape["Circle"] = "circle";
|
|
1340
|
+
KeypointMarkerShape["Cross"] = "cross";
|
|
1341
|
+
})(KeypointMarkerShape || (KeypointMarkerShape = {}));
|
|
1342
|
+
|
|
1343
|
+
var MediaRendererFit;
|
|
1344
|
+
(function (MediaRendererFit) {
|
|
1345
|
+
/**
|
|
1346
|
+
* Preserve media aspect ratio and fit the full frame inside the canvas.
|
|
1347
|
+
*/
|
|
1348
|
+
MediaRendererFit["Contain"] = "contain";
|
|
1349
|
+
/**
|
|
1350
|
+
* Preserve media aspect ratio and fill the canvas, cropping if necessary.
|
|
1351
|
+
*/
|
|
1352
|
+
MediaRendererFit["Cover"] = "cover";
|
|
1353
|
+
})(MediaRendererFit || (MediaRendererFit = {}));
|
|
1354
|
+
/**
|
|
1355
|
+
* Playback lifecycle state reported by a platform renderer.
|
|
1356
|
+
*/
|
|
1357
|
+
var MediaRendererPlaybackState;
|
|
1358
|
+
(function (MediaRendererPlaybackState) {
|
|
1359
|
+
MediaRendererPlaybackState["Loading"] = "loading";
|
|
1360
|
+
MediaRendererPlaybackState["Ready"] = "ready";
|
|
1361
|
+
MediaRendererPlaybackState["Playing"] = "playing";
|
|
1362
|
+
MediaRendererPlaybackState["Buffering"] = "buffering";
|
|
1363
|
+
MediaRendererPlaybackState["Paused"] = "paused";
|
|
1364
|
+
MediaRendererPlaybackState["Error"] = "error";
|
|
1365
|
+
MediaRendererPlaybackState["Destroyed"] = "destroyed";
|
|
1366
|
+
})(MediaRendererPlaybackState || (MediaRendererPlaybackState = {}));
|
|
1367
|
+
/**
|
|
1368
|
+
* Stable classification of a media failure.
|
|
1369
|
+
*
|
|
1370
|
+
* Applications branch on these kinds instead of parsing decoder, demuxer, or
|
|
1371
|
+
* container error text, and still own their localized user-facing copy. New
|
|
1372
|
+
* kinds may be added over time, so treat unrecognized values like `Unknown`.
|
|
1373
|
+
*/
|
|
1374
|
+
var MediaErrorKind;
|
|
1375
|
+
(function (MediaErrorKind) {
|
|
1376
|
+
/** The media could not be opened or read at all. */
|
|
1377
|
+
MediaErrorKind["Unreadable"] = "unreadable";
|
|
1378
|
+
/** The container or codec is not supported by this platform. */
|
|
1379
|
+
MediaErrorKind["UnsupportedFormat"] = "unsupportedFormat";
|
|
1380
|
+
/** The media opened but carries no usable video track. */
|
|
1381
|
+
MediaErrorKind["NoVideoTrack"] = "noVideoTrack";
|
|
1382
|
+
/** Decoding a sample failed after the source opened. */
|
|
1383
|
+
MediaErrorKind["Decode"] = "decode";
|
|
1384
|
+
/** The media could not be fetched. */
|
|
1385
|
+
MediaErrorKind["Network"] = "network";
|
|
1386
|
+
/** The host environment lacks the APIs this media source requires. */
|
|
1387
|
+
MediaErrorKind["EnvironmentUnsupported"] = "environmentUnsupported";
|
|
1388
|
+
/** The failure could not be classified. */
|
|
1389
|
+
MediaErrorKind["Unknown"] = "unknown";
|
|
1390
|
+
})(MediaErrorKind || (MediaErrorKind = {}));
|
|
1391
|
+
/**
|
|
1392
|
+
* Lower-level media source readiness.
|
|
1393
|
+
*/
|
|
1394
|
+
var MediaSourceStatus;
|
|
1395
|
+
(function (MediaSourceStatus) {
|
|
1396
|
+
MediaSourceStatus["Loading"] = "loading";
|
|
1397
|
+
MediaSourceStatus["Ready"] = "ready";
|
|
1398
|
+
MediaSourceStatus["Error"] = "error";
|
|
1399
|
+
MediaSourceStatus["Destroyed"] = "destroyed";
|
|
1400
|
+
})(MediaSourceStatus || (MediaSourceStatus = {}));
|
|
1401
|
+
|
|
1402
|
+
/**
|
|
1403
|
+
* Media-session operating mode.
|
|
1404
|
+
*
|
|
1405
|
+
* Core owns the names because file-like and stream-like lifecycle choices are
|
|
1406
|
+
* platform-neutral. Platform packages decide how these modes tune media,
|
|
1407
|
+
* storage, and renderer defaults.
|
|
1408
|
+
*/
|
|
1409
|
+
var MediaSessionMode;
|
|
1410
|
+
(function (MediaSessionMode) {
|
|
1411
|
+
/**
|
|
1412
|
+
* Finite media. Defaults usually favor seek/replay and persistent detection
|
|
1413
|
+
* storage.
|
|
1414
|
+
*/
|
|
1415
|
+
MediaSessionMode["File"] = "file";
|
|
1416
|
+
/**
|
|
1417
|
+
* Live or append-only media. Defaults usually favor rolling windows and
|
|
1418
|
+
* bounded retention.
|
|
1419
|
+
*/
|
|
1420
|
+
MediaSessionMode["Stream"] = "stream";
|
|
1421
|
+
})(MediaSessionMode || (MediaSessionMode = {}));
|
|
1422
|
+
/**
|
|
1423
|
+
* Aggregate lifecycle state for a media session.
|
|
1424
|
+
*/
|
|
1425
|
+
var MediaSessionStatus;
|
|
1426
|
+
(function (MediaSessionStatus) {
|
|
1427
|
+
MediaSessionStatus["Buffering"] = "buffering";
|
|
1428
|
+
MediaSessionStatus["Destroyed"] = "destroyed";
|
|
1429
|
+
MediaSessionStatus["Error"] = "error";
|
|
1430
|
+
MediaSessionStatus["Loading"] = "loading";
|
|
1431
|
+
MediaSessionStatus["Paused"] = "paused";
|
|
1432
|
+
MediaSessionStatus["Playing"] = "playing";
|
|
1433
|
+
MediaSessionStatus["Processing"] = "processing";
|
|
1434
|
+
MediaSessionStatus["Ready"] = "ready";
|
|
1435
|
+
})(MediaSessionStatus || (MediaSessionStatus = {}));
|
|
1436
|
+
/**
|
|
1437
|
+
* Subsystem currently affecting session readiness or presentation.
|
|
1438
|
+
*/
|
|
1439
|
+
var MediaSessionActivityKind;
|
|
1440
|
+
(function (MediaSessionActivityKind) {
|
|
1441
|
+
MediaSessionActivityKind["DetectionsBuffering"] = "detectionsBuffering";
|
|
1442
|
+
MediaSessionActivityKind["DetectionsLoading"] = "detectionsLoading";
|
|
1443
|
+
MediaSessionActivityKind["Error"] = "error";
|
|
1444
|
+
MediaSessionActivityKind["MediaNormalizing"] = "mediaNormalizing";
|
|
1445
|
+
MediaSessionActivityKind["MediaOpening"] = "mediaOpening";
|
|
1446
|
+
MediaSessionActivityKind["PlaybackBuffering"] = "playbackBuffering";
|
|
1447
|
+
MediaSessionActivityKind["RenderPreparing"] = "renderPreparing";
|
|
1448
|
+
})(MediaSessionActivityKind || (MediaSessionActivityKind = {}));
|
|
1449
|
+
/**
|
|
1450
|
+
* State of one session activity.
|
|
1451
|
+
*/
|
|
1452
|
+
var MediaSessionActivityStatus;
|
|
1453
|
+
(function (MediaSessionActivityStatus) {
|
|
1454
|
+
MediaSessionActivityStatus["Error"] = "error";
|
|
1455
|
+
MediaSessionActivityStatus["Running"] = "running";
|
|
1456
|
+
MediaSessionActivityStatus["Waiting"] = "waiting";
|
|
1457
|
+
})(MediaSessionActivityStatus || (MediaSessionActivityStatus = {}));
|
|
1458
|
+
|
|
1459
|
+
let tracker;
|
|
1460
|
+
self.addEventListener("message", (event) => {
|
|
1461
|
+
const request = event.data;
|
|
1462
|
+
try {
|
|
1463
|
+
let response;
|
|
1464
|
+
if (request.type === "configure") {
|
|
1465
|
+
switch (request.processor.algorithm) {
|
|
1466
|
+
case "bytetrack":
|
|
1467
|
+
tracker = createByteTrackTracker(request.processor.options);
|
|
1468
|
+
break;
|
|
1469
|
+
case "cbiou":
|
|
1470
|
+
tracker = createCBIoUTracker(request.processor.options);
|
|
1471
|
+
break;
|
|
1472
|
+
case "ocsort":
|
|
1473
|
+
tracker = createOCSortTracker(request.processor.options);
|
|
1474
|
+
break;
|
|
1475
|
+
default:
|
|
1476
|
+
tracker = createSortTracker(request.processor.options);
|
|
1477
|
+
}
|
|
1478
|
+
response = { requestId: request.requestId, type: "success" };
|
|
1479
|
+
}
|
|
1480
|
+
else if (request.type === "reset") {
|
|
1481
|
+
tracker?.reset();
|
|
1482
|
+
response = { requestId: request.requestId, type: "success" };
|
|
1483
|
+
}
|
|
1484
|
+
else {
|
|
1485
|
+
if (!tracker) {
|
|
1486
|
+
throw new Error("Tracking worker is not configured.");
|
|
1487
|
+
}
|
|
1488
|
+
const startedAt = performance.now();
|
|
1489
|
+
const update = tracker.update(request.detections, request.frameIndex);
|
|
1490
|
+
response = {
|
|
1491
|
+
...update,
|
|
1492
|
+
durationMs: performance.now() - startedAt,
|
|
1493
|
+
requestId: request.requestId,
|
|
1494
|
+
type: "success",
|
|
1495
|
+
};
|
|
1496
|
+
}
|
|
1497
|
+
self.postMessage(response);
|
|
1498
|
+
}
|
|
1499
|
+
catch (error) {
|
|
1500
|
+
self.postMessage({
|
|
1501
|
+
message: error instanceof Error ? error.message : "Tracking worker failed.",
|
|
1502
|
+
requestId: request.requestId,
|
|
1503
|
+
type: "error",
|
|
1504
|
+
});
|
|
1505
|
+
}
|
|
1506
|
+
});
|
|
1507
|
+
|
|
1508
|
+
})();
|
|
1509
|
+
//# sourceMappingURL=tracking.worker.js.map
|