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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
| """ Fraunhofer IOSB 乘员行为识别系统 基于3D姿态的行为分类 """
import numpy as np from typing import List, Dict, Tuple from dataclasses import dataclass from enum import Enum
class Activity(Enum): """车内活动类型(35种)""" DRIVING_NORMAL = 0 STEERING = 1 CHECKING_MIRROR = 2 PHONE_CALL_HAND = 3 PHONE_CALL_HANDSFREE = 4 TEXTING = 5 DRINKING = 6 EATING = 7 SLEEPING = 8 RESTING_EYES = 9 READING = 10 WATCHING_SCREEN = 11 LISTENING_MUSIC = 12 TALKING_PASSENGER = 13 GESTURING = 14 REACHING = 15 SEARCHING = 16 ADJUSTING_CONTROLS = 17 PUTTING_ON_SEATBELT = 18 REMOVING_SEATBELT = 19
@dataclass class Skeleton3D: """3D骨骼数据""" joints: np.ndarray confidence: np.ndarray timestamp: float
@dataclass class DetectedObject: """检测到的物体""" label: str position: np.ndarray confidence: float
class ActivityRecognizer: """ 行为识别器 基于3D骨骼 + 物体检测 + 运动分析 """ def __init__(self, history_length: int = 30, fps: int = 30): """ Args: history_length: 历史帧数 fps: 帧率 """ self.history_length = history_length self.fps = fps self.skeleton_history: List[Skeleton3D] = [] self.activity_history: List[Activity] = [] self.activity_thresholds = { Activity.DRINKING: 1.5, Activity.EATING: 2.0, Activity.PHONE_CALL_HAND: 3.0, Activity.TEXTING: 2.0, Activity.SLEEPING: 5.0, } def update_history(self, skeleton: Skeleton3D): """更新骨骼历史""" self.skeleton_history.append(skeleton) if len(self.skeleton_history) > self.history_length: self.skeleton_history.pop(0) def extract_pose_features(self, skeleton: Skeleton3D) -> Dict: """ 提取姿态特征 Returns: features: 姿态特征字典 """ joints = skeleton.joints head_position = joints[0] head_orientation = self._compute_head_orientation(joints[0:3]) left_arm_angle = self._compute_arm_angle(joints[3], joints[5], joints[7]) right_arm_angle = self._compute_arm_angle(joints[4], joints[6], joints[8]) left_hand_pos = joints[7] right_hand_pos = joints[8] left_hand_to_head = left_hand_pos - head_position right_hand_to_head = right_hand_pos - head_position torso_angle = self._compute_torso_angle(joints[9], joints[10]) gaze_direction = head_orientation return { 'head_position': head_position, 'head_orientation': head_orientation, 'left_arm_angle': left_arm_angle, 'right_arm_angle': right_arm_angle, 'left_hand_to_head': left_hand_to_head, 'right_hand_to_head': right_hand_to_head, 'torso_angle': torso_angle, 'gaze_direction': gaze_direction } def extract_motion_features(self) -> Dict: """ 提取运动特征(基于历史) Returns: features: 运动特征字典 """ if len(self.skeleton_history) < 2: return {} hand_velocities = [] for i in range(1, len(self.skeleton_history)): dt = (self.skeleton_history[i].timestamp - self.skeleton_history[i-1].timestamp) left_hand_vel = np.linalg.norm( self.skeleton_history[i].joints[7] - self.skeleton_history[i-1].joints[7] ) / dt right_hand_vel = np.linalg.norm( self.skeleton_history[i].joints[8] - self.skeleton_history[i-1].joints[8] ) / dt hand_velocities.append((left_hand_vel, right_hand_vel)) hand_velocities = np.array(hand_velocities) mean_left_vel = np.mean(hand_velocities[:, 0]) mean_right_vel = np.mean(hand_velocities[:, 1]) std_right_vel = np.std(hand_velocities[:, 1]) head_positions = np.array([s.joints[0] for s in self.skeleton_history]) head_movement = np.std(head_positions, axis=0) head_stability = 1.0 / (np.linalg.norm(head_movement) + 0.01) return { 'mean_left_hand_velocity': mean_left_vel, 'mean_right_hand_velocity': mean_right_vel, 'std_right_hand_velocity': std_right_vel, 'head_stability': head_stability } def classify_activity(self, pose_features: Dict, motion_features: Dict, detected_objects: List[DetectedObject]) -> Activity: """ 分类当前活动 Args: pose_features: 姿态特征 motion_features: 运动特征 detected_objects: 检测到的物体 Returns: activity: 识别的活动类型 """ has_phone = any(obj.label == 'phone' for obj in detected_objects) has_bottle = any(obj.label == 'bottle' for obj in detected_objects) has_book = any(obj.label == 'book' for obj in detected_objects) right_hand = pose_features['right_hand_to_head'] right_arm_angle = pose_features['right_arm_angle'] if has_phone: if right_hand[1] > 0 and np.linalg.norm(right_hand) < 0.3: return Activity.PHONE_CALL_HAND if has_bottle: if (right_arm_angle > 60 and motion_features.get('mean_right_hand_velocity', 0) < 0.1): return Activity.DRINKING if motion_features.get('std_right_hand_velocity', 0) > 0.15: if any(obj.label in ['food', 'bottle'] for obj in detected_objects): return Activity.EATING if (pose_features['head_orientation'][1] < -30 and motion_features.get('head_stability', 0) > 0.9 and motion_features.get('mean_right_hand_velocity', 0) < 0.05): return Activity.SLEEPING if has_book: if pose_features['head_orientation'][1] > 20: return Activity.READING if (right_arm_angle < 30 and motion_features.get('mean_right_hand_velocity', 0) < 0.1 and pose_features['gaze_direction'][0] < 15): return Activity.DRIVING_NORMAL return Activity.DRIVING_NORMAL def _compute_head_orientation(self, head_joints: np.ndarray) -> np.ndarray: """计算头部姿态角度""" orientation = np.array([0, 0, 0]) left_eye = head_joints[1] right_eye = head_joints[2] if left_eye[0] < right_eye[0]: orientation[0] = 0 else: orientation[0] = 30 return orientation def _compute_arm_angle(self, shoulder: np.ndarray, elbow: np.ndarray, wrist: np.ndarray) -> float: """计算手臂弯曲角度""" upper_arm = elbow - shoulder forearm = wrist - elbow cos_angle = np.dot(upper_arm, forearm) / ( np.linalg.norm(upper_arm) * np.linalg.norm(forearm) + 1e-6 ) angle = np.arccos(np.clip(cos_angle, -1, 1)) return np.degrees(angle) def _compute_torso_angle(self, torso_upper: np.ndarray, torso_lower: np.ndarray) -> float: """计算躯干倾斜角度""" direction = torso_lower - torso_upper vertical = np.array([0, -1, 0]) cos_angle = np.dot(direction, vertical) / np.linalg.norm(direction) return np.degrees(np.arccos(cos_angle))
class IntentionPredictor: """ 意图预测器 基于行为序列预测下一步动作 """ def __init__(self): self.activity_sequence = [] self.sequence_length = 10 self.transitions = { Activity.SEARCHING: [Activity.PHONE_CALL_HAND, Activity.DRINKING], Activity.REACHING: [Activity.DRINKING, Activity.EATING, Activity.TEXTING], Activity.REMOVING_SEATBELT: [Activity.SEARCHING, Activity.REACHING] } def update_sequence(self, activity: Activity): """更新活动序列""" self.activity_sequence.append(activity) if len(self.activity_sequence) > self.sequence_length: self.activity_sequence.pop(0) def predict_next_intention(self) -> List[Tuple[Activity, float]]: """ 预测下一步意图 Returns: predictions: [(活动, 概率), ...] """ if len(self.activity_sequence) == 0: return [] current_activity = self.activity_sequence[-1] if current_activity in self.transitions: possible_next = self.transitions[current_activity] predictions = [(a, 1.0/len(possible_next)) for a in possible_next] else: predictions = [] return predictions
if __name__ == "__main__": recognizer = ActivityRecognizer() predictor = IntentionPredictor() np.random.seed(42) normal_skeleton = Skeleton3D( joints=np.array([ [0, 0, 0], [-0.03, 0.05, 0.1], [0.03, 0.05, 0.1], [-0.15, -0.1, 0], [0.15, -0.1, 0], [-0.25, -0.3, 0.1], [0.25, -0.3, 0.1], [-0.2, -0.2, 0.3], [0.2, -0.2, 0.3], [0, -0.4, 0], [0, -0.7, 0], [0, -0.8, 0], [-0.1, -0.9, -0.2], [0.1, -0.9, -0.2], [-0.1, -1.0, -0.4], [0.1, -1.0, -0.4], ]), confidence=np.ones(17) * 0.9, timestamp=0.0 ) pose_features = recognizer.extract_pose_features(normal_skeleton) motion_features = recognizer.extract_motion_features() print("姿态特征:") print(f" 头部位置: {pose_features['head_position']}") print(f" 左臂角度: {pose_features['left_arm_angle']:.1f}°") print(f" 右臂角度: {pose_features['right_arm_angle']:.1f}°") phone_skeleton = Skeleton3D( joints=np.array([ [0, 0.05, 0], [-0.03, 0.05, 0.1], [0.03, 0.05, 0.1], [-0.15, -0.1, 0], [0.15, -0.1, 0], [-0.25, -0.3, 0.1], [0.35, -0.1, 0.2], [-0.2, -0.2, 0.3], [0.15, 0.1, 0.1], [0, -0.4, 0], [0, -0.7, 0], [0, -0.8, 0], [-0.1, -0.9, -0.2], [0.1, -0.9, -0.2], [-0.1, -1.0, -0.4], [0.1, -1.0, -0.4], ]), confidence=np.ones(17) * 0.9, timestamp=1.0 ) phone_features = recognizer.extract_pose_features(phone_skeleton) print(f"\n打电话姿态:") print(f" 右手到头部距离: {np.linalg.norm(phone_features['right_hand_to_head']):.2f}m") detected_objects = [DetectedObject('phone', np.array([0.15, 0.1, 0.1]), 0.95)] activity = recognizer.classify_activity(phone_features, motion_features, detected_objects) print(f" 识别活动: {activity.name}")
|