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
| import numpy as np from typing import Tuple
class GazeErrorMeasurement: """ 道路注视误差测量框架 论文 Section 3.2 的方法实现 测量流程: 1. 驾驶员声明注视目标(语音) 2. YOLO检测车辆+车牌 3. 车牌中心作为参考点 4. 计算注视光线与参考光线的角度差 """ def __init__(self, camera_intrinsics: dict): """ Args: camera_intrinsics: 相机内参 fx, fy: 焦距 cx, cy: 主点 dist: 畸变系数 """ self.fx = camera_intrinsics['fx'] self.fy = camera_intrinsics['fy'] self.cx = camera_intrinsics['cx'] self.cy = camera_intrinsics['cy'] self.dist = camera_intrinsics.get('dist', None) def pixel_to_ray(self, pixel: np.ndarray) -> np.ndarray: """ 像素坐标转单位光线 Args: pixel: (2,) 像素坐标 [u, v] Returns: ray: (3,) 单位光线方向 """ u, v = pixel x = (u - self.cx) / self.fx y = (v - self.cy) / self.fy norm = np.sqrt(x**2 + y**2 + 1) ray = np.array([x, y, 1]) / norm return ray def angular_gaze_error(self, gaze_pixel: np.ndarray, target_pixel: np.ndarray) -> float: """ 计算角度注视误差 Args: gaze_pixel: (2,) 注视点像素坐标 target_pixel: (2,) 目标点像素坐标 Returns: error_deg: 角度误差(度) """ gaze_ray = self.pixel_to_ray(gaze_pixel) target_ray = self.pixel_to_ray(target_pixel) cos_angle = np.dot(gaze_ray, target_ray) cos_angle = np.clip(cos_angle, -1, 1) angle_rad = np.arccos(cos_angle) angle_deg = np.degrees(angle_rad) return angle_deg def signed_bias(self, gaze_pixel: np.ndarray, target_pixel: np.ndarray) -> Tuple[float, float]: """ 计算有符号偏差(方位角/仰角) Returns: azimuth_bias: 方位角偏差(度) elevation_bias: 仰角偏差(度) """ gaze_ray = self.pixel_to_ray(gaze_pixel) target_ray = self.pixel_to_ray(target_pixel) gaze_az = np.arctan2(gaze_ray[0], gaze_ray[2]) gaze_el = np.arcsin(gaze_ray[1]) target_az = np.arctan2(target_ray[0], target_ray[2]) target_el = np.arcsin(target_ray[1]) az_bias = np.degrees(gaze_az - target_az) el_bias = np.degrees(gaze_el - target_el) return az_bias, el_bias def fixation_precision(self, gaze_samples: np.ndarray, target_pixel: np.ndarray) -> float: """ 注视内精度(一个注视事件内的分散度) Args: gaze_samples: (N, 2) 一次注视的N帧注视点 target_pixel: (2,) 目标点 Returns: precision_deg: 注视内精度(度) """ errors = [] for g in gaze_samples: errors.append(self.angular_gaze_error(g, target_pixel)) return np.median(errors)
class OffsetCalibration: """ 偏移校正框架 论文 Section 3.4 的方法: 1. 从室内校准场景估计偏移量 2. 将偏移转移到道路场景 3. 偏移 = 校准场景中注视到目标的角度差的中位数 """ def __init__(self): self.offset = None self.scene_offsets = {} def estimate_offset(self, gaze_pixels: np.ndarray, target_pixels: np.ndarray, camera_intrinsics: dict) -> Tuple[float, float]: """ 从校准数据估计偏移 Args: gaze_pixels: (N, 2) 校准时的注视点 target_pixels: (N, 2) 校准目标点 camera_intrinsics: 相机内参 Returns: offset: (az_offset, el_offset) 偏移量(度) """ measurer = GazeErrorMeasurement(camera_intrinsics) az_biases = [] el_biases = [] for g, t in zip(gaze_pixels, target_pixels): az_b, el_b = measurer.signed_bias(g, t) az_biases.append(az_b) el_biases.append(el_b) az_offset = np.median(az_biases) el_offset = np.median(el_biases) self.offset = (az_offset, el_offset) return self.offset def apply_offset(self, gaze_pixels: np.ndarray, camera_intrinsics: dict) -> np.ndarray: """ 将偏移应用到道路注视数据 Args: gaze_pixels: (N, 2) 原始注视点 Returns: corrected_pixels: (N, 2) 校正后注视点 """ if self.offset is None: return gaze_pixels az_off, el_off = self.offset fx = camera_intrinsics['fx'] fy = camera_intrinsics['fy'] du = fx * np.radians(az_off) dv = fy * np.radians(el_off) corrected = gaze_pixels.copy() corrected[:, 0] += du corrected[:, 1] += dv return corrected
if __name__ == "__main__": print("=" * 60) print("可穿戴注视追踪精度评估 - 测试") print("=" * 60) intrinsics = {'fx': 600, 'fy': 600, 'cx': 320, 'cy': 240} measurer = GazeErrorMeasurement(intrinsics) gaze = np.array([350, 200]) target = np.array([340, 210]) error = measurer.angular_gaze_error(gaze, target) az_b, el_b = measurer.signed_bias(gaze, target) print(f"\n注视点: {gaze}") print(f"目标点: {target}") print(f"角度误差: {error:.2f}°") print(f"方位角偏差: {az_b:.2f}°") print(f"仰角偏差: {el_b:.2f}°") calibrator = OffsetCalibration() np.random.seed(42) n_cal = 100 targets_cal = np.random.randint(200, 440, (n_cal, 2)).astype(float) gazes_cal = targets_cal + np.array([8, -5]) + np.random.randn(n_cal, 2) * 3 offset = calibrator.estimate_offset(gazes_cal, targets_cal, intrinsics) print(f"\n估计偏移: 方位角={offset[0]:.2f}°, 仰角={offset[1]:.2f}°") road_targets = np.random.randint(200, 440, (41, 2)).astype(float) road_gazes = road_targets + np.array([8, -5]) + np.random.randn(41, 2) * 4 errors_before = [measurer.angular_gaze_error(g, t) for g, t in zip(road_gazes, road_targets)] print(f"\n校正前: 平均误差={np.mean(errors_before):.2f}°") corrected_gazes = calibrator.apply_offset(road_gazes, intrinsics) errors_after = [measurer.angular_gaze_error(g, t) for g, t in zip(corrected_gazes, road_targets)] print(f"校正后: 平均误差={np.mean(errors_after):.2f}°") print(f"改善: {(1-np.mean(errors_after)/np.mean(errors_before))*100:.1f}%") print(f"改善场景数: {sum(1 for a,b in zip(errors_after, errors_before) if a < b)}/41")
|