move_detect/tests/test_fall.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

127 lines
5.5 KiB
Python

from collections import deque
from edge.models import Keypoint, BBox
from edge.tracking import Track, TrackFrame
from edge.detectors.fall import FallDetector, FallConfig
def _standing_kps():
kps = [Keypoint(50, 150, 0.9) for _ in range(17)]
kps[5] = Keypoint(40, 100, 0.9); kps[6] = Keypoint(60, 100, 0.9)
kps[11] = Keypoint(40, 200, 0.9); kps[12] = Keypoint(60, 200, 0.9)
kps[0] = Keypoint(50, 60, 0.9) # nose above hips
return kps
def _lying_kps():
kps = [Keypoint(150, 300, 0.9) for _ in range(17)]
kps[5] = Keypoint(100, 300, 0.9); kps[6] = Keypoint(100, 310, 0.9)
kps[11] = Keypoint(200, 300, 0.9); kps[12] = Keypoint(200, 310, 0.9)
kps[0] = Keypoint(90, 320, 0.9) # nose below hips
return kps
def _track():
return Track(1, deque(maxlen=90))
def _feed(det, track, kps, bbox, t):
track.frames.append(TrackFrame(t, bbox, kps))
return det.update(track, now=t)
def test_standing_produces_no_event():
det = FallDetector(FallConfig())
tr = _track()
sig, _ = _feed(det, tr, _standing_kps(), BBox(40, 60, 60, 210), t=0.0)
assert sig is None
def test_fall_detected_from_bbox_when_keypoints_low_confidence():
# A fallen person is exactly when the pose model loses confidence (occluded, on the
# floor). The old blanket confidence gate returned None -> fall/no_motion/ghost all
# DISABLED for the person on the ground. The bbox cues (wide-and-short) don't need
# keypoints and must still fire.
det = FallDetector(FallConfig(prone_confirm_s=0.5, min_conf=0.5, min_visible=10))
tr = _track()
low_conf = [Keypoint(100, 100, 0.1) for _ in range(17)] # all below min_conf
wide_short = BBox(0, 0, 200, 100) # aspect 2.0 -> prone
_feed(det, tr, low_conf, wide_short, t=0.0) # FALLING
sig, _ = _feed(det, tr, low_conf, wide_short, t=0.6) # prone 0.6s > 0.5 -> FALLEN
assert sig == "fall"
def test_low_confidence_standing_is_not_a_false_fall():
# ...but a low-confidence STANDING person (tall, narrow bbox, no collapse) must NOT be
# flagged — the bbox cues alone stay conservative.
det = FallDetector(FallConfig(prone_confirm_s=0.5, min_conf=0.5, min_visible=10))
tr = _track()
low_conf = [Keypoint(50, 100, 0.1) for _ in range(17)]
tall_narrow = BBox(40, 60, 60, 210) # aspect ~0.13 -> not prone
_feed(det, tr, low_conf, tall_narrow, t=0.0)
sig, _ = _feed(det, tr, low_conf, tall_narrow, t=0.6)
assert sig is None
def test_lying_confirmed_after_prone_seconds_emits_fall():
det = FallDetector(FallConfig(prone_confirm_s=1.0))
tr = _track()
lying_box = BBox(90, 290, 210, 320) # 넓고 낮음 (aspect>1.25)
_feed(det, tr, _lying_kps(), lying_box, t=0.0)
sig, meta = _feed(det, tr, _lying_kps(), lying_box, t=1.5) # prone 1.5s > 1.0
assert sig == "fall"
assert meta["torso_angle"] > 80
def test_front_fall_height_collapse_triggers_without_angle():
# 정면 낙상: 각도/종횡비 안 변해도 bbox 높이 급붕괴
det = FallDetector(FallConfig(prone_confirm_s=0.5))
tr = _track()
tall = BBox(40, 60, 60, 210) # height 150
_feed(det, tr, _standing_kps(), tall, t=0.0)
short = BBox(40, 180, 60, 210) # height 30 (붕괴 0.8)
_feed(det, tr, _standing_kps(), short, t=0.2)
sig, _ = _feed(det, tr, _standing_kps(), short, t=0.9)
assert sig == "fall"
def test_no_motion_after_fall_escalates():
det = FallDetector(FallConfig(prone_confirm_s=0.5, no_motion_s=1.0, motion_thr=0.05))
tr = _track()
box = BBox(90, 290, 210, 320)
_feed(det, tr, _lying_kps(), box, t=0.0)
_feed(det, tr, _lying_kps(), box, t=0.6) # -> fall
_feed(det, tr, _lying_kps(), box, t=1.0) # 미동 지속
sig, _ = _feed(det, tr, _lying_kps(), box, t=2.0) # 부동 >1.0s
assert sig == "no_motion"
def test_recovery_clears_state():
# Recovery requires SUSTAINED non-prone for recover_grace_s, not a single frame.
det = FallDetector(FallConfig(prone_confirm_s=0.5, recover_grace_s=1.0))
tr = _track()
box = BBox(90, 290, 210, 320)
_feed(det, tr, _lying_kps(), box, t=0.0)
_feed(det, tr, _lying_kps(), box, t=0.6) # fall
standing_box = BBox(40, 60, 60, 210)
# a single standing frame right after a fall must NOT recover yet
sig, _ = _feed(det, tr, _standing_kps(), standing_box, t=1.0)
assert sig is None
# sustained standing through the grace window -> recover fires
sig, _ = _feed(det, tr, _standing_kps(), standing_box, t=2.5)
assert sig == "recover"
def test_single_noisy_nonprone_frame_does_not_cancel_no_motion():
det = FallDetector(FallConfig(prone_confirm_s=0.5, no_motion_s=1.0, motion_thr=0.05,
recover_grace_s=1.0))
tr = _track()
box = BBox(90, 290, 210, 320)
_feed(det, tr, _lying_kps(), box, t=0.0)
_feed(det, tr, _lying_kps(), box, t=0.6) # -> fall
_feed(det, tr, _lying_kps(), box, t=1.0) # still
sig, _ = _feed(det, tr, _lying_kps(), box, t=2.0) # immobile >1.0s
assert sig == "no_motion"
# a single noisy non-prone frame within the grace window must NOT cancel
# the in-progress CRITICAL no_motion incident.
sig, _ = _feed(det, tr, _standing_kps(), BBox(40, 60, 60, 210), t=2.3)
assert sig is None
st = det._states[tr.track_id]
assert st.name == "NO_MOTION"
def test_low_confidence_is_ignored():
det = FallDetector(FallConfig())
tr = _track()
kps = [Keypoint(0, 0, 0.1) for _ in range(17)] # 전부 저신뢰
sig, _ = _feed(det, tr, kps, BBox(0, 0, 100, 30), t=0.0)
assert sig is None