move_detect/edge/detectors/zone.py
유창욱 752aabaff1 fix(edge): forget per-track state on lost tracks (FIX 5)
fall._states, immobility._still_since/_fired, zone._inside_since/_fired,
and engine._last_emit were never pruned when a track was lost (ghost
already pruned via on_lost). On a 24/7 edge device this is unbounded
growth as track IDs keep incrementing.

Add forget(track_id) to FallDetector, ImmobilityWatchdog, and
ZoneIntrusionDetector (drops that track's per-track keys/sets).
EventEngine.forget(track_id) calls each sub-detector's forget and removes
_last_emit cooldown keys whose tuple's second element is that track_id
(covers FALL/NO_MOTION/IMMOBILITY/zone/GHOST — GHOST keys are
(GHOST, track_id) after FIX 4). EventEngine.process now calls
self.forget(tr.track_id) for every track in lost_tracks after emitting,
since ghost.on_lost already consumed that track's ghost-relevant state at
the top of process().

app.py run_once already calls pool.forget(tr.track_id) for lost tracks as
part of FIX 1, completing the cleanup chain (smoother pool + detector
state + dedup cache).

Tests: tests/test_engine.py adds test_forget_prunes_state_on_lost —
fires a FALL event, then passes the track as lost, and asserts
engine._last_emit no longer has that track's keys and fall._states no
longer has the track id.
2026-07-11 22:19:39 +09:00

64 lines
2 KiB
Python

from dataclasses import dataclass
from edge.tracking import Track
from edge.models import L_ANKLE, R_ANKLE
def point_in_polygon(x: float, y: float, poly: list[tuple[float, float]]) -> bool:
inside = False
n = len(poly)
j = n - 1
for i in range(n):
xi, yi = poly[i]
xj, yj = poly[j]
if ((yi > y) != (yj > y)) and (x < (xj - xi) * (y - yi) / ((yj - yi) or 1e-9) + xi):
inside = not inside
j = i
return inside
@dataclass(frozen=True)
class Zone:
zone_id: str
level: str # "warning" | "restricted"
polygon: list # list[tuple[float,float]]
@dataclass
class ZoneConfig:
debounce_s: float = 0.75
class ZoneIntrusionDetector:
def __init__(self, zones: list[Zone], cfg: ZoneConfig):
self.zones = zones
self.cfg = cfg
self._inside_since: dict[tuple[int, str], float] = {}
self._fired: set[tuple[int, str]] = set()
def forget(self, track_id: int) -> None:
self._inside_since = {k: v for k, v in self._inside_since.items() if k[0] != track_id}
self._fired = {k for k in self._fired if k[0] != track_id}
def _foot(self, track: Track) -> tuple[float, float]:
kps = track.latest.keypoints
a, b = kps[L_ANKLE], kps[R_ANKLE]
return ((a.x + b.x) / 2, (a.y + b.y) / 2)
def active_zone_ids(self, track_id: int) -> set:
return {z for (tid, z) in self._inside_since if tid == track_id}
def update(self, track: Track, now: float) -> list[dict]:
fx, fy = self._foot(track)
out = []
for zone in self.zones:
key = (track.track_id, zone.zone_id)
if point_in_polygon(fx, fy, zone.polygon):
start = self._inside_since.setdefault(key, now)
if now - start >= self.cfg.debounce_s and key not in self._fired:
self._fired.add(key)
out.append({"zone_id": zone.zone_id, "level": zone.level})
else:
self._inside_since.pop(key, None)
self._fired.discard(key)
return out