@trillboards/edge-sdk 0.2.191 → 0.2.194
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.
- package/deploy/detector/pyproject.toml +1 -1
- package/deploy/detector/tests/test_detection_colors_wire.py +134 -0
- package/deploy/detector/trillboards_detector/_version.py +2 -2
- package/deploy/detector/trillboards_detector/colors.py +85 -0
- package/deploy/detector/trillboards_detector/server.py +27 -0
- package/deploy/macos/apply-update.sh +254 -104
- package/deploy/macos/ci/connector-stand-in.js +42 -0
- package/deploy/macos/ci/helper-stand-in.sh +23 -0
- package/deploy/macos/ci/launchd-matrix.js +39 -0
- package/deploy/macos/ci/lib.sh +339 -0
- package/deploy/macos/ci/test-field-self-update.sh +153 -0
- package/deploy/macos/ci/test-update-launchd-boundary.sh +260 -0
- package/dist/DetectionColors.d.ts +44 -0
- package/dist/DetectionColors.d.ts.map +1 -0
- package/dist/DetectionColors.js +51 -0
- package/dist/DetectionColors.js.map +1 -0
- package/dist/DetectorFields.d.ts +213 -0
- package/dist/DetectorFields.d.ts.map +1 -0
- package/dist/DetectorFields.js +205 -0
- package/dist/DetectorFields.js.map +1 -0
- package/dist/EdgeAgent.js +1 -1
- package/dist/LocalDetectorClient.d.ts +4 -1
- package/dist/LocalDetectorClient.d.ts.map +1 -1
- package/dist/LocalDetectorClient.js +24 -1
- package/dist/LocalDetectorClient.js.map +1 -1
- package/dist/RetainedFieldContract.d.ts +1 -1
- package/dist/RetainedFieldContract.d.ts.map +1 -1
- package/dist/RetainedFieldContract.js +8 -1
- package/dist/RetainedFieldContract.js.map +1 -1
- package/dist/ReviewModelUse.d.ts.map +1 -1
- package/dist/ReviewModelUse.js +4 -0
- package/dist/ReviewModelUse.js.map +1 -1
- package/dist/ScopedVlmReview.d.ts +41 -0
- package/dist/ScopedVlmReview.d.ts.map +1 -1
- package/dist/ScopedVlmReview.js +160 -8
- package/dist/ScopedVlmReview.js.map +1 -1
- package/dist/SensingProfileClient.d.ts +3 -0
- package/dist/SensingProfileClient.d.ts.map +1 -1
- package/dist/SensingProfileClient.js +2 -0
- package/dist/SensingProfileClient.js.map +1 -1
- package/dist/connectorUpdate.d.ts.map +1 -1
- package/dist/connectorUpdate.js +19 -0
- package/dist/connectorUpdate.js.map +1 -1
- package/dist/diagnose.d.ts +1 -1
- package/dist/diagnose.js +1 -1
- package/dist/localSensing.d.ts +8 -0
- package/dist/localSensing.d.ts.map +1 -1
- package/dist/localSensing.js +86 -4
- package/dist/localSensing.js.map +1 -1
- package/package.json +1 -1
- package/retained/ProfileAuthorityClient.cjs +2 -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.
|
|
13
|
+
version = "0.2.0+src.abd83bba2f51"
|
|
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
|
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"""Real HTTP. Uniform-colour share of each confident person box, on the original frame."""
|
|
2
|
+
import importlib.util
|
|
3
|
+
import unittest
|
|
4
|
+
_missing = [name for name in ["PIL", "numpy"] if importlib.util.find_spec(name) is None]
|
|
5
|
+
if _missing:
|
|
6
|
+
raise unittest.SkipTest("detector[cpu] dependencies absent: " + ", ".join(_missing))
|
|
7
|
+
import base64
|
|
8
|
+
import hashlib
|
|
9
|
+
import io
|
|
10
|
+
import json
|
|
11
|
+
import threading
|
|
12
|
+
import urllib.error
|
|
13
|
+
import urllib.request
|
|
14
|
+
|
|
15
|
+
from PIL import Image
|
|
16
|
+
from trillboards_detector.colors import detection_color_fractions
|
|
17
|
+
from trillboards_detector.detections import Detection, DetectionBatch
|
|
18
|
+
from trillboards_detector.server import DetectorServer
|
|
19
|
+
|
|
20
|
+
SHA = "c" * 64
|
|
21
|
+
UNIFORM = {"version": 1, "minScore": .25, "maxBoxes": 32, "hue": [[0, 10], [240, 255]],
|
|
22
|
+
"minSaturation": 100, "minValue": 60}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class Source:
|
|
26
|
+
state = "ready"
|
|
27
|
+
|
|
28
|
+
def __init__(self, detections):
|
|
29
|
+
self.detections, self.calls = detections, 0
|
|
30
|
+
|
|
31
|
+
def info(self):
|
|
32
|
+
return {"state": "ready"}
|
|
33
|
+
|
|
34
|
+
def resolve_classes(self, classes):
|
|
35
|
+
return set(classes)
|
|
36
|
+
|
|
37
|
+
def detect(self, jpeg, min_confidence, wanted, *, expected_model=None):
|
|
38
|
+
self.calls += 1
|
|
39
|
+
if expected_model != {"modelId": "rfdetr_medium", "sha256": SHA}:
|
|
40
|
+
raise ValueError("unexpected selected artifact")
|
|
41
|
+
return DetectionBatch(list(self.detections), models=["rfdetr_medium"], model_hashes={"rfdetr_medium": SHA})
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def frame():
|
|
45
|
+
"""Left half grey, right half Ace red: a box over either half is all or nothing."""
|
|
46
|
+
image = Image.new("RGB", (160, 120), (128, 128, 128))
|
|
47
|
+
image.paste((210, 20, 30), (80, 0, 160, 120))
|
|
48
|
+
out = io.BytesIO()
|
|
49
|
+
image.save(out, format="JPEG", quality=95)
|
|
50
|
+
return out.getvalue()
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class DetectionColorsWireTest(unittest.TestCase):
|
|
54
|
+
def start(self, detections):
|
|
55
|
+
self.source = Source(detections)
|
|
56
|
+
self.server = DetectorServer(("127.0.0.1", 0), self.source, log_line=lambda _: None)
|
|
57
|
+
self.worker = threading.Thread(target=lambda: self.server.serve_forever(poll_interval=.01), daemon=True)
|
|
58
|
+
self.worker.start()
|
|
59
|
+
self.base = "http://127.0.0.1:" + str(self.server.server_port)
|
|
60
|
+
self.jpeg = frame()
|
|
61
|
+
|
|
62
|
+
def tearDown(self):
|
|
63
|
+
self.server.shutdown()
|
|
64
|
+
self.server.server_close()
|
|
65
|
+
self.worker.join()
|
|
66
|
+
|
|
67
|
+
def post(self, **changes):
|
|
68
|
+
body = {"image_b64": base64.b64encode(self.jpeg).decode(), "classes": ["person", "tv"],
|
|
69
|
+
"min_confidence": .25, "expected_model": {"modelId": "rfdetr_medium", "sha256": SHA},
|
|
70
|
+
"detection_colors": UNIFORM, **changes}
|
|
71
|
+
body = {key: value for key, value in body.items() if value is not None}
|
|
72
|
+
request = urllib.request.Request(self.base + "/detect", data=json.dumps(body).encode(),
|
|
73
|
+
headers={"Content-Type": "application/json"})
|
|
74
|
+
try:
|
|
75
|
+
with urllib.request.urlopen(request, timeout=5) as r:
|
|
76
|
+
return r.status, json.load(r)
|
|
77
|
+
except urllib.error.HTTPError as e:
|
|
78
|
+
return e.code, json.load(e)
|
|
79
|
+
|
|
80
|
+
def test_each_confident_person_box_gets_its_uniform_share_keyed_by_detection_index(self):
|
|
81
|
+
self.start([Detection("tv", .9, 0, 0, 20, 20), Detection("person", .4, 10, 10, 40, 80),
|
|
82
|
+
Detection("person", .9, 100, 10, 40, 80), Detection("person", .2, 90, 0, 60, 100)])
|
|
83
|
+
status, body = self.post()
|
|
84
|
+
self.assertEqual(status, 200)
|
|
85
|
+
colors = body["detection_colors"]
|
|
86
|
+
self.assertEqual(colors["imageSha256"], hashlib.sha256(self.jpeg).hexdigest())
|
|
87
|
+
self.assertEqual(colors["frameSize"], {"width": 160, "height": 120})
|
|
88
|
+
self.assertEqual(colors["request"], UNIFORM)
|
|
89
|
+
# Highest score first; the 0.2 box is below minScore and the tv is not a person.
|
|
90
|
+
self.assertEqual([p["detectionIndex"] for p in colors["people"]], [2, 1])
|
|
91
|
+
self.assertGreater(colors["people"][0]["fraction"], .95)
|
|
92
|
+
self.assertLess(colors["people"][1]["fraction"], .05)
|
|
93
|
+
|
|
94
|
+
def test_the_service_number_is_the_measured_arithmetic(self):
|
|
95
|
+
self.start([Detection("person", .9, 70.6, 5.2, 40.9, 60.4)])
|
|
96
|
+
status, body = self.post()
|
|
97
|
+
self.assertEqual(status, 200)
|
|
98
|
+
_, expected = detection_color_fractions(self.jpeg, [[70.6, 5.2, 40.9, 60.4]], UNIFORM)
|
|
99
|
+
self.assertEqual(body["detection_colors"]["people"][0]["fraction"], expected[0])
|
|
100
|
+
# int() truncation of the float box, as measured: columns 70..110, 9 grey of 40.
|
|
101
|
+
self.assertAlmostEqual(expected[0], 31 / 40, delta=.03)
|
|
102
|
+
|
|
103
|
+
def test_a_frame_with_no_person_has_an_empty_list_not_a_missing_one(self):
|
|
104
|
+
self.start([Detection("tv", .9, 0, 0, 20, 20)])
|
|
105
|
+
status, body = self.post()
|
|
106
|
+
self.assertEqual(status, 200)
|
|
107
|
+
self.assertEqual(body["detection_colors"]["people"], [])
|
|
108
|
+
|
|
109
|
+
def test_an_ambiguous_or_unverified_request_is_refused_before_inference(self):
|
|
110
|
+
self.start([Detection("person", .9, 100, 10, 40, 80)])
|
|
111
|
+
bad = [{"expected_model": None}, {"classes": ["tv"]},
|
|
112
|
+
{"detection_colors": {**UNIFORM, "version": 2}},
|
|
113
|
+
{"detection_colors": {**UNIFORM, "minScore": 0}},
|
|
114
|
+
{"detection_colors": {**UNIFORM, "maxBoxes": 33}},
|
|
115
|
+
{"detection_colors": {**UNIFORM, "hue": []}},
|
|
116
|
+
{"detection_colors": {**UNIFORM, "hue": [[10, 0]]}},
|
|
117
|
+
{"detection_colors": {**UNIFORM, "hue": [[0, 256]]}},
|
|
118
|
+
{"detection_colors": {**UNIFORM, "minSaturation": 99.5}},
|
|
119
|
+
{"detection_colors": {**UNIFORM, "extra": 1}}]
|
|
120
|
+
for change in bad:
|
|
121
|
+
status, body = self.post(**change)
|
|
122
|
+
self.assertEqual(status, 400, change)
|
|
123
|
+
self.assertNotIn("detections", body)
|
|
124
|
+
self.assertEqual(self.source.calls, 0)
|
|
125
|
+
|
|
126
|
+
def test_an_unrequested_colour_task_is_absent(self):
|
|
127
|
+
self.start([Detection("person", .9, 100, 10, 40, 80)])
|
|
128
|
+
status, body = self.post(detection_colors=None)
|
|
129
|
+
self.assertEqual(status, 200)
|
|
130
|
+
self.assertNotIn("detection_colors", body)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
if __name__ == "__main__":
|
|
134
|
+
unittest.main()
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"""Colour fractions inside person detections, on the original frame.
|
|
2
|
+
|
|
3
|
+
A uniform is the cheapest staff cue a ceiling camera sees: the Texas paint
|
|
4
|
+
counter's staff wear the store's red vest or shirt, its customers almost never
|
|
5
|
+
do. The connector decides "is a staff member here" from the share of a
|
|
6
|
+
person's box that is uniform-coloured; this module measures that share and
|
|
7
|
+
nothing else. It runs no model and makes no decision.
|
|
8
|
+
|
|
9
|
+
The arithmetic is the one the 2026-11-04 measurement used
|
|
10
|
+
(docs/runbooks/cctv-detector-tier.md section 5b): Pillow decodes the original
|
|
11
|
+
JPEG, converts it to Pillow's 8-bit HSV, and a pixel counts when its hue is in
|
|
12
|
+
one of the requested ranges and its saturation and value reach the floors. The
|
|
13
|
+
box is the detector's own float box truncated to whole pixels and clipped to
|
|
14
|
+
the frame, exactly as measured, so a re-measurement through this service gives
|
|
15
|
+
the same number to the last digit.
|
|
16
|
+
"""
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import io
|
|
20
|
+
import math
|
|
21
|
+
|
|
22
|
+
#: A request is a closed contract: anything else is refused before decoding.
|
|
23
|
+
MAX_COLOR_BOXES = 32
|
|
24
|
+
MAX_HUE_RANGES = 4
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def validate_color_request(value):
|
|
28
|
+
"""`detection_colors` -> the same dict, or ValueError."""
|
|
29
|
+
if (not isinstance(value, dict)
|
|
30
|
+
or set(value) != {"version", "minScore", "maxBoxes", "hue", "minSaturation", "minValue"}
|
|
31
|
+
or type(value["version"]) is not int or value["version"] != 1
|
|
32
|
+
or isinstance(value["minScore"], bool) or not isinstance(value["minScore"], (int, float))
|
|
33
|
+
or not math.isfinite(value["minScore"]) or not 0 < value["minScore"] <= 1
|
|
34
|
+
or type(value["maxBoxes"]) is not int or not 1 <= value["maxBoxes"] <= MAX_COLOR_BOXES
|
|
35
|
+
or type(value["minSaturation"]) is not int or not 0 <= value["minSaturation"] <= 255
|
|
36
|
+
or type(value["minValue"]) is not int or not 0 <= value["minValue"] <= 255
|
|
37
|
+
or not isinstance(value["hue"], list) or not 1 <= len(value["hue"]) <= MAX_HUE_RANGES
|
|
38
|
+
or any(not isinstance(r, list) or len(r) != 2 or any(type(v) is not int for v in r)
|
|
39
|
+
or not 0 <= r[0] <= r[1] <= 255 for r in value["hue"])):
|
|
40
|
+
raise ValueError(
|
|
41
|
+
"detection_colors needs version 1, minScore in (0, 1], maxBoxes 1..%d, "
|
|
42
|
+
"1..%d hue ranges of whole 0..255 values, and whole saturation/value floors"
|
|
43
|
+
% (MAX_COLOR_BOXES, MAX_HUE_RANGES))
|
|
44
|
+
return {**value, "hue": [list(r) for r in value["hue"]]}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def color_box_indices(detections, request):
|
|
48
|
+
"""Person detections at or above minScore, highest score first, at most maxBoxes."""
|
|
49
|
+
people = [(index, d) for index, d in enumerate(detections)
|
|
50
|
+
if d.label == "person" and d.confidence >= request["minScore"]]
|
|
51
|
+
people.sort(key=lambda item: (-item[1].confidence, item[0]))
|
|
52
|
+
return [index for index, _ in people[:request["maxBoxes"]]]
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def detection_color_fractions(jpeg, boxes, request):
|
|
56
|
+
"""Fraction of each [x, y, width, height] box whose pixels match the request."""
|
|
57
|
+
import numpy as np
|
|
58
|
+
from PIL import Image
|
|
59
|
+
with Image.open(io.BytesIO(jpeg)) as image:
|
|
60
|
+
if image.format != "JPEG" or image.width * image.height > 16777216:
|
|
61
|
+
raise ValueError("colour fractions need a bounded original JPEG")
|
|
62
|
+
hsv = np.asarray(image.convert("HSV")).astype(np.int16)
|
|
63
|
+
height, width = hsv.shape[0], hsv.shape[1]
|
|
64
|
+
hue, sat, val = hsv[..., 0], hsv[..., 1], hsv[..., 2]
|
|
65
|
+
in_hue = np.zeros(hue.shape, dtype=bool)
|
|
66
|
+
for low, high in request["hue"]:
|
|
67
|
+
in_hue |= (hue >= low) & (hue <= high)
|
|
68
|
+
match = in_hue & (sat >= request["minSaturation"]) & (val >= request["minValue"])
|
|
69
|
+
fractions = []
|
|
70
|
+
for x, y, w, h in boxes:
|
|
71
|
+
x0, x1 = int(max(0, x)), int(min(width, x + w))
|
|
72
|
+
y0, y1 = int(max(0, y)), int(min(height, y + h))
|
|
73
|
+
cell = match[y0:max(y1, y0 + 1), x0:max(x1, x0 + 1)]
|
|
74
|
+
fractions.append(round(float(cell.mean()), 4) if cell.size else 0.0)
|
|
75
|
+
return (width, height), fractions
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def build_color_response(request, indices, fractions, image_sha256, frame_size):
|
|
79
|
+
if len(indices) != len(fractions) or any(
|
|
80
|
+
isinstance(f, bool) or not isinstance(f, float) or not 0 <= f <= 1 for f in fractions):
|
|
81
|
+
raise ValueError("colour fractions differ from the requested person boxes")
|
|
82
|
+
return {"version": 1, "imageSha256": image_sha256,
|
|
83
|
+
"frameSize": {"width": frame_size[0], "height": frame_size[1]},
|
|
84
|
+
"request": request,
|
|
85
|
+
"people": [{"detectionIndex": i, "fraction": f} for i, f in zip(indices, fractions)]}
|
|
@@ -38,6 +38,7 @@ from .actor_tracking import TrackingError, validate_tracking_request
|
|
|
38
38
|
from .actor_identity import MODEL_ID as APPEARANCE_MODEL_ID, MODEL_SHA256 as APPEARANCE_SHA256, PREPROCESSING as APPEARANCE_PREPROCESSING, checked_appearance
|
|
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
|
+
from .colors import validate_color_request
|
|
41
42
|
from ._version import VERSION
|
|
42
43
|
from .errors import DetectorNotReady, ModelVerificationError, UnknownClasses
|
|
43
44
|
|
|
@@ -273,6 +274,17 @@ def parse_detection_pose(raw: bytes):
|
|
|
273
274
|
return value
|
|
274
275
|
|
|
275
276
|
|
|
277
|
+
def parse_detection_colors(raw: bytes):
|
|
278
|
+
"""`detection_colors`: uniform-colour share of each confident person box."""
|
|
279
|
+
body = json.loads(raw.decode("utf-8"))
|
|
280
|
+
if "detection_colors" not in body:
|
|
281
|
+
return None
|
|
282
|
+
try:
|
|
283
|
+
return validate_color_request(body["detection_colors"])
|
|
284
|
+
except ValueError as err:
|
|
285
|
+
raise WireError(400, str(err)) from None
|
|
286
|
+
|
|
287
|
+
|
|
276
288
|
def parse_appearance_model(raw: bytes):
|
|
277
289
|
body = json.loads(raw.decode("utf-8"))
|
|
278
290
|
if "appearance_model" not in body:
|
|
@@ -449,6 +461,7 @@ class DetectorHandler(BaseHTTPRequestHandler):
|
|
|
449
461
|
pose_model = parse_pose_model(raw)
|
|
450
462
|
detection_pose = parse_detection_pose(raw)
|
|
451
463
|
appearance_model = parse_appearance_model(raw)
|
|
464
|
+
detection_colors = parse_detection_colors(raw)
|
|
452
465
|
tracking = json.loads(raw.decode("utf-8")).get("tracking")
|
|
453
466
|
if detection_pose is not None and (pose_model is None or tracking is not None):
|
|
454
467
|
raise WireError(400, "pose_detections needs a pinned pose_model and no tracking")
|
|
@@ -480,6 +493,12 @@ class DetectorHandler(BaseHTTPRequestHandler):
|
|
|
480
493
|
if requested is not None and "person" not in requested:
|
|
481
494
|
raise WireError(400, "pose_detections requires person detections")
|
|
482
495
|
frame_size = original_frame_size(jpeg)
|
|
496
|
+
if detection_colors is not None:
|
|
497
|
+
if expected_model is None:
|
|
498
|
+
raise WireError(400, "detection_colors requires a verified selected detector")
|
|
499
|
+
if requested is not None and "person" not in requested:
|
|
500
|
+
raise WireError(400, "detection_colors requires person detections")
|
|
501
|
+
original_frame_size(jpeg)
|
|
483
502
|
except WireError as err:
|
|
484
503
|
self._json(err.status, {"error": err.message})
|
|
485
504
|
return
|
|
@@ -549,6 +568,14 @@ class DetectorHandler(BaseHTTPRequestHandler):
|
|
|
549
568
|
boxes.append([detection.x, detection.y, detection.x + detection.width, detection.y + detection.height])
|
|
550
569
|
result = loader.estimate_pose(jpeg, boxes, pose_model)
|
|
551
570
|
body["pose"] = build_pose_response(result, body["tracking"], frame_size)
|
|
571
|
+
if detection_colors is not None:
|
|
572
|
+
from .colors import build_color_response, color_box_indices, detection_color_fractions
|
|
573
|
+
indices = color_box_indices(batch.detections, detection_colors)
|
|
574
|
+
size, fractions = detection_color_fractions(
|
|
575
|
+
jpeg, [[d.x, d.y, d.width, d.height] for d in (batch.detections[i] for i in indices)],
|
|
576
|
+
detection_colors)
|
|
577
|
+
body["detection_colors"] = build_color_response(
|
|
578
|
+
detection_colors, indices, fractions, hashlib.sha256(jpeg).hexdigest(), size)
|
|
552
579
|
except ModelVerificationError as err:
|
|
553
580
|
self._json(409, {"error": str(err)})
|
|
554
581
|
return
|