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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
| import numpy as np from dataclasses import dataclass, field from typing import List
@dataclass class LowLevelSignals: """低层视觉信号(逐帧提取)""" eye_openness: float gaze_x: float gaze_y: float head_pitch: float head_roll: float head_yaw: float mouth_open: float body_posture: np.ndarray
@dataclass class MidLevelBehaviors: """中层行为模式(时序聚合)""" blink_rate: float avg_blink_duration: float perclos: float yawn_count: int nod_count: int gaze_variance: float posture_shifts: int head_sway_freq: float
class FATEDFramework: """ FATED疲劳连续评估框架 核心理念:疲劳不是on/off,而是渐进连续体 评估从正常→疲劳→嗜睡→睡眠的渐进过程 """ def __init__(self, window_sec: int = 60): self.window_sec = window_sec self.individual_baseline = None self.stages = [ 'Alert', 'Fatigued', 'Drowsy', 'Sleepy', 'Sleep', ] def calibrate_baseline(self, signals: List[LowLevelSignals], calibration_min: int = 5) -> dict: """ 个体基线校准 Args: signals: 清醒状态信号序列 calibration_min: 最少校准分钟数 Returns: baseline: 个体基线指标 """ if len(signals) < calibration_min * 30 * 60: return None baseline = { 'eye_openness': np.mean([s.eye_openness for s in signals]), 'blink_rate': self._calc_blink_rate(signals), 'perclos': self._calc_perclos(signals), 'gaze_variance': np.var([s.gaze_x**2 + s.gaze_y**2 for s in signals]), 'head_sway': np.std([s.head_pitch for s in signals]), 'posture_stability': np.std([s.body_posture[0, 0] for s in signals]), } self.individual_baseline = baseline return baseline def assess_continuum(self, signals: List[LowLevelSignals]) -> dict: """ 评估疲劳连续体阶段 Returns: {'stage': str, 'score': 0-1, 'trend': str} """ if self.individual_baseline is None: return {'stage': 'Unknown', 'score': 0, 'trend': 'stable'} behaviors = self._extract_behaviors(signals) deviations = { 'eye': self._deviation( behaviors.perclos, self.individual_baseline['perclos'], max_val=40 ), 'blink': self._deviation( behaviors.blink_rate, self.individual_baseline['blink_rate'], max_val=30 ), 'gaze': self._deviation( behaviors.gaze_variance, self.individual_baseline['gaze_variance'], max_val=100 ), 'head': self._deviation( behaviors.head_sway_freq, self.individual_baseline['head_sway'], max_val=0.5 ), 'yawn': min(behaviors.yawn_count / 5, 1.0), 'nod': min(behaviors.nod_count / 3, 1.0), 'posture': min(behaviors.posture_shifts / 10, 1.0), } weights = { 'eye': 0.25, 'blink': 0.15, 'gaze': 0.15, 'head': 0.15, 'yawn': 0.10, 'nod': 0.10, 'posture': 0.10, } score = sum(weights[k] * deviations[k] for k in weights) if score < 0.15: stage = 'Alert' elif score < 0.35: stage = 'Fatigued' elif score < 0.55: stage = 'Drowsy' elif score < 0.75: stage = 'Sleepy' else: stage = 'Sleep' return { 'stage': stage, 'score': score, 'deviations': deviations, 'trend': self._calc_trend(deviations), } def _extract_behaviors(self, signals: List[LowLevelSignals]) -> MidLevelBehaviors: """从低层信号提取中层行为""" eye_values = [s.eye_openness for s in signals] threshold = np.mean(eye_values) * 0.7 closed_count = sum(1 for e in eye_values if e < threshold) perclos = closed_count / len(eye_values) * 100 crossings = sum(1 for i in range(1, len(eye_values)) if eye_values[i-1] > threshold and eye_values[i] <= threshold) blink_rate = crossings / (len(signals) / 30 / 60) yawns = sum(1 for s in signals if s.mouth_open > 0.6) return MidLevelBehaviors( blink_rate=blink_rate, avg_blink_duration=0.15, perclos=perclos, yawn_count=yawns, nod_count=sum(1 for s in signals if abs(s.head_pitch) > 20), gaze_variance=np.var([s.gaze_x**2 + s.gaze_y**2 for s in signals]), posture_shifts=0, head_sway_freq=np.std([s.head_pitch for s in signals]), ) def _deviation(self, current, baseline, max_val): """计算偏离度 (0-1)""" diff = abs(current - baseline) return min(diff / max_val, 1.0) def _calc_trend(self, deviations): """计算趋势方向""" avg_dev = np.mean(list(deviations.values())) if avg_dev < 0.2: return 'stable' elif avg_dev < 0.4: return 'rising' else: return 'critical'
if __name__ == "__main__": fated = FATEDFramework(window_sec=60) np.random.seed(42) alert_signals = [ LowLevelSignals( eye_openness=0.85 + np.random.randn() * 0.05, gaze_x=np.random.randn() * 5, gaze_y=np.random.randn() * 3, head_pitch=np.random.randn() * 2, head_roll=np.random.randn() * 1, head_yaw=np.random.randn() * 3, mouth_open=np.random.rand() * 0.1, body_posture=np.random.randn(17, 2), ) for _ in range(5 * 30 * 60) ] baseline = fated.calibrate_baseline(alert_signals) print(f"基线 PERCLOS: {baseline['perclos']:.1f}%") print(f"基线 眨眼率: {baseline['blink_rate']:.1f}/min") fatigue_signals = [ LowLevelSignals( eye_openness=max(0, 0.6 + np.random.randn() * 0.1), gaze_x=np.random.randn() * 12, gaze_y=np.random.randn() * 8, head_pitch=np.random.randn() * 5 + (3 if i % 100 > 80 else 0), head_roll=np.random.randn() * 2, head_yaw=np.random.randn() * 6, mouth_open=0.5 if i % 200 > 190 else np.random.rand() * 0.1, body_posture=np.random.randn(17, 2), ) for i in range(60 * 30) ] result = fated.assess_continuum(fatigue_signals) print(f"\n疲劳评估:") print(f" 阶段: {result['stage']}") print(f" 分数: {result['score']:.2f}") print(f" 趋势: {result['trend']}") print(f" 各维度偏离: {result['deviations']}")
|