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
| import cv2 import numpy as np
class AdversarialGlassesDetector: """对抗眼镜检测器""" def __init__(self): self.face_detector = cv2.CascadeClassifier( cv2.data.haarcascades + 'haarcascade_frontalface_default.xml' ) self.eye_detector = cv2.CascadeClassifier( cv2.data.haarcascades + 'haarcascade_eye.xml' ) def detect_adversarial(self, image: np.ndarray) -> dict: """检测对抗眼镜 Returns: { 'has_glasses': bool, 'is_adversarial': bool, 'confidence': float, 'reason': str } """ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) faces = self.face_detector.detectMultiScale(gray, 1.1, 4) if len(faces) == 0: return { 'has_glasses': False, 'is_adversarial': False, 'confidence': 0.0, 'reason': 'no_face' } x, y, w, h = max(faces, key=lambda f: f[2] * f[3]) face_roi = gray[y:y+h, x:x+w] eyes = self.eye_detector.detectMultiScale(face_roi, 1.1, 4) if len(eyes) < 2: return { 'has_glasses': True, 'is_adversarial': True, 'confidence': 0.7, 'reason': 'eyes_not_detected' } left_eye = eyes[0] right_eye = eyes[1] glasses_region = self._extract_glasses_region( face_roi, left_eye, right_eye ) texture_score = self._analyze_texture(glasses_region) if texture_score > 0.8: return { 'has_glasses': True, 'is_adversarial': True, 'confidence': texture_score, 'reason': 'abnormal_texture' } return { 'has_glasses': True, 'is_adversarial': False, 'confidence': 1 - texture_score, 'reason': 'normal' } def _extract_glasses_region(self, face, left_eye, right_eye): """提取眼镜区域""" x1, y1, w1, h1 = left_eye x2, y2, w2, h2 = right_eye margin = 10 x = min(x1, x2) - margin y = min(y1, y2) - margin w = max(x1 + w1, x2 + w2) - x + margin h = max(y1 + h1, y2 + h2) - y + margin return face[y:y+h, x:x+w] def _analyze_texture(self, region: np.ndarray) -> float: """分析纹理复杂度""" if region.size == 0: return 0.0 laplacian = cv2.Laplacian(region, cv2.CV_64F) variance = laplacian.var() score = min(variance / 1000, 1.0) return score
class RobustDMS: """鲁棒 DMS(对抗攻击防御)""" def __init__(self): self.glasses_detector = AdversarialGlassesDetector() self.gaze_estimator = GazeEstimator() self.head_pose_estimator = HeadPoseEstimator() def process(self, image: np.ndarray) -> dict: """处理图像,考虑对抗攻击""" glasses_result = self.glasses_detector.detect_adversarial(image) if glasses_result['is_adversarial']: head_pose = self.head_pose_estimator.estimate(image) gaze = self._infer_gaze_from_head_pose(head_pose) return { 'gaze': gaze, 'confidence': 0.5, 'defense_triggered': True, 'defense_reason': 'adversarial_glasses', 'head_pose': head_pose } gaze = self.gaze_estimator.estimate(image) return { 'gaze': gaze, 'confidence': 0.9, 'defense_triggered': False } def _infer_gaze_from_head_pose(self, head_pose: dict) -> tuple: """从头部姿态推断视线(粗略)""" return (head_pose['pitch'], head_pose['yaw'])
|