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 271 272 273 274
| """ 驾驶员认知负荷多模态融合估计器
基于 IEEE TITS 2024 + Frontiers in Neuroergonomics 2023 方法 融合: 眼动 + 驾驶行为 + 语音 + 生理信号 依赖: pip install numpy scipy torch """
import numpy as np from typing import Dict, List, Tuple, Optional from dataclasses import dataclass from enum import IntEnum
class CognitiveLoadLevel(IntEnum): LOW = 0 MODERATE = 1 HIGH = 2 OVERLOAD = 3
@dataclass class EyeFeatures: """眼动特征""" pupil_diameter_mm: float = 4.0 pupil_var: float = 0.2 saccade_freq: float = 2.0 fixation_stability: float = 0.8 gaze_entropy: float = 3.5 blink_rate: float = 15.0
@dataclass class DrivingBehaviorFeatures: """驾驶行为特征""" steering_correction_freq: float = 8.0 lane_deviation_std: float = 0.1 brake_reaction_delay: float = 0.5 speed_var: float = 2.0 acceleration_var: float = 0.1
@dataclass class VoiceFeatures: """语音特征""" speech_rate: float = 0.5 pause_count: int = 5 pitch_mean: float = 130.0 pitch_var: float = 15.0
@dataclass class PhysioFeatures: """生理特征""" hrv_rmssd: float = 40.0 breathing_rate: float = 16.0 breathing_var: float = 2.0
class CognitiveLoadEstimator: """ 多模态认知负荷估计器 方法: 加权证据融合 + 时序平滑 输出: 认知负荷等级(0-3) + 分数(0-100) """ def __init__(self): self.weights = { 'eye': 0.30, 'driving': 0.25, 'voice': 0.15, 'physio': 0.30, } self.history = [] self.history_max = 30 def estimate_eye_load(self, eye: EyeFeatures) -> float: """ 从眼动特征估计负荷(0-1) 指标: - 瞳孔变异增大 = 负荷↑ - 扫视频率下降 = 负荷↑(隧道视野效应) - 注视稳定性↑ = 负荷↑(固定凝视) - 扫视熵↓ = 负荷↑(扫描范围缩小) """ pupil_score = min(eye.pupil_var / 0.5, 1.0) saccade_score = max(0, 1 - eye.saccade_freq / 3.0) fix_score = max(0, eye.fixation_stability - 0.7) / 0.3 entropy_score = max(0, 1 - eye.gaze_entropy / 4.0) eye_load = ( 0.30 * pupil_score + 0.25 * saccade_score + 0.20 * fix_score + 0.25 * entropy_score ) return min(eye_load, 1.0) def estimate_driving_load(self, driving: DrivingBehaviorFeatures) -> float: """从驾驶行为估计负荷""" steering_score = max(0, 1 - driving.steering_correction_freq / 12.0) lane_score = min(driving.lane_deviation_std / 0.5, 1.0) brake_score = min((driving.brake_reaction_delay - 0.4) / 0.6, 1.0) brake_score = max(0, brake_score) speed_score = min(driving.speed_var / 10.0, 1.0) return ( 0.30 * steering_score + 0.25 * lane_score + 0.25 * brake_score + 0.20 * speed_score ) def estimate_voice_load(self, voice: VoiceFeatures) -> float: """从语音特征估计负荷""" if voice.speech_rate > 0.7: rate_score = 0.6 elif voice.speech_rate < 0.3: rate_score = 0.5 else: rate_score = 0.2 pause_score = min(voice.pause_count / 15, 1.0) pitch_score = max(0, (voice.pitch_mean - 130) / 50) return (rate_score + pause_score + pitch_score) / 3 def estimate_physio_load(self, physio: PhysioFeatures) -> float: """从生理信号估计负荷""" hrv_score = max(0, 1 - physio.hrv_rmssd / 40.0) breath_var_score = max(0, 1 - physio.breathing_var / 3.0) if physio.breathing_rate > 20 or physio.breathing_rate < 12: breath_rate_score = 0.6 else: breath_rate_score = 0.2 return ( 0.50 * hrv_score + 0.30 * breath_var_score + 0.20 * breath_rate_score ) def estimate(self, eye: EyeFeatures, driving: DrivingBehaviorFeatures, voice: Optional[VoiceFeatures] = None, physio: Optional[PhysioFeatures] = None) -> Dict: """ 综合认知负荷估计 Returns: load_score: 0-100 level: CognitiveLoadLevel modality_scores: 各模态分数 confidence: 置信度 """ eye_load = self.estimate_eye_load(eye) driving_load = self.estimate_driving_load(driving) if voice: voice_load = self.estimate_voice_load(voice) else: voice_load = 0.5 if physio: physio_load = self.estimate_physio_load(physio) else: physio_load = 0.5 load_score = ( self.weights['eye'] * eye_load + self.weights['driving'] * driving_load + self.weights['voice'] * voice_load + self.weights['physio'] * physio_load ) self.history.append(load_score) if len(self.history) > self.history_max: self.history.pop(0) smoothed = np.mean(self.history) if smoothed < 0.25: level = CognitiveLoadLevel.LOW elif smoothed < 0.50: level = CognitiveLoadLevel.MODERATE elif smoothed < 0.75: level = CognitiveLoadLevel.HIGH else: level = CognitiveLoadLevel.OVERLOAD n_modalities = sum([ 1, 1, 1 if voice else 0, 1 if physio else 0, ]) confidence = min(n_modalities / 4, 1.0) return { 'load_score': round(smoothed * 100, 1), 'level': level.name, 'modality_scores': { 'eye': round(eye_load, 2), 'driving': round(driving_load, 2), 'voice': round(voice_load, 2) if voice else None, 'physio': round(physio_load, 2) if physio else None, }, 'confidence': round(confidence, 2), }
if __name__ == "__main__": np.random.seed(42) estimator = CognitiveLoadEstimator() low_eye = EyeFeatures(pupil_var=0.1, saccade_freq=3.0, fixation_stability=0.7, gaze_entropy=4.0) low_drive = DrivingBehaviorFeatures(steering_correction_freq=10, brake_reaction_delay=0.4) low_physio = PhysioFeatures(hrv_rmssd=50, breathing_var=3.0) high_eye = EyeFeatures(pupil_var=0.4, saccade_freq=1.0, fixation_stability=0.9, gaze_entropy=2.0) high_drive = DrivingBehaviorFeatures(steering_correction_freq=3, lane_deviation_std=0.3, brake_reaction_delay=0.8) high_physio = PhysioFeatures(hrv_rmssd=15, breathing_var=1.0) print("=== 低负荷(正常巡航)===") result_low = estimator.estimate(low_eye, low_drive, physio=low_physio) for k, v in result_low.items(): print(f" {k}: {v}") print(f"\n=== 高负荷(复杂路口)===") result_high = estimator.estimate(high_eye, high_drive, physio=high_physio) for k, v in result_high.items(): print(f" {k}: {v}") print(f"\n=== 差异 ===") print(f"负荷分数: 低={result_low['load_score']}, 高={result_high['load_score']}") print(f"等级: 低={result_low['level']}, 高={result_high['level']}")
|