File size: 1,443 Bytes
24a5e7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
import sys

import numpy as np

PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if PROJECT_ROOT not in sys.path:
    sys.path.insert(0, PROJECT_ROOT)

from ui.pipeline import _clip_features
from models.collect_features import FEATURE_NAMES


def test_clip_features_clamps_ranges():
    idx = {name: i for i, name in enumerate(FEATURE_NAMES)}
    vec = np.zeros(len(FEATURE_NAMES), dtype=np.float32)

    # 设置超出合理范围的值
    vec[idx["yaw"]] = 90.0
    vec[idx["pitch"]] = -90.0
    vec[idx["roll"]] = 90.0
    vec[idx["head_deviation"]] = 999.0
    for name in ("ear_left", "ear_right", "ear_avg"):
        vec[idx[name]] = 2.0
    vec[idx["mar"]] = 5.0
    vec[idx["gaze_offset"]] = 1.0
    vec[idx["perclos"]] = 2.0
    vec[idx["blink_rate"]] = 100.0
    vec[idx["closure_duration"]] = 50.0
    vec[idx["yawn_duration"]] = 50.0

    out = _clip_features(vec)

    assert -45.0 <= out[idx["yaw"]] <= 45.0
    assert -30.0 <= out[idx["pitch"]] <= 30.0
    assert -30.0 <= out[idx["roll"]] <= 30.0

    for name in ("ear_left", "ear_right", "ear_avg"):
        assert 0.0 <= out[idx[name]] <= 0.85

    assert 0.0 <= out[idx["mar"]] <= 1.0
    assert 0.0 <= out[idx["gaze_offset"]] <= 0.5
    assert 0.0 <= out[idx["perclos"]] <= 0.8
    assert 0.0 <= out[idx["blink_rate"]] <= 30.0
    assert 0.0 <= out[idx["closure_duration"]] <= 10.0
    assert 0.0 <= out[idx["yawn_duration"]] <= 10.0