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
| import numpy as np
class AlcoholImpairmentDetector: """ 酒精损伤检测器 基于眼动和头部行为特征 """ def __init__(self): self.gaze_weights = { "gaze_fixation_duration": 0.25, "saccade_frequency": 0.20, "blink_rate": 0.15, "eyelid_closure": 0.20, "pupil_response": 0.10 } self.head_weights = { "head_pose_stability": 0.30, "nod_frequency": 0.20, "response_latency": 0.30 } def extract_gaze_features(self, gaze_data: dict) -> dict: """ 提取眼动特征 Args: gaze_data: 眼动数据 Returns: features: 眼动特征 """ features = {} fixation_durations = gaze_data.get("fixation_durations", []) features["gaze_fixation_duration"] = np.mean(fixation_durations) if fixation_durations else 0 saccade_count = gaze_data.get("saccade_count", 0) time_window = gaze_data.get("time_window_sec", 60) features["saccade_frequency"] = saccade_count / time_window blink_count = gaze_data.get("blink_count", 0) features["blink_rate"] = blink_count / time_window eyelid_openness = gaze_data.get("eyelid_openness", []) features["eyelid_closure"] = 1 - np.mean(eyelid_openness) if eyelid_openness else 0 pupil_response_time = gaze_data.get("pupil_response_time_ms", 200) features["pupil_response"] = pupil_response_time / 500 return features def extract_head_features(self, head_data: dict) -> dict: """ 提取头部特征 Args: head_data: 头部数据 Returns: features: 头部特征 """ features = {} head_pose_variance = head_data.get("pose_variance", 0) features["head_pose_stability"] = min(head_pose_variance / 10, 1) nod_count = head_data.get("nod_count", 0) time_window = head_data.get("time_window_sec", 60) features["nod_frequency"] = nod_count / time_window response_latency_ms = head_data.get("response_latency_ms", 200) features["response_latency"] = response_latency_ms / 1000 return features def compute_impairment_score(self, gaze_features: dict, head_features: dict) -> float: """ 计算损伤分数 Args: gaze_features: 眼动特征 head_features: 头部特征 Returns: impairment_score: 损伤分数 (0-1) """ gaze_score = sum( gaze_features.get(k, 0) * v for k, v in self.gaze_weights.items() ) head_score = sum( head_features.get(k, 0) * v for k, v in self.head_weights.items() ) total_score = 0.7 * gaze_score + 0.3 * head_score return min(total_score, 1.0) def estimate_bac(self, impairment_score: float) -> dict: """ 估计BAC范围 Args: impairment_score: 损伤分数 Returns: bac_estimate: BAC估计 """ if impairment_score < 0.3: bac_range = "0.00-0.02%" confidence = "高" elif impairment_score < 0.5: bac_range = "0.02-0.05%" confidence = "中" elif impairment_score < 0.7: bac_range = "0.05-0.10%" confidence = "高" else: bac_range = "0.10%+" confidence = "很高" return { "bac_range": bac_range, "confidence": confidence, "impairment_level": self._get_impairment_level(impairment_score) } def _get_impairment_level(self, score: float) -> str: """ 获取损伤等级 """ if score < 0.3: return "正常" elif score < 0.5: return "轻微损伤" elif score < 0.7: return "中度损伤" else: return "严重损伤"
if __name__ == "__main__": detector = AlcoholImpairmentDetector() gaze_data = { "fixation_durations": [1.2, 1.5, 1.8, 1.1], "saccade_count": 10, "time_window_sec": 60, "blink_count": 8, "eyelid_openness": [0.7, 0.65, 0.6], "pupil_response_time_ms": 350 } head_data = { "pose_variance": 8, "nod_count": 5, "response_latency_ms": 400 } gaze_features = detector.extract_gaze_features(gaze_data) head_features = detector.extract_head_features(head_data) score = detector.compute_impairment_score(gaze_features, head_features) bac = detector.estimate_bac(score) print(f"损伤分数: {score:.2f}") print(f"BAC估计: {bac['bac_range']}") print(f"置信度: {bac['confidence']}") print(f"损伤等级: {bac['impairment_level']}")
|