@trillboards/edge-sdk 0.2.267 → 0.2.276

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 (115) hide show
  1. package/deploy/detector/pyproject.toml +1 -1
  2. package/deploy/detector/tests/test_loader.py +1 -1
  3. package/deploy/detector/tests/test_staff_uniform.py +342 -0
  4. package/deploy/detector/trillboards_detector/_version.py +2 -2
  5. package/deploy/detector/trillboards_detector/loader.py +6 -0
  6. package/deploy/detector/trillboards_detector/registry.py +30 -1
  7. package/deploy/detector/trillboards_detector/server.py +43 -0
  8. package/deploy/detector/trillboards_detector/staff_uniform.py +190 -0
  9. package/deploy/detector/trillboards_detector/tasks.py +9 -1
  10. package/deploy/windows/build-windows.ps1 +3 -0
  11. package/deploy/windows/ci/test-published-install.ps1 +1 -1
  12. package/deploy/windows/e2e/test-recorder-family.ps1 +1 -1
  13. package/deploy/windows/process-priority.ps1 +229 -0
  14. package/dist/ActorPerception.d.ts.map +1 -1
  15. package/dist/ActorPerception.js +3 -1
  16. package/dist/ActorPerception.js.map +1 -1
  17. package/dist/AlertClips.d.ts +120 -0
  18. package/dist/AlertClips.d.ts.map +1 -1
  19. package/dist/AlertClips.js +296 -17
  20. package/dist/AlertClips.js.map +1 -1
  21. package/dist/AudienceAnalyzeClient.d.ts +34 -0
  22. package/dist/AudienceAnalyzeClient.d.ts.map +1 -1
  23. package/dist/AudienceAnalyzeClient.js +89 -15
  24. package/dist/AudienceAnalyzeClient.js.map +1 -1
  25. package/dist/CctvLiveViewCoordinator.d.ts +23 -2
  26. package/dist/CctvLiveViewCoordinator.d.ts.map +1 -1
  27. package/dist/CctvLiveViewCoordinator.js +50 -0
  28. package/dist/CctvLiveViewCoordinator.js.map +1 -1
  29. package/dist/ConnectorStatusClient.d.ts +8 -0
  30. package/dist/ConnectorStatusClient.d.ts.map +1 -1
  31. package/dist/ConnectorStatusClient.js +3 -0
  32. package/dist/ConnectorStatusClient.js.map +1 -1
  33. package/dist/DetectorFields.d.ts +114 -7
  34. package/dist/DetectorFields.d.ts.map +1 -1
  35. package/dist/DetectorFields.js +168 -43
  36. package/dist/DetectorFields.js.map +1 -1
  37. package/dist/EdgeAgent.js +1 -1
  38. package/dist/FloorObjects.d.ts +131 -8
  39. package/dist/FloorObjects.d.ts.map +1 -1
  40. package/dist/FloorObjects.js +396 -24
  41. package/dist/FloorObjects.js.map +1 -1
  42. package/dist/LocalDetectorClient.d.ts +21 -0
  43. package/dist/LocalDetectorClient.d.ts.map +1 -1
  44. package/dist/LocalDetectorClient.js +43 -0
  45. package/dist/LocalDetectorClient.js.map +1 -1
  46. package/dist/NativeReadiness.d.ts.map +1 -1
  47. package/dist/NativeReadiness.js +8 -3
  48. package/dist/NativeReadiness.js.map +1 -1
  49. package/dist/QueueDwell.d.ts +87 -0
  50. package/dist/QueueDwell.d.ts.map +1 -0
  51. package/dist/QueueDwell.js +167 -0
  52. package/dist/QueueDwell.js.map +1 -0
  53. package/dist/ScopedVlmReview.d.ts +21 -2
  54. package/dist/ScopedVlmReview.d.ts.map +1 -1
  55. package/dist/ScopedVlmReview.js +68 -10
  56. package/dist/ScopedVlmReview.js.map +1 -1
  57. package/dist/SensingProfileClient.d.ts +7 -0
  58. package/dist/SensingProfileClient.d.ts.map +1 -1
  59. package/dist/SensingProfileClient.js +1 -0
  60. package/dist/SensingProfileClient.js.map +1 -1
  61. package/dist/VideoSegmentUploader.d.ts +6 -0
  62. package/dist/VideoSegmentUploader.d.ts.map +1 -1
  63. package/dist/VideoSegmentUploader.js +31 -4
  64. package/dist/VideoSegmentUploader.js.map +1 -1
  65. package/dist/config.d.ts +8 -0
  66. package/dist/config.d.ts.map +1 -1
  67. package/dist/config.js.map +1 -1
  68. package/dist/connectorUpdate.d.ts +6 -0
  69. package/dist/connectorUpdate.d.ts.map +1 -1
  70. package/dist/connectorUpdate.js +8 -1
  71. package/dist/connectorUpdate.js.map +1 -1
  72. package/dist/diagnose.d.ts +1 -1
  73. package/dist/diagnose.js +1 -1
  74. package/dist/enterpriseNetwork.d.ts +9 -0
  75. package/dist/enterpriseNetwork.d.ts.map +1 -1
  76. package/dist/enterpriseNetwork.js +14 -0
  77. package/dist/enterpriseNetwork.js.map +1 -1
  78. package/dist/index.d.ts +1 -1
  79. package/dist/index.d.ts.map +1 -1
  80. package/dist/localSensing.d.ts +29 -1
  81. package/dist/localSensing.d.ts.map +1 -1
  82. package/dist/localSensing.js +64 -6
  83. package/dist/localSensing.js.map +1 -1
  84. package/dist/mainThreadStall.d.ts +11 -0
  85. package/dist/mainThreadStall.d.ts.map +1 -1
  86. package/dist/mainThreadStall.js +35 -0
  87. package/dist/mainThreadStall.js.map +1 -1
  88. package/dist/pipelineSwitches.d.ts +61 -2
  89. package/dist/pipelineSwitches.d.ts.map +1 -1
  90. package/dist/pipelineSwitches.js +93 -15
  91. package/dist/pipelineSwitches.js.map +1 -1
  92. package/dist/processPriority.d.ts +75 -0
  93. package/dist/processPriority.d.ts.map +1 -0
  94. package/dist/processPriority.js +380 -0
  95. package/dist/processPriority.js.map +1 -0
  96. package/dist/sensingRunMode.d.ts.map +1 -1
  97. package/dist/sensingRunMode.js +22 -3
  98. package/dist/sensingRunMode.js.map +1 -1
  99. package/dist/vehicleEndWarmup.d.ts +60 -0
  100. package/dist/vehicleEndWarmup.d.ts.map +1 -0
  101. package/dist/vehicleEndWarmup.js +209 -0
  102. package/dist/vehicleEndWarmup.js.map +1 -0
  103. package/dist/windowsLiveView.d.ts +3 -1
  104. package/dist/windowsLiveView.d.ts.map +1 -1
  105. package/dist/windowsLiveView.js +2 -0
  106. package/dist/windowsLiveView.js.map +1 -1
  107. package/package.json +4 -4
  108. package/retained/RetainedCaptureClient.cjs +30 -4
  109. package/retained/__tests__/archive-lifecycle.test.cjs +44 -0
  110. package/retained/__tests__/held-frames.test.cjs +94 -0
  111. package/retained/__tests__/paired-theft-manager.test.cjs +24 -0
  112. package/retained/__tests__/queue-dwell.test.cjs +238 -0
  113. package/retained/__tests__/support/theft-builder.cjs +3 -1
  114. package/retained/held-frames.cjs +39 -0
  115. package/retained/manager.cjs +15 -0
@@ -10,7 +10,7 @@ name = "trillboards-detector"
10
10
  # constant, a `labels.py`-only change survives `uv tool install` AND
11
11
  # `uv tool install --force`; move the version and a bare `uv tool install`
12
12
  # reinstalls. See `stamp_version.py`.
13
- version = "0.2.0+src.18ad6e3937e7"
13
+ version = "0.2.0+src.9917da6b7527"
14
14
  description = "On-device object-detector service for the Trillboards BYO-CCTV connector (loopback HTTP, ONNX Runtime)."
15
15
  requires-python = ">=3.10"
16
16
 
@@ -158,7 +158,7 @@ class TestHealthIsOneRead(unittest.TestCase):
158
158
  self.assertEqual(payload["model"], "rfdetr_medium")
159
159
  self.assertEqual(sorted(payload["models"]), ["edgeface_xs_gamma_06", "person_reid_osnet_0277", "ppe",
160
160
  "rfdetr_medium", "rtmpose_m_body7", "siglip2_l16_vehicle_end_v1",
161
- "yunet_2023mar"])
161
+ "staff_uniform_student_m2", "yunet_2023mar"])
162
162
  self.assertEqual(payload["resident_models"], [])
163
163
 
164
164
  def test_before_the_first_stat_the_answer_is_loading_and_names_no_reason(self):
@@ -0,0 +1,342 @@
1
+ """The staff-uniform task: the server's crop exactly, strict wire, pinned output and the shared admission."""
2
+ import base64
3
+ import hashlib
4
+ import importlib.util
5
+ import io
6
+ import json
7
+ import math
8
+ import tempfile
9
+ import threading
10
+ import unittest
11
+ import urllib.error
12
+ import urllib.request
13
+ from pathlib import Path
14
+
15
+ from trillboards_detector import staff_uniform
16
+ from trillboards_detector.errors import ModelContractError, ModelVerificationError
17
+ from trillboards_detector.labels import RFDETR_MEDIUM
18
+ from trillboards_detector.registry import ModelEntry, ModelRegistry
19
+ from trillboards_detector.server import DetectorServer
20
+ from trillboards_detector.tasks import BUILTIN_TASK_MODELS, STAFF_UNIFORM
21
+
22
+ # The dependency-free wire lane has no numerical packages: the crop rectangle, the
23
+ # catalogue, the admission and the wire refusals run there; the pixel path runs in
24
+ # the CPU SDK lane, which installs detector[cpu] at the service's pinned Pillow and numpy.
25
+ _missing = [name for name in ("numpy", "PIL") if importlib.util.find_spec(name) is None]
26
+ needs_numeric = unittest.skipIf(_missing, "detector[cpu] dependencies absent: " + ", ".join(_missing))
27
+ # Refused or rejected before any decode, so these bytes are never read as an image.
28
+ UNREAD_JPEG = b"\xff\xd8\xff\xe0 refused before decode"
29
+ PIN = {"modelId": staff_uniform.MODEL_ID, "sha256": staff_uniform.MODEL_SHA256}
30
+
31
+ # The server's preparation of this frame: services/trillboards-siglip-embedder person.student_bounds and
32
+ # person.student_input (the code that made the student's training and eval crops), run at Pillow 12.3.0 and
33
+ # numpy 2.5.2 on jpeg_bytes() below. The box must hand the model the same float32 bytes, or the student it runs
34
+ # is not the student that was graded (across the 3,093 eval crops the two agree to 5e-6 in P(staff)).
35
+ FRAME_SHA256 = "e652a0c328758b71e72502c752ee6ccc8643574e58cae586b60fbe53bca74f14"
36
+ SERVER_CASES = (
37
+ ([95.6, 90.25, 70.5, 180.75], (88, 72, 173, 289),
38
+ "9ccc8f789304087afb0d5dbdccf9538e4f89f430e2e4358f16684e176652addf"),
39
+ ([398.2, 110.9, 75.3, 195.1], (390, 91, 481, 325),
40
+ "d73db34db5b30015740ffaf7a9bec7150475e66fd8791e26efde5b6cbcb9b336"),
41
+ ([600.4, 300.7, 39.6, 179.3], (596, 282, 640, 480),
42
+ "87305a24d1573281407740f170482108ff12f0a58d8fd4b5c79ea839d9eaff0c"),
43
+ ([10.0, 405.5, 160.0, 60.0], (0, 399, 186, 471),
44
+ "79d5c08af0972d38b3737fa793c3ac7152196787419941349661521040b0f6ca"),
45
+ )
46
+
47
+
48
+ def jpeg_bytes(width=640, height=480):
49
+ from PIL import Image
50
+
51
+ image = Image.new("RGB", (width, height), (90, 90, 90))
52
+ image.paste((200, 30, 30), (100, 100, 160, 260))
53
+ image.paste((30, 60, 200), (400, 120, 470, 300))
54
+ for x in range(0, width, 16):
55
+ image.paste((40 + x % 200, 120, 80), (x, 400, x + 8, 480))
56
+ buffer = io.BytesIO()
57
+ image.save(buffer, "JPEG", quality=90)
58
+ return buffer.getvalue()
59
+
60
+
61
+ class FakeSession:
62
+ """P(staff) from the mean of the normalized crop, so the crop and preparation are observable."""
63
+
64
+ def __init__(self, output=None):
65
+ self.inputs = []
66
+ self.output = output
67
+
68
+ def run(self, names, feeds):
69
+ import numpy as np
70
+
71
+ assert names == ["prob"] and set(feeds) == {"crop"}
72
+ crop = feeds["crop"]
73
+ assert crop.shape == (1, 3, 192, 96) and crop.dtype == np.float32
74
+ self.inputs.append(crop)
75
+ if self.output is not None:
76
+ return [np.asarray(self.output, dtype=np.float32)]
77
+ p = 1.0 / (1.0 + math.exp(-4.0 * float(crop.mean())))
78
+ return [np.array([[1.0 - p, p]], dtype=np.float32)]
79
+
80
+
81
+ def fake_estimator(output=None):
82
+ estimator = object.__new__(staff_uniform.StaffUniformEstimator)
83
+ estimator._session = FakeSession(output)
84
+ estimator._lock = threading.Lock()
85
+ return estimator
86
+
87
+
88
+ class StudentBoundsTest(unittest.TestCase):
89
+ def test_the_rectangle_is_the_servers(self):
90
+ for box, bounds, _sha in SERVER_CASES:
91
+ with self.subTest(box=box):
92
+ self.assertEqual(staff_uniform.student_bounds(box, 640, 480), bounds)
93
+
94
+ def test_every_edge_is_floored_after_the_margin(self):
95
+ # 10% of 100 x 200 is 10 x 20 each side: (40.5 - 10, 50.5 - 20, 140.5 + 10, 250.5 + 20) floored.
96
+ self.assertEqual(staff_uniform.student_bounds([40.5, 50.5, 100, 200], 640, 480), (30, 30, 150, 270))
97
+ self.assertEqual(staff_uniform.student_bounds([2, 3, 50, 60], 640, 480), (0, 0, 57, 69))
98
+
99
+
100
+ class WireTest(unittest.TestCase):
101
+ def body(self, **over):
102
+ return {"image_b64": "AA==", "model": dict(PIN), "boxes": [[1, 2, 30, 60]], **over}
103
+
104
+ def test_accepts_the_pinned_model_and_boxes(self):
105
+ model, boxes = staff_uniform.validate_staff_uniform_request(self.body())
106
+ self.assertEqual(model, PIN)
107
+ self.assertEqual(boxes, [[1, 2, 30, 60]])
108
+
109
+ def test_refuses_anything_else(self):
110
+ for body in ({**self.body(), "extra": 1}, self.body(model={"modelId": staff_uniform.MODEL_ID}),
111
+ self.body(boxes=[]), self.body(boxes=[[1, 2, 30, 60]] * 33), self.body(boxes=[[1, 2, 0, 60]]),
112
+ self.body(boxes=[[-1, 2, 30, 60]]), self.body(boxes=[[1, 2, 30]]),
113
+ self.body(boxes=[[True, 2, 30, 60]]), self.body(boxes=[[1, 2, float("nan"), 60]]),
114
+ self.body(boxes=[(1, 2, 30, 60)]), [], None):
115
+ with self.subTest(body=body), self.assertRaises(ValueError):
116
+ staff_uniform.validate_staff_uniform_request(body)
117
+
118
+
119
+ @needs_numeric
120
+ class PreparationTest(unittest.TestCase):
121
+ def test_the_box_hands_the_model_the_servers_bytes(self):
122
+ from PIL import Image
123
+
124
+ jpeg = jpeg_bytes()
125
+ self.assertEqual(hashlib.sha256(jpeg).hexdigest(), FRAME_SHA256,
126
+ "the synthetic frame moved: Pillow is not the service's pinned 12.3.0")
127
+ with Image.open(io.BytesIO(jpeg)) as decoded:
128
+ decoded.load()
129
+ image = decoded.convert("RGB")
130
+ for box, bounds, sha in SERVER_CASES:
131
+ with self.subTest(box=box):
132
+ tensor, crop = staff_uniform.student_input(image, box)
133
+ self.assertEqual(crop, bounds)
134
+ self.assertEqual(tensor.shape, (1, 3, 192, 96))
135
+ self.assertEqual(hashlib.sha256(tensor.tobytes()).hexdigest(), sha)
136
+
137
+ def test_a_crop_under_two_pixels_has_no_input(self):
138
+ from PIL import Image
139
+
140
+ tensor, crop = staff_uniform.student_input(Image.new("RGB", (640, 480)), [639.5, 10, 0.4, 0.4])
141
+ self.assertIsNone(tensor)
142
+ self.assertEqual(crop, (639, 9, 639, 10))
143
+
144
+
145
+ @needs_numeric
146
+ class EstimateTest(unittest.TestCase):
147
+ def test_reads_each_person_on_the_original_frame(self):
148
+ estimator, jpeg = fake_estimator(), jpeg_bytes()
149
+ out = estimator.estimate(jpeg, [box for box, _b, _s in SERVER_CASES])
150
+ self.assertEqual(out["version"], 1)
151
+ self.assertEqual(out["modelId"], staff_uniform.MODEL_ID)
152
+ self.assertEqual(out["modelSha256"], staff_uniform.MODEL_SHA256)
153
+ self.assertEqual(out["preprocessing"], staff_uniform.PREPROCESSING)
154
+ self.assertEqual(out["imageSha256"], FRAME_SHA256)
155
+ self.assertEqual(out["frameSize"], {"width": 640, "height": 480})
156
+ self.assertEqual([p["boxIndex"] for p in out["people"]], [0, 1, 2, 3])
157
+ self.assertEqual([tuple(p["crop"]) for p in out["people"]], [b for _box, b, _s in SERVER_CASES])
158
+ self.assertEqual([p["inputSha256"] for p in out["people"]], [s for _box, _b, s in SERVER_CASES])
159
+ for person in out["people"]:
160
+ self.assertIsInstance(person["pStaff"], float)
161
+ self.assertTrue(0.0 <= person["pStaff"] <= 1.0)
162
+ # The red block is in the first crop, the blue one in the second: different crops, different reads.
163
+ self.assertNotAlmostEqual(out["people"][0]["pStaff"], out["people"][1]["pStaff"], places=3)
164
+
165
+ def test_same_inputs_same_answer(self):
166
+ estimator, jpeg = fake_estimator(), jpeg_bytes()
167
+ a = estimator.estimate(jpeg, [[95.6, 90.25, 70.5, 180.75]])
168
+ b = estimator.estimate(jpeg, [[95.6, 90.25, 70.5, 180.75]])
169
+ self.assertEqual(a["people"], b["people"])
170
+
171
+ def test_a_degenerate_crop_is_answered_without_a_read(self):
172
+ estimator, jpeg = fake_estimator(), jpeg_bytes()
173
+ out = estimator.estimate(jpeg, [[639.5, 10, 0.4, 0.4], [95.6, 90.25, 70.5, 180.75]])
174
+ self.assertEqual(out["people"][0], {"boxIndex": 0, "crop": [639, 9, 639, 10], "inputSha256": None,
175
+ "pStaff": None})
176
+ self.assertIsInstance(out["people"][1]["pStaff"], float)
177
+ self.assertEqual(len(estimator._session.inputs), 1)
178
+
179
+ def test_refuses_unbounded_or_foreign_input(self):
180
+ from PIL import Image
181
+
182
+ png = io.BytesIO()
183
+ Image.new("RGB", (64, 48)).save(png, "PNG")
184
+ estimator, jpeg = fake_estimator(), jpeg_bytes()
185
+ for args in ((jpeg, []), (jpeg, [[0, 0, 10, 10]] * 33), (jpeg, [[0, 0, 700, 10]]), (jpeg, [[5, 5, 0, 9]]),
186
+ (jpeg, [[-1, 5, 5, 9]]), (b"not a jpeg", [[0, 0, 10, 10]]), (png.getvalue(), [[0, 0, 10, 10]]),
187
+ ("text", [[0, 0, 10, 10]])):
188
+ with self.subTest(args=(args[0][:4], args[1][:2])), self.assertRaises(ValueError):
189
+ estimator.estimate(*args)
190
+
191
+ def test_an_output_that_is_not_a_two_class_probability_is_refused(self):
192
+ jpeg = jpeg_bytes()
193
+ for output in ([[0.7, 0.7]], [[float("nan"), 1.0]], [[0.2, 0.3, 0.5]], [[1.2, -0.2]]):
194
+ with self.subTest(output=output), self.assertRaises(ModelContractError):
195
+ fake_estimator(output).estimate(jpeg, [[95.6, 90.25, 70.5, 180.75]])
196
+
197
+ def test_the_artifact_must_be_the_pinned_bytes(self):
198
+ with tempfile.TemporaryDirectory() as tmp:
199
+ path = Path(tmp) / "model.onnx"
200
+ path.write_bytes(b"not the pinned export")
201
+ with self.assertRaises(ModelContractError) as caught:
202
+ staff_uniform.StaffUniformEstimator(path)
203
+ self.assertIn("pinned", str(caught.exception))
204
+
205
+
206
+ class CatalogueTest(unittest.TestCase):
207
+ def test_the_task_is_in_the_builtin_catalogue_with_its_measured_reservation(self):
208
+ self.assertIn(STAFF_UNIFORM, BUILTIN_TASK_MODELS)
209
+ self.assertEqual(STAFF_UNIFORM.model_name, staff_uniform.MODEL_ID)
210
+ self.assertEqual(STAFF_UNIFORM.task, "staff_uniform")
211
+ self.assertEqual(STAFF_UNIFORM.sha256, staff_uniform.MODEL_SHA256)
212
+ self.assertEqual(STAFF_UNIFORM.preprocessing, staff_uniform.PREPROCESSING)
213
+ self.assertEqual(STAFF_UNIFORM.approx_ram_bytes, 128 * 1024**2)
214
+
215
+ def registry(self, loads, result=None):
216
+ tmp = tempfile.TemporaryDirectory()
217
+ self.addCleanup(tmp.cleanup)
218
+ det, task = Path(tmp.name) / "det.onnx", Path(tmp.name) / "student.onnx"
219
+ det.write_bytes(b"det"); task.write_bytes(b"student")
220
+
221
+ class Session:
222
+ model_sha256 = staff_uniform.MODEL_SHA256
223
+
224
+ def estimate(self, jpeg, boxes):
225
+ return result or {"modelId": staff_uniform.MODEL_ID, "modelSha256": staff_uniform.MODEL_SHA256,
226
+ "preprocessing": staff_uniform.PREPROCESSING,
227
+ "people": [{"boxIndex": i, "pStaff": 0.1} for i, _ in enumerate(boxes)]}
228
+
229
+ def factory(entry):
230
+ loads.append(entry.name)
231
+ return Session()
232
+
233
+ reg = ModelRegistry([ModelEntry(RFDETR_MEDIUM, det, pinned=True), ModelEntry(STAFF_UNIFORM, task)],
234
+ factory, lambda session: None, 4000 * 1024**2, log=lambda line: None)
235
+ reg.refresh()
236
+ return reg
237
+
238
+ def test_admission_goes_through_the_shared_portfolio(self):
239
+ loads = []
240
+ reg = self.registry(loads)
241
+ self.assertEqual(loads, ["rfdetr_medium"])
242
+ out = reg.estimate_staff_uniform(b"jpeg", [[1, 2, 3, 4]], PIN)
243
+ self.assertEqual(len(out["people"]), 1)
244
+ self.assertEqual(loads, ["rfdetr_medium", staff_uniform.MODEL_ID])
245
+ with self.assertRaises(ModelVerificationError):
246
+ reg.estimate_staff_uniform(b"jpeg", [[1, 2, 3, 4]], {**PIN, "sha256": "b" * 64})
247
+ with self.assertRaises(ModelVerificationError):
248
+ reg.estimate_staff_uniform(b"jpeg", [[1, 2, 3, 4]], {**PIN, "modelId": "rfdetr_medium"})
249
+
250
+ def test_an_output_from_another_preparation_is_refused(self):
251
+ reg = self.registry([], result={"modelId": staff_uniform.MODEL_ID, "modelSha256": staff_uniform.MODEL_SHA256,
252
+ "preprocessing": "another-crop", "people": [{"boxIndex": 0, "pStaff": 0.1}]})
253
+ with self.assertRaises(ModelVerificationError):
254
+ reg.estimate_staff_uniform(b"jpeg", [[1, 2, 3, 4]], PIN)
255
+
256
+
257
+ class EndpointTest(unittest.TestCase):
258
+ def serve(self, loader):
259
+ server = DetectorServer(("127.0.0.1", 0), loader, log_line=lambda _m: None)
260
+ threading.Thread(target=lambda: server.serve_forever(poll_interval=0.02), daemon=True).start()
261
+ self.addCleanup(server.server_close)
262
+ self.addCleanup(server.shutdown)
263
+ host, port = server.server_address[:2]
264
+ return f"http://{host}:{port}/staff-uniform"
265
+
266
+ def post(self, url, body):
267
+ request = urllib.request.Request(url, data=json.dumps(body).encode(), method="POST")
268
+ request.add_header("Content-Type", "application/json")
269
+ try:
270
+ with urllib.request.urlopen(request, timeout=10) as res:
271
+ return res.status, json.loads(res.read().decode())
272
+ except urllib.error.HTTPError as err:
273
+ with err:
274
+ return err.code, json.loads(err.read().decode())
275
+
276
+ def loader(self, answer=None):
277
+ class Loader:
278
+ calls = []
279
+
280
+ def estimate_staff_uniform(self, jpeg, boxes, model):
281
+ self.calls.append((boxes, model))
282
+ if model != PIN:
283
+ raise ModelVerificationError("selected staff-uniform task does not match its immutable contract")
284
+ if answer is not None:
285
+ return answer(jpeg, boxes)
286
+ return fake_estimator().estimate(jpeg, boxes)
287
+
288
+ return Loader()
289
+
290
+ def body(self, **over):
291
+ return {"image_b64": base64.b64encode(UNREAD_JPEG).decode(), "model": dict(PIN),
292
+ "boxes": [[95.6, 90.25, 70.5, 180.75]], **over}
293
+
294
+ def answer(self, **person):
295
+ def make(jpeg, boxes):
296
+ return {"version": 1, "modelId": staff_uniform.MODEL_ID, "modelSha256": staff_uniform.MODEL_SHA256,
297
+ "preprocessing": staff_uniform.PREPROCESSING, "imageSha256": hashlib.sha256(jpeg).hexdigest(),
298
+ "people": [{"boxIndex": i, "pStaff": 0.25, **person} for i, _ in enumerate(boxes)]}
299
+ return make
300
+
301
+ @needs_numeric
302
+ def test_answers_each_person(self):
303
+ status, out = self.post(self.serve(self.loader()),
304
+ self.body(image_b64=base64.b64encode(jpeg_bytes()).decode(),
305
+ boxes=[[95.6, 90.25, 70.5, 180.75], [639.5, 10, 0.4, 0.4]]))
306
+ self.assertEqual(status, 200)
307
+ self.assertEqual([p["boxIndex"] for p in out["people"]], [0, 1])
308
+ self.assertIsInstance(out["people"][0]["pStaff"], float)
309
+ self.assertIsNone(out["people"][1]["pStaff"])
310
+ self.assertEqual(out["people"][0]["inputSha256"], SERVER_CASES[0][2])
311
+
312
+ def test_refuses_a_malformed_request_before_inference(self):
313
+ loader = self.loader()
314
+ url = self.serve(loader)
315
+ for body in (self.body(boxes=[]), {**self.body(), "extra": 1}, self.body(image_b64="!!"),
316
+ self.body(image_b64=""), self.body(model={"modelId": staff_uniform.MODEL_ID}),
317
+ self.body(boxes=[[1, 2, 0, 4]])):
318
+ with self.subTest(body={k: v for k, v in body.items() if k != "image_b64"}):
319
+ self.assertEqual(self.post(url, body)[0], 400)
320
+ self.assertEqual(loader.calls, [])
321
+
322
+ def test_a_different_artifact_is_a_conflict(self):
323
+ status, _ = self.post(self.serve(self.loader()),
324
+ self.body(model={"modelId": staff_uniform.MODEL_ID, "sha256": "c" * 64}))
325
+ self.assertEqual(status, 409)
326
+
327
+ def test_a_build_without_the_task_is_unavailable(self):
328
+ status, _ = self.post(self.serve(object()), self.body())
329
+ self.assertEqual(status, 503)
330
+
331
+ def test_a_malformed_answer_is_refused(self):
332
+ for answer in (self.answer(pStaff=1.5), self.answer(boxIndex=3), self.answer(pStaff="0.2"),
333
+ lambda jpeg, boxes: {**self.answer()(jpeg, boxes), "imageSha256": "0" * 64},
334
+ lambda jpeg, boxes: {**self.answer()(jpeg, boxes), "people": []}):
335
+ with self.subTest(answer=answer):
336
+ status, out = self.post(self.serve(self.loader(answer)), self.body())
337
+ self.assertEqual(status, 500)
338
+ self.assertEqual(out["error"], "refused malformed staff-uniform output")
339
+
340
+
341
+ if __name__ == "__main__":
342
+ unittest.main()
@@ -13,5 +13,5 @@ human sets.
13
13
  """
14
14
 
15
15
  BASE_VERSION = "0.2.0"
16
- SOURCE_FINGERPRINT = "18ad6e3937e7"
17
- VERSION = "0.2.0+src.18ad6e3937e7"
16
+ SOURCE_FINGERPRINT = "9917da6b7527"
17
+ VERSION = "0.2.0+src.9917da6b7527"
@@ -198,6 +198,9 @@ class DetectorLoader:
198
198
  if entry.vocabulary.task == "vehicle_end_orientation":
199
199
  from .vehicle_end import VehicleEndEstimator
200
200
  return VehicleEndEstimator(entry.path, intra_op_threads=self._intra_op_threads or 4, accelerator="cpu")
201
+ if entry.vocabulary.task == "staff_uniform":
202
+ from .staff_uniform import StaffUniformEstimator
203
+ return StaffUniformEstimator(entry.path, intra_op_threads=self._intra_op_threads or 4, accelerator="cpu")
201
204
  # The door faces task is called by the connector's face lane at up to 4 frames a second,
202
205
  # beside the look loop's object detector: ONE thread by default keeps it to one core and
203
206
  # out of RF-DETR's pool (devbox A/B under load: 4 threads ran slower than 1).
@@ -375,6 +378,9 @@ class DetectorLoader:
375
378
  def estimate_vehicle_end(self, jpeg: bytes, boxes: list, side: str, expected_model: dict) -> dict:
376
379
  return self.registry.estimate_vehicle_end(jpeg, boxes, side, expected_model)
377
380
 
381
+ def estimate_staff_uniform(self, jpeg: bytes, boxes: list, expected_model: dict) -> dict:
382
+ return self.registry.estimate_staff_uniform(jpeg, boxes, expected_model)
383
+
378
384
  def estimate_faces(self, jpeg: bytes, request: dict, detector_pin: dict, embedder_pin: dict) -> dict:
379
385
  return self.registry.estimate_faces(jpeg, request, detector_pin, embedder_pin)
380
386
 
@@ -124,7 +124,7 @@ class TaskModelContract:
124
124
 
125
125
  def _validate_task(self):
126
126
  if self.task not in {"pose_coco17", "actor_appearance", "vehicle_end_orientation",
127
- "face_detection_landmarks5", "face_identity_embedding"}:
127
+ "face_detection_landmarks5", "face_identity_embedding", "staff_uniform"}:
128
128
  raise ValueError("invalid non-box task kind")
129
129
 
130
130
  class ModelEntry:
@@ -611,6 +611,35 @@ class ModelRegistry:
611
611
  finally:
612
612
  self._release([state.entry.name])
613
613
 
614
+ def estimate_staff_uniform(self, jpeg: bytes, boxes: list, expected_model: dict) -> dict:
615
+ """Acquire the staff-uniform student through the same portfolio as every session."""
616
+ state = self._states.get(expected_model.get("modelId"))
617
+ if (state is None or not isinstance(state.entry.vocabulary, TaskModelContract)
618
+ or state.entry.vocabulary.task != "staff_uniform"
619
+ or state.entry.vocabulary.sha256 != expected_model.get("sha256")):
620
+ raise ModelVerificationError("selected staff-uniform task does not match its immutable contract")
621
+ contract = state.entry.vocabulary
622
+ sessions = self._acquire([state.entry.name])
623
+ try:
624
+ session = sessions[0]
625
+ if getattr(session, "model_sha256", None) != contract.sha256:
626
+ raise ModelVerificationError("loaded staff-uniform session does not match the selected artifact")
627
+ try:
628
+ result = session.estimate(jpeg, boxes)
629
+ if (result.get("modelId") != contract.model_name
630
+ or result.get("modelSha256") != contract.sha256
631
+ or result.get("preprocessing") != contract.preprocessing):
632
+ raise ModelVerificationError("staff-uniform output does not match its task contract")
633
+ except Exception:
634
+ with self._lock:
635
+ state.detect_failures += 1
636
+ raise
637
+ with self._lock:
638
+ state.detect_count += 1
639
+ return result
640
+ finally:
641
+ self._release([state.entry.name])
642
+
614
643
  def estimate_faces(self, jpeg: bytes, request: dict, detector_pin: dict, embedder_pin: dict) -> dict:
615
644
  """The door faces task: BOTH pinned sessions acquired together through the portfolio.
616
645
 
@@ -41,6 +41,7 @@ from .actor_action_wire import parse_actor_action_request, validate_actor_action
41
41
  from .colors import validate_color_request, prepare_color_capabilities
42
42
  from .registry import validate_preparation_models
43
43
  from .vehicle_end import validate_vehicle_end_request
44
+ from .staff_uniform import validate_staff_uniform_request
44
45
  from .face_contract import check_faces_response, validate_faces_request
45
46
  from ._version import VERSION
46
47
  from .errors import DetectorNotReady, ModelVerificationError, UnknownClasses
@@ -443,6 +444,8 @@ class DetectorHandler(BaseHTTPRequestHandler):
443
444
  self._actor_actions()
444
445
  elif self._path() == "/vehicle-end":
445
446
  self._vehicle_end()
447
+ elif self._path() == "/staff-uniform":
448
+ self._staff_uniform()
446
449
  elif self._path() == "/v1/faces":
447
450
  self._faces()
448
451
  elif self._path() == "/ready":
@@ -773,6 +776,46 @@ class DetectorHandler(BaseHTTPRequestHandler):
773
776
  self._json(500, {"error": "refused malformed vehicle-end output"}); return
774
777
  self._json(200, result)
775
778
 
779
+ def _staff_uniform(self):
780
+ """P(staff) of each named person on one original frame (staff_uniform.py)."""
781
+ try:
782
+ raw = self._read_body()
783
+ try:
784
+ body = json.loads(raw.decode("utf-8"))
785
+ model, boxes = validate_staff_uniform_request(body)
786
+ jpeg = base64.b64decode(body["image_b64"], validate=True) if isinstance(body["image_b64"], str) else b""
787
+ except (ValueError, UnicodeDecodeError, binascii.Error) as error:
788
+ raise WireError(400, str(error)) from error
789
+ if not jpeg:
790
+ raise WireError(400, "`image_b64` must be a non-empty base64 string")
791
+ except WireError as error:
792
+ self._json(error.status, {"error": error.message}); return
793
+ estimate = getattr(self.server.loader, "estimate_staff_uniform", None)
794
+ if not callable(estimate):
795
+ self._json(503, {"error": "staff-uniform task is unavailable in this detector build"}); return
796
+ try:
797
+ result = estimate(jpeg, boxes, model)
798
+ except ModelVerificationError as error:
799
+ self._json(409, {"error": str(error)}); return
800
+ except DetectorNotReady as error:
801
+ self._json(503, {"error": str(error)}); return
802
+ except ValueError as error:
803
+ self._json(400, {"error": str(error)}); return
804
+ except Exception as error:
805
+ self._log(f"[detector] staff-uniform inference failed: {type(error).__name__}: {error}")
806
+ self._json(500, {"error": "staff-uniform inference failed"}); return
807
+ people = result.get("people") if isinstance(result, dict) else None
808
+ if (not isinstance(result, dict) or result.get("modelId") != model.get("modelId")
809
+ or result.get("modelSha256") != model.get("sha256")
810
+ or not isinstance(people, list) or len(people) != len(boxes)
811
+ or any(not isinstance(p, dict) or p.get("boxIndex") != i
812
+ or not (p.get("pStaff") is None or isinstance(p.get("pStaff"), float) and 0 <= p["pStaff"] <= 1)
813
+ for i, p in enumerate(people))
814
+ or result.get("imageSha256") != hashlib.sha256(jpeg).hexdigest()):
815
+ self._log("[detector] REFUSING malformed staff-uniform output")
816
+ self._json(500, {"error": "refused malformed staff-uniform output"}); return
817
+ self._json(200, result)
818
+
776
819
  def _faces(self):
777
820
  """Door faces (face_contract.py): YuNet + ArcFace alignment + EdgeFace-XS on one original JPEG.
778
821