@trillboards/edge-sdk 0.2.190 → 0.2.193

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 (43) hide show
  1. package/deploy/detector/pyproject.toml +1 -1
  2. package/deploy/detector/tests/test_detection_colors_wire.py +134 -0
  3. package/deploy/detector/trillboards_detector/_version.py +2 -2
  4. package/deploy/detector/trillboards_detector/colors.py +85 -0
  5. package/deploy/detector/trillboards_detector/server.py +27 -0
  6. package/deploy/litertlm/test_trillboards_litertlm_shim.py +8 -2
  7. package/deploy/litertlm/trillboards_litertlm_shim.py +3 -3
  8. package/dist/DetectionColors.d.ts +44 -0
  9. package/dist/DetectionColors.d.ts.map +1 -0
  10. package/dist/DetectionColors.js +51 -0
  11. package/dist/DetectionColors.js.map +1 -0
  12. package/dist/DetectorFields.d.ts +213 -0
  13. package/dist/DetectorFields.d.ts.map +1 -0
  14. package/dist/DetectorFields.js +205 -0
  15. package/dist/DetectorFields.js.map +1 -0
  16. package/dist/EdgeAgent.js +1 -1
  17. package/dist/LocalDetectorClient.d.ts +4 -1
  18. package/dist/LocalDetectorClient.d.ts.map +1 -1
  19. package/dist/LocalDetectorClient.js +24 -1
  20. package/dist/LocalDetectorClient.js.map +1 -1
  21. package/dist/RetainedFieldContract.d.ts +1 -1
  22. package/dist/RetainedFieldContract.d.ts.map +1 -1
  23. package/dist/RetainedFieldContract.js +8 -1
  24. package/dist/RetainedFieldContract.js.map +1 -1
  25. package/dist/ReviewModelUse.d.ts.map +1 -1
  26. package/dist/ReviewModelUse.js +4 -0
  27. package/dist/ReviewModelUse.js.map +1 -1
  28. package/dist/ScopedVlmReview.d.ts +41 -0
  29. package/dist/ScopedVlmReview.d.ts.map +1 -1
  30. package/dist/ScopedVlmReview.js +160 -8
  31. package/dist/ScopedVlmReview.js.map +1 -1
  32. package/dist/SensingProfileClient.d.ts +3 -0
  33. package/dist/SensingProfileClient.d.ts.map +1 -1
  34. package/dist/SensingProfileClient.js +2 -0
  35. package/dist/SensingProfileClient.js.map +1 -1
  36. package/dist/diagnose.d.ts +1 -1
  37. package/dist/diagnose.js +1 -1
  38. package/dist/localSensing.d.ts +8 -0
  39. package/dist/localSensing.d.ts.map +1 -1
  40. package/dist/localSensing.js +86 -4
  41. package/dist/localSensing.js.map +1 -1
  42. package/package.json +1 -1
  43. 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.4e9d7ca160f6"
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()
@@ -13,5 +13,5 @@ human sets.
13
13
  """
14
14
 
15
15
  BASE_VERSION = "0.2.0"
16
- SOURCE_FINGERPRINT = "4e9d7ca160f6"
17
- VERSION = "0.2.0+src.4e9d7ca160f6"
16
+ SOURCE_FINGERPRINT = "abd83bba2f51"
17
+ VERSION = "0.2.0+src.abd83bba2f51"
@@ -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
@@ -249,10 +249,10 @@ class ShimTest(unittest.TestCase):
249
249
  Engine()
250
250
  self.assertEqual(calls[2], {})
251
251
 
252
- def _native_http(self, low_memory=False):
252
+ def _native_http(self, low_memory=False, images=None):
253
253
  shim = self._load(TRILLBOARDS_LITERTLM_SHIM="1")
254
254
  events = []
255
- images = [b"area", b"person"]
255
+ images = [b"area", b"person"] if images is None else images
256
256
  hashes = [hashlib.sha256(b).hexdigest() for b in images]
257
257
  class Engine:
258
258
  def __init__(self, **kwargs):
@@ -295,6 +295,12 @@ class ShimTest(unittest.TestCase):
295
295
  handler.do_POST()
296
296
  return handler, events, output, hashes
297
297
 
298
+ def test_single_creative_keyframe_has_native_consumption_proof(self):
299
+ handler, events, output, hashes = self._native_http(images=[b"creative keyframe"])
300
+ self.assertEqual(len(output), 1)
301
+ self.assertEqual(output[0]["nativeImageInputs"], {"version": 1, "sha256": hashes})
302
+ self.assertNotIn(("error", 400), events)
303
+
298
304
  def test_native_http_receipt_is_outside_model_text_and_capacity_reuses_one_engine(self):
299
305
  handler, events, output, hashes = self._native_http()
300
306
  self.assertEqual(events[:3], [("allocate", 1), ("release", 1), ("allocate", 5)])
@@ -269,7 +269,7 @@ def _apply_model_snapshot(engine_cls):
269
269
  if images is not None:
270
270
  # The public option used by the saved-frame comparison. No larger
271
271
  # engine is allocated on ordinary single-image/off-switch starts.
272
- kwargs['max_num_images'] = 5
272
+ kwargs['max_num_images'] = 5 if len(images['expected']) > 1 else 1
273
273
  path = kwargs.get('model_path', args[0] if args else None)
274
274
  if path is None: # lightweight contract-test engines have no artifact
275
275
  result = original(self, *args, **kwargs)
@@ -334,7 +334,7 @@ def apply_runtime_handler(module):
334
334
  return
335
335
  raw_images = self.headers.get('X-Trillboards-Image-Sha256')
336
336
  images = raw_images.split(',') if raw_images is not None else None
337
- if images is not None and (expected is None or not 2 <= len(images) <= 5
337
+ if images is not None and (expected is None or not 1 <= len(images) <= 5
338
338
  or any(not re.fullmatch(r'[a-f0-9]{64}', value) for value in images)):
339
339
  self.send_error(400, 'invalid pinned native image request')
340
340
  return
@@ -351,7 +351,7 @@ def apply_runtime_handler(module):
351
351
  if images is not None:
352
352
  server = getattr(self, 'server', None)
353
353
  previous = getattr(server, 'litert_lm_engine', None)
354
- if previous is not None and getattr(previous, '_trillboards_image_capacity', 1) < 5:
354
+ if previous is not None and getattr(previous, '_trillboards_image_capacity', 1) < len(images['expected']):
355
355
  # The pinned CLI's HTTPServer is serial. Release its previous
356
356
  # engine BEFORE allocating the larger one; no duplicate model.
357
357
  previous.__exit__(None, None, None)
@@ -0,0 +1,44 @@
1
+ /**
2
+ * The detector service's `detection_colors` task: the share of each confident
3
+ * person box whose pixels are a requested colour, measured on the original
4
+ * frame with Pillow's 8-bit HSV (deploy/detector/trillboards_detector/colors.py).
5
+ * No model and no decision: the connector turns the share into "this person
6
+ * wears the store's uniform".
7
+ */
8
+ export interface DetectionColorsRequest {
9
+ version: 1;
10
+ minScore: number;
11
+ maxBoxes: number;
12
+ hue: Array<[number, number]>;
13
+ minSaturation: number;
14
+ minValue: number;
15
+ }
16
+ export interface DetectionColorsResult {
17
+ version: 1;
18
+ imageSha256: string;
19
+ frameSize: {
20
+ width: number;
21
+ height: number;
22
+ };
23
+ request: DetectionColorsRequest;
24
+ people: Array<{
25
+ detectionIndex: number;
26
+ fraction: number;
27
+ }>;
28
+ }
29
+ export declare const MAX_COLOR_BOXES = 32;
30
+ export declare function validateDetectionColorsRequest(value: unknown): DetectionColorsRequest;
31
+ /** The service's own box selection: person boxes at or above minScore, highest score first. */
32
+ export declare function colorBoxIndices(detections: ReadonlyArray<{
33
+ label: string;
34
+ confidence: number;
35
+ }>, request: DetectionColorsRequest): number[];
36
+ /** Exactly the eligible people of this original frame, each with a share in [0,1]. */
37
+ export declare function parseDetectionColorsResult(value: unknown, request: DetectionColorsRequest, jpeg: Buffer, originalSize: {
38
+ width: number;
39
+ height: number;
40
+ }, detections: ReadonlyArray<{
41
+ label: string;
42
+ confidence: number;
43
+ }>): DetectionColorsResult;
44
+ //# sourceMappingURL=Detection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KAAK,EAAC,MAAM,CAAC;IAAA,UAAU,EAAC,MAAM,CAAA;CAAC,CAAC,GAAE,qBAAqB,CAe5H"}
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MAX_COLOR_BOXES = void 0;
4
+ exports.validateDetectionColorsRequest = validateDetectionColorsRequest;
5
+ exports.colorBoxIndices = colorBoxIndices;
6
+ exports.parseDetectionColorsResult = parseDetectionColorsResult;
7
+ const crypto_1 = require("crypto");
8
+ const util_1 = require("util");
9
+ exports.MAX_COLOR_BOXES = 32;
10
+ const keys = (v, want) => !!v && typeof v === 'object' && !Array.isArray(v) && Object.keys(v).sort().join(',') === want;
11
+ const finite = (v) => typeof v === 'number' && Number.isFinite(v);
12
+ const byte = (v) => Number.isSafeInteger(v) && v >= 0 && v <= 255;
13
+ function validateDetectionColorsRequest(value) {
14
+ const v = value;
15
+ if (!keys(v, 'hue,maxBoxes,minSaturation,minScore,minValue,version') || v.version !== 1
16
+ || !finite(v.minScore) || v.minScore <= 0 || v.minScore > 1
17
+ || !Number.isSafeInteger(v.maxBoxes) || v.maxBoxes < 1 || v.maxBoxes > exports.MAX_COLOR_BOXES
18
+ || !byte(v.minSaturation) || !byte(v.minValue)
19
+ || !Array.isArray(v.hue) || v.hue.length < 1 || v.hue.length > 4
20
+ || v.hue.some(r => !Array.isArray(r) || r.length !== 2 || !byte(r[0]) || !byte(r[1]) || r[0] > r[1]))
21
+ throw Error('Invalid detection colour request');
22
+ return JSON.parse(JSON.stringify(v));
23
+ }
24
+ /** The service's own box selection: person boxes at or above minScore, highest score first. */
25
+ function colorBoxIndices(detections, request) {
26
+ return detections.map((d, index) => ({ d, index }))
27
+ .filter(({ d }) => d.label === 'person' && d.confidence >= request.minScore)
28
+ .sort((a, b) => b.d.confidence - a.d.confidence || a.index - b.index)
29
+ .slice(0, request.maxBoxes).map(e => e.index);
30
+ }
31
+ /** Exactly the eligible people of this original frame, each with a share in [0,1]. */
32
+ function parseDetectionColorsResult(value, request, jpeg, originalSize, detections) {
33
+ validateDetectionColorsRequest(request);
34
+ const v = value;
35
+ if (!keys(v, 'frameSize,imageSha256,people,request,version') || v.version !== 1
36
+ || v.imageSha256 !== (0, crypto_1.createHash)('sha256').update(jpeg).digest('hex')
37
+ || !keys(v.frameSize, 'height,width') || v.frameSize.width !== originalSize.width
38
+ || v.frameSize.height !== originalSize.height || !(0, util_1.isDeepStrictEqual)(v.request, request)
39
+ || !Array.isArray(v.people))
40
+ throw Error('Invalid colour proof for the original detected frame');
41
+ const eligible = colorBoxIndices(detections, request);
42
+ if (v.people.length !== eligible.length)
43
+ throw Error('Colours do not cover exactly the confident people');
44
+ v.people.forEach((row, i) => {
45
+ if (!keys(row, 'detectionIndex,fraction') || row.detectionIndex !== eligible[i]
46
+ || !finite(row.fraction) || row.fraction < 0 || row.fraction > 1)
47
+ throw Error('Invalid detection colour association');
48
+ });
49
+ return JSON.parse(JSON.stringify(v));
50
+ }
51
+ //# sourceMappingURL=DetectionColors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DetectionColors.js","sourceRoot":"","sources":["../src/DetectionColors.ts"],"names":[],"mappings":";;;AAwBA,wEAUC;AAGD,0CAMC;AAGD,gEAgBC;AA9DD,mCAAkC;AAClC,+BAAuC;AAkB1B,QAAA,eAAe,GAAC,EAAE,CAAC;AAChC,MAAM,IAAI,GAAC,CAAC,CAAS,EAAC,IAAW,EAAC,EAAE,CAAA,CAAC,CAAC,CAAC,IAAE,OAAO,CAAC,KAAG,QAAQ,IAAE,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,IAAE,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,KAAG,IAAI,CAAC;AACxH,MAAM,MAAM,GAAC,CAAC,CAAS,EAAa,EAAE,CAAA,OAAO,CAAC,KAAG,QAAQ,IAAE,MAAM,CAAC,QAAQ,CAAC,CAAC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