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
| import numpy as np from typing import List, Dict
class DistractionActionGenerator: """ 分心动作生成器 生成各种分心行为的动画序列 """ DISTRACTION_TYPES = { "phone_call": { "description": "打电话", "duration": (3, 10), "hands": "one" }, "phone_texting": { "description": "发短信", "duration": (5, 15), "hands": "both" }, "eating": { "description": "吃东西", "duration": (10, 30), "hands": "one" }, "drinking": { "description": "喝水", "duration": (2, 5), "hands": "one" }, "adjusting_radio": { "description": "调整收音机", "duration": (2, 5), "hands": "one" }, "looking_away": { "description": "视线偏离", "duration": (2, 6), "hands": "none" }, "reaching_back": { "description": "伸手到后座", "duration": (3, 8), "hands": "one" } } def generate_action(self, action_type: str, driver) -> Dict: """ 生成分心动作 Args: action_type: 动作类型 driver: 驾驶员对象 Returns: action_data: 动作数据 """ action_config = self.DISTRACTION_TYPES[action_type] keyframes = self.generate_keyframes(action_type) for keyframe in keyframes: self.apply_keyframe(driver, keyframe) return { "type": action_type, "duration": np.random.uniform(*action_config["duration"]), "keyframes": keyframes, "label": self.generate_label(action_type) } def generate_keyframes(self, action_type: str) -> List[Dict]: """生成动作关键帧""" keyframes = [] if action_type == "phone_call": keyframes = [ {"frame": 0, "pose": "driving"}, {"frame": 30, "pose": "hand_to_ear"}, {"frame": 180, "pose": "phone_at_ear"}, {"frame": 210, "pose": "hand_down"}, {"frame": 240, "pose": "driving"} ] elif action_type == "looking_away": keyframes = [ {"frame": 0, "gaze": "forward"}, {"frame": 10, "gaze": "left"}, {"frame": 90, "gaze": "left"}, {"frame": 100, "gaze": "forward"} ] return keyframes def generate_label(self, action_type: str) -> Dict: """生成标注数据""" return { "class": action_type, "category": self.get_category(action_type), "severity": self.get_severity(action_type) } def get_category(self, action_type: str) -> str: """获取动作类别""" if action_type in ["phone_call", "phone_texting"]: return "phone_use" elif action_type in ["eating", "drinking"]: return "consumption" elif action_type == "looking_away": return "visual_distraction" else: return "other" def get_severity(self, action_type: str) -> int: """获取严重程度""" severity_map = { "phone_texting": 3, "phone_call": 2, "eating": 2, "drinking": 1, "looking_away": 2, "reaching_back": 3 } return severity_map.get(action_type, 1)
|