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
| """ 多波长红外眼动追踪 墨镜遮挡应对方案
波长选择: - 850nm:穿透普通墨镜较好,瞳孔检测清晰 - 940nm:隐蔽性好,但墨镜穿透差 - 780nm:穿透最佳,但可见红光(用户体验差)
策略: 1. 双波长交替照射(850nm+940nm) 2. 根据墨镜类型自动切换波长 3. 融合双波长检测结果 """
import numpy as np import cv2 from typing import Dict, Tuple from dataclasses import dataclass
@dataclass class SunglassesDetection: """墨镜检测结果""" sunglasses_detected: bool transmittance_estimate: float sunglasses_type: str ir_850nm_detected: bool ir_940nm_detected: bool wavelength_selection: str
class MultiWavelengthIREyeTracker: """ 多波长红外眼动追踪器 墨镜遮挡应对: 1. 检测墨镜类型(透光率估计) 2. 选择最佳波长(850nm/940nm/融合) 3. 眼动检测 + 眼睑检测 Euro NCAP应对策略: - 清墨镜(>70%透光率):850nm正常检测 - 浅墨镜(30%-70%透光率):850nm+增强曝光 - 暗墨镜(<15%透光率):通知驾驶员(无法检测) """ def __init__(self, ir_850nm_camera_config: Dict, ir_940nm_camera_config: Dict): """ Args: ir_850nm_camera_config: 850nm红外摄像头配置 ir_940nm_camera_config: 940nm红外摄像头配置 """ self.850nm_config = ir_850nm_camera_config self.940nm_config = ir_940nm_camera_config self.clear_threshold = 0.70 self.light_threshold = 0.30 self.dark_threshold = 0.15 self.eye_detector_850nm = None self.eye_detector_940nm = None def detect_sunglasses(self, rgb_image: np.ndarray, ir_850nm_image: np.ndarray) -> SunglassesDetection: """ 检测墨镜类型 方法: 1. RGB图像检测眼镜轮廓 2. RGB vs IR亮度对比估计透光率 Args: rgb_image: RGB可见光图像 ir_850nm_image: 850nm红外图像 Returns: SunglassesDetection: 墨镜检测结果 """ glasses_detected = self._detect_glasses_contour(rgb_image) if not glasses_detected: return SunglassesDetection( sunglasses_detected=False, transmittance_estimate=1.0, sunglasses_type="none", ir_850nm_detected=True, ir_940nm_detected=True, wavelength_selection="850nm" ) transmittance = self._estimate_transmittance(rgb_image, ir_850nm_image) if transmittance > self.clear_threshold: glasses_type = "clear" elif transmittance > self.light_threshold: glasses_type = "light" elif transmittance > self.dark_threshold: glasses_type = "medium" else: glasses_type = "dark" ir_850nm_detected = self._check_ir_detection(ir_850nm_image) ir_940nm_detected = True if glasses_type in ["clear", "light"]: wavelength_selection = "850nm" elif glasses_type == "medium": wavelength_selection = "fusion" else: wavelength_selection = "notify_driver" return SunglassesDetection( sunglasses_detected=True, transmittance_estimate=transmittance, sunglasses_type=glasses_type, ir_850nm_detected=ir_850nm_detected, ir_940nm_detected=ir_940nm_detected, wavelength_selection=wavelength_selection ) def _detect_glasses_contour(self, rgb_image: np.ndarray) -> bool: """ 检测眼镜轮廓 方法: - Haar特征分类器 - 或深度学习眼镜检测模型 """ gray = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2GRAY) edges = cv2.Canny(gray, 50, 150) contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) glasses_detected = len(contours) > 2 return glasses_detected def _estimate_transmittance(self, rgb_image: np.ndarray, ir_image: np.ndarray) -> float: """ 估计墨镜透光率 方法: - RGB亮度(眼镜区域)vs IR亮度(眼镜区域) - 红外透光率通常高于可见光透光率 Args: rgb_image: RGB图像 ir_image: 红外图像 Returns: transmittance: 估计透光率 """ rgb_brightness = np.mean(rgb_image[100:200, 200:400]) ir_brightness = np.mean(ir_image[100:200, 200:400]) transmittance = rgb_brightness / (ir_brightness + 1e-6) transmittance = np.clip(transmittance, 0.0, 1.0) return transmittance def _check_ir_detection(self, ir_image: np.ndarray) -> bool: """ 检查红外眼动检测是否成功 方法: - 眼睑检测 - 瞳孔检测 Args: ir_image: 红外图像 Returns: detected: 是否成功检测 """ eye_region_brightness = np.mean(ir_image[100:150, 300:380]) detection_threshold = 50 detected = eye_region_brightness > detection_threshold return detected def track_eye_with_sunglasses(self, ir_850nm_image: np.ndarray, ir_940nm_image: np.ndarray, sunglasses_info: SunglassesDetection) -> Dict: """ 墨镜遮挡下的眼动追踪 Args: ir_850nm_image: 850nm红外图像 ir_940nm_image: 940nm红外图像 sunglasses_info: 墨镜检测结果 Returns: eye_tracking_result: 眼动追踪结果 """ wavelength = sunglasses_info.wavelength_selection if wavelength == "notify_driver": return { 'action': 'notify_driver', 'reason': 'dark_sunglasses_occlusion', 'eye_detected': False, 'gaze_direction': None, 'eyes_closed': None } if wavelength == "850nm": primary_image = ir_850nm_image elif wavelength == "940nm": primary_image = ir_940nm_image else: primary_image = self._fuse_dual_wavelength(ir_850nm_image, ir_940nm_image) eye_detected = self._check_ir_detection(primary_image) eyes_closed = self._detect_eyes_closed(primary_image) gaze_direction = self._estimate_gaze_direction(primary_image) return { 'action': 'eye_tracking', 'wavelength': wavelength, 'eye_detected': eye_detected, 'gaze_direction': gaze_direction, 'eyes_closed': eyes_closed, 'sunglasses_type': sunglasses_info.sunglasses_type, 'transmittance': sunglasses_info.transmittance_estimate } def _fuse_dual_wavelength(self, ir_850nm: np.ndarray, ir_940nm: np.ndarray) -> np.ndarray: """ 双波长融合 方法: - 加权平均(850nm权重较高,穿透更好) - 或最大值融合(保留最亮特征) Args: ir_850nm: 850nm红外图像 ir_940nm: 940nm红外图像 Returns: fused_image: 融合图像 """ fused = 0.7 * ir_850nm + 0.3 * ir_940nm return fused.astype(np.uint8) def _detect_eyes_closed(self, ir_image: np.ndarray) -> bool: """眼睑检测(简化)""" return False def _estimate_gaze_direction(self, ir_image: np.ndarray) -> Tuple[float, float]: """注视方向估计(简化)""" return (0.5, 0.5)
if __name__ == "__main__": tracker = MultiWavelengthIREyeTracker( ir_850nm_camera_config={'fps': 60, 'resolution': (640, 480)}, ir_940nm_camera_config={'fps': 60, 'resolution': (640, 480)} ) rgb_test = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8) ir_850nm_test = np.random.randint(0, 255, (480, 640), dtype=np.uint8) ir_940nm_test = np.random.randint(0, 255, (480, 640), dtype=np.uint8) sunglasses_info = tracker.detect_sunglasses(rgb_test, ir_850nm_test) print(f"=== 墨镜检测结果 ===") print(f"墨镜检测:{sunglasses_info.sunglasses_detected}") print(f"透光率估计:{sunglasses_info.transmittance_estimate:.2%}") print(f"墨镜类型:{sunglasses_info.sunglasses_type}") print(f"推荐波长:{sunglasses_info.wavelength_selection}") eye_result = tracker.track_eye_with_sunglasses(ir_850nm_test, ir_940nm_test, sunglasses_info) print(f"\n=== 眼动追踪结果 ===") print(f"波长选择:{eye_result['wavelength']}") print(f"眼睛检测:{eye_result['eye_detected']}")
|