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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
| import numpy as np import cv2 from typing import Dict, List, Tuple from dataclasses import dataclass
@dataclass class DMSOutput: """DMS 输出""" face_detected: bool gaze_direction: Tuple[float, float] eye_closure_ratio: float blink_rate: float fatigue_score: float distraction_score: float is_fatigued: bool is_distracted: bool
@dataclass class OMSOutput: """OMS 输出""" occupant_count: int seatbelt_status: Dict[str, bool] child_seat_detected: bool child_presence: bool occupant_types: Dict[str, str]
class RCarV4HDMSOMS: """R-Car V4H DMS/OMS 系统""" def __init__(self, dms_model_path: str, oms_model_path: str): """ Args: dms_model_path: DMS 模型路径 oms_model_path: OMS 模型路径 """ self.dms_face_detector = DRPAIInference(dms_model_path + '/face_detector') self.dms_gaze_estimator = DRPAIInference(dms_model_path + '/gaze_estimator') self.dms_eye_analyzer = DRPAIInference(dms_model_path + '/eye_analyzer') self.oms_detector = DRPAIInference(oms_model_path + '/occupant_detector') self.oms_seatbelt = DRPAIInference(oms_model_path + '/seatbelt_classifier') self.oms_child_seat = DRPAIInference(oms_model_path + '/child_seat_detector') self.eye_closure_history = [] self.history_window = 300 self.fatigue_threshold = 0.3 self.distraction_threshold = 3.0 def process_frame(self, dms_frame: np.ndarray, oms_frame: np.ndarray) -> Tuple[DMSOutput, OMSOutput]: """处理单帧 Args: dms_frame: DMS 摄像头帧 oms_frame: OMS 摄像头帧 Returns: (DMSOutput, OMSOutput) """ dms_output = self._process_dms(dms_frame) oms_output = self._process_oms(oms_frame) return dms_output, oms_output def _process_dms(self, frame: np.ndarray) -> DMSOutput: """处理 DMS""" face_result = self.dms_face_detector.inference(frame) face_detected = face_result['detected'] if not face_detected: return DMSOutput( face_detected=False, gaze_direction=(0, 0), eye_closure_ratio=0, blink_rate=0, fatigue_score=0, distraction_score=0, is_fatigued=False, is_distracted=False ) face_roi = self._extract_roi(frame, face_result['bbox']) gaze_result = self.dms_gaze_estimator.inference(face_roi) gaze_direction = (gaze_result['pitch'], gaze_result['yaw']) eye_result = self.dms_eye_analyzer.inference(face_roi) eye_closure_ratio = eye_result['closure_ratio'] self.eye_closure_history.append(eye_closure_ratio) if len(self.eye_closure_history) > self.history_window: self.eye_closure_history.pop(0) perclos = self._compute_perclos() is_fatigued = perclos > self.fatigue_threshold fatigue_score = perclos is_distracted = self._is_gaze_off_road(gaze_direction) distraction_score = self._compute_distraction_score(gaze_direction) blink_rate = self._compute_blink_rate() return DMSOutput( face_detected=True, gaze_direction=gaze_direction, eye_closure_ratio=eye_closure_ratio, blink_rate=blink_rate, fatigue_score=fatigue_score, distraction_score=distraction_score, is_fatigued=is_fatigued, is_distracted=is_distracted ) def _process_oms(self, frame: np.ndarray) -> OMSOutput: """处理 OMS""" occupant_result = self.oms_detector.inference(frame) occupant_count = occupant_result['count'] seatbelt_result = self.oms_seatbelt.inference(frame) seatbelt_status = seatbelt_result['status'] child_seat_result = self.oms_child_seat.inference(frame) child_seat_detected = child_seat_result['detected'] child_presence = child_seat_result['child_present'] occupant_types = occupant_result.get('types', {}) return OMSOutput( occupant_count=occupant_count, seatbelt_status=seatbelt_status, child_seat_detected=child_seat_detected, child_presence=child_presence, occupant_types=occupant_types ) def _extract_roi(self, frame: np.ndarray, bbox: List[int]) -> np.ndarray: """提取 ROI""" x, y, w, h = bbox return frame[y:y+h, x:x+w] def _compute_perclos(self) -> float: """计算 PERCLOS(眼睑闭合时间比例)""" if len(self.eye_closure_history) < 30: return 0.0 closure_threshold = 0.7 closed_frames = sum(1 for r in self.eye_closure_history if r > closure_threshold) return closed_frames / len(self.eye_closure_history) def _is_gaze_off_road(self, gaze: Tuple[float, float]) -> bool: """判断视线是否离开道路""" pitch, yaw = gaze return abs(pitch) > 15 or abs(yaw) > 15 def _compute_distraction_score(self, gaze: Tuple[float, float]) -> float: """计算分心分数""" pitch, yaw = gaze return max(abs(pitch), abs(yaw)) / 15.0 def _compute_blink_rate(self) -> float: """计算眨眼频率""" if len(self.eye_closure_history) < 60: return 0.0 blinks = 0 for i in range(1, len(self.eye_closure_history)): if self.eye_closure_history[i-1] > 0.7 and self.eye_closure_history[i] < 0.3: blinks += 1 fps = 30 duration_minutes = len(self.eye_closure_history) / fps / 60 return blinks / duration_minutes if duration_minutes > 0 else 0
class DMSOMSApplication: """DMS/OMS 应用""" def __init__(self): self.system = RCarV4HDMSOMS( dms_model_path='/opt/models/dms', oms_model_path='/opt/models/oms' ) self.dms_camera = cv2.VideoCapture(0) self.oms_camera = cv2.VideoCapture(1) def run(self): """主循环""" while True: ret_dms, dms_frame = self.dms_camera.read() ret_oms, oms_frame = self.oms_camera.read() if not ret_dms or not ret_oms: break dms_output, oms_output = self.system.process_frame(dms_frame, oms_frame) if dms_output.is_fatigued: print("⚠️ 疲劳警告!") if dms_output.is_distracted: print("⚠️ 分心警告!") if oms_output.child_presence: print("👶 检测到儿童!") self._display(dms_frame, oms_frame, dms_output, oms_output) if cv2.waitKey(1) & 0xFF == ord('q'): break self.dms_camera.release() self.oms_camera.release() cv2.destroyAllWindows() def _display(self, dms_frame, oms_frame, dms_output, oms_output): """显示结果""" cv2.putText(dms_frame, f"Gaze: {dms_output.gaze_direction}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2) cv2.putText(dms_frame, f"FATIGUE: {dms_output.fatigue_score:.2f}", (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255) if dms_output.is_fatigued else (0, 255, 0), 2) cv2.imshow('DMS', dms_frame) cv2.imshow('OMS', oms_frame)
|