@trillboards/edge-sdk 0.2.194 → 0.2.195

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 (33) 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_vehicle_end.py +215 -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 +39 -0
  8. package/deploy/detector/trillboards_detector/tasks.py +8 -1
  9. package/deploy/detector/trillboards_detector/vehicle_end.py +154 -0
  10. package/deploy/systemd/trillboards-detector.service +10 -3
  11. package/dist/DetectorFields.d.ts +121 -1
  12. package/dist/DetectorFields.d.ts.map +1 -1
  13. package/dist/DetectorFields.js +123 -3
  14. package/dist/DetectorFields.js.map +1 -1
  15. package/dist/EdgeAgent.js +1 -1
  16. package/dist/LocalDetectorClient.d.ts +21 -0
  17. package/dist/LocalDetectorClient.d.ts.map +1 -1
  18. package/dist/LocalDetectorClient.js +44 -0
  19. package/dist/LocalDetectorClient.js.map +1 -1
  20. package/dist/ScopedVlmReview.d.ts +19 -4
  21. package/dist/ScopedVlmReview.d.ts.map +1 -1
  22. package/dist/ScopedVlmReview.js +83 -20
  23. package/dist/ScopedVlmReview.js.map +1 -1
  24. package/dist/SensingProfileClient.d.ts.map +1 -1
  25. package/dist/SensingProfileClient.js +2 -0
  26. package/dist/SensingProfileClient.js.map +1 -1
  27. package/dist/diagnose.d.ts +1 -1
  28. package/dist/diagnose.js +1 -1
  29. package/dist/localSensing.d.ts +4 -1
  30. package/dist/localSensing.d.ts.map +1 -1
  31. package/dist/localSensing.js +12 -8
  32. package/dist/localSensing.js.map +1 -1
  33. package/package.json +1 -1
@@ -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.abd83bba2f51"
13
+ version = "0.2.0+src.a8173c092f27"
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
 
@@ -131,7 +131,7 @@ class TestHealthIsOneRead(unittest.TestCase):
131
131
  self.assertEqual(len(calls), 1, "`/health` read the registry twice; it must describe one moment")
132
132
  # And it is still the payload every existing reader parses.
133
133
  self.assertEqual(payload["model"], "rfdetr_medium")
134
- self.assertEqual(sorted(payload["models"]), ["person_reid_osnet_0277", "ppe", "rfdetr_medium", "rtmpose_m_body7"])
134
+ self.assertEqual(sorted(payload["models"]), ["person_reid_osnet_0277", "ppe", "rfdetr_medium", "rtmpose_m_body7", "siglip2_l16_vehicle_end_v1"])
135
135
  self.assertEqual(payload["resident_models"], [])
136
136
 
137
137
  def test_before_the_first_stat_the_answer_is_loading_and_names_no_reason(self):
@@ -0,0 +1,215 @@
1
+ """The vehicle-end task: building-end crop, 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 tempfile
8
+ import threading
9
+ import unittest
10
+ import urllib.error
11
+ import urllib.request
12
+ from pathlib import Path
13
+
14
+ from trillboards_detector import vehicle_end
15
+ from trillboards_detector.errors import ModelVerificationError
16
+ from trillboards_detector.labels import RFDETR_MEDIUM
17
+ from trillboards_detector.registry import ModelEntry, ModelRegistry
18
+ from trillboards_detector.server import DetectorServer
19
+ from trillboards_detector.tasks import BUILTIN_TASK_MODELS, VEHICLE_END
20
+
21
+ # The dependency-free wire lane has no numerical packages: the crop geometry, the
22
+ # catalogue, the admission and the wire refusals run there; the pixel path runs in
23
+ # the CPU SDK lane, which installs detector[cpu].
24
+ _missing = [name for name in ("numpy", "PIL") if importlib.util.find_spec(name) is None]
25
+ needs_numeric = unittest.skipIf(_missing, "detector[cpu] dependencies absent: " + ", ".join(_missing))
26
+ # Refused or rejected before any decode, so these bytes are never read as an image.
27
+ UNREAD_JPEG = b"\xff\xd8\xff\xe0 refused before decode"
28
+
29
+
30
+ def jpeg_bytes(width=640, height=480):
31
+ from PIL import Image
32
+
33
+ image = Image.new("RGB", (width, height), (90, 90, 90))
34
+ image.paste((200, 30, 30), (100, 100, 220, 180))
35
+ buffer = io.BytesIO()
36
+ image.save(buffer, "JPEG", quality=90)
37
+ return buffer.getvalue()
38
+
39
+
40
+ class FakeSession:
41
+ """Scores the mean of the normalized crop, so the crop and preprocessing are observable."""
42
+
43
+ def __init__(self):
44
+ self.inputs = []
45
+
46
+ def run(self, names, feeds):
47
+ import numpy as np
48
+
49
+ assert names == ["end_score"] and set(feeds) == {"pixels"}
50
+ pixels = feeds["pixels"]
51
+ assert pixels.shape == (1, 3, 256, 256) and pixels.dtype == np.float32
52
+ self.inputs.append(pixels)
53
+ return [np.array([[float(pixels.mean())]], dtype=np.float32)]
54
+
55
+
56
+ def fake_estimator():
57
+ estimator = object.__new__(vehicle_end.VehicleEndEstimator)
58
+ estimator._session = FakeSession()
59
+ estimator._lock = threading.Lock()
60
+ return estimator
61
+
62
+
63
+ class BuildingEndCropTest(unittest.TestCase):
64
+ def test_left_is_the_measured_texas_crop(self):
65
+ # (x - 0.05w, y - 0.10h, x + 0.60w, y + 1.10h), Pillow's own rounding, clipped.
66
+ self.assertEqual(vehicle_end.building_end_crop([100, 50, 300, 150], "left", 640, 480), (90, 40, 220, 160))
67
+ self.assertEqual(vehicle_end.building_end_crop([2, 2, 202, 102], "left", 640, 480), (0, 0, 122, 112))
68
+
69
+ def test_other_sides_mirror_it(self):
70
+ self.assertEqual(vehicle_end.building_end_crop([100, 50, 300, 150], "right", 640, 480), (180, 40, 310, 160))
71
+ self.assertEqual(vehicle_end.building_end_crop([100, 50, 300, 150], "top", 640, 480), (80, 45, 320, 110))
72
+ self.assertEqual(vehicle_end.building_end_crop([100, 50, 300, 150], "bottom", 640, 480), (80, 90, 320, 155))
73
+
74
+ def test_a_degenerate_crop_is_refused(self):
75
+ with self.assertRaises(ValueError):
76
+ vehicle_end.building_end_crop([639, 10, 640, 11], "right", 640, 480)
77
+
78
+
79
+ @needs_numeric
80
+ class EstimateTest(unittest.TestCase):
81
+ def test_scores_the_crop_of_each_box_on_the_original_frame(self):
82
+ estimator, jpeg = fake_estimator(), jpeg_bytes()
83
+ out = estimator.estimate(jpeg, [[100.0, 100.0, 220.0, 180.0], [300.0, 300.0, 400.0, 400.0]], "left")
84
+ self.assertEqual(out["modelId"], vehicle_end.MODEL_ID)
85
+ self.assertEqual(out["modelSha256"], vehicle_end.MODEL_SHA256)
86
+ self.assertEqual(out["preprocessing"], vehicle_end.PREPROCESSING)
87
+ self.assertEqual(out["imageSha256"], hashlib.sha256(jpeg).hexdigest())
88
+ self.assertEqual(out["frameSize"], {"width": 640, "height": 480})
89
+ self.assertEqual([s["boxIndex"] for s in out["scores"]], [0, 1])
90
+ self.assertEqual(out["scores"][0]["crop"], [94, 92, 172, 188])
91
+ # The red block is in the first crop: its mean differs from the plain grey one.
92
+ self.assertNotAlmostEqual(out["scores"][0]["score"], out["scores"][1]["score"], places=3)
93
+ self.assertLessEqual(max(abs(s["score"]) for s in out["scores"]), 1.0)
94
+
95
+ def test_same_inputs_same_answer(self):
96
+ estimator, jpeg = fake_estimator(), jpeg_bytes()
97
+ a = estimator.estimate(jpeg, [[100.0, 100.0, 220.0, 180.0]], "left")
98
+ b = estimator.estimate(jpeg, [[100.0, 100.0, 220.0, 180.0]], "left")
99
+ self.assertEqual(a["scores"], b["scores"])
100
+
101
+ def test_refuses_unbounded_or_foreign_input(self):
102
+ estimator, jpeg = fake_estimator(), jpeg_bytes()
103
+ for args in ((jpeg, [], "left"), (jpeg, [[0, 0, 10, 10]] * 9, "left"), (jpeg, [[0, 0, 10, 10]], "front"),
104
+ (jpeg, [[0, 0, 700, 10]], "left"), (jpeg, [[5, 5, 5, 9]], "left"), (b"not a jpeg", [[0, 0, 10, 10]], "left")):
105
+ with self.subTest(args=args[1:]), self.assertRaises(ValueError):
106
+ estimator.estimate(*args)
107
+
108
+ def test_the_artifact_must_be_the_pinned_bytes(self):
109
+ with tempfile.TemporaryDirectory() as tmp:
110
+ path = Path(tmp) / "model.onnx"
111
+ path.write_bytes(b"not the pinned export")
112
+ with self.assertRaises(Exception) as caught:
113
+ vehicle_end.VehicleEndEstimator(path)
114
+ self.assertIn("pinned", str(caught.exception))
115
+
116
+
117
+ class CatalogueTest(unittest.TestCase):
118
+ def test_the_task_is_in_the_builtin_catalogue_with_its_measured_reservation(self):
119
+ self.assertIn(VEHICLE_END, BUILTIN_TASK_MODELS)
120
+ self.assertEqual(VEHICLE_END.task, "vehicle_end_orientation")
121
+ self.assertEqual(VEHICLE_END.sha256, vehicle_end.MODEL_SHA256)
122
+ self.assertEqual(VEHICLE_END.approx_ram_bytes, 1600 * 1024**2)
123
+
124
+ def test_admission_goes_through_the_shared_portfolio(self):
125
+ with tempfile.TemporaryDirectory() as tmp:
126
+ det, task = Path(tmp) / "det.onnx", Path(tmp) / "end.onnx"
127
+ det.write_bytes(b"det"); task.write_bytes(b"end")
128
+ loads = []
129
+
130
+ class Session:
131
+ model_sha256 = vehicle_end.MODEL_SHA256
132
+
133
+ def estimate(self, jpeg, boxes, side):
134
+ return {"modelId": vehicle_end.MODEL_ID, "modelSha256": vehicle_end.MODEL_SHA256,
135
+ "preprocessing": vehicle_end.PREPROCESSING, "scores": [{"score": 0.02} for _ in boxes]}
136
+
137
+ def factory(entry):
138
+ loads.append(entry.name)
139
+ return Session()
140
+
141
+ reg = ModelRegistry([ModelEntry(RFDETR_MEDIUM, det, pinned=True), ModelEntry(VEHICLE_END, task)],
142
+ factory, lambda session: None, 4000 * 1024**2, log=lambda line: None)
143
+ reg.refresh()
144
+ self.assertEqual(loads, ["rfdetr_medium"])
145
+ pin = {"modelId": vehicle_end.MODEL_ID, "sha256": vehicle_end.MODEL_SHA256}
146
+ out = reg.estimate_vehicle_end(b"jpeg", [[1, 2, 3, 4]], "left", pin)
147
+ self.assertEqual(len(out["scores"]), 1)
148
+ self.assertEqual(loads, ["rfdetr_medium", vehicle_end.MODEL_ID])
149
+ with self.assertRaises(ModelVerificationError):
150
+ reg.estimate_vehicle_end(b"jpeg", [[1, 2, 3, 4]], "left", {**pin, "sha256": "b" * 64})
151
+
152
+
153
+ class EndpointTest(unittest.TestCase):
154
+ def serve(self, loader):
155
+ server = DetectorServer(("127.0.0.1", 0), loader, log_line=lambda _m: None)
156
+ threading.Thread(target=lambda: server.serve_forever(poll_interval=0.02), daemon=True).start()
157
+ self.addCleanup(server.server_close)
158
+ self.addCleanup(server.shutdown)
159
+ host, port = server.server_address[:2]
160
+ return f"http://{host}:{port}/vehicle-end"
161
+
162
+ def post(self, url, body):
163
+ request = urllib.request.Request(url, data=json.dumps(body).encode(), method="POST")
164
+ request.add_header("Content-Type", "application/json")
165
+ try:
166
+ with urllib.request.urlopen(request, timeout=10) as res:
167
+ return res.status, json.loads(res.read().decode())
168
+ except urllib.error.HTTPError as err:
169
+ with err:
170
+ return err.code, json.loads(err.read().decode())
171
+
172
+ def loader(self):
173
+ estimator = fake_estimator()
174
+
175
+ class Loader:
176
+ calls = []
177
+
178
+ def estimate_vehicle_end(self, jpeg, boxes, side, model):
179
+ self.calls.append((side, boxes, model))
180
+ if model != {"modelId": vehicle_end.MODEL_ID, "sha256": vehicle_end.MODEL_SHA256}:
181
+ raise ModelVerificationError("selected vehicle-end task does not match its immutable contract")
182
+ return estimator.estimate(jpeg, boxes, side)
183
+
184
+ return Loader()
185
+
186
+ def body(self, **over):
187
+ return {"image_b64": base64.b64encode(UNREAD_JPEG).decode(), "buildingSide": "left",
188
+ "model": {"modelId": vehicle_end.MODEL_ID, "sha256": vehicle_end.MODEL_SHA256},
189
+ "boxes": [[100, 100, 220, 180]], **over}
190
+
191
+ @needs_numeric
192
+ def test_answers_each_box(self):
193
+ loader = self.loader()
194
+ status, out = self.post(self.serve(loader), self.body(image_b64=base64.b64encode(jpeg_bytes()).decode()))
195
+ self.assertEqual(status, 200)
196
+ self.assertEqual([s["boxIndex"] for s in out["scores"]], [0])
197
+ self.assertEqual(out["buildingSide"], "left")
198
+
199
+ def test_refuses_a_malformed_request_before_inference(self):
200
+ loader = self.loader()
201
+ url = self.serve(loader)
202
+ for body in (self.body(buildingSide="front"), self.body(boxes=[]), {**self.body(), "extra": 1},
203
+ self.body(image_b64="!!"), self.body(model={"modelId": vehicle_end.MODEL_ID})):
204
+ with self.subTest(body={k: v for k, v in body.items() if k != "image_b64"}):
205
+ self.assertEqual(self.post(url, body)[0], 400)
206
+ self.assertEqual(loader.calls, [])
207
+
208
+ def test_a_different_artifact_is_a_conflict(self):
209
+ status, _ = self.post(self.serve(self.loader()),
210
+ self.body(model={"modelId": vehicle_end.MODEL_ID, "sha256": "c" * 64}))
211
+ self.assertEqual(status, 409)
212
+
213
+
214
+ if __name__ == "__main__":
215
+ unittest.main()
@@ -13,5 +13,5 @@ human sets.
13
13
  """
14
14
 
15
15
  BASE_VERSION = "0.2.0"
16
- SOURCE_FINGERPRINT = "abd83bba2f51"
17
- VERSION = "0.2.0+src.abd83bba2f51"
16
+ SOURCE_FINGERPRINT = "a8173c092f27"
17
+ VERSION = "0.2.0+src.a8173c092f27"
@@ -171,6 +171,9 @@ class DetectorLoader:
171
171
  if entry.vocabulary.task == "actor_appearance":
172
172
  from .appearance import PersonAppearanceEstimator
173
173
  return PersonAppearanceEstimator(entry.path, intra_op_threads=self._intra_op_threads or 4, accelerator="cpu")
174
+ if entry.vocabulary.task == "vehicle_end_orientation":
175
+ from .vehicle_end import VehicleEndEstimator
176
+ return VehicleEndEstimator(entry.path, intra_op_threads=self._intra_op_threads or 4, accelerator="cpu")
174
177
  from .pose import PoseEstimator
175
178
  return PoseEstimator(entry.path, intra_op_threads=self._intra_op_threads or 4, accelerator=self._accelerator)
176
179
  from .session import OnnxDetector
@@ -318,6 +321,9 @@ class DetectorLoader:
318
321
  def estimate_appearance(self, jpeg: bytes, boxes: list, expected_model: dict) -> dict:
319
322
  return self.registry.estimate_appearance(jpeg, boxes, expected_model)
320
323
 
324
+ def estimate_vehicle_end(self, jpeg: bytes, boxes: list, side: str, expected_model: dict) -> dict:
325
+ return self.registry.estimate_vehicle_end(jpeg, boxes, side, expected_model)
326
+
321
327
  def classify_actor_actions(self,batch):
322
328
  return self.registry.classify_actor_actions(batch)
323
329
 
@@ -123,7 +123,7 @@ class TaskModelContract:
123
123
  self._validate_task()
124
124
 
125
125
  def _validate_task(self):
126
- if self.task not in {"pose_coco17", "actor_appearance"}:
126
+ if self.task not in {"pose_coco17", "actor_appearance", "vehicle_end_orientation"}:
127
127
  raise ValueError("invalid non-box task kind")
128
128
 
129
129
  class ModelEntry:
@@ -529,6 +529,35 @@ class ModelRegistry:
529
529
  finally:
530
530
  self._release([state.entry.name])
531
531
 
532
+ def estimate_vehicle_end(self, jpeg: bytes, boxes: list, side: str, expected_model: dict) -> dict:
533
+ """Acquire the vehicle-end orientation task through the same portfolio as every session."""
534
+ state = self._states.get(expected_model.get("modelId"))
535
+ if (state is None or not isinstance(state.entry.vocabulary, TaskModelContract)
536
+ or state.entry.vocabulary.task != "vehicle_end_orientation"
537
+ or state.entry.vocabulary.sha256 != expected_model.get("sha256")):
538
+ raise ModelVerificationError("selected vehicle-end task does not match its immutable contract")
539
+ contract = state.entry.vocabulary
540
+ sessions = self._acquire([state.entry.name])
541
+ try:
542
+ session = sessions[0]
543
+ if getattr(session, "model_sha256", None) != contract.sha256:
544
+ raise ModelVerificationError("loaded vehicle-end session does not match the selected artifact")
545
+ try:
546
+ result = session.estimate(jpeg, boxes, side)
547
+ if (result.get("modelId") != contract.model_name
548
+ or result.get("modelSha256") != contract.sha256
549
+ or result.get("preprocessing") != contract.preprocessing):
550
+ raise ModelVerificationError("vehicle-end output does not match its task contract")
551
+ except Exception:
552
+ with self._lock:
553
+ state.detect_failures += 1
554
+ raise
555
+ with self._lock:
556
+ state.detect_count += 1
557
+ return result
558
+ finally:
559
+ self._release([state.entry.name])
560
+
532
561
  def classify_actor_actions(self,batch):
533
562
  """Acquire and prove the selected non-box action model through the shared portfolio."""
534
563
  from .actor_action_contract import ActorActionModelContract
@@ -39,6 +39,7 @@ from .actor_identity import MODEL_ID as APPEARANCE_MODEL_ID, MODEL_SHA256 as APP
39
39
  from .tasks import RTMPOSE_MODEL_ID, RTMPOSE_SHA256, RTMPOSE_PREPROCESSING, COCO17
40
40
  from .actor_action_wire import parse_actor_action_request, validate_actor_action_response
41
41
  from .colors import validate_color_request
42
+ from .vehicle_end import validate_vehicle_end_request
42
43
  from ._version import VERSION
43
44
  from .errors import DetectorNotReady, ModelVerificationError, UnknownClasses
44
45
 
@@ -413,6 +414,8 @@ class DetectorHandler(BaseHTTPRequestHandler):
413
414
  self._detect()
414
415
  elif self._path() == "/actor-actions":
415
416
  self._actor_actions()
417
+ elif self._path() == "/vehicle-end":
418
+ self._vehicle_end()
416
419
  elif self._path() == "/health":
417
420
  self._json(405, {"error": "/health is a GET"})
418
421
  else:
@@ -620,6 +623,42 @@ class DetectorHandler(BaseHTTPRequestHandler):
620
623
  self._json(500,{"error":"refused malformed actor action output"});return
621
624
  self._json(200,result)
622
625
 
626
+ def _vehicle_end(self):
627
+ """Which end of each named parked vehicle faces the building (vehicle_end.py)."""
628
+ try:
629
+ raw = self._read_body()
630
+ try:
631
+ body = json.loads(raw.decode("utf-8"))
632
+ model, side, boxes = validate_vehicle_end_request(body)
633
+ jpeg = base64.b64decode(body["image_b64"], validate=True) if isinstance(body["image_b64"], str) else b""
634
+ except (ValueError, UnicodeDecodeError, binascii.Error) as error:
635
+ raise WireError(400, str(error)) from error
636
+ if not jpeg:
637
+ raise WireError(400, "`image_b64` must be a non-empty base64 string")
638
+ except WireError as error:
639
+ self._json(error.status, {"error": error.message}); return
640
+ estimate = getattr(self.server.loader, "estimate_vehicle_end", None)
641
+ if not callable(estimate):
642
+ self._json(503, {"error": "vehicle-end task is unavailable in this detector build"}); return
643
+ try:
644
+ result = estimate(jpeg, boxes, side, model)
645
+ except ModelVerificationError as error:
646
+ self._json(409, {"error": str(error)}); return
647
+ except DetectorNotReady as error:
648
+ self._json(503, {"error": str(error)}); return
649
+ except ValueError as error:
650
+ self._json(400, {"error": str(error)}); return
651
+ except Exception as error:
652
+ self._log(f"[detector] vehicle-end inference failed: {type(error).__name__}: {error}")
653
+ self._json(500, {"error": "vehicle-end inference failed"}); return
654
+ if (not isinstance(result, dict) or result.get("modelId") != model.get("modelId")
655
+ or result.get("modelSha256") != model.get("sha256") or result.get("buildingSide") != side
656
+ or not isinstance(result.get("scores"), list) or len(result["scores"]) != len(boxes)
657
+ or result.get("imageSha256") != hashlib.sha256(jpeg).hexdigest()):
658
+ self._log("[detector] REFUSING malformed vehicle-end output")
659
+ self._json(500, {"error": "refused malformed vehicle-end output"}); return
660
+ self._json(200, result)
661
+
623
662
  # ── plumbing ─────────────────────────────────────────────────────────────
624
663
 
625
664
  def _read_body(self) -> bytes:
@@ -2,6 +2,7 @@
2
2
  from .registry import TaskModelContract
3
3
  from .actor_identity import MODEL_ID as APPEARANCE_MODEL_ID, MODEL_SHA256 as APPEARANCE_SHA256, PREPROCESSING as APPEARANCE_PREPROCESSING
4
4
  from .actor_action_contract import ActorActionModelContract,ActorActionOutput,ActorActionWindowContract,CONTACT_PREPROCESSING
5
+ from .vehicle_end import MODEL_ID as VEHICLE_END_MODEL_ID, MODEL_SHA256 as VEHICLE_END_SHA256, PREPROCESSING as VEHICLE_END_PREPROCESSING
5
6
 
6
7
  RTMPOSE_MODEL_ID = "rtmpose_m_body7"
7
8
  RTMPOSE_SHA256 = "5c0a4bf67953e6d2ac43ce15e77dc9d5d354ae18430a47d2c5963a7bc5683e3c"
@@ -25,7 +26,13 @@ PHONE_ACTION_E3 = ActorActionModelContract(
25
26
  PERSON_APPEARANCE = TaskModelContract(
26
27
  APPEARANCE_MODEL_ID, "actor_appearance", APPEARANCE_SHA256,
27
28
  256 * 1024**2, APPEARANCE_PREPROCESSING)
28
- BUILTIN_TASK_MODELS = (RTMPOSE_BODY7, PERSON_APPEARANCE)
29
+ # Which end of a parked vehicle faces the building (vehicle_end.py). CPU4: resident
30
+ # 1,382 MiB after one inference, loaded from its verified path (proc-statm); the
31
+ # reservation adds process overhead and headroom. No field ownership of its own:
32
+ # the connector's vehicle gate turns two looks into reverse_parked_vehicle_detected.
33
+ VEHICLE_END = TaskModelContract(
34
+ VEHICLE_END_MODEL_ID, "vehicle_end_orientation", VEHICLE_END_SHA256, 1600 * 1024**2, VEHICLE_END_PREPROCESSING)
35
+ BUILTIN_TASK_MODELS = (RTMPOSE_BODY7, PERSON_APPEARANCE, VEHICLE_END)
29
36
 
30
37
  COCO17 = ("nose", "left_eye", "right_eye", "left_ear", "right_ear",
31
38
  "left_shoulder", "right_shoulder", "left_elbow", "right_elbow",
@@ -0,0 +1,154 @@
1
+ """Which end of a parked vehicle faces the building: a pinned zero-shot image-text score.
2
+
3
+ The artifact is the SigLIP 2 ViT-L/16-256 image tower (timm/ViT-L-16-SigLIP2-256 at
4
+ da9426945c7c5acbd3afd0c158c8bb9cfd4d8cc0, Apache-2.0) with one fixed direction
5
+ folded into the graph: the mean text embedding of five "back of a vehicle"
6
+ prompts minus five "front of a vehicle" prompts. It returns, per vehicle box,
7
+ `cos(image, back) - cos(image, front)` on the crop of the box's BUILDING END,
8
+ which the caller names per camera (`buildingSide`): a nose-in car shows its front
9
+ there, a backed-in car its rear. Measured on the Texas parking lot in
10
+ docs/runbooks/cctv-detector-tier.md section 5c; the decision rule (two looks, the
11
+ pinned threshold) lives with the connector's vehicle gate, not here.
12
+ """
13
+
14
+ from __future__ import annotations
15
+
16
+ import hashlib
17
+ import io
18
+ import math
19
+ from pathlib import Path
20
+ import threading
21
+ import time
22
+
23
+ from .errors import ModelContractError
24
+
25
+ MODEL_ID = "siglip2_l16_vehicle_end_v1"
26
+ MODEL_SHA256 = "682c4b3255f94bc82dda6f47ae10d73082f40fa33c3535b7b7f7e87d3cab20a8"
27
+ PREPROCESSING = "siglip2_rgb_bicubic256_unit_mean0.5_std0.5_building_end_crop_v1"
28
+ SIDES = ("left", "right", "top", "bottom")
29
+ MAX_BOXES = 8
30
+ # The building end of a box, as fractions of its own width/height:
31
+ # (x0, y0, x1, y1) offsets from the box's left/top edge. `left` is the measured
32
+ # Texas geometry (curb and store on the image's left); the others are its mirrors.
33
+ _END = {
34
+ "left": (-0.05, -0.10, 0.60, 1.10),
35
+ "right": (0.40, -0.10, 1.05, 1.10),
36
+ "top": (-0.10, -0.05, 1.10, 0.60),
37
+ "bottom": (-0.10, 0.40, 1.10, 1.05),
38
+ }
39
+
40
+
41
+ def building_end_crop(box, side, width, height):
42
+ """Integer crop rectangle (Pillow's own rounding of a float box), clipped to the frame."""
43
+ x0, y0, x1, y1 = box
44
+ w, h = x1 - x0, y1 - y0
45
+ a, b, c, d = _END[side]
46
+ rect = (max(0.0, x0 + a * w), max(0.0, y0 + b * h), min(float(width), x0 + c * w), min(float(height), y0 + d * h))
47
+ left, top, right, bottom = (int(round(v)) for v in rect)
48
+ if right - left < 2 or bottom - top < 2:
49
+ raise ValueError("Vehicle building-end crop is degenerate")
50
+ return left, top, right, bottom
51
+
52
+
53
+ def _sha256_file(path):
54
+ digest = hashlib.sha256()
55
+ with open(path, "rb") as handle:
56
+ for chunk in iter(lambda: handle.read(8 << 20), b""):
57
+ digest.update(chunk)
58
+ return digest.hexdigest()
59
+
60
+
61
+ class VehicleEndEstimator:
62
+ """One immutable CPU artifact, loaded from its verified path (1.26 GB: never held twice)."""
63
+
64
+ model_name = MODEL_ID
65
+ model_sha256 = MODEL_SHA256
66
+
67
+ def __init__(self, model_path, *, intra_op_threads=4, accelerator="cpu"):
68
+ if type(intra_op_threads) is not int or not 1 <= intra_op_threads <= 8:
69
+ raise ValueError("Invalid bounded vehicle-end thread count")
70
+ if accelerator != "cpu":
71
+ raise ModelContractError("This vehicle-end artifact is qualified for native CPU execution")
72
+ path = str(Path(model_path))
73
+ if _sha256_file(path) != MODEL_SHA256:
74
+ raise ModelContractError("Vehicle-end artifact differs from the pinned export")
75
+ import onnxruntime as ort
76
+
77
+ options = ort.SessionOptions()
78
+ options.intra_op_num_threads = intra_op_threads
79
+ options.inter_op_num_threads = 1
80
+ options.enable_cpu_mem_arena = False
81
+ options.log_severity_level = 3
82
+ self._session = ort.InferenceSession(path, sess_options=options, providers=["CPUExecutionProvider"])
83
+ # Loaded by path to keep one copy resident; re-check the bytes it read.
84
+ if _sha256_file(path) != MODEL_SHA256:
85
+ raise ModelContractError("Vehicle-end artifact changed while it was loading")
86
+ inputs, outputs = self._session.get_inputs(), self._session.get_outputs()
87
+ if (len(inputs) != 1 or inputs[0].name != "pixels" or inputs[0].shape != [1, 3, 256, 256]
88
+ or inputs[0].type != "tensor(float)" or len(outputs) != 1 or outputs[0].name != "end_score"
89
+ or outputs[0].shape != [1, 1] or outputs[0].type != "tensor(float)"):
90
+ raise ModelContractError("Vehicle-end graph differs from its pixels -> end_score contract")
91
+ self._lock = threading.Lock()
92
+ self.acceleration = {"requested": "cpu", "backend": "cpu", "providers": ["CPUExecutionProvider"],
93
+ "fallback_reason": None}
94
+
95
+ def estimate(self, jpeg, boxes, side):
96
+ import numpy as np
97
+ from PIL import Image
98
+
99
+ if (not isinstance(jpeg, bytes) or not 0 < len(jpeg) <= 16 * 1024**2 or side not in SIDES
100
+ or not isinstance(boxes, list) or not 1 <= len(boxes) <= MAX_BOXES):
101
+ raise ValueError("Invalid bounded vehicle-end request")
102
+ try:
103
+ with Image.open(io.BytesIO(jpeg)) as header:
104
+ width, height = header.size
105
+ if header.format != "JPEG" or not 2 <= width <= 8192 or not 2 <= height <= 8192:
106
+ raise ValueError("Vehicle-end requires a bounded original JPEG")
107
+ except Exception as error:
108
+ raise ValueError("Invalid original vehicle-end JPEG") from error
109
+ for box in boxes:
110
+ if (not isinstance(box, (list, tuple)) or len(box) != 4
111
+ or any(isinstance(v, bool) or not isinstance(v, (int, float)) or not math.isfinite(v) for v in box)
112
+ or not 0 <= box[0] < box[2] <= width + 0.01 or not 0 <= box[1] < box[3] <= height + 0.01):
113
+ raise ValueError("Vehicle rectangle is outside the original frame")
114
+ started = time.monotonic()
115
+ scores = []
116
+ with self._lock:
117
+ with Image.open(io.BytesIO(jpeg)) as decoded:
118
+ image = decoded.convert("RGB")
119
+ for index, box in enumerate(boxes):
120
+ crop = building_end_crop([float(v) for v in box], side, width, height)
121
+ pixels = np.asarray(image.crop(crop).resize((256, 256), Image.BICUBIC), dtype=np.float32) / 255.0
122
+ tensor = np.ascontiguousarray(((pixels - 0.5) / 0.5).transpose(2, 0, 1)[None], dtype=np.float32)
123
+ value = self._session.run(["end_score"], {"pixels": tensor})[0]
124
+ if value.shape != (1, 1) or not np.isfinite(value).all() or abs(float(value[0, 0])) > 2:
125
+ raise ModelContractError("Native vehicle-end output is invalid")
126
+ scores.append({"boxIndex": index, "crop": list(crop),
127
+ "inputSha256": hashlib.sha256(tensor.tobytes()).hexdigest(),
128
+ "score": float(value[0, 0])})
129
+ return {"version": 1, "modelId": MODEL_ID, "modelSha256": MODEL_SHA256, "preprocessing": PREPROCESSING,
130
+ "imageSha256": hashlib.sha256(jpeg).hexdigest(), "frameSize": {"width": width, "height": height},
131
+ "buildingSide": side, "scores": scores, "inferenceMs": (time.monotonic() - started) * 1000}
132
+
133
+ def probe(self):
134
+ """A forward pass completes (the loader's admission check); no claim about any decision."""
135
+ from PIL import Image
136
+
137
+ buffer = io.BytesIO()
138
+ Image.new("RGB", (64, 48), (128, 128, 128)).save(buffer, "JPEG")
139
+ self.estimate(buffer.getvalue(), [[8.0, 8.0, 56.0, 40.0]], "left")
140
+
141
+
142
+ def validate_vehicle_end_request(body):
143
+ """`/vehicle-end` body: the pinned model, one camera side, 1..8 original-frame boxes."""
144
+ if not isinstance(body, dict) or set(body) != {"image_b64", "model", "buildingSide", "boxes"}:
145
+ raise ValueError("vehicle-end request needs exactly image_b64, model, buildingSide and boxes")
146
+ model = body["model"]
147
+ if not isinstance(model, dict) or set(model) != {"modelId", "sha256"}:
148
+ raise ValueError("vehicle-end model must name modelId and sha256")
149
+ if body["buildingSide"] not in SIDES:
150
+ raise ValueError("vehicle-end buildingSide must be left, right, top or bottom")
151
+ boxes = body["boxes"]
152
+ if not isinstance(boxes, list) or not 1 <= len(boxes) <= MAX_BOXES:
153
+ raise ValueError("vehicle-end needs 1..%d boxes" % MAX_BOXES)
154
+ return model, body["buildingSide"], boxes
@@ -30,10 +30,17 @@ SyslogIdentifier=trillboards-detector
30
30
  # render nodes (`docs/runbooks/cctv-detector-tier.md` §6 — the DirectML/ROCm path
31
31
  # was never exercised and the decision is CPU-only), and it needs no model store
32
32
  # of its own beyond the connector's data dir.
33
+ #
34
+ # /var/lib/trillboards is WRITABLE here (it is TRILLBOARDS_DATA_DIR below): the
35
+ # detector keeps its actor-tracking journal at
36
+ # <data dir>/actor-tracking/tracks.sqlite3 (cli.py, since #15587). Read-only, its
37
+ # mkdir failed and the service crash-looped on every fresh Linux install. The
38
+ # path must EXIST before this unit is installed, or systemd refuses to start it
39
+ # (226/NAMESPACE): install.sh creates and chowns it before it copies this file.
33
40
  NoNewPrivileges=true
34
41
  ProtectSystem=strict
35
42
  ProtectHome=false
36
- ReadWritePaths=/opt/trillboards
43
+ ReadWritePaths=/opt/trillboards /var/lib/trillboards
37
44
  PrivateTmp=true
38
45
  PrivateDevices=true
39
46
 
@@ -54,8 +61,8 @@ Environment=HOME=/opt/trillboards
54
61
  # not an option: the connector is the process that WRITES the artifact, and its
55
62
  # unit is sandboxed to /var/lib/trillboards.
56
63
  #
57
- # READ-ONLY here, deliberately. ProtectSystem=strict leaves /var readable and
58
- # this service only ever opens the .onnx; the connector owns every write to it.
64
+ # The connector still owns every write to models/; this service only opens the
65
+ # .onnx there. Its one write is the actor-tracking journal (ReadWritePaths above).
59
66
  Environment=TRILLBOARDS_DATA_DIR=/var/lib/trillboards
60
67
  EnvironmentFile=-/etc/trillboards/env
61
68