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
| import numpy as np from dataclasses import dataclass from typing import Dict, List, Tuple from enum import Enum
class ScenarioType(Enum): """场景类型""" D_01 = "D-01" D_02 = "D-02" D_03 = "D-03" D_04 = "D-04" D_05 = "D-05" F_01 = "F-01" F_02 = "F-02" F_03 = "F-03" U_01 = "U-01" U_02 = "U-02" U_03 = "U-03"
@dataclass class ScenarioConfig: """场景配置""" scenario_type: ScenarioType trigger_duration: float detection_deadline: float warning_level: int pass_accuracy: float
SCENARIO_CONFIGS = { ScenarioType.D_01: ScenarioConfig(ScenarioType.D_01, 3.0, 3.0, 1, 0.9), ScenarioType.D_02: ScenarioConfig(ScenarioType.D_02, 3.0, 3.0, 1, 0.9), ScenarioType.D_03: ScenarioConfig(ScenarioType.D_03, 3.0, 3.0, 1, 0.9), ScenarioType.D_04: ScenarioConfig(ScenarioType.D_04, 3.0, 3.0, 1, 0.9), ScenarioType.D_05: ScenarioConfig(ScenarioType.D_05, 3.0, 3.0, 1, 0.9), ScenarioType.F_01: ScenarioConfig(ScenarioType.F_01, 5.0, 5.0, 2, 0.9), ScenarioType.F_02: ScenarioConfig(ScenarioType.F_02, 120.0, 120.0, 2, 0.9), ScenarioType.F_03: ScenarioConfig(ScenarioType.F_03, 2.0, 2.0, 2, 0.95), ScenarioType.U_01: ScenarioConfig(ScenarioType.U_01, 10.0, 15.0, 2, 0.9), ScenarioType.U_02: ScenarioConfig(ScenarioType.U_02, 10.0, 15.0, 2, 0.9), ScenarioType.U_03: ScenarioConfig(ScenarioType.U_03, 10.0, 10.0, 2, 0.9), }
class DSMSimulator: """DMS 场景模拟器""" def __init__(self): self.current_scenario = None self.elapsed_time = 0 def start_scenario(self, scenario_type: ScenarioType): """开始场景""" self.current_scenario = SCENARIO_CONFIGS[scenario_type] self.elapsed_time = 0 def update(self, dt: float, dms_detection: bool, warning_issued: bool) -> Dict: """更新场景状态 Args: dt: 时间步长(秒) dms_detection: DMS 是否检测到 warning_issued: 是否发出警告 Returns: { 'elapsed_time': float, 'scenario_complete': bool, 'detection_success': bool, 'timing_success': bool } """ self.elapsed_time += dt config = self.current_scenario scenario_complete = self.elapsed_time >= config.trigger_duration + config.detection_deadline detection_success = dms_detection and self.elapsed_time >= config.trigger_duration timing_success = ( warning_issued and self.elapsed_time >= config.trigger_duration and self.elapsed_time <= config.trigger_duration + config.detection_deadline ) return { 'elapsed_time': self.elapsed_time, 'scenario_complete': scenario_complete, 'detection_success': detection_success, 'timing_success': timing_success }
class TestRunner: """测试运行器""" def __init__(self): self.simulator = DSMSimulator() self.results = [] def run_all_scenarios(self, dms_system) -> Dict: """运行所有场景 Args: dms_system: DMS 系统实例 Returns: { 'total_scenarios': int, 'passed_scenarios': int, 'overall_accuracy': float, 'scenario_results': dict } """ scenario_results = {} for scenario_type in ScenarioType: result = self._run_single_scenario(scenario_type, dms_system) scenario_results[scenario_type.value] = result total = len(scenario_results) passed = sum(1 for r in scenario_results.values() if r['passed']) accuracy = passed / total if total > 0 else 0 return { 'total_scenarios': total, 'passed_scenarios': passed, 'overall_accuracy': accuracy, 'scenario_results': scenario_results } def _run_single_scenario(self, scenario_type: ScenarioType, dms_system) -> Dict: """运行单个场景""" self.simulator.start_scenario(scenario_type) frame_rate = 30 dt = 1.0 / frame_rate detections = [] warnings = [] while True: sim_input = self._generate_simulated_input(scenario_type) detection = dms_system.detect(sim_input) result = self.simulator.update( dt, detection['detected'], detection.get('warning_issued', False) ) detections.append(detection['detected']) if result['scenario_complete']: break config = SCENARIO_CONFIGS[scenario_type] detection_accuracy = sum(detections[-30:]) / 30 if len(detections) >= 30 else 0 passed = detection_accuracy >= config.pass_accuracy return { 'scenario': scenario_type.value, 'detection_accuracy': detection_accuracy, 'required_accuracy': config.pass_accuracy, 'passed': passed, 'warning_level': config.warning_level } def _generate_simulated_input(self, scenario_type: ScenarioType) -> Dict: """生成模拟输入""" if scenario_type == ScenarioType.D_01: return { 'gaze_pitch': -25, 'gaze_yaw': 0, 'eyes_detected': True } elif scenario_type == ScenarioType.F_01: return { 'eye_closure': 0.9, 'gaze_pitch': 0, 'gaze_yaw': 0, 'eyes_detected': True } else: return { 'gaze_pitch': 0, 'gaze_yaw': 0, 'eyes_detected': True }
|