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
| """ DMS数据智能采集触发器 """
import numpy as np from dataclasses import dataclass from typing import List, Optional from enum import Enum import time
class TriggerType(Enum): """触发类型""" FATIGUE_EVENT = "fatigue" DISTRACTION_EVENT = "distraction" UNKNOWN_DETECTION = "unknown" RANDOM_SAMPLE = "random" EDGE_CASE = "edge_case"
@dataclass class TriggerCondition: """触发条件""" type: TriggerType confidence: float duration: float context: dict
class DataCollectionTrigger: """ 数据采集触发器 触发策略: 1. 事件触发:检测到异常事件 2. 随机采样:定期随机采集 3. 困难样本:模型不确定样本 4. 边缘案例:特殊场景 """ def __init__(self): self.thresholds = { 'fatigue_score': 0.6, 'distraction_score': 0.6, 'uncertainty_threshold': 0.3, 'random_sample_rate': 0.001, } self.pre_event_buffer = 5.0 self.post_event_buffer = 2.0 self.daily_storage_limit = 100 def should_collect(self, fatigue_score: float, distraction_score: float, model_uncertainty: float, context: dict) -> Optional[TriggerCondition]: """ 判断是否采集数据 Args: fatigue_score: 疲劳评分 distraction_score: 分心评分 model_uncertainty: 模型不确定性 context: 上下文信息 Returns: 触发条件(如果需要采集) """ if fatigue_score > self.thresholds['fatigue_score']: return TriggerCondition( type=TriggerType.FATIGUE_EVENT, confidence=fatigue_score, duration=0, context=context ) if distraction_score > self.thresholds['distraction_score']: return TriggerCondition( type=TriggerType.DISTRACTION_EVENT, confidence=distraction_score, duration=0, context=context ) if model_uncertainty > self.thresholds['uncertainty_threshold']: return TriggerCondition( type=TriggerType.EDGE_CASE, confidence=model_uncertainty, duration=0, context=context ) if np.random.rand() < self.thresholds['random_sample_rate']: return TriggerCondition( type=TriggerType.RANDOM_SAMPLE, confidence=1.0, duration=0, context=context ) return None def get_collection_window(self, trigger: TriggerCondition) -> tuple: """ 获取采集窗口 Args: trigger: 触发条件 Returns: (start_offset, end_offset) 相对于触发时刻的偏移(秒) """ if trigger.type in [TriggerType.FATIGUE_EVENT, TriggerType.DISTRACTION_EVENT]: return (-self.pre_event_buffer, self.post_event_buffer) else: return (-0.5, 0.5)
if __name__ == "__main__": trigger = DataCollectionTrigger() test_cases = [ {'fatigue': 0.8, 'distraction': 0.2, 'uncertainty': 0.1}, {'fatigue': 0.3, 'distraction': 0.7, 'uncertainty': 0.2}, {'fatigue': 0.4, 'distraction': 0.3, 'uncertainty': 0.6}, {'fatigue': 0.2, 'distraction': 0.1, 'uncertainty': 0.1}, ] for i, case in enumerate(test_cases): result = trigger.should_collect( case['fatigue'], case['distraction'], case['uncertainty'], {'speed': 60, 'road_type': 'highway'} ) if result: window = trigger.get_collection_window(result) print(f"场景{i+1}: 触发={result.type.value}, 窗口={window}") else: print(f"场景{i+1}: 不采集")
|