move_detect/edge/detectors/zone.py
유창욱 9386dfc580 feat(delivery): install-path fixes + cafe24 package + delivery-audit hardening
Three intertwined bodies of work developed and tested together this session.
Full test suite green: python (edge) + phpunit (cloud).

## Edge/Pi install path
- setup_online.sh: apt-lock retry (apt-daily contention), Type=exec unit +
  StartLimitIntervalSec in [Unit], journalctl-on-failure, crash-loop detection.
- onnxruntime is now best-effort (no sdist / no 32-bit-ARM wheel); [6/8] probes the
  real backend and hard-fails if none works.
- Hailo HEF auto-fetch: edge/tools/fetch_hailo_hef.py (pinned sha256, refuses to guess
  8 vs 8L, HAILO_ARCH override for serial-less boards); hailo_pose.py run() timeout fix.
- install_offline.sh: mirrors the backend gate; onnxruntime optional.

## Cafe24 cloud package (cloud/deploy/)
- build_cafe24_package.sh emits move_detect_app/ (private, one folder) + www/move_detect/
  (docroot subdir); no-dev classmap autoloader; preflight.php; hardened .htaccess.
- Subdirectory mount support: Request::baseFrom/pathFor strip the mount prefix; index.php
  walks up to find the app root; views/JS prefix every URL with APP_BASE; session cookie
  scoped to the mount.

## Delivery audit — silent-failure & correctness (docs/delivery-audit.md)
All 4 blockers + all Phase-1 silent-failure + Phase-2 HIGH correctness:
- Edge cloud clients send a custom User-Agent (cafe24 WAF blocks python-requests) and
  allow_redirects=False.
- Daemon fails loud on blank CAFE24_URL/DEVICE_SECRET; run_loop logs swallowed exceptions;
  heartbeat carries real fps + per-camera health; UvcSource reopens a dead camera;
  offline queue is size-capped and drops poison rows; TARGET_FPS/QUEUE_DB validated.
- Zone coordinates normalized 0..1 on both sides (were canvas-px vs camera-px — every zone
  misplaced); pinch zones emit PINCH/HIGH (were downgraded to WARNING).
- Fall detection uses bbox cues when keypoints are low-confidence (occluded fallen person);
  ghost incident not cancelled by a bystander; displacement normalized per-second (FPS-invariant).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019Xmn8KZWqiQUguEMRtn6rW
2026-07-17 09:07:33 +09:00

75 lines
2.7 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, frame_wh=None) -> tuple[float, float]:
kps = track.latest.keypoints
a, b = kps[L_ANKLE], kps[R_ANKLE]
fx, fy = (a.x + b.x) / 2, (a.y + b.y) / 2
# Zones are stored NORMALIZED (0..1 of the image) by the dashboard, which stretches the
# camera snapshot to a fixed canvas. The foot is camera-native pixels, so normalize it
# by the frame size to compare in the same space — otherwise every zone is misplaced
# (a zone drawn at image-centre would be checked against a raw pixel like 480, matching
# nothing on a 1280-wide frame). frame_wh=None keeps raw coords for unit tests that
# supply their own matching space.
if frame_wh:
w, h = frame_wh
if w > 0 and h > 0:
fx, fy = fx / w, fy / h
return (fx, fy)
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, frame_wh=None) -> list[dict]:
fx, fy = self._foot(track, frame_wh)
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