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
| import numpy as np import cv2 from typing import Tuple, Optional, List from dataclasses import dataclass from enum import Enum
class GazeRegion(Enum): """视线落点区域""" ROAD_FORWARD = "road_forward" ROAD_LEFT = "road_left" ROAD_RIGHT = "road_right" MIRROR_LEFT = "mirror_left" MIRROR_RIGHT = "mirror_right" MIRROR_CENTER = "mirror_center" PHONE_KNEE_OUTBOARD = "phone_knee_outboard" PHONE_KNEE_INBOARD = "phone_knee_inboard" PHONE_LAP = "phone_lap" PHONE_MOUNTED_DASHBOARD = "phone_mounted_dashboard" PHONE_OEM_POSITION = "phone_oem_position" PHONE_HELD_CENTER = "phone_held_center" PHONE_HELD_WHEEL_TOP = "phone_held_wheel_top" PHONE_WINDSCREEN = "phone_windscreen" PHONE_CLUSTER = "phone_cluster" INFOTAINMENT = "infotainment" GLOVEBOX = "glovebox" FOOTWELL = "footwell" PASSENGER = "passenger" UNKNOWN = "unknown"
@dataclass class GazePoint: """视线落点数据结构""" x: float y: float region: GazeRegion confidence: float
class GazeEstimator: """ 视线落点估计器 基于瞳孔中心-角膜反射向量法(PCCR) 简化实现:使用眼球中心到瞳孔的向量估计视线方向 """ LEFT_EYE = { 'center': 468, 'pupil': 473, 'inner': 133, 'outer': 33, 'upper': 159, 'lower': 145 } RIGHT_EYE = { 'center': 473, 'pupil': 468, 'inner': 362, 'outer': 263, 'upper': 386, 'lower': 374 } def __init__(self, frame_width: int = 640, frame_height: int = 480, gaze_calibration: dict = None): """ 初始化视线估计器 Args: frame_width: 图像宽度 frame_height: 图像高度 gaze_calibration: 校准参数(可选) """ self.frame_width = frame_width self.frame_height = frame_height self.calibration = gaze_calibration or { 'left_eye_offset': [0, 0], 'right_eye_offset': [0, 0], 'scale': 1.0 } self.region_bounds = self._define_region_bounds() def _define_region_bounds(self) -> dict: """ 定义视线落点区域边界 Returns: 区域边界字典,每个区域包含 x, y 的 [min, max] 范围 """ w, h = self.frame_width, self.frame_height return { GazeRegion.ROAD_FORWARD: { 'x': [0.35 * w, 0.65 * w], 'y': [0.2 * h, 0.5 * h] }, GazeRegion.PHONE_KNEE_OUTBOARD: { 'x': [0, 0.2 * w], 'y': [0.7 * h, h] }, GazeRegion.PHONE_KNEE_INBOARD: { 'x': [0.2 * w, 0.4 * w], 'y': [0.7 * h, h] }, GazeRegion.PHONE_LAP: { 'x': [0.3 * w, 0.7 * w], 'y': [0.8 * h, h] }, GazeRegion.PHONE_MOUNTED_DASHBOARD: { 'x': [0.7 * w, 0.9 * w], 'y': [0.1 * h, 0.4 * h] }, GazeRegion.INFOTAINMENT: { 'x': [0.6 * w, 0.9 * w], 'y': [0.4 * h, 0.7 * h] }, GazeRegion.MIRROR_LEFT: { 'x': [0, 0.2 * w], 'y': [0, 0.3 * h] }, GazeRegion.MIRROR_RIGHT: { 'x': [0.8 * w, w], 'y': [0, 0.3 * h] } } def estimate(self, landmarks: np.ndarray, head_pose: 'HeadPose') -> Tuple[GazePoint, GazePoint]: """ 估计双眼视线落点 Args: landmarks: (468, 3) 关键点坐标 head_pose: 头部姿态 Returns: left_gaze: 左眼视线落点 right_gaze: 右眼视线落点 """ left_eye_center = landmarks[self.LEFT_EYE['center'], :2] left_pupil = landmarks[self.LEFT_EYE['pupil'], :2] right_eye_center = landmarks[self.RIGHT_EYE['center'], :2] right_pupil = landmarks[self.RIGHT_EYE['pupil'], :2] left_gaze_vector = left_pupil - left_eye_center right_gaze_vector = right_pupil - right_eye_center left_gaze_point = self._calculate_gaze_point( left_eye_center, left_gaze_vector, head_pose ) right_gaze_point = self._calculate_gaze_point( right_eye_center, right_gaze_vector, head_pose ) left_gaze_point.region = self._classify_region(left_gaze_point.x, left_gaze_point.y) right_gaze_point.region = self._classify_region(right_gaze_point.x, right_gaze_point.y) return left_gaze_point, right_gaze_point def _calculate_gaze_point(self, eye_center: np.ndarray, gaze_vector: np.ndarray, head_pose: 'HeadPose') -> GazePoint: """ 计算视线落点 Args: eye_center: 眼球中心坐标 gaze_vector: 视线向量 head_pose: 头部姿态 Returns: GazePoint 对象 """ base_point = eye_center.copy() scale = 500.0 base_point[0] += gaze_vector[0] * scale base_point[1] += gaze_vector[1] * scale yaw_offset = head_pose.yaw * 10 pitch_offset = head_pose.pitch * 8 gaze_x = base_point[0] + yaw_offset gaze_y = base_point[1] + pitch_offset gaze_x = np.clip(gaze_x, 0, self.frame_width) gaze_y = np.clip(gaze_y, 0, self.frame_height) return GazePoint( x=gaze_x, y=gaze_y, region=GazeRegion.UNKNOWN, confidence=0.8 ) def _classify_region(self, x: float, y: float) -> GazeRegion: """ 根据坐标分类视线落点区域 Args: x: x 坐标 y: y 坐标 Returns: GazeRegion 枚举值 """ for region, bounds in self.region_bounds.items(): if (bounds['x'][0] <= x <= bounds['x'][1] and bounds['y'][0] <= y <= bounds['y'][1]): return region return GazeRegion.UNKNOWN def get_combined_gaze(self, left_gaze: GazePoint, right_gaze: GazePoint) -> GazePoint: """ 融合双眼视线落点 Args: left_gaze: 左眼视线落点 right_gaze: 右眼视线落点 Returns: 融合后的视线落点 """ combined_x = (left_gaze.x + right_gaze.x) / 2 combined_y = (left_gaze.y + right_gaze.y) / 2 if left_gaze.confidence > right_gaze.confidence: region = left_gaze.region confidence = left_gaze.confidence else: region = right_gaze.region confidence = right_gaze.confidence return GazePoint( x=combined_x, y=combined_y, region=region, confidence=confidence )
if __name__ == "__main__": head_estimator = HeadPoseEstimator() gaze_estimator = GazeEstimator() cap = cv2.VideoCapture(0) print("按 'q' 退出") while True: ret, frame = cap.read() if not ret: break head_pose, landmarks = head_estimator.estimate(frame) if head_pose is not None and len(landmarks) > 0: left_gaze, right_gaze = gaze_estimator.estimate(landmarks, head_pose) combined_gaze = gaze_estimator.get_combined_gaze(left_gaze, right_gaze) cv2.circle(frame, (int(combined_gaze.x), int(combined_gaze.y)), 10, (0, 0, 255), -1) cv2.putText(frame, f"Region: {combined_gaze.region.value}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2) phone_regions = [ GazeRegion.PHONE_KNEE_OUTBOARD, GazeRegion.PHONE_KNEE_INBOARD, GazeRegion.PHONE_LAP, GazeRegion.PHONE_MOUNTED_DASHBOARD ] if combined_gaze.region in phone_regions: cv2.putText(frame, "PHONE USE DETECTED", (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) cv2.imshow("Gaze Estimation", frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() head_estimator.close() cv2.destroyAllWindows()
|