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
| import numpy as np
class AlcoholImpairmentDetector: """ 酒精损伤检测器 基于眼动和面部特征检测酒精损伤状态 """ def __init__(self): self.weights = { "saccade_velocity": 0.25, "pupil_response": 0.20, "blink_rate": 0.15, "gaze_variability": 0.20, "eyelid_drooping": 0.10, "reaction_time": 0.10 } self.baseline = { "saccade_velocity": 200, "pupil_response_time": 0.3, "blink_rate": 15, "gaze_variability": 0.05, "eyelid_openness": 0.8, "reaction_time": 0.5 } def extract_features(self, eye_tracking_data, face_data): """ 提取酒精损伤相关特征 Args: eye_tracking_data: 眼动数据 face_data: 面部数据 Returns: features: 特征字典 """ features = {} saccades = eye_tracking_data["saccades"] if len(saccades) > 0: velocities = [s["velocity"] for s in saccades] features["saccade_velocity"] = np.mean(velocities) else: features["saccade_velocity"] = self.baseline["saccade_velocity"] features["pupil_response_time"] = eye_tracking_data.get( "pupil_response_time", self.baseline["pupil_response_time"] ) features["blink_rate"] = eye_tracking_data.get( "blink_rate", self.baseline["blink_rate"] ) gaze_positions = eye_tracking_data["gaze_positions"] features["gaze_variability"] = np.std(gaze_positions) features["eyelid_openness"] = face_data.get( "eyelid_openness", self.baseline["eyelid_openness"] ) features["reaction_time"] = eye_tracking_data.get( "reaction_time", self.baseline["reaction_time"] ) return features def compute_impairment_score(self, features): """ 计算酒精损伤评分 Args: features: 特征字典 Returns: score: 损伤评分 (0-100) """ score = 0 velocity_ratio = features["saccade_velocity"] / self.baseline["saccade_velocity"] velocity_score = max(0, (1 - velocity_ratio) * 100) score += velocity_score * self.weights["saccade_velocity"] pupil_ratio = features["pupil_response_time"] / self.baseline["pupil_response_time"] pupil_score = max(0, (pupil_ratio - 1) * 50) score += pupil_score * self.weights["pupil_response"] blink_ratio = features["blink_rate"] / self.baseline["blink_rate"] blink_score = max(0, abs(blink_ratio - 1) * 50) score += blink_score * self.weights["blink_rate"] gaze_score = features["gaze_variability"] * 100 score += gaze_score * self.weights["gaze_variability"] eyelid_score = max(0, (1 - features["eyelid_openness"]) * 100) score += eyelid_score * self.weights["eyelid_drooping"] reaction_ratio = features["reaction_time"] / self.baseline["reaction_time"] reaction_score = max(0, (reaction_ratio - 1) * 50) score += reaction_score * self.weights["reaction_time"] return min(100, score) def detect(self, eye_tracking_data, face_data, threshold=60): """ 检测酒精损伤 Args: eye_tracking_data: 眼动数据 face_data: 面部数据 threshold: 损伤评分阈值 Returns: result: 检测结果 """ features = self.extract_features(eye_tracking_data, face_data) score = self.compute_impairment_score(features) return { "impaired": score >= threshold, "impairment_score": score, "confidence": min(1.0, score / 100), "features": features }
if __name__ == "__main__": detector = AlcoholImpairmentDetector() eye_data = { "saccades": [{"velocity": 120}], "pupil_response_time": 0.5, "blink_rate": 20, "gaze_positions": np.random.normal(0, 0.1, 100), "reaction_time": 0.8 } face_data = { "eyelid_openness": 0.6 } result = detector.detect(eye_data, face_data) print(f"酒精损伤评分: {result['impairment_score']:.1f}") print(f"是否损伤: {result['impaired']}")
|