move_detect/tests/test_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

48 lines
2.4 KiB
Python

from collections import deque
from edge.models import Keypoint, BBox
from edge.tracking import Track, TrackFrame
from edge.detectors.zone import point_in_polygon, Zone, ZoneIntrusionDetector, ZoneConfig
SQUARE = [(0, 0), (100, 0), (100, 100), (0, 100)]
def test_point_in_polygon_basic():
assert point_in_polygon(50, 50, SQUARE) is True
assert point_in_polygon(150, 50, SQUARE) is False
def _track_with_ankles(ax, ay, t):
kps = [Keypoint(ax, ay, 0.9) for _ in range(17)]
kps[15] = Keypoint(ax, ay, 0.9) # L_ANKLE
kps[16] = Keypoint(ax, ay, 0.9) # R_ANKLE
return Track(1, deque([TrackFrame(t, BBox(ax - 10, ay - 60, ax + 10, ay), kps)], maxlen=90))
def test_intrusion_fires_after_debounce():
z = Zone("z1", "restricted", SQUARE)
det = ZoneIntrusionDetector([z], ZoneConfig(debounce_s=0.5))
tr = _track_with_ankles(50, 50, 0.0)
assert det.update(tr, now=0.0) == []
tr = _track_with_ankles(50, 50, 0.6)
out = det.update(tr, now=0.6)
assert out == [{"zone_id": "z1", "level": "restricted"}]
def test_leaving_zone_resets():
z = Zone("z1", "warning", SQUARE)
det = ZoneIntrusionDetector([z], ZoneConfig(debounce_s=0.5))
det.update(_track_with_ankles(50, 50, 0.0), now=0.0)
det.update(_track_with_ankles(200, 200, 0.6), now=0.6) # 이탈
out = det.update(_track_with_ankles(50, 50, 1.0), now=1.0) # 재진입, 디바운스 재시작
assert out == []
def test_zone_uses_normalized_coordinates_when_frame_size_given():
# The dashboard stores zones as 0..1 of the image (it stretches the snapshot to a fixed
# 960x540 canvas). The edge foot is camera-native pixels. Normalizing the foot by the
# frame size makes the two comparable regardless of camera resolution — without this,
# every zone is silently misplaced.
zn = Zone("z1", "restricted", [(0.4, 0.4), (0.6, 0.4), (0.6, 0.6), (0.4, 0.6)])
det = ZoneIntrusionDetector([zn], ZoneConfig(debounce_s=0.0))
# foot at the centre of a 1280x720 frame -> (0.5, 0.5) -> inside the 0.4..0.6 zone
inside = det.update(_track_with_ankles(640, 360, 0.0), now=0.0, frame_wh=(1280, 720))
assert inside == [{"zone_id": "z1", "level": "restricted"}]
# foot near the corner -> ~(0.008, 0.014) -> outside
det2 = ZoneIntrusionDetector([zn], ZoneConfig(debounce_s=0.0))
assert det2.update(_track_with_ankles(10, 10, 0.0), now=0.0, frame_wh=(1280, 720)) == []