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.
Chameleon/ptz_tracker_modular/inspection_runtime.py

241 lines
11 KiB
Python

"""Offline integration candidate. No network, model loader, threads or PTZ worker.
The producer binds source/epoch under the existing camera control lock. The
classifier calls run_one on its OWN worker, never on the camera control thread.
Unknown occlusion quality is not evidence that an object is unobscured.
"""
from collections import OrderedDict
from dataclasses import dataclass
from math import ceil, floor
from itertools import islice
from threading import Lock
from .frame_time import FrameStamp
from .unknown_inspection import Recognition, finite, valid_box
def source_key(stamp):
return stamp.source_id, stamp.stream_epoch, stamp.clock_epoch
def check_stamp(stamp, *, camera, seq, owner, expected_owner, binding, now, age):
return (isinstance(stamp, FrameStamp) and stamp.camera_id == camera
and type(seq) is int and stamp.source_seq == seq and seq > 0
and owner is not None and owner == expected_owner
and binding == source_key(stamp) and finite(now, age)
and 0 <= now-stamp.decode_mono_ns/1e9 <= age)
def provenance(st, result, sample, *, camera, seq, owner, mono, visual_only):
"""Only accepted producer stamps can tighten the controller's deadline.
Called after foreign-owner early return, under the existing control lock.
Binding MUST be set by source lifecycle; it is never learned from a result.
"""
value = sample if visual_only else result
stamp = getattr(value, 'inspection_stamp', None)
incoming_owner = getattr(value, 'inspection_owner', None)
if not check_stamp(stamp, camera=camera, seq=seq, owner=incoming_owner,
expected_owner=owner, binding=st.get('inspection_source_binding'),
now=mono, age=.25 if visual_only else .65):
return 'missing_or_stale_original_stamp'
acquired = stamp.decode_mono_ns/1e9
if not visual_only:
last = st.get('inspection_detector_stamp')
if (last is not None and (source_key(last) != source_key(stamp)
or stamp.source_seq <= last.source_seq
or stamp.decode_mono_ns <= last.decode_mono_ns)):
return 'duplicate_or_reordered_original_frame'
st['inspection_detector_stamp'] = stamp
st['inspection_input_mono'] = acquired
return ''
def current_detector(st, mono):
stamp = st.get('inspection_detector_stamp')
return bool(isinstance(stamp, FrameStamp)
and source_key(stamp) == st.get('inspection_source_binding')
and finite(mono) and 0 <= mono-stamp.decode_mono_ns/1e9 <= .65)
@dataclass(frozen=True)
class CropContext:
camera: int
inspection_id: int
owner: tuple
source: tuple
classifier_sha: str
@dataclass(frozen=True)
class CropJob:
context: CropContext
stamp: FrameStamp
bbox: tuple
shape: tuple
pixels: object
sharpness: float
clipped: bool
occluded: bool | None
class CropWorker:
"""One in-flight callback, one pending crop/result per configured camera.
Replacement keeps camera FIFO position: a busy camera cannot starve others.
max_pixels bounds each copied crop; full frames are never retained. A stalled
classifier can delay recognition, but submit/take do not acquire its lock.
"""
def __init__(self, cameras, *, max_pixels=1024*1024):
if (type(cameras) not in (tuple, list) or not cameras or len(cameras) > 6
or any(type(c) is not int or c < 0 for c in cameras)
or len(set(cameras)) != len(cameras)
or type(max_pixels) is not int or not 1 <= max_pixels <= 1024*1024):
raise ValueError('Bounded physical camera set required')
self.cameras, self.max_pixels = frozenset(cameras), max_pixels
self._pending, self._results, self._active = OrderedDict(), {}, {}
self._latest = {}
self._decode_ages = {} # bounded by the configured camera set
self._generation = {c: 0 for c in self.cameras}
self._lock, self._worker = Lock(), Lock()
def bind(self, context):
from .unknown_inspection import digest
if (not isinstance(context, CropContext) or context.camera not in self.cameras
or type(context.inspection_id) is not int or context.inspection_id <= 0
or not context.owner or type(context.source) is not tuple or len(context.source) != 3
or not digest(context.classifier_sha)):
raise ValueError('Explicit inspection/source/model context required')
with self._lock:
if self._active.get(context.camera) == context:
return
self._generation[context.camera] += 1
self._active[context.camera] = context
self._pending.pop(context.camera, None)
self._results.pop(context.camera, None)
self._latest.pop(context.camera, None)
self._decode_ages.pop(context.camera, None)
def revoke(self, camera):
if camera not in self.cameras:
raise ValueError('Unknown physical camera')
with self._lock:
self._generation[camera] += 1
self._active.pop(camera, None)
self._pending.pop(camera, None)
self._results.pop(camera, None)
self._latest.pop(camera, None)
self._decode_ages.pop(camera, None)
def submit(self, context, stamp, frame, bbox, *, now, occluded=None):
import numpy as np
if (not isinstance(context, CropContext) or context.camera not in self.cameras
or not isinstance(frame, np.ndarray) or frame.dtype != np.uint8
or frame.ndim != 3 or frame.shape[2] != 3
or not valid_box(bbox, tuple(frame.shape[:2]))
or occluded is not None and type(occluded) is not bool
or not check_stamp(stamp, camera=context.camera, seq=getattr(stamp, 'source_seq', None),
owner=context.owner, expected_owner=context.owner,
binding=context.source, now=now, age=.65)):
return False
with self._lock:
if self._active.get(context.camera) != context:
return False
generation = self._generation[context.camera]
x1, y1, x2, y2 = bbox
left, top, right, bottom = floor(x1), floor(y1), ceil(x2), ceil(y2)
if (right-left)*(bottom-top) > self.max_pixels:
return False
# Copy belongs to this exact image and sequence; do not resize/upscale it
# to meet recognition thresholds or invent source detail.
pixels = frame[top:bottom, left:right].copy()
if pixels.shape[0] < 3 or pixels.shape[1] < 3:
return False
gray = pixels.astype(np.float32).mean(axis=2)
lap = -4*gray[1:-1, 1:-1]+gray[:-2, 1:-1]+gray[2:, 1:-1]+gray[1:-1, :-2]+gray[1:-1, 2:]
sharpness = float(lap.var())
pixels.flags.writeable = False
job = CropJob(context, stamp, tuple(bbox), tuple(frame.shape[:2]), pixels,
sharpness, x1 <= 0 or y1 <= 0 or x2 >= frame.shape[1] or y2 >= frame.shape[0], occluded)
with self._lock:
if (self._active.get(context.camera) != context
or self._generation[context.camera] != generation):
return False
last = self._latest.get(context.camera)
if last and (stamp.source_seq <= last.source_seq or stamp.decode_mono_ns <= last.decode_mono_ns):
return False
self._latest[context.camera] = stamp
self._pending[context.camera] = job
return True
def run_one(self, classify, *, clock):
"""Return False when busy/empty; failures leave the object UNKNOWN."""
if not self._worker.acquire(blocking=False):
return False
try:
with self._lock:
if not self._pending:
return False
camera, job = self._pending.popitem(last=False)
generation = self._generation[camera]
start_age = clock()-job.stamp.decode_mono_ns/1e9
with self._lock:
if self._active.get(camera) == job.context and self._generation[camera] == generation:
self._decode_ages[camera] = {'recognition_start': start_age}
if not 0 <= start_age <= .65:
return False
try:
scores = tuple(islice(classify(job.pixels), 5))
from .unknown_inspection import TARGETS, NON_TARGETS
if (len(scores) != 4 or any(type(p) is not tuple or len(p) != 2
or type(p[0]) is not str or not finite(p[1]) or not 0 <= p[1] <= 1 for p in scores)
or {p[0] for p in scores} != TARGETS | NON_TARGETS
or abs(sum(p[1] for p in scores)-1) > 1e-4):
return False
except Exception:
return False
# The callback cannot bless clipping, missing occlusion quality,
# completion time, source or owner. It returns semantic scores only.
rec = Recognition(camera, job.context.inspection_id, job.context.owner,
job.stamp.source_seq, job.stamp.decode_utc_ns/1e9,
job.context.classifier_sha, job.bbox, job.shape, scores,
job.sharpness, job.clipped, job.occluded is not False)
with self._lock:
if (self._active.get(camera) != job.context
or self._generation[camera] != generation):
return False
complete_age = clock()-job.stamp.decode_mono_ns/1e9
self._decode_ages[camera]['recognition_complete'] = complete_age
if not 0 <= complete_age <= .65:
return False
self._results[camera] = (job.context, job.stamp, rec)
return True
finally:
self._worker.release()
def take(self, context, *, now):
row = self.take_stamped(context, now=now)
return row[1] if row else None
def take_stamped(self, context, *, now):
"""Exact original stamp accompanies a delayed classifier result."""
with self._lock:
if self._active.get(context.camera) != context:
return None
row = self._results.get(context.camera)
if not row or row[0] != context:
return None
self._results.pop(context.camera)
if not 0 <= now-row[1].decode_mono_ns/1e9 <= .65:
return None
return row[1], row[2]
def counts(self):
with self._lock:
return len(self._pending), len(self._results)
def decode_ages(self):
"""Latest bounded local decode ages, never camera exposure/capture lag."""
with self._lock:
return {camera: dict(row) for camera, row in self._decode_ages.items()}