|
| 1 | +import numpy as np |
| 2 | +import matplotlib.pyplot as plt |
| 3 | + |
| 4 | + |
| 5 | +class CalibrationEllipse: |
| 6 | + def __init__(self, n_std_devs=2.5): |
| 7 | + self.xs = [] |
| 8 | + self.ys = [] |
| 9 | + self.n_std_devs = float(n_std_devs) |
| 10 | + self.fitted = False |
| 11 | + |
| 12 | + self.scale_factor = 0.80 |
| 13 | + |
| 14 | + self.flip_y = False |
| 15 | + self.flip_x = True |
| 16 | + |
| 17 | + # Parameters |
| 18 | + self.center = None |
| 19 | + self.axes = None |
| 20 | + self.evecs = None |
| 21 | + |
| 22 | + def add_sample(self, x, y): |
| 23 | + self.xs.append(float(x)) |
| 24 | + self.ys.append(float(y)) |
| 25 | + self.fitted = False |
| 26 | + |
| 27 | + def set_inset_percent(self, percent_smaller=0.0): |
| 28 | + clamped_percent = np.clip(percent_smaller, 0.0, 100.0) |
| 29 | + self.scale_factor = 1.0 - (clamped_percent / 100.0) |
| 30 | + |
| 31 | + def init_from_save(self, evecs, axes): |
| 32 | + """ |
| 33 | + Initialize from save. |
| 34 | + NOTE: We ignore the saved 'evecs' rotation to ensure strict axis alignment. |
| 35 | + """ |
| 36 | + try: |
| 37 | + axes_array = np.asarray(axes, dtype=float) |
| 38 | + |
| 39 | + if axes_array.shape != (2,): |
| 40 | + print(f"[ERROR] Invalid axes shape: {axes_array.shape}.") |
| 41 | + return False |
| 42 | + |
| 43 | + if np.all(axes_array == 0) or np.any(np.isnan(axes_array)): |
| 44 | + print("[ERROR] Saved data contains zero or NaN values.") |
| 45 | + return False |
| 46 | + |
| 47 | + # Force Identity Matrix (No Rotation) |
| 48 | + self.evecs = np.eye(2) |
| 49 | + self.axes = axes_array |
| 50 | + |
| 51 | + self.fitted = True |
| 52 | + return True |
| 53 | + |
| 54 | + except (ValueError, TypeError) as e: |
| 55 | + print(f"[ERROR] Failed to load calibration data: {e}") |
| 56 | + self.fitted = False |
| 57 | + return False |
| 58 | + |
| 59 | + def fit_ellipse(self): |
| 60 | + """ |
| 61 | + Fits an axis-aligned ellipse (no rotation) using standard deviation. |
| 62 | + """ |
| 63 | + N = len(self.xs) |
| 64 | + if N < 2: |
| 65 | + print("Warning: Need >= 2 samples to fit.") |
| 66 | + self.fitted = False |
| 67 | + return 0, 0 |
| 68 | + |
| 69 | + # 1. Calculate Center (Mean) |
| 70 | + mean_x = np.mean(self.xs) |
| 71 | + mean_y = np.mean(self.ys) |
| 72 | + self.center = np.array([mean_x, mean_y]) |
| 73 | + |
| 74 | + # 2. Calculate Axis Lengths (Standard Deviation) |
| 75 | + std_x = np.std(self.xs) |
| 76 | + std_y = np.std(self.ys) |
| 77 | + |
| 78 | + # Apply sigma multiplier |
| 79 | + radius_x = std_x * self.n_std_devs |
| 80 | + radius_y = std_y * self.n_std_devs |
| 81 | + |
| 82 | + # Safety clamp |
| 83 | + if radius_x < 1e-12: |
| 84 | + radius_x = 1e-12 |
| 85 | + if radius_y < 1e-12: |
| 86 | + radius_y = 1e-12 |
| 87 | + |
| 88 | + self.axes = np.array([radius_x, radius_y]) |
| 89 | + |
| 90 | + # 3. Force Identity Matrix (Strict Horizontal/Vertical alignment) |
| 91 | + self.evecs = np.eye(2) |
| 92 | + |
| 93 | + self.fitted = True |
| 94 | + return self.evecs, self.axes |
| 95 | + |
| 96 | + def normalize(self, pupil_pos, target_pos=None, clip=True): |
| 97 | + if not self.fitted: |
| 98 | + return 0.0, 0.0 |
| 99 | + |
| 100 | + x, y = float(pupil_pos[0]), float(pupil_pos[1]) |
| 101 | + |
| 102 | + if target_pos is None: |
| 103 | + cx, cy = self.center |
| 104 | + else: |
| 105 | + cx, cy = target_pos |
| 106 | + |
| 107 | + # Calculate deltas |
| 108 | + dx = x - cx |
| 109 | + dy = y - cy |
| 110 | + |
| 111 | + # Get calibration radii |
| 112 | + rx, ry = self.axes * self.scale_factor |
| 113 | + |
| 114 | + # Normalize |
| 115 | + norm_x = dx / rx |
| 116 | + norm_y = dy / ry |
| 117 | + |
| 118 | + # --- APPLY FLIPS --- |
| 119 | + # If flip_x is True: Inverts the sign. |
| 120 | + final_x = -norm_x if self.flip_x else norm_x |
| 121 | + |
| 122 | + # If flip_y is False: Inverts Screen Y (so Up is Positive). |
| 123 | + final_y = norm_y if self.flip_y else -norm_y |
| 124 | + |
| 125 | + if clip: |
| 126 | + final_x = np.clip(final_x, -1.0, 1.0) |
| 127 | + final_y = np.clip(final_y, -1.0, 1.0) |
| 128 | + |
| 129 | + return float(final_x), float(final_y) |
| 130 | + |
| 131 | + def denormalize(self, norm_x, norm_y, target_pos=None): |
| 132 | + if not self.fitted: |
| 133 | + return 0.0, 0.0 |
| 134 | + |
| 135 | + # 1. Reverse the Output Mapping |
| 136 | + nx = -norm_x if self.flip_x else norm_x |
| 137 | + ny = norm_y if self.flip_y else -norm_y |
| 138 | + |
| 139 | + # 2. Scale back up |
| 140 | + rx, ry = self.axes * self.scale_factor |
| 141 | + dx = nx * rx |
| 142 | + dy = ny * ry |
| 143 | + |
| 144 | + # 3. Add Center |
| 145 | + if target_pos is None: |
| 146 | + cx, cy = self.center |
| 147 | + else: |
| 148 | + cx, cy = target_pos |
| 149 | + |
| 150 | + return float(cx + dx), float(cy + dy) |
| 151 | + |
| 152 | + def fit_and_visualize(self): |
| 153 | + plt.figure(figsize=(10, 8)) |
| 154 | + |
| 155 | + plt.plot(self.xs, self.ys, "k.", label="Samples", alpha=0.5) |
| 156 | + plt.axis("equal") |
| 157 | + plt.grid(True, alpha=0.3) |
| 158 | + |
| 159 | + # Invert plot Y axis to match screen coordinates |
| 160 | + plt.gca().invert_yaxis() |
| 161 | + |
| 162 | + if not self.fitted: |
| 163 | + self.fit_ellipse() |
| 164 | + |
| 165 | + if self.fitted: |
| 166 | + scaled_axes = self.axes * self.scale_factor |
| 167 | + t = np.linspace(0, 2 * np.pi, 200) |
| 168 | + |
| 169 | + el_x = self.center[0] + scaled_axes[0] * np.cos(t) |
| 170 | + el_y = self.center[1] + scaled_axes[1] * np.sin(t) |
| 171 | + |
| 172 | + plt.plot(el_x, el_y, "b-", linewidth=2, label="Axis-Aligned Fit") |
| 173 | + plt.plot(self.center[0], self.center[1], "r+", markersize=15, label="Center") |
| 174 | + |
| 175 | + plt.hlines( |
| 176 | + self.center[1], |
| 177 | + self.center[0] - scaled_axes[0], |
| 178 | + self.center[0] + scaled_axes[0], |
| 179 | + colors="g", |
| 180 | + linestyles="-", |
| 181 | + label="Width (X)", |
| 182 | + ) |
| 183 | + |
| 184 | + plt.vlines( |
| 185 | + self.center[0], |
| 186 | + self.center[1] - scaled_axes[1], |
| 187 | + self.center[1] + scaled_axes[1], |
| 188 | + colors="m", |
| 189 | + linestyles="-", |
| 190 | + label="Height (Y)", |
| 191 | + ) |
| 192 | + |
| 193 | + plt.title(f"Axis-Aligned Calibration (FlipX={self.flip_x})") |
| 194 | + else: |
| 195 | + plt.title("Fit FAILED") |
| 196 | + |
| 197 | + plt.legend() |
| 198 | + plt.tight_layout() |
| 199 | + plt.show() |
0 commit comments