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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
| import numpy as np from typing import Dict, Tuple, List
class MeaningfulEngagementDetector: """ 有意义参与检测器 核心功能: 1. 关联驾驶员视线与路况 2. 判断驾驶员是否"理解"当前驾驶场景 3. 预测驾驶员响应意图 """ def __init__(self): self.road_context = { "lane_position": None, "lead_vehicle": None, "road_signs": [], "hazards": [] } self.driver_state = { "gaze_direction": None, "eye_closure": None, "head_pose": None, "attention_score": 0.0 } self.history = [] self.history_length = 30 def update_road_context( self, lane_position: float, lead_vehicle: dict, road_signs: list, hazards: list ): """ 更新路况信息(来自ADAS摄像头) Args: lane_position: 车道中心偏差(米) lead_vehicle: 前车信息 {"distance": m, "speed": km/h, "type": str} road_signs: 道路标志列表 [{"type": str, "distance": m}] hazards: 危险列表 [{"type": str, "position": (x,y), "risk": 0-1}] """ self.road_context = { "lane_position": lane_position, "lead_vehicle": lead_vehicle, "road_signs": road_signs, "hazards": hazards } def update_driver_state( self, gaze_direction: Tuple[float, float], eye_closure: float, head_pose: Tuple[float, float, float] ): """ 更新驾驶员状态(来自DMS摄像头) Args: gaze_direction: 视线方向(方位角, 俯仰角),度 eye_closure: 眼睑闭合度 [0, 1] head_pose: 头部姿态(yaw, pitch, roll),度 """ self.driver_state = { "gaze_direction": gaze_direction, "eye_closure": eye_closure, "head_pose": head_pose, "attention_score": self._calculate_attention_score() } self.history.append({ "road": self.road_context.copy(), "driver": self.driver_state.copy() }) if len(self.history) > self.history_length: self.history.pop(0) def _calculate_attention_score(self) -> float: """ 计算注意力分数 Returns: score: 注意力分数 [0, 1] """ gaze = self.driver_state.get("gaze_direction", (0, 0)) gaze_on_road = abs(gaze[0]) < 20 and abs(gaze[1]) < 15 eye_open = self.driver_state.get("eye_closure", 1.0) < 0.3 head = self.driver_state.get("head_pose", (0, 0, 0)) head_forward = abs(head[0]) < 30 and abs(head[1]) < 20 score = 0.0 if gaze_on_road: score += 0.5 if eye_open: score += 0.3 if head_forward: score += 0.2 return score def compute_engagement(self) -> Dict: """ 计算有意义参与度 Returns: engagement: { "score": 参与度分数 [0, 1], "status": "engaged" | "distracted" | "dangerous", "reasons": 参与度低的原因, "recommendations": 建议的干预措施 } """ attention = self.driver_state["attention_score"] gaze_road_alignment = self._check_gaze_road_alignment() hazard_response = self._check_hazard_response() historical_pattern = self._analyze_historical_pattern() engagement_score = ( 0.3 * attention + 0.3 * gaze_road_alignment + 0.2 * hazard_response + 0.2 * historical_pattern ) if engagement_score > 0.7: status = "engaged" elif engagement_score > 0.4: status = "distracted" else: status = "dangerous" reasons = [] if attention < 0.5: reasons.append("注意力不集中") if gaze_road_alignment < 0.5: reasons.append("视线与路况不匹配") if hazard_response < 0.5: reasons.append("未正确响应路况危险") recommendations = self._generate_recommendations(status, reasons) return { "score": engagement_score, "status": status, "reasons": reasons, "recommendations": recommendations, "details": { "attention": attention, "gaze_road_alignment": gaze_road_alignment, "hazard_response": hazard_response, "historical_pattern": historical_pattern } } def _check_gaze_road_alignment(self) -> float: """ 检查视线是否与当前路况匹配 Returns: alignment: 匹配度 [0, 1] """ gaze = self.driver_state.get("gaze_direction", (0, 0)) lead_vehicle = self.road_context.get("lead_vehicle") if lead_vehicle and lead_vehicle["distance"] < 50: if abs(gaze[0]) < 15 and abs(gaze[1]) < 10: return 1.0 else: return 0.3 road_signs = self.road_context.get("road_signs", []) for sign in road_signs: if sign["distance"] < 100: pass if abs(gaze[0]) < 20 and abs(gaze[1]) < 15: return 0.9 else: return 0.4 def _check_hazard_response(self) -> float: """ 检查驾驶员是否正确响应路况危险 Returns: response_score: 响应分数 [0, 1] """ hazards = self.road_context.get("hazards", []) if not hazards: return 1.0 response_scores = [] for hazard in hazards: if hazard["risk"] > 0.5: hazard_angle = hazard.get("angle", 0) gaze = self.driver_state.get("gaze_direction", (0, 0)) angle_diff = abs(gaze[0] - hazard_angle) if angle_diff < 15: response_scores.append(1.0) elif angle_diff < 30: response_scores.append(0.6) else: response_scores.append(0.2) else: response_scores.append(1.0) return np.mean(response_scores) if response_scores else 1.0 def _analyze_historical_pattern(self) -> float: """ 分析历史行为模式 Returns: pattern_score: 模式分数 [0, 1] """ if len(self.history) < 10: return 0.5 recent_history = self.history[-10:] attention_scores = [ h["driver"]["attention_score"] for h in recent_history ] mean_attention = np.mean(attention_scores) trend = np.polyfit(range(len(attention_scores)), attention_scores, 1)[0] if trend < -0.05: return max(0.3, mean_attention - 0.2) else: return mean_attention def _generate_recommendations( self, status: str, reasons: List[str] ) -> List[str]: """ 生成干预建议 Args: status: 当前状态 reasons: 参与度低的原因 Returns: recommendations: 建议列表 """ recommendations = [] if status == "dangerous": recommendations.append("⚠️ 立即发出声光警报") recommendations.append("⚠️ 准备紧急停车程序") if "未正确响应路况危险" in reasons: recommendations.append("⚠️ 增强ADAS介入等级") elif status == "distracted": recommendations.append("📢 发出注意力提醒") if "视线与路况不匹配" in reasons: recommendations.append("📢 提示驾驶员注意前方") if "注意力不集中" in reasons: recommendations.append("📢 建议驾驶员休息") else: recommendations.append("✅ 驾驶员状态正常") return recommendations
if __name__ == "__main__": detector = MeaningfulEngagementDetector() print("=== 场景1:正常驾驶 ===") detector.update_road_context( lane_position=0.1, lead_vehicle={"distance": 50, "speed": 80, "type": "car"}, road_signs=[], hazards=[] ) detector.update_driver_state( gaze_direction=(5, 3), eye_closure=0.1, head_pose=(0, 0, 0) ) result = detector.compute_engagement() print(f"参与度分数: {result['score']:.2f}") print(f"状态: {result['status']}") print(f"建议: {result['recommendations']}") print("\n=== 场景2:看路但发呆(眼动追踪正常,有意义参与低)===") detector.update_road_context( lane_position=0.3, lead_vehicle={"distance": 30, "speed": 60, "type": "car"}, road_signs=[], hazards=[{"type": "braking", "position": (0, 30), "risk": 0.8}] ) detector.update_driver_state( gaze_direction=(3, 2), eye_closure=0.15, head_pose=(0, 0, 0) ) for i in range(10): detector.update_driver_state( gaze_direction=(3 + i * 0.5, 2), eye_closure=0.15, head_pose=(i, 0, 0) ) result = detector.compute_engagement() print(f"参与度分数: {result['score']:.2f}") print(f"状态: {result['status']}") print(f"原因: {result['reasons']}") print(f"建议: {result['recommendations']}") print("\n=== 场景3:看后视镜(眼动追踪误判,有意义参与正确)===") detector.update_road_context( lane_position=0.1, lead_vehicle={"distance": 40, "speed": 70, "type": "car"}, road_signs=[], hazards=[] ) detector.update_driver_state( gaze_direction=(-35, 10), eye_closure=0.1, head_pose=(-20, 5, 0) ) result = detector.compute_engagement() print(f"参与度分数: {result['score']:.2f}") print(f"状态: {result['status']}") print(f"建议: {result['recommendations']}") print("注意:眼动追踪可能判定为分心,但有意义参与检测识别为正常驾驶行为")
|