You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
3.1 KiB
Python
65 lines
3.1 KiB
Python
"""Explicit detector-result adapter; never invent a fresh acquisition timestamp.
|
|
|
|
Call at the inference producer with the frame's original local decode time and
|
|
owner token. Actual model hash must come from the validated loaded-model contract.
|
|
The producer must not pass time.time() after inference as observed_at.
|
|
"""
|
|
from .unknown_inspection import Observation
|
|
from .frame_time import FrameStamp
|
|
|
|
|
|
class InspectionVisual:
|
|
def __init__(self, original, stamp, owner):
|
|
if (not isinstance(stamp,FrameStamp) or original.source_seq != stamp.source_seq
|
|
or original.captured_at != stamp.decode_utc_ns/1e9 or original.owner != owner):
|
|
raise ValueError('Exact original visual sample stamp/owner required')
|
|
self.original, self.inspection_stamp, self.inspection_owner = original,stamp,owner
|
|
|
|
def __getattr__(self,name):
|
|
return getattr(self.original,name)
|
|
|
|
|
|
class InspectionResult:
|
|
def __init__(self, original, observation, recognition, stamp, owner, recognition_stamp):
|
|
self.original = original
|
|
self.inspection_observation = observation
|
|
self.inspection_recognition = recognition
|
|
self.inspection_stamp = stamp
|
|
self.inspection_owner = owner
|
|
self.inspection_recognition_stamp = recognition_stamp
|
|
|
|
def __getattr__(self, name):
|
|
return getattr(self.original, name)
|
|
|
|
|
|
def adapt_result(result, *, camera, owner, source_seq, observed_at,
|
|
frame_shape, actual_model_sha, frame_stamp, recognition=None, recognition_stamp=None):
|
|
if (not isinstance(frame_stamp, FrameStamp) or frame_stamp.camera_id != camera
|
|
or frame_stamp.source_seq != source_seq or owner is None
|
|
or frame_stamp.decode_utc_ns/1e9 != observed_at):
|
|
raise ValueError('Exact original producer stamp required')
|
|
if recognition is not None and (not isinstance(recognition_stamp, FrameStamp)
|
|
or recognition_stamp.camera_id != camera
|
|
or recognition_stamp.source_seq != recognition.source_seq
|
|
or recognition_stamp.decode_utc_ns/1e9 != recognition.observed_at
|
|
or recognition.owner != owner):
|
|
raise ValueError('Original recognition frame stamp required')
|
|
names = getattr(result, 'names', {})
|
|
if names != {0: 'airborne_object'}:
|
|
raise ValueError('A generic model contract is required; old class 0 is not generic')
|
|
boxes = result.boxes
|
|
if boxes is None:
|
|
detections = ()
|
|
else:
|
|
xyxy = boxes.xyxy.detach().cpu().tolist()
|
|
scores = boxes.conf.detach().cpu().tolist()
|
|
classes = boxes.cls.detach().cpu().tolist()
|
|
if not len(xyxy) == len(scores) == len(classes) or len(xyxy) > 64:
|
|
raise ValueError('Invalid or excessive proposals')
|
|
if any(c != int(c) for c in classes):
|
|
raise ValueError('Non-integral proposal class')
|
|
detections = tuple((tuple(b), c, int(k)) for b, c, k in zip(xyxy, scores, classes))
|
|
obs = Observation(camera, owner, source_seq, observed_at, actual_model_sha,
|
|
('airborne_object',), tuple(frame_shape[:2]), detections)
|
|
return InspectionResult(result, obs, recognition, frame_stamp, owner, recognition_stamp)
|