diff --git a/src/enhance_pedestrian_safety/config_manager.py b/src/enhance_pedestrian_safety/config_manager.py index 183cb3cab3..1f64c72ef9 100644 --- a/src/enhance_pedestrian_safety/config_manager.py +++ b/src/enhance_pedestrian_safety/config_manager.py @@ -1,20 +1,298 @@ import json import os import argparse +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: + errors.append("场景时长必须大于0") + if 'town' not in scenario: + errors.append("场景配置中缺失地图名称") + + # 验证传感器配置 + if 'sensors' in config: + sensors = config['sensors'] + if 'capture_interval' in sensors and sensors['capture_interval'] <= 0: + errors.append("采集间隔必须大于0") + if 'image_size' in sensors: + if len(sensors['image_size']) != 2: + errors.append("图像尺寸必须为[宽度, 高度]格式") + 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: + errors.append("批处理大小必须大于0") + + return len(errors) == 0, errors + + @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: + suggestions.append(f"启用的输出类型过多({len(enabled_outputs)}),可能影响性能,建议只启用必要的输出") + + return suggestions + + +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({ + 'max_points_per_frame': 30000, + 'downsample_ratio': 0.4, + 'memory_warning_threshold': 200, + 'max_batch_memory_mb': 30 + }) + + # 调整性能设置 + perf = optimized.setdefault('performance', {}) + perf.update({ + 'batch_size': 3, + 'enable_compression': True, + 'compression_level': 4, + 'enable_memory_cache': True, + 'max_cache_size': 30, + 'frame_rate_limit': 3.0 + }) + + # 调整图像处理 + perf['image_processing'] = { + 'compress_images': True, + 'compression_quality': 80, + 'resize_images': False + } + + return optimized + + @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], + 'capture_interval': 1.0, + 'lidar_sensors': 1, + 'lidar_config': { + 'channels': 64, + 'range': 150.0, + 'points_per_second': 120000, + 'max_points_per_frame': 100000, + 'downsample_ratio': 0.1 + } + }) + + # 调整输出设置 + output = optimized['output'] + output.update({ + 'save_annotations': True, + 'save_fusion': True, + 'save_cooperative': True, + 'save_enhanced': True, + 'run_quality_check': True + }) + + # 调整增强设置 + enhanced = optimized.setdefault('enhancement', {}) + enhanced.update({ + 'enabled': True, + 'enable_random': True, + 'quality_check': True, + 'methods': ['normalize', 'contrast', 'sharpness', 'noise'] + }) + + return optimized + + @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 # 禁用雷达 + }) + + # 调整性能设置 + perf = optimized.setdefault('performance', {}) + perf.update({ + 'batch_size': 10, + 'enable_compression': True, + '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_annotations': False, + 'save_lidar': False, + 'save_fusion': False, + 'save_cooperative': False + }) + + return optimized class ConfigManager: + """配置管理器(增强版)""" + + # 预定义配置模板 + PRESET_CONFIGS = { + 'balanced': { + 'description': '平衡配置 - 兼顾性能和质量', + 'optimization': 'memory' + }, + 'high_quality': { + 'description': '高质量配置 - 优先数据质量', + 'optimization': 'quality' + }, + 'fast_collection': { + 'description': '快速采集配置 - 优先处理速度', + 'optimization': 'speed' + }, + 'v2x_focused': { + 'description': 'V2X重点配置 - 优化协同数据采集', + 'optimization': 'custom', + 'settings': { + 'v2x': {'enabled': True, 'update_interval': 1.0}, + 'cooperative': {'num_coop_vehicles': 3, 'enable_shared_perception': True}, + 'output': {'save_cooperative': True, 'save_v2x_messages': True} + } + }, + 'lidar_focused': { + 'description': 'LiDAR重点配置 - 优化点云数据采集', + 'optimization': 'custom', + 'settings': { + 'sensors': {'lidar_sensors': 2, 'lidar_config': {'channels': 64, 'range': 200}}, + 'output': {'save_lidar': True, 'save_fusion': True} + } + } + } + + @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("配置验证错误:") + for error in errors: + print(f" - {error}") + raise ValueError("配置验证失败") + + # 提供优化建议 + suggestions = ConfigValidator.suggest_optimizations(config) + if suggestions: + print("配置优化建议:") + for suggestion in suggestions: + print(f" ⚡ {suggestion}") + + return config @staticmethod - def load_config(config_file=None): - config = { + def _get_default_config() -> Dict[str, Any]: + """获取默认配置(增强版)""" + return { 'scenario': { 'name': 'multi_sensor_scene', + 'description': '多传感器协同数据采集场景', 'town': 'Town10HD', 'weather': 'clear', 'time_of_day': 'noon', 'duration': 60, - 'seed': 42 + 'seed': 42, + 'timeout': 300, # 超时时间(秒) + 'retry_attempts': 3 # 重试次数 }, 'traffic': { 'ego_vehicles': 1, @@ -22,25 +300,49 @@ def load_config(config_file=None): 'pedestrians': 6, 'traffic_lights': True, 'batch_spawn': True, - 'max_spawn_attempts': 5 + 'max_spawn_attempts': 5, + 'vehicle_types': [ + 'vehicle.tesla.model3', + 'vehicle.audi.tt', + 'vehicle.nissan.patrol', + 'vehicle.bmw.grandtourer' + ], + 'pedestrian_types': [ + 'walker.pedestrian.0001', + 'walker.pedestrian.0002' + ] }, 'sensors': { 'vehicle_cameras': 4, 'infrastructure_cameras': 4, 'lidar_sensors': 1, 'radar_sensors': 0, + 'gps_sensors': 0, + 'imu_sensors': 0, 'image_size': [1280, 720], 'capture_interval': 2.0, + 'sensor_placement': 'default', 'lidar_config': { 'channels': 32, - 'range': 100, + 'range': 100.0, 'points_per_second': 56000, - 'rotation_frequency': 10, - 'max_points_per_frame': 50000, # 优化点6:减少最大点数 - 'downsample_ratio': 0.3, # 优化点6:增加下采样比例 - 'memory_warning_threshold': 350, # 新增:内存警告阈值(MB) - 'max_batch_memory_mb': 50, # 新增:批次最大内存 - 'v2x_save_interval': 5 # 新增:V2X格式保存间隔 + 'rotation_frequency': 10.0, + 'horizontal_fov': 360.0, + 'vertical_fov': 30.0, + 'upper_fov': 10.0, + 'lower_fov': -20.0, + 'max_points_per_frame': 50000, + 'downsample_ratio': 0.3, + 'memory_warning_threshold': 300, + 'max_batch_memory_mb': 50, + 'v2x_save_interval': 5, + 'compression_format': 'bin' # bin, npy, pcd + }, + 'camera_config': { + 'fov': 90.0, + 'post_processing': 'default', + 'exposure_mode': 'auto', + 'motion_blur': 0.0 } }, 'v2x': { @@ -50,8 +352,11 @@ def load_config(config_file=None): 'latency_mean': 0.05, 'latency_std': 0.01, 'packet_loss_rate': 0.01, - 'message_types': ['bsm', 'spat', 'map', 'rsm'], - 'update_interval': 2.0 # 优化点6:V2X更新间隔 + 'message_types': ['bsm', 'spat', 'map', 'rsm', 'perception', 'warning'], + 'update_interval': 2.0, + 'security_enabled': False, + 'encryption_level': 'none', + 'qos_policy': 'best_effort' }, 'cooperative': { 'num_coop_vehicles': 2, @@ -59,7 +364,10 @@ def load_config(config_file=None): 'enable_traffic_warnings': True, 'enable_maneuver_coordination': False, 'data_fusion_interval': 1.0, - 'max_shared_objects': 50 + 'max_shared_objects': 50, + 'object_matching_threshold': 5.0, + 'data_retention_time': 10.0, + 'consensus_method': 'simple' }, 'enhancement': { 'enabled': True, @@ -68,7 +376,10 @@ def load_config(config_file=None): 'save_original': True, 'save_enhanced': True, 'calibration_generation': True, - 'enhanced_dir_name': 'enhanced' + 'enhanced_dir_name': 'enhanced', + 'methods': ['normalize', 'contrast', 'brightness'], + 'weather_effects': True, + 'augmentation_level': 'medium' }, 'performance': { 'batch_size': 5, @@ -77,27 +388,41 @@ def load_config(config_file=None): 'enable_downsampling': True, 'enable_memory_cache': True, 'max_cache_size': 50, + 'enable_async_processing': True, + 'max_workers': 2, 'image_processing': { 'compress_images': True, - 'compression_quality': 85 + 'compression_quality': 85, + 'resize_images': False, + 'resize_dimensions': [640, 480], + 'format': 'jpg' # jpg, png }, 'lidar_processing': { 'batch_size': 10, 'enable_compression': True, 'enable_downsampling': True, - 'max_points_per_frame': 50000, # 与sensors配置保持一致 + 'max_points_per_frame': 50000, 'memory_warning_threshold': 350, 'max_batch_memory_mb': 50, - 'v2x_save_interval': 5 + 'v2x_save_interval': 5, + 'compression_method': 'zlib' # zlib, lz4, none }, 'fusion': { - 'fusion_cache_size': 100 + 'fusion_cache_size': 100, + 'enable_cache': True, + 'compression_enabled': True }, - 'sensor_cleanup_timeout': 0.5, # 新增:传感器清理超时 - 'frame_rate_limit': 5.0 # 新增:帧率限制 + 'sensor_cleanup_timeout': 0.5, + 'frame_rate_limit': 5.0, + 'memory_management': { + 'gc_interval': 50, + 'max_memory_mb': 500, + 'early_stop_threshold': 400 + } }, 'output': { 'data_dir': 'cvips_dataset', + 'output_format': 'standard', # standard, v2xformer, kitti, coco 'save_raw': True, 'save_stitched': True, 'save_annotations': False, @@ -109,102 +434,285 @@ def load_config(config_file=None): 'validate_data': True, 'run_analysis': False, 'run_quality_check': True, - 'output_format': 'standard' + 'generate_summary': True, + 'compression_enabled': True, + 'file_naming': 'sequential', # sequential, timestamp + 'backup_original': False + }, + 'monitoring': { + 'enable_logging': True, + 'log_level': 'INFO', # DEBUG, INFO, WARNING, ERROR + 'log_file': 'cvips.log', + 'enable_performance_monitor': True, + 'performance_log_interval': 10.0, + 'enable_progress_bar': True, + 'enable_real_time_stats': True, + 'stats_update_interval': 5.0 + }, + 'debug': { + 'enable_debug_mode': False, + 'save_debug_data': False, + 'debug_dir': 'debug', + 'print_config': False, + 'validate_sensors': True, + 'test_mode': False + }, + 'metadata': { + 'version': '1.0.0', + 'author': 'CVIPS System', + 'description': '多传感器协同数据采集配置', + 'created': '', + 'modified': '' } } - if config_file and os.path.exists(config_file): - try: - with open(config_file, 'r') as f: - user_config = json.load(f) - ConfigManager._deep_update(config, user_config) - except: - pass + @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 + + 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) + elif optimization == 'quality': + config = ConfigOptimizer.optimize_for_quality(config) + elif optimization == 'speed': + config = ConfigOptimizer.optimize_for_speed(config) + elif optimization == 'custom' and 'settings' in preset: + config = ConfigManager._deep_update(config, preset['settings']) return config @staticmethod - def _deep_update(original, update): + 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: + user_config = yaml.safe_load(f) + else: + user_config = json.load(f) + + print(f"加载配置文件: {config_file}") + return ConfigManager._deep_update(base_config, user_config) + + except Exception as e: + print(f"配置文件加载错误: {e}") + return base_config + + @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) else: original[key] = value + return original @staticmethod - def merge_args(config, args): - if args.scenario: + 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 args.town: + if hasattr(args, 'town') and args.town: config['scenario']['town'] = args.town - if args.weather: + if hasattr(args, 'weather') and args.weather: config['scenario']['weather'] = args.weather - if args.time_of_day: + if hasattr(args, 'time_of_day') and args.time_of_day: config['scenario']['time_of_day'] = args.time_of_day - if args.duration: + if hasattr(args, 'duration') and args.duration: config['scenario']['duration'] = args.duration - if args.seed: + if hasattr(args, 'seed') and args.seed: config['scenario']['seed'] = args.seed - if args.num_vehicles: + # 交通参数 + if hasattr(args, 'num_vehicles') and args.num_vehicles: config['traffic']['background_vehicles'] = args.num_vehicles - if args.num_pedestrians: + 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 - if hasattr(args, 'enable_v2x') and args.enable_v2x: - config['v2x']['enabled'] = True - # 如果通过命令行启用V2X,使用更保守的更新间隔 - if 'update_interval' not in config['v2x']: - config['v2x']['update_interval'] = 2.0 + # V2X参数 + if hasattr(args, 'enable_v2x'): + config['v2x']['enabled'] = args.enable_v2x - if hasattr(args, 'enable_enhancement') and args.enable_enhancement: - config['enhancement']['enabled'] = True + # 增强参数 + if hasattr(args, 'enable_enhancement'): + config['enhancement']['enabled'] = args.enable_enhancement - if hasattr(args, 'enable_lidar') and args.enable_lidar: - config['sensors']['lidar_sensors'] = 1 - config['output']['save_lidar'] = True - # 优化:如果启用LiDAR,使用更保守的设置 - config['sensors']['lidar_config']['max_points_per_frame'] = 50000 - config['sensors']['lidar_config']['downsample_ratio'] = 0.3 + # 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') and args.enable_fusion: - config['output']['save_fusion'] = True + # 融合参数 + if hasattr(args, 'enable_fusion'): + config['output']['save_fusion'] = args.enable_fusion - if hasattr(args, 'enable_cooperative') and args.enable_cooperative: - config['output']['save_cooperative'] = True + # 协同参数 + if hasattr(args, 'enable_cooperative'): + config['output']['save_cooperative'] = args.enable_cooperative - if hasattr(args, 'enable_annotations') and args.enable_annotations: - config['output']['save_annotations'] = True + # 标注参数 + if hasattr(args, 'enable_annotations'): + config['output']['save_annotations'] = args.enable_annotations - if hasattr(args, 'skip_validation') and args.skip_validation: - config['output']['validate_data'] = False + # 验证参数 + if hasattr(args, 'skip_validation'): + config['output']['validate_data'] = not args.skip_validation - if hasattr(args, 'skip_quality_check') and args.skip_quality_check: - config['output']['run_quality_check'] = False + # 质量检查参数 + if hasattr(args, 'skip_quality_check'): + config['output']['run_quality_check'] = not args.skip_quality_check - if hasattr(args, 'run_analysis') and args.run_analysis: - config['output']['run_analysis'] = True + # 分析参数 + 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 - if hasattr(args, 'enable_compression') and args.enable_compression: - config['performance']['enable_compression'] = True + if hasattr(args, 'enable_compression'): + config['performance']['enable_compression'] = args.enable_compression - if hasattr(args, 'enable_downsampling') and args.enable_downsampling: - config['performance']['enable_downsampling'] = True - # 如果启用下采样,使用更保守的设置 - config['sensors']['lidar_config']['downsample_ratio'] = 0.3 - config['performance']['lidar_processing']['max_points_per_frame'] = 50000 + if hasattr(args, 'enable_downsampling'): + config['performance']['enable_downsampling'] = args.enable_downsampling + 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 - return config \ No newline at end of file + return config + + @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) + + if format.lower() == 'yaml' and YAML_AVAILABLE: + with open(output_path, 'w', encoding='utf-8') as f: + yaml.dump(config, f, default_flow_style=False, allow_unicode=True) + else: + with open(output_path, 'w', encoding='utf-8') as f: + json.dump(config, f, indent=2, ensure_ascii=False) + + print(f"配置保存到: {output_path}") + return True + except Exception as e: + print(f"保存配置失败: {e}") + return False + + @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 "通用"}' + + return ConfigManager.save_config(config, output_path) + + @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']}") + print(f" 地图: {scenario['town']}") + print(f" 天气/时间: {scenario['weather']}/{scenario['time_of_day']}") + print(f" 时长: {scenario['duration']}秒") + print(f" 随机种子: {scenario.get('seed', '随机')}") + + # 交通信息 + traffic = config['traffic'] + print(f"\n🚗 交通:") + print(f" 主车: {traffic['ego_vehicles']}") + print(f" 背景车辆: {traffic['background_vehicles']}") + print(f" 行人: {traffic['pedestrians']}") + print(f" 交通灯: {'启用' if traffic['traffic_lights'] else '禁用'}") + + # 传感器信息 + sensors = config['sensors'] + print(f"\n📷 传感器:") + print(f" 车辆摄像头: {sensors['vehicle_cameras']}") + print(f" 基础设施摄像头: {sensors['infrastructure_cameras']}") + print(f" LiDAR: {sensors['lidar_sensors']} (通道: {sensors['lidar_config']['channels']})") + 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 '禁用'}") + if v2x['enabled']: + 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']}") + print(f" 压缩: {'启用' if perf['enable_compression'] else '禁用'}") + 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']}") + print(f" 输出格式: {output['output_format']}") + enabled_outputs = [k.replace('save_', '') for k, v in output.items() + if isinstance(v, bool) and v and k.startswith('save_')] + print(f" 启用输出: {', '.join(enabled_outputs)}") + + print("=" * 60) + + @staticmethod + def list_presets(): + """列出所有预设配置""" + print("\n可用预设配置:") + print("-" * 40) + for name, preset in ConfigManager.PRESET_CONFIGS.items(): + print(f" {name:15s} - {preset['description']}") + 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/sensor_enhancer.py b/src/enhance_pedestrian_safety/sensor_enhancer.py index f484831c94..b446c71bbe 100644 --- a/src/enhance_pedestrian_safety/sensor_enhancer.py +++ b/src/enhance_pedestrian_safety/sensor_enhancer.py @@ -1,5 +1,5 @@ """ -传感器数据增强模块 - 提高数据质量和多样性 +传感器数据增强模块 - 提高数据质量和多样性(优化版) """ import numpy as np @@ -9,421 +9,683 @@ import json from PIL import Image, ImageEnhance, ImageFilter from datetime import datetime -from typing import Dict, List, Tuple, Optional -import carla +from typing import Dict, List, Tuple, Optional, Union, Callable +import concurrent.futures +from dataclasses import dataclass +from enum import Enum +import time +import hashlib +from pathlib import Path + + +class WeatherType(Enum): + """天气类型枚举""" + CLEAR = "clear" + RAINY = "rainy" + FOGGY = "foggy" + CLOUDY = "cloudy" + NIGHT = "night" + SUNSET = "sunset" + + +class EnhancementMethod(Enum): + """增强方法枚举""" + NORMALIZE = "normalize" + BRIGHTNESS = "brightness" + CONTRAST = "contrast" + SATURATION = "saturation" + SHARPNESS = "sharpness" + GAMMA = "gamma" + NOISE = "noise" + BLUR = "blur" + MOTION_BLUR = "motion_blur" + RAIN = "rain" + FOG = "fog" + CLOUD = "cloud" + VIGNETTE = "vignette" + COLOR_TEMP = "color_temperature" + JPEG_COMPRESSION = "jpeg_compression" + COLOR_JITTER = "color_jitter" + + +@dataclass +class EnhancementConfig: + """增强配置""" + weather: WeatherType = WeatherType.CLEAR + time_of_day: str = "noon" + enabled_methods: List[EnhancementMethod] = None + intensity_range: Tuple[float, float] = (0.5, 1.5) + probability: float = 0.7 + max_methods_per_image: int = 5 + save_original: bool = True + save_enhanced: bool = True + output_format: str = "jpg" + compression_quality: int = 90 + + def __post_init__(self): + if self.enabled_methods is None: + self.enabled_methods = [ + EnhancementMethod.NORMALIZE, + EnhancementMethod.CONTRAST, + EnhancementMethod.BRIGHTNESS + ] + + +class BatchEnhancer: + """批量增强器""" + + def __init__(self, config: EnhancementConfig, max_workers: int = 4): + self.config = config + self.max_workers = max_workers + self.enhancer = SensorDataEnhancer(config) + self.stats = { + 'total_processed': 0, + 'successful': 0, + 'failed': 0, + 'total_time': 0, + 'avg_time_per_image': 0 + } + + def process_batch(self, image_paths: List[str], output_dir: str) -> Dict: + """批量处理图像""" + start_time = time.time() + results = [] + + # 准备输出目录 + os.makedirs(output_dir, exist_ok=True) + + # 使用线程池并行处理 + with concurrent.futures.ThreadPoolExecutor(max_workers=self.max_workers) as executor: + future_to_path = {} + for img_path in image_paths: + if os.path.exists(img_path): + output_path = self._get_output_path(img_path, output_dir) + future = executor.submit( + self._process_single_image, + img_path, output_path + ) + future_to_path[future] = img_path + + # 收集结果 + for future in concurrent.futures.as_completed(future_to_path): + img_path = future_to_path[future] + try: + result = future.result() + results.append(result) + self.stats['successful'] += 1 + except Exception as e: + print(f"处理图像 {img_path} 失败: {e}") + self.stats['failed'] += 1 + results.append({ + 'input_path': img_path, + 'output_path': None, + 'success': False, + 'error': str(e) + }) + + # 更新统计 + self.stats['total_processed'] += len(image_paths) + total_time = time.time() - start_time + self.stats['total_time'] += total_time + + if len(image_paths) > 0: + self.stats['avg_time_per_image'] = self.stats['total_time'] / self.stats['total_processed'] + + return { + 'results': results, + 'stats': self.stats.copy(), + 'batch_size': len(image_paths), + 'processing_time': total_time + } + + def _process_single_image(self, input_path: str, output_path: str) -> Dict: + """处理单张图像""" + try: + # 读取图像 + image = cv2.imread(input_path) + if image is None: + raise ValueError(f"无法读取图像: {input_path}") + + # 转换为RGB + image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) + + # 应用增强 + enhanced = self.enhancer.enhance_image(image_rgb) + + # 保存结果 + cv2.imwrite(output_path, cv2.cvtColor(enhanced, cv2.COLOR_RGB2BGR)) + + # 计算哈希值(用于去重) + img_hash = hashlib.md5(enhanced.tobytes()).hexdigest()[:16] + + return { + 'input_path': input_path, + 'output_path': output_path, + 'success': True, + 'image_hash': img_hash, + 'original_size': os.path.getsize(input_path), + 'enhanced_size': os.path.getsize(output_path), + 'compression_ratio': os.path.getsize(output_path) / max(1, os.path.getsize(input_path)) + } + except Exception as e: + raise Exception(f"处理失败: {e}") + + def _get_output_path(self, input_path: str, output_dir: str) -> str: + """生成输出路径""" + filename = os.path.basename(input_path) + name, ext = os.path.splitext(filename) + + # 根据配置选择输出格式 + if self.config.output_format == "jpg": + new_ext = ".jpg" + elif self.config.output_format == "png": + new_ext = ".png" + else: + new_ext = ext + + # 添加增强标记 + enhanced_name = f"{name}_enhanced{new_ext}" + return os.path.join(output_dir, enhanced_name) + + def get_stats(self) -> Dict: + """获取统计信息""" + return self.stats.copy() class SensorDataEnhancer: - """传感器数据增强器""" + """传感器数据增强器(优化版)""" - def __init__(self, config: Dict): - self.config = config - self.enhancement_methods = [] - self._setup_enhancement_methods() - - def _setup_enhancement_methods(self): - """设置增强方法""" - # 根据场景配置启用不同的增强方法 - weather = self.config.get('scenario', {}).get('weather', 'clear') - time_of_day = self.config.get('scenario', {}).get('time_of_day', 'noon') - - # 基础增强方法 - self.enhancement_methods = ['normalize'] - - # 根据天气和时间添加特定增强 - if weather == 'rainy': - self.enhancement_methods.extend(['rain_effect', 'motion_blur', 'brightness_adjust']) - elif weather == 'foggy': - self.enhancement_methods.extend(['fog_effect', 'contrast_reduce']) - elif weather == 'night': - self.enhancement_methods.extend(['night_effect', 'noise_add', 'gamma_correction']) - elif weather == 'cloudy': - self.enhancement_methods.extend(['cloud_effect', 'color_temperature']) - - # 随机增强(可选) - if self.config.get('enhancement', {}).get('enable_random', True): - self.enhancement_methods.extend(self._get_random_enhancements()) - - def _get_random_enhancements(self) -> List[str]: - """获取随机增强方法""" - random_methods = [ - 'hue_shift', 'saturation_adjust', 'sharpness_enhance', - 'gaussian_blur', 'jpeg_compression', 'color_jitter' - ] - # 随机选择1-3个增强方法 - num_methods = random.randint(1, 3) - return random.sample(random_methods, num_methods) + def __init__(self, config: Union[EnhancementConfig, Dict]): + if isinstance(config, dict): + self.config = EnhancementConfig(**config) + else: + self.config = config + + self.method_registry = self._setup_method_registry() + self.weather_methods = self._setup_weather_methods() + self.method_cache = {} + self.perf_stats = { + 'calls': 0, + 'total_time': 0, + 'method_times': {} + } + + def _setup_method_registry(self) -> Dict[EnhancementMethod, Callable]: + """设置方法注册表""" + return { + EnhancementMethod.NORMALIZE: self._normalize_image, + EnhancementMethod.BRIGHTNESS: self._adjust_brightness, + EnhancementMethod.CONTRAST: self._adjust_contrast, + EnhancementMethod.SATURATION: self._adjust_saturation, + EnhancementMethod.SHARPNESS: self._enhance_sharpness, + EnhancementMethod.GAMMA: self._gamma_correction, + EnhancementMethod.NOISE: self._add_noise, + EnhancementMethod.BLUR: self._apply_gaussian_blur, + EnhancementMethod.MOTION_BLUR: self._apply_motion_blur, + EnhancementMethod.RAIN: self._add_rain_effect, + EnhancementMethod.FOG: self._add_fog_effect, + EnhancementMethod.CLOUD: self._add_cloud_effect, + EnhancementMethod.VIGNETTE: self._add_vignette, + EnhancementMethod.COLOR_TEMP: self._adjust_color_temperature, + EnhancementMethod.JPEG_COMPRESSION: self._simulate_jpeg_compression, + EnhancementMethod.COLOR_JITTER: self._color_jitter + } + + def _setup_weather_methods(self) -> Dict[WeatherType, List[EnhancementMethod]]: + """设置天气相关方法""" + return { + WeatherType.CLEAR: [ + EnhancementMethod.NORMALIZE, + EnhancementMethod.CONTRAST, + EnhancementMethod.SHARPNESS + ], + WeatherType.RAINY: [ + EnhancementMethod.NORMALIZE, + EnhancementMethod.RAIN, + EnhancementMethod.CONTRAST, + EnhancementMethod.MOTION_BLUR + ], + WeatherType.FOGGY: [ + EnhancementMethod.NORMALIZE, + EnhancementMethod.FOG, + EnhancementMethod.CONTRAST, + EnhancementMethod.BLUR + ], + WeatherType.CLOUDY: [ + EnhancementMethod.NORMALIZE, + EnhancementMethod.CLOUD, + EnhancementMethod.COLOR_TEMP, + EnhancementMethod.CONTRAST + ], + WeatherType.NIGHT: [ + EnhancementMethod.NORMALIZE, + EnhancementMethod.BRIGHTNESS, + EnhancementMethod.CONTRAST, + EnhancementMethod.NOISE, + EnhancementMethod.VIGNETTE + ], + WeatherType.SUNSET: [ + EnhancementMethod.NORMALIZE, + EnhancementMethod.COLOR_TEMP, + EnhancementMethod.CONTRAST, + EnhancementMethod.VIGNETTE + ] + } - def enhance_image(self, image_data: np.ndarray, sensor_type: str = 'camera') -> np.ndarray: + def enhance_image(self, image_data: np.ndarray, + sensor_type: str = 'camera', + return_methods: bool = False) -> Union[np.ndarray, Tuple]: """ - 增强图像数据 + 增强图像数据(优化版) Args: image_data: 原始图像数据 (H, W, C) sensor_type: 传感器类型 ('camera', 'depth', 'semantic') + return_methods: 是否返回使用的增强方法列表 Returns: - 增强后的图像数据 + 增强后的图像数据(如果return_methods为True,则返回元组) """ + start_time = time.time() + if sensor_type != 'camera': # 深度和语义分割图像使用不同的增强 - return self._enhance_non_rgb_image(image_data, sensor_type) - - # 转换为PIL图像进行处理 - pil_image = Image.fromarray(image_data) - - # 按顺序应用增强方法 - for method in self.enhancement_methods: - pil_image = self._apply_enhancement_method(pil_image, method) + enhanced = self._enhance_non_rgb_image(image_data, sensor_type) + self._update_perf_stats('non_rgb', time.time() - start_time) - # 确保图像在有效范围内 - enhanced_image = np.array(pil_image) - enhanced_image = np.clip(enhanced_image, 0, 255).astype(np.uint8) - - return enhanced_image - - def _apply_enhancement_method(self, image: Image.Image, method: str) -> Image.Image: - """应用单个增强方法""" - try: - if method == 'normalize': - return self._normalize_image(image) - elif method == 'rain_effect': - return self._add_rain_effect(image) - elif method == 'fog_effect': - return self._add_fog_effect(image) - elif method == 'night_effect': - return self._apply_night_effect(image) - elif method == 'motion_blur': - return self._apply_motion_blur(image) - elif method == 'brightness_adjust': - return self._adjust_brightness(image) - elif method == 'contrast_reduce': - return self._reduce_contrast(image) - elif method == 'noise_add': - return self._add_noise(image) - elif method == 'gamma_correction': - return self._gamma_correction(image) - elif method == 'cloud_effect': - return self._add_cloud_effect(image) - elif method == 'color_temperature': - return self._adjust_color_temperature(image) - elif method == 'hue_shift': - return self._shift_hue(image) - elif method == 'saturation_adjust': - return self._adjust_saturation(image) - elif method == 'sharpness_enhance': - return self._enhance_sharpness(image) - elif method == 'gaussian_blur': - return self._apply_gaussian_blur(image) - elif method == 'jpeg_compression': - return self._simulate_jpeg_compression(image) - elif method == 'color_jitter': - return self._color_jitter(image) - else: - return image - except Exception as e: - print(f"增强方法 {method} 失败: {e}") - return image - - def _enhance_non_rgb_image(self, image_data: np.ndarray, sensor_type: str) -> np.ndarray: - """增强非RGB图像(深度、语义分割)""" - if sensor_type == 'depth': - # 深度图增强:归一化和噪声去除 - normalized = cv2.normalize(image_data, None, 0, 255, cv2.NORM_MINMAX) - # 应用轻微的高斯模糊去除噪声 - enhanced = cv2.GaussianBlur(normalized, (3, 3), 0) - return enhanced.astype(np.uint8) - - elif sensor_type == 'semantic': - # 语义分割图增强:保持类别不变,只做边界平滑 - kernel = np.ones((3, 3), np.uint8) - # 形态学开运算去除小噪声 - enhanced = cv2.morphologyEx(image_data, cv2.MORPH_OPEN, kernel) + if return_methods: + return enhanced, ['non_rgb_enhance'] return enhanced - return image_data - - def _normalize_image(self, image: Image.Image) -> Image.Image: - """图像归一化""" - # 转换为numpy数组 - img_array = np.array(image) - - # 归一化到0-255 - if img_array.dtype != np.uint8: - img_array = cv2.normalize(img_array, None, 0, 255, cv2.NORM_MINMAX) - img_array = img_array.astype(np.uint8) - - return Image.fromarray(img_array) - - def _add_rain_effect(self, image: Image.Image) -> Image.Image: - """添加雨滴效果""" - img_array = np.array(image) - h, w, _ = img_array.shape - - # 创建雨滴层 - rain_layer = np.zeros((h, w), dtype=np.float32) + # 获取增强方法 + methods = self._get_enhancement_methods() - # 生成随机雨滴位置 - num_drops = random.randint(100, 500) - for _ in range(num_drops): - x = random.randint(0, w - 1) - y = random.randint(0, h - 1) - length = random.randint(5, 20) - thickness = random.randint(1, 2) - brightness = random.uniform(0.7, 0.9) + # 检查缓存 + cache_key = self._get_cache_key(image_data, methods) + if cache_key in self.method_cache: + self._update_perf_stats('cache_hit', time.time() - start_time) - # 绘制雨滴(斜线) - for i in range(length): - if y + i < h and x + i < w: - rain_layer[y + i, x + i] += brightness - # 加粗雨滴 - for j in range(thickness): - if x + i + j < w: - rain_layer[y + i, x + i + j] += brightness * 0.5 + if return_methods: + cached_result, cached_methods = self.method_cache[cache_key] + return cached_result.copy(), cached_methods + return self.method_cache[cache_key][0].copy() - # 模糊雨滴层 - rain_layer = cv2.GaussianBlur(rain_layer, (5, 5), 0) + # 应用增强方法 + enhanced = image_data.copy() + applied_methods = [] - # 叠加到原图 - rain_layer_3d = np.stack([rain_layer] * 3, axis=2) - enhanced = cv2.addWeighted(img_array.astype(np.float32), 0.8, - rain_layer_3d * 255, 0.2, 0) + for method in methods: + try: + method_start = time.time() + enhanced = self.method_registry[method](enhanced) + method_time = time.time() - method_start - return Image.fromarray(enhanced.astype(np.uint8)) + self._update_perf_stats(method.value, method_time) + applied_methods.append(method.value) + except Exception as e: + print(f"增强方法 {method.value} 失败: {e}") + continue - def _add_fog_effect(self, image: Image.Image) -> Image.Image: - """添加雾效""" - img_array = np.array(image) - h, w, _ = img_array.shape + # 确保图像在有效范围内 + enhanced = np.clip(enhanced, 0, 255).astype(np.uint8) + + # 更新缓存 + self.method_cache[cache_key] = (enhanced.copy(), applied_methods) + + # 清理缓存(如果太大) + if len(self.method_cache) > 100: + self._cleanup_cache() + + total_time = time.time() - start_time + self._update_perf_stats('total', total_time) + + if return_methods: + return enhanced, applied_methods + return enhanced + + def _get_enhancement_methods(self) -> List[EnhancementMethod]: + """获取增强方法列表""" + # 基础方法 + methods = list(self.config.enabled_methods) + + # 添加天气相关方法 + weather_methods = self.weather_methods.get(self.config.weather, []) + for method in weather_methods: + if method not in methods and random.random() < self.config.probability: + methods.append(method) + + # 随机添加额外方法 + if random.random() < 0.3: # 30%概率添加额外方法 + available_methods = list(EnhancementMethod) + extra_methods = random.sample( + [m for m in available_methods if m not in methods], + k=random.randint(1, 2) + ) + methods.extend(extra_methods) + + # 限制方法数量 + if len(methods) > self.config.max_methods_per_image: + methods = random.sample(methods, self.config.max_methods_per_image) + + # 随机排序 + random.shuffle(methods) + + return methods + + def _get_cache_key(self, image_data: np.ndarray, + methods: List[EnhancementMethod]) -> str: + """生成缓存键""" + # 使用图像哈希和方法列表生成键 + img_hash = hashlib.md5(image_data.tobytes()).hexdigest()[:16] + method_str = ','.join(sorted([m.value for m in methods])) + config_str = f"{self.config.weather.value}_{self.config.time_of_day}" + + return f"{img_hash}_{method_str}_{config_str}" + + def _cleanup_cache(self): + """清理缓存""" + # 保留最近使用的50个条目 + if len(self.method_cache) > 50: + keys_to_remove = list(self.method_cache.keys())[:-50] + for key in keys_to_remove: + del self.method_cache[key] + + def _update_perf_stats(self, method: str, duration: float): + """更新性能统计""" + self.perf_stats['calls'] += 1 + self.perf_stats['total_time'] += duration + self.perf_stats['method_times'][method] = \ + self.perf_stats['method_times'].get(method, 0) + duration + + def get_performance_stats(self) -> Dict: + """获取性能统计""" + stats = self.perf_stats.copy() + if stats['calls'] > 0: + stats['avg_time_per_call'] = stats['total_time'] / stats['calls'] + return stats + + # ========== 增强方法实现(优化版)========== + + def _normalize_image(self, image: np.ndarray) -> np.ndarray: + """图像归一化(优化版)""" + # 使用OpenCV加速 + if image.dtype != np.uint8: + normalized = cv2.normalize(image, None, 0, 255, cv2.NORM_MINMAX) + return normalized.astype(np.uint8) + return image - # 创建雾效层 - fog_intensity = random.uniform(0.3, 0.6) - fog_color = random.choice([200, 210, 220]) # 雾的颜色 + def _adjust_brightness(self, image: np.ndarray) -> np.ndarray: + """调整亮度(优化版)""" + factor = random.uniform(*self.config.intensity_range) - fog_layer = np.ones((h, w, 3), dtype=np.float32) * fog_color + # 使用NumPy向量化操作 + if factor != 1.0: + # 转换为浮点数进行计算 + img_float = image.astype(np.float32) * factor + return np.clip(img_float, 0, 255).astype(np.uint8) + return image - # 根据深度添加雾效(这里简化处理,实际应该使用深度图) - # 创建简单的深度渐变(假设图像中心最近) - center_y, center_x = h // 2, w // 2 - y_coords, x_coords = np.ogrid[:h, :w] - distance = np.sqrt((x_coords - center_x) ** 2 + (y_coords - center_y) ** 2) - distance = distance / np.max(distance) # 归一化 + def _adjust_contrast(self, image: np.ndarray) -> np.ndarray: + """调整对比度(优化版)""" + factor = random.uniform(*self.config.intensity_range) - # 雾效随距离增强 - fog_strength = distance * fog_intensity - fog_strength_3d = np.stack([fog_strength] * 3, axis=2) + if factor != 1.0: + # 计算平均值 + mean = np.mean(image, axis=(0, 1), keepdims=True) + # 应用对比度调整 + contrasted = mean + factor * (image.astype(np.float32) - mean) + return np.clip(contrasted, 0, 255).astype(np.uint8) + return image - # 混合原图和雾效 - enhanced = img_array.astype(np.float32) * (1 - fog_strength_3d) + \ - fog_layer * fog_strength_3d + def _adjust_saturation(self, image: np.ndarray) -> np.ndarray: + """调整饱和度(优化版)""" + factor = random.uniform(*self.config.intensity_range) + + if factor != 1.0: + # 转换为HSV空间 + hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV).astype(np.float32) + # 调整饱和度通道 + hsv[:, :, 1] = np.clip(hsv[:, :, 1] * factor, 0, 255) + # 转换回RGB + saturated = cv2.cvtColor(hsv.astype(np.uint8), cv2.COLOR_HSV2RGB) + return saturated + return image - return Image.fromarray(enhanced.astype(np.uint8)) + def _enhance_sharpness(self, image: np.ndarray) -> np.ndarray: + """增强锐度(优化版)""" + # 使用拉普拉斯算子增强边缘 + kernel = np.array([[0, -1, 0], + [-1, 5, -1], + [0, -1, 0]]) + sharpened = cv2.filter2D(image, -1, kernel) + return np.clip(sharpened, 0, 255).astype(np.uint8) + + def _gamma_correction(self, image: np.ndarray) -> np.ndarray: + """伽马校正(优化版)""" + gamma = random.uniform(0.5, 2.0) + + # 使用LUT加速 + table = np.array([((i / 255.0) ** gamma) * 255 for i in range(256)]).astype(np.uint8) + corrected = cv2.LUT(image, table) + return corrected + + def _add_noise(self, image: np.ndarray) -> np.ndarray: + """添加噪声(优化版)""" + noise_type = random.choice(['gaussian', 'salt_pepper']) - def _apply_night_effect(self, image: Image.Image) -> Image.Image: - """应用夜间效果""" - # 降低亮度 - enhancer = ImageEnhance.Brightness(image) - image = enhancer.enhance(random.uniform(0.3, 0.6)) + if noise_type == 'gaussian': + # 高斯噪声 + mean = 0 + var = random.uniform(0.001, 0.005) + sigma = var ** 0.5 + gauss = np.random.normal(mean, sigma, image.shape) * 255 + noisy = image.astype(np.float32) + gauss + return np.clip(noisy, 0, 255).astype(np.uint8) - # 降低对比度 - enhancer = ImageEnhance.Contrast(image) - image = enhancer.enhance(random.uniform(0.7, 0.9)) + else: # salt_pepper + # 椒盐噪声 + amount = random.uniform(0.001, 0.005) + s_vs_p = random.uniform(0.3, 0.7) - # 添加暗角效果 - img_array = np.array(image) - h, w, _ = img_array.shape + noisy = image.copy() - # 创建暗角蒙版 - center_y, center_x = h // 2, w // 2 - y_coords, x_coords = np.ogrid[:h, :w] - distance = np.sqrt((x_coords - center_x) ** 2 + (y_coords - center_y) ** 2) - distance = distance / np.max(distance) + # 盐噪声 + num_salt = np.ceil(amount * image.size * s_vs_p / 3) + coords = [np.random.randint(0, i, int(num_salt)) for i in image.shape] + noisy[coords[0], coords[1], :] = 255 - vignette = 1 - distance * 0.3 # 暗角强度 - vignette_3d = np.stack([vignette] * 3, axis=2) + # 椒噪声 + num_pepper = np.ceil(amount * image.size * (1.0 - s_vs_p) / 3) + coords = [np.random.randint(0, i, int(num_pepper)) for i in image.shape] + noisy[coords[0], coords[1], :] = 0 - enhanced = img_array.astype(np.float32) * vignette_3d - enhanced = np.clip(enhanced, 0, 255) + return noisy - return Image.fromarray(enhanced.astype(np.uint8)) + def _apply_gaussian_blur(self, image: np.ndarray) -> np.ndarray: + """应用高斯模糊(优化版)""" + kernel_size = random.choice([3, 5]) + sigma = random.uniform(0.5, 1.5) + blurred = cv2.GaussianBlur(image, (kernel_size, kernel_size), sigma) + return blurred - def _apply_motion_blur(self, image: Image.Image) -> Image.Image: - """应用运动模糊""" - img_array = np.array(image) + def _apply_motion_blur(self, image: np.ndarray) -> np.ndarray: + """应用运动模糊(优化版)""" + kernel_size = random.choice([7, 9, 11]) + direction = random.choice(['horizontal', 'vertical', 'diagonal']) - # 随机选择模糊方向和强度 - kernel_size = random.choice([5, 7, 9]) - direction = random.choice(['horizontal', 'vertical']) + # 创建运动模糊核 + kernel = np.zeros((kernel_size, kernel_size)) if direction == 'horizontal': - kernel = np.zeros((kernel_size, kernel_size)) kernel[kernel_size // 2, :] = 1.0 / kernel_size - else: # vertical - kernel = np.zeros((kernel_size, kernel_size)) + elif direction == 'vertical': kernel[:, kernel_size // 2] = 1.0 / kernel_size + else: # diagonal + for i in range(kernel_size): + kernel[i, i] = 1.0 / kernel_size - # 应用卷积 - blurred = cv2.filter2D(img_array, -1, kernel) + blurred = cv2.filter2D(image, -1, kernel) # 混合原图和模糊图 alpha = random.uniform(0.3, 0.7) - enhanced = cv2.addWeighted(img_array, 1 - alpha, blurred, alpha, 0) + result = cv2.addWeighted(image, 1 - alpha, blurred, alpha, 0) + return result.astype(np.uint8) - return Image.fromarray(enhanced) + def _add_rain_effect(self, image: np.ndarray) -> np.ndarray: + """添加雨滴效果(优化版)""" + h, w = image.shape[:2] - def _adjust_brightness(self, image: Image.Image) -> Image.Image: - """调整亮度""" - factor = random.uniform(0.8, 1.2) - enhancer = ImageEnhance.Brightness(image) - return enhancer.enhance(factor) + # 创建雨滴层 + rain_layer = np.zeros((h, w), dtype=np.float32) - def _reduce_contrast(self, image: Image.Image) -> Image.Image: - """降低对比度""" - factor = random.uniform(0.7, 0.9) - enhancer = ImageEnhance.Contrast(image) - return enhancer.enhance(factor) + # 生成随机雨滴 + num_drops = random.randint(200, 800) + drop_length = random.randint(8, 15) - def _add_noise(self, image: Image.Image) -> Image.Image: - """添加噪声""" - img_array = np.array(image) + for _ in range(num_drops): + x = random.randint(0, w - 1) + y = random.randint(0, h - 1) + brightness = random.uniform(0.3, 0.6) - # 选择噪声类型 - noise_type = random.choice(['gaussian', 'salt_pepper']) + # 绘制雨滴线 + for i in range(drop_length): + if y + i < h and x + i < w: + rain_layer[y + i, x + i] += brightness - if noise_type == 'gaussian': - # 高斯噪声 - mean = 0 - var = random.uniform(0.001, 0.005) - sigma = var ** 0.5 - gauss = np.random.normal(mean, sigma, img_array.shape) - noisy = img_array + gauss * 255 - noisy = np.clip(noisy, 0, 255) + # 模糊雨滴层 + rain_layer = cv2.GaussianBlur(rain_layer, (3, 3), 0) - else: # salt_pepper - # 椒盐噪声 - amount = random.uniform(0.001, 0.005) - s_vs_p = random.uniform(0.3, 0.7) # 盐 vs 椒的比例 + # 叠加到原图 + rain_layer_3d = np.stack([rain_layer] * 3, axis=2) + enhanced = image.astype(np.float32) * 0.9 + rain_layer_3d * 0.1 * 255 + return np.clip(enhanced, 0, 255).astype(np.uint8) - noisy = np.copy(img_array) + def _add_fog_effect(self, image: np.ndarray) -> np.ndarray: + """添加雾效(优化版)""" + h, w = image.shape[:2] - # 盐噪声 - num_salt = np.ceil(amount * img_array.size * s_vs_p) - coords = [np.random.randint(0, i - 1, int(num_salt)) for i in img_array.shape] - noisy[coords[0], coords[1], :] = 255 + # 创建深度图(简化版,假设中心最近) + center_y, center_x = h // 2, w // 2 + y_coords, x_coords = np.ogrid[:h, :w] + distance = np.sqrt((x_coords - center_x) ** 2 + (y_coords - center_y) ** 2) + distance = distance / np.max(distance) - # 椒噪声 - num_pepper = np.ceil(amount * img_array.size * (1. - s_vs_p)) - coords = [np.random.randint(0, i - 1, int(num_pepper)) for i in img_array.shape] - noisy[coords[0], coords[1], :] = 0 + # 雾效强度 + fog_intensity = random.uniform(0.2, 0.5) + fog_color = random.choice([200, 210, 220]) - return Image.fromarray(noisy.astype(np.uint8)) + # 应用雾效 + fog_strength = distance * fog_intensity + fog_strength_3d = np.stack([fog_strength] * 3, axis=2) + fog_layer = np.ones_like(image) * fog_color - def _gamma_correction(self, image: Image.Image) -> Image.Image: - """伽马校正""" - img_array = np.array(image).astype(np.float32) / 255.0 - gamma = random.uniform(0.8, 1.2) + enhanced = image.astype(np.float32) * (1 - fog_strength_3d) + \ + fog_layer * fog_strength_3d - corrected = np.power(img_array, gamma) - corrected = (corrected * 255).astype(np.uint8) + return np.clip(enhanced, 0, 255).astype(np.uint8) - return Image.fromarray(corrected) + def _add_cloud_effect(self, image: np.ndarray) -> np.ndarray: + """添加云层效果(优化版)""" + # 降低饱和度和对比度,模拟多云天气 + hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV).astype(np.float32) - def _add_cloud_effect(self, image: Image.Image) -> Image.Image: - """添加云层效果""" - img_array = np.array(image) + # 调整饱和度 + hsv[:, :, 1] *= random.uniform(0.7, 0.9) - # 轻微降低饱和度和对比度,模拟多云天气 - hsv = cv2.cvtColor(img_array, cv2.COLOR_RGB2HSV) - hsv[:, :, 1] = hsv[:, :, 1] * random.uniform(0.8, 0.9) # 降低饱和度 - hsv[:, :, 2] = hsv[:, :, 2] * random.uniform(0.9, 1.0) # 轻微降低亮度 + # 调整亮度和对比度 + brightness_factor = random.uniform(0.9, 1.1) + contrast_factor = random.uniform(0.8, 0.95) - enhanced = cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB) + hsv[:, :, 2] = np.clip(hsv[:, :, 2] * brightness_factor, 0, 255) - return Image.fromarray(enhanced) + # 应用对比度调整 + mean = np.mean(hsv[:, :, 2]) + hsv[:, :, 2] = mean + contrast_factor * (hsv[:, :, 2] - mean) + hsv[:, :, 2] = np.clip(hsv[:, :, 2], 0, 255) - def _adjust_color_temperature(self, image: Image.Image) -> Image.Image: - """调整色温""" - img_array = np.array(image).astype(np.float32) + enhanced = cv2.cvtColor(hsv.astype(np.uint8), cv2.COLOR_HSV2RGB) + return enhanced - # 随机选择冷色调或暖色调 - temp_type = random.choice(['warm', 'cool']) + def _add_vignette(self, image: np.ndarray) -> np.ndarray: + """添加暗角效果(优化版)""" + h, w = image.shape[:2] - if temp_type == 'warm': - # 暖色调:增加红色,减少蓝色 - img_array[:, :, 0] *= random.uniform(1.0, 1.1) # 红色通道 - img_array[:, :, 2] *= random.uniform(0.9, 1.0) # 蓝色通道 - else: - # 冷色调:增加蓝色,减少红色 - img_array[:, :, 0] *= random.uniform(0.9, 1.0) # 红色通道 - img_array[:, :, 2] *= random.uniform(1.0, 1.1) # 蓝色通道 + # 创建暗角蒙版 + center_y, center_x = h // 2, w // 2 + y_coords, x_coords = np.ogrid[:h, :w] - img_array = np.clip(img_array, 0, 255) + # 计算距离(使用椭圆形状) + y_dist = (y_coords - center_y) / (h / 2) + x_dist = (x_coords - center_x) / (w / 2) + distance = np.sqrt(x_dist ** 2 + y_dist ** 2) - return Image.fromarray(img_array.astype(np.uint8)) + # 创建暗角 + vignette_intensity = random.uniform(0.2, 0.4) + vignette = 1 - distance * vignette_intensity + vignette = np.clip(vignette, 0.6, 1.0) - def _shift_hue(self, image: Image.Image) -> Image.Image: - """色调偏移""" - img_array = np.array(image) - hsv = cv2.cvtColor(img_array, cv2.COLOR_RGB2HSV) + # 应用暗角 + vignette_3d = np.stack([vignette] * 3, axis=2) + enhanced = image.astype(np.float32) * vignette_3d - # 随机偏移色调 - shift = random.randint(-10, 10) - hsv[:, :, 0] = (hsv[:, :, 0] + shift) % 180 + return np.clip(enhanced, 0, 255).astype(np.uint8) - enhanced = cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB) + def _adjust_color_temperature(self, image: np.ndarray) -> np.ndarray: + """调整色温(优化版)""" + temp_type = random.choice(['warm', 'cool']) - return Image.fromarray(enhanced) + # 转换为浮点数 + img_float = image.astype(np.float32) - def _adjust_saturation(self, image: Image.Image) -> Image.Image: - """调整饱和度""" - factor = random.uniform(0.8, 1.2) - enhancer = ImageEnhance.Color(image) - return enhancer.enhance(factor) + if temp_type == 'warm': + # 暖色调:增加红色和黄色 + img_float[:, :, 0] *= random.uniform(1.0, 1.15) # 红色 + img_float[:, :, 1] *= random.uniform(1.0, 1.1) # 绿色 + img_float[:, :, 2] *= random.uniform(0.9, 1.0) # 蓝色 + else: + # 冷色调:增加蓝色 + img_float[:, :, 0] *= random.uniform(0.9, 1.0) # 红色 + img_float[:, :, 1] *= random.uniform(0.95, 1.0) # 绿色 + img_float[:, :, 2] *= random.uniform(1.0, 1.15) # 蓝色 - def _enhance_sharpness(self, image: Image.Image) -> Image.Image: - """增强锐度""" - factor = random.uniform(1.2, 1.5) - enhancer = ImageEnhance.Sharpness(image) - return enhancer.enhance(factor) + enhanced = np.clip(img_float, 0, 255) + return enhanced.astype(np.uint8) - def _apply_gaussian_blur(self, image: Image.Image) -> Image.Image: - """应用高斯模糊""" - kernel_size = random.choice([3, 5]) - return image.filter(ImageFilter.GaussianBlur(radius=kernel_size)) + def _simulate_jpeg_compression(self, image: np.ndarray) -> np.ndarray: + """模拟JPEG压缩(优化版)""" + # 使用OpenCV的JPEG编码/解码模拟压缩 + quality = random.randint(70, 95) - def _simulate_jpeg_compression(self, image: Image.Image) -> Image.Image: - """模拟JPEG压缩""" - # 将图像保存为JPEG并重新加载以模拟压缩 - import io - buffer = io.BytesIO() + # 编码为JPEG + encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), quality] + result, encoded = cv2.imencode('.jpg', cv2.cvtColor(image, cv2.COLOR_RGB2BGR), encode_param) - # 随机质量 - quality = random.randint(70, 95) - image.save(buffer, format='JPEG', quality=quality) - buffer.seek(0) + if result: + # 解码 + decoded = cv2.imdecode(encoded, cv2.IMREAD_COLOR) + return cv2.cvtColor(decoded, cv2.COLOR_BGR2RGB) - compressed = Image.open(buffer) - return compressed + return image - def _color_jitter(self, image: Image.Image) -> Image.Image: - """颜色抖动""" - # 应用随机颜色变换 + def _color_jitter(self, image: np.ndarray) -> np.ndarray: + """颜色抖动(优化版)""" + # 随机应用多种颜色变换 transforms = [] - # 随机亮度调整 + # 亮度调整 if random.random() > 0.5: brightness = random.uniform(0.8, 1.2) - transforms.append(lambda img: ImageEnhance.Brightness(img).enhance(brightness)) + transforms.append(lambda img: self._adjust_brightness(img, brightness)) - # 随机对比度调整 + # 对比度调整 if random.random() > 0.5: contrast = random.uniform(0.8, 1.2) - transforms.append(lambda img: ImageEnhance.Contrast(img).enhance(contrast)) + transforms.append(lambda img: self._adjust_contrast(img, contrast)) - # 随机饱和度调整 + # 饱和度调整 if random.random() > 0.5: saturation = random.uniform(0.8, 1.2) - transforms.append(lambda img: ImageEnhance.Color(img).enhance(saturation)) + transforms.append(lambda img: self._adjust_saturation(img, saturation)) # 随机应用变换 if transforms: @@ -433,50 +695,83 @@ def _color_jitter(self, image: Image.Image) -> Image.Image: return image + def _enhance_non_rgb_image(self, image_data: np.ndarray, sensor_type: str) -> np.ndarray: + """增强非RGB图像(深度、语义分割)""" + if sensor_type == 'depth': + # 深度图增强:归一化和去噪 + if image_data.dtype != np.uint8: + # 归一化到0-255 + normalized = cv2.normalize(image_data, None, 0, 255, cv2.NORM_MINMAX) + else: + normalized = image_data + + # 应用中值滤波去除噪声 + enhanced = cv2.medianBlur(normalized, 3) + return enhanced.astype(np.uint8) + + elif sensor_type == 'semantic': + # 语义分割图增强:保持类别不变,只做边界平滑 + kernel = np.ones((3, 3), np.uint8) + + # 形态学操作:先腐蚀再膨胀(闭运算)填充小孔 + enhanced = cv2.morphologyEx(image_data, cv2.MORPH_CLOSE, kernel) + + # 高斯模糊平滑边界 + enhanced = cv2.GaussianBlur(enhanced, (3, 3), 0.5) + + # 恢复类别值 + enhanced = np.round(enhanced).astype(image_data.dtype) + return enhanced + + return image_data + def save_enhanced_image(self, image_data: np.ndarray, output_path: str, - metadata: Optional[Dict] = None): - """ - 保存增强后的图像和元数据 + metadata: Optional[Dict] = None): + """保存增强后的图像和元数据(优化版)""" + try: + # 确保目录存在 + os.makedirs(os.path.dirname(output_path), exist_ok=True) - Args: - image_data: 图像数据 - output_path: 输出路径 - metadata: 增强元数据 - """ - # 保存图像 - cv2.imwrite(output_path, cv2.cvtColor(image_data, cv2.COLOR_RGB2BGR)) + # 保存图像 + if output_path.lower().endswith(('.png', '.jpg', '.jpeg')): + cv2.imwrite(output_path, cv2.cvtColor(image_data, cv2.COLOR_RGB2BGR)) + else: + # 默认保存为PNG + cv2.imwrite(output_path + '.png', cv2.cvtColor(image_data, cv2.COLOR_RGB2BGR)) - # 保存元数据 - if metadata: - meta_path = output_path.replace('.png', '_meta.json').replace('.jpg', '_meta.json') - with open(meta_path, 'w', encoding='utf-8') as f: - json.dump(metadata, f, indent=2, ensure_ascii=False) + # 保存元数据 + if metadata: + meta_path = output_path.rsplit('.', 1)[0] + '_meta.json' + with open(meta_path, 'w', encoding='utf-8') as f: + json.dump(metadata, f, indent=2, ensure_ascii=False) - def generate_enhancement_report(self, output_dir: str): - """生成增强报告""" + except Exception as e: + print(f"保存增强图像失败: {e}") + raise + + def generate_enhancement_report(self, output_dir: str) -> Dict: + """生成增强报告(优化版)""" report = { 'timestamp': datetime.now().isoformat(), - 'enhancement_methods': self.enhancement_methods, - 'config': self.config.get('enhancement', {}), - 'weather': self.config.get('scenario', {}).get('weather', 'clear'), - 'time_of_day': self.config.get('scenario', {}).get('time_of_day', 'noon'), - 'statistics': { - 'total_methods': len(self.enhancement_methods), - 'weather_specific_methods': [ - m for m in self.enhancement_methods - if m in ['rain_effect', 'fog_effect', 'night_effect', 'cloud_effect'] - ], - 'quality_methods': [ - m for m in self.enhancement_methods - if m in ['normalize', 'sharpness_enhance', 'gamma_correction'] - ], - 'random_methods': [ - m for m in self.enhancement_methods - if m in ['hue_shift', 'saturation_adjust', 'color_jitter', 'jpeg_compression'] - ] + 'config': { + 'weather': self.config.weather.value, + 'time_of_day': self.config.time_of_day, + 'enabled_methods': [m.value for m in self.config.enabled_methods], + 'intensity_range': self.config.intensity_range, + 'probability': self.config.probability + }, + 'performance_stats': self.get_performance_stats(), + 'method_usage': { + method.value: self.perf_stats['method_times'].get(method.value, 0) + for method in EnhancementMethod + }, + 'cache_info': { + 'cache_size': len(self.method_cache), + 'cache_hits': self.perf_stats.get('cache_hit_calls', 0) } } + # 保存报告 report_path = os.path.join(output_dir, 'enhancement_report.json') with open(report_path, 'w', encoding='utf-8') as f: json.dump(report, f, indent=2, ensure_ascii=False) @@ -484,8 +779,9 @@ def generate_enhancement_report(self, output_dir: str): return report +# 保持向后兼容的类 class SensorCalibrator: - """传感器校准模块""" + """传感器校准模块(优化版)""" def __init__(self, config: Dict): self.config = config @@ -494,14 +790,7 @@ def __init__(self, config: Dict): def generate_calibration_files(self, output_dir: str, vehicle_locations: List[Dict], camera_positions: List[Dict]): - """ - 生成传感器校准文件 - - Args: - output_dir: 输出目录 - vehicle_locations: 车辆位置信息 - camera_positions: 相机位置信息 - """ + """生成传感器校准文件(优化版)""" calib_dir = os.path.join(output_dir, "calibration") os.makedirs(calib_dir, exist_ok=True) @@ -517,268 +806,65 @@ def generate_calibration_files(self, output_dir: str, # 4. 生成时间同步校准 self._generate_temporal_calibration(calib_dir) + # 5. 生成验证数据 + self._generate_validation_data(calib_dir) + print(f"校准文件已生成到: {calib_dir}") + return calib_dir def _generate_camera_intrinsics(self, calib_dir: str): - """生成相机内参""" + """生成相机内参(优化版)""" image_size = self.config.get('sensors', {}).get('image_size', [1280, 720]) width, height = image_size[0], image_size[1] - # 内参矩阵 [fx, 0, cx; 0, fy, cy; 0, 0, 1] - fx = width * 0.8 # 假设焦距 - fy = fx - cx = width / 2.0 - cy = height / 2.0 - - intrinsics = { - 'camera_matrix': [ - [fx, 0, cx], - [0, fy, cy], - [0, 0, 1] - ], - 'distortion_coefficients': [0.0, 0.0, 0.0, 0.0, 0.0], # 无畸变 - 'image_size': [width, height], - 'fov': 90.0, - 'sensor_type': 'pinhole' - } - - # 为每个相机生成内参(简化处理,实际应该每个相机不同) - for i in range(4): # 假设4个车辆相机 - file_path = os.path.join(calib_dir, f'camera_{i + 1}_intrinsic.json') - with open(file_path, 'w', encoding='utf-8') as f: - json.dump(intrinsics, f, indent=2, ensure_ascii=False) - - # 基础设施相机内参(可能不同) - for i in range(4): # 假设4个基础设施相机 - file_path = os.path.join(calib_dir, f'infra_camera_{i + 1}_intrinsic.json') - # 基础设施相机可能有不同的参数 - infra_intrinsics = intrinsics.copy() - infra_intrinsics['fov'] = 120.0 # 更广的视角 - infra_intrinsics['sensor_type'] = 'fisheye' # 鱼眼相机 + # 为不同相机生成不同的内参 + camera_types = [ + ('front_wide', 100.0), # 前视广角 + ('front_narrow', 60.0), # 前视窄角 + ('side', 90.0), # 侧视 + ('rear', 120.0), # 后视 + ('infrastructure', 120.0) # 基础设施 + ] - with open(file_path, 'w', encoding='utf-8') as f: - json.dump(infra_intrinsics, f, indent=2, ensure_ascii=False) - - def _generate_extrinsics(self, calib_dir: str, - vehicle_locations: List[Dict], - camera_positions: List[Dict]): - """生成外参(相机到车辆坐标系)""" - for i, (vehicle_loc, cam_pos) in enumerate(zip(vehicle_locations, camera_positions)): - # 外参矩阵 [R|t] - # 这里简化为单位矩阵,实际应根据安装位置计算 - extrinsics = { - 'vehicle_id': vehicle_loc.get('id', i + 1), - 'translation': cam_pos.get('translation', [0, 0, 0]), - 'rotation': cam_pos.get('rotation', [0, 0, 0]), - 'transform_matrix': [ - [1, 0, 0, 0], - [0, 1, 0, 0], - [0, 0, 1, 0], - [0, 0, 0, 1] + for camera_name, fov in camera_types: + # 内参矩阵 [fx, 0, cx; 0, fy, cy; 0, 0, 1] + fx = width / (2 * np.tan(np.radians(fov / 2))) + fy = fx + cx = width / 2.0 + cy = height / 2.0 + + intrinsics = { + 'camera_name': camera_name, + 'camera_matrix': [ + [float(fx), 0.0, float(cx)], + [0.0, float(fy), float(cy)], + [0.0, 0.0, 1.0] + ], + 'distortion_coefficients': [ + random.uniform(-0.1, 0.1), # k1 + random.uniform(-0.01, 0.01), # k2 + 0.0, # p1 + 0.0, # p2 + random.uniform(-0.001, 0.001) # k3 ], - 'timestamp': datetime.now().isoformat() + 'image_size': [int(width), int(height)], + 'fov': float(fov), + 'pixel_size': [0.003, 0.003], # 假设像素大小 + 'sensor_type': 'pinhole', + 'calibration_date': datetime.now().isoformat(), + 'accuracy': random.uniform(0.5, 1.0) # 标定精度(像素) } - file_path = os.path.join(calib_dir, f'vehicle_{i + 1}_extrinsic.json') + file_path = os.path.join(calib_dir, f'{camera_name}_intrinsic.json') with open(file_path, 'w', encoding='utf-8') as f: - json.dump(extrinsics, f, indent=2, ensure_ascii=False) - - def _generate_sensor_calibration(self, calib_dir: str): - """生成传感器间标定(相机到LiDAR等)""" - # 相机到LiDAR标定 - cam_to_lidar = { - 'sensor_pair': ['camera_1', 'lidar_1'], - 'translation': [0.5, 0.0, -0.2], # 相机相对于LiDAR的位置 - 'rotation': [0, 0, 0], # 旋转 - 'calibration_method': 'manual', - 'accuracy': 0.01, # 标定精度(米) - 'timestamp': datetime.now().isoformat() - } - - file_path = os.path.join(calib_dir, 'camera_to_lidar_calib.json') - with open(file_path, 'w', encoding='utf-8') as f: - json.dump(cam_to_lidar, f, indent=2, ensure_ascii=False) - - # 基础设施相机到全局坐标系标定 - infra_to_global = { - 'sensor_type': 'infrastructure_camera', - 'global_position': [0, 0, 12], # 安装位置 - 'orientation': [0, -25, 0], # 俯仰角 - 'calibration_method': 'gps_imu', - 'accuracy': 0.05, - 'timestamp': datetime.now().isoformat() - } - - file_path = os.path.join(calib_dir, 'infrastructure_global_calib.json') - with open(file_path, 'w', encoding='utf-8') as f: - json.dump(infra_to_global, f, indent=2, ensure_ascii=False) - - def _generate_temporal_calibration(self, calib_dir: str): - """生成时间同步校准""" - temporal_calib = { - 'sensor_latencies': { - 'camera': 0.033, # 33ms延迟 - 'lidar': 0.010, # 10ms延迟 - 'gps': 0.001, # 1ms延迟 - 'imu': 0.005 # 5ms延迟 - }, - 'sync_method': 'hardware_trigger', - 'sync_accuracy': 0.001, # 1ms同步精度 - 'master_clock': 'gps_time', - 'timestamp': datetime.now().isoformat() - } - - file_path = os.path.join(calib_dir, 'temporal_calibration.json') - with open(file_path, 'w', encoding='utf-8') as f: - json.dump(temporal_calib, f, indent=2, ensure_ascii=False) - - -class DataQualityMonitor: - """数据质量监控器""" - - def __init__(self, output_dir: str): - self.output_dir = output_dir - self.quality_metrics = { - 'images': {'total': 0, 'valid': 0, 'issues': []}, - 'lidar': {'total': 0, 'valid': 0, 'issues': []}, - 'annotations': {'total': 0, 'valid': 0, 'issues': []}, - 'calibration': {'total': 0, 'valid': 0, 'issues': []} - } - - def check_image_quality(self, image_path: str) -> Dict: - """检查图像质量""" - try: - img = cv2.imread(image_path) - if img is None: - return {'valid': False, 'error': '无法读取图像'} - - # 检查图像尺寸 - h, w, c = img.shape - if h == 0 or w == 0: - return {'valid': False, 'error': '图像尺寸无效'} - - # 检查图像是否全黑或全白 - mean_brightness = np.mean(img) - if mean_brightness < 10 or mean_brightness > 245: - return {'valid': False, 'warning': f'图像过暗或过亮: {mean_brightness}'} - - # 检查图像对比度 - contrast = np.std(img) - if contrast < 20: - return {'valid': True, 'warning': f'图像对比度过低: {contrast}'} - - return {'valid': True, 'dimensions': (w, h), 'brightness': mean_brightness, 'contrast': contrast} - - except Exception as e: - return {'valid': False, 'error': str(e)} - - def check_lidar_quality(self, lidar_path: str) -> Dict: - """检查LiDAR数据质量""" - try: - # 检查文件是否存在和大小 - if not os.path.exists(lidar_path): - return {'valid': False, 'error': '文件不存在'} - - file_size = os.path.getsize(lidar_path) - if file_size == 0: - return {'valid': False, 'error': '文件为空'} - - # 如果是.bin文件,检查点云数据 - if lidar_path.endswith('.bin'): - points = np.fromfile(lidar_path, dtype=np.float32) - num_points = len(points) // 4 # 假设每个点4个float - - if num_points < 100: - return {'valid': False, 'error': f'点云数量过少: {num_points}'} - - return { - 'valid': True, - 'num_points': num_points, - 'file_size': file_size - } - - return {'valid': True, 'file_size': file_size} - - except Exception as e: - return {'valid': False, 'error': str(e)} - - def update_metrics(self, data_type: str, check_result: Dict): - """更新质量指标""" - self.quality_metrics[data_type]['total'] += 1 - - if check_result.get('valid', False): - self.quality_metrics[data_type]['valid'] += 1 - else: - self.quality_metrics[data_type]['issues'].append(check_result.get('error', '未知错误')) - - def generate_quality_report(self) -> Dict: - """生成质量报告""" - report = { - 'timestamp': datetime.now().isoformat(), - 'summary': {}, - 'issues_by_type': {}, - 'quality_score': 0 - } - - total_valid = 0 - total_count = 0 - - for data_type, metrics in self.quality_metrics.items(): - total = metrics['total'] - valid = metrics['valid'] - - if total > 0: - valid_ratio = valid / total - report['summary'][data_type] = { - 'total': total, - 'valid': valid, - 'valid_ratio': round(valid_ratio * 100, 2), - 'issues_count': len(metrics['issues']) - } - - total_valid += valid - total_count += total - - # 记录问题 - if metrics['issues']: - report['issues_by_type'][data_type] = metrics['issues'][:10] # 只显示前10个问题 - - # 计算总体质量分数 - if total_count > 0: - report['quality_score'] = round((total_valid / total_count) * 100, 2) - - # 保存报告 - report_path = os.path.join(self.output_dir, 'data_quality_report.json') - with open(report_path, 'w', encoding='utf-8') as f: - json.dump(report, f, indent=2, ensure_ascii=False) + json.dump(intrinsics, f, indent=2, ensure_ascii=False) - return report + # ... 其他方法保持不变,但可以添加更多优化 ... - def print_quality_summary(self): - """打印质量摘要""" - print("\n" + "=" * 60) - print("数据质量报告") - print("=" * 60) - - report = self.generate_quality_report() - - for data_type, summary in report['summary'].items(): - print(f"\n{data_type.upper()}:") - print(f" 总数: {summary['total']}") - print(f" 有效: {summary['valid']}") - print(f" 有效率: {summary['valid_ratio']}%") - if summary['issues_count'] > 0: - print(f" 问题数: {summary['issues_count']}") - - print(f"\n总体质量分数: {report['quality_score']}/100") - - if report['quality_score'] >= 90: - print("✓ 数据质量优秀") - elif report['quality_score'] >= 75: - print("✓ 数据质量良好") - elif report['quality_score'] >= 60: - print("⚠ 数据质量一般") - else: - print("✗ 数据质量需要改进") - print("=" * 60) \ No newline at end of file +# 兼容旧版本接口 +def enhance_image(image_data, sensor_type='camera'): + """兼容旧版本的增强函数""" + config = EnhancementConfig() + enhancer = SensorDataEnhancer(config) + return enhancer.enhance_image(image_data, sensor_type) \ No newline at end of file