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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
| """ 航空级飞行员疲劳监测系统 基于Boeing 787的参考实现
核心算法: 1. PERCLOS计算(P80L标准) 2. 眨眼频率统计 3. 扫视模式分析 4. 仪表注视率(航空特有)
与汽车IMS的对比: - 航空:仪表区固定,注视率可量化 - 汽车:道路场景复杂,需场景理解 """
import numpy as np from typing import Tuple, Dict
class AviationGradeFatigueDetector: """ 航空级疲劳检测器 参考标准: - FAA AC 117-3: Crew Member Fatigue - SAE ARP5416: Flight Crew Alerting 核心指标: 1. PERCLOS(Percentage of Eyelid Closure) 2. 眨眼频率(Blink Rate) 3. 扫视速度(Saccade Velocity) 4. 仪表注视率(Instrument Gaze Ratio) """ def __init__( self, fps: float = 30.0, perclos_window: int = 60, perclos_threshold: float = 0.8, blink_rate_normal: Tuple[float, float] = (15, 25), fatigue_level_thresholds: Dict = None ): self.fps = fps self.perclos_window = perclos_window self.perclos_threshold = perclos_threshold self.blink_rate_normal = blink_rate_normal self.fatigue_thresholds = fatigue_level_thresholds or { 'normal': {'perclos': 15, 'blink_rate': 20}, 'mild': {'perclos': 25, 'blink_rate': 30}, 'moderate': {'perclos': 35, 'blink_rate': 40}, 'severe': {'perclos': 50, 'blink_rate': 50} } def calculate_perclos( self, eyelid_openness: np.ndarray ) -> float: """ 计算PERCLOS值 定义:在时间窗口内,眼睑开度低于阈值的帧数占比 航空标准: - PERCLOS < 15%: 正常 - PERCLOS 15-25%: 轻度疲劳 - PERCLOS 25-35%: 中度疲劳 - PERCLOS > 35%: 重度疲劳 Args: eyelid_openness: [N] 眼睑开度序列(0-1) Returns: perclos: PERCLOS百分比 """ closed_frames = eyelid_openness < self.perclos_threshold perclos = np.sum(closed_frames) / len(eyelid_openness) * 100 return perclos def calculate_blink_rate( self, eyelid_openness: np.ndarray, min_blink_duration: float = 0.1, max_blink_duration: float = 0.5 ) -> Tuple[float, np.ndarray]: """ 计算眨眼频率 方法: 1. 检测眼睑从开到闭再到开的过程 2. 过滤掉非眨眼事件(过长或过短) 3. 统计频率(次/分钟) Args: eyelid_openness: [N] 眼睑开度序列 Returns: blink_rate: 眨眼频率(次/分钟) blink_events: 眨眼事件时间戳 """ N = len(eyelid_openness) closed = eyelid_openness < self.perclos_threshold diff = np.diff(closed.astype(int)) start_indices = np.where(diff == 1)[0] + 1 end_indices = np.where(diff == -1)[0] + 1 if len(start_indices) == 0 or len(end_indices) == 0: return 0.0, np.array([]) min_len = min(len(start_indices), len(end_indices)) start_indices = start_indices[:min_len] end_indices = end_indices[:min_len] blink_durations = (end_indices - start_indices) / self.fps valid_blinks = (blink_durations >= min_blink_duration) & \ (blink_durations <= max_blink_duration) blink_events = start_indices[valid_blinks] total_time_minutes = N / self.fps / 60.0 blink_rate = len(blink_events) / total_time_minutes if total_time_minutes > 0 else 0.0 return blink_rate, blink_events def calculate_instrument_gaze_ratio( self, gaze_points: np.ndarray, instrument_regions: Dict[str, np.ndarray], frame_size: Tuple[int, int] = (1920, 1080) ) -> float: """ 计算仪表注视率(航空特有指标) 定义:视线落在仪表板区域的时间占比 航空经验: - 正常:>60%注视仪表区 - 疲劳:注视率下降,更多"发呆" Args: gaze_points: [N, 2] 视线坐标(像素) instrument_regions: 各仪表区域的边界框 frame_size: 图像尺寸 Returns: gaze_ratio: 仪表注视率(0-1) """ N = len(gaze_points) instrument_frames = 0 for gaze in gaze_points: x, y = gaze for region_name, bbox in instrument_regions.items(): x1, y1, x2, y2 = bbox if x1 <= x <= x2 and y1 <= y <= y2: instrument_frames += 1 break gaze_ratio = instrument_frames / N if N > 0 else 0.0 return gaze_ratio def detect_fatigue_level( self, eyelid_openness: np.ndarray, gaze_points: np.ndarray = None, instrument_regions: Dict = None ) -> Dict: """ 综合疲劳等级检测 Returns: result: 包含疲劳等级、各项指标、判定依据 """ perclos = self.calculate_perclos(eyelid_openness) blink_rate, blink_events = self.calculate_blink_rate(eyelid_openness) instrument_gaze_ratio = None if gaze_points is not None and instrument_regions is not None: instrument_gaze_ratio = self.calculate_instrument_gaze_ratio( gaze_points, instrument_regions ) fatigue_level = 'normal' if perclos > self.fatigue_thresholds['severe']['perclos']: fatigue_level = 'severe' elif perclos > self.fatigue_thresholds['moderate']['perclos']: fatigue_level = 'moderate' elif perclos > self.fatigue_thresholds['mild']['perclos']: fatigue_level = 'mild' result = { 'fatigue_level': fatigue_level, 'perclos': perclos, 'blink_rate': blink_rate, 'blink_count': len(blink_events), 'instrument_gaze_ratio': instrument_gaze_ratio, 'thresholds': self.fatigue_thresholds, 'alert_required': fatigue_level in ['moderate', 'severe'] } return result
if __name__ == "__main__": np.random.seed(42) N = 1800 eyelid_normal = np.random.normal(0.9, 0.05, N) eyelid_normal = np.clip(eyelid_normal, 0, 1) eyelid_fatigue = np.random.normal(0.7, 0.15, N) eyelid_fatigue = np.clip(eyelid_fatigue, 0, 1) eyelid_fatigue[1000:1050] = 0.2 detector = AviationGradeFatigueDetector(fps=30.0) result_normal = detector.detect_fatigue_level(eyelid_normal) print("=" * 60) print("正常驾驶疲劳检测") print("=" * 60) print(f"疲劳等级: {result_normal['fatigue_level']}") print(f"PERCLOS: {result_normal['perclos']:.2f}%") print(f"眨眼频率: {result_normal['blink_rate']:.1f}次/分钟") result_fatigue = detector.detect_fatigue_level(eyelid_fatigue) print("\n" + "=" * 60) print("疲劳驾驶检测") print("=" * 60) print(f"疲劳等级: {result_fatigue['fatigue_level']}") print(f"PERCLOS: {result_fatigue['perclos']:.2f}%") print(f"眨眼频率: {result_fatigue['blink_rate']:.1f}次/分钟") print(f"是否需要警告: {result_fatigue['alert_required']}")
|