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.
188 lines
8.1 KiB
Python
188 lines
8.1 KiB
Python
"""Offline candidate: original-frame semantic evidence, never record/track score.
|
|
All times are local monotonic acquisition times from the producer, not inference
|
|
completion or wall UTC. No model, network, camera or filesystem operations.
|
|
"""
|
|
from dataclasses import dataclass, field
|
|
from math import isfinite
|
|
from threading import RLock
|
|
|
|
TARGETS = frozenset(('multirotor_uav', 'fixed_wing_uav'))
|
|
NAMES = TARGETS | {'bird', 'manned_aircraft'}
|
|
|
|
|
|
def finite(*values):
|
|
return all(type(v) in (int, float) and isfinite(v) for v in values)
|
|
|
|
|
|
def sha(value):
|
|
return isinstance(value, str) and len(value) == 64 and all(c in '0123456789abcdef' for c in value)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Context:
|
|
camera: int
|
|
inspection_id: int
|
|
owner: tuple
|
|
source_id: str # producer stream/epoch identity; changes on reconnect
|
|
proposal_sha: str
|
|
classifier_sha: str
|
|
|
|
def valid(self):
|
|
return (type(self.camera) is int and self.camera >= 0
|
|
and type(self.inspection_id) is int and self.inspection_id > 0
|
|
and type(self.owner) is tuple and bool(self.owner)
|
|
and all((type(v) is str and bool(v)) or finite(v) for v in self.owner)
|
|
and isinstance(self.source_id, str) and 0 < len(self.source_id) <= 128
|
|
and sha(self.proposal_sha) and sha(self.classifier_sha))
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Frame:
|
|
seq: int
|
|
acquired_mono: float
|
|
bbox: tuple
|
|
shape: tuple # original (height, width), never resized crop shape
|
|
|
|
def valid(self, now):
|
|
if not (type(self.seq) is int and self.seq > 0
|
|
and finite(now, self.acquired_mono) and 0 <= now-self.acquired_mono <= .65
|
|
and type(self.shape) is tuple and len(self.shape) == 2
|
|
and all(type(v) is int and v > 0 for v in self.shape)
|
|
and type(self.bbox) is tuple and len(self.bbox) == 4 and finite(*self.bbox)):
|
|
return False
|
|
x1, y1, x2, y2 = self.bbox
|
|
return 0 <= x1 < x2 <= self.shape[1] and 0 <= y1 < y2 <= self.shape[0]
|
|
|
|
|
|
def same_object(a, b):
|
|
# Same bounded continuity as the retained V1 policy; never semantic identity.
|
|
area_a = (a.bbox[2]-a.bbox[0])*(a.bbox[3]-a.bbox[1])
|
|
area_b = (b.bbox[2]-b.bbox[0])*(b.bbox[3]-b.bbox[1])
|
|
jump = max(abs(a.bbox[0]+a.bbox[2]-b.bbox[0]-b.bbox[2])/(2*b.shape[1]),
|
|
abs(a.bbox[1]+a.bbox[3]-b.bbox[1]-b.bbox[3])/(2*b.shape[0]))
|
|
return .3 <= area_a/area_b <= 3.33 and jump <= .035
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Semantic:
|
|
frame: Frame
|
|
classifier_sha: str
|
|
scores: tuple
|
|
sharpness: float
|
|
clipped: bool = False
|
|
occluded: bool = False
|
|
|
|
|
|
@dataclass
|
|
class _Evidence:
|
|
context: Context
|
|
frames: dict = field(default_factory=dict)
|
|
latest: Frame | None = None
|
|
semantic_seq: int = 0
|
|
votes: list = field(default_factory=list)
|
|
|
|
|
|
class AlarmEligibility:
|
|
def __init__(self, *, semantic_min=.8, margin=.2, min_side=32, sharpness_min=50):
|
|
if not (finite(semantic_min, margin, min_side, sharpness_min)
|
|
and 0 < semantic_min <= 1 and 0 < margin <= 1
|
|
and min_side > 0 and sharpness_min >= 0):
|
|
raise ValueError('Invalid recognition thresholds')
|
|
# These are offline examples, not calibrated production values.
|
|
self.thresholds = semantic_min, margin, min_side, sharpness_min
|
|
self._states = {}
|
|
self._lock = RLock()
|
|
|
|
def begin(self, context, *, proposal_contract, classifier_contract):
|
|
if not isinstance(context, Context) or not context.valid():
|
|
raise ValueError('Validated source/model/owner context required')
|
|
from .model_class_contract import ModelContract
|
|
if not (isinstance(proposal_contract, ModelContract) and isinstance(classifier_contract, ModelContract)
|
|
and proposal_contract.task == 'detect' and classifier_contract.task == 'classify'
|
|
and proposal_contract.role == 'generic_detector' and proposal_contract.names == ('airborne_object',)
|
|
and classifier_contract.role == 'semantic_classifier' and set(classifier_contract.names) == NAMES
|
|
and proposal_contract.sha256 == context.proposal_sha
|
|
and classifier_contract.sha256 == context.classifier_sha):
|
|
raise ValueError('Verified generic and classifier contracts required')
|
|
with self._lock:
|
|
self._states[context.camera] = _Evidence(context)
|
|
|
|
def revoke(self, camera):
|
|
with self._lock:
|
|
self._states.pop(camera, None)
|
|
|
|
def _state(self, context):
|
|
if not isinstance(context, Context) or not context.valid():
|
|
return None
|
|
st = self._states.get(context.camera)
|
|
return st if st and st.context == context else None
|
|
|
|
def observe(self, context, frame, now):
|
|
with self._lock:
|
|
st = self._state(context)
|
|
if not st:
|
|
return False
|
|
if (not isinstance(frame, Frame) or not frame.valid(now)
|
|
or (st.latest and (frame.seq <= st.latest.seq
|
|
or frame.acquired_mono <= st.latest.acquired_mono
|
|
or frame.shape != st.latest.shape
|
|
or not same_object(st.latest, frame)))):
|
|
st.votes.clear()
|
|
return False
|
|
# Caller supplies only accepted same-object observations; discontinuity
|
|
# or lost object must revoke before another track is admitted.
|
|
if st.latest and frame.acquired_mono-st.latest.acquired_mono > .65:
|
|
st.votes.clear()
|
|
st.latest = frame
|
|
st.frames[frame.seq] = frame
|
|
st.frames = {k: v for k, v in st.frames.items() if 0 <= now-v.acquired_mono <= .65}
|
|
while len(st.frames) > 16:
|
|
del st.frames[min(st.frames)]
|
|
return True
|
|
|
|
def submit(self, context, result, now):
|
|
with self._lock:
|
|
st = self._state(context)
|
|
if not st:
|
|
return False
|
|
valid = (isinstance(result, Semantic) and isinstance(result.frame, Frame)
|
|
and result.frame.valid(now) and result.classifier_sha == context.classifier_sha
|
|
and st.frames.get(result.frame.seq) == result.frame
|
|
and result.frame.seq > st.semantic_seq)
|
|
if not valid:
|
|
st.votes.clear()
|
|
return False
|
|
st.semantic_seq = result.frame.seq
|
|
scores = result.scores
|
|
good = (type(scores) is tuple and len(scores) == len(NAMES)
|
|
and all(type(p) is tuple and len(p) == 2 and isinstance(p[0], str)
|
|
and finite(p[1]) and 0 <= p[1] <= 1 for p in scores)
|
|
and {p[0] for p in scores} == NAMES
|
|
and abs(sum(p[1] for p in scores)-1) <= 1e-4
|
|
and result.clipped is False and result.occluded is False
|
|
and finite(result.sharpness))
|
|
min_score, margin, min_side, sharpness_min = self.thresholds
|
|
good = (good and min(result.frame.bbox[2]-result.frame.bbox[0],
|
|
result.frame.bbox[3]-result.frame.bbox[1]) >= min_side
|
|
and result.sharpness >= sharpness_min)
|
|
ranked = sorted(scores, key=lambda p: p[1], reverse=True) if good else []
|
|
if not ranked or ranked[0][1] < min_score or ranked[0][1]-ranked[1][1] < margin:
|
|
st.votes.clear()
|
|
return False
|
|
name = ranked[0][0]
|
|
if name not in TARGETS:
|
|
st.votes.clear()
|
|
return False
|
|
if st.votes and (name != st.votes[-1][0]
|
|
or not 0 < result.frame.acquired_mono-st.votes[-1][1].acquired_mono <= .65):
|
|
st.votes.clear()
|
|
st.votes.append((name, result.frame))
|
|
st.votes = st.votes[-2:]
|
|
return self.eligible(context, now)
|
|
|
|
def eligible(self, context, now):
|
|
with self._lock:
|
|
st = self._state(context)
|
|
return bool(st and st.latest and st.latest.valid(now) and len(st.votes) == 2
|
|
and all(frame.valid(now) for _, frame in st.votes))
|