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
| import numpy as np
class GazeCalibrationManager: """ DMS视线估计校准管理器 核心问题: 1. 实验室训练的模型在实车部署时有系统偏移 2. 偏移量在不同驾驶会话间变化 3. 需要在线重校准 校准方案: 1. 出厂校准:生产线固定点校准 2. 会话校准:每次启动驾驶时快速校准 3. 在线校准:行驶中持续微调 """ def __init__(self): self.offset = None self.calibration_history = [] self.confidence_threshold = 0.5 def factory_calibration(self, gaze_samples: np.ndarray, target_points: np.ndarray) -> dict: """ 出厂校准 Args: gaze_samples: (N, 2) 校准点视线预测 target_points: (N, 2) 真实目标点 Returns: {'offset': (2,), 'residual_error': float} """ offset = np.mean(target_points - gaze_samples, axis=0) residual = np.std(target_points - gaze_samples - offset, axis=0) self.offset = offset return { 'offset': offset.tolist(), 'residual_error_rad': float(np.linalg.norm(residual)), 'residual_error_deg': float(np.degrees(np.linalg.norm(residual))), 'n_samples': len(gaze_samples) } def session_calibration(self, gaze_samples: np.ndarray, target_points: np.ndarray) -> dict: """ 会话校准(每次启动驾驶) 使用少量点快速校准 """ if len(gaze_samples) < 3: return {'status': 'insufficient_data'} session_offset = np.mean(target_points - gaze_samples, axis=0) if self.offset is not None: alpha = 0.3 adjusted_offset = (1 - alpha) * self.offset + alpha * session_offset else: adjusted_offset = session_offset self.offset = adjusted_offset return { 'offset': adjusted_offset.tolist(), 'status': 'calibrated', 'n_samples': len(gaze_samples) } def apply_calibration(self, gaze: np.ndarray) -> np.ndarray: """应用校准偏移""" if self.offset is None: return gaze return gaze + self.offset def online_update(self, gaze: np.ndarray, road_gaze_estimate: np.ndarray, confidence: float = 0.5): """ 在线更新(行驶中) 使用道路上下文作为弱监督: - 直道行驶时,视线应在前方道路 - 车道保持时,视线应在道路中心区域 """ if confidence < self.confidence_threshold: return delta = road_gaze_estimate - gaze alpha = 0.001 self.offset = (1 - alpha) * self.offset + alpha * delta
if __name__ == "__main__": calibrator = GazeCalibrationManager() np.random.seed(42) n = 9 factory_gaze = np.random.randn(n, 2) * 0.05 factory_targets = np.array([ [-0.5, -0.3], [-0.5, 0], [-0.5, 0.3], [0, -0.3], [0, 0], [0, 0.3], [0.5, -0.3], [0.5, 0], [0.5, 0.3], ]) result = calibrator.factory_calibration(factory_gaze, factory_targets) print("出厂校准:", result) session_gaze = factory_gaze + np.array([0.05, -0.03]) result = calibrator.session_calibration(session_gaze[:5], factory_targets[:5]) print("会话校准:", result) test_gaze = np.array([0.1, 0.05]) calibrated = calibrator.apply_calibration(test_gaze) print(f"原始视线: {test_gaze} → 校准后: {calibrated}")
|