diff --git a/src/enhance_pedestrian_safety/config_manager.py b/src/enhance_pedestrian_safety/config_manager.py index 1f64c72ef9..c608de6674 100644 --- a/src/enhance_pedestrian_safety/config_manager.py +++ b/src/enhance_pedestrian_safety/config_manager.py @@ -4,31 +4,24 @@ import copy from typing import Dict, Any, Optional, List, Tuple -# 尝试导入yaml,如果不存在则跳过 try: import yaml - YAML_AVAILABLE = True except ImportError: YAML_AVAILABLE = False - print("警告: PyYAML未安装,将使用JSON格式") class ConfigValidator: - """配置验证器""" @staticmethod def validate_config(config: Dict[str, Any]) -> Tuple[bool, List[str]]: - """验证配置的有效性""" errors = [] - # 验证必填字段 required_sections = ['scenario', 'sensors', 'output'] for section in required_sections: if section not in config: errors.append(f"缺失必要配置节: {section}") - # 验证场景配置 if 'scenario' in config: scenario = config['scenario'] if 'duration' in scenario and scenario['duration'] <= 0: @@ -36,7 +29,6 @@ def validate_config(config: Dict[str, Any]) -> Tuple[bool, List[str]]: if 'town' not in scenario: errors.append("场景配置中缺失地图名称") - # 验证传感器配置 if 'sensors' in config: sensors = config['sensors'] if 'capture_interval' in sensors and sensors['capture_interval'] <= 0: @@ -47,7 +39,6 @@ def validate_config(config: Dict[str, Any]) -> Tuple[bool, List[str]]: elif any(dim <= 0 for dim in sensors['image_size']): errors.append("图像尺寸必须大于0") - # 验证性能配置 if 'performance' in config: perf = config['performance'] if 'batch_size' in perf and perf['batch_size'] <= 0: @@ -57,22 +48,18 @@ def validate_config(config: Dict[str, Any]) -> Tuple[bool, List[str]]: @staticmethod def suggest_optimizations(config: Dict[str, Any]) -> List[str]: - """根据配置提供优化建议""" suggestions = [] - # 内存使用建议 if config.get('sensors', {}).get('lidar_sensors', 0) > 0: lidar_config = config['sensors'].get('lidar_config', {}) max_points = lidar_config.get('max_points_per_frame', 50000) if max_points > 50000: suggestions.append(f"LiDAR最大点数({max_points})较高,建议降低到50000以下以减少内存使用") - # 性能建议 capture_interval = config['sensors'].get('capture_interval', 2.0) if capture_interval < 1.0: suggestions.append(f"采集间隔({capture_interval}s)较短,可能导致高负载,建议增加到1.0s以上") - # 输出建议 output = config.get('output', {}) enabled_outputs = [k for k, v in output.items() if isinstance(v, bool) and v] if len(enabled_outputs) > 5: @@ -82,14 +69,11 @@ def suggest_optimizations(config: Dict[str, Any]) -> List[str]: class ConfigOptimizer: - """配置优化器""" @staticmethod def optimize_for_memory(config: Dict[str, Any]) -> Dict[str, Any]: - """为内存使用优化配置""" optimized = copy.deepcopy(config) - # 调整LiDAR设置 if optimized['sensors'].get('lidar_sensors', 0) > 0: lidar_config = optimized['sensors'].setdefault('lidar_config', {}) lidar_config.update({ @@ -99,7 +83,6 @@ def optimize_for_memory(config: Dict[str, Any]) -> Dict[str, Any]: 'max_batch_memory_mb': 30 }) - # 调整性能设置 perf = optimized.setdefault('performance', {}) perf.update({ 'batch_size': 3, @@ -110,7 +93,6 @@ def optimize_for_memory(config: Dict[str, Any]) -> Dict[str, Any]: 'frame_rate_limit': 3.0 }) - # 调整图像处理 perf['image_processing'] = { 'compress_images': True, 'compression_quality': 80, @@ -121,10 +103,8 @@ def optimize_for_memory(config: Dict[str, Any]) -> Dict[str, Any]: @staticmethod def optimize_for_quality(config: Dict[str, Any]) -> Dict[str, Any]: - """为数据质量优化配置""" optimized = copy.deepcopy(config) - # 调整传感器设置 sensors = optimized['sensors'] sensors.update({ 'image_size': [1920, 1080], @@ -139,7 +119,6 @@ def optimize_for_quality(config: Dict[str, Any]) -> Dict[str, Any]: } }) - # 调整输出设置 output = optimized['output'] output.update({ 'save_annotations': True, @@ -149,7 +128,6 @@ def optimize_for_quality(config: Dict[str, Any]) -> Dict[str, Any]: 'run_quality_check': True }) - # 调整增强设置 enhanced = optimized.setdefault('enhancement', {}) enhanced.update({ 'enabled': True, @@ -162,35 +140,31 @@ def optimize_for_quality(config: Dict[str, Any]) -> Dict[str, Any]: @staticmethod def optimize_for_speed(config: Dict[str, Any]) -> Dict[str, Any]: - """为处理速度优化配置""" optimized = copy.deepcopy(config) - # 调整传感器设置 sensors = optimized['sensors'] sensors.update({ 'image_size': [640, 480], 'capture_interval': 3.0, - 'lidar_sensors': 0, # 禁用LiDAR - 'radar_sensors': 0 # 禁用雷达 + 'lidar_sensors': 0, + 'radar_sensors': 0 }) - # 调整性能设置 perf = optimized.setdefault('performance', {}) perf.update({ 'batch_size': 10, 'enable_compression': True, - 'compression_level': 1, # 快速压缩 + 'compression_level': 1, 'enable_downsampling': True, 'enable_async_processing': True, 'max_cache_size': 20, 'frame_rate_limit': 10.0 }) - # 简化输出 output = optimized['output'] output.update({ 'save_raw': True, - 'save_stitched': False, # 不拼接图像 + 'save_stitched': False, 'save_annotations': False, 'save_lidar': False, 'save_fusion': False, @@ -201,9 +175,7 @@ def optimize_for_speed(config: Dict[str, Any]) -> Dict[str, Any]: class ConfigManager: - """配置管理器(增强版)""" - # 预定义配置模板 PRESET_CONFIGS = { 'balanced': { 'description': '平衡配置 - 兼顾性能和质量', @@ -238,31 +210,17 @@ class ConfigManager: @staticmethod def load_config(config_file: Optional[str] = None, preset: Optional[str] = None) -> Dict[str, Any]: - """ - 加载配置 - - Args: - config_file: 配置文件路径 - preset: 预设配置名称 - - Returns: - 配置字典 - """ - # 基础配置 config = ConfigManager._get_default_config() - # 应用预设配置 if preset: config = ConfigManager._apply_preset(config, preset) - # 加载用户配置文件 if config_file: if os.path.exists(config_file): config = ConfigManager._load_config_file(config_file, config) else: print(f"警告: 配置文件不存在: {config_file}") - # 验证配置 is_valid, errors = ConfigValidator.validate_config(config) if not is_valid: print("配置验证错误:") @@ -270,7 +228,6 @@ def load_config(config_file: Optional[str] = None, preset: Optional[str] = None) print(f" - {error}") raise ValueError("配置验证失败") - # 提供优化建议 suggestions = ConfigValidator.suggest_optimizations(config) if suggestions: print("配置优化建议:") @@ -281,7 +238,6 @@ def load_config(config_file: Optional[str] = None, preset: Optional[str] = None) @staticmethod def _get_default_config() -> Dict[str, Any]: - """获取默认配置(增强版)""" return { 'scenario': { 'name': 'multi_sensor_scene', @@ -291,8 +247,8 @@ def _get_default_config() -> Dict[str, Any]: 'time_of_day': 'noon', 'duration': 60, 'seed': 42, - 'timeout': 300, # 超时时间(秒) - 'retry_attempts': 3 # 重试次数 + 'timeout': 300, + 'retry_attempts': 3 }, 'traffic': { 'ego_vehicles': 1, @@ -336,7 +292,7 @@ def _get_default_config() -> Dict[str, Any]: 'memory_warning_threshold': 300, 'max_batch_memory_mb': 50, 'v2x_save_interval': 5, - 'compression_format': 'bin' # bin, npy, pcd + 'compression_format': 'bin' }, 'camera_config': { 'fov': 90.0, @@ -395,7 +351,7 @@ def _get_default_config() -> Dict[str, Any]: 'compression_quality': 85, 'resize_images': False, 'resize_dimensions': [640, 480], - 'format': 'jpg' # jpg, png + 'format': 'jpg' }, 'lidar_processing': { 'batch_size': 10, @@ -405,7 +361,7 @@ def _get_default_config() -> Dict[str, Any]: 'memory_warning_threshold': 350, 'max_batch_memory_mb': 50, 'v2x_save_interval': 5, - 'compression_method': 'zlib' # zlib, lz4, none + 'compression_method': 'zlib' }, 'fusion': { 'fusion_cache_size': 100, @@ -422,7 +378,7 @@ def _get_default_config() -> Dict[str, Any]: }, 'output': { 'data_dir': 'cvips_dataset', - 'output_format': 'standard', # standard, v2xformer, kitti, coco + 'output_format': 'standard', 'save_raw': True, 'save_stitched': True, 'save_annotations': False, @@ -436,12 +392,12 @@ def _get_default_config() -> Dict[str, Any]: 'run_quality_check': True, 'generate_summary': True, 'compression_enabled': True, - 'file_naming': 'sequential', # sequential, timestamp + 'file_naming': 'sequential', 'backup_original': False }, 'monitoring': { 'enable_logging': True, - 'log_level': 'INFO', # DEBUG, INFO, WARNING, ERROR + 'log_level': 'INFO', 'log_file': 'cvips.log', 'enable_performance_monitor': True, 'performance_log_interval': 10.0, @@ -468,7 +424,6 @@ def _get_default_config() -> Dict[str, Any]: @staticmethod def _apply_preset(config: Dict[str, Any], preset_name: str) -> Dict[str, Any]: - """应用预设配置""" if preset_name not in ConfigManager.PRESET_CONFIGS: print(f"警告: 未知的预设配置: {preset_name}") return config @@ -476,7 +431,6 @@ def _apply_preset(config: Dict[str, Any], preset_name: str) -> Dict[str, Any]: preset = ConfigManager.PRESET_CONFIGS[preset_name] print(f"应用预设配置: {preset_name} - {preset['description']}") - # 根据优化类型应用配置 optimization = preset.get('optimization', 'balanced') if optimization == 'memory': config = ConfigOptimizer.optimize_for_memory(config) @@ -491,7 +445,6 @@ def _apply_preset(config: Dict[str, Any], preset_name: str) -> Dict[str, Any]: @staticmethod def _load_config_file(config_file: str, base_config: Dict[str, Any]) -> Dict[str, Any]: - """加载配置文件""" try: with open(config_file, 'r', encoding='utf-8') as f: if (config_file.endswith('.yaml') or config_file.endswith('.yml')) and YAML_AVAILABLE: @@ -508,7 +461,6 @@ def _load_config_file(config_file: str, base_config: Dict[str, Any]) -> Dict[str @staticmethod def _deep_update(original: Dict[str, Any], update: Dict[str, Any]) -> Dict[str, Any]: - """深度更新字典""" for key, value in update.items(): if key in original and isinstance(original[key], dict) and isinstance(value, dict): ConfigManager._deep_update(original[key], value) @@ -518,8 +470,6 @@ def _deep_update(original: Dict[str, Any], update: Dict[str, Any]) -> Dict[str, @staticmethod def merge_args(config: Dict[str, Any], args: argparse.Namespace) -> Dict[str, Any]: - """合并命令行参数到配置""" - # 场景参数 if hasattr(args, 'scenario') and args.scenario: config['scenario']['name'] = args.scenario if hasattr(args, 'town') and args.town: @@ -533,58 +483,45 @@ def merge_args(config: Dict[str, Any], args: argparse.Namespace) -> Dict[str, An if hasattr(args, 'seed') and args.seed: config['scenario']['seed'] = args.seed - # 交通参数 if hasattr(args, 'num_vehicles') and args.num_vehicles: config['traffic']['background_vehicles'] = args.num_vehicles if hasattr(args, 'num_pedestrians') and args.num_pedestrians: config['traffic']['pedestrians'] = args.num_pedestrians - # 协同参数 if hasattr(args, 'num_coop_vehicles') and args.num_coop_vehicles: config['cooperative']['num_coop_vehicles'] = args.num_coop_vehicles - # 传感器参数 if hasattr(args, 'capture_interval') and args.capture_interval: config['sensors']['capture_interval'] = args.capture_interval - # V2X参数 if hasattr(args, 'enable_v2x'): config['v2x']['enabled'] = args.enable_v2x - # 增强参数 if hasattr(args, 'enable_enhancement'): config['enhancement']['enabled'] = args.enable_enhancement - # LiDAR参数 if hasattr(args, 'enable_lidar'): config['sensors']['lidar_sensors'] = 1 if args.enable_lidar else 0 config['output']['save_lidar'] = args.enable_lidar - # 融合参数 if hasattr(args, 'enable_fusion'): config['output']['save_fusion'] = args.enable_fusion - # 协同参数 if hasattr(args, 'enable_cooperative'): config['output']['save_cooperative'] = args.enable_cooperative - # 标注参数 if hasattr(args, 'enable_annotations'): config['output']['save_annotations'] = args.enable_annotations - # 验证参数 if hasattr(args, 'skip_validation'): config['output']['validate_data'] = not args.skip_validation - # 质量检查参数 if hasattr(args, 'skip_quality_check'): config['output']['run_quality_check'] = not args.skip_quality_check - # 分析参数 if hasattr(args, 'run_analysis'): config['output']['run_analysis'] = args.run_analysis - # 性能参数 if hasattr(args, 'batch_size') and args.batch_size: config['performance']['batch_size'] = args.batch_size @@ -596,7 +533,6 @@ def merge_args(config: Dict[str, Any], args: argparse.Namespace) -> Dict[str, An if args.enable_downsampling: config['sensors']['lidar_config']['downsample_ratio'] = 0.3 - # 输出格式 if hasattr(args, 'output_format') and args.output_format: config['output']['output_format'] = args.output_format @@ -604,7 +540,6 @@ def merge_args(config: Dict[str, Any], args: argparse.Namespace) -> Dict[str, An @staticmethod def save_config(config: Dict[str, Any], output_path: str, format: str = 'json'): - """保存配置到文件""" try: os.makedirs(os.path.dirname(output_path), exist_ok=True) @@ -623,7 +558,6 @@ def save_config(config: Dict[str, Any], output_path: str, format: str = 'json'): @staticmethod def generate_config_template(output_path: str, preset: Optional[str] = None): - """生成配置模板""" config = ConfigManager.load_config(preset=preset) config['metadata']['created'] = 'template' config['metadata']['description'] = f'配置模板 - {preset if preset else "通用"}' @@ -632,12 +566,10 @@ def generate_config_template(output_path: str, preset: Optional[str] = None): @staticmethod def print_config_summary(config: Dict[str, Any]): - """打印配置摘要""" print("\n" + "=" * 60) print("配置摘要") print("=" * 60) - # 场景信息 scenario = config['scenario'] print(f"\n📋 场景:") print(f" 名称: {scenario['name']}") @@ -646,7 +578,6 @@ def print_config_summary(config: Dict[str, Any]): print(f" 时长: {scenario['duration']}秒") print(f" 随机种子: {scenario.get('seed', '随机')}") - # 交通信息 traffic = config['traffic'] print(f"\n🚗 交通:") print(f" 主车: {traffic['ego_vehicles']}") @@ -654,7 +585,6 @@ def print_config_summary(config: Dict[str, Any]): print(f" 行人: {traffic['pedestrians']}") print(f" 交通灯: {'启用' if traffic['traffic_lights'] else '禁用'}") - # 传感器信息 sensors = config['sensors'] print(f"\n📷 传感器:") print(f" 车辆摄像头: {sensors['vehicle_cameras']}") @@ -663,7 +593,6 @@ def print_config_summary(config: Dict[str, Any]): print(f" 采集间隔: {sensors['capture_interval']}秒") print(f" 图像尺寸: {sensors['image_size'][0]}x{sensors['image_size'][1]}") - # V2X信息 v2x = config['v2x'] print(f"\n📡 V2X通信:") print(f" 状态: {'启用' if v2x['enabled'] else '禁用'}") @@ -671,14 +600,12 @@ def print_config_summary(config: Dict[str, Any]): print(f" 通信范围: {v2x['communication_range']}米") print(f" 更新间隔: {v2x['update_interval']}秒") - # 协同信息 coop = config['cooperative'] print(f"\n🤝 协同感知:") print(f" 协同车辆: {coop['num_coop_vehicles']}") print(f" 共享感知: {'启用' if coop['enable_shared_perception'] else '禁用'}") print(f" 交通警告: {'启用' if coop['enable_traffic_warnings'] else '禁用'}") - # 性能信息 perf = config['performance'] print(f"\n⚡ 性能:") print(f" 批处理大小: {perf['batch_size']}") @@ -686,7 +613,6 @@ def print_config_summary(config: Dict[str, Any]): print(f" 下采样: {'启用' if perf['enable_downsampling'] else '禁用'}") print(f" 帧率限制: {perf['frame_rate_limit']} FPS") - # 输出信息 output = config['output'] print(f"\n💾 输出:") print(f" 输出目录: {output['data_dir']}") @@ -699,7 +625,6 @@ def print_config_summary(config: Dict[str, Any]): @staticmethod def list_presets(): - """列出所有预设配置""" print("\n可用预设配置:") print("-" * 40) for name, preset in ConfigManager.PRESET_CONFIGS.items(): @@ -707,12 +632,9 @@ def list_presets(): print("-" * 40) -# 兼容旧版本的接口 def load_config(config_file=None): - """兼容旧版本的加载配置函数""" return ConfigManager.load_config(config_file) def merge_args(config, args): - """兼容旧版本的合并参数函数""" return ConfigManager.merge_args(config, args) \ No newline at end of file diff --git a/src/enhance_pedestrian_safety/lidar_processor.py b/src/enhance_pedestrian_safety/lidar_processor.py index 654aa674b8..8c0f6ebb20 100644 --- a/src/enhance_pedestrian_safety/lidar_processor.py +++ b/src/enhance_pedestrian_safety/lidar_processor.py @@ -23,21 +23,18 @@ def __init__(self, output_dir, config=None): self.frame_counter = 0 self.data_lock = threading.Lock() - # 性能优化:批量处理设置 self.batch_size = config.get('batch_size', 10) if config else 10 self.point_cloud_batch = [] self.enable_compression = config.get('enable_compression', True) if config else True self.compression_level = config.get('compression_level', 3) if config else 3 - # 内存管理 - 优化点3:更保守的内存设置 - self.max_points_per_frame = config.get('max_points_per_frame', 50000) if config else 50000 # 减少到5万点 + self.max_points_per_frame = config.get('max_points_per_frame', 50000) if config else 50000 self.enable_downsampling = config.get('enable_downsampling', True) if config else True - self.downsample_ratio = config.get('downsample_ratio', 0.3) if config else 0.3 # 增加下采样比例 + self.downsample_ratio = config.get('downsample_ratio', 0.3) if config else 0.3 - # 新增:内存监控和限制 - self.memory_warning_threshold = config.get('memory_warning_threshold', 300) if config else 300 # MB - self.max_batch_memory_mb = config.get('max_batch_memory_mb', 50) if config else 50 # 批次最大内存 - self.v2x_save_interval = config.get('v2x_save_interval', 5) if config else 5 # 每5帧保存一次V2X格式 + self.memory_warning_threshold = config.get('memory_warning_threshold', 300) if config else 300 + self.max_batch_memory_mb = config.get('max_batch_memory_mb', 50) if config else 50 + self.v2x_save_interval = config.get('v2x_save_interval', 5) if config else 5 self._init_calibration_files() @@ -81,109 +78,81 @@ def process_lidar_data(self, lidar_data, frame_num): points = self._carla_lidar_to_numpy(lidar_data) if points is None or points.shape[0] == 0: - print(f"警告: LiDAR数据为空或无效 (帧 {frame_num})") return None - # 检查内存使用情况 if self._check_memory_usage(): - print(f"警告: 内存使用过高,跳过LiDAR处理 (帧 {frame_num})") return None - # 下采样以减少内存使用 original_count = points.shape[0] if self.enable_downsampling and points.shape[0] > self.max_points_per_frame: points = self._downsample_point_cloud(points) - print(f"LiDAR下采样: {original_count} -> {points.shape[0]} 点 (帧 {frame_num})") - # 检查批次内存使用 batch_memory_mb = self._estimate_batch_memory_mb() if batch_memory_mb > self.max_batch_memory_mb: - print(f"批处理内存过高 ({batch_memory_mb:.1f}MB),强制保存当前批次") self._save_batch() - # 添加到批处理 self.point_cloud_batch.append((frame_num, points)) - # 如果达到批处理大小,保存批处理数据 if len(self.point_cloud_batch) >= self.batch_size: self._save_batch() - # 保存单个文件(向后兼容) bin_path = self._save_as_bin(points, frame_num) npy_path = self._save_as_npy(points, frame_num) - # 生成V2XFormer兼容格式 - 优化点:每v2x_save_interval帧保存一次 v2xformer_path = None if frame_num % self.v2x_save_interval == 0: try: v2xformer_path = self._save_as_v2xformer_format(points, frame_num) except Exception as e: - print(f"警告: V2XFormer格式保存失败: {e}") v2xformer_path = None metadata = self._generate_metadata(points, bin_path, npy_path, v2xformer_path) - # 定期清理内存 if frame_num % 20 == 0: gc.collect() return metadata except Exception as e: - print(f"处理LiDAR数据失败: {e}") - # 强制清理内存 gc.collect() return None def _carla_lidar_to_numpy(self, lidar_data): - """高效转换LiDAR数据""" try: - # 使用内存视图减少内存分配 points = np.frombuffer(lidar_data.raw_data, dtype=np.float32) points = np.reshape(points, (int(points.shape[0] / 4), 4)) - - # 只保留位置信息(x, y, z),忽略强度 points = points[:, :3] - # 清理临时数组 del lidar_data gc.collect() return points except Exception as e: - print(f"LiDAR数据转换失败: {e}") - - # 备选转换方法 try: points = [] for i in range(0, len(lidar_data), 4): point = lidar_data[i:i + 4] points.append([point.x, point.y, point.z]) - return np.array(points, dtype=np.float32) except: return None def _downsample_point_cloud(self, points): - """下采样点云以减少内存使用""" if points.shape[0] <= self.max_points_per_frame: return points - # 随机下采样 indices = np.random.choice(points.shape[0], int(points.shape[0] * self.downsample_ratio), replace=False) downsampled = points[indices] - # 清理内存 del points gc.collect() return downsampled def _estimate_batch_memory_mb(self): - """估计批处理内存使用""" if not self.point_cloud_batch: return 0 @@ -191,45 +160,38 @@ def _estimate_batch_memory_mb(self): for _, points in self.point_cloud_batch: total_points += points.shape[0] - # 每个点3个float32 (12字节),加上一些额外开销 - memory_bytes = total_points * 12 * 1.2 # 20%额外开销 + memory_bytes = total_points * 12 * 1.2 return memory_bytes / (1024 * 1024) def _check_memory_usage(self): - """检查内存使用情况""" try: import psutil process = psutil.Process() memory_mb = process.memory_info().rss / (1024 * 1024) if memory_mb > self.memory_warning_threshold: - print(f"警告: 进程内存使用过高: {memory_mb:.1f}MB") return True return False except: return False def _save_batch(self): - """批量保存点云数据(提高效率)""" if not self.point_cloud_batch: return batch_data = [] for frame_num, points in self.point_cloud_batch: - # 只保存点的坐标,不保存完整对象 batch_data.append({ 'frame_num': frame_num, 'points': points.tolist(), 'num_points': points.shape[0] }) - # 生成批处理文件名 min_frame = min([item[0] for item in self.point_cloud_batch]) max_frame = max([item[0] for item in self.point_cloud_batch]) batch_filename = f"lidar_batch_{min_frame:06d}_{max_frame:06d}.json" batch_path = os.path.join(self.lidar_dir, batch_filename) - # 压缩保存 if self.enable_compression: try: json_str = json.dumps(batch_data) @@ -240,49 +202,39 @@ def _save_batch(self): with open(batch_path, 'wb') as f: f.write(compressed_data) except Exception as e: - print(f"压缩保存批处理数据失败: {e}") - # 尝试非压缩保存 try: with open(batch_path, 'w') as f: json.dump(batch_data, f) except Exception as e2: - print(f"非压缩保存批处理数据失败: {e2}") + pass else: try: with open(batch_path, 'w') as f: json.dump(batch_data, f) except Exception as e: - print(f"保存批处理数据失败: {e}") + pass - # 清理批处理缓存 self.point_cloud_batch.clear() gc.collect() - print(f"批量保存LiDAR数据: {batch_filename}") - def _save_as_bin(self, points, frame_num): - """保存为二进制格式(兼容KITTI)""" bin_path = os.path.join(self.lidar_dir, f"lidar_{frame_num:06d}.bin") - # 添加强度信息(全为1) points_with_intensity = np.zeros((points.shape[0], 4), dtype=np.float32) points_with_intensity[:, :3] = points - points_with_intensity[:, 3] = 1.0 # 强度值 + points_with_intensity[:, 3] = 1.0 try: points_with_intensity.tofile(bin_path) except Exception as e: - print(f"保存BIN文件失败: {e}") return None return bin_path def _save_as_npy(self, points, frame_num): - """保存为numpy格式""" npy_path = os.path.join(self.lidar_dir, f"lidar_{frame_num:06d}.npy") try: - # 使用内存映射减少内存使用 memmap_array = np.lib.format.open_memmap( npy_path, mode='w+', @@ -292,23 +244,18 @@ def _save_as_npy(self, points, frame_num): memmap_array[:] = points[:] memmap_array.flush() except Exception as e: - print(f"保存NPY文件失败: {e}") - # 备选保存方式 try: np.save(npy_path, points) except Exception as e2: - print(f"备选保存NPY文件失败: {e2}") return None return npy_path def _save_as_v2xformer_format(self, points, frame_num): - """保存为V2XFormer兼容格式 - 优化点:减少保存频率""" v2x_dir = os.path.join(self.output_dir, "v2xformer_format") os.makedirs(v2x_dir, exist_ok=True) try: - # 创建V2XFormer格式的数据结构 v2x_data = { 'frame_id': frame_num, 'timestamp': datetime.now().timestamp(), @@ -327,21 +274,15 @@ def _save_as_v2xformer_format(self, points, frame_num): } } - # 保存为压缩的JSON格式 filename = f"{frame_num:06d}.pkl.gz" filepath = os.path.join(v2x_dir, filename) - # 使用pickle+gzip压缩保存 with gzip.open(filepath, 'wb') as f: pickle.dump(v2x_data, f, protocol=pickle.HIGHEST_PROTOCOL) - file_size = os.path.getsize(filepath) / 1024 # KB - print(f"保存V2XFormer格式: {filepath} ({file_size:.2f} KB)") - return filepath except Exception as e: - print(f"警告: 保存V2XFormer格式失败: {e}") return None def _generate_metadata(self, points, bin_path, npy_path, v2xformer_path): @@ -375,21 +316,18 @@ def _generate_metadata(self, points, bin_path, npy_path, v2xformer_path): with open(meta_path, 'w') as f: json.dump(metadata, f, indent=2) except Exception as e: - print(f"保存LiDAR元数据失败: {e}") + pass return metadata def flush_batch(self): - """强制刷新批处理数据""" if self.point_cloud_batch: self._save_batch() def generate_lidar_summary(self): - """生成LiDAR数据摘要(优化版)""" if not os.path.exists(self.lidar_dir): return None - # 快速统计文件 import glob bin_files = glob.glob(os.path.join(self.lidar_dir, "*.bin")) npy_files = glob.glob(os.path.join(self.lidar_dir, "*.npy")) @@ -399,20 +337,17 @@ def generate_lidar_summary(self): total_points = 0 total_size = 0 - # 采样统计,避免读取所有文件 - sample_files = bin_files[:5] if len(bin_files) > 5 else bin_files # 减少采样数量 + sample_files = bin_files[:5] if len(bin_files) > 5 else bin_files for bin_file in sample_files: if os.path.exists(bin_file): try: file_size = os.path.getsize(bin_file) total_size += file_size - # 估算点数 - points_in_file = file_size // (4 * 4) # 4个float32,每个4字节 + points_in_file = file_size // (4 * 4) total_points += points_in_file except: continue - # 根据采样估算总数 if sample_files and len(bin_files) > 0: avg_points_per_file = total_points / max(len(sample_files), 1) total_points_estimated = avg_points_per_file * len(bin_files) @@ -440,13 +375,12 @@ def generate_lidar_summary(self): with open(summary_path, 'w') as f: json.dump(summary, f, indent=2) except Exception as e: - print(f"保存LiDAR摘要失败: {e}") + pass return summary class MultiSensorFusion: - """多传感器融合(优化版)""" def __init__(self, output_dir, config=None): self.output_dir = output_dir @@ -454,8 +388,8 @@ def __init__(self, output_dir, config=None): os.makedirs(self.fusion_dir, exist_ok=True) self.calibration_data = {} - self.fusion_cache = {} # 融合缓存 - self.cache_size = config.get('fusion_cache_size', 50) if config else 50 # 减少缓存大小 + self.fusion_cache = {} + self.cache_size = config.get('fusion_cache_size', 50) if config else 50 self._load_calibration() @@ -463,48 +397,41 @@ def _load_calibration(self): calibration_dir = os.path.join(self.output_dir, "calibration") if os.path.exists(calibration_dir): - # 批量读取校准文件 calibration_files = [] for root, dirs, files in os.walk(calibration_dir): for file in files: if file.endswith('.json'): calibration_files.append(os.path.join(root, file)) - # 限制同时读取的文件数量 - for file in calibration_files[:10]: # 最多读取10个文件 + for file in calibration_files[:10]: try: with open(file, 'r') as f: data = json.load(f) sensor_name = os.path.basename(file).replace('.json', '') self.calibration_data[sensor_name] = data except Exception as e: - print(f"加载校准文件 {file} 失败: {e}") + pass def create_synchronization_file(self, frame_num, sensor_data): - """创建同步文件(优化版)""" - # 检查缓存 cache_key = f"{frame_num}_{hash(str(sensor_data))}" if cache_key in self.fusion_cache: return self.fusion_cache[cache_key] sync_data = { 'frame_id': frame_num, - 'timestamp': datetime.now().timestamp(), # 使用时间戳而不是ISO格式 + 'timestamp': datetime.now().timestamp(), 'sensors': {}, 'transformations': {} } - # 批量处理传感器数据 for sensor_type, data_path in sensor_data.items(): if data_path and os.path.exists(data_path): - # 获取文件信息(不加载完整文件) file_info = { 'file_path': os.path.basename(data_path), 'file_size': os.path.getsize(data_path), 'modified_time': os.path.getmtime(data_path) } - # 如果是图像,获取尺寸信息 if data_path.endswith(('.png', '.jpg', '.jpeg')): try: import cv2 @@ -516,7 +443,6 @@ def create_synchronization_file(self, frame_num, sensor_data): sync_data['sensors'][sensor_type] = file_info - # 添加变换信息(如果可用) if sensor_type in self.calibration_data: sync_data['transformations'][sensor_type] = { 'matrix': self.calibration_data[sensor_type].get('matrix', @@ -524,11 +450,9 @@ def create_synchronization_file(self, frame_num, sensor_data): [0, 0, 0, 1]]) } - # 压缩保存 sync_file = os.path.join(self.fusion_dir, f"sync_{frame_num:06d}.json") - # 使用压缩的JSON - if len(str(sync_data)) > 1000: # 如果数据较大,压缩保存 + if len(str(sync_data)) > 1000: compressed_data = zlib.compress( json.dumps(sync_data).encode('utf-8'), level=3 @@ -538,11 +462,9 @@ def create_synchronization_file(self, frame_num, sensor_data): f.write(compressed_data) else: with open(sync_file, 'w') as f: - json.dump(sync_data, f, separators=(',', ':')) # 紧凑格式 + json.dump(sync_data, f, separators=(',', ':')) - # 更新缓存 if len(self.fusion_cache) >= self.cache_size: - # 移除最旧的缓存项 oldest_key = next(iter(self.fusion_cache)) del self.fusion_cache[oldest_key] @@ -551,19 +473,15 @@ def create_synchronization_file(self, frame_num, sensor_data): return sync_file def generate_fusion_report(self): - """生成融合报告(优化版)""" if not os.path.exists(self.fusion_dir): return None - # 快速扫描文件 import glob sync_files = glob.glob(os.path.join(self.fusion_dir, "*.json*")) - # 解析文件名获取帧范围(避免读取所有文件) frame_ids = [] - for sync_file in sync_files[:20]: # 只检查前20个文件 + for sync_file in sync_files[:20]: try: - # 从文件名提取帧号 filename = os.path.basename(sync_file) if filename.startswith('sync_'): frame_id = int(filename.split('_')[1].split('.')[0])