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
| class SunglassesRobustEyeTracker: """ 墨镜鲁棒眼动追踪 策略: 1. 利用镜片反射亮点追踪视线 2. 基于头部姿态推断视线方向 3. 多帧时序融合 """ def __init__(self): self.sunglasses_detector = SunglassesDetector() self.head_pose_estimator = HeadPoseEstimator() self.gaze_history = [] def track_eyes(self, image: np.ndarray) -> dict: """ 眼动追踪(墨镜鲁棒) Returns: { 'gaze_vector': (pitch, yaw), 'eye_openness': float, 'blink_detected': bool, 'tracking_method': str } """ eye_region = self._extract_eye_region(image) glasses_status = self.sunglasses_detector.detect(image, eye_region) if not glasses_status['has_sunglasses']: return self._normal_eye_tracking(eye_region) else: return self._sunglasses_eye_tracking(image, eye_region, glasses_status) def _normal_eye_tracking(self, eye_region: np.ndarray) -> dict: """正常眼动追踪""" pupil = self._detect_pupil(eye_region) openness = self._measure_eye_openness(eye_region) gaze = self._estimate_gaze_from_pupil(pupil) return { 'gaze_vector': gaze, 'eye_openness': openness, 'blink_detected': openness < 0.2, 'tracking_method': 'pupil_detection' } def _sunglasses_eye_tracking(self, image: np.ndarray, eye_region: np.ndarray, glasses_status: dict) -> dict: """ 墨镜条件下眼动追踪 方法: 1. 检测镜片反射亮点(普尔金耶像) 2. 结合头部姿态推断 3. 时序平滑 """ reflections = self._detect_corneal_reflections(eye_region) if len(reflections) >= 2: gaze = self._estimate_gaze_from_reflections(reflections) method = 'reflection_based' else: head_pose = self.head_pose_estimator.estimate(image) gaze = self._infer_gaze_from_head_pose(head_pose) method = 'head_pose_inference' openness = self._estimate_openness_from_contour(eye_region) self.gaze_history.append(gaze) if len(self.gaze_history) > 5: self.gaze_history.pop(0) smoothed_gaze = self._smooth_gaze() return { 'gaze_vector': smoothed_gaze, 'eye_openness': openness, 'blink_detected': False, 'tracking_method': method, 'glasses_type': glasses_status['type'] } def _detect_corneal_reflections(self, eye_region: np.ndarray) -> list: """ 检测角膜反射(普尔金耶像) IR LED在角膜上的反射点,位置随视线变化 """ gray = cv2.cvtColor(eye_region, cv2.COLOR_BGR2GRAY) if len(eye_region.shape) == 3 else eye_region _, thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY) contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) reflections = [] for contour in contours: M = cv2.moments(contour) if M['m00'] > 0: cx = M['m10'] / M['m00'] cy = M['m01'] / M['m00'] reflections.append((cx, cy)) return reflections def _estimate_gaze_from_reflections(self, reflections: list) -> tuple: """ 从反射点估计视线 双普尔金耶像法: - 第一普尔金耶像(P1):角膜前表面反射 - 第四普尔金耶像(P4):晶状体后表面反射 - P1-P4距离与视线角度相关 """ if len(reflections) < 2: return (0.0, 0.0) p1, p2 = reflections[0], reflections[1] dx = p2[0] - p1[0] dy = p2[1] - p1[1] pitch = np.arctan2(dy, 20) * 180 / np.pi yaw = np.arctan2(dx, 20) * 180 / np.pi pitch = np.clip(pitch, -30, 30) yaw = np.clip(yaw, -45, 45) return (float(pitch), float(yaw)) def _infer_gaze_from_head_pose(self, head_pose: dict) -> tuple: """ 从头部姿态推断视线 假设:驾驶员主要看前方,头部转动约等于视线转动 """ pitch = head_pose.get('pitch', 0) yaw = head_pose.get('yaw', 0) GAZE_HEAD_RATIO = 0.8 return (pitch * GAZE_HEAD_RATIO, yaw * GAZE_HEAD_RATIO) def _smooth_gaze(self) -> tuple: """时序平滑""" if not self.gaze_history: return (0.0, 0.0) pitches = [g[0] for g in self.gaze_history] yaws = [g[1] for g in self.gaze_history] return (float(np.median(pitches)), float(np.median(yaws)))
class HeadPoseEstimator: """头部姿态估计""" def estimate(self, image: np.ndarray) -> dict: """ 估计头部姿态 Returns: {pitch, yaw, roll} in degrees """ return {'pitch': 0.0, 'yaw': 0.0, 'roll': 0.0}
|