@trillboards/edge-sdk 0.2.253 → 0.2.254

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.
Files changed (103) hide show
  1. package/deploy/detector/README.md +15 -0
  2. package/deploy/detector/pyproject.toml +1 -1
  3. package/deploy/detector/tests/test_face_identity.py +448 -0
  4. package/deploy/detector/tests/test_loader.py +3 -1
  5. package/deploy/detector/trillboards_detector/_version.py +2 -2
  6. package/deploy/detector/trillboards_detector/face_contract.py +166 -0
  7. package/deploy/detector/trillboards_detector/face_identity.py +467 -0
  8. package/deploy/detector/trillboards_detector/loader.py +12 -0
  9. package/deploy/detector/trillboards_detector/registry.py +45 -1
  10. package/deploy/detector/trillboards_detector/server.py +42 -1
  11. package/deploy/detector/trillboards_detector/tasks.py +12 -1
  12. package/deploy/windows/ci/test-main-stream-gpu-decode.ps1 +119 -0
  13. package/dist/CloudSensingManager.d.ts +5 -0
  14. package/dist/CloudSensingManager.d.ts.map +1 -1
  15. package/dist/CloudSensingManager.js +5 -2
  16. package/dist/CloudSensingManager.js.map +1 -1
  17. package/dist/ConnectorStatusClient.d.ts +18 -1
  18. package/dist/ConnectorStatusClient.d.ts.map +1 -1
  19. package/dist/ConnectorStatusClient.js +6 -0
  20. package/dist/ConnectorStatusClient.js.map +1 -1
  21. package/dist/DetectorFields.d.ts +77 -1
  22. package/dist/DetectorFields.d.ts.map +1 -1
  23. package/dist/DetectorFields.js +34 -4
  24. package/dist/DetectorFields.js.map +1 -1
  25. package/dist/EdgeAgent.js +1 -1
  26. package/dist/FaceIdentity.d.ts +122 -0
  27. package/dist/FaceIdentity.d.ts.map +1 -0
  28. package/dist/FaceIdentity.js +256 -0
  29. package/dist/FaceIdentity.js.map +1 -0
  30. package/dist/FaceSightingUploader.d.ts +95 -0
  31. package/dist/FaceSightingUploader.d.ts.map +1 -0
  32. package/dist/FaceSightingUploader.js +214 -0
  33. package/dist/FaceSightingUploader.js.map +1 -0
  34. package/dist/FaceWatchLane.d.ts +297 -0
  35. package/dist/FaceWatchLane.d.ts.map +1 -0
  36. package/dist/FaceWatchLane.js +660 -0
  37. package/dist/FaceWatchLane.js.map +1 -0
  38. package/dist/FaceWatchPolicy.d.ts +154 -0
  39. package/dist/FaceWatchPolicy.d.ts.map +1 -0
  40. package/dist/FaceWatchPolicy.js +101 -0
  41. package/dist/FaceWatchPolicy.js.map +1 -0
  42. package/dist/LocalDetectorClient.d.ts +11 -0
  43. package/dist/LocalDetectorClient.d.ts.map +1 -1
  44. package/dist/LocalDetectorClient.js +44 -0
  45. package/dist/LocalDetectorClient.js.map +1 -1
  46. package/dist/MainStreamRing.d.ts +63 -1
  47. package/dist/MainStreamRing.d.ts.map +1 -1
  48. package/dist/MainStreamRing.js +101 -7
  49. package/dist/MainStreamRing.js.map +1 -1
  50. package/dist/NativeReadiness.d.ts.map +1 -1
  51. package/dist/NativeReadiness.js +7 -3
  52. package/dist/NativeReadiness.js.map +1 -1
  53. package/dist/ReviewPriority.d.ts +9 -0
  54. package/dist/ReviewPriority.d.ts.map +1 -1
  55. package/dist/ReviewPriority.js +6 -1
  56. package/dist/ReviewPriority.js.map +1 -1
  57. package/dist/ScopedVlmReview.d.ts +11 -2
  58. package/dist/ScopedVlmReview.d.ts.map +1 -1
  59. package/dist/ScopedVlmReview.js +43 -16
  60. package/dist/ScopedVlmReview.js.map +1 -1
  61. package/dist/SensingProfileClient.d.ts +14 -0
  62. package/dist/SensingProfileClient.d.ts.map +1 -1
  63. package/dist/SensingProfileClient.js +9 -0
  64. package/dist/SensingProfileClient.js.map +1 -1
  65. package/dist/UniformSceneStore.d.ts +6 -0
  66. package/dist/UniformSceneStore.d.ts.map +1 -1
  67. package/dist/UniformSceneStore.js +22 -1
  68. package/dist/UniformSceneStore.js.map +1 -1
  69. package/dist/VideoSegmentUploader.d.ts +11 -0
  70. package/dist/VideoSegmentUploader.d.ts.map +1 -1
  71. package/dist/VideoSegmentUploader.js +28 -2
  72. package/dist/VideoSegmentUploader.js.map +1 -1
  73. package/dist/VlmCandidateBuffer.d.ts +2 -0
  74. package/dist/VlmCandidateBuffer.d.ts.map +1 -1
  75. package/dist/VlmCandidateBuffer.js.map +1 -1
  76. package/dist/WatchlistClient.d.ts +55 -0
  77. package/dist/WatchlistClient.d.ts.map +1 -0
  78. package/dist/WatchlistClient.js +161 -0
  79. package/dist/WatchlistClient.js.map +1 -0
  80. package/dist/autoSense.d.ts.map +1 -1
  81. package/dist/autoSense.js +3 -0
  82. package/dist/autoSense.js.map +1 -1
  83. package/dist/cli.d.ts.map +1 -1
  84. package/dist/cli.js +3 -0
  85. package/dist/cli.js.map +1 -1
  86. package/dist/connectorUpdate.d.ts +16 -0
  87. package/dist/connectorUpdate.d.ts.map +1 -1
  88. package/dist/connectorUpdate.js +30 -6
  89. package/dist/connectorUpdate.js.map +1 -1
  90. package/dist/diagnose.d.ts +1 -1
  91. package/dist/diagnose.js +1 -1
  92. package/dist/hostCpu.d.ts +56 -0
  93. package/dist/hostCpu.d.ts.map +1 -0
  94. package/dist/hostCpu.js +113 -0
  95. package/dist/hostCpu.js.map +1 -0
  96. package/dist/localSensing.d.ts +17 -0
  97. package/dist/localSensing.d.ts.map +1 -1
  98. package/dist/localSensing.js +52 -2
  99. package/dist/localSensing.js.map +1 -1
  100. package/package.json +2 -2
  101. package/retained/RetainedCaptureClient.cjs +2 -0
  102. package/retained/WebsiteAdmissionCaptureClient.cjs +3 -1
  103. package/retained/__tests__/website-admission-realtime.test.cjs +27 -2
@@ -0,0 +1,660 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.FaceWatchLane = exports.FACE_START_GRACE_MS = exports.FACE_REPORT_MIN_WINDOW_MS = void 0;
37
+ exports.personScore = personScore;
38
+ exports.bestMatch = bestMatch;
39
+ exports.announceRule = announceRule;
40
+ exports.recordVisit = recordVisit;
41
+ exports.toCameraBox = toCameraBox;
42
+ exports.attachFaceWatch = attachFaceWatch;
43
+ exports.activeFaceWatchLanes = activeFaceWatchLanes;
44
+ exports.faceCaptureReportForToken = faceCaptureReportForToken;
45
+ exports.__resetFaceWatchLanesForTests = __resetFaceWatchLanesForTests;
46
+ const path = __importStar(require("path"));
47
+ const edge_core_1 = require("@trillboards/edge-core");
48
+ const ChannelProber_1 = require("./ChannelProber");
49
+ const FaceIdentity_1 = require("./FaceIdentity");
50
+ const FaceSightingUploader_1 = require("./FaceSightingUploader");
51
+ const FaceWatchPolicy_1 = require("./FaceWatchPolicy");
52
+ const SensingProfileClient_1 = require("./SensingProfileClient");
53
+ const WatchlistClient_1 = require("./WatchlistClient");
54
+ /** A window shorter than this says nothing; the baseline is kept for the next report. */
55
+ exports.FACE_REPORT_MIN_WINDOW_MS = 1000;
56
+ /** After an attach, a running session that has not delivered a frame yet is `starting`, not `no_frames`. */
57
+ exports.FACE_START_GRACE_MS = 60000;
58
+ const MAX_TRACKS = 64;
59
+ const MAX_CROP_BYTES = 96 * 1024;
60
+ /**
61
+ * A track is a fixture, not a person, when for STATIC_MS it stayed within STATIC_SHIFT of its face width of where it
62
+ * was born and its size within STATIC_SIZE: a person at the door moves more than a tenth of their face in 30 s. Its
63
+ * spot is remembered, so the same fixture is a fixture from its first frame after a break in detection.
64
+ */
65
+ const STATIC_MS = 30000;
66
+ const STATIC_MIN_FRAMES = 20;
67
+ const STATIC_SHIFT = 0.1;
68
+ const STATIC_SIZE = 0.2;
69
+ const STATIC_SPOT_TTL_MS = 6 * 60 * 60000;
70
+ const MAX_STATIC_SPOTS = 16;
71
+ /** The best cosine of `probe` to a person, or null when the probe is closer to one of their negatives. */
72
+ function personScore(probe, templates, negatives) {
73
+ let pos = -1, neg = -1;
74
+ for (const t of templates)
75
+ pos = Math.max(pos, (0, FaceIdentity_1.faceCosine)(probe, t));
76
+ for (const n of negatives)
77
+ neg = Math.max(neg, (0, FaceIdentity_1.faceCosine)(probe, n));
78
+ return templates.length && pos > neg ? pos : null;
79
+ }
80
+ /** The gallery person a probe matches best, at or above `threshold`, or null. */
81
+ function bestMatch(probe, gallery, threshold) {
82
+ let best = null;
83
+ for (const person of gallery.people) {
84
+ const score = personScore(probe, person.templates, person.negatives);
85
+ if (score !== null && score >= threshold && (!best || score > best.similarity))
86
+ best = { personId: person.personId, similarity: score };
87
+ }
88
+ return best;
89
+ }
90
+ /**
91
+ * The announce rule, pure: given a person's recent hits and the memory, announce or not, and the memory after.
92
+ * A person is announced when `minFrames` hits fall within `windowMs` and the memory holds no visit of theirs
93
+ * seen within `revisitMs`; every hit keeps the visit alive. The memory after never holds a NEW visit: that is
94
+ * recorded (recordVisit) only once the announcement exists, so an announcement that could not be made is retried.
95
+ */
96
+ function announceRule(input) {
97
+ const m = input.method;
98
+ const recent = input.hits.filter(h => input.nowMs - h.atMs <= m.windowMs);
99
+ const entries = input.memory.announced
100
+ .filter(a => input.nowMs - a.lastSeenAtMs < m.announce.revisitMs)
101
+ .map(a => ({ ...a }));
102
+ const visit = entries.find(a => a.personId === input.personId);
103
+ if (visit)
104
+ visit.lastSeenAtMs = Math.max(visit.lastSeenAtMs, input.nowMs);
105
+ const announce = !visit && recent.length >= m.minFrames;
106
+ const bounded = entries.sort((a, b) => b.lastSeenAtMs - a.lastSeenAtMs).slice(0, m.announce.maxAnnounced);
107
+ return { announce, framesAbove: recent.length, memory: { version: 1, announced: bounded } };
108
+ }
109
+ /** The memory once a person's announcement exists: their visit recorded, seen now. Pure. */
110
+ function recordVisit(memory, personId, nowMs, method) {
111
+ const entries = memory.announced
112
+ .filter(a => a.personId !== personId && nowMs - a.lastSeenAtMs < method.announce.revisitMs)
113
+ .map(a => ({ ...a }));
114
+ entries.push({ personId, announcedAtMs: nowMs, lastSeenAtMs: nowMs });
115
+ const bounded = entries.sort((a, b) => b.lastSeenAtMs - a.lastSeenAtMs).slice(0, method.announce.maxAnnounced);
116
+ return { version: 1, announced: bounded };
117
+ }
118
+ async function sharpCrop(jpeg, box) {
119
+ try {
120
+ const sharp = require('sharp');
121
+ const meta = await sharp(jpeg).metadata();
122
+ if (!meta.width || !meta.height)
123
+ return null;
124
+ // The head and some shoulders: 1.8x the face box, square, inside the frame.
125
+ const side = Math.max(box.width, box.height) * 1.8;
126
+ const cx = box.x + box.width / 2, cy = box.y + box.height / 2;
127
+ const left = Math.max(0, Math.floor(cx - side / 2)), top = Math.max(0, Math.floor(cy - side / 2));
128
+ const width = Math.min(meta.width - left, Math.ceil(side)), height = Math.min(meta.height - top, Math.ceil(side));
129
+ if (width < 8 || height < 8)
130
+ return null;
131
+ let out = await sharp(jpeg).extract({ left, top, width, height })
132
+ .resize({ width: Math.min(320, width), height: Math.min(320, height), fit: 'inside' }).jpeg({ quality: 88 }).toBuffer();
133
+ if (out.length > MAX_CROP_BYTES)
134
+ out = await sharp(out).jpeg({ quality: 70 }).toBuffer();
135
+ return out.length <= MAX_CROP_BYTES ? out : null;
136
+ }
137
+ catch {
138
+ return null;
139
+ }
140
+ }
141
+ class FaceWatchLane {
142
+ constructor(options) {
143
+ this.capture = null;
144
+ this.output = null;
145
+ this.attached = false;
146
+ this.tracker = null;
147
+ this.tracks = new Map();
148
+ this.staticSpots = [];
149
+ /** Track ids of this process: a random base (a restart never reuses the last run's ids) and a counter. */
150
+ this.trackBase = Math.floor(Math.random() * 0x7fff) * 0x10000;
151
+ this.trackSeq = 0;
152
+ this.busy = false;
153
+ this.policy = null;
154
+ this.hits = new Map();
155
+ this.announcements = [];
156
+ /** People whose announcement is being made (waiting for its face crop): one at a time per person. */
157
+ this.announcing = new Set();
158
+ /** People whose last announcement had no face crop: warned once until one is made. */
159
+ this.cropMissWarned = new Set();
160
+ this.memory = { version: 1, announced: [] };
161
+ this.memoryStore = null;
162
+ this.timers = [];
163
+ this.started = false;
164
+ this.onAnnounceListeners = [];
165
+ this.offReason = 'not_enabled';
166
+ this.counts = { frames: 0, droppedBusy: 0, detected: 0, detectorFailures: 0, faces: 0, embedded: 0, sightings: 0,
167
+ hits: 0, announced: 0, taken: 0, staticFaces: 0 };
168
+ /** When the face output was last attached (monotonic ms): the start of its grace. */
169
+ this.attachedAtMs = null;
170
+ this.o = options;
171
+ this.now = options.now ?? Date.now;
172
+ this.monotonicMs = options.monotonicMs ?? (() => Number(process.hrtime.bigint()) / 1e6);
173
+ this.reportBase = { atMs: this.monotonicMs(), frames: 0, detected: 0, failures: 0, faces: 0, sightings: 0 };
174
+ this.log = options.log ?? ((level, message) => (level === 'warn' ? console.warn : console.log)(message));
175
+ this.profileClient = new SensingProfileClient_1.SensingProfileClient({ apiBaseUrl: options.apiBaseUrl, deviceToken: options.deviceToken }, options.poster);
176
+ this.watchlist = new WatchlistClient_1.WatchlistClient({ apiBaseUrl: options.apiBaseUrl, deviceToken: options.deviceToken,
177
+ cacheFile: path.join(options.dir, 'watchlist-gallery.json'), ...(options.poster ? { poster: options.poster } : {}) });
178
+ this.uploader = new FaceSightingUploader_1.FaceSightingUploader({ apiBaseUrl: options.apiBaseUrl, deviceToken: options.deviceToken,
179
+ dir: path.join(options.dir, 'sightings'), ...(options.poster ? { poster: options.poster } : {}), log: this.log });
180
+ }
181
+ /** Poll the camera's face capture, flush sightings, end idle tracks. Idempotent. */
182
+ start() {
183
+ if (this.started)
184
+ return;
185
+ this.started = true;
186
+ void this.poll();
187
+ const every = (ms, fn) => {
188
+ const t = setInterval(fn, ms);
189
+ if (typeof t.unref === 'function')
190
+ t.unref();
191
+ this.timers.push(t);
192
+ };
193
+ every(this.o.pollMs ?? 5 * 60000, () => void this.poll());
194
+ every(this.o.galleryPollMs ?? 60000, () => { if (this.policy)
195
+ void this.watchlist.refresh(); });
196
+ every(1000, () => { this.endIdleTracks(); void this.uploader.flush(); });
197
+ }
198
+ stop() {
199
+ this.started = false;
200
+ for (const t of this.timers)
201
+ clearInterval(t);
202
+ this.timers = [];
203
+ this.applyCapture(false);
204
+ }
205
+ /** The camera's scene memory keeps the announce memory across restarts (UniformSceneStore). */
206
+ useMemory(store) {
207
+ this.memoryStore = store;
208
+ this.memory = (0, FaceWatchPolicy_1.readWatchlistMemory)(store.get());
209
+ }
210
+ /** face_watchlist_v1 for this camera, from its applied retained profile; null = collect-only. */
211
+ setMatchPolicy(method) {
212
+ const before = this.policy;
213
+ this.policy = method && method.method === FaceWatchPolicy_1.WATCHLIST_METHOD ? method : null;
214
+ if (this.policy && (!before || before.galleryVersion !== this.policy.galleryVersion))
215
+ void this.watchlist.refresh();
216
+ if (!this.policy)
217
+ this.hits.clear();
218
+ }
219
+ onAnnounce(listener) {
220
+ this.onAnnounceListeners.push(listener);
221
+ }
222
+ /**
223
+ * The announcement this look takes, at most one, at most once: the oldest made under this method (its threshold
224
+ * and minimum frames) inside announceMs of the look's capture. Later looks take nothing until the next return.
225
+ */
226
+ take(method, capturedAtMs) {
227
+ const live = (a) => capturedAtMs - a.announcedAtMs < method.announce.announceMs;
228
+ for (let i = this.announcements.length - 1; i >= 0; i--) {
229
+ if (!live(this.announcements[i]) && this.announcements[i].announcedAtMs < capturedAtMs)
230
+ this.announcements.splice(i, 1);
231
+ }
232
+ const index = this.announcements.findIndex(a => a.threshold === method.threshold && a.minFrames === method.minFrames
233
+ && a.announcedAtMs <= capturedAtMs + 60000 && live(a));
234
+ if (index < 0)
235
+ return undefined;
236
+ this.counts.taken += 1;
237
+ return this.announcements.splice(index, 1)[0];
238
+ }
239
+ /** An announcement no look has taken yet, made within the method's announce window. */
240
+ hasPendingAnnouncement(nowMs) {
241
+ const announceMs = this.policy?.announce.announceMs ?? 0;
242
+ return this.announcements.some(a => nowMs - a.announcedAtMs < announceMs && a.announcedAtMs <= nowMs + 60000);
243
+ }
244
+ stats() {
245
+ return { feed: this.o.feedName, capture: this.capture ? { zone: this.capture.zone } : null, offReason: this.offReason,
246
+ matching: this.policy ? { threshold: this.policy.threshold, galleryVersion: this.policy.galleryVersion } : null,
247
+ gallery: this.watchlist.current()?.galleryVersion ?? null, tracks: this.tracks.size,
248
+ staticSpots: this.staticSpots.length, ...this.counts,
249
+ uploads: { ...this.uploader.counts, pending: this.uploader.pending().length },
250
+ mainStream: this.attached ? this.o.mainStream?.sessionState?.() ?? null : null,
251
+ output: this.output?.getStats?.() ?? null };
252
+ }
253
+ /** The device token of the feed this lane serves (its beacon report rides that token). */
254
+ get deviceToken() {
255
+ return this.o.deviceToken;
256
+ }
257
+ /**
258
+ * The beacon's `faceCapture` block: the window since the previous report (or the lane's start), then a new
259
+ * baseline. Null only for a window under FACE_REPORT_MIN_WINDOW_MS (the baseline is kept). In `off` every count
260
+ * is 0 and lastFrameAt null, so a camera turned off stops claiming it captures.
261
+ */
262
+ report() {
263
+ const atMs = this.monotonicMs();
264
+ const windowMs = atMs - this.reportBase.atMs;
265
+ if (!(windowMs >= exports.FACE_REPORT_MIN_WINDOW_MS))
266
+ return null;
267
+ const out = this.outputCounters();
268
+ const next = { atMs, frames: out.frames, detected: this.counts.detected, failures: this.counts.detectorFailures,
269
+ faces: this.counts.faces, sightings: this.uploader.counts.accepted };
270
+ const base = this.reportBase;
271
+ this.reportBase = next;
272
+ // A counter below its baseline started again (a new output): all of it is this window's.
273
+ const delta = (k) => (next[k] >= base[k] ? next[k] - base[k] : next[k]);
274
+ const window = { frames: delta('frames'), detected: delta('detected'), failures: delta('failures') };
275
+ const state = this.captureState(window, atMs);
276
+ const windowSeconds = Math.round(windowMs / 100) / 10;
277
+ if (state === 'off') {
278
+ return { state, capturing: false, frames: 0, framesDetected: 0, faces: 0, sightings: 0, lastFrameAt: null, windowSeconds };
279
+ }
280
+ return { state, capturing: state === 'capturing', frames: window.frames, framesDetected: window.detected,
281
+ faces: delta('faces'), sightings: delta('sightings'),
282
+ lastFrameAt: out.lastFrameAtMs !== null ? new Date(out.lastFrameAtMs).toISOString() : null, windowSeconds };
283
+ }
284
+ /** What the lane is doing (FaceCaptureState), from its own state, the camera's main-stream session and the window. */
285
+ captureState(window, atMs) {
286
+ if (!this.capture) {
287
+ if (this.offReason === 'main_stream_unknown' || this.offReason === 'main_stream_unavailable')
288
+ return 'no_main_stream';
289
+ if (this.offReason === 'face_source_unavailable')
290
+ return 'unsupported';
291
+ return 'off';
292
+ }
293
+ const flowing = () => {
294
+ if (window.frames > 0)
295
+ return window.detected === 0 && window.failures > 0 ? 'detector_down' : 'capturing';
296
+ return this.attachedAtMs !== null && atMs - this.attachedAtMs < exports.FACE_START_GRACE_MS ? 'starting' : 'no_frames';
297
+ };
298
+ let session;
299
+ try {
300
+ session = this.attached ? this.o.mainStream?.sessionState?.() : undefined;
301
+ }
302
+ catch {
303
+ session = undefined;
304
+ }
305
+ switch (session) {
306
+ case 'running': return flowing();
307
+ // An owner that cannot name its session's state: the frames are the only evidence.
308
+ case undefined: return window.frames > 0 ? flowing() : 'starting';
309
+ case 'starting': return 'starting';
310
+ case 'backoff': return 'backoff';
311
+ case 'session_limit': return 'session_limit';
312
+ default: {
313
+ // 'idle' or 'stopped': no session carries the output. The owner's probe says whether the recorder refused it.
314
+ let probe;
315
+ try {
316
+ probe = this.o.mainStream?.report?.()?.probeState;
317
+ }
318
+ catch {
319
+ probe = undefined;
320
+ }
321
+ if (probe === 'session_limit')
322
+ return 'session_limit';
323
+ if (probe === 'nvr_busy' || probe === 'auth' || probe === 'bad_path')
324
+ return 'refused';
325
+ if (probe === 'unsupported')
326
+ return 'unsupported';
327
+ return 'starting';
328
+ }
329
+ }
330
+ }
331
+ /** The face output's own frame count and last frame time (FaceZoneOutput.getStats), tolerant of an older output. */
332
+ outputCounters() {
333
+ let stats = null;
334
+ try {
335
+ stats = this.output?.getStats?.() ?? null;
336
+ }
337
+ catch {
338
+ stats = null;
339
+ }
340
+ const s = (stats && typeof stats === 'object' ? stats : {});
341
+ const frames = typeof s.frames === 'number' && Number.isFinite(s.frames) && s.frames >= 0 ? Math.floor(s.frames) : 0;
342
+ const last = typeof s.lastFrameAtMs === 'number' && Number.isFinite(s.lastFrameAtMs) && s.lastFrameAtMs > 0
343
+ ? s.lastFrameAtMs : null;
344
+ return { frames, lastFrameAtMs: last };
345
+ }
346
+ /** One profile read: only an explicit faceCapture changes anything (absent and unreadable keep the last). */
347
+ async poll() {
348
+ let profile;
349
+ try {
350
+ profile = await this.profileClient.fetchProfile();
351
+ }
352
+ catch {
353
+ return;
354
+ }
355
+ if (!profile || profile.faceCapture === undefined || profile.faceCapture === null)
356
+ return;
357
+ this.applyCapture(profile.faceCapture);
358
+ }
359
+ /** Start, re-plan (zone or fps changed) or stop the face-zone output on the camera's main-stream session. */
360
+ applyCapture(next) {
361
+ const same = next && this.capture && JSON.stringify(next) === JSON.stringify(this.capture);
362
+ if (same)
363
+ return;
364
+ this.tracker = null;
365
+ for (const id of [...this.tracks.keys()])
366
+ this.endTrack(id);
367
+ this.capture = null;
368
+ this.staticSpots = []; // zone pixels: a new zone is a new frame
369
+ if (!next) {
370
+ this.detach();
371
+ this.offReason = 'not_enabled';
372
+ return;
373
+ }
374
+ if (!(0, ChannelProber_1.deriveMainStreamUrl)(this.o.rtspUrl)) {
375
+ this.detach();
376
+ this.offReason = 'main_stream_unknown';
377
+ this.log('warn', `[face] ${this.o.feedName}: face capture is on, but this feed's main stream cannot be named; staying off`);
378
+ return;
379
+ }
380
+ const mainStream = this.o.mainStream;
381
+ if (!mainStream) {
382
+ this.offReason = 'main_stream_unavailable';
383
+ this.log('warn', `[face] ${this.o.feedName}: face capture is on, but this feed has no main-stream session to ride; staying off`);
384
+ return;
385
+ }
386
+ if (!this.output) {
387
+ try {
388
+ const factory = this.o.outputFactory ?? (() => {
389
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
390
+ const { FaceZoneOutput } = require('@trillboards/edge-platform-rtsp');
391
+ return new FaceZoneOutput();
392
+ });
393
+ const output = factory();
394
+ output.on('frame', frame => void this.onFrame(frame));
395
+ this.output = output;
396
+ }
397
+ catch (error) {
398
+ // An installed edge-platform-rtsp older than FaceZoneOutput: the lane stays off.
399
+ this.offReason = 'face_source_unavailable';
400
+ this.log('warn', `[face] ${this.o.feedName}: face capture is on, but the face-zone output is unavailable: `
401
+ + `${error instanceof Error ? error.message : String(error)}`);
402
+ return;
403
+ }
404
+ }
405
+ this.capture = next;
406
+ this.offReason = null;
407
+ this.tracker = new edge_core_1.CentroidTracker({ matchRatio: next.tracking.matchRatio, staleMs: next.tracking.staleMs, maxTracks: MAX_TRACKS });
408
+ this.output.setCapture({ zone: next.zone, fps: next.fps });
409
+ // Attaching again re-plans a running session once (a changed zone); the first attach starts or joins it.
410
+ mainStream.attachOutput(this.output);
411
+ this.attached = true;
412
+ this.attachedAtMs = this.monotonicMs();
413
+ this.log('info', `[face] ${this.o.feedName}: face capture on (${next.zone ? 'zone' : 'whole frame'}, ${next.fps} fps, `
414
+ + `on the camera's main-stream session)` + (this.policy ? '; matching' : '; collect-only'));
415
+ }
416
+ /** Take the face output off the camera's main-stream session (the session stops if nothing else rides it). */
417
+ detach() {
418
+ if (!this.output)
419
+ return;
420
+ this.output.setCapture(null);
421
+ if (this.attached)
422
+ this.o.mainStream?.detachOutput(this.output.name);
423
+ this.attached = false;
424
+ this.attachedAtMs = null;
425
+ }
426
+ /** One face-zone frame. Dropped, never queued, while the previous one is with the detector. */
427
+ async onFrame(frame) {
428
+ const capture = this.capture;
429
+ if (!capture || !this.tracker)
430
+ return;
431
+ this.counts.frames += 1;
432
+ if (this.busy) {
433
+ this.counts.droppedBusy += 1;
434
+ return;
435
+ }
436
+ this.busy = true;
437
+ let result;
438
+ try {
439
+ result = await this.o.faces(frame.jpeg, { detector: { ...FaceWatchPolicy_1.FACE_DETECTOR }, embedder: { ...FaceWatchPolicy_1.FACE_EMBEDDER },
440
+ minScore: capture.quality.minScore, minEyePx: capture.quality.minEyePx, maxAbsYaw: capture.quality.maxAbsYaw,
441
+ minSharpness: capture.quality.minSharpness, maxFaces: capture.quality.maxFaces });
442
+ }
443
+ catch {
444
+ this.counts.detectorFailures += 1;
445
+ this.busy = false;
446
+ return;
447
+ }
448
+ this.counts.detected += 1;
449
+ try {
450
+ if (this.capture !== capture || !this.tracker)
451
+ return;
452
+ const records = this.tracker.track(result.faces.map(f => f.box), frame.atMs);
453
+ this.counts.faces += result.faces.length;
454
+ result.faces.forEach((face, i) => this.observe(records[i].id, face, frame, result, capture));
455
+ for (const id of [...this.tracks.keys()])
456
+ if (!this.tracker.active().some(t => t.id === id))
457
+ this.endTrack(id);
458
+ }
459
+ finally {
460
+ this.busy = false;
461
+ }
462
+ }
463
+ observe(id, face, frame, result, capture) {
464
+ const cx = face.box.x + face.box.width / 2, cy = face.box.y + face.box.height / 2;
465
+ let t = this.tracks.get(id);
466
+ if (!t) {
467
+ const origin = { cx, cy, width: Math.max(1, face.box.width) };
468
+ t = { trackId: (this.trackBase + (++this.trackSeq % 0x10000)) >>> 0, firstSeenAtMs: frame.atMs,
469
+ lastSeenAtMs: frame.atMs, frames: 0, origin, maxShift: 0, minWidth: face.box.width, maxWidth: face.box.width,
470
+ isStatic: this.staticSpots.some(spot => sameSpot(spot, origin)), embeddedFrames: 0, best: [], maxEyePx: 0,
471
+ bestQuality: -1, crop: null, box: null, cropping: false };
472
+ this.tracks.set(id, t);
473
+ }
474
+ t.lastSeenAtMs = frame.atMs;
475
+ t.frames += 1;
476
+ t.maxShift = Math.max(t.maxShift, Math.hypot(cx - t.origin.cx, cy - t.origin.cy));
477
+ t.minWidth = Math.min(t.minWidth, face.box.width);
478
+ t.maxWidth = Math.max(t.maxWidth, face.box.width);
479
+ if (!t.isStatic && t.frames >= STATIC_MIN_FRAMES && t.lastSeenAtMs - t.firstSeenAtMs >= STATIC_MS
480
+ && t.maxShift < STATIC_SHIFT * t.origin.width && t.maxWidth - t.minWidth < STATIC_SIZE * t.origin.width) {
481
+ t.isStatic = true;
482
+ this.staticSpots = [{ ...t.origin, lastSeenAtMs: frame.atMs },
483
+ ...this.staticSpots.filter(spot => !sameSpot(spot, t.origin))].slice(0, MAX_STATIC_SPOTS);
484
+ }
485
+ if (t.isStatic) {
486
+ this.counts.staticFaces += 1;
487
+ const spot = this.staticSpots.find(s => sameSpot(s, t.origin));
488
+ if (spot)
489
+ spot.lastSeenAtMs = frame.atMs;
490
+ return;
491
+ }
492
+ t.maxEyePx = Math.max(t.maxEyePx, face.eyeDistancePx);
493
+ // The sighting's photo: the best-quality face of the track, cropped once each time a better one arrives.
494
+ if (face.quality > t.bestQuality && !t.cropping) {
495
+ t.bestQuality = face.quality;
496
+ t.box = toCameraBox(face.box, result.frameSize, capture.zone);
497
+ t.cropping = true;
498
+ const track = t;
499
+ void (this.o.cropFace ?? sharpCrop)(frame.jpeg, face.box).then(crop => { if (crop)
500
+ track.crop = crop; })
501
+ .finally(() => { track.cropping = false; });
502
+ }
503
+ if (face.gate !== 'passed' || !face.embedding)
504
+ return;
505
+ this.counts.embedded += 1;
506
+ t.embeddedFrames += 1;
507
+ t.best.push({ vector: face.embedding, quality: face.quality, eyePx: face.eyeDistancePx, atMs: frame.atMs });
508
+ t.best.sort((a, b) => b.quality - a.quality);
509
+ t.best.length = Math.min(t.best.length, capture.quality.bestK);
510
+ this.match(face, frame);
511
+ }
512
+ /** Score one embedded face against the gallery; announce a person who has returned. */
513
+ match(face, frame) {
514
+ const policy = this.policy;
515
+ const gallery = this.watchlist.current();
516
+ if (!policy || !gallery || !face.embedding)
517
+ return;
518
+ const found = bestMatch(face.embedding, gallery, policy.threshold);
519
+ if (!found)
520
+ return;
521
+ this.counts.hits += 1;
522
+ const hits = (this.hits.get(found.personId) ?? []).filter(h => frame.atMs - h.atMs <= policy.windowMs);
523
+ const hit = { atMs: frame.atMs, similarity: found.similarity, vector: face.embedding, crop: null };
524
+ hits.push(hit);
525
+ this.hits.set(found.personId, hits);
526
+ void (this.o.cropFace ?? sharpCrop)(frame.jpeg, face.box).then(crop => { hit.crop = crop; });
527
+ const rule = announceRule({ personId: found.personId, hits, nowMs: frame.atMs, method: policy, memory: this.memory });
528
+ this.memory = rule.memory;
529
+ // Every hit keeps the visit alive; the store writes an announcement at once and the rest on its own cadence.
530
+ this.memoryStore?.set(this.memory);
531
+ if (rule.announce && !this.announcing.has(found.personId)) {
532
+ this.announcing.add(found.personId);
533
+ void this.announce(found.personId, hits, rule.framesAbove, policy, gallery.galleryVersion, frame.atMs)
534
+ .finally(() => this.announcing.delete(found.personId));
535
+ }
536
+ }
537
+ async announce(personId, hits, framesAbove, policy, galleryVersion, atMs) {
538
+ const best = hits.reduce((a, b) => (b.similarity > a.similarity ? b : a));
539
+ // The crop is the best frame's; give a crop in flight a moment to land.
540
+ for (let i = 0; i < 20 && !best.crop; i++)
541
+ await new Promise(resolve => setTimeout(resolve, 25));
542
+ const crop = best.crop ?? hits.find(h => h.crop)?.crop;
543
+ if (!crop) {
544
+ // No visit is recorded, so the person's next matching frame tries again.
545
+ if (!this.cropMissWarned.has(personId)) {
546
+ this.cropMissWarned.add(personId);
547
+ this.log('warn', `[face] ${this.o.feedName}: a watchlist match had no face crop yet; announcing on the next match`);
548
+ }
549
+ return;
550
+ }
551
+ this.cropMissWarned.delete(personId);
552
+ // The visit starts now that its announcement exists; the store writes it at once.
553
+ this.memory = recordVisit(this.memory, personId, atMs, policy);
554
+ this.memoryStore?.set(this.memory);
555
+ const announcement = { personId, announcedAtMs: atMs, similarity: best.similarity,
556
+ threshold: policy.threshold, framesAbove, minFrames: policy.minFrames, galleryVersion,
557
+ detectorSha256: FaceWatchPolicy_1.FACE_DETECTOR.sha256, embedderSha256: FaceWatchPolicy_1.FACE_EMBEDDER.sha256, probeCrop: crop,
558
+ probeEmbedding: (0, FaceIdentity_1.encodeFaceVector)(best.vector) };
559
+ this.announcements.push(announcement);
560
+ this.counts.announced += 1;
561
+ this.log('info', `[face] ${this.o.feedName}: watchlist person announced (similarity ${best.similarity.toFixed(3)}, `
562
+ + `${framesAbove} frames)`);
563
+ for (const listener of this.onAnnounceListeners) {
564
+ try {
565
+ listener(announcement);
566
+ }
567
+ catch { /* a listener never stops the lane */ }
568
+ }
569
+ }
570
+ endIdleTracks() {
571
+ if (!this.tracker)
572
+ return;
573
+ const now = this.now();
574
+ this.staticSpots = this.staticSpots.filter(spot => now - spot.lastSeenAtMs < STATIC_SPOT_TTL_MS);
575
+ this.tracker.track([], now);
576
+ for (const id of [...this.tracks.keys()])
577
+ if (!this.tracker.active().some(t => t.id === id))
578
+ this.endTrack(id);
579
+ }
580
+ /** A track that ended becomes one sighting (with its photo), queued until the server answers for it. */
581
+ endTrack(id) {
582
+ const t = this.tracks.get(id);
583
+ this.tracks.delete(id);
584
+ const capture = this.capture;
585
+ if (!t || t.isStatic || !t.crop || !t.box)
586
+ return;
587
+ const mean = (0, FaceIdentity_1.qualityWeightedMean)(t.best.map(k => ({ vector: k.vector, quality: k.quality })));
588
+ const sighting = {
589
+ sightingId: (0, FaceSightingUploader_1.newSightingId)(), trackId: t.trackId, firstSeenAtMs: t.firstSeenAtMs,
590
+ lastSeenAtMs: Math.max(t.lastSeenAtMs, t.firstSeenAtMs), frames: t.frames,
591
+ embeddedFrames: mean ? t.embeddedFrames : 0, embedding: mean ? (0, FaceIdentity_1.encodeFaceVector)(mean) : null,
592
+ bestQuality: Math.max(0, Math.min(1, t.bestQuality)), maxEyeDistancePx: Math.max(0.01, t.maxEyePx), box: t.box,
593
+ zone: capture?.zone ?? { x: 0, y: 0, width: 1, height: 1 }, crop: t.crop.toString('base64'),
594
+ models: { detector: { ...FaceWatchPolicy_1.FACE_DETECTOR }, embedder: { ...FaceWatchPolicy_1.FACE_EMBEDDER } },
595
+ };
596
+ this.uploader.enqueue(sighting);
597
+ this.counts.sightings += 1;
598
+ }
599
+ }
600
+ exports.FaceWatchLane = FaceWatchLane;
601
+ /** A track born where a remembered fixture is: its centre within STATIC_SHIFT of the fixture's width, its size alike. */
602
+ function sameSpot(spot, origin) {
603
+ return Math.hypot(spot.cx - origin.cx, spot.cy - origin.cy) < STATIC_SHIFT * spot.width
604
+ && Math.abs(spot.width - origin.width) < STATIC_SIZE * spot.width;
605
+ }
606
+ /** A face box in zone-frame pixels, normalised to the whole camera frame. */
607
+ function toCameraBox(box, frame, zone) {
608
+ const z = zone ?? { x: 0, y: 0, width: 1, height: 1 };
609
+ const clamp = (v) => Math.max(0, Math.min(1, v));
610
+ const x = clamp(z.x + (box.x / frame.width) * z.width), y = clamp(z.y + (box.y / frame.height) * z.height);
611
+ const width = Math.max(1e-6, Math.min(1 - x, (box.width / frame.width) * z.width));
612
+ const height = Math.max(1e-6, Math.min(1 - y, (box.height / frame.height) * z.height));
613
+ return { x, y, width, height };
614
+ }
615
+ // ── Wiring: one lane per camera feed ─────────────────────────────────────────
616
+ const lanesByDir = new Map();
617
+ /**
618
+ * The face lane of a crop-less camera feed: polls the camera's face capture and does nothing more until it is on.
619
+ * A second lane for the same directory replaces the first.
620
+ */
621
+ function attachFaceWatch(args) {
622
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
623
+ const { LocalDetectorClient } = require('./LocalDetectorClient');
624
+ const detector = new LocalDetectorClient({ ...(args.detectorBaseUrl ? { baseUrl: args.detectorBaseUrl } : {}), timeoutMs: 10000 });
625
+ const lane = new FaceWatchLane({ feedName: args.feedName, rtspUrl: args.rtspUrl, mainStream: args.mainStream,
626
+ apiBaseUrl: args.apiBaseUrl, deviceToken: args.deviceToken, dir: args.dir, faces: (jpeg, request) => detector.faces(jpeg, request),
627
+ ...(args.httpPoster ? { poster: args.httpPoster } : {}), ...(args.pollMs !== undefined ? { pollMs: args.pollMs } : {}) });
628
+ lanesByDir.get(args.dir)?.stop();
629
+ lanesByDir.set(args.dir, lane);
630
+ lane.start();
631
+ return lane;
632
+ }
633
+ /** Every attached lane, for diagnostics. */
634
+ function activeFaceWatchLanes() {
635
+ return [...lanesByDir.values()];
636
+ }
637
+ /**
638
+ * The beacon's `faceCapture` block of the feed that uses this device token (its lane's window since the last call),
639
+ * or null for a token with no face lane. Call it once per beacon per token: each call starts a new window.
640
+ */
641
+ function faceCaptureReportForToken(deviceToken) {
642
+ for (const lane of lanesByDir.values()) {
643
+ if (lane.deviceToken !== deviceToken)
644
+ continue;
645
+ try {
646
+ return lane.report();
647
+ }
648
+ catch {
649
+ return null;
650
+ }
651
+ }
652
+ return null;
653
+ }
654
+ /** Test seam. */
655
+ function __resetFaceWatchLanesForTests() {
656
+ for (const lane of lanesByDir.values())
657
+ lane.stop();
658
+ lanesByDir.clear();
659
+ }
660
+ //# sourceMappingURL=FaceWatchLane.js.map