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