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 263 264 265 266 267 268 269 270 271
| """ Fraunhofer AktiMeter 骨骼数据接口仿真 基于3D人体姿态估计的座舱行为分析
核心思想: - 不存储/传输原始视频 - 只处理骨骼关键点序列(3D坐标+置信度) - 所有行为分析基于骨骼运动模式
硬件配置: - 边缘设备: 65W USB-C供电 - 摄像头: RGB + 可选IR - 帧率: 30fps - 骨骼模型: 17关键点 (COCO格式) 或 33关键点 (BlazePose) """
import numpy as np from dataclasses import dataclass, field from typing import List, Optional, Tuple from enum import Enum import time
class ActivityType(Enum): """座舱活动分类""" NORMAL_DRIVING = "normal_driving" EATING = "eating" DRINKING = "drinking" PHONE_CALL = "phone_call" SLEEPING = "sleeping" READING = "reading" FEET_ON_DASH = "feet_on_dashboard" RECLINED = "reclined" SIDEWAYS = "sideways" REACHING_BACK = "reaching_back" TEXTING = "texting" UNKNOWN = "unknown"
class OPLevel(Enum): """OOP严重程度""" NORMAL = 0 MILD_OOP = 1 SEVERE_OOP = 2 CRITICAL = 3
@dataclass class Skeleton3D: """ 3D骨骼关键点(单帧) COCO-17格式 + 3D深度估计 关键点索引: 0: nose, 1: left_eye, 2: right_eye, 3: left_ear, 4: right_ear 5: left_shoulder, 6: right_shoulder, 7: left_elbow, 8: right_elbow 9: left_wrist, 10: right_wrist, 11: left_hip, 12: right_hip 13: left_knee, 14: right_knee, 15: left_ankle, 16: right_ankle """ keypoints: np.ndarray confidence: np.ndarray timestamp: float def get_joint(self, idx: int) -> np.ndarray: return self.keypoints[idx] def get_torso_angle(self) -> float: """计算躯干倾斜角度(垂直=0°,后仰>0°)""" shoulder_mid = (self.keypoints[5] + self.keypoints[6]) / 2 hip_mid = (self.keypoints[11] + self.keypoints[12]) / 2 torso_vec = hip_mid - shoulder_mid vertical = np.array([0, 0, -1]) cos_angle = np.dot(torso_vec, vertical) / ( np.linalg.norm(torso_vec) * np.linalg.norm(vertical) ) return np.degrees(np.arccos(np.clip(cos_angle, -1, 1))) def get_torso_rotation(self) -> float: """计算躯干旋转角度(正面=0°)""" ls = self.keypoints[5] rs = self.keypoints[6] shoulder_vec = rs - ls return np.degrees(np.arctan2(shoulder_vec[2], shoulder_vec[0])) def get_feet_position(self) -> str: """判断脚的位置""" left_ankle = self.keypoints[15] right_ankle = self.keypoints[16] if left_ankle[1] > 0.3 and left_ankle[2] > 0.2: return "feet_on_dashboard" if right_ankle[1] > 0.3 and right_ankle[2] > 0.2: return "feet_on_dashboard" return "normal"
class CabinActivityAnalyzer: """ 座舱行为分析器 基于骨骼序列的活动分类 + OOP检测 """ HISTORY_LEN = 90 def __init__(self): self.history: List[Skeleton3D] = [] self.current_activity = ActivityType.NORMAL_DRIVING self.oop_level = OPLevel.NORMAL self.confidence = 0.0 def update(self, skeleton: Skeleton3D) -> dict: """处理新骨骼帧,输出行为分析""" self.history.append(skeleton) if len(self.history) > self.HISTORY_LEN: self.history = self.history[-self.HISTORY_LEN:] if len(self.history) < 10: return {'activity': ActivityType.UNKNOWN, 'oop': OPLevel.NORMAL} oop_level = self._detect_oop(skeleton) activity = self._classify_activity() if self._detect_sleep(): activity = ActivityType.SLEEPING self.current_activity = activity self.oop_level = oop_level return { 'activity': activity, 'oop_level': oop_level, 'torso_angle': skeleton.get_torso_angle(), 'torso_rotation': skeleton.get_torso_rotation(), 'feet_position': skeleton.get_feet_position(), 'confidence': self.confidence, 'timestamp': skeleton.timestamp, 'skeleton_data': skeleton.keypoints, 'raw_video': None } def _detect_oop(self, skel: Skeleton3D) -> OPLevel: """OOP严重程度检测""" torso_angle = skel.get_torso_angle() feet_pos = skel.get_feet_position() if feet_pos == "feet_on_dashboard": return OPLevel.CRITICAL if torso_angle > 45: return OPLevel.SEVERE_OOP if torso_angle > 25: return OPLevel.MILD_OOP return OPLevel.NORMAL def _classify_activity(self) -> ActivityType: """基于骨骼序列的活动分类""" recent = self.history[-30:] left_wrist_y = [s.keypoints[9][1] for s in recent] right_wrist_y = [s.keypoints[10][1] for s in recent] nose = recent[-1].keypoints[0] left_wrist = recent[-1].keypoints[9] right_wrist = recent[-1].keypoints[10] dist_l = np.linalg.norm(left_wrist - nose) dist_r = np.linalg.norm(right_wrist - nose) min_hand_dist = min(dist_l, dist_r) if min_hand_dist < 0.15: left_ear = recent[-1].keypoints[3] right_ear = recent[-1].keypoints[4] dist_ear = min( np.linalg.norm(left_wrist - left_ear), np.linalg.norm(right_wrist - right_ear) ) if dist_ear < 0.20: return ActivityType.PHONE_CALL if max(left_wrist_y) - min(left_wrist_y) > 0.1: return ActivityType.EATING if dist_l < dist_r else ActivityType.DRINKING return ActivityType.NORMAL_DRIVING def _detect_sleep(self) -> bool: """基于骨骼的睡眠检测""" if len(self.history) < 60: return False recent = self.history[-60:] nose_y = [s.keypoints[0][1] for s in recent] shoulder_y = [(s.keypoints[5][1] + s.keypoints[6][1]) / 2 for s in recent] head_drop = nose_y[-1] - shoulder_y[-1] if head_drop > 0.05: movement = np.std([s.keypoints[0] for s in recent], axis=0) if np.mean(movement) < 0.01: return True return False
if __name__ == "__main__": analyzer = CabinActivityAnalyzer() print("=== 场景1: 正常驾驶 ===") for i in range(30): skel = Skeleton3D( keypoints=np.random.normal(0, 0.01, (17, 3)) + np.array([ [0, 0.1, 0.5], [0.05, 0.12, 0.5], [−0.05, 0.12, 0.5], [0.08, 0.1, 0.5], [−0.08, 0.1, 0.5], [0.1, 0, 0.4], [−0.1, 0, 0.4], [0.15, 0, 0.3], [−0.15, 0, 0.3], [0.2, 0, 0.2], [−0.2, 0, 0.2], [0.08, −0.3, 0.4], [−0.08, −0.3, 0.4], [0.1, −0.5, 0.3], [−0.1, −0.5, 0.3], [0.1, −0.7, 0.1], [−0.1, −0.7, 0.1], ]), confidence=np.ones(17) * 0.9, timestamp=time.time() ) result = analyzer.update(skel) print(f"活动: {result['activity'].value}") print(f"OOP: {result['oop_level'].name}") print(f"躯干角度: {result['torso_angle']:.1f}°") print("\n=== 场景2: 脚放仪表台 (严重OOP) ===") for i in range(30): base = np.random.normal(0, 0.01, (17, 3)) base[15] = [0.2, 0.4, 0.35] base[16] = [0.1, 0.38, 0.33] base += np.array([ [0, 0.1, 0.5], [0.05, 0.12, 0.5], [−0.05, 0.12, 0.5], [0.08, 0.1, 0.5], [−0.08, 0.1, 0.5], [0.1, 0, 0.4], [−0.1, 0, 0.4], [0.15, 0, 0.3], [−0.15, 0, 0.3], [0.2, 0, 0.2], [−0.2, 0, 0.2], [0.08, −0.3, 0.4], [−0.08, −0.3, 0.4], [0.1, −0.5, 0.3], [−0.1, −0.5, 0.3], [0.1, −0.7, 0.1], [−0.1, −0.7, 0.1], ]) skel = Skeleton3D( keypoints=base, confidence=np.ones(17) * 0.85, timestamp=time.time() ) result = analyzer.update(skel) print(f"活动: {result['activity'].value}") print(f"OOP: {result['oop_level'].name}") print(f"脚部位置: {result['feet_position']}") print(f"躯干角度: {result['torso_angle']:.1f}°")
|