@trillboards/edge-sdk 0.2.246 → 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 (141) hide show
  1. package/deploy/detector/README.md +15 -0
  2. package/deploy/detector/pyproject.toml +1 -1
  3. package/deploy/detector/tests/test_actor_tracking.py +136 -0
  4. package/deploy/detector/tests/test_actor_tracking_wire.py +11 -0
  5. package/deploy/detector/tests/test_face_identity.py +448 -0
  6. package/deploy/detector/tests/test_loader.py +28 -1
  7. package/deploy/detector/tests/test_registry.py +53 -0
  8. package/deploy/detector/trillboards_detector/_version.py +2 -2
  9. package/deploy/detector/trillboards_detector/actor_tracking.py +95 -9
  10. package/deploy/detector/trillboards_detector/face_contract.py +166 -0
  11. package/deploy/detector/trillboards_detector/face_identity.py +467 -0
  12. package/deploy/detector/trillboards_detector/loader.py +40 -0
  13. package/deploy/detector/trillboards_detector/registry.py +72 -2
  14. package/deploy/detector/trillboards_detector/server.py +48 -6
  15. package/deploy/detector/trillboards_detector/tasks.py +12 -1
  16. package/deploy/windows/build-windows.ps1 +1 -0
  17. package/deploy/windows/ci/test-detector-looks-field.ps1 +15 -3
  18. package/deploy/windows/ci/test-detector-reinstall.ps1 +173 -2
  19. package/deploy/windows/ci/test-main-stream-gpu-decode.ps1 +119 -0
  20. package/deploy/windows/ci/test-rollout-proofs.ps1 +3 -2
  21. package/deploy/windows/ensure-detector.ps1 +255 -0
  22. package/deploy/windows/install-update.ps1 +4 -3
  23. package/deploy/windows/stop-existing.ps1 +1 -0
  24. package/deploy/windows/trillboards-edge.cmd +10 -22
  25. package/dist/CloudSensingManager.d.ts +14 -0
  26. package/dist/CloudSensingManager.d.ts.map +1 -1
  27. package/dist/CloudSensingManager.js +51 -5
  28. package/dist/CloudSensingManager.js.map +1 -1
  29. package/dist/ConnectorStatusClient.d.ts +26 -1
  30. package/dist/ConnectorStatusClient.d.ts.map +1 -1
  31. package/dist/ConnectorStatusClient.js +9 -0
  32. package/dist/ConnectorStatusClient.js.map +1 -1
  33. package/dist/DetectorFields.d.ts +77 -1
  34. package/dist/DetectorFields.d.ts.map +1 -1
  35. package/dist/DetectorFields.js +34 -4
  36. package/dist/DetectorFields.js.map +1 -1
  37. package/dist/EdgeAgent.js +1 -1
  38. package/dist/FaceIdentity.d.ts +122 -0
  39. package/dist/FaceIdentity.d.ts.map +1 -0
  40. package/dist/FaceIdentity.js +256 -0
  41. package/dist/FaceIdentity.js.map +1 -0
  42. package/dist/FaceSightingUploader.d.ts +95 -0
  43. package/dist/FaceSightingUploader.d.ts.map +1 -0
  44. package/dist/FaceSightingUploader.js +214 -0
  45. package/dist/FaceSightingUploader.js.map +1 -0
  46. package/dist/FaceWatchLane.d.ts +297 -0
  47. package/dist/FaceWatchLane.d.ts.map +1 -0
  48. package/dist/FaceWatchLane.js +660 -0
  49. package/dist/FaceWatchLane.js.map +1 -0
  50. package/dist/FaceWatchPolicy.d.ts +154 -0
  51. package/dist/FaceWatchPolicy.d.ts.map +1 -0
  52. package/dist/FaceWatchPolicy.js +101 -0
  53. package/dist/FaceWatchPolicy.js.map +1 -0
  54. package/dist/LocalDetectorClient.d.ts +30 -0
  55. package/dist/LocalDetectorClient.d.ts.map +1 -1
  56. package/dist/LocalDetectorClient.js +72 -4
  57. package/dist/LocalDetectorClient.js.map +1 -1
  58. package/dist/MainStreamRing.d.ts +63 -1
  59. package/dist/MainStreamRing.d.ts.map +1 -1
  60. package/dist/MainStreamRing.js +101 -7
  61. package/dist/MainStreamRing.js.map +1 -1
  62. package/dist/NativeReadiness.d.ts +21 -2
  63. package/dist/NativeReadiness.d.ts.map +1 -1
  64. package/dist/NativeReadiness.js +32 -9
  65. package/dist/NativeReadiness.js.map +1 -1
  66. package/dist/OrderedReviewClient.d.ts +3 -0
  67. package/dist/OrderedReviewClient.d.ts.map +1 -1
  68. package/dist/OrderedReviewClient.js +3 -0
  69. package/dist/OrderedReviewClient.js.map +1 -1
  70. package/dist/ReviewPriority.d.ts +9 -0
  71. package/dist/ReviewPriority.d.ts.map +1 -1
  72. package/dist/ReviewPriority.js +6 -1
  73. package/dist/ReviewPriority.js.map +1 -1
  74. package/dist/ScopedVlmReview.d.ts +11 -2
  75. package/dist/ScopedVlmReview.d.ts.map +1 -1
  76. package/dist/ScopedVlmReview.js +43 -16
  77. package/dist/ScopedVlmReview.js.map +1 -1
  78. package/dist/SensingProfileClient.d.ts +14 -0
  79. package/dist/SensingProfileClient.d.ts.map +1 -1
  80. package/dist/SensingProfileClient.js +9 -0
  81. package/dist/SensingProfileClient.js.map +1 -1
  82. package/dist/TemporalReviewClient.d.ts +3 -0
  83. package/dist/TemporalReviewClient.d.ts.map +1 -1
  84. package/dist/TemporalReviewClient.js +3 -0
  85. package/dist/TemporalReviewClient.js.map +1 -1
  86. package/dist/UniformSceneStore.d.ts +6 -0
  87. package/dist/UniformSceneStore.d.ts.map +1 -1
  88. package/dist/UniformSceneStore.js +22 -1
  89. package/dist/UniformSceneStore.js.map +1 -1
  90. package/dist/VideoSegmentUploader.d.ts +11 -0
  91. package/dist/VideoSegmentUploader.d.ts.map +1 -1
  92. package/dist/VideoSegmentUploader.js +28 -2
  93. package/dist/VideoSegmentUploader.js.map +1 -1
  94. package/dist/VlmCandidateBuffer.d.ts +2 -0
  95. package/dist/VlmCandidateBuffer.d.ts.map +1 -1
  96. package/dist/VlmCandidateBuffer.js.map +1 -1
  97. package/dist/WatchlistClient.d.ts +55 -0
  98. package/dist/WatchlistClient.d.ts.map +1 -0
  99. package/dist/WatchlistClient.js +161 -0
  100. package/dist/WatchlistClient.js.map +1 -0
  101. package/dist/autoSense.d.ts.map +1 -1
  102. package/dist/autoSense.js +6 -0
  103. package/dist/autoSense.js.map +1 -1
  104. package/dist/cli.d.ts.map +1 -1
  105. package/dist/cli.js +6 -0
  106. package/dist/cli.js.map +1 -1
  107. package/dist/connectorUpdate.d.ts +34 -9
  108. package/dist/connectorUpdate.d.ts.map +1 -1
  109. package/dist/connectorUpdate.js +96 -9
  110. package/dist/connectorUpdate.js.map +1 -1
  111. package/dist/connectorUpdateApply.d.ts +20 -1
  112. package/dist/connectorUpdateApply.d.ts.map +1 -1
  113. package/dist/connectorUpdateApply.js +55 -1
  114. package/dist/connectorUpdateApply.js.map +1 -1
  115. package/dist/connectorUpdateApplyMac.d.ts +3 -1
  116. package/dist/connectorUpdateApplyMac.d.ts.map +1 -1
  117. package/dist/connectorUpdateApplyMac.js +6 -0
  118. package/dist/connectorUpdateApplyMac.js.map +1 -1
  119. package/dist/detectorEndpointState.d.ts +30 -0
  120. package/dist/detectorEndpointState.d.ts.map +1 -0
  121. package/dist/detectorEndpointState.js +125 -0
  122. package/dist/detectorEndpointState.js.map +1 -0
  123. package/dist/detectorLauncher.d.ts +23 -0
  124. package/dist/detectorLauncher.d.ts.map +1 -0
  125. package/dist/detectorLauncher.js +94 -0
  126. package/dist/detectorLauncher.js.map +1 -0
  127. package/dist/diagnose.d.ts +1 -1
  128. package/dist/diagnose.js +1 -1
  129. package/dist/hostCpu.d.ts +56 -0
  130. package/dist/hostCpu.d.ts.map +1 -0
  131. package/dist/hostCpu.js +113 -0
  132. package/dist/hostCpu.js.map +1 -0
  133. package/dist/localSensing.d.ts +86 -0
  134. package/dist/localSensing.d.ts.map +1 -1
  135. package/dist/localSensing.js +263 -30
  136. package/dist/localSensing.js.map +1 -1
  137. package/package.json +2 -2
  138. package/retained/ProfileAuthorityClient.cjs +4 -0
  139. package/retained/RetainedCaptureClient.cjs +6 -0
  140. package/retained/WebsiteAdmissionCaptureClient.cjs +3 -1
  141. package/retained/__tests__/website-admission-realtime.test.cjs +27 -2
@@ -13,6 +13,7 @@ property of `/health` — both of which are about `DetectorLoader` specifically.
13
13
 
14
14
  from __future__ import annotations
15
15
 
16
+ import importlib.util
16
17
  import unittest
17
18
 
18
19
  try:
@@ -42,6 +43,30 @@ if HAVE_RUNTIME:
42
43
  )
43
44
 
44
45
 
46
+ # The loader imports on a runner without Pillow or NumPy (guards-fast's detector step); the
47
+ # warm-up frame needs both, so this case skips there on its own, with its reason.
48
+ @unittest.skipUnless(HAVE_RUNTIME and importlib.util.find_spec("numpy") and importlib.util.find_spec("PIL"),
49
+ "onnxruntime/numpy/Pillow are not installed")
50
+ class TestFullSizeWarmup(unittest.TestCase):
51
+ def test_the_warmup_frame_is_a_textured_live_sized_jpeg_at_the_probes_threshold(self):
52
+ import io
53
+ from PIL import Image
54
+ calls = []
55
+
56
+ class Session:
57
+ def detect(self, jpeg, min_confidence, wanted):
58
+ calls.append((jpeg, min_confidence, wanted))
59
+
60
+ DetectorLoader._warm_full_size(Session())
61
+ (jpeg, min_confidence, wanted), = calls
62
+ image = Image.open(io.BytesIO(jpeg))
63
+ self.assertEqual((image.format, image.size), ("JPEG", (640, 480)))
64
+ self.assertEqual((min_confidence, wanted), (0.99, None))
65
+ # Texture, not a flat fill: a flat frame decodes in no time and would warm
66
+ # the forward pass but not the decode a live frame costs.
67
+ self.assertGreater(len(jpeg), 20_000)
68
+
69
+
45
70
  @unittest.skipUnless(HAVE_RUNTIME, "onnxruntime/numpy/Pillow are not installed")
46
71
  class TestBuildCatalogue(unittest.TestCase):
47
72
  def test_non_box_paths_do_not_inherit_the_detector_only_override(self):
@@ -131,7 +156,9 @@ class TestHealthIsOneRead(unittest.TestCase):
131
156
  self.assertEqual(len(calls), 1, "`/health` read the registry twice; it must describe one moment")
132
157
  # And it is still the payload every existing reader parses.
133
158
  self.assertEqual(payload["model"], "rfdetr_medium")
134
- self.assertEqual(sorted(payload["models"]), ["person_reid_osnet_0277", "ppe", "rfdetr_medium", "rtmpose_m_body7", "siglip2_l16_vehicle_end_v1"])
159
+ self.assertEqual(sorted(payload["models"]), ["edgeface_xs_gamma_06", "person_reid_osnet_0277", "ppe",
160
+ "rfdetr_medium", "rtmpose_m_body7", "siglip2_l16_vehicle_end_v1",
161
+ "yunet_2023mar"])
135
162
  self.assertEqual(payload["resident_models"], [])
136
163
 
137
164
  def test_before_the_first_stat_the_answer_is_loading_and_names_no_reason(self):
@@ -162,6 +162,7 @@ class RegistryCase(unittest.TestCase):
162
162
  vocabularies,
163
163
  pinned: str,
164
164
  budget_mb: "int | None" = 1536,
165
+ warm=None,
165
166
  ) -> ModelRegistry:
166
167
  """Every case INJECTS a ceiling, because the service owns none.
167
168
 
@@ -179,9 +180,61 @@ class RegistryCase(unittest.TestCase):
179
180
  ram_budget_bytes=None if budget_mb is None else budget_mb * MB,
180
181
  log=self.logs.append,
181
182
  clock=lambda: self.now,
183
+ warm=warm,
182
184
  )
183
185
 
184
186
 
187
+ class TestThePinnedModelWarmsAtFullSize(RegistryCase):
188
+ """The probe proves a forward pass on a 64x48 frame; the pinned model then warms
189
+ once more at full size before READY, so the first live look is not the slow one
190
+ (Texas, 0.2.246 swap: first live call 3.2 s, the next 18 averaged 1.4 s)."""
191
+
192
+ def test_the_pinned_model_warms_once_before_ready_and_a_specialist_never_does(self):
193
+ self.write_artifact("general")
194
+ self.write_artifact("ppe")
195
+ warmed = []
196
+ reg = None
197
+ def warm(session):
198
+ warmed.append((session.model_name, reg.snapshot()["models"][session.model_name]["state"]))
199
+ reg = self.registry([GENERAL, PPE], pinned="general", warm=warm)
200
+ reg.refresh()
201
+ self.assertEqual(warmed, [("general", "loading")])
202
+ self.assertEqual(reg.snapshot()["models"]["general"]["state"], STATE_READY)
203
+ self.assertTrue(any("full-size warm-up" in line and "general" in line for line in self.logs))
204
+ reg.detect(b"jpeg", 0.25, reg.resolve_classes(["glove"]))
205
+ self.assertEqual(self.probed, ["general", "ppe"])
206
+ self.assertEqual(warmed, [("general", "loading")])
207
+
208
+ def test_a_failed_full_size_warmup_leaves_the_model_ready_on_its_probe(self):
209
+ self.write_artifact("general")
210
+ def warm(session):
211
+ raise RuntimeError("no full-size frame today")
212
+ reg = self.registry([GENERAL], pinned="general", warm=warm)
213
+ reg.refresh()
214
+ row = reg.snapshot()["models"]["general"]
215
+ self.assertEqual(row["state"], STATE_READY)
216
+ self.assertIn("warmup_inference_ms", row)
217
+ self.assertTrue(any("full-size warm-up failed; ready on its probe: no full-size frame today" in line
218
+ for line in self.logs))
219
+ self.assertEqual(reg.detect(b"jpeg", 0.25, reg.resolve_classes(["person"])).models, ["general"])
220
+
221
+ def test_the_warmup_is_not_timed_into_the_probes_inference_time(self):
222
+ import time
223
+ self.write_artifact("general")
224
+ reg = self.registry([GENERAL], pinned="general", warm=lambda session: time.sleep(0.25))
225
+ reg.refresh()
226
+ self.assertLess(reg.snapshot()["models"]["general"]["warmup_inference_ms"], 200)
227
+
228
+ def test_a_reload_of_a_new_pinned_artifact_warms_again(self):
229
+ self.write_artifact("general")
230
+ warmed = []
231
+ reg = self.registry([GENERAL], pinned="general", warm=lambda session: warmed.append(session.model_name))
232
+ reg.refresh()
233
+ self.write_artifact("general", b"onnx-v2-different-size")
234
+ reg.refresh()
235
+ self.assertEqual(warmed, ["general", "general"])
236
+
237
+
185
238
  class TestTwoModelsAnswerOneRequest(RegistryCase):
186
239
  """§3: "A request spanning two models runs both and merges." """
187
240
 
@@ -13,5 +13,5 @@ human sets.
13
13
  """
14
14
 
15
15
  BASE_VERSION = "0.2.0"
16
- SOURCE_FINGERPRINT = "740c359ba4b7"
17
- VERSION = "0.2.0+src.740c359ba4b7"
16
+ SOURCE_FINGERPRINT = "18ad6e3937e7"
17
+ VERSION = "0.2.0+src.18ad6e3937e7"
@@ -4,6 +4,16 @@ SQLite commits frame inputs and identities together. In-memory Kalman state is a
4
4
  cache: restart recovery replays the bounded detection journal through the same
5
5
  tracker, restoring external actor IDs from exact saved detection associations.
6
6
  Predicted boxes are never returned as new visual observations.
7
+
8
+ A frame's per-track records (track_records.py) are read inside the same lock and
9
+ transaction that commits the frame, and kept beside it (actor_frame_records), so
10
+ `track_and_records` answers a frame with ITS records: never the records of a
11
+ request on the same camera that committed in between, and on a replay the ones
12
+ the frame had. A face vector is never kept there (a face never outlives its
13
+ track): the row keeps the face slot's digest, and a replay serves the face only
14
+ while the actor's live record still holds that same slot. A side table, not a
15
+ column on actor_frame: an older build inserts frames by position and must still
16
+ be able to write this journal after a rollback.
7
17
  """
8
18
  from __future__ import annotations
9
19
 
@@ -105,6 +115,9 @@ class ActorTrackerJournal:
105
115
  CREATE TABLE IF NOT EXISTS actor_motion_frame (
106
116
  camera_id TEXT NOT NULL, frame_id TEXT NOT NULL, result_json TEXT NOT NULL,
107
117
  PRIMARY KEY(camera_id,frame_id));
118
+ CREATE TABLE IF NOT EXISTS actor_frame_records (
119
+ camera_id TEXT NOT NULL, frame_id TEXT NOT NULL, fingerprint TEXT NOT NULL,
120
+ records_json TEXT NOT NULL, PRIMARY KEY(camera_id,frame_id));
108
121
  CREATE TABLE IF NOT EXISTS actor_identity_checkpoint (
109
122
  camera_id TEXT PRIMARY KEY, seq INTEGER NOT NULL, payload_json TEXT NOT NULL);
110
123
  CREATE TABLE IF NOT EXISTS actor_checkpoint (
@@ -146,15 +159,63 @@ class ActorTrackerJournal:
146
159
  "actor_track_record WHERE camera_id=? ORDER BY last_seen_ms DESC, actor_id LIMIT ?)",
147
160
  (camera, camera, MAX_RECORDS_PER_CAMERA))
148
161
 
149
- def track_records(self, camera_id):
150
- """The camera's live actors' records, oldest first (list of TrackRecord)."""
151
- with self._lock:
152
- rows = self._db.execute(
153
- "SELECT actor_id,kind,epoch,first_seen_ms,last_seen_ms,frames,box_json,confidence,face_json "
154
- "FROM actor_track_record WHERE camera_id=? ORDER BY first_seen_ms, actor_id", (camera_id,)).fetchall()
162
+ def _read_track_records(self, camera_id):
163
+ """The camera's live actors' records, oldest first. The caller holds the lock."""
164
+ rows = self._db.execute(
165
+ "SELECT actor_id,kind,epoch,first_seen_ms,last_seen_ms,frames,box_json,confidence,face_json "
166
+ "FROM actor_track_record WHERE camera_id=? ORDER BY first_seen_ms, actor_id", (camera_id,)).fetchall()
155
167
  return [TrackRecord(r[0], r[1], r[2], r[3], r[4], r[5], tuple(json.loads(r[6])), r[7],
156
168
  None if r[8] is None else FaceSlot.from_wire(json.loads(r[8]))) for r in rows]
157
169
 
170
+ def track_records(self, camera_id):
171
+ """The camera's live actors' records, oldest first (list of TrackRecord).
172
+
173
+ The LIVE state: a /detect response serves the records of its own frame from
174
+ track_and_records instead, which another frame cannot move.
175
+ """
176
+ with self._lock:
177
+ return self._read_track_records(camera_id)
178
+
179
+ @staticmethod
180
+ def _stored_records(records):
181
+ """A frame's records as its journal row keeps them: the wire form, each face replaced
182
+ by its slot's digest. The vector stays only on the live record, which ends with its
183
+ actor (track_records.py)."""
184
+ stored = []
185
+ for record in records:
186
+ wire = record.to_wire()
187
+ if record.face is not None:
188
+ wire["face"] = {"sha256": _hash(record.face.to_wire())}
189
+ stored.append(wire)
190
+ return _json(stored)
191
+
192
+ def _replayed_records(self, camera, frame_id, fingerprint):
193
+ """A replayed frame's own records, as they were kept beside it. Each face comes back
194
+ only while the actor's live record still holds that same slot. A frame with none kept
195
+ (journalled before the snapshot existed, or by an older build) has none to serve: [].
196
+ The caller holds the lock."""
197
+ row = self._db.execute(
198
+ "SELECT fingerprint,records_json FROM actor_frame_records WHERE camera_id=? AND frame_id=?",
199
+ (camera, frame_id)).fetchone()
200
+ if row is None or row[0] != fingerprint:
201
+ return []
202
+ try:
203
+ stored = json.loads(row[1])
204
+ if not isinstance(stored, list) or any(not isinstance(r, dict) for r in stored):
205
+ raise ValueError("records snapshot is not a list of records")
206
+ live = {r.actor_id: r.face for r in self._read_track_records(camera)}
207
+ replayed = []
208
+ for record in stored:
209
+ saved, face = record.get("face"), None
210
+ if saved is not None:
211
+ current = live.get(record.get("actorId"))
212
+ if current is not None and saved.get("sha256") == _hash(current.to_wire()):
213
+ face = current.to_wire()
214
+ replayed.append({**record, "face": face})
215
+ return replayed
216
+ except (ValueError, TypeError, AttributeError) as error:
217
+ raise TrackingError("Corrupt actor frame journal") from error
218
+
158
219
  def record_face(self, camera_id, actor_id, face):
159
220
  """Store one FaceSlot on a LIVE actor's record; False when the actor is not live.
160
221
 
@@ -359,6 +420,21 @@ class ActorTrackerJournal:
359
420
 
360
421
  def track(self, request, detections, *, image_sha256, model_hashes, frame_size,
361
422
  appearance=None):
423
+ """The signed tracking result for one frame (track_and_records without its records)."""
424
+ return self.track_and_records(request, detections, image_sha256=image_sha256,
425
+ model_hashes=model_hashes, frame_size=frame_size,
426
+ appearance=appearance)[0]
427
+
428
+ def track_and_records(self, request, detections, *, image_sha256, model_hashes, frame_size,
429
+ appearance=None):
430
+ """Track one frame: (signed tracking result, the camera's per-track records AS OF
431
+ THIS FRAME in wire form, oldest first).
432
+
433
+ Both are read inside one lock and one transaction, so a request on the same camera
434
+ that commits in between cannot hand this frame its records, and a replayed frame
435
+ (the same frame again, e.g. a retry after a lost response) gets the records it had
436
+ then, from its journal row, not the camera's live ones.
437
+ """
362
438
  request = validate_tracking_request(request)
363
439
  if (not isinstance(image_sha256, str) or not _SHA.fullmatch(image_sha256)
364
440
  or not isinstance(model_hashes, dict) or not 1 <= len(model_hashes) <= 16
@@ -410,8 +486,9 @@ class ActorTrackerJournal:
410
486
  if own[0] != fingerprint:
411
487
  raise TrackingError("Retained actor frame identity changed")
412
488
  _, saved = self._checked_frame(camera, request["frameId"], own[3], own[0], own[1], own[2])
489
+ records = self._replayed_records(camera, request["frameId"], own[0])
413
490
  self._db.execute("COMMIT")
414
- return saved
491
+ return saved, records
415
492
  head = self._db.execute(
416
493
  "SELECT context_hash,epoch,captured_at,seq,active_json FROM actor_camera WHERE camera_id=?",
417
494
  (camera,)).fetchone()
@@ -517,13 +594,19 @@ class ActorTrackerJournal:
517
594
  (camera, request["frameId"], motion_json))
518
595
  self._db.execute("INSERT INTO actor_frame VALUES (?,?,?,?,?,?,?)",
519
596
  (camera, request["frameId"], seq, epoch, fingerprint, _json(inputs), _json(result)))
597
+ # This frame's records, read in its own transaction and kept beside it.
598
+ self._write_track_records(camera, result, values, captured_at)
599
+ records = self._read_track_records(camera)
600
+ self._db.execute(
601
+ "INSERT INTO actor_frame_records VALUES (?,?,?,?) ON CONFLICT(camera_id,frame_id) DO UPDATE SET "
602
+ "fingerprint=excluded.fingerprint,records_json=excluded.records_json",
603
+ (camera, request["frameId"], fingerprint, self._stored_records(records)))
520
604
  self._db.execute(
521
605
  "INSERT INTO actor_camera VALUES (?,?,?,?,?,?) ON CONFLICT(camera_id) DO UPDATE SET "
522
606
  "context_hash=excluded.context_hash,epoch=excluded.epoch,captured_at=excluded.captured_at,"
523
607
  "seq=excluded.seq,active_json=excluded.active_json",
524
608
  (camera, context, epoch, captured_at, seq, _json(active)))
525
609
  self._save_checkpoint(camera,context,epoch,captured_at,seq,fingerprint,tracker,identities)
526
- self._write_track_records(camera, result, values, captured_at)
527
610
  if appearance is not None:
528
611
  self._save_identity_checkpoint(
529
612
  camera, request, context, epoch, seq, fingerprint,
@@ -535,13 +618,16 @@ class ActorTrackerJournal:
535
618
  self._db.execute(
536
619
  "DELETE FROM actor_motion_frame WHERE camera_id=? AND frame_id NOT IN "
537
620
  "(SELECT frame_id FROM actor_frame WHERE camera_id=?)", (camera,camera))
621
+ self._db.execute(
622
+ "DELETE FROM actor_frame_records WHERE camera_id=? AND frame_id NOT IN "
623
+ "(SELECT frame_id FROM actor_frame WHERE camera_id=?)", (camera,camera))
538
624
  self._db.execute("COMMIT")
539
625
  self._cache[camera] = (seq, tracker, identities)
540
626
  if identity is not None:
541
627
  self._identity_cache[camera] = (seq, identity)
542
628
  else:
543
629
  self._identity_cache.pop(camera, None)
544
- return json.loads(_json(result))
630
+ return json.loads(_json(result)), [record.to_wire() for record in records]
545
631
  except BaseException:
546
632
  if self._db.in_transaction:
547
633
  self._db.execute("ROLLBACK")
@@ -0,0 +1,166 @@
1
+ """The `/v1/faces` contract: the two pinned artifacts, the request and the answer.
2
+
3
+ STDLIB ONLY, like `server.py` and `registry.py`: the wire refusals and the check
4
+ of every answer before it leaves run on a runner with no numpy, no OpenCV and no
5
+ onnxruntime. The arithmetic lives in `face_identity.py`, which only the loader
6
+ imports.
7
+
8
+ The consumer is `LocalDetectorClient.faces` (edge-sdk), which re-checks the same
9
+ things on its side; this module is the service's half of that bargain — a
10
+ malformed answer becomes a 500 here, never a plausible 200.
11
+ """
12
+
13
+ from __future__ import annotations
14
+
15
+ import base64
16
+ import binascii
17
+ import hashlib
18
+ import math
19
+ import re
20
+
21
+ FACE_DETECT_TASK = "face_detection_landmarks5"
22
+ FACE_EMBED_TASK = "face_identity_embedding"
23
+
24
+ FACE_DETECTOR_MODEL_ID = "yunet_2023mar"
25
+ #: opencv_zoo `face_detection_yunet_2023mar.onnx` (MIT; sha256 8f2383e4...2fa4) with its input
26
+ #: height/width and its outputs' anchor counts declared symbolic, nodes and weights untouched.
27
+ FACE_DETECTOR_SHA256 = "2a62f235515c3a3f80c8c96ee75f5146d3518d5a63d2b3c77d9d1ae50e632f20"
28
+ FACE_DETECT_PREPROCESSING = "yunet_bgr_raw_long1920_pad32_floor0.5_nms0.3_top5000_v1"
29
+
30
+ FACE_EMBEDDER_MODEL_ID = "edgeface_xs_gamma_06"
31
+ #: EdgeFace-XS-gamma-0.6 exported to ONNX from the upstream checkpoint; cosine 1.000000000 against
32
+ #: the tablet's bundled TFLite (trillboard-ctv agent-core assets, sha256 7f912022...a254).
33
+ FACE_EMBEDDER_SHA256 = "1028899e16ca6c756dd1b333f0f2c136c45a049fc2ef098a2b9645e128d38f63"
34
+ FACE_EMBED_PREPROCESSING = "arcface5_umeyama_112_bilinear_rgb_unit127.5_l2_v1"
35
+
36
+ EMBEDDING_DIM = 512
37
+ MAX_FACES = 16
38
+ GATES = ("passed", "score", "eye_distance", "yaw", "sharpness")
39
+ REQUEST_KEYS = frozenset({"image_b64", "detector", "embedder", "minScore", "minEyePx", "maxAbsYaw",
40
+ "minSharpness", "maxFaces"})
41
+ _SHA = re.compile(r"[0-9a-f]{64}\Z")
42
+
43
+
44
+ def _number(value):
45
+ return not isinstance(value, bool) and isinstance(value, (int, float)) and math.isfinite(value)
46
+
47
+
48
+ def _pin(value, model_id, label):
49
+ if (not isinstance(value, dict) or set(value) != {"modelId", "sha256"}
50
+ or not isinstance(value["modelId"], str) or not isinstance(value["sha256"], str)
51
+ or not _SHA.fullmatch(value["sha256"])):
52
+ raise ValueError(f"faces {label} must name one modelId and a lowercase sha256")
53
+ if value["modelId"] != model_id:
54
+ raise ValueError(f"faces {label} must be {model_id}")
55
+ return {"modelId": value["modelId"], "sha256": value["sha256"]}
56
+
57
+
58
+ def validate_faces_request(body):
59
+ """Body -> (jpeg bytes, detector pin, embedder pin, bars). ValueError on anything else.
60
+
61
+ A pin naming the right model with another sha256 is NOT refused here: whether
62
+ those bytes are loaded is the registry's question, and it answers 409.
63
+ """
64
+ if not isinstance(body, dict) or set(body) != REQUEST_KEYS:
65
+ raise ValueError("faces request needs exactly " + ", ".join(sorted(REQUEST_KEYS)))
66
+ image = body["image_b64"]
67
+ if not isinstance(image, str) or not image:
68
+ raise ValueError("`image_b64` must be a non-empty base64 string")
69
+ try:
70
+ jpeg = base64.b64decode(image, validate=True)
71
+ except (binascii.Error, ValueError):
72
+ raise ValueError("`image_b64` is not valid base64") from None
73
+ if not jpeg:
74
+ raise ValueError("`image_b64` decoded to zero bytes")
75
+ detector = _pin(body["detector"], FACE_DETECTOR_MODEL_ID, "detector")
76
+ embedder = _pin(body["embedder"], FACE_EMBEDDER_MODEL_ID, "embedder")
77
+ bars = {key: body[key] for key in ("minScore", "minEyePx", "maxAbsYaw", "minSharpness", "maxFaces")}
78
+ if not _number(bars["minScore"]) or not 0 < bars["minScore"] <= 1:
79
+ raise ValueError("minScore must be in (0, 1]")
80
+ if not _number(bars["minEyePx"]) or not 1 <= bars["minEyePx"] <= 512:
81
+ raise ValueError("minEyePx must be in [1, 512]")
82
+ if not _number(bars["maxAbsYaw"]) or not 0 < bars["maxAbsYaw"] <= 2:
83
+ raise ValueError("maxAbsYaw must be in (0, 2]")
84
+ if not _number(bars["minSharpness"]) or not 0 <= bars["minSharpness"] <= 100000:
85
+ raise ValueError("minSharpness must be in [0, 100000]")
86
+ if type(bars["maxFaces"]) is not int or not 1 <= bars["maxFaces"] <= MAX_FACES:
87
+ raise ValueError("maxFaces must be a whole number 1..%d" % MAX_FACES)
88
+ bars = {key: float(value) if key != "maxFaces" else value for key, value in bars.items()}
89
+ return jpeg, detector, embedder, bars
90
+
91
+
92
+ def check_faces_response(result, jpeg, detector, embedder, bars):
93
+ """Raise ValueError unless `result` is exactly the answer to this request.
94
+
95
+ Every face: a box inside the frame with extent, a score in [0,1], five finite
96
+ landmarks, finite measures, a quality in [0,1], a known gate, and a vector
97
+ (512 finite values of unit length) exactly when the gate passed. The faces
98
+ are at most maxFaces, best score first.
99
+ """
100
+ if not isinstance(result, dict) or set(result) != {"version", "imageSha256", "frameSize", "detector",
101
+ "embedder", "faces", "inferenceMs"}:
102
+ raise ValueError("faces answer has unknown or missing keys")
103
+ if result["version"] != 1 or result["imageSha256"] != hashlib.sha256(jpeg).hexdigest():
104
+ raise ValueError("faces answer is not about this image")
105
+ size = result["frameSize"]
106
+ if (not isinstance(size, dict) or set(size) != {"width", "height"}
107
+ or type(size["width"]) is not int or type(size["height"]) is not int
108
+ or not 2 <= size["width"] <= 8192 or not 2 <= size["height"] <= 8192):
109
+ raise ValueError("faces answer has no original frame size")
110
+ width, height = size["width"], size["height"]
111
+ for label, pin, preprocessing in (("detector", detector, FACE_DETECT_PREPROCESSING),
112
+ ("embedder", embedder, FACE_EMBED_PREPROCESSING)):
113
+ if result[label] != {**pin, "preprocessing": preprocessing}:
114
+ raise ValueError(f"faces answer's {label} is not the selected artifact")
115
+ timing = result["inferenceMs"]
116
+ if (not isinstance(timing, dict) or set(timing) != {"detect", "embed"}
117
+ or any(not _number(timing[key]) or timing[key] < 0 for key in timing)):
118
+ raise ValueError("faces answer has no timing")
119
+ faces = result["faces"]
120
+ if not isinstance(faces, list) or len(faces) > bars["maxFaces"]:
121
+ raise ValueError("faces answer has more faces than were asked for")
122
+ previous = None
123
+ for index, face in enumerate(faces):
124
+ if not isinstance(face, dict) or set(face) != {"box", "score", "landmarks", "eyeDistancePx", "yaw",
125
+ "sharpness", "quality", "gate", "embedding"}:
126
+ raise ValueError(f"face {index} has unknown or missing keys")
127
+ box = face["box"]
128
+ if (not isinstance(box, dict) or set(box) != {"x", "y", "width", "height"}
129
+ or any(not _number(box[key]) for key in box)
130
+ or box["width"] <= 0 or box["height"] <= 0 or box["x"] < 0 or box["y"] < 0
131
+ or box["x"] + box["width"] > width + 0.01 or box["y"] + box["height"] > height + 0.01):
132
+ raise ValueError(f"face {index} box is outside the original frame")
133
+ if not _number(face["score"]) or not 0 <= face["score"] <= 1:
134
+ raise ValueError(f"face {index} score is outside [0, 1]")
135
+ if previous is not None and face["score"] > previous:
136
+ raise ValueError("faces are not best score first")
137
+ previous = face["score"]
138
+ points = face["landmarks"]
139
+ if (not isinstance(points, list) or len(points) != 5
140
+ or any(not isinstance(p, list) or len(p) != 2 or not all(_number(v) for v in p) for p in points)
141
+ or points[0][0] > points[1][0] or points[3][0] > points[4][0]):
142
+ raise ValueError(f"face {index} landmarks are not five points in template order")
143
+ if (not _number(face["eyeDistancePx"]) or face["eyeDistancePx"] < 0 or not _number(face["yaw"])
144
+ or not _number(face["sharpness"]) or face["sharpness"] < 0):
145
+ raise ValueError(f"face {index} measures are not finite")
146
+ if not _number(face["quality"]) or not 0 <= face["quality"] <= 1:
147
+ raise ValueError(f"face {index} quality is outside [0, 1]")
148
+ if face["gate"] not in GATES:
149
+ raise ValueError(f"face {index} gate is unknown")
150
+ expected = ("score" if face["score"] < bars["minScore"]
151
+ else "eye_distance" if face["eyeDistancePx"] < bars["minEyePx"]
152
+ else "yaw" if abs(face["yaw"]) > bars["maxAbsYaw"]
153
+ else "sharpness" if face["sharpness"] < bars["minSharpness"] else "passed")
154
+ if face["gate"] != expected:
155
+ raise ValueError(f"face {index} gate does not follow from its measures")
156
+ vector = face["embedding"]
157
+ if face["gate"] != "passed":
158
+ if vector is not None:
159
+ raise ValueError(f"face {index} has a vector it did not earn")
160
+ continue
161
+ if (not isinstance(vector, list) or len(vector) != EMBEDDING_DIM
162
+ or not all(_number(v) for v in vector)):
163
+ raise ValueError(f"face {index} vector is not {EMBEDDING_DIM} finite values")
164
+ norm = math.sqrt(math.fsum(v * v for v in vector))
165
+ if not 0.999 <= norm <= 1.001:
166
+ raise ValueError(f"face {index} vector is not unit length")