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
| class PERCLOSOptimizer: """PERCLOS优化器""" def __init__(self, window_sec: int = 60): self.window_sec = window_sec self.eye_openness_history = [] def add_frame(self, eye_openness: float): self.eye_openness_history.append(eye_openness) if len(self.eye_openness_history) > self.window_sec * 30: self.eye_openness_history.pop(0) def compute_perclos(self) -> float: if len(self.eye_openness_history) < self.window_sec * 15: return 0.0 threshold = 0.2 closed_count = sum(1 for x in self.eye_openness_history if x < threshold) return closed_count / len(self.eye_openness_history) * 100 def detect_fatigue(self) -> str: perclos = self.compute_perclos() if perclos < 30: return '正常' elif perclos < 50: return '轻度疲劳' else: return '重度疲劳'
|