@trillboards/edge-sdk 0.2.245 → 0.2.246

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 (84) hide show
  1. package/deploy/detector/bench/detect_frames.py +88 -0
  2. package/deploy/detector/bench/openvino_parity.py +152 -0
  3. package/deploy/detector/pyproject.toml +10 -1
  4. package/deploy/detector/tests/test_actor_tracking.py +32 -0
  5. package/deploy/detector/tests/test_actor_tracking_wire.py +11 -0
  6. package/deploy/detector/tests/test_openvino_engines.py +251 -0
  7. package/deploy/detector/tests/test_server.py +43 -0
  8. package/deploy/detector/tests/test_version_stamp.py +17 -4
  9. package/deploy/detector/trillboards_detector/_version.py +2 -2
  10. package/deploy/detector/trillboards_detector/actor_tracking.py +51 -0
  11. package/deploy/detector/trillboards_detector/cli.py +5 -0
  12. package/deploy/detector/trillboards_detector/detections.py +5 -0
  13. package/deploy/detector/trillboards_detector/execution.py +386 -0
  14. package/deploy/detector/trillboards_detector/loader.py +12 -1
  15. package/deploy/detector/trillboards_detector/registry.py +6 -0
  16. package/deploy/detector/trillboards_detector/server.py +41 -0
  17. package/deploy/detector/trillboards_detector/session.py +81 -3
  18. package/deploy/detector/trillboards_detector/track_records.py +113 -0
  19. package/deploy/windows/ci/test-detector-reinstall.ps1 +474 -0
  20. package/deploy/windows/setup-detector.ps1 +122 -29
  21. package/dist/ActorTracking.d.ts +38 -0
  22. package/dist/ActorTracking.d.ts.map +1 -1
  23. package/dist/ActorTracking.js +40 -0
  24. package/dist/ActorTracking.js.map +1 -1
  25. package/dist/AlertClips.d.ts +4 -2
  26. package/dist/AlertClips.d.ts.map +1 -1
  27. package/dist/AlertClips.js +24 -9
  28. package/dist/AlertClips.js.map +1 -1
  29. package/dist/CloudSensingManager.d.ts.map +1 -1
  30. package/dist/CloudSensingManager.js +6 -1
  31. package/dist/CloudSensingManager.js.map +1 -1
  32. package/dist/DurableVlmCandidateSpool.d.ts +11 -0
  33. package/dist/DurableVlmCandidateSpool.d.ts.map +1 -1
  34. package/dist/DurableVlmCandidateSpool.js +31 -2
  35. package/dist/DurableVlmCandidateSpool.js.map +1 -1
  36. package/dist/EdgeAgent.js +1 -1
  37. package/dist/FrameGroupingShadow.d.ts +57 -0
  38. package/dist/FrameGroupingShadow.d.ts.map +1 -1
  39. package/dist/FrameGroupingShadow.js +123 -5
  40. package/dist/FrameGroupingShadow.js.map +1 -1
  41. package/dist/LocalDetectorClient.d.ts +25 -1
  42. package/dist/LocalDetectorClient.d.ts.map +1 -1
  43. package/dist/LocalDetectorClient.js +60 -0
  44. package/dist/LocalDetectorClient.js.map +1 -1
  45. package/dist/LocalVideoBuffer.d.ts +14 -2
  46. package/dist/LocalVideoBuffer.d.ts.map +1 -1
  47. package/dist/LocalVideoBuffer.js +23 -3
  48. package/dist/LocalVideoBuffer.js.map +1 -1
  49. package/dist/SensingProfileClient.d.ts.map +1 -1
  50. package/dist/SensingProfileClient.js +3 -0
  51. package/dist/SensingProfileClient.js.map +1 -1
  52. package/dist/VideoSegmentUploader.d.ts +14 -7
  53. package/dist/VideoSegmentUploader.d.ts.map +1 -1
  54. package/dist/VideoSegmentUploader.js +34 -38
  55. package/dist/VideoSegmentUploader.js.map +1 -1
  56. package/dist/VlmCandidateBuffer.d.ts +13 -0
  57. package/dist/VlmCandidateBuffer.d.ts.map +1 -1
  58. package/dist/VlmCandidateBuffer.js.map +1 -1
  59. package/dist/autoSense.d.ts +4 -0
  60. package/dist/autoSense.d.ts.map +1 -1
  61. package/dist/autoSense.js +4 -0
  62. package/dist/autoSense.js.map +1 -1
  63. package/dist/continuousReview.d.ts +6 -2
  64. package/dist/continuousReview.d.ts.map +1 -1
  65. package/dist/continuousReview.js.map +1 -1
  66. package/dist/detectorBackendRelay.d.ts +24 -0
  67. package/dist/detectorBackendRelay.d.ts.map +1 -0
  68. package/dist/detectorBackendRelay.js +70 -0
  69. package/dist/detectorBackendRelay.js.map +1 -0
  70. package/dist/diagnose.d.ts +1 -1
  71. package/dist/diagnose.js +1 -1
  72. package/dist/localSensing.d.ts +3 -0
  73. package/dist/localSensing.d.ts.map +1 -1
  74. package/dist/localSensing.js +69 -3
  75. package/dist/localSensing.js.map +1 -1
  76. package/dist/pipelineSwitches.d.ts +59 -0
  77. package/dist/pipelineSwitches.d.ts.map +1 -0
  78. package/dist/pipelineSwitches.js +240 -0
  79. package/dist/pipelineSwitches.js.map +1 -0
  80. package/package.json +4 -4
  81. package/retained/RetainedCaptureClient.cjs +37 -2
  82. package/retained/WebsiteAdmissionCaptureClient.cjs +57 -2
  83. package/retained/__tests__/look-reuse.test.cjs +167 -0
  84. package/retained/manager.cjs +28 -2
@@ -0,0 +1,88 @@
1
+ """Person boxes for a list of recorded looks, through the detector service's own pixels and selection.
2
+
3
+ The input half of `scripts/look-reuse-replay.cjs` (plan v7 W8): for every <capture ms>.jpg the replay's
4
+ schedule names, RF-DETR Medium's person detections at the service floor (0.25), selected exactly as
5
+ `session.postprocess` selects, written one JSON line per look: {"ms": ..., "persons": [[conf, x, y, w, h], ...]}
6
+ in the look's own pixels. Both replay paths read the same file, so the engine only has to be faithful to
7
+ itself; `--engine openvino` (CPU, FP32, async) is the fast one, parity-checked by `openvino_parity.py`.
8
+
9
+ usage: python bench/detect_frames.py --model rfdetr-medium.onnx --frames DIR --only looks.json --out det.jsonl
10
+ [--engine openvino|ort] [--threads N] [--jobs N]
11
+ """
12
+ from __future__ import annotations
13
+
14
+ import argparse
15
+ import concurrent.futures as cf
16
+ import json
17
+ import sys
18
+ from pathlib import Path
19
+
20
+
21
+ def main(argv=None) -> int:
22
+ ap = argparse.ArgumentParser()
23
+ ap.add_argument("--model", required=True)
24
+ ap.add_argument("--frames", required=True)
25
+ ap.add_argument("--only", required=True, help="JSON list of capture ms to detect")
26
+ ap.add_argument("--out", required=True)
27
+ ap.add_argument("--engine", choices=("openvino", "ort"), default="openvino")
28
+ ap.add_argument("--threads", type=int, default=None)
29
+ ap.add_argument("--jobs", type=int, default=4, help="preprocessing threads")
30
+ a = ap.parse_args(argv)
31
+
32
+ import numpy as np
33
+ from trillboards_detector.labels import RFDETR_MEDIUM
34
+ from trillboards_detector.session import postprocess, preprocess
35
+
36
+ wanted = [int(ms) for ms in json.loads(Path(a.only).read_text())]
37
+ out_path = Path(a.out)
38
+ done = set()
39
+ if out_path.exists():
40
+ done = {json.loads(line)["ms"] for line in out_path.read_text().splitlines() if line.strip()}
41
+ todo = [ms for ms in wanted if ms not in done]
42
+ model = Path(a.model).read_bytes()
43
+
44
+ def prep(ms):
45
+ blob, w, h = preprocess((Path(a.frames) / f"{ms}.jpg").read_bytes(), 576, 576)
46
+ return ms, np.ascontiguousarray(blob), w, h
47
+
48
+ def boxes(dets, logits, w, h):
49
+ batch = postprocess(dets, logits, w, h, 0.25, {"person"}, vocabulary=RFDETR_MEDIUM)
50
+ return [[round(d.confidence, 5), round(d.x, 2), round(d.y, 2), round(d.width, 2), round(d.height, 2)]
51
+ for d in batch.detections]
52
+
53
+ with out_path.open("a") as out, cf.ThreadPoolExecutor(a.jobs) as pool:
54
+ if a.engine == "openvino":
55
+ import openvino as ov
56
+ core = ov.Core()
57
+ compiled = core.compile_model(core.read_model(model=model), "CPU",
58
+ {"PERFORMANCE_HINT": "THROUGHPUT", "INFERENCE_PRECISION_HINT": "f32",
59
+ **({"INFERENCE_NUM_THREADS": a.threads} if a.threads else {})})
60
+ ports = {p.get_any_name(): p for p in compiled.outputs}
61
+ queue = ov.AsyncInferQueue(compiled, compiled.get_property("OPTIMAL_NUMBER_OF_INFER_REQUESTS"))
62
+
63
+ def done_cb(request, meta):
64
+ ms, w, h = meta
65
+ dets = request.get_tensor(ports["dets"]).data.copy()
66
+ logits = request.get_tensor(ports["labels"]).data.copy()
67
+ out.write(json.dumps({"ms": ms, "persons": boxes(dets, logits, w, h)}) + "\n")
68
+
69
+ queue.set_callback(done_cb)
70
+ for ms, blob, w, h in pool.map(prep, todo):
71
+ queue.start_async({0: blob}, (ms, w, h))
72
+ queue.wait_all()
73
+ else:
74
+ import onnxruntime as ort
75
+ so = ort.SessionOptions()
76
+ if a.threads:
77
+ so.intra_op_num_threads = a.threads
78
+ session = ort.InferenceSession(model, sess_options=so, providers=["CPUExecutionProvider"])
79
+ name = session.get_inputs()[0].name
80
+ for ms, blob, w, h in pool.map(prep, todo):
81
+ dets, logits = session.run(["dets", "labels"], {name: blob})
82
+ out.write(json.dumps({"ms": ms, "persons": boxes(dets, logits, w, h)}) + "\n")
83
+ print(json.dumps({"looks": len(wanted), "detected": len(todo), "already": len(done)}))
84
+ return 0
85
+
86
+
87
+ if __name__ == "__main__":
88
+ sys.exit(main())
@@ -0,0 +1,152 @@
1
+ """OpenVINO against ONNX Runtime on real frames: the plan v7 W8 parity bar and gain.
2
+
3
+ Runs the SHIPPED pipeline: `session.preprocess` (Pillow stretch + ImageNet),
4
+ the shipped artifact, ORT's CPU session exactly as `OnnxDetector` builds it, and
5
+ `execution.OpenVinoSession` (OpenVINO used directly, FP32 on the CPU; `--gpu-precision`
6
+ adds the integrated GPU). Per frame it compares every engine's outputs with ORT's:
7
+
8
+ person_diff the largest person-probability gap over queries either engine scores
9
+ at or above 0.2 (session.PARITY_FLOOR: where detections live)
10
+ mismatches (query, class) pairs kept at an operating threshold (0.25, 0.2939, 0.5)
11
+ by one engine and not the other, selected exactly as postprocess selects
12
+ box_shift the largest box-coordinate gap among pairs both engines keep (frame fraction)
13
+ floor_0.05 the same set comparison at the uniform rule's 0.05 fixtures floor, where
14
+ OpenVINO is NOT used (execution.OPENVINO_MIN_CONFIDENCE)
15
+ persons person detections ORT keeps at 0.25 (what the bar is about)
16
+
17
+ and times each engine interleaved, frame by frame, so a noisy host affects both
18
+ alike. Bar (plan v7 W8): identical detections (0 mismatches) and person scores
19
+ within 1e-3 of ORT on the 360-frame and the 134-frame sets.
20
+
21
+ usage: python -I bench/openvino_parity.py --model rfdetr-medium.onnx --frames LIST
22
+ [--frames LIST2 ...] [--threads N] [--gpu-precision f16 --gpu-precision f32]
23
+ [--avx2-only] --out result.json
24
+
25
+ `--avx2-only` sets ONEDNN_MAX_CPU_ISA=AVX2 for OpenVINO's CPU plugin before it
26
+ loads, to approximate an AVX2-only box (the Texas i7-7700) on a newer CPU.
27
+ """
28
+ from __future__ import annotations
29
+
30
+ import argparse
31
+ import hashlib
32
+ import json
33
+ import os
34
+ import platform
35
+ import statistics
36
+ import sys
37
+ import time
38
+ from pathlib import Path
39
+
40
+
41
+ def main(argv=None) -> int:
42
+ ap = argparse.ArgumentParser()
43
+ ap.add_argument("--model", required=True)
44
+ ap.add_argument("--frames", action="append", required=True, help="a file listing JPEG paths, one per line")
45
+ ap.add_argument("--threads", type=int, default=None)
46
+ ap.add_argument("--gpu-precision", action="append", default=[])
47
+ ap.add_argument("--avx2-only", action="store_true")
48
+ ap.add_argument("--warmup", type=int, default=3)
49
+ ap.add_argument("--out", required=True)
50
+ a = ap.parse_args(argv)
51
+ if a.avx2_only:
52
+ os.environ["ONEDNN_MAX_CPU_ISA"] = "AVX2"
53
+
54
+ import numpy as np
55
+ import onnxruntime as ort
56
+ from trillboards_detector.execution import OpenVinoSession, openvino_version
57
+ from trillboards_detector.labels import RFDETR_MEDIUM
58
+ from trillboards_detector.session import NUM_SELECT, OPERATING_THRESHOLDS, PARITY_FLOOR, _sigmoid, preprocess
59
+
60
+ model_bytes = Path(a.model).read_bytes()
61
+ sha = hashlib.sha256(model_bytes).hexdigest()
62
+ so = ort.SessionOptions()
63
+ so.log_severity_level = 3
64
+ if a.threads:
65
+ so.intra_op_num_threads = a.threads
66
+ ref = ort.InferenceSession(model_bytes, sess_options=so, providers=["CPUExecutionProvider"])
67
+ in_name = ref.get_inputs()[0].name
68
+ size = ref.get_inputs()[0].shape[2]
69
+ engines = {"openvino_cpu": OpenVinoSession(model_bytes, "CPU", "f32", threads=a.threads)}
70
+ for precision in a.gpu_precision:
71
+ try:
72
+ engines[f"openvino_gpu_{precision}"] = OpenVinoSession(model_bytes, "GPU", precision)
73
+ except Exception as error: # report, never hide: a GPU that does not bind is a result
74
+ engines[f"openvino_gpu_{precision}"] = error
75
+ person = next(slot for slot, name in RFDETR_MEDIUM.slot_names.items() if name == "person")
76
+
77
+ def outputs(session, blob):
78
+ return session.run(["dets", "labels"], {in_name: blob})
79
+
80
+ def kept_at(prob, threshold):
81
+ flat = prob.reshape(-1)
82
+ order = np.argsort(-flat, kind="stable")[:NUM_SELECT]
83
+ return {int(i) for i in order[flat[order] > threshold]}
84
+
85
+ def grade(reference, candidate):
86
+ p_ref, p_cand = _sigmoid(reference[1][0]), _sigmoid(candidate[1][0])
87
+ live = (p_ref >= PARITY_FLOOR) | (p_cand >= PARITY_FLOOR)
88
+ diff = np.where(live, np.abs(p_ref - p_cand), 0.0)
89
+ mismatches, shift, n_classes = 0, 0.0, p_ref.shape[1]
90
+ for threshold in OPERATING_THRESHOLDS:
91
+ a, b = kept_at(p_ref, threshold), kept_at(p_cand, threshold)
92
+ mismatches += len(a ^ b)
93
+ for index in a & b:
94
+ q = index // n_classes
95
+ shift = max(shift, float(np.abs(reference[0][0][q] - candidate[0][0][q]).max()))
96
+ low = len(kept_at(p_ref, 0.05) ^ kept_at(p_cand, 0.05))
97
+ return float(diff[:, person].max()), float(diff.max()), mismatches, shift, low
98
+
99
+ result = {"model_sha256": sha, "openvino": openvino_version(), "onnxruntime": ort.__version__,
100
+ "host": {"platform": platform.platform(), "processor": platform.processor(),
101
+ "cpu_count": os.cpu_count(), "avx2_only": a.avx2_only, "threads": a.threads},
102
+ "sets": {}}
103
+ live = {name: s for name, s in engines.items() if not isinstance(s, Exception)}
104
+ for name, s in engines.items():
105
+ if isinstance(s, Exception):
106
+ result.setdefault("unavailable", {})[name] = f"{type(s).__name__}: {s}"[:500]
107
+ for listing in a.frames:
108
+ paths = [p for p in Path(listing).read_text().split() if p]
109
+ times = {"ort": [], **{name: [] for name in live}}
110
+ rows = {name: {"frames": 0, "max_person_diff": 0.0, "max_score_diff": 0.0, "mismatches": 0,
111
+ "max_box_shift": 0.0, "floor_0.05_mismatches": 0, "mismatch_frames": []} for name in live}
112
+ persons = 0
113
+ for i, path in enumerate(paths):
114
+ blob, _w, _h = preprocess(Path(path).read_bytes(), size, size)
115
+ blob = np.ascontiguousarray(blob)
116
+ if i < a.warmup:
117
+ outputs(ref, blob)
118
+ for s in live.values():
119
+ outputs(s, blob)
120
+ t = time.perf_counter(); reference = outputs(ref, blob); times["ort"].append((time.perf_counter() - t) * 1000)
121
+ for name, s in live.items():
122
+ t = time.perf_counter(); candidate = outputs(s, blob); times[name].append((time.perf_counter() - t) * 1000)
123
+ pd, sd, mm, shift, low = grade(reference, candidate)
124
+ row = rows[name]
125
+ row["frames"] += 1
126
+ row["max_person_diff"] = max(row["max_person_diff"], pd)
127
+ row["max_score_diff"] = max(row["max_score_diff"], sd)
128
+ row["mismatches"] += mm
129
+ row["max_box_shift"] = max(row["max_box_shift"], shift)
130
+ row["floor_0.05_mismatches"] += low
131
+ if mm:
132
+ row["mismatch_frames"].append(path)
133
+ persons += int((_sigmoid(reference[1][0])[:, person] > 0.25).sum())
134
+ summary = {"frames": len(paths), "ort_persons_at_0.25": persons,
135
+ "ms_p50": {k: round(statistics.median(v), 1) for k, v in times.items() if v},
136
+ "engines": {}}
137
+ for name, row in rows.items():
138
+ row["passes"] = row["mismatches"] == 0 and row["max_person_diff"] <= 1e-3 and row["max_box_shift"] <= 0.01
139
+ row["max_box_shift"] = float(f"{row['max_box_shift']:.3e}")
140
+ row["speedup_vs_ort_p50"] = round(statistics.median(times["ort"]) / statistics.median(times[name]), 3)
141
+ row["max_person_diff"] = float(f"{row['max_person_diff']:.3e}")
142
+ row["max_score_diff"] = float(f"{row['max_score_diff']:.3e}")
143
+ row["mismatch_frames"] = row["mismatch_frames"][:20]
144
+ summary["engines"][name] = row
145
+ result["sets"][Path(listing).name] = summary
146
+ print(json.dumps({Path(listing).name: summary}), flush=True)
147
+ Path(a.out).write_text(json.dumps(result, indent=1))
148
+ return 0
149
+
150
+
151
+ if __name__ == "__main__":
152
+ sys.exit(main())
@@ -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.befcd33e8e35"
13
+ version = "0.2.0+src.740c359ba4b7"
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
 
@@ -52,6 +52,15 @@ cuda = [
52
52
  "nvidia-nvjitlink==13.4.92",
53
53
  ]
54
54
 
55
+ # Plan v7 W8: OpenVINO used directly (the ONNX Runtime OpenVINO plug-in on the
56
+ # pinned ORT does not compile the shipped RF-DETR artifact), installed BESIDE
57
+ # [cpu] on an Intel host with no NVIDIA card: ORT stays loaded as the reference
58
+ # every OpenVINO engine is probed against, and the fallback. Measured on the
59
+ # shipped artifact 2026-11-16: +36% frames/s on the CPU, person scores within
60
+ # 0.0004 of ORT. Serves only when the server asks (CCTV_DETECTOR_BACKEND) and the
61
+ # engine's own parity probe passed on that host.
62
+ openvino = ["openvino==2026.4.1", "openvino-telemetry==2025.2.0"]
63
+
55
64
  [tool.uv]
56
65
  conflicts = [[{extra = "cpu"}, {extra = "cuda"}]]
57
66
 
@@ -90,6 +90,38 @@ class ActorTrackingTest(unittest.TestCase):
90
90
  self.assertNotEqual(a["epoch"], c["epoch"])
91
91
  self.assertNotEqual(a["id"], c["id"])
92
92
 
93
+ def test_track_record_counts_observations_never_predictions(self):
94
+ who = self.confirmed()
95
+ self.track(1400, [])
96
+ record, = self.journal.track_records("cam_a")
97
+ self.assertEqual((record.actor_id, record.epoch, record.kind), (who["id"], who["epoch"], "person"))
98
+ self.assertEqual((record.first_seen_ms, record.last_seen_ms, record.frames), (1200, 1200, 1))
99
+ self.track(1600, [person(14, .9)])
100
+ record, = self.journal.track_records("cam_a")
101
+ self.assertEqual((record.first_seen_ms, record.last_seen_ms, record.frames), (1200, 1600, 2))
102
+ self.assertEqual(record.box, (14, 20, 40, 100))
103
+ self.assertEqual(record.confidence, .9)
104
+ self.assertIsNone(record.face)
105
+ self.assertEqual(self.journal.track_records("cam_b"), [])
106
+
107
+ def test_track_record_and_its_face_end_with_the_actor_and_survive_restart(self):
108
+ from trillboards_detector.track_records import FaceSlot
109
+ who = self.confirmed()
110
+ face = FaceSlot("face_model", "c" * 64, (0.6, 0.8), "frame_1200", 1200, .7)
111
+ self.assertTrue(self.journal.record_face("cam_a", who["id"], face))
112
+ self.assertFalse(self.journal.record_face("cam_a", "pa_unknown", face))
113
+ self.journal.close()
114
+ self.journal = ActorTrackerJournal(self.path)
115
+ record, = self.journal.track_records("cam_a")
116
+ self.assertEqual(record.face, face)
117
+ self.assertEqual(record.to_wire()["face"]["dims"], 2)
118
+ with self.assertRaises(ValueError):
119
+ FaceSlot.from_wire({**face.to_wire(), "dims": 3})
120
+ reset = self.track(10000)
121
+ self.assertIn(who, reset["endedActors"])
122
+ self.assertEqual(self.journal.track_records("cam_a"), [])
123
+ self.assertFalse(self.journal.record_face("cam_a", who["id"], face))
124
+
93
125
  def test_restart_and_bounded_history_preserve_actor(self):
94
126
  who = self.confirmed()
95
127
  for at in range(1400, 62000, 200):
@@ -81,6 +81,17 @@ class ActorTrackingWireTest(unittest.TestCase):
81
81
  with urllib.request.urlopen(self.base+"/health") as r:
82
82
  self.assertEqual(json.load(r)["actor_tracking"]["state"],"ready")
83
83
 
84
+ def test_tracked_response_serves_the_live_actor_record_outside_the_proof(self):
85
+ _,first=self.post(1000)
86
+ self.assertEqual(first["tracks"],[])
87
+ _,second=self.post(1200)
88
+ actor=second["tracking"]["actors"][0]["actor"]
89
+ self.assertEqual(second["tracks"],[{"version":1,"actorId":actor["id"],"kind":"person",
90
+ "epoch":actor["epoch"],"firstSeenAtMs":1200,"lastSeenAtMs":1200,"frames":1,
91
+ "box":{"x":10,"y":20,"width":30,"height":80},"confidence":.95,"face":None}])
92
+ self.assertEqual(self.post(1400)[1]["tracks"][0]["frames"],2)
93
+ self.assertNotIn("tracks",self.post(1600,tracking=None)[1])
94
+
84
95
  def test_invalid_or_unavailable_tracking_never_becomes_an_empty_scene(self):
85
96
  for change in [{"tracking":{}},{"min_confidence":.5},{"expected_model":None},
86
97
  {"classes":["cell phone"]}]:
@@ -0,0 +1,251 @@
1
+ """Engine policy (plan v7 W8): which engine answers a frame, and when OpenVINO may.
2
+
3
+ Hermetic: fake sessions stand in for ORT and OpenVINO, so the policy is tested
4
+ without a model, a GPU or the openvino wheel. Parity on real frames is measured
5
+ by the bench (`bench/openvino_parity.py`), not here.
6
+ """
7
+ import importlib.util
8
+ import threading
9
+ import time
10
+ import unittest
11
+ from unittest import mock
12
+
13
+ # guards-fast runs this directory on a bare interpreter (no numpy). The engine
14
+ # policy itself imports no numpy, so TestPolicy runs there; the fake sessions
15
+ # answer numpy arrays, so TestEngines runs in the lanes that install the
16
+ # detector's [cpu] extra.
17
+ HAVE_NUMPY = importlib.util.find_spec("numpy") is not None
18
+ if HAVE_NUMPY:
19
+ import numpy as np
20
+
21
+ from trillboards_detector import execution
22
+ from trillboards_detector.execution import BackendPolicy, DetectorEngines, EngineUnavailable
23
+
24
+ OUTPUTS = ("dets", "labels")
25
+
26
+
27
+ class FakeSession:
28
+ def __init__(self, name, offset=0.0, fail=False, delay=0.0, precision="f32"):
29
+ self.name, self.offset, self.fail, self.delay, self.precision = name, offset, fail, delay, precision
30
+ self.calls = 0
31
+ self.device_name = name
32
+
33
+ def get_providers(self):
34
+ return [self.name]
35
+
36
+ def run(self, output_names, feeds):
37
+ self.calls += 1
38
+ if self.delay:
39
+ time.sleep(self.delay)
40
+ if self.fail:
41
+ raise RuntimeError("device lost")
42
+ (blob,) = feeds.values()
43
+ value = float(np.asarray(blob).ravel()[0])
44
+ return [np.full((1, 4, 4), value, np.float32), np.full((1, 4, 3), value + self.offset, np.float32)]
45
+
46
+
47
+ def compare(reference, candidate):
48
+ diff = float(np.abs(reference[1] - candidate[1]).max())
49
+ return {"person_diff": diff, "score_diff": diff, "mismatches": 1 if diff > 0.1 else 0}
50
+
51
+
52
+ def engines(mode, factory, probe_frames=3):
53
+ ort = FakeSession("CPUExecutionProvider")
54
+ with mock.patch.object(execution, "openvino_version", return_value="2026.4.1"):
55
+ set_ = DetectorEngines(ort, lambda: b"model", BackendPolicy(mode), compare,
56
+ log=lambda *_: None, session_factory=factory, probe_frames=probe_frames)
57
+ return set_, ort
58
+
59
+
60
+ def frame(v=1.0):
61
+ return {"input": np.full((1, 3, 2, 2), v, np.float32)}
62
+
63
+
64
+ def wait_built(set_, names=("openvino_cpu", "openvino_gpu"), timeout=5.0):
65
+ deadline = time.time() + timeout
66
+ while time.time() < deadline:
67
+ snap = set_.snapshot()["engines"]
68
+ if all(n in snap for n in names) and not set_._building:
69
+ return snap
70
+ time.sleep(0.01)
71
+ raise AssertionError(f"engines not built: {set_.snapshot()}")
72
+
73
+
74
+ class TestPolicy(unittest.TestCase):
75
+ def test_modes_are_closed(self):
76
+ policy = BackendPolicy()
77
+ self.assertEqual(policy.mode, "ort")
78
+ self.assertTrue(policy.set("openvino"))
79
+ self.assertFalse(policy.set("openvino"))
80
+ with self.assertRaises(ValueError):
81
+ policy.set("tensorrt")
82
+ with self.assertRaises(ValueError):
83
+ BackendPolicy("gpu")
84
+
85
+
86
+ @unittest.skipUnless(HAVE_NUMPY, "numpy is not installed: the fake sessions answer numpy arrays")
87
+ class TestEngines(unittest.TestCase):
88
+ def test_ort_mode_builds_nothing_and_answers_on_ort(self):
89
+ factory = mock.Mock(side_effect=AssertionError("must not build"))
90
+ set_, ort = engines("ort", factory)
91
+ outputs, name = set_.run(OUTPUTS, frame())
92
+ self.assertEqual(name, "ort")
93
+ self.assertEqual(ort.calls, 1)
94
+ self.assertEqual(set_.snapshot()["serving"], ["ort"])
95
+ self.assertEqual(set_.snapshot()["engines"].keys(), {"ort"})
96
+ factory.assert_not_called()
97
+
98
+ def test_openvino_serves_only_after_its_probe_passed(self):
99
+ built = {}
100
+
101
+ def factory(model, device, precision, threads=None):
102
+ built[device] = FakeSession(f"ov-{device}", precision=precision)
103
+ return built[device]
104
+
105
+ set_, ort = engines("openvino", factory)
106
+ set_.run(OUTPUTS, frame()) # starts the background build; ORT answers
107
+ wait_built(set_)
108
+ for i in range(10): # probe frames: ORT answers AND each engine is compared
109
+ if set_.snapshot()["engines"]["openvino_cpu"]["state"] != "probing":
110
+ break
111
+ _, name = set_.run(OUTPUTS, frame(i))
112
+ self.assertEqual(name, "ort")
113
+ snap = set_.snapshot()
114
+ self.assertEqual(snap["engines"]["openvino_cpu"]["state"], "passed")
115
+ self.assertEqual(snap["engines"]["openvino_cpu"]["probe"]["frames"], 3)
116
+ self.assertEqual(snap["engines"]["openvino_cpu"]["probe"]["mismatches"], 0)
117
+ _, name = set_.run(OUTPUTS, frame())
118
+ self.assertEqual(name, "openvino_cpu")
119
+ self.assertEqual(set_.snapshot()["serving"], ["openvino_cpu"]) # GPU passed too, but only openvino_gpu uses it
120
+
121
+ def test_a_probe_that_misses_parity_never_serves(self):
122
+ set_, _ = engines("openvino", lambda m, d, p, threads=None: FakeSession(d, offset=0.5, precision=p))
123
+ set_.run(OUTPUTS, frame())
124
+ wait_built(set_)
125
+ for i in range(3):
126
+ set_.run(OUTPUTS, frame(i))
127
+ snap = set_.snapshot()["engines"]["openvino_cpu"]
128
+ self.assertEqual(snap["state"], "failed")
129
+ self.assertIn("parity", snap["reason"])
130
+ self.assertEqual(set_.run(OUTPUTS, frame())[1], "ort")
131
+
132
+ def test_probe_mode_reports_but_never_serves(self):
133
+ set_, _ = engines("openvino_probe", lambda m, d, p, threads=None: FakeSession(d, precision=p))
134
+ set_.run(OUTPUTS, frame())
135
+ wait_built(set_)
136
+ for i in range(4):
137
+ set_.run(OUTPUTS, frame(i))
138
+ self.assertEqual(set_.snapshot()["engines"]["openvino_cpu"]["state"], "passed")
139
+ self.assertEqual(set_.run(OUTPUTS, frame())[1], "ort")
140
+
141
+ def test_gpu_fp16_miss_is_rebuilt_in_fp32_and_then_serves_as_second_engine(self):
142
+ def factory(model, device, precision, threads=None):
143
+ if device == "GPU" and precision == "f16":
144
+ return FakeSession("gpu-f16", offset=0.01, precision="f16") # 1e-2 > the 1e-3 person bar
145
+ return FakeSession(f"{device}-{precision}", precision=precision, delay=0.05)
146
+
147
+ set_, _ = engines("openvino_gpu", factory)
148
+ set_.run(OUTPUTS, frame())
149
+ wait_built(set_)
150
+ for i in range(3):
151
+ set_.run(OUTPUTS, frame(i))
152
+ gpu = set_.snapshot()["engines"]["openvino_gpu"]
153
+ self.assertIn(gpu["state"], ("failed", "probing"))
154
+ deadline = time.time() + 5
155
+ while set_.snapshot()["engines"]["openvino_gpu"].get("precision") != "f32" and time.time() < deadline:
156
+ time.sleep(0.01)
157
+ for i in range(3):
158
+ set_.run(OUTPUTS, frame(i))
159
+ gpu = set_.snapshot()["engines"]["openvino_gpu"]
160
+ self.assertEqual((gpu["state"], gpu["precision"]), ("passed", "f32"))
161
+ self.assertEqual(gpu["probe"]["previous"]["precision"], "f16")
162
+ self.assertEqual(set_.snapshot()["serving"], ["openvino_cpu", "openvino_gpu"])
163
+ # Two frames at once: one on each engine.
164
+ names = []
165
+ threads = [threading.Thread(target=lambda: names.append(set_.run(OUTPUTS, frame())[1])) for _ in range(2)]
166
+ [t.start() for t in threads]
167
+ [t.join() for t in threads]
168
+ self.assertEqual(sorted(names), ["openvino_cpu", "openvino_gpu"])
169
+
170
+ def test_a_run_time_failure_is_answered_by_ort_and_disables_the_engine(self):
171
+ sessions = {}
172
+
173
+ def factory(model, device, precision, threads=None):
174
+ sessions[device] = FakeSession(device, precision=precision)
175
+ return sessions[device]
176
+
177
+ set_, ort = engines("openvino", factory)
178
+ set_.run(OUTPUTS, frame())
179
+ wait_built(set_)
180
+ for i in range(3):
181
+ set_.run(OUTPUTS, frame(i))
182
+ sessions["CPU"].fail = True
183
+ before = ort.calls
184
+ outputs, name = set_.run(OUTPUTS, frame(7.0))
185
+ self.assertEqual(float(outputs[0].ravel()[0]), 7.0) # the same input, answered
186
+ self.assertEqual(ort.calls, before + 1)
187
+ self.assertEqual(set_.snapshot()["engines"]["openvino_cpu"]["state"], "failed")
188
+ self.assertEqual(set_.run(OUTPUTS, frame())[1], "ort")
189
+
190
+ def test_a_host_without_openvino_reports_unavailable_and_stays_on_ort(self):
191
+ ort = FakeSession("CPUExecutionProvider")
192
+ with mock.patch.object(execution, "openvino_version", return_value=None):
193
+ set_ = DetectorEngines(ort, lambda: b"model", BackendPolicy("openvino_gpu"), compare,
194
+ log=lambda *_: None, session_factory=mock.Mock())
195
+ self.assertEqual(set_.run(OUTPUTS, frame())[1], "ort")
196
+ snap = set_.snapshot()["engines"]
197
+ self.assertEqual(snap["openvino_cpu"]["state"], "unavailable")
198
+ self.assertEqual(snap["openvino_gpu"]["state"], "unavailable")
199
+
200
+ def test_a_missing_device_is_unavailable_not_failed(self):
201
+ def factory(model, device, precision, threads=None):
202
+ if device == "GPU":
203
+ raise EngineUnavailable("GPU is not an OpenVINO device on this host (['CPU'])")
204
+ return FakeSession(device, precision=precision)
205
+
206
+ set_, _ = engines("openvino_gpu", factory)
207
+ set_.run(OUTPUTS, frame())
208
+ snap = wait_built(set_)
209
+ self.assertEqual(snap["openvino_gpu"]["state"], "unavailable")
210
+ self.assertIn("not an OpenVINO device", snap["openvino_gpu"]["reason"])
211
+
212
+ def test_a_low_floor_request_stays_on_ort_even_when_openvino_serves(self):
213
+ """A box method that asks at a 0.05 floor (the uniform rule's fixtures) keeps ORT's answers."""
214
+ set_, _ = engines("openvino", lambda m, d, p, threads=None: FakeSession(d, precision=p))
215
+ set_.run(OUTPUTS, frame())
216
+ wait_built(set_)
217
+ for i in range(10):
218
+ if set_.snapshot()["engines"]["openvino_cpu"]["state"] != "probing":
219
+ break
220
+ set_.run(OUTPUTS, frame(i))
221
+ self.assertEqual(set_.run(OUTPUTS, frame(), min_confidence=0.25)[1], "openvino_cpu")
222
+ self.assertEqual(set_.run(OUTPUTS, frame(), min_confidence=0.05)[1], "ort")
223
+
224
+ def test_a_box_that_moves_past_the_tolerance_fails_the_probe(self):
225
+ def shifted(reference, candidate):
226
+ return {"person_diff": 0.0, "score_diff": 0.0, "mismatches": 0, "box_shift": 0.02}
227
+
228
+ ort = FakeSession("CPUExecutionProvider")
229
+ with mock.patch.object(execution, "openvino_version", return_value="2026.4.1"):
230
+ set_ = DetectorEngines(ort, lambda: b"model", BackendPolicy("openvino"), shifted, log=lambda *_: None,
231
+ session_factory=lambda m, d, p, threads=None: FakeSession(d, precision=p), probe_frames=2)
232
+ set_.run(OUTPUTS, frame())
233
+ wait_built(set_)
234
+ for i in range(4):
235
+ set_.run(OUTPUTS, frame(i))
236
+ cpu = set_.snapshot()["engines"]["openvino_cpu"]
237
+ self.assertEqual(cpu["state"], "failed")
238
+ self.assertIn("box shift", cpu["reason"])
239
+
240
+ def test_busy_time_is_accounted_per_engine(self):
241
+ set_, _ = engines("ort", mock.Mock())
242
+ for _ in range(3):
243
+ set_.run(OUTPUTS, frame())
244
+ ort = set_.snapshot()["engines"]["ort"]
245
+ self.assertEqual(ort["calls"], 3)
246
+ self.assertGreaterEqual(ort["busy_ms"], 0.0)
247
+ self.assertIsNotNone(ort["ms_p50"])
248
+
249
+
250
+ if __name__ == "__main__":
251
+ unittest.main()
@@ -1079,3 +1079,46 @@ class ActorActionServerTest(ServerCase):
1079
1079
 
1080
1080
  if __name__ == "__main__":
1081
1081
  unittest.main()
1082
+
1083
+
1084
+ class TestBackendSelection(ServerCase):
1085
+ """POST /backend (plan v7 W8): the connector relays CCTV_DETECTOR_BACKEND here."""
1086
+
1087
+ def loader(self):
1088
+ from trillboards_detector.execution import BackendPolicy
1089
+
1090
+ class PolicyLoader(FakeLoader):
1091
+ def __init__(self):
1092
+ super().__init__()
1093
+ self.backend_policy = BackendPolicy("ort")
1094
+
1095
+ def info(self):
1096
+ return {"state": "ready", "model": "rfdetr_medium",
1097
+ "backend": {"requested": self.backend_policy.mode, "serving": ["ort"], "engines": {}}}
1098
+
1099
+ return PolicyLoader()
1100
+
1101
+ def test_a_valid_mode_is_taken_and_reported(self):
1102
+ loader = self.loader()
1103
+ url = self.serve(loader)
1104
+ status, body = self.request(f"{url}/backend", method="POST", body={"version": 1, "mode": "openvino"})
1105
+ self.assertEqual(status, 200)
1106
+ self.assertEqual(body["mode"], "openvino")
1107
+ self.assertEqual(body["backend"]["requested"], "openvino")
1108
+ self.assertEqual(loader.backend_policy.mode, "openvino")
1109
+
1110
+ def test_anything_else_is_refused_and_changes_nothing(self):
1111
+ loader = self.loader()
1112
+ url = self.serve(loader)
1113
+ for bad in ({"version": 1, "mode": "tensorrt"}, {"version": 2, "mode": "ort"}, {"mode": "ort"},
1114
+ {"version": 1, "mode": "ort", "extra": True}, ["ort"]):
1115
+ status, _ = self.request(f"{url}/backend", method="POST", body=bad)
1116
+ self.assertEqual(status, 400, bad)
1117
+ status, _ = self.request(f"{url}/backend", method="POST", raw_body=b"not json")
1118
+ self.assertEqual(status, 400)
1119
+ self.assertEqual(loader.backend_policy.mode, "ort")
1120
+
1121
+ def test_a_build_without_selection_says_so(self):
1122
+ url = self.serve(FakeLoader())
1123
+ status, _ = self.request(f"{url}/backend", method="POST", body={"version": 1, "mode": "ort"})
1124
+ self.assertEqual(status, 503)
@@ -247,20 +247,33 @@ class TestInstallersCannotAcceptAStaleInstall(unittest.TestCase):
247
247
  with self.subTest(installer=path.name + f" ({path.parent.name})"):
248
248
  text = path.read_text(encoding="utf-8")
249
249
  install_lines = self._install_invocations(text)
250
- # TWO: the ordinary install, and the uninstall-and-retry repair
251
- # rung. A file that grew a third would be installing somewhere
252
- # this test never looked.
250
+ # TWO: on macOS and Linux the ordinary install and the
251
+ # uninstall-and-retry repair rung; on Windows one line per cache
252
+ # mode (online, offline) that every install goes through. A file
253
+ # that grew a third would be installing somewhere this test
254
+ # never looked.
253
255
  self.assertEqual(
254
256
  len(install_lines),
255
257
  2,
256
258
  f"{path} runs {len(install_lines)} `uv tool install` invocations, expected 2",
257
259
  )
258
- for line in install_lines:
260
+ online = [line for line in install_lines if "--offline" not in line]
261
+ self.assertTrue(online, f"{path} never rebuilds the staged package online")
262
+ for line in online:
259
263
  self.assertIn(
260
264
  "--refresh-package trillboards-detector",
261
265
  line,
262
266
  f"{path} installs without busting uv's cached wheel: {line.strip()}",
263
267
  )
268
+ # Windows replaces the serving detector OFFLINE, after a trial
269
+ # install that carried --refresh-package rebuilt the staged bytes
270
+ # into uv's cache. Current uv refuses --offline with any refresh,
271
+ # and the offline install takes the trial's rebuilt wheel
272
+ # (measured on uv 0.11.15, 2026-11-17: same path, version
273
+ # unchanged, new bytes installed with the network dead).
274
+ for line in install_lines:
275
+ if "--offline" in line:
276
+ self.assertNotIn("--refresh", line, f"{path}: uv refuses --offline with a refresh: {line.strip()}")
264
277
 
265
278
  def test_every_installer_verifies_what_it_installed(self) -> None:
266
279
  for path in self.INSTALLERS: