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
| """ Mobileye 上下文感知 DMS 算法复现
融合驾驶员视线 + 道路场景判断分心 """
import numpy as np from typing import Dict, List
class ContextAwareDMS: """ 上下文感知 DMS 融合驾驶员视线和道路场景判断分心 """ def __init__(self): self.legal_gaze_zones = { 'road_ahead': {'center': (0, 0), 'radius': 15}, 'left_mirror': {'center': (-50, 5), 'radius': 10}, 'right_mirror': {'center': (50, 5), 'radius': 10}, 'rear_mirror': {'center': (0, 20), 'radius': 10}, 'speedometer': {'center': (10, -20), 'radius': 8}, } self.distraction_threshold = 3.0 def classify_gaze( self, gaze_point: tuple, road_scene: dict ) -> Dict: """ 分类视线行为 Args: gaze_point: 视线落点 (x, y) 度 road_scene: 道路场景信息 { 'has_hazard': bool, 'curve_ahead': bool, 'intersection': bool } Returns: { 'is_legal': bool, 'gaze_zone': str, 'distraction_level': float } """ for zone_name, zone_info in self.legal_gaze_zones.items(): distance = np.sqrt( (gaze_point[0] - zone_info['center'][0])**2 + (gaze_point[1] - zone_info['center'][1])**2 ) if distance <= zone_info['radius']: if self._is_zone_legal_in_scene(zone_name, road_scene): return { 'is_legal': True, 'gaze_zone': zone_name, 'distraction_level': 0.0 } return { 'is_legal': False, 'gaze_zone': 'other', 'distraction_level': 1.0 } def _is_zone_legal_in_scene( self, zone: str, scene: dict ) -> bool: """判断该场景下该区域是否合法""" if scene.get('has_hazard'): return zone == 'road_aahead' if scene.get('curve_ahead'): return zone in ['road_ahead', 'left_mirror', 'right_mirror'] return True def detect_distraction( self, gaze_history: List[dict], scene_history: List[dict] ) -> Dict: """ 检测分心 Args: gaze_history: 最近 N 帧视线历史 scene_history: 最近 N 帧场景历史 Returns: { 'is_distracted': bool, 'distraction_type': str, 'duration': float } """ distraction_duration = 0.0 distraction_type = None for gaze, scene in zip(gaze_history, scene_history): result = self.classify_gaze(gaze['point'], scene) if not result['is_legal']: distraction_duration += gaze.get('dt', 0.033) if distraction_type is None: distraction_type = 'visual_distraction' else: distraction_duration = 0.0 distraction_type = None return { 'is_distracted': distraction_duration >= self.distraction_threshold, 'distraction_type': distraction_type, 'duration': distraction_duration }
if __name__ == "__main__": dms = ContextAwareDMS() gaze_history = [ {'point': (0, 0), 'dt': 0.5}, {'point': (30, -30), 'dt': 0.5}, {'point': (30, -30), 'dt': 0.5}, {'point': (30, -30), 'dt': 0.5}, {'point': (30, -30), 'dt': 0.5}, {'point': (30, -30), 'dt': 0.5}, {'point': (0, 0), 'dt': 0.5}, ] scene_history = [ {'has_hazard': False, 'curve_ahead': False} for _ in gaze_history ] result = dms.detect_distraction(gaze_history, scene_history) print(f"是否分心: {result['is_distracted']}") print(f"分心类型: {result['distraction_type']}") print(f"持续时长: {result['duration']:.1f}s")
|