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
| import numpy as np from typing import Dict, Tuple from dataclasses import dataclass
@dataclass class EmotionResult: """情绪检测结果""" emotion: str confidence: float arousal: float valence: float action_units: dict
class MultimodalEmotionDetector: """多模态情绪检测器""" def __init__(self): self.facial_analyzer = FacialExpressionAnalyzer() self.physio_analyzer = PhysiologicalAnalyzer() self.driving_analyzer = DrivingBehaviorAnalyzer() self.weights = { 'facial': 0.6, 'physio': 0.2, 'driving': 0.2 } def detect(self, face_image: np.ndarray, physio_data: dict = None, driving_data: dict = None) -> EmotionResult: """检测情绪 Args: face_image: 面部图像 physio_data: { 'heart_rate': float, 'skin_conductance': float, 'eeg': np.ndarray (可选) } driving_data: { 'steering_variance': float, 'brake_count': int, 'speed_variance': float } Returns: EmotionResult """ facial_result = self.facial_analyzer.analyze(face_image) physio_result = None if physio_data: physio_result = self.physio_analyzer.analyze(physio_data) driving_result = None if driving_data: driving_result = self.driving_analyzer.analyze(driving_data) fused_emotion = self._fuse(facial_result, physio_result, driving_result) return fused_emotion def _fuse(self, facial, physio, driving) -> EmotionResult: """融合多模态结果""" if facial: return EmotionResult( emotion=facial['emotion'], confidence=facial['confidence'], arousal=facial.get('arousal', 0.5), valence=facial.get('valence', 0), action_units=facial.get('action_units', {}) ) return EmotionResult( emotion='neutral', confidence=0.5, arousal=0.5, valence=0, action_units={} )
class FacialExpressionAnalyzer: """面部表情分析器""" def __init__(self): self.au_detector = AUDetector() self.emotion_classifier = EmotionClassifier() def analyze(self, face_image: np.ndarray) -> dict: """分析面部表情 Returns: { 'emotion': str, 'confidence': float, 'arousal': float, 'valence': float, 'action_units': dict } """ aus = self.au_detector.detect(face_image) emotion, confidence = self.emotion_classifier.classify(aus) arousal = self._compute_arousal(aus) valence = self._compute_valence(emotion) return { 'emotion': emotion, 'confidence': confidence, 'arousal': arousal, 'valence': valence, 'action_units': aus } def _compute_arousal(self, aus: dict) -> float: """计算唤醒度(情绪强度)""" active_aus = sum(1 for au, intensity in aus.items() if intensity > 0.3) return min(active_aus / 10, 1.0) def _compute_valence(self, emotion: str) -> float: """计算效价(正面/负面)""" valence_map = { 'joy': 0.8, 'neutral': 0.0, 'sadness': -0.6, 'fear': -0.7, 'anger': -0.9, 'surprise': 0.3, 'disgust': -0.8 } return valence_map.get(emotion, 0)
class AUDetector: """Action Unit 检测器""" def detect(self, face_image: np.ndarray) -> dict: """检测 Action Units 返回 AU 强度(0-1) """ return { 'AU1': 0.0, 'AU2': 0.0, 'AU4': 0.0, 'AU5': 0.0, 'AU6': 0.0, 'AU7': 0.0, 'AU9': 0.0, 'AU12': 0.0, 'AU15': 0.0, 'AU17': 0.0, 'AU20': 0.0, 'AU23': 0.0, 'AU24': 0.0, 'AU25': 0.0, 'AU26': 0.0, 'AU27': 0.0, }
class EmotionClassifier: """情绪分类器""" def __init__(self): self.emotion_rules = { 'anger': ['AU4', 'AU5', 'AU7', 'AU23'], 'fear': ['AU1', 'AU2', 'AU5', 'AU20'], 'sadness': ['AU1', 'AU15', 'AU17'], 'joy': ['AU6', 'AU12'], 'surprise': ['AU1', 'AU2', 'AU5', 'AU26'], 'disgust': ['AU9', 'AU15', 'AU17'], } def classify(self, aus: dict) -> Tuple[str, float]: """分类情绪 Returns: (emotion, confidence) """ scores = {} for emotion, required_aus in self.emotion_rules.items(): score = sum(aus.get(au, 0) for au in required_aus) / len(required_aus) scores[emotion] = score best_emotion = max(scores, key=scores.get) confidence = scores[best_emotion] if confidence < 0.3: return 'neutral', 1 - confidence return best_emotion, confidence
|