edge/engine.py keyed the ghost cooldown as (GHOST, round(x), round(y)) — purely spatial, ignoring track identity. Two distinct workers collapsing at ~the same pixel within the cooldown window would suppress each other's ghost event, potentially masking a second CRITICAL incident. edge/detectors/ghost.py GhostIncidentDetector.update now includes the candidate's track_id in each emitted dict. edge/engine.py keys the cooldown as (EventType.GHOST, track_id) and sets the emitted Event's track_id accordingly, matching how FALL/NO_MOTION/IMMOBILITY/zone events are already keyed per-track. Tests: tests/test_ghost.py asserts ghosts[0]["track_id"] == 1 on the existing reappear-timeout test.
48 lines
1.8 KiB
Python
48 lines
1.8 KiB
Python
from dataclasses import dataclass
|
|
|
|
from edge.tracking import Track
|
|
|
|
_DANGEROUS = {"FALLING", "FALLEN", "NO_MOTION"}
|
|
|
|
|
|
@dataclass
|
|
class GhostConfig:
|
|
reappear_s: float = 10.0
|
|
reappear_dist: float = 1.0 # bbox 높이 배수
|
|
|
|
|
|
class GhostIncidentDetector:
|
|
def __init__(self, cfg: GhostConfig):
|
|
self.cfg = cfg
|
|
self._states: dict[int, str] = {}
|
|
# 후보: track_id -> (spatial(x,y), last_state, height, lost_at)
|
|
self._candidates: dict[int, tuple] = {}
|
|
|
|
def note_state(self, track_id: int, state: str) -> None:
|
|
self._states[track_id] = state
|
|
|
|
def on_lost(self, lost_tracks: list[Track], now: float) -> None:
|
|
for tr in lost_tracks:
|
|
state = self._states.get(tr.track_id, "STANDING")
|
|
if state in _DANGEROUS:
|
|
b = tr.latest.bbox
|
|
self._candidates[tr.track_id] = ((b.cx, b.cy), state, b.height, now)
|
|
self._states.pop(tr.track_id, None)
|
|
|
|
def update(self, active_tracks: list[Track], now: float) -> list[dict]:
|
|
# 인근 재출현이면 후보 취소
|
|
for tid, (spatial, _state, height, _lost_at) in list(self._candidates.items()):
|
|
for tr in active_tracks:
|
|
b = tr.latest.bbox
|
|
dist = ((b.cx - spatial[0]) ** 2 + (b.cy - spatial[1]) ** 2) ** 0.5
|
|
if dist <= self.cfg.reappear_dist * (height or 1.0):
|
|
self._candidates.pop(tid, None)
|
|
break
|
|
# 대기 초과 방출
|
|
out = []
|
|
for tid, (spatial, state, _height, lost_at) in list(self._candidates.items()):
|
|
if now - lost_at >= self.cfg.reappear_s:
|
|
out.append({"track_id": tid, "spatial": spatial, "last_state": state,
|
|
"waited_s": round(now - lost_at, 1)})
|
|
self._candidates.pop(tid, None)
|
|
return out
|