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
| import numpy as np from typing import List, Dict
class FatigueSceneGenerator: """ 使用Cosmos 3生成疲劳驾驶场景 """ def __init__(self): self.api_endpoint = "https://api.nvidia.com/cosmos/v3" self.fatigue_levels = { 'mild': { 'eye_closure_rate': 0.15, 'yawning_freq': 0.2, 'blinking_freq': 20 }, 'moderate': { 'eye_closure_rate': 0.30, 'yawning_freq': 0.5, 'blinking_freq': 15 }, 'severe': { 'eye_closure_rate': 0.50, 'yawning_freq': 1.0, 'blinking_freq': 10 } } def generate_prompt(self, fatigue_level: str, scene_context: Dict) -> str: """ 生成Cosmos 3提示词 Args: fatigue_level: 'mild' | 'moderate' | 'severe' scene_context: { 'time': 'day' | 'night', 'weather': 'sunny' | 'rainy', 'road_type': 'highway' | 'city', 'vehicle_speed': 80 # km/h } Returns: prompt: 文本提示词 """ params = self.fatigue_levels[fatigue_level] prompt = f""" 生成一段30秒的驾驶舱内视频: 场景设定: - 时间:{scene_context.get('time', 'day')} - 天气:{scene_context.get('weather', 'sunny')} - 道路:{scene_context.get('road_type', 'highway')} - 车速:{scene_context.get('vehicle_speed', 80)} km/h 驾驶员状态(疲劳等级:{fatigue_level}): - 眼睛闭合率:{params['eye_closure_rate']*100:.0f}%(PERCLOS指标) - 打哈欠频率:{params['yawning_freq']:.1f}次/分钟 - 眨眼频率:{params['blinking_freq']}次/分钟 - 头部姿态:缓慢下垂 - 面部表情:疲惫、眼神呆滞 物理约束: - 车辆逐渐偏离车道中心线 - 方向盘修正幅度增大 - 车速轻微波动(±5 km/h) 摄像机视角: - 红外摄像头,安装在仪表台上方 - 覆盖驾驶员面部和上半身 - 帧率:30fps,分辨率:1920×1080 """ return prompt def generate_video(self, fatigue_level: str, scene_context: Dict, duration: int = 30) -> Dict: """ 生成疲劳驾驶视频 Returns: { 'video_path': str, 'metadata': dict, 'annotations': list } """ prompt = self.generate_prompt(fatigue_level, scene_context) video_path = f"synthetic/fatigue_{fatigue_level}_{scene_context['time']}.mp4" annotations = self.generate_annotations( fatigue_level, scene_context, duration ) return { 'video_path': video_path, 'metadata': { 'fatigue_level': fatigue_level, 'scene_context': scene_context, 'duration': duration, 'source': 'cosmos_3_synthetic' }, 'annotations': annotations } def generate_annotations(self, fatigue_level: str, scene_context: Dict, duration: int) -> List[Dict]: """ 自动生成标注(基于物理模型) """ annotations = [] params = self.fatigue_levels[fatigue_level] fps = 30 n_frames = duration * fps for frame_idx in range(n_frames): timestamp = frame_idx / fps annotation = { 'frame_idx': frame_idx, 'timestamp': timestamp, 'driver_state': { 'fatigue_level': fatigue_level, 'perclos': params['eye_closure_rate'] + np.random.randn() * 0.05, 'eye_openness': 1.0 - params['eye_closure_rate'] * np.random.rand(), 'yawning': np.random.rand() < params['yawning_freq'] / 60, 'head_pose': { 'pitch': np.random.randn() * 5 + 10, 'yaw': np.random.randn() * 3, 'roll': np.random.randn() * 2 } }, 'vehicle_state': { 'lane_offset': np.random.randn() * 0.3, 'speed_variation': np.random.randn() * 5, 'steering_angle': np.random.randn() * 10 } } annotations.append(annotation) return annotations
if __name__ == "__main__": generator = FatigueSceneGenerator() scene = generator.generate_video( fatigue_level='moderate', scene_context={ 'time': 'night', 'weather': 'sunny', 'road_type': 'highway', 'vehicle_speed': 80 }, duration=30 ) print(f"生成视频: {scene['video_path']}") print(f"标注帧数: {len(scene['annotations'])}")
|