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
| """ Tesla FSD眼动追踪算法推断
基于摄像头图像分析驾驶员视线方向 """
import numpy as np from typing import Tuple, Optional from dataclasses import dataclass
@dataclass class GazeEstimation: """视线估计结果""" pitch: float yaw: float confidence: float looking_at_road: bool
class TeslaGazeTracker: """ Tesla风格视线追踪器 特点: 1. 纯视觉方案 2. 面部关键点检测 3. 眼球方向估计 4. 实时判断是否看路 """ def __init__(self): self.LEFT_EYE_INDICES = list(range(36, 42)) self.RIGHT_EYE_INDICES = list(range(42, 48)) self.ROAD_REGION = { 'pitch_range': (-20, 20), 'yaw_range': (-30, 30) } def estimate_gaze( self, face_landmarks: np.ndarray, eye_image: Optional[np.ndarray] = None ) -> GazeEstimation: """ 估计视线方向 Args: face_landmarks: 面部关键点 (68点), shape=(68, 2) eye_image: 眼部图像(可选,用于更高精度) Returns: GazeEstimation: 视线估计结果 """ left_eye = face_landmarks[self.LEFT_EYE_INDICES] right_eye = face_landmarks[self.RIGHT_EYE_INDICES] left_center = np.mean(left_eye, axis=0) right_center = np.mean(right_eye, axis=0) head_pitch, head_yaw = self._estimate_head_pose(face_landmarks) eye_pitch, eye_yaw = 0, 0 if eye_image is not None: eye_pitch, eye_yaw = self._estimate_eye_direction(eye_image) total_pitch = head_pitch + eye_pitch total_yaw = head_yaw + eye_yaw looking_at_road = self._is_looking_at_road(total_pitch, total_yaw) return GazeEstimation( pitch=total_pitch, yaw=total_yaw, confidence=0.9, looking_at_road=looking_at_road ) def _estimate_head_pose(self, landmarks: np.ndarray) -> Tuple[float, float]: """ 估计头部姿态 Returns: (pitch, yaw) in degrees """ nose = landmarks[30] left_eye_center = np.mean(landmarks[36:42], axis=0) right_eye_center = np.mean(landmarks[42:48], axis=0) eye_center = (left_eye_center + right_eye_center) / 2 yaw = (nose[0] - eye_center[0]) * 0.5 chin = landmarks[8] forehead = landmarks[27] pitch = (nose[1] - (chin[1] + forehead[1]) / 2) * 0.3 return pitch, yaw def _estimate_eye_direction(self, eye_image: np.ndarray) -> Tuple[float, float]: """ 估计眼球方向 基于眼部图像分析瞳孔位置 """ return 0, 0 def _is_looking_at_road(self, pitch: float, yaw: float) -> bool: """判断是否在看路""" return ( self.ROAD_REGION['pitch_range'][0] <= pitch <= self.ROAD_REGION['pitch_range'][1] and self.ROAD_REGION['yaw_range'][0] <= yaw <= self.ROAD_REGION['yaw_range'][1] )
class FSDMonitoringMode: """FSD监控模式""" def __init__(self, mode: str = 'Standard'): self.mode = mode self.thresholds = { 'Standard': { 'max_distraction_time': 3.0, 'gaze_away_threshold': 30, 'warning_cooldown': 10, }, 'Hurry': { 'max_distraction_time': 2.0, 'gaze_away_threshold': 25, 'warning_cooldown': 8, }, 'Mad Max': { 'max_distraction_time': 1.0, 'gaze_away_threshold': 20, 'warning_cooldown': 5, } } def check_driver_attention( self, gaze: GazeEstimation, distraction_time: float ) -> dict: """ 检查驾驶员注意力 Returns: { 'is_alert': 是否需要警告, 'alert_type': 警告类型, 'remaining_time': 剩余时间 } """ threshold = self.thresholds[self.mode] if not gaze.looking_at_road: remaining = threshold['max_distraction_time'] - distraction_time if remaining <= 0: return { 'is_alert': True, 'alert_type': 'TAKE_OVER', 'remaining_time': 0 } elif remaining <= 1: return { 'is_alert': True, 'alert_type': 'WARNING', 'remaining_time': remaining } else: return { 'is_alert': False, 'alert_type': None, 'remaining_time': remaining } else: return { 'is_alert': False, 'alert_type': None, 'remaining_time': threshold['max_distraction_time'] }
if __name__ == "__main__": tracker = TeslaGazeTracker() landmarks = np.random.randn(68, 2) * 100 + 200 gaze = tracker.estimate_gaze(landmarks) print("Tesla FSD视线追踪测试:") print(f" 俯仰角: {gaze.pitch:.1f}°") print(f" 偏航角: {gaze.yaw:.1f}°") print(f" 是否看路: {gaze.looking_at_road}") for mode in ['Standard', 'Hurry', 'Mad Max']: monitor = FSDMonitoringMode(mode) result = monitor.check_driver_attention(gaze, 0.5) print(f"\n{mode}模式:") print(f" 最大分心时间: {monitor.thresholds[mode]['max_distraction_time']}秒")
|