@camstack/lib-pipeline-analysis 0.1.1 → 0.1.3
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.js +1412 -1222
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1473 -1208
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,1279 +1,1544 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const dy1 = a.p2.y - a.p1.y;
|
|
20
|
-
const dx2 = b.p2.x - b.p1.x;
|
|
21
|
-
const dy2 = b.p2.y - b.p1.y;
|
|
22
|
-
const denom = dx1 * dy2 - dy1 * dx2;
|
|
23
|
-
if (Math.abs(denom) < 1e-10) return null;
|
|
24
|
-
const dx3 = b.p1.x - a.p1.x;
|
|
25
|
-
const dy3 = b.p1.y - a.p1.y;
|
|
26
|
-
const t = (dx3 * dy2 - dy3 * dx2) / denom;
|
|
27
|
-
const u = (dx3 * dy1 - dy3 * dx1) / denom;
|
|
28
|
-
if (t < 0 || t > 1 || u < 0 || u > 1) return null;
|
|
29
|
-
return {
|
|
30
|
-
x: a.p1.x + t * dx1,
|
|
31
|
-
y: a.p1.y + t * dy1
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
function tripwireCrossing(prev, curr, tripwire) {
|
|
35
|
-
const movement = { p1: prev, p2: curr };
|
|
36
|
-
const intersection = lineIntersection(movement, tripwire);
|
|
37
|
-
if (intersection === null) return null;
|
|
38
|
-
const twDx = tripwire.p2.x - tripwire.p1.x;
|
|
39
|
-
const twDy = tripwire.p2.y - tripwire.p1.y;
|
|
40
|
-
const movDx = curr.x - prev.x;
|
|
41
|
-
const movDy = curr.y - prev.y;
|
|
42
|
-
const cross = twDx * movDy - twDy * movDx;
|
|
43
|
-
const direction = cross > 0 ? "left" : "right";
|
|
44
|
-
return { crossed: true, direction };
|
|
45
|
-
}
|
|
46
|
-
function bboxCentroid(bbox) {
|
|
47
|
-
return {
|
|
48
|
-
x: bbox.x + bbox.w / 2,
|
|
49
|
-
y: bbox.y + bbox.h / 2
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
function normalizeToPixel(point, imageWidth, imageHeight) {
|
|
53
|
-
return {
|
|
54
|
-
x: point.x * imageWidth,
|
|
55
|
-
y: point.y * imageHeight
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// src/tracker/hungarian.ts
|
|
60
|
-
function greedyAssignment(costMatrix, threshold) {
|
|
61
|
-
const numTracks = costMatrix.length;
|
|
62
|
-
const numDetections = numTracks > 0 ? costMatrix[0]?.length ?? 0 : 0;
|
|
63
|
-
const candidates = [];
|
|
64
|
-
for (let t = 0; t < numTracks; t++) {
|
|
65
|
-
for (let d = 0; d < numDetections; d++) {
|
|
66
|
-
const iou2 = costMatrix[t][d] ?? 0;
|
|
67
|
-
if (iou2 >= threshold) {
|
|
68
|
-
candidates.push({ iou: iou2, trackIdx: t, detIdx: d });
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
candidates.sort((a, b) => b.iou - a.iou);
|
|
73
|
-
const assignedTracks = /* @__PURE__ */ new Set();
|
|
74
|
-
const assignedDets = /* @__PURE__ */ new Set();
|
|
75
|
-
const matches = [];
|
|
76
|
-
for (const { trackIdx, detIdx } of candidates) {
|
|
77
|
-
if (assignedTracks.has(trackIdx) || assignedDets.has(detIdx)) continue;
|
|
78
|
-
matches.push([trackIdx, detIdx]);
|
|
79
|
-
assignedTracks.add(trackIdx);
|
|
80
|
-
assignedDets.add(detIdx);
|
|
81
|
-
}
|
|
82
|
-
const unmatchedTracks = [];
|
|
83
|
-
for (let t = 0; t < numTracks; t++) {
|
|
84
|
-
if (!assignedTracks.has(t)) unmatchedTracks.push(t);
|
|
85
|
-
}
|
|
86
|
-
const unmatchedDetections = [];
|
|
87
|
-
for (let d = 0; d < numDetections; d++) {
|
|
88
|
-
if (!assignedDets.has(d)) unmatchedDetections.push(d);
|
|
89
|
-
}
|
|
90
|
-
return { matches, unmatchedTracks, unmatchedDetections };
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// src/tracker/sort-tracker.ts
|
|
94
|
-
var MAX_PATH_LENGTH = 300;
|
|
95
|
-
function iou(a, b) {
|
|
96
|
-
const ax1 = a.x;
|
|
97
|
-
const ay1 = a.y;
|
|
98
|
-
const ax2 = a.x + a.w;
|
|
99
|
-
const ay2 = a.y + a.h;
|
|
100
|
-
const bx1 = b.x;
|
|
101
|
-
const by1 = b.y;
|
|
102
|
-
const bx2 = b.x + b.w;
|
|
103
|
-
const by2 = b.y + b.h;
|
|
104
|
-
const interX1 = Math.max(ax1, bx1);
|
|
105
|
-
const interY1 = Math.max(ay1, by1);
|
|
106
|
-
const interX2 = Math.min(ax2, bx2);
|
|
107
|
-
const interY2 = Math.min(ay2, by2);
|
|
108
|
-
const interW = Math.max(0, interX2 - interX1);
|
|
109
|
-
const interH = Math.max(0, interY2 - interY1);
|
|
110
|
-
const interArea = interW * interH;
|
|
111
|
-
if (interArea === 0) return 0;
|
|
112
|
-
const aArea = a.w * a.h;
|
|
113
|
-
const bArea = b.w * b.h;
|
|
114
|
-
const unionArea = aArea + bArea - interArea;
|
|
115
|
-
return unionArea <= 0 ? 0 : interArea / unionArea;
|
|
116
|
-
}
|
|
117
|
-
function trackToTrackedDetection(track) {
|
|
118
|
-
return {
|
|
119
|
-
class: track.class,
|
|
120
|
-
originalClass: track.originalClass,
|
|
121
|
-
score: track.score,
|
|
122
|
-
bbox: track.bbox,
|
|
123
|
-
landmarks: track.landmarks,
|
|
124
|
-
trackId: track.id,
|
|
125
|
-
trackAge: track.hits,
|
|
126
|
-
velocity: track.velocity,
|
|
127
|
-
path: track.path.slice()
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
function pushToRingBuffer(buffer, item, maxLength) {
|
|
131
|
-
if (buffer.length >= maxLength) {
|
|
132
|
-
buffer.shift();
|
|
133
|
-
}
|
|
134
|
-
buffer.push(item);
|
|
135
|
-
}
|
|
136
|
-
var DEFAULT_TRACKER_CONFIG = {
|
|
137
|
-
maxAge: 30,
|
|
138
|
-
minHits: 3,
|
|
139
|
-
iouThreshold: 0.3
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
8
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
9
|
+
}) : x)(function(x) {
|
|
10
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
11
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
12
|
+
});
|
|
13
|
+
var __commonJS = (cb, mod) => function __require2() {
|
|
14
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
15
|
+
};
|
|
16
|
+
var __export = (target, all) => {
|
|
17
|
+
for (var name in all)
|
|
18
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
140
19
|
};
|
|
141
|
-
var
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
constructor(config = {}) {
|
|
147
|
-
this.config = { ...DEFAULT_TRACKER_CONFIG, ...config };
|
|
20
|
+
var __copyProps = (to, from, except, desc) => {
|
|
21
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
22
|
+
for (let key of __getOwnPropNames(from))
|
|
23
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
24
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
148
25
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
26
|
+
return to;
|
|
27
|
+
};
|
|
28
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
29
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
30
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
31
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
32
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
33
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
34
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
35
|
+
mod
|
|
36
|
+
));
|
|
37
|
+
|
|
38
|
+
// src/zones/geometry.js
|
|
39
|
+
var require_geometry = __commonJS({
|
|
40
|
+
"src/zones/geometry.js"(exports) {
|
|
41
|
+
"use strict";
|
|
42
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
43
|
+
exports.pointInPolygon = pointInPolygon;
|
|
44
|
+
exports.lineIntersection = lineIntersection;
|
|
45
|
+
exports.tripwireCrossing = tripwireCrossing;
|
|
46
|
+
exports.bboxCentroid = bboxCentroid;
|
|
47
|
+
exports.normalizeToPixel = normalizeToPixel;
|
|
48
|
+
function pointInPolygon(point, polygon) {
|
|
49
|
+
if (polygon.length < 3)
|
|
50
|
+
return false;
|
|
51
|
+
let inside = false;
|
|
52
|
+
const { x, y } = point;
|
|
53
|
+
const n = polygon.length;
|
|
54
|
+
for (let i = 0, j = n - 1; i < n; j = i++) {
|
|
55
|
+
const xi = polygon[i].x;
|
|
56
|
+
const yi = polygon[i].y;
|
|
57
|
+
const xj = polygon[j].x;
|
|
58
|
+
const yj = polygon[j].y;
|
|
59
|
+
const intersect = yi > y !== yj > y && x < (xj - xi) * (y - yi) / (yj - yi) + xi;
|
|
60
|
+
if (intersect)
|
|
61
|
+
inside = !inside;
|
|
154
62
|
}
|
|
155
|
-
return
|
|
156
|
-
}
|
|
157
|
-
if (detections.length === 0) {
|
|
158
|
-
this.ageTracksAndPruneLost(frameTimestamp);
|
|
159
|
-
return this.getConfirmedTracks();
|
|
160
|
-
}
|
|
161
|
-
const costMatrix = this.tracks.map(
|
|
162
|
-
(track) => detections.map((det) => iou(track.bbox, det.bbox))
|
|
163
|
-
);
|
|
164
|
-
const { matches, unmatchedTracks, unmatchedDetections } = greedyAssignment(
|
|
165
|
-
costMatrix,
|
|
166
|
-
this.config.iouThreshold
|
|
167
|
-
);
|
|
168
|
-
for (const [trackIdx, detIdx] of matches) {
|
|
169
|
-
const track = this.tracks[trackIdx];
|
|
170
|
-
const det = detections[detIdx];
|
|
171
|
-
const prevCx = track.bbox.x + track.bbox.w / 2;
|
|
172
|
-
const prevCy = track.bbox.y + track.bbox.h / 2;
|
|
173
|
-
const newCx = det.bbox.x + det.bbox.w / 2;
|
|
174
|
-
const newCy = det.bbox.y + det.bbox.h / 2;
|
|
175
|
-
pushToRingBuffer(track.path, track.bbox, MAX_PATH_LENGTH);
|
|
176
|
-
track.bbox = det.bbox;
|
|
177
|
-
track.class = det.class;
|
|
178
|
-
track.originalClass = det.originalClass;
|
|
179
|
-
track.score = det.score;
|
|
180
|
-
track.landmarks = det.landmarks;
|
|
181
|
-
track.age = 0;
|
|
182
|
-
track.hits++;
|
|
183
|
-
track.lastSeen = frameTimestamp;
|
|
184
|
-
track.velocity = { dx: newCx - prevCx, dy: newCy - prevCy };
|
|
63
|
+
return inside;
|
|
185
64
|
}
|
|
186
|
-
|
|
187
|
-
const
|
|
188
|
-
|
|
65
|
+
function lineIntersection(a, b) {
|
|
66
|
+
const dx1 = a.p2.x - a.p1.x;
|
|
67
|
+
const dy1 = a.p2.y - a.p1.y;
|
|
68
|
+
const dx2 = b.p2.x - b.p1.x;
|
|
69
|
+
const dy2 = b.p2.y - b.p1.y;
|
|
70
|
+
const denom = dx1 * dy2 - dy1 * dx2;
|
|
71
|
+
if (Math.abs(denom) < 1e-10)
|
|
72
|
+
return null;
|
|
73
|
+
const dx3 = b.p1.x - a.p1.x;
|
|
74
|
+
const dy3 = b.p1.y - a.p1.y;
|
|
75
|
+
const t = (dx3 * dy2 - dy3 * dx2) / denom;
|
|
76
|
+
const u = (dx3 * dy1 - dy3 * dx1) / denom;
|
|
77
|
+
if (t < 0 || t > 1 || u < 0 || u > 1)
|
|
78
|
+
return null;
|
|
79
|
+
return {
|
|
80
|
+
x: a.p1.x + t * dx1,
|
|
81
|
+
y: a.p1.y + t * dy1
|
|
82
|
+
};
|
|
189
83
|
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
84
|
+
function tripwireCrossing(prev, curr, tripwire) {
|
|
85
|
+
const movement = { p1: prev, p2: curr };
|
|
86
|
+
const intersection = lineIntersection(movement, tripwire);
|
|
87
|
+
if (intersection === null)
|
|
88
|
+
return null;
|
|
89
|
+
const twDx = tripwire.p2.x - tripwire.p1.x;
|
|
90
|
+
const twDy = tripwire.p2.y - tripwire.p1.y;
|
|
91
|
+
const movDx = curr.x - prev.x;
|
|
92
|
+
const movDy = curr.y - prev.y;
|
|
93
|
+
const cross = twDx * movDy - twDy * movDx;
|
|
94
|
+
const direction = cross > 0 ? "left" : "right";
|
|
95
|
+
return { crossed: true, direction };
|
|
198
96
|
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
97
|
+
function bboxCentroid(bbox) {
|
|
98
|
+
return {
|
|
99
|
+
x: bbox.x + bbox.w / 2,
|
|
100
|
+
y: bbox.y + bbox.h / 2
|
|
101
|
+
};
|
|
203
102
|
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
getLostTracks() {
|
|
210
|
-
return this.lostTracks.map(trackToTrackedDetection);
|
|
211
|
-
}
|
|
212
|
-
reset() {
|
|
213
|
-
this.tracks = [];
|
|
214
|
-
this.lostTracks = [];
|
|
215
|
-
this.nextTrackId = 1;
|
|
216
|
-
}
|
|
217
|
-
createTrack(det, timestamp) {
|
|
218
|
-
const track = {
|
|
219
|
-
id: String(this.nextTrackId++),
|
|
220
|
-
bbox: det.bbox,
|
|
221
|
-
class: det.class,
|
|
222
|
-
originalClass: det.originalClass,
|
|
223
|
-
score: det.score,
|
|
224
|
-
landmarks: det.landmarks,
|
|
225
|
-
age: 0,
|
|
226
|
-
hits: 1,
|
|
227
|
-
path: [],
|
|
228
|
-
firstSeen: timestamp,
|
|
229
|
-
lastSeen: timestamp,
|
|
230
|
-
velocity: { dx: 0, dy: 0 },
|
|
231
|
-
lost: false
|
|
232
|
-
};
|
|
233
|
-
this.tracks.push(track);
|
|
234
|
-
return track;
|
|
235
|
-
}
|
|
236
|
-
getConfirmedTracks() {
|
|
237
|
-
return this.tracks.filter((t) => t.hits >= this.config.minHits).map(trackToTrackedDetection);
|
|
238
|
-
}
|
|
239
|
-
ageTracksAndPruneLost(frameTimestamp) {
|
|
240
|
-
const survived = [];
|
|
241
|
-
for (const track of this.tracks) {
|
|
242
|
-
track.age++;
|
|
243
|
-
if (track.age > this.config.maxAge) {
|
|
244
|
-
track.lost = true;
|
|
245
|
-
this.lostTracks.push(track);
|
|
246
|
-
} else {
|
|
247
|
-
survived.push(track);
|
|
248
|
-
}
|
|
103
|
+
function normalizeToPixel(point, imageWidth, imageHeight) {
|
|
104
|
+
return {
|
|
105
|
+
x: point.x * imageWidth,
|
|
106
|
+
y: point.y * imageHeight
|
|
107
|
+
};
|
|
249
108
|
}
|
|
250
|
-
this.tracks = survived;
|
|
251
|
-
void frameTimestamp;
|
|
252
109
|
}
|
|
253
|
-
};
|
|
110
|
+
});
|
|
254
111
|
|
|
255
|
-
// src/
|
|
256
|
-
var
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
analyze(tracks, timestamp) {
|
|
272
|
-
const results = [];
|
|
273
|
-
const activeIds = /* @__PURE__ */ new Set();
|
|
274
|
-
for (const track of tracks) {
|
|
275
|
-
activeIds.add(track.trackId);
|
|
276
|
-
const prev = this.states.get(track.trackId);
|
|
277
|
-
const centroid = {
|
|
278
|
-
x: track.bbox.x + track.bbox.w / 2,
|
|
279
|
-
y: track.bbox.y + track.bbox.h / 2
|
|
280
|
-
};
|
|
281
|
-
const speed = track.velocity ? magnitude(track.velocity) : 0;
|
|
282
|
-
let enteredAt;
|
|
283
|
-
let stationarySince;
|
|
284
|
-
let totalDistancePx;
|
|
285
|
-
if (!prev) {
|
|
286
|
-
enteredAt = timestamp;
|
|
287
|
-
stationarySince = speed <= this.config.velocityThreshold ? timestamp : void 0;
|
|
288
|
-
totalDistancePx = 0;
|
|
289
|
-
} else {
|
|
290
|
-
enteredAt = prev.enteredAt;
|
|
291
|
-
const dx = centroid.x - prev.lastPosition.x;
|
|
292
|
-
const dy = centroid.y - prev.lastPosition.y;
|
|
293
|
-
const dist = Math.sqrt(dx * dx + dy * dy);
|
|
294
|
-
totalDistancePx = prev.totalDistancePx + dist;
|
|
295
|
-
if (speed > this.config.velocityThreshold) {
|
|
296
|
-
stationarySince = void 0;
|
|
297
|
-
} else {
|
|
298
|
-
stationarySince = prev.stationarySince ?? timestamp;
|
|
112
|
+
// src/tracker/hungarian.js
|
|
113
|
+
var require_hungarian = __commonJS({
|
|
114
|
+
"src/tracker/hungarian.js"(exports) {
|
|
115
|
+
"use strict";
|
|
116
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
117
|
+
exports.greedyAssignment = greedyAssignment2;
|
|
118
|
+
function greedyAssignment2(costMatrix, threshold) {
|
|
119
|
+
const numTracks = costMatrix.length;
|
|
120
|
+
const numDetections = numTracks > 0 ? costMatrix[0]?.length ?? 0 : 0;
|
|
121
|
+
const candidates = [];
|
|
122
|
+
for (let t = 0; t < numTracks; t++) {
|
|
123
|
+
for (let d = 0; d < numDetections; d++) {
|
|
124
|
+
const iou = costMatrix[t][d] ?? 0;
|
|
125
|
+
if (iou >= threshold) {
|
|
126
|
+
candidates.push({ iou, trackIdx: t, detIdx: d });
|
|
127
|
+
}
|
|
299
128
|
}
|
|
300
129
|
}
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
trackId: track.trackId,
|
|
312
|
-
state,
|
|
313
|
-
stationarySince,
|
|
314
|
-
enteredAt,
|
|
315
|
-
totalDistancePx,
|
|
316
|
-
dwellTimeMs
|
|
317
|
-
});
|
|
318
|
-
}
|
|
319
|
-
for (const [id, internalState] of this.states) {
|
|
320
|
-
if (!activeIds.has(id)) {
|
|
321
|
-
results.push({
|
|
322
|
-
trackId: id,
|
|
323
|
-
state: "leaving",
|
|
324
|
-
stationarySince: internalState.stationarySince,
|
|
325
|
-
enteredAt: internalState.enteredAt,
|
|
326
|
-
totalDistancePx: internalState.totalDistancePx,
|
|
327
|
-
dwellTimeMs: timestamp - internalState.enteredAt
|
|
328
|
-
});
|
|
329
|
-
this.states.delete(id);
|
|
130
|
+
candidates.sort((a, b) => b.iou - a.iou);
|
|
131
|
+
const assignedTracks = /* @__PURE__ */ new Set();
|
|
132
|
+
const assignedDets = /* @__PURE__ */ new Set();
|
|
133
|
+
const matches = [];
|
|
134
|
+
for (const { trackIdx, detIdx } of candidates) {
|
|
135
|
+
if (assignedTracks.has(trackIdx) || assignedDets.has(detIdx))
|
|
136
|
+
continue;
|
|
137
|
+
matches.push([trackIdx, detIdx]);
|
|
138
|
+
assignedTracks.add(trackIdx);
|
|
139
|
+
assignedDets.add(detIdx);
|
|
330
140
|
}
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
if (trackAge <= this.config.enteringFrames) {
|
|
336
|
-
return "entering";
|
|
337
|
-
}
|
|
338
|
-
if (stationarySince !== void 0) {
|
|
339
|
-
const stationaryDurationSec = (timestamp - stationarySince) / 1e3;
|
|
340
|
-
if (stationaryDurationSec >= this.config.loiteringThresholdSec) {
|
|
341
|
-
return "loitering";
|
|
141
|
+
const unmatchedTracks = [];
|
|
142
|
+
for (let t = 0; t < numTracks; t++) {
|
|
143
|
+
if (!assignedTracks.has(t))
|
|
144
|
+
unmatchedTracks.push(t);
|
|
342
145
|
}
|
|
343
|
-
|
|
344
|
-
|
|
146
|
+
const unmatchedDetections = [];
|
|
147
|
+
for (let d = 0; d < numDetections; d++) {
|
|
148
|
+
if (!assignedDets.has(d))
|
|
149
|
+
unmatchedDetections.push(d);
|
|
345
150
|
}
|
|
151
|
+
return { matches, unmatchedTracks, unmatchedDetections };
|
|
346
152
|
}
|
|
347
|
-
if (speed > this.config.velocityThreshold) {
|
|
348
|
-
return "moving";
|
|
349
|
-
}
|
|
350
|
-
return "moving";
|
|
351
153
|
}
|
|
352
|
-
};
|
|
154
|
+
});
|
|
353
155
|
|
|
354
|
-
// src/
|
|
355
|
-
var
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
const
|
|
364
|
-
const
|
|
365
|
-
const
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
156
|
+
// src/tracker/sort-tracker.js
|
|
157
|
+
var require_sort_tracker = __commonJS({
|
|
158
|
+
"src/tracker/sort-tracker.js"(exports) {
|
|
159
|
+
"use strict";
|
|
160
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
161
|
+
exports.SortTracker = exports.DEFAULT_TRACKER_CONFIG = void 0;
|
|
162
|
+
var hungarian_js_1 = require_hungarian();
|
|
163
|
+
var MAX_PATH_LENGTH = 300;
|
|
164
|
+
function iou(a, b) {
|
|
165
|
+
const ax1 = a.x;
|
|
166
|
+
const ay1 = a.y;
|
|
167
|
+
const ax2 = a.x + a.w;
|
|
168
|
+
const ay2 = a.y + a.h;
|
|
169
|
+
const bx1 = b.x;
|
|
170
|
+
const by1 = b.y;
|
|
171
|
+
const bx2 = b.x + b.w;
|
|
172
|
+
const by2 = b.y + b.h;
|
|
173
|
+
const interX1 = Math.max(ax1, bx1);
|
|
174
|
+
const interY1 = Math.max(ay1, by1);
|
|
175
|
+
const interX2 = Math.min(ax2, bx2);
|
|
176
|
+
const interY2 = Math.min(ay2, by2);
|
|
177
|
+
const interW = Math.max(0, interX2 - interX1);
|
|
178
|
+
const interH = Math.max(0, interY2 - interY1);
|
|
179
|
+
const interArea = interW * interH;
|
|
180
|
+
if (interArea === 0)
|
|
181
|
+
return 0;
|
|
182
|
+
const aArea = a.w * a.h;
|
|
183
|
+
const bArea = b.w * b.h;
|
|
184
|
+
const unionArea = aArea + bArea - interArea;
|
|
185
|
+
return unionArea <= 0 ? 0 : interArea / unionArea;
|
|
186
|
+
}
|
|
187
|
+
function trackToTrackedDetection(track) {
|
|
188
|
+
return {
|
|
189
|
+
class: track.class,
|
|
190
|
+
originalClass: track.originalClass,
|
|
191
|
+
score: track.score,
|
|
192
|
+
bbox: track.bbox,
|
|
193
|
+
landmarks: track.landmarks,
|
|
194
|
+
trackId: track.id,
|
|
195
|
+
trackAge: track.hits,
|
|
196
|
+
velocity: track.velocity,
|
|
197
|
+
path: track.path.slice()
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
function pushToRingBuffer(buffer, item, maxLength) {
|
|
201
|
+
if (buffer.length >= maxLength) {
|
|
202
|
+
buffer.shift();
|
|
203
|
+
}
|
|
204
|
+
buffer.push(item);
|
|
205
|
+
}
|
|
206
|
+
exports.DEFAULT_TRACKER_CONFIG = {
|
|
207
|
+
maxAge: 30,
|
|
208
|
+
minHits: 3,
|
|
209
|
+
iouThreshold: 0.3
|
|
210
|
+
};
|
|
211
|
+
var SortTracker2 = class {
|
|
212
|
+
tracks = [];
|
|
213
|
+
lostTracks = [];
|
|
214
|
+
nextTrackId = 1;
|
|
215
|
+
config;
|
|
216
|
+
constructor(config = {}) {
|
|
217
|
+
this.config = { ...exports.DEFAULT_TRACKER_CONFIG, ...config };
|
|
218
|
+
}
|
|
219
|
+
update(detections, frameTimestamp) {
|
|
220
|
+
this.lostTracks = [];
|
|
221
|
+
if (this.tracks.length === 0) {
|
|
222
|
+
for (const det of detections) {
|
|
223
|
+
this.createTrack(det, frameTimestamp);
|
|
224
|
+
}
|
|
225
|
+
return this.getConfirmedTracks();
|
|
369
226
|
}
|
|
370
|
-
if (
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
227
|
+
if (detections.length === 0) {
|
|
228
|
+
this.ageTracksAndPruneLost(frameTimestamp);
|
|
229
|
+
return this.getConfirmedTracks();
|
|
230
|
+
}
|
|
231
|
+
const costMatrix = this.tracks.map((track) => detections.map((det) => iou(track.bbox, det.bbox)));
|
|
232
|
+
const { matches, unmatchedTracks, unmatchedDetections } = (0, hungarian_js_1.greedyAssignment)(costMatrix, this.config.iouThreshold);
|
|
233
|
+
for (const [trackIdx, detIdx] of matches) {
|
|
234
|
+
const track = this.tracks[trackIdx];
|
|
235
|
+
const det = detections[detIdx];
|
|
236
|
+
const prevCx = track.bbox.x + track.bbox.w / 2;
|
|
237
|
+
const prevCy = track.bbox.y + track.bbox.h / 2;
|
|
238
|
+
const newCx = det.bbox.x + det.bbox.w / 2;
|
|
239
|
+
const newCy = det.bbox.y + det.bbox.h / 2;
|
|
240
|
+
pushToRingBuffer(track.path, track.bbox, MAX_PATH_LENGTH);
|
|
241
|
+
track.bbox = det.bbox;
|
|
242
|
+
track.class = det.class;
|
|
243
|
+
track.originalClass = det.originalClass;
|
|
244
|
+
track.score = det.score;
|
|
245
|
+
track.landmarks = det.landmarks;
|
|
246
|
+
track.age = 0;
|
|
247
|
+
track.hits++;
|
|
248
|
+
track.lastSeen = frameTimestamp;
|
|
249
|
+
track.velocity = { dx: newCx - prevCx, dy: newCy - prevCy };
|
|
250
|
+
}
|
|
251
|
+
for (const trackIdx of unmatchedTracks) {
|
|
252
|
+
const track = this.tracks[trackIdx];
|
|
253
|
+
track.age++;
|
|
254
|
+
}
|
|
255
|
+
const survived = [];
|
|
256
|
+
for (const track of this.tracks) {
|
|
257
|
+
if (track.age > this.config.maxAge) {
|
|
258
|
+
track.lost = true;
|
|
259
|
+
this.lostTracks.push(track);
|
|
260
|
+
} else {
|
|
261
|
+
survived.push(track);
|
|
375
262
|
}
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
263
|
+
}
|
|
264
|
+
this.tracks = survived;
|
|
265
|
+
for (const detIdx of unmatchedDetections) {
|
|
266
|
+
const det = detections[detIdx];
|
|
267
|
+
this.createTrack(det, frameTimestamp);
|
|
268
|
+
}
|
|
269
|
+
return this.getConfirmedTracks();
|
|
270
|
+
}
|
|
271
|
+
getActiveTracks() {
|
|
272
|
+
return this.tracks.map(trackToTrackedDetection);
|
|
273
|
+
}
|
|
274
|
+
getLostTracks() {
|
|
275
|
+
return this.lostTracks.map(trackToTrackedDetection);
|
|
276
|
+
}
|
|
277
|
+
reset() {
|
|
278
|
+
this.tracks = [];
|
|
279
|
+
this.lostTracks = [];
|
|
280
|
+
this.nextTrackId = 1;
|
|
281
|
+
}
|
|
282
|
+
createTrack(det, timestamp) {
|
|
283
|
+
const track = {
|
|
284
|
+
id: String(this.nextTrackId++),
|
|
285
|
+
bbox: det.bbox,
|
|
286
|
+
class: det.class,
|
|
287
|
+
originalClass: det.originalClass,
|
|
288
|
+
score: det.score,
|
|
289
|
+
landmarks: det.landmarks,
|
|
290
|
+
age: 0,
|
|
291
|
+
hits: 1,
|
|
292
|
+
path: [],
|
|
293
|
+
firstSeen: timestamp,
|
|
294
|
+
lastSeen: timestamp,
|
|
295
|
+
velocity: { dx: 0, dy: 0 },
|
|
296
|
+
lost: false
|
|
297
|
+
};
|
|
298
|
+
this.tracks.push(track);
|
|
299
|
+
return track;
|
|
300
|
+
}
|
|
301
|
+
getConfirmedTracks() {
|
|
302
|
+
return this.tracks.filter((t) => t.hits >= this.config.minHits).map(trackToTrackedDetection);
|
|
303
|
+
}
|
|
304
|
+
ageTracksAndPruneLost(frameTimestamp) {
|
|
305
|
+
const survived = [];
|
|
306
|
+
for (const track of this.tracks) {
|
|
307
|
+
track.age++;
|
|
308
|
+
if (track.age > this.config.maxAge) {
|
|
309
|
+
track.lost = true;
|
|
310
|
+
this.lostTracks.push(track);
|
|
311
|
+
} else {
|
|
312
|
+
survived.push(track);
|
|
385
313
|
}
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
314
|
+
}
|
|
315
|
+
this.tracks = survived;
|
|
316
|
+
void frameTimestamp;
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
exports.SortTracker = SortTracker2;
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
// src/state/state-analyzer.js
|
|
324
|
+
var require_state_analyzer = __commonJS({
|
|
325
|
+
"src/state/state-analyzer.js"(exports) {
|
|
326
|
+
"use strict";
|
|
327
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
328
|
+
exports.StateAnalyzer = exports.DEFAULT_STATE_ANALYZER_CONFIG = void 0;
|
|
329
|
+
exports.DEFAULT_STATE_ANALYZER_CONFIG = {
|
|
330
|
+
stationaryThresholdSec: 10,
|
|
331
|
+
loiteringThresholdSec: 60,
|
|
332
|
+
velocityThreshold: 2,
|
|
333
|
+
enteringFrames: 5
|
|
334
|
+
};
|
|
335
|
+
function magnitude(v) {
|
|
336
|
+
return Math.sqrt(v.dx * v.dx + v.dy * v.dy);
|
|
337
|
+
}
|
|
338
|
+
var StateAnalyzer2 = class {
|
|
339
|
+
states = /* @__PURE__ */ new Map();
|
|
340
|
+
config;
|
|
341
|
+
constructor(config = {}) {
|
|
342
|
+
this.config = { ...exports.DEFAULT_STATE_ANALYZER_CONFIG, ...config };
|
|
343
|
+
}
|
|
344
|
+
analyze(tracks, timestamp) {
|
|
345
|
+
const results = [];
|
|
346
|
+
const activeIds = /* @__PURE__ */ new Set();
|
|
347
|
+
for (const track of tracks) {
|
|
348
|
+
activeIds.add(track.trackId);
|
|
349
|
+
const prev = this.states.get(track.trackId);
|
|
350
|
+
const centroid = {
|
|
351
|
+
x: track.bbox.x + track.bbox.w / 2,
|
|
352
|
+
y: track.bbox.y + track.bbox.h / 2
|
|
353
|
+
};
|
|
354
|
+
const speed = track.velocity ? magnitude(track.velocity) : 0;
|
|
355
|
+
let enteredAt;
|
|
356
|
+
let stationarySince;
|
|
357
|
+
let totalDistancePx;
|
|
358
|
+
if (!prev) {
|
|
359
|
+
enteredAt = timestamp;
|
|
360
|
+
stationarySince = speed <= this.config.velocityThreshold ? timestamp : void 0;
|
|
361
|
+
totalDistancePx = 0;
|
|
362
|
+
} else {
|
|
363
|
+
enteredAt = prev.enteredAt;
|
|
364
|
+
const dx = centroid.x - prev.lastPosition.x;
|
|
365
|
+
const dy = centroid.y - prev.lastPosition.y;
|
|
366
|
+
const dist = Math.sqrt(dx * dx + dy * dy);
|
|
367
|
+
totalDistancePx = prev.totalDistancePx + dist;
|
|
368
|
+
if (speed > this.config.velocityThreshold) {
|
|
369
|
+
stationarySince = void 0;
|
|
370
|
+
} else {
|
|
371
|
+
stationarySince = prev.stationarySince ?? timestamp;
|
|
372
|
+
}
|
|
395
373
|
}
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
const p1 = zone.points[1];
|
|
402
|
-
if (!p0 || !p1) continue;
|
|
403
|
-
const tripwireLine = {
|
|
404
|
-
p1: normalizeToPixel(p0, imageWidth, imageHeight),
|
|
405
|
-
p2: normalizeToPixel(p1, imageWidth, imageHeight)
|
|
374
|
+
const newInternal = {
|
|
375
|
+
enteredAt,
|
|
376
|
+
stationarySince,
|
|
377
|
+
totalDistancePx,
|
|
378
|
+
lastPosition: centroid
|
|
406
379
|
};
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
380
|
+
this.states.set(track.trackId, newInternal);
|
|
381
|
+
const dwellTimeMs = timestamp - enteredAt;
|
|
382
|
+
const state = this.computeObjectState(track.trackAge, speed, stationarySince, timestamp);
|
|
383
|
+
results.push({
|
|
384
|
+
trackId: track.trackId,
|
|
385
|
+
state,
|
|
386
|
+
stationarySince,
|
|
387
|
+
enteredAt,
|
|
388
|
+
totalDistancePx,
|
|
389
|
+
dwellTimeMs
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
for (const [id, internalState] of this.states) {
|
|
393
|
+
if (!activeIds.has(id)) {
|
|
394
|
+
results.push({
|
|
395
|
+
trackId: id,
|
|
396
|
+
state: "leaving",
|
|
397
|
+
stationarySince: internalState.stationarySince,
|
|
398
|
+
enteredAt: internalState.enteredAt,
|
|
399
|
+
totalDistancePx: internalState.totalDistancePx,
|
|
400
|
+
dwellTimeMs: timestamp - internalState.enteredAt
|
|
417
401
|
});
|
|
402
|
+
this.states.delete(id);
|
|
418
403
|
}
|
|
419
404
|
}
|
|
405
|
+
return results;
|
|
420
406
|
}
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
407
|
+
computeObjectState(trackAge, speed, stationarySince, timestamp) {
|
|
408
|
+
if (trackAge <= this.config.enteringFrames) {
|
|
409
|
+
return "entering";
|
|
410
|
+
}
|
|
411
|
+
if (stationarySince !== void 0) {
|
|
412
|
+
const stationaryDurationSec = (timestamp - stationarySince) / 1e3;
|
|
413
|
+
if (stationaryDurationSec >= this.config.loiteringThresholdSec) {
|
|
414
|
+
return "loitering";
|
|
415
|
+
}
|
|
416
|
+
if (stationaryDurationSec >= this.config.stationaryThresholdSec) {
|
|
417
|
+
return "stationary";
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
if (speed > this.config.velocityThreshold) {
|
|
421
|
+
return "moving";
|
|
422
|
+
}
|
|
423
|
+
return "moving";
|
|
426
424
|
}
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
}
|
|
430
|
-
/** Returns a snapshot of the current track → zones mapping */
|
|
431
|
-
getTrackZones() {
|
|
432
|
-
return new Map(this.trackZoneState);
|
|
425
|
+
};
|
|
426
|
+
exports.StateAnalyzer = StateAnalyzer2;
|
|
433
427
|
}
|
|
434
|
-
};
|
|
428
|
+
});
|
|
435
429
|
|
|
436
|
-
// src/
|
|
437
|
-
var
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
430
|
+
// src/zones/zone-evaluator.js
|
|
431
|
+
var require_zone_evaluator = __commonJS({
|
|
432
|
+
"src/zones/zone-evaluator.js"(exports) {
|
|
433
|
+
"use strict";
|
|
434
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
435
|
+
exports.ZoneEvaluator = void 0;
|
|
436
|
+
var geometry_js_1 = require_geometry();
|
|
437
|
+
var ZoneEvaluator2 = class {
|
|
438
|
+
/** Track which zones each track was in last frame */
|
|
439
|
+
trackZoneState = /* @__PURE__ */ new Map();
|
|
440
|
+
evaluate(tracks, zones, imageWidth, imageHeight, timestamp) {
|
|
441
|
+
const events = [];
|
|
442
|
+
const activeTrackIds = /* @__PURE__ */ new Set();
|
|
443
|
+
for (const track of tracks) {
|
|
444
|
+
activeTrackIds.add(track.trackId);
|
|
445
|
+
const centroid = (0, geometry_js_1.bboxCentroid)(track.bbox);
|
|
446
|
+
const prevZones = this.trackZoneState.get(track.trackId) ?? /* @__PURE__ */ new Set();
|
|
447
|
+
const currZones = /* @__PURE__ */ new Set();
|
|
448
|
+
for (const zone of zones) {
|
|
449
|
+
if (zone.alertOnClasses && zone.alertOnClasses.length > 0) {
|
|
450
|
+
if (!zone.alertOnClasses.includes(track.class))
|
|
451
|
+
continue;
|
|
452
|
+
}
|
|
453
|
+
if (zone.type === "polygon") {
|
|
454
|
+
const pixelPoints = zone.points.map((p) => (0, geometry_js_1.normalizeToPixel)(p, imageWidth, imageHeight));
|
|
455
|
+
const inside = (0, geometry_js_1.pointInPolygon)(centroid, pixelPoints);
|
|
456
|
+
if (inside) {
|
|
457
|
+
currZones.add(zone.id);
|
|
458
|
+
}
|
|
459
|
+
if (inside && !prevZones.has(zone.id)) {
|
|
460
|
+
events.push({
|
|
461
|
+
type: "zone-enter",
|
|
462
|
+
zoneId: zone.id,
|
|
463
|
+
zoneName: zone.name,
|
|
464
|
+
trackId: track.trackId,
|
|
465
|
+
detection: track,
|
|
466
|
+
timestamp
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
if (!inside && prevZones.has(zone.id)) {
|
|
470
|
+
events.push({
|
|
471
|
+
type: "zone-exit",
|
|
472
|
+
zoneId: zone.id,
|
|
473
|
+
zoneName: zone.name,
|
|
474
|
+
trackId: track.trackId,
|
|
475
|
+
detection: track,
|
|
476
|
+
timestamp
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
} else if (zone.type === "tripwire" && track.path.length >= 1) {
|
|
480
|
+
const prevBbox = track.path[track.path.length - 1];
|
|
481
|
+
if (!prevBbox)
|
|
482
|
+
continue;
|
|
483
|
+
const prevCentroid = (0, geometry_js_1.bboxCentroid)(prevBbox);
|
|
484
|
+
const p0 = zone.points[0];
|
|
485
|
+
const p1 = zone.points[1];
|
|
486
|
+
if (!p0 || !p1)
|
|
487
|
+
continue;
|
|
488
|
+
const tripwireLine = {
|
|
489
|
+
p1: (0, geometry_js_1.normalizeToPixel)(p0, imageWidth, imageHeight),
|
|
490
|
+
p2: (0, geometry_js_1.normalizeToPixel)(p1, imageWidth, imageHeight)
|
|
491
|
+
};
|
|
492
|
+
const cross = (0, geometry_js_1.tripwireCrossing)(prevCentroid, centroid, tripwireLine);
|
|
493
|
+
if (cross?.crossed) {
|
|
494
|
+
events.push({
|
|
495
|
+
type: "tripwire-cross",
|
|
496
|
+
zoneId: zone.id,
|
|
497
|
+
zoneName: zone.name,
|
|
498
|
+
trackId: track.trackId,
|
|
499
|
+
detection: track,
|
|
500
|
+
direction: cross.direction,
|
|
501
|
+
timestamp
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
this.trackZoneState.set(track.trackId, currZones);
|
|
507
|
+
}
|
|
508
|
+
for (const trackId of this.trackZoneState.keys()) {
|
|
509
|
+
if (!activeTrackIds.has(trackId)) {
|
|
510
|
+
this.trackZoneState.delete(trackId);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
return events;
|
|
478
514
|
}
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
515
|
+
/** Returns a snapshot of the current track → zones mapping */
|
|
516
|
+
getTrackZones() {
|
|
517
|
+
return new Map(this.trackZoneState);
|
|
518
|
+
}
|
|
519
|
+
};
|
|
520
|
+
exports.ZoneEvaluator = ZoneEvaluator2;
|
|
484
521
|
}
|
|
485
|
-
};
|
|
522
|
+
});
|
|
486
523
|
|
|
487
|
-
// src/events/event-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
this.
|
|
512
|
-
}
|
|
513
|
-
if (state.state === "leaving") {
|
|
514
|
-
this.tryEmit(events, "object.leaving", track, state, classifs, [], deviceId, timestamp);
|
|
515
|
-
}
|
|
516
|
-
if (state.state === "stationary" && prevState !== void 0 && prevState !== "stationary" && prevState !== "loitering") {
|
|
517
|
-
this.tryEmit(events, "object.stationary", track, state, classifs, [], deviceId, timestamp);
|
|
518
|
-
}
|
|
519
|
-
if (state.state === "loitering" && prevState === "stationary") {
|
|
520
|
-
this.tryEmit(events, "object.loitering", track, state, classifs, [], deviceId, timestamp);
|
|
521
|
-
}
|
|
522
|
-
if (state.state !== "leaving") {
|
|
523
|
-
this.previousStates.set(state.trackId, state.state);
|
|
524
|
-
} else {
|
|
525
|
-
this.previousStates.delete(state.trackId);
|
|
524
|
+
// src/events/event-filters.js
|
|
525
|
+
var require_event_filters = __commonJS({
|
|
526
|
+
"src/events/event-filters.js"(exports) {
|
|
527
|
+
"use strict";
|
|
528
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
529
|
+
exports.EventFilter = exports.DEFAULT_EVENT_FILTER_CONFIG = void 0;
|
|
530
|
+
exports.DEFAULT_EVENT_FILTER_CONFIG = {
|
|
531
|
+
minTrackAge: 3,
|
|
532
|
+
cooldownSec: 5,
|
|
533
|
+
enabledTypes: [
|
|
534
|
+
"object.entering",
|
|
535
|
+
"object.leaving",
|
|
536
|
+
"object.stationary",
|
|
537
|
+
"object.loitering",
|
|
538
|
+
"zone.enter",
|
|
539
|
+
"zone.exit",
|
|
540
|
+
"tripwire.cross"
|
|
541
|
+
]
|
|
542
|
+
};
|
|
543
|
+
var EventFilter2 = class {
|
|
544
|
+
config;
|
|
545
|
+
/** key: `${trackId}:${eventType}` → last emitted timestamp */
|
|
546
|
+
lastEmitted = /* @__PURE__ */ new Map();
|
|
547
|
+
constructor(config) {
|
|
548
|
+
this.config = config;
|
|
526
549
|
}
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
reset() {
|
|
563
|
-
this.previousStates.clear();
|
|
564
|
-
this.filter.reset();
|
|
550
|
+
shouldEmit(trackId, eventType, trackAge, timestamp) {
|
|
551
|
+
if (!this.config.enabledTypes.includes(eventType))
|
|
552
|
+
return false;
|
|
553
|
+
if (trackAge < this.config.minTrackAge)
|
|
554
|
+
return false;
|
|
555
|
+
const key = `${trackId}:${eventType}`;
|
|
556
|
+
const last = this.lastEmitted.get(key);
|
|
557
|
+
if (last !== void 0 && timestamp - last < this.config.cooldownSec * 1e3)
|
|
558
|
+
return false;
|
|
559
|
+
this.lastEmitted.set(key, timestamp);
|
|
560
|
+
return true;
|
|
561
|
+
}
|
|
562
|
+
/** Record an emission without a gate check — for events that bypass normal cooldown logic */
|
|
563
|
+
recordEmission(trackId, eventType, timestamp) {
|
|
564
|
+
const key = `${trackId}:${eventType}`;
|
|
565
|
+
this.lastEmitted.set(key, timestamp);
|
|
566
|
+
}
|
|
567
|
+
/** Remove cooldown entries for tracks that are no longer active */
|
|
568
|
+
cleanup(activeTrackIds) {
|
|
569
|
+
for (const key of this.lastEmitted.keys()) {
|
|
570
|
+
const colonIdx = key.indexOf(":");
|
|
571
|
+
if (colonIdx === -1)
|
|
572
|
+
continue;
|
|
573
|
+
const trackId = key.slice(0, colonIdx);
|
|
574
|
+
if (!activeTrackIds.has(trackId)) {
|
|
575
|
+
this.lastEmitted.delete(key);
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
/** Reset all cooldown state */
|
|
580
|
+
reset() {
|
|
581
|
+
this.lastEmitted.clear();
|
|
582
|
+
}
|
|
583
|
+
};
|
|
584
|
+
exports.EventFilter = EventFilter2;
|
|
565
585
|
}
|
|
566
|
-
};
|
|
586
|
+
});
|
|
567
587
|
|
|
568
|
-
// src/
|
|
569
|
-
var
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
trackId: track.trackId,
|
|
583
|
-
class: track.class,
|
|
584
|
-
originalClass: track.originalClass,
|
|
585
|
-
state: "entering",
|
|
586
|
-
firstSeen: track.path.length === 0 ? now : now,
|
|
587
|
-
lastSeen: now,
|
|
588
|
-
path: [],
|
|
589
|
-
zoneTransitions: [],
|
|
590
|
-
events: []
|
|
591
|
-
};
|
|
592
|
-
this.activeTracks.set(track.trackId, acc);
|
|
593
|
-
}
|
|
594
|
-
acc.class = track.class;
|
|
595
|
-
acc.originalClass = track.originalClass;
|
|
596
|
-
acc.lastSeen = now;
|
|
597
|
-
const state = stateMap.get(track.trackId);
|
|
598
|
-
if (state) acc.state = state.state;
|
|
599
|
-
const pathPoint = {
|
|
600
|
-
timestamp: now,
|
|
601
|
-
bbox: track.bbox,
|
|
602
|
-
velocity: track.velocity ?? { dx: 0, dy: 0 }
|
|
603
|
-
};
|
|
604
|
-
if (acc.path.length >= MAX_PATH_LENGTH2) {
|
|
605
|
-
acc.path.shift();
|
|
606
|
-
}
|
|
607
|
-
acc.path.push(pathPoint);
|
|
608
|
-
const classifs = classifMap.get(track.trackId) ?? [];
|
|
609
|
-
for (const c of classifs) {
|
|
610
|
-
if (c.metadata?.type === "face-recognition" && typeof c.text === "string") {
|
|
611
|
-
acc.identity = { name: c.text, confidence: c.score, matchedAt: now };
|
|
612
|
-
} else if (c.metadata?.type === "plate-recognition" && typeof c.text === "string") {
|
|
613
|
-
acc.plateText = { text: c.text, confidence: c.score, readAt: now };
|
|
614
|
-
} else if (c.metadata?.type === "sub-class") {
|
|
615
|
-
acc.subClass = { class: c.class, confidence: c.score };
|
|
616
|
-
}
|
|
588
|
+
// src/events/event-emitter.js
|
|
589
|
+
var require_event_emitter = __commonJS({
|
|
590
|
+
"src/events/event-emitter.js"(exports) {
|
|
591
|
+
"use strict";
|
|
592
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
593
|
+
exports.DetectionEventEmitter = void 0;
|
|
594
|
+
var node_crypto_1 = __require("crypto");
|
|
595
|
+
var event_filters_js_1 = require_event_filters();
|
|
596
|
+
var DetectionEventEmitter2 = class {
|
|
597
|
+
filter;
|
|
598
|
+
/** trackId → last known ObjectState */
|
|
599
|
+
previousStates = /* @__PURE__ */ new Map();
|
|
600
|
+
constructor(filterConfig = {}) {
|
|
601
|
+
this.filter = new event_filters_js_1.EventFilter({ ...event_filters_js_1.DEFAULT_EVENT_FILTER_CONFIG, ...filterConfig });
|
|
617
602
|
}
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
603
|
+
emit(tracks, states, zoneEvents, classifications, deviceId) {
|
|
604
|
+
const events = [];
|
|
605
|
+
const timestamp = Date.now();
|
|
606
|
+
const trackMap = /* @__PURE__ */ new Map();
|
|
607
|
+
for (const t of tracks)
|
|
608
|
+
trackMap.set(t.trackId, t);
|
|
609
|
+
const stateMap = /* @__PURE__ */ new Map();
|
|
610
|
+
for (const s of states)
|
|
611
|
+
stateMap.set(s.trackId, s);
|
|
612
|
+
const classifMap = /* @__PURE__ */ new Map();
|
|
613
|
+
for (const c of classifications)
|
|
614
|
+
classifMap.set(c.trackId, c.classifications);
|
|
615
|
+
for (const state of states) {
|
|
616
|
+
const track = trackMap.get(state.trackId);
|
|
617
|
+
if (!track)
|
|
618
|
+
continue;
|
|
619
|
+
const prevState = this.previousStates.get(state.trackId);
|
|
620
|
+
const classifs = classifMap.get(state.trackId) ?? [];
|
|
621
|
+
if (state.state === "entering" && prevState === void 0) {
|
|
622
|
+
this.tryEmit(events, "object.entering", track, state, classifs, [], deviceId, timestamp);
|
|
623
|
+
}
|
|
624
|
+
if (state.state === "leaving") {
|
|
625
|
+
this.tryEmit(events, "object.leaving", track, state, classifs, [], deviceId, timestamp);
|
|
626
|
+
}
|
|
627
|
+
if (state.state === "stationary" && prevState !== void 0 && prevState !== "stationary" && prevState !== "loitering") {
|
|
628
|
+
this.tryEmit(events, "object.stationary", track, state, classifs, [], deviceId, timestamp);
|
|
629
|
+
}
|
|
630
|
+
if (state.state === "loitering" && prevState === "stationary") {
|
|
631
|
+
this.tryEmit(events, "object.loitering", track, state, classifs, [], deviceId, timestamp);
|
|
632
|
+
}
|
|
633
|
+
if (state.state !== "leaving") {
|
|
634
|
+
this.previousStates.set(state.trackId, state.state);
|
|
635
|
+
} else {
|
|
636
|
+
this.previousStates.delete(state.trackId);
|
|
636
637
|
}
|
|
637
638
|
}
|
|
638
|
-
|
|
639
|
-
const
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
639
|
+
for (const ze of zoneEvents) {
|
|
640
|
+
const track = trackMap.get(ze.trackId);
|
|
641
|
+
if (!track)
|
|
642
|
+
continue;
|
|
643
|
+
const state = stateMap.get(ze.trackId);
|
|
644
|
+
if (!state)
|
|
645
|
+
continue;
|
|
646
|
+
let eventType;
|
|
647
|
+
if (ze.type === "tripwire-cross") {
|
|
648
|
+
eventType = "tripwire.cross";
|
|
649
|
+
} else if (ze.type === "zone-enter") {
|
|
650
|
+
eventType = "zone.enter";
|
|
651
|
+
} else {
|
|
652
|
+
eventType = "zone.exit";
|
|
653
|
+
}
|
|
654
|
+
const classifs = classifMap.get(ze.trackId) ?? [];
|
|
655
|
+
this.tryEmit(events, eventType, track, state, classifs, [ze], deviceId, timestamp);
|
|
645
656
|
}
|
|
657
|
+
const activeIds = new Set(tracks.map((t) => t.trackId));
|
|
658
|
+
this.filter.cleanup(activeIds);
|
|
659
|
+
return events;
|
|
660
|
+
}
|
|
661
|
+
tryEmit(events, eventType, track, state, classifications, zoneEvents, deviceId, timestamp) {
|
|
662
|
+
if (!this.filter.shouldEmit(track.trackId, eventType, track.trackAge, timestamp))
|
|
663
|
+
return;
|
|
664
|
+
events.push({
|
|
665
|
+
id: (0, node_crypto_1.randomUUID)(),
|
|
666
|
+
type: eventType,
|
|
667
|
+
timestamp,
|
|
668
|
+
deviceId,
|
|
669
|
+
detection: track,
|
|
670
|
+
classifications,
|
|
671
|
+
objectState: state,
|
|
672
|
+
zoneEvents,
|
|
673
|
+
trackPath: track.path
|
|
674
|
+
});
|
|
675
|
+
}
|
|
676
|
+
reset() {
|
|
677
|
+
this.previousStates.clear();
|
|
678
|
+
this.filter.reset();
|
|
646
679
|
}
|
|
647
|
-
}
|
|
648
|
-
}
|
|
649
|
-
/** Attach a DetectionEvent to its associated track accumulator */
|
|
650
|
-
addEvent(trackId, event) {
|
|
651
|
-
const acc = this.activeTracks.get(trackId);
|
|
652
|
-
if (!acc) return;
|
|
653
|
-
acc.events.push(event);
|
|
654
|
-
if (event.snapshot && !acc.bestSnapshot) {
|
|
655
|
-
acc.bestSnapshot = event.snapshot;
|
|
656
|
-
}
|
|
657
|
-
}
|
|
658
|
-
/** Get accumulated detail for an active track */
|
|
659
|
-
getTrackDetail(trackId) {
|
|
660
|
-
const acc = this.activeTracks.get(trackId);
|
|
661
|
-
if (!acc) return null;
|
|
662
|
-
return this.buildTrackDetail(acc);
|
|
663
|
-
}
|
|
664
|
-
/** Called when a track ends — returns complete TrackDetail and removes from active set */
|
|
665
|
-
finishTrack(trackId) {
|
|
666
|
-
const acc = this.activeTracks.get(trackId);
|
|
667
|
-
if (!acc) return null;
|
|
668
|
-
const detail = this.buildTrackDetail(acc);
|
|
669
|
-
this.activeTracks.delete(trackId);
|
|
670
|
-
return detail;
|
|
671
|
-
}
|
|
672
|
-
/** Get all active track IDs */
|
|
673
|
-
getActiveTrackIds() {
|
|
674
|
-
return Array.from(this.activeTracks.keys());
|
|
675
|
-
}
|
|
676
|
-
/** Clear all accumulated state */
|
|
677
|
-
reset() {
|
|
678
|
-
this.activeTracks.clear();
|
|
679
|
-
}
|
|
680
|
-
buildTrackDetail(acc) {
|
|
681
|
-
return {
|
|
682
|
-
trackId: acc.trackId,
|
|
683
|
-
class: acc.class,
|
|
684
|
-
originalClass: acc.originalClass,
|
|
685
|
-
state: acc.state,
|
|
686
|
-
firstSeen: acc.firstSeen,
|
|
687
|
-
lastSeen: acc.lastSeen,
|
|
688
|
-
totalDwellMs: acc.lastSeen - acc.firstSeen,
|
|
689
|
-
path: acc.path,
|
|
690
|
-
identity: acc.identity,
|
|
691
|
-
plateText: acc.plateText,
|
|
692
|
-
subClass: acc.subClass,
|
|
693
|
-
zoneTransitions: acc.zoneTransitions,
|
|
694
|
-
bestSnapshot: acc.bestSnapshot,
|
|
695
|
-
events: acc.events
|
|
696
680
|
};
|
|
681
|
+
exports.DetectionEventEmitter = DetectionEventEmitter2;
|
|
697
682
|
}
|
|
698
|
-
};
|
|
683
|
+
});
|
|
699
684
|
|
|
700
|
-
// src/analytics/
|
|
701
|
-
var
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
685
|
+
// src/analytics/track-store.js
|
|
686
|
+
var require_track_store = __commonJS({
|
|
687
|
+
"src/analytics/track-store.js"(exports) {
|
|
688
|
+
"use strict";
|
|
689
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
690
|
+
exports.TrackStore = void 0;
|
|
691
|
+
var MAX_PATH_LENGTH = 300;
|
|
692
|
+
var TrackStore2 = class {
|
|
693
|
+
activeTracks = /* @__PURE__ */ new Map();
|
|
694
|
+
update(tracks, states, zoneEvents, classifications) {
|
|
695
|
+
const stateMap = /* @__PURE__ */ new Map();
|
|
696
|
+
for (const s of states)
|
|
697
|
+
stateMap.set(s.trackId, s);
|
|
698
|
+
const classifMap = /* @__PURE__ */ new Map();
|
|
699
|
+
for (const c of classifications)
|
|
700
|
+
classifMap.set(c.trackId, c.classifications);
|
|
701
|
+
const now = Date.now();
|
|
702
|
+
for (const track of tracks) {
|
|
703
|
+
let acc = this.activeTracks.get(track.trackId);
|
|
704
|
+
if (!acc) {
|
|
705
|
+
acc = {
|
|
706
|
+
trackId: track.trackId,
|
|
707
|
+
class: track.class,
|
|
708
|
+
originalClass: track.originalClass,
|
|
709
|
+
state: "entering",
|
|
710
|
+
firstSeen: track.path.length === 0 ? now : now,
|
|
711
|
+
lastSeen: now,
|
|
712
|
+
path: [],
|
|
713
|
+
zoneTransitions: [],
|
|
714
|
+
events: []
|
|
715
|
+
};
|
|
716
|
+
this.activeTracks.set(track.trackId, acc);
|
|
717
|
+
}
|
|
718
|
+
acc.class = track.class;
|
|
719
|
+
acc.originalClass = track.originalClass;
|
|
720
|
+
acc.lastSeen = now;
|
|
721
|
+
const state = stateMap.get(track.trackId);
|
|
722
|
+
if (state)
|
|
723
|
+
acc.state = state.state;
|
|
724
|
+
const pathPoint = {
|
|
725
|
+
timestamp: now,
|
|
726
|
+
bbox: track.bbox,
|
|
727
|
+
velocity: track.velocity ?? { dx: 0, dy: 0 }
|
|
728
|
+
};
|
|
729
|
+
if (acc.path.length >= MAX_PATH_LENGTH) {
|
|
730
|
+
acc.path.shift();
|
|
731
|
+
}
|
|
732
|
+
acc.path.push(pathPoint);
|
|
733
|
+
const classifs = classifMap.get(track.trackId) ?? [];
|
|
734
|
+
for (const c of classifs) {
|
|
735
|
+
if (c.metadata?.type === "face-recognition" && typeof c.text === "string") {
|
|
736
|
+
acc.identity = { name: c.text, confidence: c.score, matchedAt: now };
|
|
737
|
+
} else if (c.metadata?.type === "plate-recognition" && typeof c.text === "string") {
|
|
738
|
+
acc.plateText = { text: c.text, confidence: c.score, readAt: now };
|
|
739
|
+
} else if (c.metadata?.type === "sub-class") {
|
|
740
|
+
acc.subClass = { class: c.class, confidence: c.score };
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
for (const ze of zoneEvents) {
|
|
745
|
+
const acc = this.activeTracks.get(ze.trackId);
|
|
746
|
+
if (!acc)
|
|
747
|
+
continue;
|
|
748
|
+
if (ze.type === "zone-enter") {
|
|
749
|
+
acc.zoneTransitions.push({
|
|
750
|
+
zoneId: ze.zoneId,
|
|
751
|
+
zoneName: ze.zoneName,
|
|
752
|
+
entered: ze.timestamp,
|
|
753
|
+
dwellMs: 0
|
|
754
|
+
});
|
|
755
|
+
} else if (ze.type === "zone-exit") {
|
|
756
|
+
let openIdx = -1;
|
|
757
|
+
for (let i = acc.zoneTransitions.length - 1; i >= 0; i--) {
|
|
758
|
+
const t = acc.zoneTransitions[i];
|
|
759
|
+
if (t.zoneId === ze.zoneId && t.exited === void 0) {
|
|
760
|
+
openIdx = i;
|
|
761
|
+
break;
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
if (openIdx >= 0) {
|
|
765
|
+
const open = acc.zoneTransitions[openIdx];
|
|
766
|
+
acc.zoneTransitions[openIdx] = {
|
|
767
|
+
...open,
|
|
768
|
+
exited: ze.timestamp,
|
|
769
|
+
dwellMs: ze.timestamp - open.entered
|
|
770
|
+
};
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
/** Attach a DetectionEvent to its associated track accumulator */
|
|
776
|
+
addEvent(trackId, event) {
|
|
777
|
+
const acc = this.activeTracks.get(trackId);
|
|
778
|
+
if (!acc)
|
|
779
|
+
return;
|
|
780
|
+
acc.events.push(event);
|
|
781
|
+
if (event.snapshot && !acc.bestSnapshot) {
|
|
782
|
+
acc.bestSnapshot = event.snapshot;
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
/** Get accumulated detail for an active track */
|
|
786
|
+
getTrackDetail(trackId) {
|
|
787
|
+
const acc = this.activeTracks.get(trackId);
|
|
788
|
+
if (!acc)
|
|
789
|
+
return null;
|
|
790
|
+
return this.buildTrackDetail(acc);
|
|
791
|
+
}
|
|
792
|
+
/** Called when a track ends — returns complete TrackDetail and removes from active set */
|
|
793
|
+
finishTrack(trackId) {
|
|
794
|
+
const acc = this.activeTracks.get(trackId);
|
|
795
|
+
if (!acc)
|
|
796
|
+
return null;
|
|
797
|
+
const detail = this.buildTrackDetail(acc);
|
|
798
|
+
this.activeTracks.delete(trackId);
|
|
799
|
+
return detail;
|
|
800
|
+
}
|
|
801
|
+
/** Get all active track IDs */
|
|
802
|
+
getActiveTrackIds() {
|
|
803
|
+
return Array.from(this.activeTracks.keys());
|
|
804
|
+
}
|
|
805
|
+
/** Clear all accumulated state */
|
|
806
|
+
reset() {
|
|
807
|
+
this.activeTracks.clear();
|
|
808
|
+
}
|
|
809
|
+
buildTrackDetail(acc) {
|
|
810
|
+
return {
|
|
811
|
+
trackId: acc.trackId,
|
|
812
|
+
class: acc.class,
|
|
813
|
+
originalClass: acc.originalClass,
|
|
814
|
+
state: acc.state,
|
|
815
|
+
firstSeen: acc.firstSeen,
|
|
816
|
+
lastSeen: acc.lastSeen,
|
|
817
|
+
totalDwellMs: acc.lastSeen - acc.firstSeen,
|
|
818
|
+
path: acc.path,
|
|
819
|
+
identity: acc.identity,
|
|
820
|
+
plateText: acc.plateText,
|
|
821
|
+
subClass: acc.subClass,
|
|
822
|
+
zoneTransitions: acc.zoneTransitions,
|
|
823
|
+
bestSnapshot: acc.bestSnapshot,
|
|
824
|
+
events: acc.events
|
|
825
|
+
};
|
|
826
|
+
}
|
|
797
827
|
};
|
|
828
|
+
exports.TrackStore = TrackStore2;
|
|
798
829
|
}
|
|
799
|
-
|
|
800
|
-
this.pipelineMsHistory.length = 0;
|
|
801
|
-
}
|
|
802
|
-
};
|
|
830
|
+
});
|
|
803
831
|
|
|
804
|
-
// src/analytics/
|
|
805
|
-
var
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
this.addPoint(px / this.width, py / this.height);
|
|
831
|
-
}
|
|
832
|
-
getHeatmap() {
|
|
833
|
-
return {
|
|
834
|
-
width: this.width,
|
|
835
|
-
height: this.height,
|
|
836
|
-
gridSize: this.gridSize,
|
|
837
|
-
cells: new Float32Array(this.grid),
|
|
838
|
-
maxCount: this.maxCount
|
|
832
|
+
// src/analytics/live-state-manager.js
|
|
833
|
+
var require_live_state_manager = __commonJS({
|
|
834
|
+
"src/analytics/live-state-manager.js"(exports) {
|
|
835
|
+
"use strict";
|
|
836
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
837
|
+
exports.LiveStateManager = void 0;
|
|
838
|
+
var FpsCounter = class {
|
|
839
|
+
frameTimes = [];
|
|
840
|
+
windowMs = 5e3;
|
|
841
|
+
tick(timestamp) {
|
|
842
|
+
this.frameTimes.push(timestamp);
|
|
843
|
+
const cutoff = timestamp - this.windowMs;
|
|
844
|
+
while (this.frameTimes.length > 0 && this.frameTimes[0] < cutoff) {
|
|
845
|
+
this.frameTimes.shift();
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
getFps() {
|
|
849
|
+
if (this.frameTimes.length < 2)
|
|
850
|
+
return 0;
|
|
851
|
+
const oldest = this.frameTimes[0];
|
|
852
|
+
const newest = this.frameTimes[this.frameTimes.length - 1];
|
|
853
|
+
const durationSec = (newest - oldest) / 1e3;
|
|
854
|
+
if (durationSec <= 0)
|
|
855
|
+
return 0;
|
|
856
|
+
return (this.frameTimes.length - 1) / durationSec;
|
|
857
|
+
}
|
|
839
858
|
};
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
859
|
+
var LiveStateManager2 = class {
|
|
860
|
+
zones = [];
|
|
861
|
+
fpsCounter = new FpsCounter();
|
|
862
|
+
pipelineMsHistory = [];
|
|
863
|
+
maxPipelineHistory = 30;
|
|
864
|
+
setZones(zones) {
|
|
865
|
+
this.zones = [...zones];
|
|
866
|
+
}
|
|
867
|
+
buildState(deviceId, tracks, states, trackZones, pipelineStatus, pipelineMs, lastEvent, timestamp) {
|
|
868
|
+
this.fpsCounter.tick(timestamp);
|
|
869
|
+
if (this.pipelineMsHistory.length >= this.maxPipelineHistory) {
|
|
870
|
+
this.pipelineMsHistory.shift();
|
|
871
|
+
}
|
|
872
|
+
this.pipelineMsHistory.push(pipelineMs);
|
|
873
|
+
const avgPipelineMs = this.pipelineMsHistory.length === 0 ? 0 : this.pipelineMsHistory.reduce((sum, v) => sum + v, 0) / this.pipelineMsHistory.length;
|
|
874
|
+
const stateMap = /* @__PURE__ */ new Map();
|
|
875
|
+
for (const s of states)
|
|
876
|
+
stateMap.set(s.trackId, s);
|
|
877
|
+
const trackSummaries = tracks.map((track) => {
|
|
878
|
+
const state = stateMap.get(track.trackId);
|
|
879
|
+
const inZones = Array.from(trackZones.get(track.trackId) ?? []);
|
|
880
|
+
return {
|
|
881
|
+
trackId: track.trackId,
|
|
882
|
+
class: track.class,
|
|
883
|
+
originalClass: track.originalClass,
|
|
884
|
+
state: state?.state ?? "moving",
|
|
885
|
+
bbox: track.bbox,
|
|
886
|
+
velocity: track.velocity ?? { dx: 0, dy: 0 },
|
|
887
|
+
dwellTimeMs: state?.dwellTimeMs ?? 0,
|
|
888
|
+
inZones
|
|
889
|
+
};
|
|
890
|
+
});
|
|
891
|
+
let movingObjects = 0;
|
|
892
|
+
let stationaryObjects = 0;
|
|
893
|
+
for (const summary of trackSummaries) {
|
|
894
|
+
if (summary.state === "moving" || summary.state === "entering")
|
|
895
|
+
movingObjects++;
|
|
896
|
+
else if (summary.state === "stationary" || summary.state === "loitering")
|
|
897
|
+
stationaryObjects++;
|
|
898
|
+
}
|
|
899
|
+
const objectCounts = {};
|
|
900
|
+
for (const summary of trackSummaries) {
|
|
901
|
+
objectCounts[summary.class] = (objectCounts[summary.class] ?? 0) + 1;
|
|
902
|
+
}
|
|
903
|
+
const zoneLiveStates = this.zones.map((zone) => {
|
|
904
|
+
const tracksInZone = trackSummaries.filter((t) => t.inZones.includes(zone.id));
|
|
905
|
+
const objectsByClass = {};
|
|
906
|
+
const loiteringTracks = [];
|
|
907
|
+
for (const t of tracksInZone) {
|
|
908
|
+
objectsByClass[t.class] = (objectsByClass[t.class] ?? 0) + 1;
|
|
909
|
+
if (t.state === "loitering")
|
|
910
|
+
loiteringTracks.push(t.trackId);
|
|
911
|
+
}
|
|
912
|
+
const avgDwellTimeMs = tracksInZone.length === 0 ? 0 : tracksInZone.reduce((sum, t) => sum + t.dwellTimeMs, 0) / tracksInZone.length;
|
|
913
|
+
return {
|
|
914
|
+
zoneId: zone.id,
|
|
915
|
+
zoneName: zone.name,
|
|
916
|
+
occupied: tracksInZone.length > 0,
|
|
917
|
+
objectCount: tracksInZone.length,
|
|
918
|
+
objectsByClass,
|
|
919
|
+
trackIds: tracksInZone.map((t) => t.trackId),
|
|
920
|
+
avgDwellTimeMs,
|
|
921
|
+
totalEntrancesToday: 0,
|
|
922
|
+
// requires DB — left for server orchestrator
|
|
923
|
+
totalExitsToday: 0,
|
|
924
|
+
loiteringTracks
|
|
925
|
+
};
|
|
926
|
+
});
|
|
927
|
+
return {
|
|
928
|
+
deviceId,
|
|
929
|
+
lastFrameTimestamp: timestamp,
|
|
930
|
+
activeObjects: trackSummaries.length,
|
|
931
|
+
movingObjects,
|
|
932
|
+
stationaryObjects,
|
|
933
|
+
objectCounts,
|
|
934
|
+
tracks: trackSummaries,
|
|
935
|
+
zones: zoneLiveStates,
|
|
936
|
+
lastEvent,
|
|
937
|
+
pipelineStatus,
|
|
938
|
+
avgPipelineMs,
|
|
939
|
+
currentFps: this.fpsCounter.getFps()
|
|
940
|
+
};
|
|
941
|
+
}
|
|
942
|
+
reset() {
|
|
943
|
+
this.pipelineMsHistory.length = 0;
|
|
944
|
+
}
|
|
902
945
|
};
|
|
946
|
+
exports.LiveStateManager = LiveStateManager2;
|
|
903
947
|
}
|
|
904
|
-
|
|
905
|
-
return this.trackStore.getTrackDetail(trackId);
|
|
906
|
-
}
|
|
907
|
-
getCameraStatus(_deviceId) {
|
|
908
|
-
return this.lastLiveState ? [...this.lastLiveState.pipelineStatus] : [];
|
|
909
|
-
}
|
|
910
|
-
};
|
|
948
|
+
});
|
|
911
949
|
|
|
912
|
-
// src/
|
|
913
|
-
var
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
950
|
+
// src/analytics/heatmap-aggregator.js
|
|
951
|
+
var require_heatmap_aggregator = __commonJS({
|
|
952
|
+
"src/analytics/heatmap-aggregator.js"(exports) {
|
|
953
|
+
"use strict";
|
|
954
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
955
|
+
exports.HeatmapAggregator = void 0;
|
|
956
|
+
var HeatmapAggregator2 = class {
|
|
957
|
+
width;
|
|
958
|
+
height;
|
|
959
|
+
gridSize;
|
|
960
|
+
grid;
|
|
961
|
+
maxCount = 0;
|
|
962
|
+
constructor(width, height, gridSize) {
|
|
963
|
+
this.width = width;
|
|
964
|
+
this.height = height;
|
|
965
|
+
this.gridSize = gridSize;
|
|
966
|
+
if (gridSize <= 0)
|
|
967
|
+
throw new Error("gridSize must be > 0");
|
|
968
|
+
this.grid = new Float32Array(gridSize * gridSize);
|
|
969
|
+
}
|
|
970
|
+
/**
|
|
971
|
+
* Add a normalized point (0–1 range) to the heatmap.
|
|
972
|
+
* Points outside [0, 1] are clamped to the grid boundary.
|
|
973
|
+
*/
|
|
974
|
+
addPoint(x, y) {
|
|
975
|
+
const col = Math.min(Math.floor(x * this.gridSize), this.gridSize - 1);
|
|
976
|
+
const row = Math.min(Math.floor(y * this.gridSize), this.gridSize - 1);
|
|
977
|
+
const idx = row * this.gridSize + col;
|
|
978
|
+
this.grid[idx] += 1;
|
|
979
|
+
if (this.grid[idx] > this.maxCount) {
|
|
980
|
+
this.maxCount = this.grid[idx];
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
/** Add a pixel-coordinate point. Normalizes against the configured width/height. */
|
|
984
|
+
addPixelPoint(px, py) {
|
|
985
|
+
this.addPoint(px / this.width, py / this.height);
|
|
986
|
+
}
|
|
987
|
+
getHeatmap() {
|
|
988
|
+
return {
|
|
989
|
+
width: this.width,
|
|
990
|
+
height: this.height,
|
|
991
|
+
gridSize: this.gridSize,
|
|
992
|
+
cells: new Float32Array(this.grid),
|
|
993
|
+
maxCount: this.maxCount
|
|
994
|
+
};
|
|
995
|
+
}
|
|
996
|
+
reset() {
|
|
997
|
+
this.grid.fill(0);
|
|
998
|
+
this.maxCount = 0;
|
|
999
|
+
}
|
|
1000
|
+
/** Total number of points accumulated */
|
|
1001
|
+
get totalPoints() {
|
|
1002
|
+
let total = 0;
|
|
1003
|
+
for (let i = 0; i < this.grid.length; i++) {
|
|
1004
|
+
total += this.grid[i];
|
|
1005
|
+
}
|
|
1006
|
+
return total;
|
|
1007
|
+
}
|
|
938
1008
|
};
|
|
1009
|
+
exports.HeatmapAggregator = HeatmapAggregator2;
|
|
939
1010
|
}
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
const channels = frame.format === "rgb" ? 3 : 3;
|
|
1012
|
-
return {
|
|
1013
|
-
raw: {
|
|
1014
|
-
width: frame.width,
|
|
1015
|
-
height: frame.height,
|
|
1016
|
-
channels
|
|
1011
|
+
});
|
|
1012
|
+
|
|
1013
|
+
// src/analytics/analytics-provider.js
|
|
1014
|
+
var require_analytics_provider = __commonJS({
|
|
1015
|
+
"src/analytics/analytics-provider.js"(exports) {
|
|
1016
|
+
"use strict";
|
|
1017
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1018
|
+
exports.AnalyticsProvider = void 0;
|
|
1019
|
+
var AnalyticsProvider2 = class {
|
|
1020
|
+
liveStateManager;
|
|
1021
|
+
trackStore;
|
|
1022
|
+
getZoneHistoryCb;
|
|
1023
|
+
getHeatmapCb;
|
|
1024
|
+
lastLiveState;
|
|
1025
|
+
constructor(liveStateManager, trackStore, getZoneHistoryCb, getHeatmapCb, lastLiveState = null) {
|
|
1026
|
+
this.liveStateManager = liveStateManager;
|
|
1027
|
+
this.trackStore = trackStore;
|
|
1028
|
+
this.getZoneHistoryCb = getZoneHistoryCb;
|
|
1029
|
+
this.getHeatmapCb = getHeatmapCb;
|
|
1030
|
+
this.lastLiveState = lastLiveState;
|
|
1031
|
+
}
|
|
1032
|
+
/** Called by the orchestrator each frame to update the cached live state */
|
|
1033
|
+
updateLiveState(state) {
|
|
1034
|
+
this.lastLiveState = state;
|
|
1035
|
+
}
|
|
1036
|
+
getLiveState(_deviceId) {
|
|
1037
|
+
return this.lastLiveState;
|
|
1038
|
+
}
|
|
1039
|
+
getTracks(_deviceId, filter) {
|
|
1040
|
+
const tracks = this.lastLiveState?.tracks ?? [];
|
|
1041
|
+
if (!filter)
|
|
1042
|
+
return [...tracks];
|
|
1043
|
+
return tracks.filter((t) => {
|
|
1044
|
+
if (filter.class && !filter.class.includes(t.class))
|
|
1045
|
+
return false;
|
|
1046
|
+
if (filter.state && !filter.state.includes(t.state))
|
|
1047
|
+
return false;
|
|
1048
|
+
if (filter.inZone && !t.inZones.includes(filter.inZone))
|
|
1049
|
+
return false;
|
|
1050
|
+
if (filter.minDwellMs !== void 0 && t.dwellTimeMs < filter.minDwellMs)
|
|
1051
|
+
return false;
|
|
1052
|
+
return true;
|
|
1053
|
+
});
|
|
1054
|
+
}
|
|
1055
|
+
getZoneState(_deviceId, zoneId) {
|
|
1056
|
+
return this.lastLiveState?.zones.find((z) => z.zoneId === zoneId) ?? null;
|
|
1057
|
+
}
|
|
1058
|
+
async getZoneHistory(deviceId, zoneId, options) {
|
|
1059
|
+
if (this.getZoneHistoryCb) {
|
|
1060
|
+
return this.getZoneHistoryCb(deviceId, zoneId, options);
|
|
1061
|
+
}
|
|
1062
|
+
return [];
|
|
1063
|
+
}
|
|
1064
|
+
async getHeatmap(deviceId, options) {
|
|
1065
|
+
if (this.getHeatmapCb) {
|
|
1066
|
+
return this.getHeatmapCb(deviceId, options);
|
|
1067
|
+
}
|
|
1068
|
+
const gridSize = options.resolution;
|
|
1069
|
+
return {
|
|
1070
|
+
width: 1920,
|
|
1071
|
+
height: 1080,
|
|
1072
|
+
gridSize,
|
|
1073
|
+
cells: new Float32Array(gridSize * gridSize),
|
|
1074
|
+
maxCount: 0
|
|
1075
|
+
};
|
|
1076
|
+
}
|
|
1077
|
+
getTrackDetail(_deviceId, trackId) {
|
|
1078
|
+
return this.trackStore.getTrackDetail(trackId);
|
|
1079
|
+
}
|
|
1080
|
+
getCameraStatus(_deviceId) {
|
|
1081
|
+
return this.lastLiveState ? [...this.lastLiveState.pipelineStatus] : [];
|
|
1017
1082
|
}
|
|
1018
1083
|
};
|
|
1084
|
+
exports.AnalyticsProvider = AnalyticsProvider2;
|
|
1019
1085
|
}
|
|
1020
|
-
|
|
1021
|
-
if (this.config.outputDir) {
|
|
1022
|
-
const { mkdir } = await import("fs/promises");
|
|
1023
|
-
await mkdir(this.config.outputDir, { recursive: true });
|
|
1024
|
-
}
|
|
1025
|
-
}
|
|
1026
|
-
};
|
|
1086
|
+
});
|
|
1027
1087
|
|
|
1028
|
-
// src/
|
|
1029
|
-
var
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1088
|
+
// src/snapshots/snapshot-manager.js
|
|
1089
|
+
var require_snapshot_manager = __commonJS({
|
|
1090
|
+
"src/snapshots/snapshot-manager.js"(exports) {
|
|
1091
|
+
"use strict";
|
|
1092
|
+
var __createBinding = exports && exports.__createBinding || (Object.create ? (function(o, m, k, k2) {
|
|
1093
|
+
if (k2 === void 0) k2 = k;
|
|
1094
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
1095
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
1096
|
+
desc = { enumerable: true, get: function() {
|
|
1097
|
+
return m[k];
|
|
1098
|
+
} };
|
|
1099
|
+
}
|
|
1100
|
+
Object.defineProperty(o, k2, desc);
|
|
1101
|
+
}) : (function(o, m, k, k2) {
|
|
1102
|
+
if (k2 === void 0) k2 = k;
|
|
1103
|
+
o[k2] = m[k];
|
|
1104
|
+
}));
|
|
1105
|
+
var __setModuleDefault = exports && exports.__setModuleDefault || (Object.create ? (function(o, v) {
|
|
1106
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
1107
|
+
}) : function(o, v) {
|
|
1108
|
+
o["default"] = v;
|
|
1109
|
+
});
|
|
1110
|
+
var __importStar = exports && exports.__importStar || /* @__PURE__ */ (function() {
|
|
1111
|
+
var ownKeys = function(o) {
|
|
1112
|
+
ownKeys = Object.getOwnPropertyNames || function(o2) {
|
|
1113
|
+
var ar = [];
|
|
1114
|
+
for (var k in o2) if (Object.prototype.hasOwnProperty.call(o2, k)) ar[ar.length] = k;
|
|
1115
|
+
return ar;
|
|
1116
|
+
};
|
|
1117
|
+
return ownKeys(o);
|
|
1118
|
+
};
|
|
1119
|
+
return function(mod) {
|
|
1120
|
+
if (mod && mod.__esModule) return mod;
|
|
1121
|
+
var result = {};
|
|
1122
|
+
if (mod != null) {
|
|
1123
|
+
for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
1124
|
+
}
|
|
1125
|
+
__setModuleDefault(result, mod);
|
|
1126
|
+
return result;
|
|
1127
|
+
};
|
|
1128
|
+
})();
|
|
1129
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1130
|
+
exports.SnapshotManager = exports.DEFAULT_SNAPSHOT_CONFIG = void 0;
|
|
1131
|
+
exports.DEFAULT_SNAPSHOT_CONFIG = {
|
|
1132
|
+
enabled: false,
|
|
1133
|
+
saveThumbnail: true,
|
|
1134
|
+
saveAnnotatedFrame: false,
|
|
1135
|
+
saveDebugThumbnails: false
|
|
1054
1136
|
};
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
this.cameras.clear();
|
|
1060
|
-
this.listeners.clear();
|
|
1061
|
-
}
|
|
1062
|
-
async processFrame(deviceId, frame, pipelineResult) {
|
|
1063
|
-
const camera = this.getOrCreateCamera(deviceId);
|
|
1064
|
-
const frameWidth = frame.width > 0 ? frame.width : this.config.defaultFrameWidth;
|
|
1065
|
-
const frameHeight = frame.height > 0 ? frame.height : this.config.defaultFrameHeight;
|
|
1066
|
-
const timestamp = frame.timestamp;
|
|
1067
|
-
const detections = pipelineResult ? this.extractDetections(pipelineResult) : [];
|
|
1068
|
-
const tracked = camera.tracker.update(detections, timestamp);
|
|
1069
|
-
const states = camera.stateAnalyzer.analyze(tracked, timestamp);
|
|
1070
|
-
const zoneEvents = camera.zoneEvaluator.evaluate(
|
|
1071
|
-
tracked,
|
|
1072
|
-
camera.zones,
|
|
1073
|
-
frameWidth,
|
|
1074
|
-
frameHeight,
|
|
1075
|
-
timestamp
|
|
1076
|
-
);
|
|
1077
|
-
const classificationsByTrack = pipelineResult ? this.matchClassifications(pipelineResult, tracked.map((t) => t.trackId)) : [];
|
|
1078
|
-
camera.trackStore.update(tracked, states, zoneEvents, classificationsByTrack);
|
|
1079
|
-
const events = camera.eventEmitter.emit(
|
|
1080
|
-
tracked,
|
|
1081
|
-
states,
|
|
1082
|
-
zoneEvents,
|
|
1083
|
-
classificationsByTrack,
|
|
1084
|
-
deviceId
|
|
1085
|
-
);
|
|
1086
|
-
for (const event of events) {
|
|
1087
|
-
const snapshot = await camera.snapshotManager.capture(frame, event.detection, tracked, event.id);
|
|
1088
|
-
if (snapshot) {
|
|
1089
|
-
;
|
|
1090
|
-
event.snapshot = snapshot;
|
|
1091
|
-
}
|
|
1092
|
-
camera.trackStore.addEvent(event.detection.trackId, event);
|
|
1093
|
-
}
|
|
1094
|
-
for (const track of tracked) {
|
|
1095
|
-
const cx = (track.bbox.x + track.bbox.w / 2) / frameWidth;
|
|
1096
|
-
const cy = (track.bbox.y + track.bbox.h / 2) / frameHeight;
|
|
1097
|
-
camera.heatmapAggregator.addPoint(cx, cy);
|
|
1098
|
-
}
|
|
1099
|
-
for (const lostTrack of camera.tracker.getLostTracks()) {
|
|
1100
|
-
const detail = camera.trackStore.finishTrack(lostTrack.trackId);
|
|
1101
|
-
if (detail && this.onTrackFinished) {
|
|
1102
|
-
this.onTrackFinished(deviceId, detail);
|
|
1137
|
+
var SnapshotManager2 = class {
|
|
1138
|
+
config;
|
|
1139
|
+
constructor(config) {
|
|
1140
|
+
this.config = config;
|
|
1103
1141
|
}
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1142
|
+
async capture(frame, detection, allDetections, eventId) {
|
|
1143
|
+
if (!this.config.enabled)
|
|
1144
|
+
return void 0;
|
|
1145
|
+
await this.ensureOutputDir();
|
|
1146
|
+
let thumbnailPath;
|
|
1147
|
+
let annotatedFramePath;
|
|
1148
|
+
if (this.config.saveThumbnail) {
|
|
1149
|
+
thumbnailPath = await this.saveThumbnail(frame, detection.bbox, eventId);
|
|
1150
|
+
}
|
|
1151
|
+
if (this.config.saveAnnotatedFrame) {
|
|
1152
|
+
annotatedFramePath = await this.saveAnnotatedFrame(frame, allDetections, eventId);
|
|
1153
|
+
}
|
|
1154
|
+
if (!thumbnailPath && !annotatedFramePath)
|
|
1155
|
+
return void 0;
|
|
1156
|
+
return {
|
|
1157
|
+
thumbnailPath: thumbnailPath ?? "",
|
|
1158
|
+
annotatedFramePath: annotatedFramePath ?? ""
|
|
1159
|
+
};
|
|
1160
|
+
}
|
|
1161
|
+
async saveThumbnail(frame, bbox, eventId) {
|
|
1162
|
+
const sharp = await Promise.resolve().then(() => __importStar(__require("sharp")));
|
|
1163
|
+
const { frameWidth, frameHeight } = await this.resolveFrameDimensions(frame);
|
|
1164
|
+
const left = Math.max(0, Math.round(bbox.x));
|
|
1165
|
+
const top = Math.max(0, Math.round(bbox.y));
|
|
1166
|
+
const width = Math.min(Math.round(bbox.w), frameWidth - left);
|
|
1167
|
+
const height = Math.min(Math.round(bbox.h), frameHeight - top);
|
|
1168
|
+
if (width <= 0 || height <= 0) {
|
|
1169
|
+
throw new Error(`Invalid crop dimensions for event ${eventId}: ${JSON.stringify({ left, top, width, height, frameWidth, frameHeight })}`);
|
|
1170
|
+
}
|
|
1171
|
+
const relativePath = `${eventId}_thumb.jpg`;
|
|
1172
|
+
const inputOptions = this.sharpInputOptions(frame);
|
|
1173
|
+
const buffer = await sharp.default(frame.data, inputOptions).extract({ left, top, width, height }).jpeg({ quality: 85 }).toBuffer();
|
|
1174
|
+
await this.writeOutput(relativePath, buffer);
|
|
1175
|
+
return relativePath;
|
|
1176
|
+
}
|
|
1177
|
+
async saveAnnotatedFrame(frame, detections, eventId) {
|
|
1178
|
+
const sharp = await Promise.resolve().then(() => __importStar(__require("sharp")));
|
|
1179
|
+
const { frameWidth, frameHeight } = await this.resolveFrameDimensions(frame);
|
|
1180
|
+
const relativePath = `${eventId}_annotated.jpg`;
|
|
1181
|
+
const svgBoxes = detections.map((det) => {
|
|
1182
|
+
const x = Math.max(0, Math.round(det.bbox.x));
|
|
1183
|
+
const y = Math.max(0, Math.round(det.bbox.y));
|
|
1184
|
+
const w = Math.min(Math.round(det.bbox.w), frameWidth - x);
|
|
1185
|
+
const h = Math.min(Math.round(det.bbox.h), frameHeight - y);
|
|
1186
|
+
const label = `${det.class} ${(det.score * 100).toFixed(0)}%`;
|
|
1187
|
+
return `<rect x="${x}" y="${y}" width="${w}" height="${h}" fill="none" stroke="lime" stroke-width="2"/>
|
|
1188
|
+
<text x="${x + 2}" y="${Math.max(y - 2, 10)}" font-size="12" fill="lime" font-family="sans-serif">${label}</text>`;
|
|
1189
|
+
}).join("\n");
|
|
1190
|
+
const svgOverlay = Buffer.from(`<svg width="${frameWidth}" height="${frameHeight}" xmlns="http://www.w3.org/2000/svg">${svgBoxes}</svg>`);
|
|
1191
|
+
const inputOptions = this.sharpInputOptions(frame);
|
|
1192
|
+
const buffer = await sharp.default(frame.data, inputOptions).composite([{ input: svgOverlay, top: 0, left: 0 }]).jpeg({ quality: 85 }).toBuffer();
|
|
1193
|
+
await this.writeOutput(relativePath, buffer);
|
|
1194
|
+
return relativePath;
|
|
1195
|
+
}
|
|
1196
|
+
/**
|
|
1197
|
+
* Write output using mediaStorage if available, otherwise fall back to fs.writeFile.
|
|
1198
|
+
*/
|
|
1199
|
+
async writeOutput(relativePath, data) {
|
|
1200
|
+
if (this.config.mediaStorage) {
|
|
1201
|
+
await this.config.mediaStorage.writeFile(relativePath, data);
|
|
1202
|
+
} else if (this.config.outputDir) {
|
|
1203
|
+
const { writeFile } = await Promise.resolve().then(() => __importStar(__require("fs/promises")));
|
|
1204
|
+
const { join } = await Promise.resolve().then(() => __importStar(__require("path")));
|
|
1205
|
+
await writeFile(join(this.config.outputDir, relativePath), data);
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
/**
|
|
1209
|
+
* Resolve actual frame dimensions. JPEG frames from decoders may report
|
|
1210
|
+
* width=0, height=0 — in that case read the real size from sharp metadata.
|
|
1211
|
+
*/
|
|
1212
|
+
async resolveFrameDimensions(frame) {
|
|
1213
|
+
if (frame.width > 0 && frame.height > 0) {
|
|
1214
|
+
return { frameWidth: frame.width, frameHeight: frame.height };
|
|
1215
|
+
}
|
|
1216
|
+
if (frame.format === "jpeg") {
|
|
1217
|
+
const sharp = await Promise.resolve().then(() => __importStar(__require("sharp")));
|
|
1218
|
+
const meta = await sharp.default(frame.data).metadata();
|
|
1219
|
+
return {
|
|
1220
|
+
frameWidth: meta.width ?? 0,
|
|
1221
|
+
frameHeight: meta.height ?? 0
|
|
1222
|
+
};
|
|
1223
|
+
}
|
|
1224
|
+
return { frameWidth: frame.width, frameHeight: frame.height };
|
|
1225
|
+
}
|
|
1226
|
+
sharpInputOptions(frame) {
|
|
1227
|
+
if (frame.format === "jpeg") {
|
|
1228
|
+
return {};
|
|
1229
|
+
}
|
|
1230
|
+
const channels = frame.format === "rgb" ? 3 : 3;
|
|
1231
|
+
return {
|
|
1232
|
+
raw: {
|
|
1233
|
+
width: frame.width,
|
|
1234
|
+
height: frame.height,
|
|
1235
|
+
channels
|
|
1236
|
+
}
|
|
1237
|
+
};
|
|
1238
|
+
}
|
|
1239
|
+
async ensureOutputDir() {
|
|
1240
|
+
if (this.config.outputDir) {
|
|
1241
|
+
const { mkdir } = await Promise.resolve().then(() => __importStar(__require("fs/promises")));
|
|
1242
|
+
await mkdir(this.config.outputDir, { recursive: true });
|
|
1243
|
+
}
|
|
1123
1244
|
}
|
|
1124
|
-
}
|
|
1125
|
-
return events;
|
|
1126
|
-
}
|
|
1127
|
-
setCameraPipeline(deviceId, _config) {
|
|
1128
|
-
this.getOrCreateCamera(deviceId);
|
|
1129
|
-
}
|
|
1130
|
-
setCameraZones(deviceId, zones) {
|
|
1131
|
-
const camera = this.getOrCreateCamera(deviceId);
|
|
1132
|
-
camera.zones = [...zones];
|
|
1133
|
-
camera.liveStateManager.setZones(zones);
|
|
1134
|
-
}
|
|
1135
|
-
setKnownFaces(faces) {
|
|
1136
|
-
this.knownFaces = [...faces];
|
|
1137
|
-
}
|
|
1138
|
-
setKnownPlates(plates) {
|
|
1139
|
-
this.knownPlates = [...plates];
|
|
1140
|
-
}
|
|
1141
|
-
onLiveStateChange(deviceId, callback) {
|
|
1142
|
-
if (!this.listeners.has(deviceId)) {
|
|
1143
|
-
this.listeners.set(deviceId, /* @__PURE__ */ new Set());
|
|
1144
|
-
}
|
|
1145
|
-
this.listeners.get(deviceId).add(callback);
|
|
1146
|
-
return () => {
|
|
1147
|
-
this.listeners.get(deviceId)?.delete(callback);
|
|
1148
1245
|
};
|
|
1246
|
+
exports.SnapshotManager = SnapshotManager2;
|
|
1149
1247
|
}
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
height: this.config.defaultFrameHeight,
|
|
1176
|
-
gridSize,
|
|
1177
|
-
cells: new Float32Array(gridSize * gridSize),
|
|
1178
|
-
maxCount: 0
|
|
1248
|
+
});
|
|
1249
|
+
|
|
1250
|
+
// src/pipeline/analysis-pipeline.js
|
|
1251
|
+
var require_analysis_pipeline = __commonJS({
|
|
1252
|
+
"src/pipeline/analysis-pipeline.js"(exports) {
|
|
1253
|
+
"use strict";
|
|
1254
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1255
|
+
exports.AnalysisPipeline = void 0;
|
|
1256
|
+
var sort_tracker_js_1 = require_sort_tracker();
|
|
1257
|
+
var state_analyzer_js_1 = require_state_analyzer();
|
|
1258
|
+
var zone_evaluator_js_1 = require_zone_evaluator();
|
|
1259
|
+
var event_emitter_js_1 = require_event_emitter();
|
|
1260
|
+
var track_store_js_1 = require_track_store();
|
|
1261
|
+
var live_state_manager_js_1 = require_live_state_manager();
|
|
1262
|
+
var heatmap_aggregator_js_1 = require_heatmap_aggregator();
|
|
1263
|
+
var analytics_provider_js_1 = require_analytics_provider();
|
|
1264
|
+
var snapshot_manager_js_1 = require_snapshot_manager();
|
|
1265
|
+
var AnalysisPipeline2 = class {
|
|
1266
|
+
id = "pipeline-analysis";
|
|
1267
|
+
manifest = {
|
|
1268
|
+
id: "pipeline-analysis",
|
|
1269
|
+
name: "Pipeline Analysis",
|
|
1270
|
+
version: "0.1.0",
|
|
1271
|
+
description: "Object tracking, state analysis, zone evaluation, and event emission",
|
|
1272
|
+
packageName: "@camstack/lib-pipeline-analysis"
|
|
1179
1273
|
};
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1274
|
+
config;
|
|
1275
|
+
cameras = /* @__PURE__ */ new Map();
|
|
1276
|
+
knownFaces = [];
|
|
1277
|
+
knownPlates = [];
|
|
1278
|
+
listeners = /* @__PURE__ */ new Map();
|
|
1279
|
+
/** Optional callback: server orchestrator can hook in to persist finished tracks */
|
|
1280
|
+
onTrackFinished;
|
|
1281
|
+
constructor(config = {}) {
|
|
1282
|
+
this.config = {
|
|
1283
|
+
tracker: config.tracker ?? {},
|
|
1284
|
+
stateAnalyzer: config.stateAnalyzer ?? {},
|
|
1285
|
+
eventFilter: config.eventFilter ?? {},
|
|
1286
|
+
snapshot: config.snapshot ?? {},
|
|
1287
|
+
heatmapGridSize: config.heatmapGridSize ?? 32,
|
|
1288
|
+
defaultFrameWidth: config.defaultFrameWidth ?? 1920,
|
|
1289
|
+
defaultFrameHeight: config.defaultFrameHeight ?? 1080
|
|
1290
|
+
};
|
|
1291
|
+
}
|
|
1292
|
+
async initialize(_ctx) {
|
|
1293
|
+
}
|
|
1294
|
+
async shutdown() {
|
|
1295
|
+
this.cameras.clear();
|
|
1296
|
+
this.listeners.clear();
|
|
1297
|
+
}
|
|
1298
|
+
async processFrame(deviceId, frame, pipelineResult) {
|
|
1299
|
+
const camera = this.getOrCreateCamera(deviceId);
|
|
1300
|
+
const frameWidth = frame.width > 0 ? frame.width : this.config.defaultFrameWidth;
|
|
1301
|
+
const frameHeight = frame.height > 0 ? frame.height : this.config.defaultFrameHeight;
|
|
1302
|
+
const timestamp = frame.timestamp;
|
|
1303
|
+
const detections = pipelineResult ? this.extractDetections(pipelineResult) : [];
|
|
1304
|
+
const tracked = camera.tracker.update(detections, timestamp);
|
|
1305
|
+
const states = camera.stateAnalyzer.analyze(tracked, timestamp);
|
|
1306
|
+
const zoneEvents = camera.zoneEvaluator.evaluate(tracked, camera.zones, frameWidth, frameHeight, timestamp);
|
|
1307
|
+
const classificationsByTrack = pipelineResult ? this.matchClassifications(pipelineResult, tracked.map((t) => t.trackId)) : [];
|
|
1308
|
+
camera.trackStore.update(tracked, states, zoneEvents, classificationsByTrack);
|
|
1309
|
+
const events = camera.eventEmitter.emit(tracked, states, zoneEvents, classificationsByTrack, deviceId);
|
|
1310
|
+
for (const event of events) {
|
|
1311
|
+
const snapshot = await camera.snapshotManager.capture(frame, event.detection, tracked, event.id);
|
|
1312
|
+
if (snapshot) {
|
|
1313
|
+
;
|
|
1314
|
+
event.snapshot = snapshot;
|
|
1315
|
+
}
|
|
1316
|
+
camera.trackStore.addEvent(event.detection.trackId, event);
|
|
1317
|
+
}
|
|
1318
|
+
for (const track of tracked) {
|
|
1319
|
+
const cx = (track.bbox.x + track.bbox.w / 2) / frameWidth;
|
|
1320
|
+
const cy = (track.bbox.y + track.bbox.h / 2) / frameHeight;
|
|
1321
|
+
camera.heatmapAggregator.addPoint(cx, cy);
|
|
1322
|
+
}
|
|
1323
|
+
for (const lostTrack of camera.tracker.getLostTracks()) {
|
|
1324
|
+
const detail = camera.trackStore.finishTrack(lostTrack.trackId);
|
|
1325
|
+
if (detail && this.onTrackFinished) {
|
|
1326
|
+
this.onTrackFinished(deviceId, detail);
|
|
1327
|
+
}
|
|
1328
|
+
}
|
|
1329
|
+
const pipelineMs = pipelineResult?.totalMs ?? 0;
|
|
1330
|
+
const pipelineStatus = [];
|
|
1331
|
+
const liveState = camera.liveStateManager.buildState(deviceId, tracked, states, camera.zoneEvaluator.getTrackZones(), pipelineStatus, pipelineMs, events[events.length - 1], timestamp);
|
|
1332
|
+
camera.lastLiveState = liveState;
|
|
1333
|
+
camera.analyticsProvider.updateLiveState(liveState);
|
|
1334
|
+
const cameraListeners = this.listeners.get(deviceId);
|
|
1335
|
+
if (cameraListeners) {
|
|
1336
|
+
for (const cb of cameraListeners) {
|
|
1337
|
+
cb(liveState);
|
|
1338
|
+
}
|
|
1235
1339
|
}
|
|
1340
|
+
return events;
|
|
1236
1341
|
}
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1342
|
+
setCameraPipeline(deviceId, _config) {
|
|
1343
|
+
this.getOrCreateCamera(deviceId);
|
|
1344
|
+
}
|
|
1345
|
+
setCameraZones(deviceId, zones) {
|
|
1346
|
+
const camera = this.getOrCreateCamera(deviceId);
|
|
1347
|
+
camera.zones = [...zones];
|
|
1348
|
+
camera.liveStateManager.setZones(zones);
|
|
1349
|
+
}
|
|
1350
|
+
setKnownFaces(faces) {
|
|
1351
|
+
this.knownFaces = [...faces];
|
|
1352
|
+
}
|
|
1353
|
+
setKnownPlates(plates) {
|
|
1354
|
+
this.knownPlates = [...plates];
|
|
1355
|
+
}
|
|
1356
|
+
onLiveStateChange(deviceId, callback) {
|
|
1357
|
+
if (!this.listeners.has(deviceId)) {
|
|
1358
|
+
this.listeners.set(deviceId, /* @__PURE__ */ new Set());
|
|
1247
1359
|
}
|
|
1360
|
+
this.listeners.get(deviceId).add(callback);
|
|
1361
|
+
return () => {
|
|
1362
|
+
this.listeners.get(deviceId)?.delete(callback);
|
|
1363
|
+
};
|
|
1248
1364
|
}
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1365
|
+
// ICameraAnalyticsProvider delegates to per-camera AnalyticsProvider
|
|
1366
|
+
getLiveState(deviceId) {
|
|
1367
|
+
return this.cameras.get(deviceId)?.lastLiveState ?? null;
|
|
1368
|
+
}
|
|
1369
|
+
getTracks(deviceId, filter) {
|
|
1370
|
+
const camera = this.cameras.get(deviceId);
|
|
1371
|
+
if (!camera)
|
|
1372
|
+
return [];
|
|
1373
|
+
return camera.analyticsProvider.getTracks(deviceId, filter);
|
|
1374
|
+
}
|
|
1375
|
+
getZoneState(deviceId, zoneId) {
|
|
1376
|
+
const camera = this.cameras.get(deviceId);
|
|
1377
|
+
if (!camera)
|
|
1378
|
+
return null;
|
|
1379
|
+
return camera.analyticsProvider.getZoneState(deviceId, zoneId);
|
|
1380
|
+
}
|
|
1381
|
+
async getZoneHistory(deviceId, zoneId, options) {
|
|
1382
|
+
const camera = this.cameras.get(deviceId);
|
|
1383
|
+
if (!camera)
|
|
1384
|
+
return [];
|
|
1385
|
+
return camera.analyticsProvider.getZoneHistory(deviceId, zoneId, options);
|
|
1386
|
+
}
|
|
1387
|
+
async getHeatmap(deviceId, options) {
|
|
1388
|
+
const camera = this.cameras.get(deviceId);
|
|
1389
|
+
if (!camera) {
|
|
1390
|
+
const gridSize = options.resolution;
|
|
1391
|
+
return {
|
|
1392
|
+
width: this.config.defaultFrameWidth,
|
|
1393
|
+
height: this.config.defaultFrameHeight,
|
|
1394
|
+
gridSize,
|
|
1395
|
+
cells: new Float32Array(gridSize * gridSize),
|
|
1396
|
+
maxCount: 0
|
|
1397
|
+
};
|
|
1398
|
+
}
|
|
1399
|
+
return camera.heatmapAggregator.getHeatmap();
|
|
1400
|
+
}
|
|
1401
|
+
getTrackDetail(deviceId, trackId) {
|
|
1402
|
+
const camera = this.cameras.get(deviceId);
|
|
1403
|
+
if (!camera)
|
|
1404
|
+
return null;
|
|
1405
|
+
return camera.trackStore.getTrackDetail(trackId);
|
|
1406
|
+
}
|
|
1407
|
+
getCameraStatus(deviceId) {
|
|
1408
|
+
const camera = this.cameras.get(deviceId);
|
|
1409
|
+
if (!camera)
|
|
1410
|
+
return [];
|
|
1411
|
+
return camera.analyticsProvider.getCameraStatus(deviceId);
|
|
1412
|
+
}
|
|
1413
|
+
getOrCreateCamera(deviceId) {
|
|
1414
|
+
const existing = this.cameras.get(deviceId);
|
|
1415
|
+
if (existing)
|
|
1416
|
+
return existing;
|
|
1417
|
+
const tracker = new sort_tracker_js_1.SortTracker(this.config.tracker);
|
|
1418
|
+
const stateAnalyzer = new state_analyzer_js_1.StateAnalyzer(this.config.stateAnalyzer);
|
|
1419
|
+
const zoneEvaluator = new zone_evaluator_js_1.ZoneEvaluator();
|
|
1420
|
+
const eventEmitter = new event_emitter_js_1.DetectionEventEmitter(this.config.eventFilter);
|
|
1421
|
+
const trackStore = new track_store_js_1.TrackStore();
|
|
1422
|
+
const liveStateManager = new live_state_manager_js_1.LiveStateManager();
|
|
1423
|
+
const heatmapAggregator = new heatmap_aggregator_js_1.HeatmapAggregator(this.config.defaultFrameWidth, this.config.defaultFrameHeight, this.config.heatmapGridSize);
|
|
1424
|
+
const analyticsProvider = new analytics_provider_js_1.AnalyticsProvider(liveStateManager, trackStore);
|
|
1425
|
+
const snapshotManager = new snapshot_manager_js_1.SnapshotManager({
|
|
1426
|
+
...snapshot_manager_js_1.DEFAULT_SNAPSHOT_CONFIG,
|
|
1427
|
+
...this.config.snapshot
|
|
1428
|
+
});
|
|
1429
|
+
const cameraState = {
|
|
1430
|
+
tracker,
|
|
1431
|
+
stateAnalyzer,
|
|
1432
|
+
zoneEvaluator,
|
|
1433
|
+
eventEmitter,
|
|
1434
|
+
trackStore,
|
|
1435
|
+
liveStateManager,
|
|
1436
|
+
heatmapAggregator,
|
|
1437
|
+
analyticsProvider,
|
|
1438
|
+
snapshotManager,
|
|
1439
|
+
zones: [],
|
|
1440
|
+
lastLiveState: null
|
|
1441
|
+
};
|
|
1442
|
+
this.cameras.set(deviceId, cameraState);
|
|
1443
|
+
return cameraState;
|
|
1444
|
+
}
|
|
1445
|
+
extractDetections(pipelineResult) {
|
|
1446
|
+
const detections = [];
|
|
1447
|
+
for (const stepResult of pipelineResult.results) {
|
|
1448
|
+
if (stepResult.slot === "detector") {
|
|
1449
|
+
const output = stepResult.output;
|
|
1450
|
+
if (output.detections) {
|
|
1451
|
+
detections.push(...output.detections);
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
}
|
|
1455
|
+
return detections;
|
|
1456
|
+
}
|
|
1457
|
+
matchClassifications(pipelineResult, trackIds) {
|
|
1458
|
+
const classifierResults = [];
|
|
1459
|
+
for (const stepResult of pipelineResult.results) {
|
|
1460
|
+
if (stepResult.slot === "classifier") {
|
|
1461
|
+
const output = stepResult.output;
|
|
1462
|
+
if (output.classifications) {
|
|
1463
|
+
classifierResults.push([...output.classifications]);
|
|
1464
|
+
}
|
|
1465
|
+
}
|
|
1466
|
+
}
|
|
1467
|
+
return trackIds.slice(0, classifierResults.length).map((trackId, i) => ({
|
|
1468
|
+
trackId,
|
|
1469
|
+
classifications: classifierResults[i] ?? []
|
|
1470
|
+
}));
|
|
1471
|
+
}
|
|
1472
|
+
};
|
|
1473
|
+
exports.AnalysisPipeline = AnalysisPipeline2;
|
|
1254
1474
|
}
|
|
1255
|
-
};
|
|
1475
|
+
});
|
|
1476
|
+
|
|
1477
|
+
// src/index.ts
|
|
1478
|
+
var src_exports = {};
|
|
1479
|
+
__export(src_exports, {
|
|
1480
|
+
AnalysisPipeline: () => import_analysis_pipeline.AnalysisPipeline,
|
|
1481
|
+
AnalyticsProvider: () => import_analytics_provider.AnalyticsProvider,
|
|
1482
|
+
DEFAULT_EVENT_FILTER_CONFIG: () => import_event_filters.DEFAULT_EVENT_FILTER_CONFIG,
|
|
1483
|
+
DEFAULT_SNAPSHOT_CONFIG: () => import_snapshot_manager.DEFAULT_SNAPSHOT_CONFIG,
|
|
1484
|
+
DEFAULT_STATE_ANALYZER_CONFIG: () => import_state_analyzer.DEFAULT_STATE_ANALYZER_CONFIG,
|
|
1485
|
+
DEFAULT_TRACKER_CONFIG: () => import_sort_tracker.DEFAULT_TRACKER_CONFIG,
|
|
1486
|
+
DetectionEventEmitter: () => import_event_emitter.DetectionEventEmitter,
|
|
1487
|
+
EventFilter: () => import_event_filters.EventFilter,
|
|
1488
|
+
HeatmapAggregator: () => import_heatmap_aggregator.HeatmapAggregator,
|
|
1489
|
+
LiveStateManager: () => import_live_state_manager.LiveStateManager,
|
|
1490
|
+
SnapshotManager: () => import_snapshot_manager.SnapshotManager,
|
|
1491
|
+
SortTracker: () => import_sort_tracker.SortTracker,
|
|
1492
|
+
StateAnalyzer: () => import_state_analyzer.StateAnalyzer,
|
|
1493
|
+
TrackStore: () => import_track_store.TrackStore,
|
|
1494
|
+
ZoneEvaluator: () => import_zone_evaluator.ZoneEvaluator,
|
|
1495
|
+
greedyAssignment: () => import_hungarian.greedyAssignment
|
|
1496
|
+
});
|
|
1497
|
+
__reExport(src_exports, __toESM(require_geometry()));
|
|
1498
|
+
var import_sort_tracker = __toESM(require_sort_tracker());
|
|
1499
|
+
var import_hungarian = __toESM(require_hungarian());
|
|
1500
|
+
var import_state_analyzer = __toESM(require_state_analyzer());
|
|
1501
|
+
var import_zone_evaluator = __toESM(require_zone_evaluator());
|
|
1502
|
+
var import_event_filters = __toESM(require_event_filters());
|
|
1503
|
+
var import_event_emitter = __toESM(require_event_emitter());
|
|
1504
|
+
var import_track_store = __toESM(require_track_store());
|
|
1505
|
+
var import_live_state_manager = __toESM(require_live_state_manager());
|
|
1506
|
+
var import_heatmap_aggregator = __toESM(require_heatmap_aggregator());
|
|
1507
|
+
var import_analytics_provider = __toESM(require_analytics_provider());
|
|
1508
|
+
var import_snapshot_manager = __toESM(require_snapshot_manager());
|
|
1509
|
+
var import_analysis_pipeline = __toESM(require_analysis_pipeline());
|
|
1510
|
+
var export_AnalysisPipeline = import_analysis_pipeline.AnalysisPipeline;
|
|
1511
|
+
var export_AnalyticsProvider = import_analytics_provider.AnalyticsProvider;
|
|
1512
|
+
var export_DEFAULT_EVENT_FILTER_CONFIG = import_event_filters.DEFAULT_EVENT_FILTER_CONFIG;
|
|
1513
|
+
var export_DEFAULT_SNAPSHOT_CONFIG = import_snapshot_manager.DEFAULT_SNAPSHOT_CONFIG;
|
|
1514
|
+
var export_DEFAULT_STATE_ANALYZER_CONFIG = import_state_analyzer.DEFAULT_STATE_ANALYZER_CONFIG;
|
|
1515
|
+
var export_DEFAULT_TRACKER_CONFIG = import_sort_tracker.DEFAULT_TRACKER_CONFIG;
|
|
1516
|
+
var export_DetectionEventEmitter = import_event_emitter.DetectionEventEmitter;
|
|
1517
|
+
var export_EventFilter = import_event_filters.EventFilter;
|
|
1518
|
+
var export_HeatmapAggregator = import_heatmap_aggregator.HeatmapAggregator;
|
|
1519
|
+
var export_LiveStateManager = import_live_state_manager.LiveStateManager;
|
|
1520
|
+
var export_SnapshotManager = import_snapshot_manager.SnapshotManager;
|
|
1521
|
+
var export_SortTracker = import_sort_tracker.SortTracker;
|
|
1522
|
+
var export_StateAnalyzer = import_state_analyzer.StateAnalyzer;
|
|
1523
|
+
var export_TrackStore = import_track_store.TrackStore;
|
|
1524
|
+
var export_ZoneEvaluator = import_zone_evaluator.ZoneEvaluator;
|
|
1525
|
+
var export_greedyAssignment = import_hungarian.greedyAssignment;
|
|
1256
1526
|
export {
|
|
1257
|
-
AnalysisPipeline,
|
|
1258
|
-
AnalyticsProvider,
|
|
1259
|
-
DEFAULT_EVENT_FILTER_CONFIG,
|
|
1260
|
-
DEFAULT_SNAPSHOT_CONFIG,
|
|
1261
|
-
DEFAULT_STATE_ANALYZER_CONFIG,
|
|
1262
|
-
DEFAULT_TRACKER_CONFIG,
|
|
1263
|
-
DetectionEventEmitter,
|
|
1264
|
-
EventFilter,
|
|
1265
|
-
HeatmapAggregator,
|
|
1266
|
-
LiveStateManager,
|
|
1267
|
-
SnapshotManager,
|
|
1268
|
-
SortTracker,
|
|
1269
|
-
StateAnalyzer,
|
|
1270
|
-
TrackStore,
|
|
1271
|
-
ZoneEvaluator,
|
|
1272
|
-
|
|
1273
|
-
greedyAssignment,
|
|
1274
|
-
lineIntersection,
|
|
1275
|
-
normalizeToPixel,
|
|
1276
|
-
pointInPolygon,
|
|
1277
|
-
tripwireCrossing
|
|
1527
|
+
export_AnalysisPipeline as AnalysisPipeline,
|
|
1528
|
+
export_AnalyticsProvider as AnalyticsProvider,
|
|
1529
|
+
export_DEFAULT_EVENT_FILTER_CONFIG as DEFAULT_EVENT_FILTER_CONFIG,
|
|
1530
|
+
export_DEFAULT_SNAPSHOT_CONFIG as DEFAULT_SNAPSHOT_CONFIG,
|
|
1531
|
+
export_DEFAULT_STATE_ANALYZER_CONFIG as DEFAULT_STATE_ANALYZER_CONFIG,
|
|
1532
|
+
export_DEFAULT_TRACKER_CONFIG as DEFAULT_TRACKER_CONFIG,
|
|
1533
|
+
export_DetectionEventEmitter as DetectionEventEmitter,
|
|
1534
|
+
export_EventFilter as EventFilter,
|
|
1535
|
+
export_HeatmapAggregator as HeatmapAggregator,
|
|
1536
|
+
export_LiveStateManager as LiveStateManager,
|
|
1537
|
+
export_SnapshotManager as SnapshotManager,
|
|
1538
|
+
export_SortTracker as SortTracker,
|
|
1539
|
+
export_StateAnalyzer as StateAnalyzer,
|
|
1540
|
+
export_TrackStore as TrackStore,
|
|
1541
|
+
export_ZoneEvaluator as ZoneEvaluator,
|
|
1542
|
+
export_greedyAssignment as greedyAssignment
|
|
1278
1543
|
};
|
|
1279
1544
|
//# sourceMappingURL=index.mjs.map
|