move_detect/tests/test_fall.py
유창욱 3240428380 fix(edge): require sustained non-prone before FallDetector recover (FIX 2)
edge/detectors/fall.py update(): a single frame where prone was false while
state was in {FALLING, FALLEN, NO_MOTION} immediately emitted "recover" and
reset _State. One jittery frame (a momentary pose-estimation miss) could
wipe an in-progress CRITICAL no_motion incident.

Add FallConfig.recover_grace_s (default 1.0s) and _State.not_prone_since.
On the first non-prone frame while in an incident state, start the grace
timer instead of recovering immediately; only reset to STANDING once
recover_grace_s of sustained non-prone has elapsed. A re-prone frame while
in an incident state clears the pending grace timer (cancels the recover
attempt). The non-incident STANDING path is unchanged.

BEHAVIOR CHANGE: recover now requires recover_grace_s of sustained
non-prone instead of firing on a single frame.

Tests: test_recovery_clears_state (tests/test_fall.py) strengthened per
review brief to assert a single standing frame does NOT recover but
sustained standing across the grace window does. Added
test_single_noisy_nonprone_frame_does_not_cancel_no_motion covering the
life-safety scenario: one noisy non-prone frame during NO_MOTION must not
cancel the incident.
2026-07-11 22:15:47 +09:00

101 lines
4.1 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_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