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
| import numpy as np from typing import List, Dict
class InCabinSceneGenerator: """车内场景生成器""" def __init__(self, config: dict): self.config = config self.renderer = self._init_renderer() self.assets = { 'drivers': ['male_01', 'female_01', 'elderly_01'], 'poses': ['normal', 'lean_forward', 'recline', 'turn_left'], 'objects': ['phone', 'bottle', 'bag', 'luggage'], 'vehicles': ['sedan', 'suv', 'truck'] } def render(self, params: Dict) -> np.ndarray: """ 渲染车内场景 Args: params: 场景参数 Returns: image: RGB 图像 (H, W, 3) """ self._set_driver_pose(params.get('driver_pose', 'normal')) self._place_objects(params.get('objects', [])) self._set_lighting(params.get('lighting', 500)) self._set_seatbelt(params.get('seatbelt', True)) image = self.renderer.render() return image def _set_driver_pose(self, pose: str): """设置驾驶员姿态""" pose_angles = { 'normal': {'torso': 0, 'head': 0, 'arms': 0}, 'lean_forward': {'torso': 25, 'head': 15, 'arms': 0}, 'recline': {'torso': -20, 'head': -10, 'arms': 0}, 'turn_left': {'torso': 0, 'head': 30, 'arms': 10} } angles = pose_angles.get(pose, pose_angles['normal']) self.renderer.set_skeleton_angles(angles) def _place_objects(self, objects: List[Dict]): """放置物品""" for obj in objects: obj_type = obj.get('type') position = obj.get('position', [0, 0, 0]) rotation = obj.get('rotation', [0, 0, 0]) self.renderer.place_object(obj_type, position, rotation) def _set_lighting(self, intensity: float): """设置光照""" self.renderer.set_light_intensity(intensity) def _set_seatbelt(self, worn: bool): """设置安全带""" self.renderer.set_seatbelt_state(worn)
class VLMInterface: """VLM 模型接口""" def __init__(self, model_name: str = 'gpt-4-vision'): self.model_name = model_name self.api_key = self._load_api_key() def generate(self, image: np.ndarray, question: str) -> str: """ 生成回答 Args: image: 输入图像 question: 问题文本 Returns: answer: 生成的回答 """ image_base64 = self._encode_image(image) prompt = self._build_prompt(question) response = self._call_api(image_base64, prompt) return response def _build_prompt(self, question: str) -> str: """构建提示词""" return f"""Analyze the in-car scene and answer the question.
Context: This is a driver monitoring system for automotive safety.
Question: {question}
Please provide a structured answer with: 1. Direct answer (Yes/No/Description) 2. Confidence level (High/Medium/Low) 3. Supporting observations""" def parse_answer(self, response: str) -> dict: """解析回答""" lines = response.strip().split('\n') result = { 'answer': '', 'confidence': 'Medium', 'observations': [] } for line in lines: if line.startswith('Answer:'): result['answer'] = line.replace('Answer:', '').strip() elif line.startswith('Confidence:'): result['confidence'] = line.replace('Confidence:', '').strip() elif line.startswith('-'): result['observations'].append(line[1:].strip()) return result
|