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
| import torch import torch.nn as nn from typing import Tuple
class DifferentialGazeModel(nn.Module): """ 差分视线估计模型 论文: arXiv:2610.11036 核心: 预测两帧之间的视线位移而非绝对视线 架构: 1. 权重共享ResNet-18编码两帧眼部图像 2. MLP映射编码差到2D视线位移 3. 绝对视线 = 锚点视线 + 预测位移 优势: - 消除个体化偏移(差分操作平移不变) - 对标定漂移鲁棒(相对量不受绝对漂移影响) - 在窄范围中区分微小变化更敏感 """ def __init__(self, hidden_dim: int = 256): super().__init__() import torchvision.models as models resnet = models.resnet18(weights='DEFAULT') resnet.fc = nn.Identity() self.encoder = resnet self.diff_head = nn.Sequential( nn.Linear(512 * 2, hidden_dim), nn.BatchNorm1d(hidden_dim), nn.ReLU(inplace=True), nn.Dropout(0.2), nn.Linear(hidden_dim, hidden_dim // 2), nn.BatchNorm1d(hidden_dim // 2), nn.ReLU(inplace=True), nn.Dropout(0.2), nn.Linear(hidden_dim // 2, 2) ) self.temporal_lstm = nn.LSTM( input_size=2, hidden_size=64, num_layers=1, batch_first=True ) self.temporal_head = nn.Linear(64, 2) def forward( self, anchor_eye: torch.Tensor, current_eye: torch.Tensor, anchor_gaze: torch.Tensor, prev_diffs: torch.Tensor = None ) -> Tuple[torch.Tensor, torch.Tensor]: """ 前向传播 Args: anchor_eye: 标定锚点帧的眼部裁剪 current_eye: 当前帧的眼部裁剪 anchor_gaze: 锚点的已知视线 prev_diffs: 过去T帧的差分序列(用于时序增强) Returns: predicted_gaze: (B, 2) 预测的绝对视线 predicted_diff: (B, 2) 预测的差分 """ e_anchor = self.encoder(anchor_eye) e_current = self.encoder(current_eye) concat = torch.cat([e_anchor, e_current], dim=-1) diff = self.diff_head(concat) predicted_gaze = anchor_gaze + diff if prev_diffs is not None: temporal_out, _ = self.temporal_lstm(prev_diffs) temporal_correction = self.temporal_head(temporal_out[:, -1, :]) predicted_gaze = predicted_gaze + temporal_correction return predicted_gaze, diff def calibrate(self, anchor_eye, anchor_gaze): """ 被试标定:记录锚点帧和视线 在长时间场景中,需要周期性重新标定 因为标定漂移会累积 """ self.register_buffer('anchor_eye', anchor_eye) self.register_buffer('anchor_gaze', anchor_gaze)
class LongDurationGazeTracker: """ 长时驾驶视线追踪系统 集成: 1. 差分视线估计 2. 周期性重标定(对抗漂移) 3. 时序平滑 4. 注视/扫视事件检测 """ def __init__(self, model, recalibration_interval=300): """ Args: model: DifferentialGazeModel recalibration_interval: 重标定间隔(帧数,300帧=10秒@30fps) """ self.model = model self.recal_interval = recalibration_interval self.frame_count = 0 self.gaze_history = [] self.diff_history = [] self.anchor = None def initialize(self, anchor_eye, anchor_gaze): """初始化锚点""" self.model.calibrate(anchor_eye, anchor_gaze) self.anchor = (anchor_eye, anchor_gaze) self.frame_count = 0 self.gaze_history = [anchor_gaze] self.diff_history = [] def step(self, current_eye): """ 单帧处理 Returns: gaze: (2,) 预测视线 is_fixation: bool 是否为注视 is_saccade: bool 是否为扫视 need_recalibration: bool 是否需要重标定 """ if self.anchor is None: return None, False, False, True anchor_eye, anchor_gaze = self.anchor prev_diffs = None if len(self.diff_history) > 5: import numpy as np prev_diffs = torch.stack([ torch.tensor(d) for d in self.diff_history[-10:] ]).unsqueeze(0) with torch.no_grad(): gaze, diff = self.model( anchor_eye.unsqueeze(0), current_eye.unsqueeze(0), anchor_gaze.unsqueeze(0), prev_diffs ) gaze = gaze[0] diff = diff[0] self.gaze_history.append(gaze) self.diff_history.append(diff) self.frame_count += 1 diff_mag = diff.norm().item() is_saccade = diff_mag > 0.05 is_fixation = diff_mag < 0.01 need_recal = self.frame_count >= self.recal_interval return gaze.numpy(), is_fixation, is_saccade, need_recal
if __name__ == "__main__": print("=" * 60) print("差分视线估计 - 长时窄范围场景测试") print("=" * 60) model = DifferentialGazeModel() total_params = sum(p.numel() for p in model.parameters()) print(f"\n模型参数量: {total_params:,}") B = 2 anchor_eye = torch.randn(B, 3, 64, 64) current_eye = torch.randn(B, 3, 64, 64) anchor_gaze = torch.tensor([[0.0, 0.0], [0.1, -0.05]]) gaze, diff = model(anchor_eye, current_eye, anchor_gaze) print(f"\n输入:") print(f" 锚点视线: {anchor_gaze}") print(f" 锚点眼部: {anchor_eye.shape}") print(f" 当前眼部: {current_eye.shape}") print(f"\n输出:") print(f" 预测视线: {gaze}") print(f" 视线差分: {diff}") print(f" 差分幅度(rad): {diff.norm(dim=-1)}") print(f" 差分幅度(°): {diff.norm(dim=-1) * 180 / 3.14159}") print(f"\n{'='*60}") print("长时追踪系统测试(10帧)") print(f"{'='*60}") tracker = LongDurationGazeTracker(model, recalibration_interval=5) anchor_eye_single = torch.randn(3, 64, 64) anchor_gaze_single = torch.tensor([0.0, 0.0]) tracker.initialize(anchor_eye_single, anchor_gaze_single) print(f"\n{'帧':>4} | {'pitch(°)':>8} | {'yaw(°)':>8} | {'Δ(°)':>6} | {'状态':>8} | {'重标定':>6}") print("-" * 55) import numpy as np for i in range(10): current = torch.randn(3, 64, 64) gaze, is_fix, is_sac, need_recal = tracker.step(current) if gaze is not None: pitch = gaze[0] * 180 / np.pi yaw = gaze[1] * 180 / np.pi diff_mag = np.sqrt(gaze[0]**2 + gaze[1]**2) * 180 / np.pi status = "注视" if is_fix else ("扫视" if is_sac else "追踪") recal = "是" if need_recal else "否" print(f"{i:4d} | {pitch:8.2f} | {yaw:8.2f} | {diff_mag:6.2f} | {status:>8} | {recal:>6}") print(f"\n历史长度: {len(tracker.gaze_history)}")
|