diff --git a/src/enhance_pedestrian_safety/annotation_generator.py b/src/enhance_pedestrian_safety/annotation_generator.py index 734fa91616..3df7e12e87 100644 --- a/src/enhance_pedestrian_safety/annotation_generator.py +++ b/src/enhance_pedestrian_safety/annotation_generator.py @@ -2,10 +2,11 @@ import os import numpy as np from datetime import datetime +import threading class AnnotationGenerator: - """标注生成器""" + """标注生成器(行人安全增强版)""" def __init__(self, output_dir): self.output_dir = output_dir @@ -14,9 +15,21 @@ def __init__(self, output_dir): self.frame_annotations = {} self.object_counter = 0 + self.lock = threading.RLock() + + # 行人安全相关统计 + self.safety_stats = { + 'near_misses': 0, + 'critical_interactions': 0, + 'pedestrian_trajectories': {}, + 'risk_zones': [] + } + + # 高风险区域定义 + self.high_risk_zones = [] # 学校、人行横道等区域 def detect_objects(self, world, frame_num, timestamp): - """检测场景中的物体""" + """检测场景中的物体(行人安全增强)""" annotations = { 'frame_id': frame_num, 'timestamp': timestamp, @@ -25,12 +38,17 @@ def detect_objects(self, world, frame_num, timestamp): 'safety_info': { 'pedestrian_count': 0, 'vehicle_count': 0, - 'high_risk_interactions': 0 + 'high_risk_interactions': 0, + 'near_miss_count': 0, + 'pedestrian_distances': [], + 'risk_level': 'low' } } try: actors = world.get_actors() + pedestrians = [] + vehicles = [] for actor in actors: obj_type = actor.type_id @@ -41,15 +59,36 @@ def detect_objects(self, world, frame_num, timestamp): annotations['objects'].append(obj_info) self.object_counter += 1 - # 更新安全统计 + # 分类统计 if 'walker' in obj_type: annotations['safety_info']['pedestrian_count'] += 1 + pedestrians.append({ + 'id': actor.id, + 'location': obj_info['location'], + 'velocity': obj_info['velocity'] + }) elif 'vehicle' in obj_type: annotations['safety_info']['vehicle_count'] += 1 - - # 检测高风险交互 - annotations['safety_info']['high_risk_interactions'] = self._detect_high_risk_interactions( - annotations['objects']) + vehicles.append({ + 'id': actor.id, + 'location': obj_info['location'], + 'velocity': obj_info['velocity'], + 'type': obj_info['class'] + }) + + # 检测高风险交互和近碰撞 + risk_analysis = self._analyze_pedestrian_risk(pedestrians, vehicles) + annotations['safety_info']['high_risk_interactions'] = risk_analysis['high_risk_count'] + annotations['safety_info']['near_miss_count'] = risk_analysis['near_miss_count'] + annotations['safety_info']['pedestrian_distances'] = risk_analysis['distances'] + annotations['safety_info']['risk_level'] = risk_analysis['risk_level'] + + # 跟踪行人轨迹 + self._track_pedestrian_trajectories(frame_num, pedestrians) + + # 检测高风险区域 + risk_zones = self._detect_risk_zones(pedestrians, vehicles) + annotations['safety_info']['risk_zones'] = risk_zones self._save_annotations(frame_num, annotations) return annotations @@ -59,11 +98,15 @@ def detect_objects(self, world, frame_num, timestamp): return annotations def _extract_object_info(self, actor): - """提取物体信息""" + """提取物体信息(增强版)""" try: bbox = actor.bounding_box location = actor.get_location() velocity = actor.get_velocity() + transform = actor.get_transform() + + # 计算速度大小 + speed = np.sqrt(velocity.x ** 2 + velocity.y ** 2 + velocity.z ** 2) * 3.6 # 转换为km/h obj_info = { 'id': actor.id, @@ -77,8 +120,10 @@ def _extract_object_info(self, actor): 'velocity': { 'x': float(velocity.x), 'y': float(velocity.y), - 'z': float(velocity.z) + 'z': float(velocity.z), + 'speed_kmh': float(speed) }, + 'heading': float(transform.rotation.yaw), 'bounding_box': { 'extent': { 'x': float(bbox.extent.x), @@ -87,7 +132,8 @@ def _extract_object_info(self, actor): } }, 'attributes': { - 'is_alive': actor.is_alive + 'is_alive': actor.is_alive, + 'is_stationary': speed < 1.0 } } @@ -97,7 +143,7 @@ def _extract_object_info(self, actor): return None def _get_object_class(self, type_id): - """获取物体类别""" + """获取物体类别(增强版)""" type_lower = type_id.lower() if 'vehicle' in type_lower: @@ -113,6 +159,10 @@ def _get_object_class(self, type_id): return 'car' elif 'bmw' in type_lower: return 'car' + elif 'truck' in type_lower: + return 'truck' + elif 'bus' in type_lower: + return 'bus' else: return 'vehicle' @@ -122,16 +172,25 @@ def _get_object_class(self, type_id): elif 'traffic' in type_lower: return 'traffic_light' + elif 'bike' in type_lower or 'bicycle' in type_lower: + return 'bicycle' + + elif 'motorcycle' in type_lower: + return 'motorcycle' + else: return 'unknown' - def _detect_high_risk_interactions(self, objects): - """检测高风险交互""" + def _analyze_pedestrian_risk(self, pedestrians, vehicles): + """分析行人风险(增强版)""" high_risk_count = 0 - pedestrians = [obj for obj in objects if obj['class'] == 'pedestrian'] - vehicles = [obj for obj in objects if obj['class'] in ['car', 'vehicle']] + near_miss_count = 0 + distances = [] for pedestrian in pedestrians: + min_distance = float('inf') + min_vehicle = None + for vehicle in vehicles: # 计算距离 p_loc = pedestrian['location'] @@ -139,15 +198,118 @@ def _detect_high_risk_interactions(self, objects): distance = np.sqrt( (p_loc['x'] - v_loc['x']) ** 2 + - (p_loc['y'] - v_loc['y']) ** 2 + - (p_loc['z'] - v_loc['z']) ** 2 + (p_loc['y'] - v_loc['y']) ** 2 ) - # 如果距离小于5米,认为是高风险交互 - if distance < 5.0: - high_risk_count += 1 + # 考虑车辆速度和类型 + vehicle_speed = vehicle['velocity'].get('speed_kmh', 0) + vehicle_type = vehicle.get('type', 'car') + + # 调整风险阈值(卡车和公交车需要更大的安全距离) + safe_distance = 5.0 + if vehicle_type in ['truck', 'bus']: + safe_distance = 7.0 + + # 考虑速度因素 + if vehicle_speed > 30: + safe_distance *= (1 + (vehicle_speed - 30) / 50) + + distances.append({ + 'pedestrian_id': pedestrian['id'], + 'vehicle_id': vehicle['id'], + 'distance': distance, + 'vehicle_speed': vehicle_speed, + 'vehicle_type': vehicle_type, + 'safe_distance': safe_distance + }) + + if distance < safe_distance: + if distance < safe_distance * 0.5: + high_risk_count += 1 + else: + near_miss_count += 1 + + # 评估整体风险级别 + risk_level = 'low' + if high_risk_count > 3: + risk_level = 'critical' + elif high_risk_count > 1: + risk_level = 'high' + elif near_miss_count > 3: + risk_level = 'medium' + + return { + 'high_risk_count': high_risk_count, + 'near_miss_count': near_miss_count, + 'distances': distances, + 'risk_level': risk_level + } - return high_risk_count + def _track_pedestrian_trajectories(self, frame_num, pedestrians): + """跟踪行人轨迹""" + for pedestrian in pedestrians: + ped_id = pedestrian['id'] + if ped_id not in self.safety_stats['pedestrian_trajectories']: + self.safety_stats['pedestrian_trajectories'][ped_id] = [] + + self.safety_stats['pedestrian_trajectories'][ped_id].append({ + 'frame': frame_num, + 'location': pedestrian['location'], + 'velocity': pedestrian['velocity'] + }) + + # 只保留最近100帧的轨迹 + if len(self.safety_stats['pedestrian_trajectories'][ped_id]) > 100: + self.safety_stats['pedestrian_trajectories'][ped_id].pop(0) + + def _detect_risk_zones(self, pedestrians, vehicles): + """检测高风险区域""" + risk_zones = [] + + # 行人密集区域 + if len(pedestrians) >= 3: + # 计算行人质心 + locations = np.array([[p['location']['x'], p['location']['y']] for p in pedestrians]) + centroid = np.mean(locations, axis=0) + + # 计算区域半径 + distances = np.sqrt(np.sum((locations - centroid) ** 2, axis=1)) + radius = np.max(distances) + 5.0 + + risk_zones.append({ + 'type': 'pedestrian_crowd', + 'center': {'x': float(centroid[0]), 'y': float(centroid[1])}, + 'radius': float(radius), + 'pedestrian_count': len(pedestrians), + 'risk_level': 'high' if len(pedestrians) > 5 else 'medium' + }) + + # 车辆行人交互区域 + for vehicle in vehicles: + if vehicle['velocity'].get('speed_kmh', 0) > 30: + v_loc = vehicle['location'] + nearby_pedestrians = 0 + + for pedestrian in pedestrians: + p_loc = pedestrian['location'] + distance = np.sqrt( + (p_loc['x'] - v_loc['x']) ** 2 + + (p_loc['y'] - v_loc['y']) ** 2 + ) + + if distance < 20: + nearby_pedestrians += 1 + + if nearby_pedestrians >= 2: + risk_zones.append({ + 'type': 'vehicle_pedestrian_interaction', + 'location': v_loc, + 'vehicle_speed': vehicle['velocity'].get('speed_kmh', 0), + 'pedestrian_count': nearby_pedestrians, + 'risk_level': 'high' if vehicle['velocity'].get('speed_kmh', 0) > 50 else 'medium' + }) + + return risk_zones def _save_annotations(self, frame_num, annotations): """保存标注到文件""" @@ -158,13 +320,29 @@ def _save_annotations(self, frame_num, annotations): json.dump(annotations, f, indent=2, ensure_ascii=False) # 同时更新总标注文件 - self.frame_annotations[frame_num] = annotations - self._update_master_annotation() + with self.lock: + self.frame_annotations[frame_num] = annotations + self._update_master_annotation() def _update_master_annotation(self): """更新主标注文件""" master_file = os.path.join(self.annotations_dir, "annotations.json") + # 计算安全统计 + total_high_risk = 0 + total_near_miss = 0 + max_risk_level = 'low' + + for frame_data in self.frame_annotations.values(): + total_high_risk += frame_data['safety_info']['high_risk_interactions'] + total_near_miss += frame_data['safety_info']['near_miss_count'] + + # 更新最高风险级别 + risk_level = frame_data['safety_info']['risk_level'] + risk_weights = {'low': 0, 'medium': 1, 'high': 2, 'critical': 3} + if risk_weights[risk_level] > risk_weights[max_risk_level]: + max_risk_level = risk_level + master_data = { 'total_frames': len(self.frame_annotations), 'total_objects': self.object_counter, @@ -173,8 +351,10 @@ def _update_master_annotation(self): 'safety_summary': { 'total_pedestrians': sum(f['safety_info']['pedestrian_count'] for f in self.frame_annotations.values()), 'total_vehicles': sum(f['safety_info']['vehicle_count'] for f in self.frame_annotations.values()), - 'total_high_risk': sum( - f['safety_info']['high_risk_interactions'] for f in self.frame_annotations.values()) + 'total_high_risk': total_high_risk, + 'total_near_misses': total_near_miss, + 'max_risk_level': max_risk_level, + 'pedestrian_trajectories': len(self.safety_stats['pedestrian_trajectories']) } } @@ -182,35 +362,58 @@ def _update_master_annotation(self): json.dump(master_data, f, indent=2, ensure_ascii=False) def generate_summary(self): - """生成标注摘要""" + """生成标注摘要(增强版)""" vehicle_count = 0 pedestrian_count = 0 + bicycle_count = 0 other_count = 0 high_risk_count = 0 + near_miss_count = 0 for frame_data in self.frame_annotations.values(): for obj in frame_data.get('objects', []): - if obj['class'] in ['car', 'vehicle']: + if obj['class'] in ['car', 'vehicle', 'truck', 'bus']: vehicle_count += 1 elif obj['class'] == 'pedestrian': pedestrian_count += 1 + elif obj['class'] == 'bicycle': + bicycle_count += 1 else: other_count += 1 high_risk_count += frame_data['safety_info']['high_risk_interactions'] + near_miss_count += frame_data['safety_info']['near_miss_count'] + + # 计算安全指标 + total_interactions = high_risk_count + near_miss_count + safety_score = 100 + if total_interactions > 0: + risk_ratio = high_risk_count / total_interactions + safety_score = max(0, 100 - risk_ratio * 100) summary = { 'total_frames': len(self.frame_annotations), 'total_objects': self.object_counter, 'vehicles': vehicle_count, 'pedestrians': pedestrian_count, + 'bicycles': bicycle_count, 'other_objects': other_count, 'high_risk_interactions': high_risk_count, + 'near_miss_interactions': near_miss_count, 'average_objects_per_frame': self.object_counter / len( self.frame_annotations) if self.frame_annotations else 0, 'safety_metrics': { + 'safety_score': safety_score, 'pedestrian_to_vehicle_ratio': pedestrian_count / max(1, vehicle_count), - 'high_risk_percentage': high_risk_count / max(1, self.object_counter) * 100 + 'high_risk_percentage': high_risk_count / max(1, total_interactions) * 100, + 'interaction_frequency': total_interactions / len( + self.frame_annotations) if self.frame_annotations else 0 + }, + 'trajectory_analysis': { + 'unique_pedestrians': len(self.safety_stats['pedestrian_trajectories']), + 'average_trajectory_length': np.mean( + [len(t) for t in self.safety_stats['pedestrian_trajectories'].values()]) if self.safety_stats[ + 'pedestrian_trajectories'] else 0 } } @@ -218,4 +421,68 @@ def generate_summary(self): with open(summary_file, 'w', encoding='utf-8') as f: json.dump(summary, f, indent=2, ensure_ascii=False) - return summary \ No newline at end of file + return summary + + def generate_safety_report(self): + """生成详细安全报告""" + report = { + 'timestamp': datetime.now().isoformat(), + 'frames_analyzed': len(self.frame_annotations), + 'safety_statistics': self.safety_stats, + 'risk_analysis': self._analyze_risk_patterns(), + 'recommendations': self._generate_safety_recommendations() + } + + report_file = os.path.join(self.annotations_dir, "safety_report.json") + with open(report_file, 'w', encoding='utf-8') as f: + json.dump(report, f, indent=2, ensure_ascii=False) + + return report + + def _analyze_risk_patterns(self): + """分析风险模式""" + risk_patterns = { + 'high_risk_frames': [], + 'time_of_day_patterns': {}, + 'location_patterns': {}, + 'speed_patterns': {} + } + + # 分析高风险帧的模式 + for frame_num, frame_data in self.frame_annotations.items(): + if frame_data['safety_info']['high_risk_interactions'] > 0: + risk_patterns['high_risk_frames'].append({ + 'frame': frame_num, + 'high_risk_count': frame_data['safety_info']['high_risk_interactions'], + 'risk_level': frame_data['safety_info']['risk_level'] + }) + + return risk_patterns + + def _generate_safety_recommendations(self): + """生成安全建议""" + recommendations = [] + + # 基于统计数据的建议 + total_high_risk = sum(f['safety_info']['high_risk_interactions'] for f in self.frame_annotations.values()) + + if total_high_risk > 10: + recommendations.append("建议增加行人安全监控频率") + recommendations.append("考虑降低高风险区域的车辆速度限制") + + if self.safety_stats['near_misses'] > 20: + recommendations.append("建议实施主动行人检测和预警系统") + recommendations.append("考虑增加人行横道和行人信号灯") + + # 基于轨迹分析的建议 + if len(self.safety_stats['pedestrian_trajectories']) > 0: + avg_speed = np.mean([ + np.sqrt(p['velocity']['x'] ** 2 + p['velocity']['y'] ** 2) + for traj in self.safety_stats['pedestrian_trajectories'].values() + for p in traj + ]) + + if avg_speed > 2.0: # 米/秒 + recommendations.append("行人移动速度较快,建议加强行人流量管理") + + return recommendations \ No newline at end of file diff --git a/src/enhance_pedestrian_safety/carla_utils.py b/src/enhance_pedestrian_safety/carla_utils.py index 349f4e7c78..dae031b8fe 100644 --- a/src/enhance_pedestrian_safety/carla_utils.py +++ b/src/enhance_pedestrian_safety/carla_utils.py @@ -1,14 +1,16 @@ """ -CARLA 工具模块 - 用于自动查找和配置 CARLA 路径 +CARLA 工具模块 - 用于自动查找和配置 CARLA 路径(增强版) """ import sys import os import glob import argparse +import subprocess +import time def find_carla_egg(): - """自动查找CARLA的egg文件""" + """自动查找CARLA的egg文件(增强版)""" common_paths = [ os.path.expanduser("~/carla/*"), os.path.expanduser("~/Desktop/carla/*"), @@ -16,87 +18,338 @@ def find_carla_egg(): "/opt/carla/*", "C:/carla/*", "D:/carla/*", + "E:/carla/*", os.path.dirname(os.path.abspath(__file__)), os.path.join(os.path.dirname(os.path.abspath(__file__)), "carla"), + os.getcwd(), + os.path.join(os.getcwd(), "carla"), ] + # 添加环境变量路径 + if os.getenv("CARLA_ROOT"): + common_paths.insert(0, os.getenv("CARLA_ROOT")) + + egg_files = [] + for path in common_paths: - egg_pattern = os.path.join(path, "PythonAPI", "carla", "dist", "carla-*.egg") - egg_files = glob.glob(egg_pattern, recursive=True) - if egg_files: - return egg_files[0] - egg_pattern = os.path.join(path, "dist", "carla-*.egg") - egg_files = glob.glob(egg_pattern, recursive=True) - if egg_files: - return egg_files[0] - return None + # 检查路径是否存在 + if not os.path.exists(path.replace("*", "")): + continue + + # 搜索.egg文件 + egg_patterns = [ + os.path.join(path, "PythonAPI", "carla", "dist", "carla-*.egg"), + os.path.join(path, "PythonAPI", "carla", "dist", "carla-*%d*.egg" % sys.version_info.major), + os.path.join(path, "dist", "carla-*.egg"), + os.path.join(path, "dist", "carla-*%d*.egg" % sys.version_info.major), + os.path.join(path, "carla-*.egg"), + os.path.join(path, "PythonAPI", "dist", "carla-*.egg"), + ] + + for pattern in egg_patterns: + found = glob.glob(pattern, recursive=True) + if found: + egg_files.extend(found) + + # 去重并排序(最新的优先) + egg_files = list(set(egg_files)) + egg_files.sort(key=os.path.getmtime, reverse=True) + + return egg_files[0] if egg_files else None + + +def check_carla_server(): + """检查CARLA服务器是否运行""" + try: + import socket + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.settimeout(2) + result = sock.connect_ex(('localhost', 2000)) + sock.close() + return result == 0 + except: + return False + + +def start_carla_server(carla_path=None): + """启动CARLA服务器""" + if check_carla_server(): + print("CARLA服务器已在运行") + return True + + print("正在启动CARLA服务器...") + + # 查找CarlaUE4可执行文件 + carla_binaries = [] + + if carla_path: + search_paths = [carla_path] + else: + search_paths = [ + os.path.expanduser("~/carla"), + "/opt/carla", + "C:/carla", + "D:/carla", + "E:/carla", + ] + + for path in search_paths: + if os.path.exists(path): + # Linux + linux_bin = os.path.join(path, "CarlaUE4", "Binaries", "Linux", "CarlaUE4-Linux-Shipping") + if os.path.exists(linux_bin): + carla_binaries.append(linux_bin) + + # Windows + win_bin = os.path.join(path, "CarlaUE4.exe") + if os.path.exists(win_bin): + carla_binaries.append(win_bin) + + if not carla_binaries: + print("未找到CARLA可执行文件") + return False + + # 启动服务器 + try: + cmd = [carla_binaries[0], "-quality-level=Low", "-fps=10"] + if os.name == 'posix': # Linux/Unix + subprocess.Popen(cmd, start_new_session=True) + else: # Windows + subprocess.Popen(cmd, creationflags=subprocess.CREATE_NEW_PROCESS_GROUP) + + # 等待服务器启动 + print("等待CARLA服务器启动...") + for i in range(30): # 等待30秒 + if check_carla_server(): + print("CARLA服务器启动成功") + time.sleep(2) # 额外等待2秒确保完全启动 + return True + time.sleep(1) + + print("CARLA服务器启动超时") + return False + + except Exception as e: + print(f"启动CARLA服务器失败: {e}") + return False def setup_carla_path(): - """设置CARLA路径并返回配置信息""" - print("\n[1/6] 初始化CARLA环境...") + """设置CARLA路径并返回配置信息(增强版)""" + print("\n" + "=" * 50) + print("[1/6] 初始化CARLA环境...") + print("=" * 50) path_parser = argparse.ArgumentParser(add_help=False) - path_parser.add_argument('--carla-path', type=str, help='CARLA的egg文件路径或dist目录路径') + path_parser.add_argument('--carla-path', type=str, help='CARLA的egg文件路径或CARLA根目录路径') + path_parser.add_argument('--start-server', action='store_true', help='自动启动CARLA服务器') + path_parser.add_argument('--server-timeout', type=int, default=30, help='服务器启动超时时间(秒)') args_path, remaining_argv = path_parser.parse_known_args() carla_egg_path = None + # 1. 首先检查命令行参数 if args_path.carla_path: + print(f"使用命令行指定的CARLA路径: {args_path.carla_path}") + if os.path.isfile(args_path.carla_path) and args_path.carla_path.endswith('.egg'): carla_egg_path = args_path.carla_path + egg_dir = os.path.dirname(carla_egg_path) + if egg_dir not in sys.path: + sys.path.append(egg_dir) + print(f"✓ 添加egg文件目录到系统路径: {egg_dir}") + elif os.path.isdir(args_path.carla_path): - egg_files = glob.glob(os.path.join(args_path.carla_path, "carla-*.egg")) - if egg_files: + # 搜索.egg文件 + egg_files = glob.glob(os.path.join(args_path.carla_path, "**", "carla-*.egg"), recursive=True) + + # 优先选择与Python版本匹配的egg + py_version = f"{sys.version_info.major}{sys.version_info.minor}" + version_matched = [f for f in egg_files if py_version in f] + + if version_matched: + carla_egg_path = version_matched[0] + elif egg_files: carla_egg_path = egg_files[0] - if carla_egg_path: - sys.path.append(os.path.dirname(carla_egg_path)) - print(f"✓ 通过命令行参数加载CARLA egg文件: {carla_egg_path}") - return carla_egg_path, remaining_argv - else: - print(f"✗ 命令行参数指定的路径中未找到CARLA egg文件: {args_path.carla_path}") - sys.exit(1) + if carla_egg_path: + egg_dir = os.path.dirname(carla_egg_path) + if egg_dir not in sys.path: + sys.path.append(egg_dir) + print(f"✓ 添加egg文件目录到系统路径: {egg_dir}") + else: + # 如果目录下没有.egg文件,尝试将其添加到Python路径 + pythonapi_path = os.path.join(args_path.carla_path, "PythonAPI") + if os.path.exists(pythonapi_path): + sys.path.append(pythonapi_path) + print(f"✓ 添加PythonAPI目录到系统路径: {pythonapi_path}") + else: + sys.path.append(args_path.carla_path) + print(f"✓ 添加CARLA目录到系统路径: {args_path.carla_path}") - elif os.getenv("CARLA_PYTHON_PATH"): + # 2. 检查环境变量 + if not carla_egg_path and os.getenv("CARLA_PYTHON_PATH"): env_carla_path = os.getenv("CARLA_PYTHON_PATH") + print(f"使用环境变量CARLA_PYTHON_PATH: {env_carla_path}") + if os.path.isfile(env_carla_path) and env_carla_path.endswith('.egg'): carla_egg_path = env_carla_path + egg_dir = os.path.dirname(carla_egg_path) + if egg_dir not in sys.path: + sys.path.append(egg_dir) + print(f"✓ 通过环境变量添加egg文件目录: {egg_dir}") + elif os.path.isdir(env_carla_path): - egg_files = glob.glob(os.path.join(env_carla_path, "carla-*.egg")) + egg_files = glob.glob(os.path.join(env_carla_path, "**", "carla-*.egg"), recursive=True) if egg_files: carla_egg_path = egg_files[0] + egg_dir = os.path.dirname(carla_egg_path) + if egg_dir not in sys.path: + sys.path.append(egg_dir) + print(f"✓ 通过环境变量添加egg文件目录: {egg_dir}") + else: + # 将目录添加到Python路径 + sys.path.append(env_carla_path) + print(f"✓ 通过环境变量添加目录到系统路径: {env_carla_path}") - if carla_egg_path: - sys.path.append(os.path.dirname(carla_egg_path)) - print(f"✓ 通过环境变量加载CARLA egg文件: {carla_egg_path}") - return carla_egg_path, remaining_argv - else: - print(f"✗ 环境变量CARLA_PYTHON_PATH中未找到CARLA egg文件: {env_carla_path}") - sys.exit(1) - - else: + # 3. 自动查找 + if not carla_egg_path: + print("正在自动查找CARLA egg文件...") carla_egg_path = find_carla_egg() + if carla_egg_path: - sys.path.append(os.path.dirname(carla_egg_path)) - print(f"✓ 自动找到CARLA egg文件: {carla_egg_path}") - return carla_egg_path, remaining_argv + egg_dir = os.path.dirname(carla_egg_path) + if egg_dir not in sys.path: + sys.path.append(egg_dir) + print(f"✓ 自动找到并添加CARLA egg文件目录: {egg_dir}") else: - print("✗ 未找到CARLA egg文件!") - print("提示:请通过以下方式之一配置CARLA路径:") - print(" 1. 命令行参数:--carla-path ") - print(" 2. 环境变量:设置CARLA_PYTHON_PATH=") - print(" 3. 将CARLA放在用户目录carla/、桌面carla/或/opt/carla/(自动查找)") - sys.exit(1) + print("⚠ 未找到CARLA egg文件,尝试继续...") + + # 4. 检查CARLA模块是否可导入 + print("\n[2/6] 检查CARLA模块...") + try: + import carla + print("✓ CARLA模块导入成功") + + # 检查服务器连接 + print("\n[3/6] 检查CARLA服务器连接...") + if args_path.start_server: + if not start_carla_server(args_path.carla_path): + print("✗ CARLA服务器启动失败") + print("提示:请手动启动CARLA服务器后再运行程序") + sys.exit(1) + elif not check_carla_server(): + print("⚠ CARLA服务器未运行") + print("提示:请启动CARLA服务器或使用 --start-server 参数") + # 不退出,让用户选择是否继续 + + return carla_egg_path, remaining_argv + + except ImportError as e: + print(f"✗ CARLA模块导入失败: {e}") + print("\n" + "=" * 50) + print("CARLA配置指南:") + print("=" * 50) + print("请通过以下方式之一配置CARLA路径:") + print("\n1. 命令行参数(推荐):") + print(" python main.py --carla-path ") + print("\n2. 环境变量:") + print(" export CARLA_PYTHON_PATH=/PythonAPI") + print("\n3. 自动查找(将CARLA放在以下目录之一):") + print(" ~/carla/") + print(" ~/Desktop/carla/") + print(" /opt/carla/") + print(" C:/carla/") + print("\n4. 启动CARLA服务器:") + print(" python main.py --start-server") + print("\n提示:确保CARLA版本与Python版本匹配") + print("=" * 50) + sys.exit(1) def import_carla_module(): """导入CARLA模块""" - print("\n[2/6] 导入CARLA模块...") + print("\n[4/6] 导入CARLA模块...") try: import carla print("✓ CARLA模块导入成功") + + # 打印CARLA版本信息 + if hasattr(carla, '__version__'): + print(f" CARLA版本: {carla.__version__}") + else: + print(" CARLA版本: 未知") + + # 测试基本功能 + try: + client = carla.Client('localhost', 2000) + client.set_timeout(5.0) + world = client.get_world() + print(f" CARLA服务器: 已连接 ({world.get_map().name})") + client.reload_world(False) # 重置但不重新生成交通 + except Exception as e: + print(f" CARLA服务器连接: 失败 ({str(e)[:50]})") + print(" 提示:确保CARLA服务器正在运行") + return carla except ImportError as e: print(f"✗ 导入失败: {e}") - sys.exit(1) \ No newline at end of file + sys.exit(1) + + +def setup_carla_environment(): + """设置完整的CARLA环境""" + print("\n" + "=" * 50) + print("CARLA环境设置") + print("=" * 50) + + # 1. 设置路径 + carla_egg_path, remaining_argv = setup_carla_path() + + # 2. 导入模块 + carla_module = import_carla_module() + + # 3. 验证环境 + print("\n[5/6] 验证环境配置...") + + # 检查Python版本兼容性 + py_version = f"{sys.version_info.major}.{sys.version_info.minor}" + print(f" Python版本: {py_version}") + + # 检查必要依赖 + required_modules = ['numpy', 'PIL', 'opencv-python'] + for module in required_modules: + try: + __import__(module.replace('-', '_')) + print(f" {module}: ✓") + except ImportError: + print(f" {module}: ✗ (建议安装: pip install {module})") + + # 4. 最终状态 + print("\n[6/6] 环境设置完成") + print("-" * 30) + print("状态汇总:") + print(f" CARLA路径: {carla_egg_path or '使用系统路径'}") + print(f" 剩余参数: {remaining_argv}") + print("=" * 50) + print() + + return carla_module, remaining_argv + + +def cleanup_carla(): + """清理CARLA资源""" + print("\n清理CARLA环境...") + try: + import carla + # 这里可以添加自定义的清理逻辑 + print("✓ CARLA环境清理完成") + except: + print("⚠ CARLA环境清理跳过") + + +if __name__ == "__main__": + # 测试函数 + carla_module, args = setup_carla_environment() + print(f"CARLA模块: {carla_module}") + print(f"剩余参数: {args}") \ No newline at end of file diff --git a/src/enhance_pedestrian_safety/config_manager.py b/src/enhance_pedestrian_safety/config_manager.py index 7276b09a15..c30e39fb5d 100644 --- a/src/enhance_pedestrian_safety/config_manager.py +++ b/src/enhance_pedestrian_safety/config_manager.py @@ -3,6 +3,7 @@ import argparse import copy from typing import Dict, Any, Optional, List, Tuple +from datetime import datetime try: import yaml @@ -13,129 +14,222 @@ class ConfigValidator: + """配置验证器(增强版)""" @staticmethod def validate_config(config: Dict[str, Any]) -> Tuple[bool, List[str]]: errors = [] + warnings = [] + # 基本结构验证 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 'duration' in scenario: + if scenario['duration'] <= 0: + errors.append("场景时长必须大于0") + elif scenario['duration'] > 3600: + warnings.append("场景时长超过1小时,可能导致内存不足") if 'town' not in scenario: errors.append("场景配置中缺失地图名称") + if 'pedestrian_safety_mode' not in scenario: + warnings.append("建议启用行人安全模式以增强数据收集") + # 传感器配置验证 if 'sensors' in config: sensors = config['sensors'] - if 'capture_interval' in sensors and sensors['capture_interval'] <= 0: - errors.append("采集间隔必须大于0") + if 'capture_interval' in sensors: + if sensors['capture_interval'] <= 0: + errors.append("采集间隔必须大于0") + elif sensors['capture_interval'] < 0.5: + warnings.append("采集间隔过小可能导致性能问题") 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") - + elif sensors['image_size'][0] > 1920 or sensors['image_size'][1] > 1080: + warnings.append("图像尺寸较大,可能影响处理速度") + + # 行人安全配置验证 + if 'traffic' in config: + traffic = config['traffic'] + if 'pedestrians' in traffic: + if traffic['pedestrians'] < 0: + errors.append("行人数量不能为负数") + elif traffic['pedestrians'] > 50: + warnings.append("行人数量过多,可能导致场景混乱") + if 'speed_limit' in traffic: + if traffic['speed_limit'] > 120: + warnings.append("车速限制过高,影响行人安全评估") + + # 性能配置验证 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 + if 'batch_size' in perf: + if perf['batch_size'] <= 0: + errors.append("批处理大小必须大于0") + elif perf['batch_size'] > 100: + warnings.append("批处理大小过大,可能导致内存溢出") + if 'frame_rate_limit' in perf: + if perf['frame_rate_limit'] > 60: + warnings.append("帧率限制过高,可能导致性能问题") + + # V2X配置验证 + if 'v2x' in config and config['v2x'].get('enabled', False): + v2x = config['v2x'] + if 'communication_range' in v2x and v2x['communication_range'] > 1000: + warnings.append("V2X通信范围过大,可能导致不切实际的模拟") + if 'update_interval' in v2x and v2x['update_interval'] < 0.1: + warnings.append("V2X更新间隔过小,可能导致通信拥塞") + + return len(errors) == 0, errors, warnings @staticmethod def suggest_optimizations(config: Dict[str, Any]) -> List[str]: suggestions = [] + # LiDAR优化建议 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以下以减少内存使用") + channels = lidar_config.get('channels', 32) + if channels > 64: + suggestions.append(f"LiDAR通道数({channels})较高,建议降低到64以下以提高性能") + + # 采集间隔建议 capture_interval = config['sensors'].get('capture_interval', 2.0) - if capture_interval < 1.0: - suggestions.append(f"采集间隔({capture_interval}s)较短,可能导致高负载,建议增加到1.0s以上") + if capture_interval < 0.5: + suggestions.append(f"采集间隔({capture_interval}s)过短,可能导致高负载,建议增加到0.5s以上") + elif capture_interval > 5.0: + suggestions.append(f"采集间隔({capture_interval}s)较长,可能丢失重要数据,建议降低到5.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: + if len(enabled_outputs) > 8: suggestions.append(f"启用的输出类型过多({len(enabled_outputs)}),可能影响性能,建议只启用必要的输出") - # 行人安全相关建议 - if config.get('traffic', {}).get('pedestrians', 0) < 5: + # 行人安全建议 + pedestrians = config.get('traffic', {}).get('pedestrians', 0) + if pedestrians < 5: suggestions.append("行人数量较少,建议增加行人数量以更好地测试行人安全") + elif pedestrians > 30: + suggestions.append("行人数量较多,建议减少以保持场景清晰度") if not config.get('v2x', {}).get('enabled', False): suggestions.append("V2X通信未启用,建议启用以支持行人安全预警") + # 协同感知建议 + if config.get('cooperative', {}).get('num_coop_vehicles', 0) > 5: + suggestions.append("协同车辆数量过多,建议减少到5辆以下以降低复杂度") + return suggestions class ConfigOptimizer: + """配置优化器(增强版)""" @staticmethod def optimize_for_memory(config: Dict[str, Any]) -> Dict[str, Any]: + """内存优化配置""" optimized = copy.deepcopy(config) - 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 - }) + # 传感器优化 + optimized['sensors'].update({ + 'image_size': [640, 480], + 'capture_interval': 3.0, + 'lidar_sensors': 0, + 'radar_sensors': 0, + 'infrastructure_cameras': 2 + }) + # 性能优化 perf = optimized.setdefault('performance', {}) perf.update({ - 'batch_size': 3, + 'batch_size': 2, 'enable_compression': True, - 'compression_level': 4, + 'compression_level': 6, 'enable_memory_cache': True, - 'max_cache_size': 30, - 'frame_rate_limit': 3.0 + 'max_cache_size': 20, + 'frame_rate_limit': 2.0, + 'enable_async_processing': False, + 'max_workers': 1, + 'memory_management': { + 'gc_interval': 20, + 'max_memory_mb': 200, + 'early_stop_threshold': 180 + } }) - perf['image_processing'] = { - 'compress_images': True, - 'compression_quality': 80, - 'resize_images': False - } + # 输出优化 + output = optimized['output'] + output.update({ + 'save_raw': True, + 'save_stitched': False, + 'save_annotations': True, + 'save_lidar': False, + 'save_fusion': False, + 'save_cooperative': False, + 'save_v2x_messages': False, + 'save_enhanced': False, + 'save_safety_reports': True, + 'compression_enabled': True + }) 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 - } + 'capture_interval': 0.5, + 'lidar_sensors': 2, + 'radar_sensors': 1, + 'infrastructure_cameras': 6, + 'vehicle_cameras': 4 }) + # LiDAR配置 + sensors['lidar_config'].update({ + 'channels': 64, + 'range': 200.0, + 'points_per_second': 200000, + 'max_points_per_frame': 120000, + 'downsample_ratio': 0.05 + }) + + # 输出配置 output = optimized['output'] output.update({ + 'save_raw': True, + 'save_stitched': True, 'save_annotations': True, + 'save_lidar': True, 'save_fusion': True, 'save_cooperative': True, + 'save_v2x_messages': True, 'save_enhanced': True, - 'run_quality_check': True + 'save_safety_reports': True, + 'run_quality_check': True, + 'validate_data': True, + 'generate_safety_summary': True }) + # 增强配置 enhanced = optimized.setdefault('enhancement', {}) enhanced.update({ 'enabled': True, @@ -144,37 +238,50 @@ def optimize_for_quality(config: Dict[str, Any]) -> Dict[str, Any]: 'save_original': True, 'save_enhanced': True, 'calibration_generation': True, - 'enhanced_dir_name': 'enhanced', - 'methods': ['normalize', 'contrast', 'brightness'], + 'enhanced_dir_name': 'enhanced_quality', + 'methods': ['normalize', 'contrast', 'brightness', 'sharpness', 'noise'], 'weather_effects': True, - 'augmentation_level': 'medium' + 'augmentation_level': 'high', + 'pedestrian_safety_mode': True }) 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, + 'image_size': [640, 360], + 'capture_interval': 5.0, 'lidar_sensors': 0, - 'radar_sensors': 0 + 'radar_sensors': 0, + 'infrastructure_cameras': 2, + 'vehicle_cameras': 2 }) + # 性能优化 perf = optimized.setdefault('performance', {}) perf.update({ - 'batch_size': 10, + 'batch_size': 20, 'enable_compression': True, 'compression_level': 1, - 'enable_downsampling': True, + 'enable_memory_cache': False, + 'max_cache_size': 10, + 'frame_rate_limit': 15.0, 'enable_async_processing': True, - 'max_cache_size': 20, - 'frame_rate_limit': 10.0 + 'max_workers': 4, + 'memory_management': { + 'gc_interval': 100, + 'max_memory_mb': 300, + 'early_stop_threshold': 280 + } }) + # 输出优化 output = optimized['output'] output.update({ 'save_raw': True, @@ -182,85 +289,113 @@ def optimize_for_speed(config: Dict[str, Any]) -> Dict[str, Any]: 'save_annotations': False, 'save_lidar': False, 'save_fusion': False, - 'save_cooperative': False + 'save_cooperative': False, + 'save_v2x_messages': False, + 'save_enhanced': False, + 'save_safety_reports': True, + 'compression_enabled': True }) return optimized @staticmethod def optimize_for_safety(config: Dict[str, Any]) -> Dict[str, Any]: - """优化配置以增强行人安全""" + """行人安全优化配置(增强版)""" optimized = copy.deepcopy(config) - # 增加行人密度 + # 增加行人密度和多样性 traffic = optimized['traffic'] traffic.update({ - 'pedestrians': 12, # 增加行人数量 + 'pedestrians': 20, # 增加行人数量 'pedestrian_types': [ 'walker.pedestrian.0001', 'walker.pedestrian.0002', 'walker.pedestrian.0003', - 'walker.pedestrian.0004' + 'walker.pedestrian.0004', + 'walker.pedestrian.0005', + 'walker.pedestrian.0006', + 'walker.pedestrian.0007', + 'walker.pedestrian.0008' ], - 'speed_limit': 30.0 # 添加车速限制 + 'pedestrian_behaviors': ['walking', 'crossing', 'waiting', 'running', 'jogging'], + 'speed_limit': 30.0, + 'pedestrian_safety_zones': True, + 'crosswalk_density': 'high' }) # 优化传感器配置以更好地检测行人 sensors = optimized['sensors'] sensors.update({ - 'image_size': [1280, 720], - 'capture_interval': 1.5, # 更频繁地捕获 + 'image_size': [1920, 1080], + 'capture_interval': 1.0, # 更频繁地捕获 'vehicle_cameras': 4, + 'infrastructure_cameras': 6, 'camera_config': { - 'fov': 100.0, # 更宽的视野 - 'post_processing': 'default', + 'fov': 120.0, # 更宽的视野 + 'post_processing': 'semantic', # 语义分割 'exposure_mode': 'auto', - 'motion_blur': 0.0 + 'motion_blur': 0.0, + 'pedestrian_detection_mode': True } }) # 启用LiDAR以检测行人 - sensors['lidar_sensors'] = 1 + sensors['lidar_sensors'] = 2 sensors['lidar_config'].update({ 'channels': 64, # 更多通道以检测行人 - 'range': 120.0, - 'points_per_second': 100000, - 'max_points_per_frame': 80000, - 'downsample_ratio': 0.2 + 'range': 150.0, + 'points_per_second': 150000, + 'max_points_per_frame': 100000, + 'downsample_ratio': 0.1, + 'pedestrian_detection': True, + 'height_filter': [-0.5, 2.5] # 过滤地面和过高点 }) # 启用V2X和协同感知 v2x = optimized.setdefault('v2x', {}) v2x.update({ 'enabled': True, - 'communication_range': 300.0, - 'update_interval': 1.0, # 更频繁地更新 + 'communication_range': 500.0, + 'update_interval': 0.5, # 更频繁地更新 'enable_safety_warnings': True, - 'pedestrian_warning_threshold': 10.0 # 行人警告距离阈值 + 'pedestrian_warning_threshold': 15.0, + 'emergency_brake_warning': True, + 'collision_prediction': True, + 'vulnerable_road_user_protection': True }) + # 协同感知配置 coop = optimized.setdefault('cooperative', {}) coop.update({ - 'num_coop_vehicles': 2, + 'num_coop_vehicles': 3, 'enable_shared_perception': True, 'enable_traffic_warnings': True, - 'enable_pedestrian_warnings': True, # 启用行人警告 - 'enable_maneuver_coordination': False, - 'data_fusion_interval': 0.5, # 更频繁地融合 - 'max_shared_objects': 100, - 'object_matching_threshold': 3.0 # 更严格的对象匹配 + 'enable_pedestrian_warnings': True, + 'enable_emergency_brake_assist': True, + 'enable_maneuver_coordination': True, + 'data_fusion_interval': 0.3, # 更频繁地融合 + 'max_shared_objects': 200, + 'object_matching_threshold': 2.0, # 更严格的对象匹配 + 'pedestrian_tracking': True, + 'intention_prediction': True }) # 性能优化 perf = optimized.setdefault('performance', {}) perf.update({ - 'batch_size': 5, + 'batch_size': 8, 'enable_compression': True, - 'compression_level': 3, + 'compression_level': 4, 'enable_memory_cache': True, - 'max_cache_size': 40, - 'frame_rate_limit': 8.0, - 'safety_monitoring_interval': 1.0 # 安全监控间隔 + 'max_cache_size': 60, + 'frame_rate_limit': 10.0, + 'safety_monitoring_interval': 0.5, # 安全监控间隔 + 'emergency_response_time': 0.1, + 'memory_management': { + 'gc_interval': 30, + 'max_memory_mb': 600, + 'early_stop_threshold': 550 + } }) # 输出配置 @@ -272,12 +407,17 @@ def optimize_for_safety(config: Dict[str, Any]) -> Dict[str, Any]: 'save_lidar': True, 'save_fusion': True, 'save_cooperative': True, + 'save_v2x_messages': True, 'save_enhanced': True, - 'save_safety_reports': True, # 保存安全报告 + 'save_safety_reports': True, + 'save_risk_maps': True, + 'save_emergency_events': True, 'validate_data': True, 'run_analysis': True, 'run_quality_check': True, - 'generate_safety_summary': True # 生成安全摘要 + 'generate_summary': True, + 'generate_safety_summary': True, + 'generate_risk_assessment': True }) # 增强配置 @@ -289,21 +429,77 @@ def optimize_for_safety(config: Dict[str, Any]) -> Dict[str, Any]: 'save_original': True, 'save_enhanced': True, 'calibration_generation': True, - 'enhanced_dir_name': 'enhanced', - 'methods': ['normalize', 'contrast', 'brightness', 'pedestrian_highlight', 'safety_warning'], + 'enhanced_dir_name': 'enhanced_safety', + 'methods': ['normalize', 'contrast', 'brightness', 'pedestrian_highlight', + 'safety_warning', 'risk_visualization', 'attention_heatmap'], 'weather_effects': True, - 'augmentation_level': 'medium', - 'pedestrian_safety_mode': True # 启用行人安全模式 + 'augmentation_level': 'high', + 'pedestrian_safety_mode': True, + 'vulnerable_user_protection': True, + 'emergency_scenario_simulation': True + }) + + # 场景配置 + scenario = optimized['scenario'] + scenario.update({ + 'pedestrian_safety_mode': True, + 'emergency_scenarios': ['crosswalk', 'school_zone', 'blind_spot'], + 'risk_assessment': True, + 'safety_metrics_collection': True + }) + + return optimized + + @staticmethod + def optimize_for_research(config: Dict[str, Any]) -> Dict[str, Any]: + """研究优化配置""" + optimized = copy.deepcopy(config) + + # 增加数据多样性 + optimized['scenario'].update({ + 'weather_variations': ['clear', 'rainy', 'cloudy', 'foggy', 'wet'], + 'time_variations': ['noon', 'sunset', 'night', 'dawn'], + 'random_seed': -1, # 随机种子 + 'scenario_variations': 10 + }) + + # 传感器配置 + sensors = optimized['sensors'] + sensors.update({ + 'image_size': [1280, 720], + 'capture_interval': 1.0, + 'lidar_sensors': 1, + 'radar_sensors': 1, + 'vehicle_cameras': 3, + 'infrastructure_cameras': 4 + }) + + # 输出配置 + output = optimized['output'] + output.update({ + 'save_raw': True, + 'save_stitched': True, + 'save_annotations': True, + 'save_lidar': True, + 'save_fusion': True, + 'save_cooperative': True, + 'save_v2x_messages': True, + 'save_enhanced': True, + 'save_safety_reports': True, + 'save_metadata': True, + 'save_calibration': True }) return optimized class ConfigManager: + """配置管理器(增强版)""" + PRESET_CONFIGS = { 'balanced': { 'description': '平衡配置 - 兼顾性能和质量', - 'optimization': 'memory' + 'optimization': 'balanced' }, 'high_quality': { 'description': '高质量配置 - 优先数据质量', @@ -314,15 +510,23 @@ class ConfigManager: 'optimization': 'speed' }, 'pedestrian_safety': { - 'description': '行人安全配置 - 优化行人检测和安全评估', + 'description': '行人安全配置 - 优化行人检测和安全评估(增强版)', 'optimization': 'safety' }, + 'memory_efficient': { + 'description': '内存高效配置 - 优化内存使用', + 'optimization': 'memory' + }, + 'research_ready': { + 'description': '研究就绪配置 - 适用于学术研究', + 'optimization': 'research' + }, 'v2x_focused': { 'description': 'V2X重点配置 - 优化协同数据采集', 'optimization': 'custom', 'settings': { - 'v2x': {'enabled': True, 'update_interval': 1.0}, - 'cooperative': {'num_coop_vehicles': 3, 'enable_shared_perception': True}, + 'v2x': {'enabled': True, 'update_interval': 0.5}, + 'cooperative': {'num_coop_vehicles': 4, 'enable_shared_perception': True}, 'output': {'save_cooperative': True, 'save_v2x_messages': True} } }, @@ -333,11 +537,35 @@ class ConfigManager: 'sensors': {'lidar_sensors': 2, 'lidar_config': {'channels': 64, 'range': 200}}, 'output': {'save_lidar': True, 'save_fusion': True} } + }, + 'urban_testing': { + 'description': '城市测试配置 - 针对城市环境', + 'optimization': 'custom', + 'settings': { + 'traffic': {'pedestrians': 25, 'background_vehicles': 15}, + 'scenario': {'town': 'Town10HD'} + } + }, + 'emergency_testing': { + 'description': '紧急情况测试 - 测试安全系统响应', + 'optimization': 'custom', + 'settings': { + 'scenario': {'emergency_scenarios': ['pedestrian_crossing', 'child_darting']}, + 'v2x': {'emergency_brake_warning': True}, + 'performance': {'emergency_response_time': 0.05} + } } } @staticmethod - def load_config(config_file: Optional[str] = None, preset: Optional[str] = None) -> Dict[str, Any]: + def load_config(config_file: Optional[str] = None, + preset: Optional[str] = None, + validate: bool = True) -> Dict[str, Any]: + """加载配置(增强版)""" + print("\n" + "=" * 50) + print("加载配置") + print("=" * 50) + config = ConfigManager._get_default_config() if preset: @@ -346,118 +574,180 @@ def load_config(config_file: Optional[str] = None, preset: Optional[str] = None) if config_file: if os.path.exists(config_file): config = ConfigManager._load_config_file(config_file, config) + print(f"✓ 从文件加载配置: {config_file}") 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("配置验证失败") + print(f"⚠ 配置文件不存在: {config_file}") + # 尝试在预设目录中查找 + preset_dirs = ['configs', 'config', 'presets'] + for preset_dir in preset_dirs: + preset_path = os.path.join(preset_dir, config_file) + if os.path.exists(preset_path): + config = ConfigManager._load_config_file(preset_path, config) + print(f"✓ 从预设目录加载配置: {preset_path}") + break + + if validate: + is_valid, errors, warnings = ConfigValidator.validate_config(config) + + if warnings: + print("\n配置警告:") + for warning in warnings: + print(f" ⚠ {warning}") + + if not is_valid: + print("\n配置验证错误:") + for error in errors: + print(f" ✗ {error}") + raise ValueError("配置验证失败") + else: + print("✓ 配置验证通过") suggestions = ConfigValidator.suggest_optimizations(config) if suggestions: - print("配置优化建议:") + print("\n配置优化建议:") for suggestion in suggestions: - print(f" ⚡ {suggestion}") + print(f" 💡 {suggestion}") + + # 确保必要目录存在 + config['output']['data_dir'] = os.path.abspath(config['output']['data_dir']) + os.makedirs(config['output']['data_dir'], exist_ok=True) + + print(f"输出目录: {config['output']['data_dir']}") + print("=" * 50) return config @staticmethod def _get_default_config() -> Dict[str, Any]: + """获取默认配置(增强版)""" return { 'scenario': { - 'name': 'pedestrian_safety', + 'name': 'pedestrian_safety_enhanced', 'description': '行人安全增强数据采集场景', 'town': 'Town10HD', 'weather': 'clear', 'time_of_day': 'noon', - 'duration': 60, + 'duration': 120, 'seed': 42, - 'timeout': 300, - 'retry_attempts': 3 + 'timeout': 600, + 'retry_attempts': 5, + 'pedestrian_safety_mode': True, + 'emergency_scenarios': ['crosswalk', 'school_zone'], + 'risk_assessment': True, + 'safety_metrics_collection': True }, 'traffic': { 'ego_vehicles': 1, - 'background_vehicles': 8, - 'pedestrians': 12, # 增加默认行人数量 + 'background_vehicles': 12, + 'pedestrians': 15, + 'bicycles': 3, + 'motorcycles': 2, 'traffic_lights': True, 'batch_spawn': True, - 'max_spawn_attempts': 5, + 'max_spawn_attempts': 10, 'vehicle_types': [ 'vehicle.tesla.model3', 'vehicle.audi.tt', 'vehicle.nissan.patrol', - 'vehicle.bmw.grandtourer' + 'vehicle.bmw.grandtourer', + 'vehicle.mercedes.coupe', + 'vehicle.ford.mustang', + 'vehicle.lincoln.mkz2017', + 'vehicle.chevrolet.impala' ], 'pedestrian_types': [ 'walker.pedestrian.0001', 'walker.pedestrian.0002', 'walker.pedestrian.0003', - 'walker.pedestrian.0004' + 'walker.pedestrian.0004', + 'walker.pedestrian.0005', + 'walker.pedestrian.0006', + 'walker.pedestrian.0007', + 'walker.pedestrian.0008' ], - 'speed_limit': 30.0 + 'pedestrian_behaviors': ['walking', 'crossing', 'waiting', 'running'], + 'speed_limit': 40.0, + 'pedestrian_safety_zones': True, + 'crosswalk_density': 'medium', + 'vulnerable_user_protection': True }, 'sensors': { 'vehicle_cameras': 4, 'infrastructure_cameras': 4, 'lidar_sensors': 1, 'radar_sensors': 0, - 'gps_sensors': 0, - 'imu_sensors': 0, + 'gps_sensors': 1, + 'imu_sensors': 1, 'image_size': [1280, 720], 'capture_interval': 2.0, - 'sensor_placement': 'default', + 'sensor_placement': 'optimized', 'lidar_config': { 'channels': 32, - 'range': 100.0, - 'points_per_second': 56000, + 'range': 120.0, + 'points_per_second': 100000, '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, + 'upper_fov': 15.0, + 'lower_fov': -25.0, + 'max_points_per_frame': 60000, 'downsample_ratio': 0.3, - 'memory_warning_threshold': 300, - 'max_batch_memory_mb': 50, + 'memory_warning_threshold': 400, + 'max_batch_memory_mb': 60, 'v2x_save_interval': 5, - 'compression_format': 'bin' + 'compression_format': 'bin', + 'pedestrian_detection': True, + 'height_filter': [-0.5, 2.5] }, 'camera_config': { 'fov': 90.0, - 'post_processing': 'default', + 'post_processing': 'semantic', 'exposure_mode': 'auto', - 'motion_blur': 0.0 + 'motion_blur': 0.0, + 'pedestrian_detection_mode': True, + 'dynamic_range': 'high' + }, + 'radar_config': { + 'range': 200.0, + 'points_per_second': 2000, + 'horizontal_fov': 60.0, + 'vertical_fov': 20.0 } }, 'v2x': { 'enabled': True, 'communication_range': 300.0, - 'bandwidth': 10.0, - 'latency_mean': 0.05, - 'latency_std': 0.01, - 'packet_loss_rate': 0.01, - 'message_types': ['bsm', 'spat', 'map', 'rsm', 'perception', 'warning', 'pedestrian_warning'], - 'update_interval': 2.0, - 'security_enabled': False, - 'encryption_level': 'none', - 'qos_policy': 'best_effort', + 'bandwidth': 20.0, + 'latency_mean': 0.02, + 'latency_std': 0.005, + 'packet_loss_rate': 0.005, + 'message_types': ['bsm', 'spat', 'map', 'rsm', 'perception', + 'warning', 'pedestrian_warning', 'emergency_brake'], + 'update_interval': 1.0, + 'security_enabled': True, + 'encryption_level': 'basic', + 'qos_policy': 'priority_based', 'enable_safety_warnings': True, - 'pedestrian_warning_threshold': 10.0 + 'pedestrian_warning_threshold': 10.0, + 'emergency_brake_warning': True, + 'collision_prediction': True, + 'vulnerable_road_user_protection': True }, 'cooperative': { 'num_coop_vehicles': 2, 'enable_shared_perception': True, 'enable_traffic_warnings': True, 'enable_pedestrian_warnings': True, - 'enable_maneuver_coordination': False, + 'enable_emergency_brake_assist': True, + 'enable_maneuver_coordination': True, 'data_fusion_interval': 1.0, - 'max_shared_objects': 50, - 'object_matching_threshold': 5.0, - 'data_retention_time': 10.0, - 'consensus_method': 'simple' + 'max_shared_objects': 100, + 'object_matching_threshold': 3.0, + 'data_retention_time': 15.0, + 'consensus_method': 'weighted', + 'pedestrian_tracking': True, + 'intention_prediction': True, + 'risk_assessment': True }, 'enhancement': { 'enabled': True, @@ -467,53 +757,62 @@ def _get_default_config() -> Dict[str, Any]: 'save_enhanced': True, 'calibration_generation': True, 'enhanced_dir_name': 'enhanced', - 'methods': ['normalize', 'contrast', 'brightness', 'pedestrian_highlight', 'safety_warning'], + 'methods': ['normalize', 'contrast', 'brightness', 'pedestrian_highlight', + 'safety_warning', 'risk_visualization'], 'weather_effects': True, 'augmentation_level': 'medium', - 'pedestrian_safety_mode': True + 'pedestrian_safety_mode': True, + 'vulnerable_user_protection': True, + 'emergency_scenario_simulation': True }, 'performance': { - 'batch_size': 5, + 'batch_size': 8, 'enable_compression': True, - 'compression_level': 3, + 'compression_level': 4, 'enable_downsampling': True, 'enable_memory_cache': True, - 'max_cache_size': 50, + 'max_cache_size': 80, 'enable_async_processing': True, - 'max_workers': 2, + 'max_workers': 4, 'image_processing': { 'compress_images': True, - 'compression_quality': 85, + 'compression_quality': 90, 'resize_images': False, 'resize_dimensions': [640, 480], - 'format': 'jpg' + 'format': 'jpg', + 'progressive_encoding': True }, 'lidar_processing': { - 'batch_size': 10, + 'batch_size': 15, 'enable_compression': True, 'enable_downsampling': True, - 'max_points_per_frame': 50000, - 'memory_warning_threshold': 350, - 'max_batch_memory_mb': 50, + 'max_points_per_frame': 60000, + 'memory_warning_threshold': 450, + 'max_batch_memory_mb': 70, 'v2x_save_interval': 5, - 'compression_method': 'zlib' + 'compression_method': 'zlib', + 'parallel_processing': True }, 'fusion': { - 'fusion_cache_size': 100, + 'fusion_cache_size': 150, 'enable_cache': True, - 'compression_enabled': True + 'compression_enabled': True, + 'real_time_fusion': True }, - 'sensor_cleanup_timeout': 0.5, - 'frame_rate_limit': 5.0, - 'safety_monitoring_interval': 1.0, + 'sensor_cleanup_timeout': 0.3, + 'frame_rate_limit': 8.0, + 'safety_monitoring_interval': 0.5, + 'emergency_response_time': 0.1, 'memory_management': { - 'gc_interval': 50, + 'gc_interval': 40, 'max_memory_mb': 500, - 'early_stop_threshold': 400 + 'early_stop_threshold': 450, + 'memory_monitoring': True, + 'leak_detection': True } }, 'output': { - 'data_dir': 'cvips_dataset', + 'data_dir': 'cvips_dataset_enhanced', 'output_format': 'standard', 'save_raw': True, 'save_stitched': True, @@ -524,26 +823,34 @@ def _get_default_config() -> Dict[str, Any]: 'save_v2x_messages': True, 'save_enhanced': True, 'save_safety_reports': True, + 'save_risk_maps': True, + 'save_emergency_events': True, 'validate_data': True, 'run_analysis': True, 'run_quality_check': True, 'generate_summary': True, 'generate_safety_summary': True, + 'generate_risk_assessment': True, 'compression_enabled': True, - 'file_naming': 'sequential', - 'backup_original': False + 'file_naming': 'timestamp', + 'backup_original': True, + 'metadata_inclusion': 'full' }, 'monitoring': { 'enable_logging': True, 'log_level': 'INFO', - 'log_file': 'cvips.log', + 'log_file': 'cvips_enhanced.log', + 'log_rotation': True, + 'max_log_size': 100, 'enable_performance_monitor': True, - 'performance_log_interval': 10.0, + 'performance_log_interval': 5.0, 'enable_progress_bar': True, 'enable_real_time_stats': True, - 'stats_update_interval': 5.0, + 'stats_update_interval': 2.0, 'enable_safety_monitor': True, - 'safety_log_interval': 2.0 + 'safety_log_interval': 1.0, + 'enable_memory_monitor': True, + 'memory_log_interval': 10.0 }, 'debug': { 'enable_debug_mode': False, @@ -551,21 +858,29 @@ def _get_default_config() -> Dict[str, Any]: 'debug_dir': 'debug', 'print_config': False, 'validate_sensors': True, - 'test_mode': False + 'test_mode': False, + 'profiling': False, + 'traceback_limit': 5 }, 'metadata': { - 'version': '1.0.0', - 'author': 'CVIPS System', + 'version': '2.0.0', + 'author': 'CVIPS System - Enhanced', 'description': '行人安全增强数据采集配置', - 'created': '', - 'modified': '' + 'created': datetime.now().isoformat(), + 'modified': '', + 'pedestrian_safety_features': True, + 'compatibility': { + 'carla_version': '>=0.9.13', + 'python_version': '>=3.7' + } } } @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}") + print(f"⚠ 未知的预设配置: {preset_name}") return config preset = ConfigManager.PRESET_CONFIGS[preset_name] @@ -580,6 +895,11 @@ def _apply_preset(config: Dict[str, Any], preset_name: str) -> Dict[str, Any]: config = ConfigOptimizer.optimize_for_speed(config) elif optimization == 'safety': config = ConfigOptimizer.optimize_for_safety(config) + elif optimization == 'research': + config = ConfigOptimizer.optimize_for_research(config) + elif optimization == 'balanced': + # 平衡配置:中等质量,中等性能 + pass # 使用默认配置 elif optimization == 'custom' and 'settings' in preset: config = ConfigManager._deep_update(config, preset['settings']) @@ -587,6 +907,7 @@ 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: @@ -594,15 +915,22 @@ def _load_config_file(config_file: str, base_config: Dict[str, Any]) -> Dict[str else: user_config = json.load(f) - print(f"加载配置文件: {config_file}") + print(f"✓ 加载配置文件: {config_file}") return ConfigManager._deep_update(base_config, user_config) + except json.JSONDecodeError as e: + print(f"✗ JSON解析错误: {e}") + return base_config + except yaml.YAMLError as e: + print(f"✗ YAML解析错误: {e}") + return base_config except Exception as e: - print(f"配置文件加载错误: {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) @@ -612,39 +940,58 @@ 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: - config['scenario']['town'] = args.town - if hasattr(args, 'weather') and args.weather: - config['scenario']['weather'] = args.weather - if hasattr(args, 'time_of_day') and args.time_of_day: - config['scenario']['time_of_day'] = args.time_of_day - if hasattr(args, 'duration') and args.duration: - config['scenario']['duration'] = args.duration - 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 - - if hasattr(args, 'enable_v2x'): - config['v2x']['enabled'] = args.enable_v2x - - if hasattr(args, 'enable_enhancement'): - config['enhancement']['enabled'] = args.enable_enhancement - - if hasattr(args, 'enable_lidar'): - config['sensors']['lidar_sensors'] = 1 if args.enable_lidar else 0 - config['output']['save_lidar'] = args.enable_lidar + """合并命令行参数""" + arg_mappings = { + 'scenario': ('scenario', 'name'), + 'town': ('scenario', 'town'), + 'weather': ('scenario', 'weather'), + 'time_of_day': ('scenario', 'time_of_day'), + 'duration': ('scenario', 'duration'), + 'seed': ('scenario', 'seed'), + 'num_vehicles': ('traffic', 'background_vehicles'), + 'num_pedestrians': ('traffic', 'pedestrians'), + 'num_coop_vehicles': ('cooperative', 'num_coop_vehicles'), + 'capture_interval': ('sensors', 'capture_interval'), + 'batch_size': ('performance', 'batch_size'), + 'output_format': ('output', 'output_format'), + } + + bool_mappings = { + 'enable_v2x': ('v2x', 'enabled'), + 'enable_enhancement': ('enhancement', 'enabled'), + 'enable_lidar': ('sensors', 'lidar_sensors'), + 'enable_fusion': ('output', 'save_fusion'), + 'enable_cooperative': ('output', 'save_cooperative'), + 'enable_annotations': ('output', 'save_annotations'), + 'enable_safety_monitor': ('monitoring', 'enable_safety_monitor'), + 'enable_compression': ('performance', 'enable_compression'), + 'enable_downsampling': ('performance', 'enable_downsampling'), + 'skip_validation': ('output', 'validate_data'), + 'skip_quality_check': ('output', 'run_quality_check'), + 'run_analysis': ('output', 'run_analysis'), + } + + # 处理普通参数 + for arg_name, (section, key) in arg_mappings.items(): + if hasattr(args, arg_name) and getattr(args, arg_name) is not None: + if section in config and key in config[section]: + config[section][key] = getattr(args, arg_name) + + # 处理布尔参数 + for arg_name, (section, key) in bool_mappings.items(): + if hasattr(args, arg_name): + if arg_name.startswith('skip_'): + config[section][key] = not getattr(args, arg_name) + elif arg_name.startswith('enable_'): + value = getattr(args, arg_name) + if section == 'sensors' and key == 'lidar_sensors': + config[section][key] = 1 if value else 0 + else: + config[section][key] = value + + # 特殊处理 + if hasattr(args, 'enable_lidar') and args.enable_lidar: + config['output']['save_lidar'] = True if hasattr(args, 'enable_fusion'): config['output']['save_fusion'] = args.enable_fusion @@ -652,32 +999,6 @@ def merge_args(config: Dict[str, Any], args: argparse.Namespace) -> Dict[str, An 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 - - if hasattr(args, 'enable_compression'): - config['performance']['enable_compression'] = args.enable_compression - - 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 - if hasattr(args, 'enable_safety_monitor'): config['monitoring']['enable_safety_monitor'] = args.enable_safety_monitor @@ -685,32 +1006,41 @@ 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) + # 更新修改时间 + if 'metadata' in config: + config['metadata']['modified'] = datetime.now().isoformat() + 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) + yaml.dump(config, f, default_flow_style=False, allow_unicode=True, sort_keys=False) + print(f"✓ 配置保存为YAML: {output_path}") else: with open(output_path, 'w', encoding='utf-8') as f: - json.dump(config, f, indent=2, ensure_ascii=False) + json.dump(config, f, indent=2, ensure_ascii=False, sort_keys=False) + print(f"✓ 配置保存为JSON: {output_path}") - print(f"配置保存到: {output_path}") return True except Exception as e: - print(f"保存配置失败: {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 = ConfigManager.load_config(preset=preset, validate=False) config['metadata']['created'] = 'template' config['metadata']['description'] = f'配置模板 - {preset if preset else "通用"}' + config['metadata']['modified'] = '' return ConfigManager.save_config(config, output_path) @staticmethod def print_config_summary(config: Dict[str, Any]): + """打印配置摘要""" print("\n" + "=" * 60) print("配置摘要") print("=" * 60) @@ -721,6 +1051,7 @@ def print_config_summary(config: Dict[str, Any]): print(f" 地图: {scenario['town']}") print(f" 天气/时间: {scenario['weather']}/{scenario['time_of_day']}") print(f" 时长: {scenario['duration']}秒") + print(f" 行人安全模式: {'启用' if scenario.get('pedestrian_safety_mode', False) else '禁用'}") print(f" 随机种子: {scenario.get('seed', '随机')}") traffic = config['traffic'] @@ -728,8 +1059,10 @@ def print_config_summary(config: Dict[str, Any]): print(f" 主车: {traffic['ego_vehicles']}") print(f" 背景车辆: {traffic['background_vehicles']}") print(f" 行人: {traffic['pedestrians']}") + print(f" 自行车: {traffic.get('bicycles', 0)}") + print(f" 摩托车: {traffic.get('motorcycles', 0)}") print(f" 车速限制: {traffic.get('speed_limit', '无')} km/h") - print(f" 交通灯: {'启用' if traffic['traffic_lights'] else '禁用'}") + print(f" 行人安全区域: {'启用' if traffic.get('pedestrian_safety_zones', False) else '禁用'}") sensors = config['sensors'] print(f"\n📷 传感器:") @@ -738,6 +1071,8 @@ def print_config_summary(config: Dict[str, Any]): 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]}") + print( + f" 行人检测模式: {'启用' if sensors['camera_config'].get('pedestrian_detection_mode', False) else '禁用'}") v2x = config['v2x'] print(f"\n📡 V2X通信:") @@ -746,12 +1081,14 @@ def print_config_summary(config: Dict[str, Any]): print(f" 通信范围: {v2x['communication_range']}米") print(f" 更新间隔: {v2x['update_interval']}秒") print(f" 安全警告: {'启用' if v2x.get('enable_safety_warnings', False) else '禁用'}") + print(f" 紧急制动: {'启用' if v2x.get('emergency_brake_warning', False) else '禁用'}") coop = config['cooperative'] print(f"\n🤝 协同感知:") print(f" 协同车辆: {coop['num_coop_vehicles']}") print(f" 共享感知: {'启用' if coop['enable_shared_perception'] else '禁用'}") print(f" 行人警告: {'启用' if coop.get('enable_pedestrian_warnings', False) else '禁用'}") + print(f" 紧急制动辅助: {'启用' if coop.get('enable_emergency_brake_assist', False) else '禁用'}") perf = config['performance'] print(f"\n⚡ 性能:") @@ -760,6 +1097,7 @@ def print_config_summary(config: Dict[str, Any]): print(f" 下采样: {'启用' if perf['enable_downsampling'] else '禁用'}") print(f" 帧率限制: {perf['frame_rate_limit']} FPS") print(f" 安全监控间隔: {perf.get('safety_monitoring_interval', 1.0)}秒") + print(f" 紧急响应时间: {perf.get('emergency_response_time', 0.1)}秒") output = config['output'] print(f"\n💾 输出:") @@ -767,26 +1105,58 @@ def print_config_summary(config: Dict[str, Any]): 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(f" 启用输出: {', '.join(enabled_outputs[:8])}") + if len(enabled_outputs) > 8: + print(f" {', '.join(enabled_outputs[8:])}") - print(f"\n🛡️ 行人安全:") + print(f"\n🛡️ 行人安全增强:") print(f" 安全监控: {'启用' if config['monitoring'].get('enable_safety_monitor', False) else '禁用'}") print(f" 增强安全模式: {'启用' if config['enhancement'].get('pedestrian_safety_mode', False) else '禁用'}") + print(f" 脆弱用户保护: {'启用' if config['enhancement'].get('vulnerable_user_protection', False) else '禁用'}") + + print("\n📊 统计:") + total_objects = (traffic['ego_vehicles'] + traffic['background_vehicles'] + + traffic['pedestrians'] + traffic.get('bicycles', 0) + + traffic.get('motorcycles', 0)) + print(f" 场景总对象数: {total_objects}") + print(f" 预计数据量: {ConfigManager._estimate_data_size(config)}") print("=" * 60) + @staticmethod + def _estimate_data_size(config: Dict[str, Any]) -> str: + """估计数据大小""" + duration = config['scenario']['duration'] + interval = config['sensors']['capture_interval'] + frames = int(duration / interval) + + # 计算每帧数据大小 + image_size = config['sensors']['image_size'] + image_pixels = image_size[0] * image_size[1] + + # 粗略估计 + total_size_mb = frames * image_pixels * 3 / (1024 * 1024) * 0.1 # 假设压缩率 + + if total_size_mb < 1024: + return f"{total_size_mb:.1f} MB" + else: + return f"{total_size_mb / 1024:.1f} GB" + @staticmethod def list_presets(): + """列出可用预设""" print("\n可用预设配置:") - print("-" * 40) + print("-" * 50) for name, preset in ConfigManager.PRESET_CONFIGS.items(): - print(f" {name:15s} - {preset['description']}") - print("-" * 40) + print(f" {name:20s} - {preset['description']}") + print("-" * 50) -def load_config(config_file=None): - return ConfigManager.load_config(config_file) +def load_config(config_file=None, preset=None): + """加载配置(兼容函数)""" + return ConfigManager.load_config(config_file, preset) def merge_args(config, args): + """合并参数(兼容函数)""" return ConfigManager.merge_args(config, args) \ No newline at end of file diff --git a/src/enhance_pedestrian_safety/data_validator.py b/src/enhance_pedestrian_safety/data_validator.py index b472b40eb8..1292bc1852 100644 --- a/src/enhance_pedestrian_safety/data_validator.py +++ b/src/enhance_pedestrian_safety/data_validator.py @@ -1,14 +1,22 @@ import json import os +import numpy as np +from datetime import datetime class DataValidator: + """数据验证器(增强版)""" @staticmethod - def validate_dataset(data_dir): + def validate_dataset(data_dir, detailed=False): + """验证数据集(增强版)""" + print(f"\n{'=' * 60}") print(f"验证数据集: {data_dir}") + print(f"{'=' * 60}") validation_results = { + 'validation_time': datetime.now().isoformat(), + 'dataset_path': data_dir, 'directory_structure': DataValidator._check_directory_structure(data_dir), 'raw_images': DataValidator._validate_raw_images(data_dir), 'stitched_images': DataValidator._validate_stitched_images(data_dir), @@ -17,10 +25,21 @@ def validate_dataset(data_dir): 'lidar_data': DataValidator._validate_lidar_data(data_dir), 'cooperative_data': DataValidator._validate_cooperative_data(data_dir), 'fusion_data': DataValidator._validate_fusion_data(data_dir), - 'safety_data': DataValidator._validate_safety_data(data_dir) + 'safety_data': DataValidator._validate_safety_data(data_dir), + 'enhanced_data': DataValidator._validate_enhanced_data(data_dir), + 'calibration_data': DataValidator._validate_calibration_data(data_dir), + 'timestamps': DataValidator._validate_timestamps(data_dir) } + # 计算安全指标 + validation_results['safety_metrics'] = DataValidator._calculate_safety_metrics(data_dir) + validation_results['overall_score'] = DataValidator._calculate_score(validation_results) + validation_results['health_status'] = DataValidator._get_health_status(validation_results['overall_score']) + + # 详细验证 + if detailed: + validation_results['detailed_analysis'] = DataValidator._detailed_analysis(data_dir) DataValidator._save_validation_report(data_dir, validation_results) DataValidator._print_validation_report(validation_results) @@ -29,22 +48,27 @@ def validate_dataset(data_dir): @staticmethod def _check_directory_structure(data_dir): + """检查目录结构(增强版)""" required_dirs = [ - "raw/vehicle", + "raw/vehicle_1", "raw/infrastructure", "stitched", "metadata", - "cooperative", "cooperative/v2x_messages", "cooperative/shared_perception", - "fusion" + "fusion", + "annotations" ] optional_dirs = [ "lidar", "calibration", - "annotations", - "safety_reports" + "safety_reports", + "enhanced", + "v2xformer_format", + "kitti_format", + "risk_maps", + "emergency_events" ] missing_dirs = [] @@ -59,12 +83,28 @@ def _check_directory_structure(data_dir): if not os.path.exists(full_path): missing_optional.append(dir_path) + # 检查文件数量 + dir_stats = {} + total_files = 0 + for root, dirs, files in os.walk(data_dir): + if root.startswith(os.path.join(data_dir, '.')): + continue # 跳过隐藏目录 + + rel_path = os.path.relpath(root, data_dir) + if rel_path == '.': + rel_path = 'root' + + dir_stats[rel_path] = len(files) + total_files += len(files) + status = 'PASS' if len(missing_dirs) == 0 else 'FAIL' result = { 'status': status, 'missing_directories': missing_dirs, 'missing_optional_directories': missing_optional, + 'directory_stats': dir_stats, + 'total_files': total_files, 'required_directories': required_dirs, 'optional_directories': optional_dirs } @@ -73,11 +113,12 @@ def _check_directory_structure(data_dir): @staticmethod def _validate_raw_images(data_dir): + """验证原始图像(增强版)""" raw_path = os.path.join(data_dir, "raw") if not os.path.exists(raw_path): - return {'vehicle': {'status': 'MISSING', 'count': 0, 'errors': []}, - 'infrastructure': {'status': 'MISSING', 'count': 0, 'errors': []}} + return {'vehicle': {'status': 'MISSING', 'count': 0, 'errors': [], 'sizes': []}, + 'infrastructure': {'status': 'MISSING', 'count': 0, 'errors': [], 'sizes': []}} raw_dirs = [d for d in os.listdir(raw_path) if os.path.isdir(os.path.join(raw_path, d))] results = {} @@ -91,6 +132,7 @@ def _validate_raw_images(data_dir): total_images = 0 errors = [] + image_sizes = [] for camera_dir in camera_dirs: camera_path = os.path.join(path, camera_dir) @@ -99,8 +141,11 @@ def _validate_raw_images(data_dir): for img_file in images: img_path = os.path.join(camera_path, img_file) try: - if os.path.getsize(img_path) == 0: + file_size = os.path.getsize(img_path) + if file_size == 0: errors.append(f"空文件: {img_file}") + else: + image_sizes.append(file_size) except: errors.append(f"文件访问失败: {img_file}") @@ -108,34 +153,51 @@ def _validate_raw_images(data_dir): if len(errors) == 0 and total_images > 0: status = 'PASS' - elif len(errors) < 3 and total_images > 0: + elif len(errors) < 5 and total_images > 0: status = 'WARNING' else: status = 'FAIL' + # 计算统计信息 + stats = {} + if image_sizes: + stats = { + 'min_size_kb': min(image_sizes) / 1024, + 'max_size_kb': max(image_sizes) / 1024, + 'avg_size_kb': np.mean(image_sizes) / 1024, + 'total_size_mb': sum(image_sizes) / (1024 * 1024) + } + results[raw_dir] = { 'status': status, 'count': total_images, - 'errors': errors + 'camera_count': len(camera_dirs), + 'errors': errors, + 'statistics': stats } return results @staticmethod def _validate_stitched_images(data_dir): + """验证拼接图像(增强版)""" stitched_dir = os.path.join(data_dir, "stitched") if not os.path.exists(stitched_dir): - return {'status': 'MISSING', 'count': 0, 'errors': []} + return {'status': 'MISSING', 'count': 0, 'errors': [], 'statistics': {}} images = [f for f in os.listdir(stitched_dir) if f.endswith(('.jpg', '.jpeg', '.png'))] errors = [] + image_sizes = [] for img_file in images: img_path = os.path.join(stitched_dir, img_file) try: - if os.path.getsize(img_path) == 0: + file_size = os.path.getsize(img_path) + if file_size == 0: errors.append(f"空文件: {img_file}") + else: + image_sizes.append(file_size) except: errors.append(f"文件访问失败: {img_file}") @@ -146,22 +208,35 @@ def _validate_stitched_images(data_dir): else: status = 'FAIL' + # 计算统计信息 + stats = {} + if image_sizes: + stats = { + 'min_size_kb': min(image_sizes) / 1024, + 'max_size_kb': max(image_sizes) / 1024, + 'avg_size_kb': np.mean(image_sizes) / 1024, + 'total_size_mb': sum(image_sizes) / (1024 * 1024) + } + return { 'status': status, 'count': len(images), - 'errors': errors + 'errors': errors, + 'statistics': stats } @staticmethod def _validate_annotations(data_dir): + """验证标注文件(增强版)""" annotations_dir = os.path.join(data_dir, "annotations") if not os.path.exists(annotations_dir): - return {'status': 'MISSING', 'count': 0, 'errors': []} + return {'status': 'MISSING', 'count': 0, 'errors': [], 'frame_range': []} json_files = [f for f in os.listdir(annotations_dir) if f.endswith('.json')] errors = [] valid_files = 0 + frame_ids = [] for json_file in json_files: json_path = os.path.join(annotations_dir, json_file) @@ -170,44 +245,80 @@ def _validate_annotations(data_dir): data = json.load(f) # 检查基本结构 - required_keys = ['frame_id', 'objects'] + required_keys = ['frame_id', 'objects', 'safety_info'] for key in required_keys: if key not in data: errors.append(f"缺失必要键: {key} in {json_file}") + # 检查安全信息 + if 'safety_info' in data: + safety_info = data['safety_info'] + if 'pedestrian_count' not in safety_info or 'vehicle_count' not in safety_info: + errors.append(f"缺失安全统计信息: {json_file}") + + # 记录帧ID + if 'frame_id' in data: + frame_ids.append(data['frame_id']) + valid_files += 1 except Exception as e: errors.append(f"无效的JSON文件: {json_file} - {str(e)}") if len(errors) == 0 and valid_files > 0: status = 'PASS' - elif len(errors) < 3 and valid_files > 0: + elif len(errors) < 5 and valid_files > 0: status = 'WARNING' else: status = 'FAIL' + # 计算帧范围 + frame_range = [] + if frame_ids: + frame_range = [min(frame_ids), max(frame_ids)] + return { 'status': status, 'count': valid_files, + 'total_files': len(json_files), + 'frame_range': frame_range, 'errors': errors } @staticmethod def _validate_metadata(data_dir): + """验证元数据(增强版)""" metadata_dir = os.path.join(data_dir, "metadata") if not os.path.exists(metadata_dir): - return {'status': 'MISSING', 'count': 0, 'errors': []} + return {'status': 'MISSING', 'count': 0, 'errors': [], 'files': []} json_files = [f for f in os.listdir(metadata_dir) if f.endswith('.json')] errors = [] valid_files = 0 + file_details = [] + + required_files = ['collection_info.json', 'scene_description.json'] + missing_files = [] + + for req_file in required_files: + if req_file not in json_files: + missing_files.append(req_file) for json_file in json_files: json_path = os.path.join(metadata_dir, json_file) try: with open(json_path, 'r') as f: data = json.load(f) + + # 检查文件大小 + file_size = os.path.getsize(json_path) + + file_details.append({ + 'file': json_file, + 'size_kb': file_size / 1024, + 'keys': list(data.keys()) + }) + valid_files += 1 except Exception as e: errors.append(f"无效的JSON文件: {json_file} - {str(e)}") @@ -219,18 +330,24 @@ def _validate_metadata(data_dir): else: status = 'FAIL' + if missing_files: + errors.extend([f"缺失必要元数据文件: {f}" for f in missing_files]) + return { 'status': status, 'count': valid_files, + 'files': file_details, + 'missing_files': missing_files, 'errors': errors } @staticmethod def _validate_lidar_data(data_dir): + """验证LiDAR数据(增强版)""" lidar_dir = os.path.join(data_dir, "lidar") if not os.path.exists(lidar_dir): - return {'status': 'MISSING', 'count': 0, 'errors': []} + return {'status': 'MISSING', 'count': 0, 'errors': [], 'statistics': {}} bin_files = [f for f in os.listdir(lidar_dir) if f.endswith('.bin')] npy_files = [f for f in os.listdir(lidar_dir) if f.endswith('.npy')] @@ -238,12 +355,15 @@ def _validate_lidar_data(data_dir): errors = [] valid_bin_files = 0 + file_sizes = [] for bin_file in bin_files: bin_path = os.path.join(lidar_dir, bin_file) try: - if os.path.getsize(bin_path) > 0: + file_size = os.path.getsize(bin_path) + if file_size > 0: valid_bin_files += 1 + file_sizes.append(file_size) else: errors.append(f"空文件: {bin_file}") except: @@ -253,23 +373,35 @@ def _validate_lidar_data(data_dir): if len(errors) == 0 and valid_bin_files > 0: status = 'PASS' - elif len(errors) < 3 and valid_bin_files > 0: + elif len(errors) < 5 and valid_bin_files > 0: status = 'WARNING' else: status = 'FAIL' + # 计算统计信息 + stats = {} + if file_sizes: + stats = { + 'min_size_mb': min(file_sizes) / (1024 * 1024), + 'max_size_mb': max(file_sizes) / (1024 * 1024), + 'avg_size_mb': np.mean(file_sizes) / (1024 * 1024), + 'total_size_gb': sum(file_sizes) / (1024 * 1024 * 1024) + } + return { 'status': status, 'count': total_files, 'bin_files': len(bin_files), 'npy_files': len(npy_files), 'json_files': len(json_files), - 'errors': errors + 'valid_bin_files': valid_bin_files, + 'errors': errors, + 'statistics': stats } @staticmethod def _validate_cooperative_data(data_dir): - """验证协同数据""" + """验证协同数据(增强版)""" coop_dir = os.path.join(data_dir, "cooperative") if not os.path.exists(coop_dir): @@ -280,9 +412,13 @@ def _validate_cooperative_data(data_dir): # 检查V2X消息目录 v2x_dir = os.path.join(coop_dir, "v2x_messages") v2x_files = [] + v2x_stats = {'total': 0, 'types': {}} + if os.path.exists(v2x_dir): v2x_files = [f for f in os.listdir(v2x_dir) if f.endswith('.json')] - for v2x_file in v2x_files[:min(5, len(v2x_files))]: # 检查前5个文件 + v2x_stats['total'] = len(v2x_files) + + for v2x_file in v2x_files[:min(10, len(v2x_files))]: try: with open(os.path.join(v2x_dir, v2x_file), 'r') as f: data = json.load(f) @@ -292,6 +428,12 @@ def _validate_cooperative_data(data_dir): for key in required_keys: if key not in data: errors.append(f"V2X消息缺失字段 {key}: {v2x_file}") + + # 统计消息类型 + if 'message' in data and 'message_type' in data['message']: + msg_type = data['message']['message_type'] + v2x_stats['types'][msg_type] = v2x_stats['types'].get(msg_type, 0) + 1 + except Exception as e: errors.append(f"V2X消息文件无效: {v2x_file} - {str(e)}") else: @@ -300,9 +442,14 @@ def _validate_cooperative_data(data_dir): # 检查共享感知目录 perception_dir = os.path.join(coop_dir, "shared_perception") perception_files = [] + perception_stats = {'total': 0, 'avg_objects': 0} + if os.path.exists(perception_dir): perception_files = [f for f in os.listdir(perception_dir) if f.endswith('.json')] - for perception_file in perception_files[:min(5, len(perception_files))]: + perception_stats['total'] = len(perception_files) + + object_counts = [] + for perception_file in perception_files[:min(10, len(perception_files))]: try: with open(os.path.join(perception_dir, perception_file), 'r') as f: data = json.load(f) @@ -312,13 +459,22 @@ def _validate_cooperative_data(data_dir): for key in required_keys: if key not in data: errors.append(f"共享感知文件缺失字段 {key}: {perception_file}") + + # 统计对象数量 + if 'shared_objects' in data: + object_counts.append(len(data['shared_objects'])) + except Exception as e: errors.append(f"共享感知文件无效: {perception_file} - {str(e)}") + + if object_counts: + perception_stats['avg_objects'] = np.mean(object_counts) else: errors.append("共享感知目录不存在") # 检查协同摘要 summary_file = os.path.join(coop_dir, "cooperative_summary.json") + summary_valid = False if not os.path.exists(summary_file): errors.append("协同摘要文件不存在") else: @@ -331,14 +487,16 @@ def _validate_cooperative_data(data_dir): for key in required_keys: if key not in data: errors.append(f"协同摘要缺失字段: {key}") + + summary_valid = True except Exception as e: errors.append(f"协同摘要文件无效: {str(e)}") total_files = len(v2x_files) + len(perception_files) - if len(errors) == 0 and total_files > 0: + if len(errors) == 0 and total_files > 0 and summary_valid: status = 'PASS' - elif len(errors) < 3 and total_files > 0: + elif len(errors) < 5 and total_files > 0: status = 'WARNING' else: status = 'FAIL' @@ -346,28 +504,39 @@ def _validate_cooperative_data(data_dir): return { 'status': status, 'count': total_files, - 'v2x_messages': len(v2x_files), - 'shared_perception': len(perception_files), + 'v2x_messages': v2x_stats, + 'shared_perception': perception_stats, + 'summary_valid': summary_valid, 'errors': errors } @staticmethod def _validate_fusion_data(data_dir): - """验证融合数据""" + """验证融合数据(增强版)""" fusion_dir = os.path.join(data_dir, "fusion") if not os.path.exists(fusion_dir): - return {'status': 'MISSING', 'count': 0, 'errors': []} + return {'status': 'MISSING', 'count': 0, 'errors': [], 'statistics': {}} json_files = [f for f in os.listdir(fusion_dir) if f.endswith('.json')] + gz_files = [f for f in os.listdir(fusion_dir) if f.endswith('.gz')] + all_files = json_files + gz_files + errors = [] valid_files = 0 + sensor_types = set() - for json_file in json_files[:min(5, len(json_files))]: # 检查前5个文件 + for json_file in all_files[:min(10, len(all_files))]: json_path = os.path.join(fusion_dir, json_file) try: - with open(json_path, 'r') as f: - data = json.load(f) + # 处理压缩文件 + if json_file.endswith('.gz'): + import gzip + with gzip.open(json_path, 'rt', encoding='utf-8') as f: + data = json.load(f) + else: + with open(json_path, 'r') as f: + data = json.load(f) # 检查必要字段 required_keys = ['frame_id', 'timestamp', 'sensors'] @@ -375,36 +544,45 @@ def _validate_fusion_data(data_dir): if key not in data: errors.append(f"融合文件缺失字段 {key}: {json_file}") + # 收集传感器类型 + if 'sensors' in data: + sensor_types.update(data['sensors'].keys()) + valid_files += 1 except Exception as e: errors.append(f"融合文件无效: {json_file} - {str(e)}") if len(errors) == 0 and valid_files > 0: status = 'PASS' - elif len(errors) < 3 and valid_files > 0: + elif len(errors) < 5 and valid_files > 0: status = 'WARNING' else: status = 'FAIL' return { 'status': status, - 'count': len(json_files), + 'count': len(all_files), + 'json_files': len(json_files), + 'gz_files': len(gz_files), + 'valid_files': valid_files, + 'sensor_types': list(sensor_types), 'errors': errors } @staticmethod def _validate_safety_data(data_dir): - """验证安全数据""" + """验证安全数据(增强版)""" safety_dir = os.path.join(data_dir, "safety_reports") if not os.path.exists(safety_dir): - return {'status': 'MISSING', 'count': 0, 'errors': []} + return {'status': 'MISSING', 'count': 0, 'errors': [], 'risk_stats': {}} json_files = [f for f in os.listdir(safety_dir) if f.endswith('.json')] errors = [] valid_files = 0 + risk_stats = {'high': 0, 'medium': 0, 'low': 0, 'critical': 0} - for json_file in json_files[:min(5, len(json_files))]: + for json_file in json_files[:min(10, len(json_files))]: json_path = os.path.join(safety_dir, json_file) try: with open(json_path, 'r') as f: @@ -416,13 +594,19 @@ def _validate_safety_data(data_dir): if key not in data: errors.append(f"安全报告缺失字段 {key}: {json_file}") + # 统计风险级别 + if 'risk_level' in data: + risk_level = data['risk_level'] + if risk_level in risk_stats: + risk_stats[risk_level] += 1 + valid_files += 1 except Exception as e: errors.append(f"安全报告无效: {json_file} - {str(e)}") if len(errors) == 0 and valid_files > 0: status = 'PASS' - elif len(errors) < 3 and valid_files > 0: + elif len(errors) < 5 and valid_files > 0: status = 'WARNING' else: status = 'FAIL' @@ -430,21 +614,195 @@ def _validate_safety_data(data_dir): return { 'status': status, 'count': len(json_files), + 'valid_files': valid_files, + 'risk_stats': risk_stats, + 'errors': errors + } + + @staticmethod + def _validate_enhanced_data(data_dir): + """验证增强数据""" + enhanced_dir = os.path.join(data_dir, "enhanced") + + if not os.path.exists(enhanced_dir): + return {'status': 'MISSING', 'count': 0, 'errors': []} + + # 查找所有增强图像 + enhanced_files = [] + for root, dirs, files in os.walk(enhanced_dir): + for file in files: + if file.endswith(('.png', '.jpg', '.jpeg')): + enhanced_files.append(os.path.join(root, file)) + + errors = [] + + # 检查样本文件 + sample_files = enhanced_files[:min(5, len(enhanced_files))] + for file_path in sample_files: + try: + if os.path.getsize(file_path) == 0: + errors.append(f"空增强文件: {os.path.basename(file_path)}") + except: + errors.append(f"文件访问失败: {os.path.basename(file_path)}") + + if len(errors) == 0 and len(enhanced_files) > 0: + status = 'PASS' + elif len(errors) < 3 and len(enhanced_files) > 0: + status = 'WARNING' + else: + status = 'FAIL' + + return { + 'status': status, + 'count': len(enhanced_files), + 'errors': errors + } + + @staticmethod + def _validate_calibration_data(data_dir): + """验证标定数据""" + calib_dir = os.path.join(data_dir, "calibration") + + if not os.path.exists(calib_dir): + return {'status': 'MISSING', 'count': 0, 'errors': []} + + json_files = [f for f in os.listdir(calib_dir) if f.endswith('.json')] + errors = [] + valid_files = 0 + + for json_file in json_files: + json_path = os.path.join(calib_dir, json_file) + try: + with open(json_path, 'r') as f: + data = json.load(f) + valid_files += 1 + except Exception as e: + errors.append(f"标定文件无效: {json_file} - {str(e)}") + + if len(errors) == 0 and valid_files > 0: + status = 'PASS' + elif len(errors) < 3 and valid_files > 0: + status = 'WARNING' + else: + status = 'FAIL' + + return { + 'status': status, + 'count': valid_files, 'errors': errors } + @staticmethod + def _validate_timestamps(data_dir): + """验证时间戳连续性""" + annotations_dir = os.path.join(data_dir, "annotations") + + if not os.path.exists(annotations_dir): + return {'status': 'SKIPPED', 'errors': ['标注目录不存在']} + + json_files = [f for f in os.listdir(annotations_dir) if f.endswith('.json')] + if len(json_files) < 2: + return {'status': 'SKIPPED', 'errors': ['标注文件不足']} + + timestamps = [] + errors = [] + + # 按文件名排序 + json_files.sort() + + for json_file in json_files[:min(100, len(json_files))]: + json_path = os.path.join(annotations_dir, json_file) + try: + with open(json_path, 'r') as f: + data = json.load(f) + + if 'timestamp' in data: + timestamps.append(data['timestamp']) + elif 'frame_id' in data: + timestamps.append(data['frame_id']) + except: + continue + + if len(timestamps) < 2: + return {'status': 'SKIPPED', 'errors': ['有效时间戳不足']} + + # 检查时间戳是否递增 + for i in range(1, len(timestamps)): + if isinstance(timestamps[i], (int, float)) and isinstance(timestamps[i - 1], (int, float)): + if timestamps[i] <= timestamps[i - 1]: + errors.append(f"时间戳不递增: {timestamps[i - 1]} -> {timestamps[i]}") + + if len(errors) == 0: + status = 'PASS' + elif len(errors) < 3: + status = 'WARNING' + else: + status = 'FAIL' + + return { + 'status': status, + 'timestamp_count': len(timestamps), + 'errors': errors + } + + @staticmethod + def _calculate_safety_metrics(data_dir): + """计算安全指标""" + safety_dir = os.path.join(data_dir, "safety_reports") + + if not os.path.exists(safety_dir): + return {'status': 'MISSING', 'metrics': {}} + + json_files = [f for f in os.listdir(safety_dir) if f.endswith('.json')] + + total_high_risk = 0 + total_interactions = 0 + safety_scores = [] + + for json_file in json_files[:min(20, len(json_files))]: + json_path = os.path.join(safety_dir, json_file) + try: + with open(json_path, 'r') as f: + data = json.load(f) + + if 'high_risk_cases' in data: + total_high_risk += data['high_risk_cases'] + if 'total_interactions' in data: + total_interactions += data['total_interactions'] + if 'safety_score' in data: + safety_scores.append(data['safety_score']) + except: + continue + + metrics = {} + if total_interactions > 0: + metrics['high_risk_ratio'] = total_high_risk / total_interactions + metrics['interaction_density'] = total_interactions / len(json_files) if json_files else 0 + + if safety_scores: + metrics['avg_safety_score'] = np.mean(safety_scores) + metrics['min_safety_score'] = min(safety_scores) + metrics['max_safety_score'] = max(safety_scores) + + return {'status': 'CALCULATED', 'metrics': metrics} + @staticmethod def _calculate_score(results): + """计算总体评分(增强版)""" weights = { 'directory_structure': 0.10, 'raw_images': 0.15, 'stitched_images': 0.05, - 'annotations': 0.08, + 'annotations': 0.10, 'metadata': 0.08, - 'lidar_data': 0.12, - 'cooperative_data': 0.12, - 'fusion_data': 0.10, - 'safety_data': 0.20 + 'lidar_data': 0.10, + 'cooperative_data': 0.10, + 'fusion_data': 0.08, + 'safety_data': 0.12, + 'enhanced_data': 0.04, + 'calibration_data': 0.03, + 'timestamps': 0.03, + 'safety_metrics': 0.02 } score = 0 @@ -462,64 +820,191 @@ def _calculate_score(results): score += 100 * weight elif result['status'] == 'WARNING': score += 70 * weight - elif result['status'] in ['FAIL', 'MISSING']: + elif result['status'] == 'FAIL': score += 30 * weight + elif result['status'] == 'MISSING': + score += 20 * weight + elif result['status'] == 'SKIPPED': + score += 50 * weight else: score += 50 * weight - return round(score, 1) + return round(min(100, max(0, score)), 1) + + @staticmethod + def _get_health_status(score): + """获取健康状态""" + if score >= 90: + return 'EXCELLENT' + elif score >= 75: + return 'GOOD' + elif score >= 60: + return 'FAIR' + elif score >= 40: + return 'POOR' + else: + return 'CRITICAL' + + @staticmethod + def _detailed_analysis(data_dir): + """详细分析""" + analysis = { + 'file_size_distribution': DataValidator._analyze_file_sizes(data_dir), + 'data_consistency': DataValidator._check_data_consistency(data_dir), + 'completeness': DataValidator._check_completeness(data_dir) + } + return analysis + + @staticmethod + def _analyze_file_sizes(data_dir): + """分析文件大小分布""" + size_ranges = { + 'tiny': 0, # < 1KB + 'small': 0, # 1KB - 100KB + 'medium': 0, # 100KB - 1MB + 'large': 0, # 1MB - 10MB + 'huge': 0 # > 10MB + } + + total_size = 0 + file_count = 0 + + for root, dirs, files in os.walk(data_dir): + for file in files: + file_path = os.path.join(root, file) + try: + size = os.path.getsize(file_path) + total_size += size + file_count += 1 + + if size < 1024: + size_ranges['tiny'] += 1 + elif size < 1024 * 100: + size_ranges['small'] += 1 + elif size < 1024 * 1024: + size_ranges['medium'] += 1 + elif size < 1024 * 1024 * 10: + size_ranges['large'] += 1 + else: + size_ranges['huge'] += 1 + except: + continue + + return { + 'total_files': file_count, + 'total_size_gb': total_size / (1024 ** 3), + 'avg_file_size_kb': (total_size / max(1, file_count)) / 1024, + 'size_distribution': size_ranges + } + + @staticmethod + def _check_data_consistency(data_dir): + """检查数据一致性""" + # 检查标注文件和图像文件数量是否匹配 + annotations_dir = os.path.join(data_dir, "annotations") + raw_dir = os.path.join(data_dir, "raw") + + annotation_count = 0 + image_count = 0 + + if os.path.exists(annotations_dir): + annotation_count = len([f for f in os.listdir(annotations_dir) if f.endswith('.json')]) + + if os.path.exists(raw_dir): + for root, dirs, files in os.walk(raw_dir): + for file in files: + if file.endswith(('.png', '.jpg', '.jpeg')): + image_count += 1 + + consistency = abs(annotation_count - image_count) <= max(annotation_count, image_count) * 0.1 + + return { + 'annotation_count': annotation_count, + 'image_count': image_count, + 'consistency': consistency, + 'difference': abs(annotation_count - image_count) + } + + @staticmethod + def _check_completeness(data_dir): + """检查数据完整性""" + completeness = {} + + # 检查必要目录 + required_dirs = ['raw', 'annotations', 'metadata'] + for dir_name in required_dirs: + dir_path = os.path.join(data_dir, dir_name) + completeness[dir_name] = os.path.exists(dir_path) and len(os.listdir(dir_path)) > 0 + + # 检查必要文件 + metadata_dir = os.path.join(data_dir, "metadata") + if os.path.exists(metadata_dir): + required_files = ['collection_info.json'] + for file_name in required_files: + file_path = os.path.join(metadata_dir, file_name) + completeness[file_name] = os.path.exists(file_path) + + return completeness @staticmethod def _save_validation_report(data_dir, results): + """保存验证报告""" report_path = os.path.join(data_dir, "metadata", "validation_report.json") + os.makedirs(os.path.dirname(report_path), exist_ok=True) + with open(report_path, 'w', encoding='utf-8') as f: json.dump(results, f, indent=2, ensure_ascii=False) + print(f"\n验证报告已保存: {report_path}") + @staticmethod def _print_validation_report(results): + """打印验证报告""" print("\n" + "=" * 60) print("数据集验证报告") print("=" * 60) + # 总体信息 + print(f"\n📊 总体信息:") + print(f" 数据集路径: {results['dataset_path']}") + print(f" 验证时间: {results['validation_time']}") + print(f" 总体评分: {results['overall_score']}/100") + print(f" 健康状态: {results['health_status']}") + + # 各模块验证结果 + print(f"\n🔍 详细验证结果:") + for key, result in results.items(): - if key == 'overall_score': + if key in ['overall_score', 'health_status', 'dataset_path', 'validation_time', + 'safety_metrics', 'detailed_analysis']: continue print(f"\n{key.replace('_', ' ').title()}:") if 'status' in result: - status_icon = '✓' if result['status'] == 'PASS' else '⚠' if result['status'] == 'WARNING' else '✗' + status_icon = '✓' if result['status'] == 'PASS' else '⚠' if result['status'] in ['WARNING', + 'SKIPPED'] else '✗' print(f" 状态: {status_icon} {result['status']}") - else: - print(f" 状态: 未知") if 'count' in result: - print(f" 数量: {result['count']}") + if isinstance(result['count'], dict): + print(f" 统计: {json.dumps(result['count'], indent=2)}") + else: + print(f" 数量: {result['count']}") # 特定类型数据的额外信息 if key == 'raw_images' and isinstance(result, dict): for subkey, subresult in result.items(): if isinstance(subresult, dict) and 'count' in subresult: print(f" {subkey}: {subresult['count']} 图像") - - if key == 'cooperative_data' and isinstance(result, dict): - if 'v2x_messages' in result: - print(f" V2X消息: {result['v2x_messages']}") - if 'shared_perception' in result: - print(f" 共享感知: {result['shared_perception']}") - - if key == 'lidar_data' and isinstance(result, dict): - if 'bin_files' in result: - print(f" BIN文件: {result['bin_files']}") - if 'npy_files' in result: - print(f" NPY文件: {result['npy_files']}") - if 'json_files' in result: - print(f" JSON文件: {result['json_files']}") + if 'statistics' in subresult and subresult['statistics']: + stats = subresult['statistics'] + print(f" 大小: {stats.get('avg_size_kb', 0):.1f} KB/张") if key == 'safety_data' and isinstance(result, dict): - if 'count' in result: - print(f" 安全报告: {result['count']} 个") + if 'risk_stats' in result: + print(f" 风险统计: {json.dumps(result['risk_stats'], indent=2)}") if 'errors' in result and result['errors']: print(f" 错误 ({len(result['errors'])}):") @@ -528,16 +1013,33 @@ def _print_validation_report(results): if len(result['errors']) > 3: print(f" ... 还有 {len(result['errors']) - 3} 个错误") - print(f"\n总体评分: {results.get('overall_score', 0)}/100") + # 安全指标 + if 'safety_metrics' in results and results['safety_metrics']['status'] != 'MISSING': + print(f"\n🛡️ 安全指标:") + metrics = results['safety_metrics']['metrics'] + for key, value in metrics.items(): + if isinstance(value, float): + print(f" {key}: {value:.3f}") + else: + print(f" {key}: {value}") + # 建议 + print(f"\n💡 建议:") overall_score = results.get('overall_score', 0) if overall_score >= 90: - print("✓ 数据集质量优秀") - elif overall_score >= 70: - print("✓ 数据集质量良好") - elif overall_score >= 50: - print("⚠ 数据集质量一般") + print(" ✓ 数据集质量优秀,可直接使用") + elif overall_score >= 75: + print(" ✓ 数据集质量良好,建议进行少量优化") + elif overall_score >= 60: + print(" ⚠ 数据集质量一般,建议进行优化") + if results.get('directory_structure', {}).get('missing_directories'): + print(" - 补全缺失的必要目录") + if results.get('raw_images', {}).get('vehicle', {}).get('count', 0) < 10: + print(" - 增加车辆图像数量") else: - print("✗ 数据集质量需要改进") + print(" ✗ 数据集质量需要重大改进") + print(" - 检查数据采集过程") + print(" - 验证传感器配置") + print(" - 重新收集关键数据") - print("=" * 60) \ No newline at end of file + print("\n" + "=" * 60) \ 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 8c0f6ebb20..a6bf33c30d 100644 --- a/src/enhance_pedestrian_safety/lidar_processor.py +++ b/src/enhance_pedestrian_safety/lidar_processor.py @@ -8,9 +8,13 @@ import zlib import pickle import gzip +import time +from concurrent.futures import ThreadPoolExecutor +import hashlib class LidarProcessor: + """LiDAR处理器(增强版)""" def __init__(self, output_dir, config=None): self.output_dir = output_dir @@ -21,36 +25,67 @@ def __init__(self, output_dir, config=None): os.makedirs(self.calibration_dir, exist_ok=True) self.frame_counter = 0 - self.data_lock = threading.Lock() + self.data_lock = threading.RLock() + self.processing_lock = threading.Lock() - self.batch_size = config.get('batch_size', 10) if config else 10 + # 配置参数 + self.config = config or {} + self.batch_size = self.config.get('batch_size', 8) 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 + self.enable_compression = self.config.get('enable_compression', True) + self.compression_level = self.config.get('compression_level', 4) + + self.max_points_per_frame = self.config.get('max_points_per_frame', 60000) + self.enable_downsampling = self.config.get('enable_downsampling', True) + self.downsample_ratio = self.config.get('downsample_ratio', 0.3) + + self.memory_warning_threshold = self.config.get('memory_warning_threshold', 400) + self.max_batch_memory_mb = self.config.get('max_batch_memory_mb', 60) + self.v2x_save_interval = self.config.get('v2x_save_interval', 5) + + # 性能监控 + self.processing_stats = { + 'total_frames': 0, + 'total_points': 0, + 'avg_points_per_frame': 0, + 'processing_times': [], + 'compression_ratios': [], + 'errors': 0 + } - 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.thread_pool = ThreadPoolExecutor(max_workers=2) - 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.pedestrian_detection = self.config.get('pedestrian_detection', True) + self.pedestrian_points = [] self._init_calibration_files() def _init_calibration_files(self): + """初始化标定文件(增强版)""" + # LiDAR内参 lidar_intrinsic = { + "sensor_type": "lidar", + "model": "velodyne_vls_128", "channels": 32, - "range": 100.0, - "points_per_second": 56000, + "range": 120.0, + "points_per_second": 100000, "rotation_frequency": 10.0, "horizontal_fov": 360.0, "vertical_fov": 30.0, - "upper_fov": 10.0, - "lower_fov": -20.0 + "upper_fov": 15.0, + "lower_fov": -25.0, + "angular_resolution": 0.1, + "timestamp": datetime.now().isoformat(), + "pedestrian_detection": self.pedestrian_detection, + "height_filter": [-0.5, 2.5] } + # LiDAR外参(车辆坐标系) lidar_extrinsic = { + "sensor_id": "lidar_main", + "vehicle_id": "ego_vehicle_1", "translation": [0.0, 0.0, 2.5], "rotation": [0.0, 0.0, 0.0], "matrix": [ @@ -58,47 +93,94 @@ def _init_calibration_files(self): [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0] - ] + ], + "timestamp": datetime.now().isoformat(), + "coordinate_system": "carla_vehicle" + } + + # 相机-LiDAR标定 + camera_lidar_calib = { + "camera_id": "front_wide", + "lidar_id": "lidar_main", + "projection_matrix": [ + [7.215377e+02, 0.000000e+00, 6.095593e+02, 0.000000e+00], + [0.000000e+00, 7.215377e+02, 1.728540e+02, 0.000000e+00], + [0.000000e+00, 0.000000e+00, 1.000000e+00, 0.000000e+00] + ], + "rectification_matrix": [ + [9.999239e-01, 9.837760e-03, -7.445048e-03], + [-9.869795e-03, 9.999421e-01, -4.278459e-03], + [7.402527e-03, 4.351614e-03, 9.999631e-01] + ], + "translation": [0.0, 0.0, 0.0], + "rotation": [0.0, 0.0, 0.0] } intrinsic_file = os.path.join(self.calibration_dir, "lidar_intrinsic.json") extrinsic_file = os.path.join(self.calibration_dir, "lidar_extrinsic.json") + camera_lidar_file = os.path.join(self.calibration_dir, "camera_lidar_calibration.json") - with open(intrinsic_file, 'w') as f: + with open(intrinsic_file, 'w', encoding='utf-8') as f: json.dump(lidar_intrinsic, f, indent=2) - with open(extrinsic_file, 'w') as f: + with open(extrinsic_file, 'w', encoding='utf-8') as f: json.dump(lidar_extrinsic, f, indent=2) + with open(camera_lidar_file, 'w', encoding='utf-8') as f: + json.dump(camera_lidar_calib, f, indent=2) + def process_lidar_data(self, lidar_data, frame_num): - with self.data_lock: + """处理LiDAR数据(增强版)""" + start_time = time.time() + + with self.processing_lock: try: self.frame_counter = frame_num + # 转换数据格式 points = self._carla_lidar_to_numpy(lidar_data) if points is None or points.shape[0] == 0: return None + # 检查内存使用 if self._check_memory_usage(): return None original_count = points.shape[0] + + # 预处理:过滤地面和过高点 + points = self._preprocess_point_cloud(points) + + # 行人检测 + if self.pedestrian_detection: + pedestrian_clusters = self._detect_pedestrians(points) + if pedestrian_clusters: + self._save_pedestrian_detections(frame_num, pedestrian_clusters) + + # 下采样 if self.enable_downsampling and points.shape[0] > self.max_points_per_frame: points = self._downsample_point_cloud(points) + # 检查批处理内存 batch_memory_mb = self._estimate_batch_memory_mb() if batch_memory_mb > self.max_batch_memory_mb: - self._save_batch() + self._save_batch_async() - self.point_cloud_batch.append((frame_num, points)) + # 添加到批处理 + with self.data_lock: + self.point_cloud_batch.append((frame_num, points)) + self.processing_stats['total_frames'] += 1 + self.processing_stats['total_points'] += points.shape[0] + # 异步保存 if len(self.point_cloud_batch) >= self.batch_size: - self._save_batch() + self._save_batch_async() - bin_path = self._save_as_bin(points, frame_num) - npy_path = self._save_as_npy(points, frame_num) + # 保存单个文件 + save_results = self._save_point_cloud_files(points, frame_num) + # 每N帧保存V2X格式 v2xformer_path = None if frame_num % self.v2x_save_interval == 0: try: @@ -106,53 +188,199 @@ def process_lidar_data(self, lidar_data, frame_num): except Exception as e: v2xformer_path = None - metadata = self._generate_metadata(points, bin_path, npy_path, v2xformer_path) + # 生成元数据 + metadata = self._generate_metadata(points, frame_num, save_results, v2xformer_path, original_count) + # 定期清理 if frame_num % 20 == 0: gc.collect() + # 更新统计 + processing_time = time.time() - start_time + self.processing_stats['processing_times'].append(processing_time) + + # 计算平均点数 + if self.processing_stats['total_frames'] > 0: + self.processing_stats['avg_points_per_frame'] = \ + self.processing_stats['total_points'] / self.processing_stats['total_frames'] + return metadata except Exception as e: - gc.collect() + self.processing_stats['errors'] += 1 return None def _carla_lidar_to_numpy(self, lidar_data): + """CARLA LiDAR数据转numpy(增强版)""" try: + # 方法1:从原始数据解析 points = np.frombuffer(lidar_data.raw_data, dtype=np.float32) points = np.reshape(points, (int(points.shape[0] / 4), 4)) - points = points[:, :3] + + # 分离坐标和强度 + coordinates = points[:, :3] + intensities = points[:, 3] + + # 过滤无效点 + valid_mask = ~np.any(np.isnan(coordinates), axis=1) & ~np.any(np.isinf(coordinates), axis=1) + coordinates = coordinates[valid_mask] + intensities = intensities[valid_mask] + + # 添加强度信息 + points_with_intensity = np.column_stack([coordinates, intensities]) del lidar_data gc.collect() - return points + return points_with_intensity except Exception as e: try: + # 方法2:从点列表解析 points = [] - for i in range(0, len(lidar_data), 4): - point = lidar_data[i:i + 4] + intensities = [] + + # 假设lidar_data是点列表 + for point in lidar_data: points.append([point.x, point.y, point.z]) - return np.array(points, dtype=np.float32) + intensities.append(point.intensity if hasattr(point, 'intensity') else 1.0) + + points_array = np.array(points, dtype=np.float32) + intensities_array = np.array(intensities, dtype=np.float32) + + return np.column_stack([points_array, intensities_array]) + except: return None + def _preprocess_point_cloud(self, points): + """预处理点云""" + if points.shape[1] < 3: + return points + + # 过滤地面点(z坐标接近0) + ground_mask = points[:, 2] > 0.2 # 保留地面以上20cm的点 + + # 过滤过高点(高于建筑物) + height_mask = points[:, 2] < 50.0 + + # 过滤无效点 + valid_mask = ground_mask & height_mask + + return points[valid_mask] + + def _detect_pedestrians(self, points): + """检测行人""" + if points.shape[0] < 100: + return [] + + # 高度过滤:行人通常在0.5m到2.5m之间 + height_mask = (points[:, 2] > 0.5) & (points[:, 2] < 2.5) + candidate_points = points[height_mask] + + if candidate_points.shape[0] < 10: + return [] + + # 简单聚类:基于空间距离 + from sklearn.cluster import DBSCAN + + # 使用DBSCAN聚类 + clustering = DBSCAN(eps=0.5, min_samples=5).fit(candidate_points[:, :3]) + labels = clustering.labels_ + + clusters = [] + unique_labels = set(labels) + + for label in unique_labels: + if label == -1: # 噪声点 + continue + + cluster_points = candidate_points[labels == label] + if cluster_points.shape[0] < 10: # 点数太少 + continue + + # 计算聚类中心 + center = np.mean(cluster_points[:, :3], axis=0) + bbox_min = np.min(cluster_points[:, :3], axis=0) + bbox_max = np.max(cluster_points[:, :3], axis=0) + + # 计算大小 + size = bbox_max - bbox_min + + # 判断是否为行人(基于大小和形状) + if (size[0] < 1.0 and size[1] < 1.0 and size[2] > 0.5 and size[2] < 2.0): + clusters.append({ + 'center': center.tolist(), + 'size': size.tolist(), + 'point_count': cluster_points.shape[0], + 'bbox': { + 'min': bbox_min.tolist(), + 'max': bbox_max.tolist() + } + }) + + return clusters + + def _save_pedestrian_detections(self, frame_num, clusters): + """保存行人检测结果""" + detection_data = { + 'frame_id': frame_num, + 'timestamp': datetime.now().isoformat(), + 'pedestrian_count': len(clusters), + 'clusters': clusters + } + + detection_file = os.path.join(self.lidar_dir, f"pedestrian_detections_{frame_num:06d}.json") + + try: + with open(detection_file, 'w', encoding='utf-8') as f: + json.dump(detection_data, f, indent=2) + except: + pass + 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] + # 使用体素网格下采样 + try: + from sklearn.neighbors import NearestNeighbors + + # 创建体素网格 + voxel_size = 0.1 # 10cm体素 - del points - gc.collect() + # 计算每个点所属的体素 + voxel_indices = np.floor(points[:, :3] / voxel_size).astype(int) + + # 找到唯一体素 + unique_voxels, indices = np.unique(voxel_indices, axis=0, return_index=True) - return downsampled + # 从每个体素中随机选择一个点 + if len(indices) > self.max_points_per_frame: + # 如果需要进一步下采样,随机选择 + selected_indices = np.random.choice( + indices, + size=self.max_points_per_frame, + replace=False + ) + downsampled = points[selected_indices] + else: + downsampled = points[indices] + + return downsampled + + except: + # 如果体素下采样失败,使用随机下采样 + indices = np.random.choice( + points.shape[0], + size=self.max_points_per_frame, + replace=False + ) + return points[indices] def _estimate_batch_memory_mb(self): + """估计批处理内存使用""" if not self.point_cloud_batch: return 0 @@ -160,10 +388,12 @@ def _estimate_batch_memory_mb(self): for _, points in self.point_cloud_batch: total_points += points.shape[0] - memory_bytes = total_points * 12 * 1.2 + # 估计内存:每个点4个float(x,y,z,intensity),加上开销 + memory_bytes = total_points * 4 * 4 * 1.2 return memory_bytes / (1024 * 1024) def _check_memory_usage(self): + """检查内存使用""" try: import psutil process = psutil.Process() @@ -175,105 +405,161 @@ def _check_memory_usage(self): except: return False - def _save_batch(self): + def _save_batch_async(self): + """异步保存批处理数据""" if not self.point_cloud_batch: return - batch_data = [] - for frame_num, points in self.point_cloud_batch: - batch_data.append({ + # 复制当前批处理数据 + with self.data_lock: + batch_to_save = self.point_cloud_batch.copy() + self.point_cloud_batch.clear() + + # 异步保存 + self.thread_pool.submit(self._save_batch_sync, batch_to_save) + + def _save_batch_sync(self, batch_data): + """同步保存批处理数据""" + if not batch_data: + return + + batch_list = [] + for frame_num, points in batch_data: + batch_list.append({ 'frame_num': frame_num, 'points': points.tolist(), - 'num_points': points.shape[0] + 'num_points': points.shape[0], + 'timestamp': datetime.now().timestamp() }) - min_frame = min([item[0] for item in self.point_cloud_batch]) - max_frame = max([item[0] for item in self.point_cloud_batch]) + min_frame = min([item[0] for item in batch_data]) + max_frame = max([item[0] for item in batch_data]) 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) + try: + if self.enable_compression: + json_str = json.dumps(batch_list) compressed_data = zlib.compress( json_str.encode('utf-8'), level=self.compression_level ) + + # 计算压缩率 + original_size = len(json_str.encode('utf-8')) + compressed_size = len(compressed_data) + compression_ratio = compressed_size / max(1, original_size) + self.processing_stats['compression_ratios'].append(compression_ratio) + with open(batch_path, 'wb') as f: f.write(compressed_data) - except Exception as e: - try: - with open(batch_path, 'w') as f: - json.dump(batch_data, f) - except Exception as e2: - pass - else: - try: - with open(batch_path, 'w') as f: - json.dump(batch_data, f) - except Exception as e: - pass + else: + with open(batch_path, 'w', encoding='utf-8') as f: + json.dump(batch_list, f) - self.point_cloud_batch.clear() - gc.collect() + except Exception as e: + pass - def _save_as_bin(self, points, frame_num): - bin_path = os.path.join(self.lidar_dir, f"lidar_{frame_num:06d}.bin") + gc.collect() - points_with_intensity = np.zeros((points.shape[0], 4), dtype=np.float32) - points_with_intensity[:, :3] = points - points_with_intensity[:, 3] = 1.0 + def _save_point_cloud_files(self, points, frame_num): + """保存点云文件(多种格式)""" + save_results = { + 'bin': None, + 'npy': None, + 'pcd': None + } + # 1. 保存为.bin文件(KITTI格式) + bin_path = os.path.join(self.lidar_dir, f"lidar_{frame_num:06d}.bin") try: - points_with_intensity.tofile(bin_path) - except Exception as e: - return None + # KITTI格式:x,y,z,intensity + points_with_intensity = np.zeros((points.shape[0], 4), dtype=np.float32) + if points.shape[1] >= 4: + points_with_intensity[:, :] = points[:, :4] + else: + points_with_intensity[:, :3] = points[:, :3] + points_with_intensity[:, 3] = 1.0 - return bin_path + points_with_intensity.tofile(bin_path) + save_results['bin'] = bin_path + except: + save_results['bin'] = None - def _save_as_npy(self, points, frame_num): + # 2. 保存为.npy文件 npy_path = os.path.join(self.lidar_dir, f"lidar_{frame_num:06d}.npy") + try: + np.save(npy_path, points) + save_results['npy'] = npy_path + except: + save_results['npy'] = None + # 3. 尝试保存为.pcd文件(点云库格式) + pcd_path = os.path.join(self.lidar_dir, f"lidar_{frame_num:06d}.pcd") try: - memmap_array = np.lib.format.open_memmap( - npy_path, - mode='w+', - dtype=np.float32, - shape=points.shape - ) - memmap_array[:] = points[:] - memmap_array.flush() - except Exception as e: - try: - np.save(npy_path, points) - except Exception as e2: - return None + self._save_as_pcd(points, pcd_path) + save_results['pcd'] = pcd_path + except: + save_results['pcd'] = None - return npy_path + return save_results + + def _save_as_pcd(self, points, filepath): + """保存为PCD格式""" + try: + with open(filepath, 'w') as f: + f.write("# .PCD v0.7 - Point Cloud Data file format\n") + f.write("VERSION 0.7\n") + f.write("FIELDS x y z intensity\n") + f.write("SIZE 4 4 4 4\n") + f.write("TYPE F F F F\n") + f.write("COUNT 1 1 1 1\n") + f.write(f"WIDTH {points.shape[0]}\n") + f.write("HEIGHT 1\n") + f.write("VIEWPOINT 0 0 0 1 0 0 0\n") + f.write(f"POINTS {points.shape[0]}\n") + f.write("DATA ascii\n") + + for i in range(points.shape[0]): + if points.shape[1] >= 4: + f.write(f"{points[i, 0]:.6f} {points[i, 1]:.6f} {points[i, 2]:.6f} {points[i, 3]:.6f}\n") + else: + f.write(f"{points[i, 0]:.6f} {points[i, 1]:.6f} {points[i, 2]:.6f} 1.0\n") + except: + pass def _save_as_v2xformer_format(self, points, frame_num): - v2x_dir = os.path.join(self.output_dir, "v2xformer_format") + """保存为V2XFormer格式""" + v2x_dir = os.path.join(self.output_dir, "v2xformer_format", "point_cloud") os.makedirs(v2x_dir, exist_ok=True) try: + # 创建V2XFormer格式数据 v2x_data = { 'frame_id': frame_num, 'timestamp': datetime.now().timestamp(), + 'sensor_id': 'lidar_main', 'point_cloud': { - 'points': points.tolist(), + 'points': points[:, :3].tolist(), + 'intensities': points[:, 3].tolist() if points.shape[1] >= 4 else [1.0] * points.shape[0], 'num_points': points.shape[0], - 'range': [float(points[:, 0].min()), float(points[:, 0].max()), - float(points[:, 1].min()), float(points[:, 1].max()), - float(points[:, 2].min()), float(points[:, 2].max())] + 'bounding_box': { + 'min': [float(points[:, 0].min()), float(points[:, 1].min()), float(points[:, 2].min())], + 'max': [float(points[:, 0].max()), float(points[:, 1].max()), float(points[:, 2].max())] + } }, 'metadata': { 'sensor_type': 'lidar', - 'format_version': '1.0', + 'format_version': '2.0', 'coordinate_system': 'carla_world', - 'frame_rate': 10.0 - } + 'frame_rate': 10.0, + 'pedestrian_detection': self.pedestrian_detection, + 'compression': 'none' + }, + 'pedestrian_detections': self.pedestrian_points[-10:] if self.pedestrian_points else [] } + # 使用gzip压缩保存 filename = f"{frame_num:06d}.pkl.gz" filepath = os.path.join(v2x_dir, filename) @@ -285,35 +571,52 @@ def _save_as_v2xformer_format(self, points, frame_num): except Exception as e: return None - def _generate_metadata(self, points, bin_path, npy_path, v2xformer_path): + def _generate_metadata(self, points, frame_num, save_results, v2xformer_path, original_count): + """生成元数据(增强版)""" metadata = { - 'frame_id': self.frame_counter, + 'frame_id': frame_num, 'timestamp': datetime.now().isoformat(), - 'point_count': int(points.shape[0]), + 'processing_info': { + 'original_points': original_count, + 'processed_points': int(points.shape[0]), + 'downsampling_applied': self.enable_downsampling and original_count > self.max_points_per_frame, + 'downsample_ratio': self.downsample_ratio if self.enable_downsampling else 1.0, + 'compression_enabled': self.enable_compression, + 'compression_level': self.compression_level, + 'v2x_saved': v2xformer_path is not None, + 'pedestrian_detection': self.pedestrian_detection + }, 'file_paths': { - 'bin': os.path.basename(bin_path) if bin_path else None, - 'npy': os.path.basename(npy_path) if npy_path else None, + 'bin': os.path.basename(save_results['bin']) if save_results['bin'] else None, + 'npy': os.path.basename(save_results['npy']) if save_results['npy'] else None, + 'pcd': os.path.basename(save_results['pcd']) if save_results['pcd'] else None, 'v2xformer': os.path.basename(v2xformer_path) if v2xformer_path else None }, 'statistics': { + 'point_count': int(points.shape[0]), 'x_range': [float(points[:, 0].min()), float(points[:, 0].max())], 'y_range': [float(points[:, 1].min()), float(points[:, 1].max())], 'z_range': [float(points[:, 2].min()), float(points[:, 2].max())], - 'mean': [float(points[:, 0].mean()), float(points[:, 1].mean()), float(points[:, 2].mean())], - 'std': [float(points[:, 0].std()), float(points[:, 1].std()), float(points[:, 2].std())] - }, - 'processing_info': { - 'downsampled': self.enable_downsampling and points.shape[0] > self.max_points_per_frame, - 'downsample_ratio': self.downsample_ratio if self.enable_downsampling else 1.0, - 'compression_enabled': self.enable_compression, - 'compression_level': self.compression_level, - 'v2x_saved': v2xformer_path is not None + 'mean_position': [float(points[:, 0].mean()), float(points[:, 1].mean()), float(points[:, 2].mean())], + 'std_position': [float(points[:, 0].std()), float(points[:, 1].std()), float(points[:, 2].std())], + 'density': points.shape[0] / max(1, (points[:, 0].max() - points[:, 0].min()) * + (points[:, 1].max() - points[:, 1].min())) } } - meta_path = os.path.join(self.lidar_dir, f"lidar_meta_{self.frame_counter:06d}.json") + # 如果有强度信息,添加强度统计 + if points.shape[1] >= 4: + metadata['statistics']['intensity'] = { + 'min': float(points[:, 3].min()), + 'max': float(points[:, 3].max()), + 'mean': float(points[:, 3].mean()), + 'std': float(points[:, 3].std()) + } + + # 保存元数据文件 + meta_path = os.path.join(self.lidar_dir, f"lidar_meta_{frame_num:06d}.json") try: - with open(meta_path, 'w') as f: + with open(meta_path, 'w', encoding='utf-8') as f: json.dump(metadata, f, indent=2) except Exception as e: pass @@ -321,66 +624,113 @@ def _generate_metadata(self, points, bin_path, npy_path, v2xformer_path): return metadata def flush_batch(self): + """刷新批处理数据""" if self.point_cloud_batch: - self._save_batch() + self._save_batch_sync(self.point_cloud_batch.copy()) + self.point_cloud_batch.clear() 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")) + pcd_files = glob.glob(os.path.join(self.lidar_dir, "*.pcd")) + json_files = glob.glob(os.path.join(self.lidar_dir, "*.json")) batch_files = glob.glob(os.path.join(self.lidar_dir, "*batch*.json")) - v2x_files = glob.glob(os.path.join(self.output_dir, "v2xformer_format", "*.pkl.gz")) + # V2XFormer格式文件 + v2x_dir = os.path.join(self.output_dir, "v2xformer_format", "point_cloud") + v2x_files = glob.glob(os.path.join(v2x_dir, "*.pkl.gz")) if os.path.exists(v2x_dir) else [] + + # 计算总点数和文件大小 total_points = 0 total_size = 0 - sample_files = bin_files[:5] if len(bin_files) > 5 else bin_files + # 抽样检查文件 + sample_files = bin_files[:min(10, len(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) - total_points += points_in_file - except: - continue + try: + file_size = os.path.getsize(bin_file) + total_size += file_size + # 估计点数:每个点4个float32 = 16字节 + points_in_file = file_size // 16 + 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) else: total_points_estimated = 0 + avg_points_per_file = 0 + + # 处理统计 + avg_processing_time = np.mean(self.processing_stats['processing_times']) if self.processing_stats[ + 'processing_times'] else 0 + avg_compression_ratio = np.mean(self.processing_stats['compression_ratios']) if self.processing_stats[ + 'compression_ratios'] else 1.0 summary = { - 'total_frames': len(bin_files), - 'total_points_estimated': int(total_points_estimated), - 'average_points_per_frame': int(avg_points_per_file) if sample_files else 0, - 'file_types': { - 'bin': len(bin_files), - 'npy': len(npy_files), - 'batch': len(batch_files), - 'v2xformer': len(v2x_files) + 'summary_time': datetime.now().isoformat(), + 'file_statistics': { + 'total_frames': len(bin_files), + 'bin_files': len(bin_files), + 'npy_files': len(npy_files), + 'pcd_files': len(pcd_files), + 'json_files': len(json_files), + 'batch_files': len(batch_files), + 'v2xformer_files': len(v2x_files) + }, + 'point_statistics': { + 'total_points_estimated': int(total_points_estimated), + 'average_points_per_frame': int(avg_points_per_file), + 'total_points_processed': self.processing_stats['total_points'] }, - 'total_size_mb': round(total_size / (1024 * 1024), 2), - 'compression_ratio': round(sum([os.path.getsize(f) for f in batch_files]) / - max(1, sum([os.path.getsize(f) for f in bin_files])), 2) - if bin_files and batch_files else 1.0 + 'size_statistics': { + 'total_size_mb': round(total_size / (1024 * 1024), 2), + 'average_file_size_kb': round((total_size / max(1, len(sample_files))) / 1024, 2), + 'compression_ratio': round(avg_compression_ratio, 3) + }, + 'processing_statistics': { + 'total_frames_processed': self.processing_stats['total_frames'], + 'average_processing_time_ms': round(avg_processing_time * 1000, 2), + 'error_count': self.processing_stats['errors'], + 'success_rate': round((self.processing_stats['total_frames'] - self.processing_stats['errors']) / + max(1, self.processing_stats['total_frames']) * 100, 1) + }, + 'pedestrian_detection': { + 'enabled': self.pedestrian_detection, + 'detection_count': len(self.pedestrian_points) + } } + # 保存摘要 summary_path = os.path.join(self.output_dir, "metadata", "lidar_summary.json") try: - with open(summary_path, 'w') as f: + with open(summary_path, 'w', encoding='utf-8') as f: json.dump(summary, f, indent=2) except Exception as e: pass return summary + def cleanup(self): + """清理资源""" + self.flush_batch() + self.thread_pool.shutdown(wait=True) + gc.collect() + class MultiSensorFusion: + """多传感器融合(增强版)""" def __init__(self, output_dir, config=None): self.output_dir = output_dir @@ -389,90 +739,210 @@ def __init__(self, output_dir, config=None): self.calibration_data = {} self.fusion_cache = {} - self.cache_size = config.get('fusion_cache_size', 50) if config else 50 + self.cache_size = config.get('fusion_cache_size', 100) if config else 100 + + self.fusion_stats = { + 'total_fusions': 0, + 'cache_hits': 0, + 'processing_times': [] + } self._load_calibration() 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]: - 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: - pass + if not os.path.exists(calibration_dir): + return + + 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: + try: + with open(file, 'r', encoding='utf-8') as f: + data = json.load(f) + + sensor_name = os.path.basename(file).replace('.json', '') + self.calibration_data[sensor_name] = data + + # 解析传感器类型 + if 'sensor_type' in data: + sensor_type = data['sensor_type'] + if sensor_type not in self.calibration_data: + self.calibration_data[sensor_type] = [] + self.calibration_data[sensor_type].append(data) + + except Exception as e: + pass def create_synchronization_file(self, frame_num, sensor_data): - cache_key = f"{frame_num}_{hash(str(sensor_data))}" + """创建同步文件(增强版)""" + start_time = time.time() + + # 生成缓存键 + cache_key = f"{frame_num}_{hash(str(sorted(sensor_data.items())))}" + if cache_key in self.fusion_cache: + self.fusion_stats['cache_hits'] += 1 return self.fusion_cache[cache_key] sync_data = { 'frame_id': frame_num, 'timestamp': datetime.now().timestamp(), + 'iso_timestamp': datetime.now().isoformat(), 'sensors': {}, - 'transformations': {} + 'transformations': {}, + 'synchronization_quality': 'good', + 'missing_sensors': [] } + # 处理每个传感器的数据 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 - img = cv2.imread(data_path) - if img is not None: - file_info['dimensions'] = img.shape[:2] - except: - pass - + file_info = self._get_file_info(data_path) sync_data['sensors'][sensor_type] = file_info + # 添加标定变换 if sensor_type in self.calibration_data: + calib_data = self.calibration_data[sensor_type] + sync_data['transformations'][sensor_type] = { + 'matrix': calib_data.get('matrix', + [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]), + 'calibration_source': 'loaded' + } + else: + # 使用默认变换 sync_data['transformations'][sensor_type] = { - 'matrix': self.calibration_data[sensor_type].get('matrix', - [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], - [0, 0, 0, 1]]) + 'matrix': [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], + 'calibration_source': 'default' } + else: + sync_data['missing_sensors'].append(sensor_type) + + # 计算同步质量 + if len(sync_data['missing_sensors']) > 2: + sync_data['synchronization_quality'] = 'poor' + elif len(sync_data['missing_sensors']) > 0: + sync_data['synchronization_quality'] = 'fair' + + # 添加传感器间的时间偏差估计 + sync_data['temporal_alignment'] = self._estimate_temporal_alignment(sync_data['sensors']) + # 保存同步文件 sync_file = os.path.join(self.fusion_dir, f"sync_{frame_num:06d}.json") - if len(str(sync_data)) > 1000: - compressed_data = zlib.compress( - json.dumps(sync_data).encode('utf-8'), - level=3 - ) - sync_file = sync_file.replace('.json', '.json.gz') - with open(sync_file, 'wb') as f: - f.write(compressed_data) - else: - with open(sync_file, 'w') as f: - json.dump(sync_data, f, separators=(',', ':')) + try: + # 根据数据大小选择是否压缩 + json_str = json.dumps(sync_data, separators=(',', ':')) + + if len(json_str) > 5000: # 大于5KB时压缩 + compressed_data = zlib.compress( + json_str.encode('utf-8'), + level=3 + ) + sync_file = sync_file.replace('.json', '.json.gz') + with open(sync_file, 'wb') as f: + f.write(compressed_data) + else: + with open(sync_file, 'w', encoding='utf-8') as f: + json.dump(sync_data, f, indent=2) - if len(self.fusion_cache) >= self.cache_size: + except Exception as e: + # 保存失败时使用简单格式 + try: + with open(sync_file, 'w') as f: + f.write(json.dumps({'frame_id': frame_num, 'error': str(e)})) + except: + sync_file = None + + # 更新缓存 + if sync_file and len(self.fusion_cache) >= self.cache_size: + # 移除最旧的缓存项 oldest_key = next(iter(self.fusion_cache)) del self.fusion_cache[oldest_key] - self.fusion_cache[cache_key] = sync_file + if sync_file: + self.fusion_cache[cache_key] = sync_file + + # 更新统计 + self.fusion_stats['total_fusions'] += 1 + processing_time = time.time() - start_time + self.fusion_stats['processing_times'].append(processing_time) return sync_file + def _get_file_info(self, filepath): + """获取文件信息(增强版)""" + file_info = { + 'file_path': os.path.basename(filepath), + 'file_size': os.path.getsize(filepath), + 'modified_time': os.path.getmtime(filepath), + 'file_type': os.path.splitext(filepath)[1][1:] + } + + # 图像文件额外信息 + if filepath.endswith(('.png', '.jpg', '.jpeg')): + try: + import cv2 + img = cv2.imread(filepath) + if img is not None: + file_info['dimensions'] = img.shape[:2] + file_info['channels'] = img.shape[2] if len(img.shape) > 2 else 1 + file_info['dtype'] = str(img.dtype) + except: + pass + + # LiDAR文件额外信息 + elif filepath.endswith('.bin'): + try: + file_size = os.path.getsize(filepath) + # 估计点数:每个点4个float32 = 16字节 + estimated_points = file_size // 16 + file_info['estimated_points'] = estimated_points + file_info['point_format'] = 'xyz-intensity' + except: + pass + + return file_info + + def _estimate_temporal_alignment(self, sensors_info): + """估计时间对齐""" + alignment = { + 'max_time_difference': 0, + 'average_time_difference': 0, + 'alignment_score': 1.0 + } + + if len(sensors_info) < 2: + return alignment + + # 获取修改时间 + mod_times = [info['modified_time'] for info in sensors_info.values()] + + if mod_times: + max_diff = max(mod_times) - min(mod_times) + avg_diff = np.mean([abs(t - np.mean(mod_times)) for t in mod_times]) + + alignment['max_time_difference'] = max_diff + alignment['average_time_difference'] = avg_diff + + # 计算对齐分数(差异越小分数越高) + if max_diff > 1.0: # 超过1秒 + alignment['alignment_score'] = 0.3 + elif max_diff > 0.1: # 超过0.1秒 + alignment['alignment_score'] = 0.7 + else: + alignment['alignment_score'] = 1.0 + + return alignment + def generate_fusion_report(self): + """生成融合报告(增强版)""" if not os.path.exists(self.fusion_dir): return None @@ -480,36 +950,80 @@ def generate_fusion_report(self): sync_files = glob.glob(os.path.join(self.fusion_dir, "*.json*")) frame_ids = [] - for sync_file in sync_files[:20]: + file_sizes = [] + compression_stats = {'compressed': 0, 'uncompressed': 0} + + for sync_file in sync_files[:50]: # 只检查前50个文件 try: filename = os.path.basename(sync_file) if filename.startswith('sync_'): - frame_id = int(filename.split('_')[1].split('.')[0]) - frame_ids.append(frame_id) + # 提取帧ID + frame_id_str = filename.split('_')[1].split('.')[0] + if frame_id_str.isdigit(): + frame_id = int(frame_id_str) + frame_ids.append(frame_id) + + # 统计文件大小 + file_size = os.path.getsize(sync_file) + file_sizes.append(file_size) + + # 统计压缩情况 + if sync_file.endswith('.gz'): + compression_stats['compressed'] += 1 + else: + compression_stats['uncompressed'] += 1 + except: continue + # 计算处理统计 + avg_processing_time = np.mean(self.fusion_stats['processing_times']) if self.fusion_stats[ + 'processing_times'] else 0 + cache_hit_rate = self.fusion_stats['cache_hits'] / max(1, self.fusion_stats['total_fusions']) + report = { - 'total_sync_frames': len(sync_files), - 'calibration_data_count': len(self.calibration_data), - 'sensor_types': list(set([f.split('_')[0] for f in self.calibration_data.keys()])), - 'frame_range': [min(frame_ids), max(frame_ids)] if frame_ids else [], - 'file_sizes': { - 'total_mb': round(sum([os.path.getsize(f) for f in sync_files]) / (1024 * 1024), 2), - 'average_kb': round(np.mean([os.path.getsize(f) / 1024 for f in sync_files]), 2) if sync_files else 0 + 'report_time': datetime.now().isoformat(), + 'fusion_statistics': { + 'total_sync_frames': len(sync_files), + 'calibration_data_count': len(self.calibration_data), + 'sensor_types': list(set([data.get('sensor_type', 'unknown') + for data in self.calibration_data.values() + if isinstance(data, dict)])), + 'frame_range': [min(frame_ids), max(frame_ids)] if frame_ids else [], + 'cache_hit_rate': round(cache_hit_rate, 3), + 'average_processing_time_ms': round(avg_processing_time * 1000, 2) }, - 'compression_info': { - 'compressed_files': len([f for f in sync_files if f.endswith('.gz')]), + 'file_statistics': { + 'total_files': len(sync_files), + 'total_size_mb': round(sum(file_sizes) / (1024 * 1024), 2), + 'average_file_size_kb': round(np.mean(file_sizes) / 1024, 2) if file_sizes else 0, 'compression_ratio': round( - sum([os.path.getsize(f) for f in sync_files if f.endswith('.gz')]) / - max(1, sum([os.path.getsize(f) for f in sync_files if not f.endswith('.gz')])), - 2 - ) if sync_files else 1.0 + sum([s for f, s in zip(sync_files, file_sizes) if f.endswith('.gz')]) / + max(1, sum([s for f, s in zip(sync_files, file_sizes) if not f.endswith('.gz')])), 2 + ) if sync_files else 1.0, + 'compression_stats': compression_stats + }, + 'calibration_summary': { + 'total_calibrations': len(self.calibration_data), + 'by_type': {} } } + # 按类型统计标定数据 + for data in self.calibration_data.values(): + if isinstance(data, dict) and 'sensor_type' in data: + sensor_type = data['sensor_type'] + report['calibration_summary']['by_type'][sensor_type] = \ + report['calibration_summary']['by_type'].get(sensor_type, 0) + 1 + + # 保存报告 report_path = os.path.join(self.output_dir, "metadata", "fusion_report.json") - with open(report_path, 'w') as f: + with open(report_path, 'w', encoding='utf-8') as f: json.dump(report, f, indent=2) - return report \ No newline at end of file + return report + + def cleanup(self): + """清理资源""" + self.fusion_cache.clear() + gc.collect() \ No newline at end of file diff --git a/src/enhance_pedestrian_safety/main.py b/src/enhance_pedestrian_safety/main.py index bfd920d8f4..012872ae61 100644 --- a/src/enhance_pedestrian_safety/main.py +++ b/src/enhance_pedestrian_safety/main.py @@ -1,4 +1,7 @@ -# !/usr/bin/env python3 +#!/usr/bin/env python3 +""" +CVIPS 行人安全增强数据收集系统 - 主程序(增强版) +""" import sys import os import time @@ -12,38 +15,71 @@ import gc import psutil import numpy as np +import signal -from carla_utils import setup_carla_path, import_carla_module +from carla_utils import setup_carla_path, import_carla_module, setup_carla_environment from config_manager import ConfigManager from annotation_generator import AnnotationGenerator from data_validator import DataValidator from lidar_processor import LidarProcessor, MultiSensorFusion from multi_vehicle_manager import MultiVehicleManager from pedestrian_safety_monitor import PedestrianSafetyMonitor +from scene_manager import SceneManager +from sensor_enhancer import SensorDataEnhancer, EnhancementConfig, WeatherType, EnhancementMethod +from v2x_communication import V2XCommunication -carla_egg_path, remaining_argv = setup_carla_path() -carla = import_carla_module() +# 设置CARLA环境 +carla_module, remaining_argv = setup_carla_environment() +carla = carla_module class PerformanceMonitor: - def __init__(self): + """性能监控器(增强版)""" + + def __init__(self, config): + self.config = config self.start_time = time.time() self.memory_samples = [] self.cpu_samples = [] self.frame_times = [] self.first_frame_time = None self.last_frame_time = None + self.gc_stats = [] + self.network_stats = [] + + # 性能阈值 + self.warning_thresholds = { + 'memory_mb': config.get('performance', {}).get('memory_management', {}).get('early_stop_threshold', 450), + 'cpu_percent': 90, + 'frame_time_ms': 200 # 5 FPS + } + + # 警告记录 + self.warnings = [] def sample_memory(self): + """采样内存使用""" try: process = psutil.Process(os.getpid()) - memory_mb = process.memory_info().rss / 1024 / 1024 + memory_info = process.memory_info() + + memory_mb = memory_info.rss / 1024 / 1024 self.memory_samples.append(memory_mb) + + # 检查内存警告 + if memory_mb > self.warning_thresholds['memory_mb'] * 0.8: + if not any('内存使用高' in w for w in self.warnings[-5:]): # 避免重复警告 + self.warnings.append(f"内存使用高: {memory_mb:.1f}MB") + + system_memory = psutil.virtual_memory() + return { 'process_mb': memory_mb, - 'system_total_mb': psutil.virtual_memory().total / 1024 / 1024, - 'system_used_percent': psutil.virtual_memory().percent, - 'system_available_mb': psutil.virtual_memory().available / 1024 / 1024 + 'process_vms_mb': memory_info.vms / 1024 / 1024, + 'system_total_mb': system_memory.total / 1024 / 1024, + 'system_used_percent': system_memory.percent, + 'system_available_mb': system_memory.available / 1024 / 1024, + 'warning': memory_mb > self.warning_thresholds['memory_mb'] * 0.8 } except: memory_mb = 0 @@ -52,17 +88,28 @@ def sample_memory(self): 'process_mb': memory_mb, 'system_total_mb': 0, 'system_used_percent': 0, - 'system_available_mb': 0 + 'system_available_mb': 0, + 'warning': False } def sample_cpu(self): + """采样CPU使用""" try: cpu_percent = psutil.cpu_percent(interval=0.1) self.cpu_samples.append(cpu_percent) + + # 检查CPU警告 + if cpu_percent > self.warning_thresholds['cpu_percent']: + if not any('CPU使用高' in w for w in self.warnings[-5:]): + self.warnings.append(f"CPU使用高: {cpu_percent:.1f}%") + + per_core = psutil.cpu_percent(interval=0.1, percpu=True) + return { 'total_percent': cpu_percent, - 'per_core': psutil.cpu_percent(interval=0.1, percpu=True), - 'count': psutil.cpu_count() + 'per_core': per_core, + 'count': psutil.cpu_count(), + 'warning': cpu_percent > self.warning_thresholds['cpu_percent'] } except: cpu_percent = 0 @@ -70,16 +117,41 @@ def sample_cpu(self): return { 'total_percent': cpu_percent, 'per_core': [0], - 'count': 1 + 'count': 1, + 'warning': False + } + + def sample_gc(self): + """采样垃圾回收统计""" + try: + gc.collect() + gc_stats = { + 'collected': gc.get_count()[0], + 'uncollectable': len(gc.garbage), + 'thresholds': gc.get_threshold(), + 'enabled': gc.isenabled() } + self.gc_stats.append(gc_stats) + return gc_stats + except: + return {} def record_frame_time(self, frame_time): + """记录帧时间""" self.frame_times.append(frame_time) + + # 检查帧时间警告 + frame_time_ms = frame_time * 1000 + if frame_time_ms > self.warning_thresholds['frame_time_ms']: + if not any('帧时间过长' in w for w in self.warnings[-5:]): + self.warnings.append(f"帧时间过长: {frame_time_ms:.1f}ms") + if self.first_frame_time is None: self.first_frame_time = time.time() self.last_frame_time = time.time() def get_performance_summary(self): + """获取性能摘要""" if not self.frame_times or len(self.frame_times) < 2: avg_frame_time = 0 fps = 0 @@ -89,19 +161,36 @@ def get_performance_summary(self): summary = { 'total_runtime': time.time() - self.start_time, - 'average_memory_mb': np.mean(self.memory_samples) if self.memory_samples else 0, - 'max_memory_mb': max(self.memory_samples) if self.memory_samples else 0, - 'min_memory_mb': min(self.memory_samples) if self.memory_samples else 0, - 'average_cpu_percent': np.mean(self.cpu_samples) if self.cpu_samples else 0, - 'max_cpu_percent': max(self.cpu_samples) if self.cpu_samples else 0, - 'average_frame_time': avg_frame_time, - 'frames_per_second': fps, - 'total_frames': len(self.frame_times) + 'memory_statistics': { + 'average_memory_mb': np.mean(self.memory_samples) if self.memory_samples else 0, + 'max_memory_mb': max(self.memory_samples) if self.memory_samples else 0, + 'min_memory_mb': min(self.memory_samples) if self.memory_samples else 0, + 'memory_warnings': len( + [m for m in self.memory_samples if m > self.warning_thresholds['memory_mb'] * 0.8]) + }, + 'cpu_statistics': { + 'average_cpu_percent': np.mean(self.cpu_samples) if self.cpu_samples else 0, + 'max_cpu_percent': max(self.cpu_samples) if self.cpu_samples else 0, + 'cpu_warnings': len([c for c in self.cpu_samples if c > self.warning_thresholds['cpu_percent']]) + }, + 'frame_statistics': { + 'average_frame_time': avg_frame_time, + 'frames_per_second': fps, + 'total_frames': len(self.frame_times), + 'frame_time_warnings': len( + [f for f in self.frame_times if f * 1000 > self.warning_thresholds['frame_time_ms']]) + }, + 'gc_statistics': { + 'total_collections': len(self.gc_stats), + 'average_collected': np.mean([s.get('collected', 0) for s in self.gc_stats]) if self.gc_stats else 0 + }, + 'warnings': self.warnings[-10:] # 最近10个警告 } if self.frame_times and len(self.frame_times) >= 2: summary['frame_time_stats'] = { 'p50': np.percentile(self.frame_times, 50), + 'p75': np.percentile(self.frame_times, 75), 'p95': np.percentile(self.frame_times, 95), 'p99': np.percentile(self.frame_times, 99) if len(self.frame_times) > 1 else 0, 'std': np.std(self.frame_times) @@ -109,6 +198,7 @@ def get_performance_summary(self): else: summary['frame_time_stats'] = { 'p50': 0, + 'p75': 0, 'p95': 0, 'p99': 0, 'std': 0 @@ -116,97 +206,361 @@ def get_performance_summary(self): return summary + def check_emergency_stop(self): + """检查是否需要紧急停止""" + if not self.memory_samples: + return False + + current_memory = self.memory_samples[-1] + return current_memory > self.warning_thresholds['memory_mb'] + class Log: + """日志系统(增强版)""" + + LOG_LEVELS = { + 'DEBUG': 0, + 'INFO': 1, + 'WARNING': 2, + 'ERROR': 3, + 'CRITICAL': 4 + } + + current_level = 'INFO' + log_file = None + + @classmethod + def setup(cls, config): + """设置日志系统""" + cls.current_level = config.get('monitoring', {}).get('log_level', 'INFO') + log_file = config.get('monitoring', {}).get('log_file', 'cvips.log') + + if config.get('monitoring', {}).get('enable_logging', True): + cls.log_file = open(log_file, 'a', encoding='utf-8') + + @classmethod + def _should_log(cls, level): + """检查是否应该记录""" + return cls.LOG_LEVELS[level] >= cls.LOG_LEVELS[cls.current_level] + @staticmethod def info(msg): - timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") - print(f"[INFO][{timestamp}] {msg}") + """信息日志""" + if not Log._should_log('INFO'): + return + + timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3] + log_msg = f"[INFO][{timestamp}] {msg}" + print(f"\033[92m{log_msg}\033[0m") + + if Log.log_file: + Log.log_file.write(log_msg + '\n') + Log.log_file.flush() @staticmethod def warning(msg): - timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") - print(f"[WARNING][{timestamp}] {msg}") + """警告日志""" + if not Log._should_log('WARNING'): + return + + timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3] + log_msg = f"[WARNING][{timestamp}] {msg}" + print(f"\033[93m{log_msg}\033[0m") + + if Log.log_file: + Log.log_file.write(log_msg + '\n') + Log.log_file.flush() @staticmethod def error(msg): - timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") - print(f"[ERROR][{timestamp}] {msg}") + """错误日志""" + if not Log._should_log('ERROR'): + return + + timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3] + log_msg = f"[ERROR][{timestamp}] {msg}" + print(f"\033[91m{log_msg}\033[0m") + + if Log.log_file: + Log.log_file.write(log_msg + '\n') + Log.log_file.flush() @staticmethod def debug(msg): - timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") - print(f"[DEBUG][{timestamp}] {msg}") + """调试日志""" + if not Log._should_log('DEBUG'): + return + + timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3] + log_msg = f"[DEBUG][{timestamp}] {msg}" + print(f"\033[90m{log_msg}\033[0m") + + if Log.log_file: + Log.log_file.write(log_msg + '\n') + Log.log_file.flush() @staticmethod def performance(msg): - timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") - print(f"[PERF][{timestamp}] {msg}") + """性能日志""" + timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3] + log_msg = f"[PERF][{timestamp}] {msg}" + print(f"\033[96m{log_msg}\033[0m") + + if Log.log_file: + Log.log_file.write(log_msg + '\n') + Log.log_file.flush() @staticmethod def safety(msg): - timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") - print(f"[SAFETY][{timestamp}] {msg}") + """安全日志""" + timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3] + log_msg = f"[SAFETY][{timestamp}] {msg}" + print(f"\033[95m{log_msg}\033[0m") + + if Log.log_file: + Log.log_file.write(log_msg + '\n') + Log.log_file.flush() + + @staticmethod + def critical(msg): + """关键日志""" + timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3] + log_msg = f"[CRITICAL][{timestamp}] {msg}" + print(f"\033[91;1m{log_msg}\033[0m") + + if Log.log_file: + Log.log_file.write(log_msg + '\n') + Log.log_file.flush() + + @classmethod + def cleanup(cls): + """清理日志资源""" + if cls.log_file: + cls.log_file.close() + cls.log_file = None + + +class EmergencyHandler: + """紧急处理程序""" + + def __init__(self, data_collector): + self.data_collector = data_collector + self.emergency_stop = False + self.last_emergency_time = 0 + + # 设置信号处理器 + signal.signal(signal.SIGINT, self._signal_handler) + signal.signal(signal.SIGTERM, self._signal_handler) + + def _signal_handler(self, signum, frame): + """信号处理""" + Log.critical(f"收到信号 {signum},正在安全关闭...") + self.emergency_stop = True + self.data_collector.is_running = False + + def check_emergency(self): + """检查紧急情况""" + current_time = time.time() + + # 避免过于频繁的紧急检查 + if current_time - self.last_emergency_time < 5.0: + return False + + self.last_emergency_time = current_time + + # 检查内存使用 + try: + import psutil + process = psutil.Process() + memory_mb = process.memory_info().rss / (1024 * 1024) + + memory_threshold = self.data_collector.config['performance'].get( + 'memory_management', {}).get('early_stop_threshold', 450) + + if memory_mb > memory_threshold: + Log.critical(f"内存使用超过阈值: {memory_mb:.1f}MB > {memory_threshold}MB") + return True + + except: + pass + + return False + + def emergency_cleanup(self): + """紧急清理""" + Log.critical("执行紧急清理...") + + try: + # 强制垃圾回收 + gc.collect() + + # 清理传感器数据 + for sensor_manager in self.data_collector.sensor_managers.values(): + if hasattr(sensor_manager, 'image_processor'): + if hasattr(sensor_manager.image_processor, 'image_cache'): + sensor_manager.image_processor.image_cache.clear() + + Log.critical("紧急清理完成") + + except Exception as e: + Log.error(f"紧急清理失败: {e}") class WeatherSystem: + """天气系统(增强版)""" + WEATHER_PRESETS = { - 'clear': {'cloudiness': 10, 'precipitation': 0, 'wind': 5}, - 'rainy': {'cloudiness': 90, 'precipitation': 80, 'wind': 15}, - 'cloudy': {'cloudiness': 70, 'precipitation': 10, 'wind': 10}, - 'foggy': {'cloudiness': 50, 'precipitation': 0, 'fog_density': 40} + 'clear': { + 'cloudiness': 10, + 'precipitation': 0, + 'wind': 5, + 'fog_density': 0, + 'wetness': 0, + 'sun_altitude': 75 + }, + 'rainy': { + 'cloudiness': 90, + 'precipitation': 80, + 'wind': 15, + 'fog_density': 20, + 'wetness': 80, + 'sun_altitude': 30 + }, + 'cloudy': { + 'cloudiness': 70, + 'precipitation': 10, + 'wind': 10, + 'fog_density': 10, + 'wetness': 20, + 'sun_altitude': 45 + }, + 'foggy': { + 'cloudiness': 50, + 'precipitation': 0, + 'fog_density': 60, + 'wind': 3, + 'wetness': 30, + 'sun_altitude': 20 + }, + 'wet': { + 'cloudiness': 40, + 'precipitation': 0, + 'wind': 8, + 'fog_density': 10, + 'wetness': 60, + 'sun_altitude': 50 + } + } + + TIME_PRESETS = { + 'noon': {'sun_altitude': 75, 'sun_azimuth': 0}, + 'sunset': {'sun_altitude': 15, 'sun_azimuth': 270}, + 'night': {'sun_altitude': -20, 'sun_azimuth': 0}, + 'dawn': {'sun_altitude': 15, 'sun_azimuth': 90} } @staticmethod def create_weather(weather_type, time_of_day): + """创建天气(增强版)""" weather = carla.WeatherParameters() if weather_type in WeatherSystem.WEATHER_PRESETS: preset = WeatherSystem.WEATHER_PRESETS[weather_type] weather.cloudiness = preset.get('cloudiness', 30) weather.precipitation = preset.get('precipitation', 0) + weather.precipitation_deposits = preset.get('wetness', 0) weather.wind_intensity = preset.get('wind', 5) - if 'fog_density' in preset: - weather.fog_density = preset['fog_density'] + weather.fog_density = preset.get('fog_density', 0) + weather.fog_distance = 5.0 if preset.get('fog_density', 0) > 50 else 15.0 + weather.wetness = preset.get('wetness', 0) - if time_of_day == 'noon': - weather.sun_altitude_angle = 75 - elif time_of_day == 'sunset': - weather.sun_altitude_angle = 15 - elif time_of_day == 'night': - weather.sun_altitude_angle = -20 + if time_of_day in WeatherSystem.TIME_PRESETS: + time_preset = WeatherSystem.TIME_PRESETS[time_of_day] + weather.sun_altitude_angle = time_preset['sun_altitude'] + weather.sun_azimuth_angle = time_preset['sun_azimuth'] + + # 根据时间调整亮度 + if time_of_day == 'night': + weather.cloudiness = min(weather.cloudiness + 20, 100) return weather + @staticmethod + def create_dynamic_weather(base_weather, variation=0.1): + """创建动态变化的天气""" + dynamic = carla.WeatherParameters() + + # 基于基础天气添加随机变化 + for attr in ['cloudiness', 'precipitation', 'wind_intensity', 'fog_density']: + if hasattr(base_weather, attr): + base_value = getattr(base_weather, attr) + variation_range = base_value * variation + new_value = max(0, min(100, base_value + random.uniform(-variation_range, variation_range))) + setattr(dynamic, attr, new_value) + + # 保持其他属性 + for attr in ['sun_altitude_angle', 'sun_azimuth_angle', 'precipitation_deposits', 'wetness']: + if hasattr(base_weather, attr): + setattr(dynamic, attr, getattr(base_weather, attr)) + + return dynamic + class ImageProcessor: + """图像处理器(增强版)""" + def __init__(self, output_dir, config=None): self.output_dir = output_dir self.stitched_dir = os.path.join(output_dir, "stitched") os.makedirs(self.stitched_dir, exist_ok=True) - self.compress_images = config.get('compress_images', True) if config else True - self.compression_quality = config.get('compression_quality', 85) if config else 85 - self.enable_memory_cache = config.get('enable_memory_cache', True) if config else True + self.config = config or {} + img_config = self.config.get('image_processing', {}) + + self.compress_images = img_config.get('compress_images', True) + self.compression_quality = img_config.get('compression_quality', 85) + self.enable_memory_cache = img_config.get('enable_memory_cache', True) self.image_cache = {} - self.max_cache_size = config.get('max_cache_size', 50) if config else 50 + self.max_cache_size = img_config.get('max_cache_size', 50) + + self.stitch_layouts = { + 'vehicle_4cam': { + 'canvas_size': (640 * 2 + 20, 360 * 2 + 20), + 'positions': [(10, 10), (660, 10), (10, 390), (660, 390)], + 'tile_size': (640, 360) + }, + 'infrastructure_4cam': { + 'canvas_size': (640 * 2 + 20, 360 * 2 + 20), + 'positions': [(10, 10), (660, 10), (10, 390), (660, 390)], + 'tile_size': (640, 360) + }, + 'wide_view': { + 'canvas_size': (1280, 720), + 'positions': [(0, 0)], + 'tile_size': (1280, 720) + } + } self.stats = { 'images_processed': 0, 'cache_hits': 0, 'cache_misses': 0, - 'total_processing_time': 0 + 'total_processing_time': 0, + 'stitching_errors': 0, + 'saved_size_mb': 0 } def stitch(self, image_paths, frame_num, view_type="vehicle"): + """拼接图像(增强版)""" try: - from PIL import Image, ImageDraw + from PIL import Image, ImageDraw, ImageFont except ImportError: Log.warning("PIL未安装,跳过图像拼接") return False start_time = time.time() + # 检查缓存 cache_key = f"{view_type}_{frame_num}" if self.enable_memory_cache and cache_key in self.image_cache: cached_image = self.image_cache[cache_key] @@ -220,52 +574,98 @@ def stitch(self, image_paths, frame_num, view_type="vehicle"): progressive=True) else: cached_image.save(output_path, "PNG", optimize=True) + + self.stats['cache_hits'] += 1 + self.stats['images_processed'] += 1 + processing_time = time.time() - start_time + self.stats['total_processing_time'] += processing_time + + return True except Exception as e: Log.error(f"保存缓存图像失败: {e}") return False - self.stats['cache_hits'] += 1 - self.stats['images_processed'] += 1 - processing_time = time.time() - start_time - self.stats['total_processing_time'] += processing_time - return True - self.stats['cache_misses'] += 1 - positions = [(10, 10), (660, 10), (10, 390), (660, 390)] + # 获取布局配置 + layout = self.stitch_layouts.get(view_type, self.stitch_layouts['vehicle_4cam']) + canvas_size = layout['canvas_size'] + positions = layout['positions'] + tile_size = layout['tile_size'] - canvas = Image.new('RGB', (640 * 2 + 20, 360 * 2 + 20), (40, 40, 40)) + # 创建画布 + canvas = Image.new('RGB', canvas_size, (40, 40, 40)) draw = ImageDraw.Draw(canvas) + # 尝试加载字体 + try: + font = ImageFont.truetype("arial.ttf", 14) + except: + font = ImageFont.load_default() + images_loaded = 0 - for idx, (cam_name, img_path) in enumerate(list(image_paths.items())[:4]): + image_items = list(image_paths.items()) + + for idx, (cam_name, img_path) in enumerate(image_items[:len(positions)]): if img_path and os.path.exists(img_path): try: + # 检查缓存 if img_path in self.image_cache: img = self.image_cache[img_path] else: + # 加载图像 img = Image.open(img_path) img.load() - img = img.resize((640, 360), Image.Resampling.LANCZOS) + # 调整大小 + if img.size != tile_size: + img = img.resize(tile_size, Image.Resampling.LANCZOS) + + # 缓存图像 if self.enable_memory_cache: self.image_cache[img_path] = img if len(self.image_cache) > self.max_cache_size: self._cleanup_cache() - images_loaded += 1 + + # 添加到画布 + if idx < len(positions): + canvas.paste(img, positions[idx]) + + # 添加摄像机标签 + label = cam_name.replace('_', ' ').title() + draw.text((positions[idx][0] + 5, positions[idx][1] + 5), + label, fill=(255, 255, 200), font=font) + + images_loaded += 1 + except Exception as e: - Log.warning(f"加载图像失败: {e}") - img = Image.new('RGB', (640, 360), (80, 80, 80)) + Log.warning(f"加载图像失败 {cam_name}: {e}") + + # 创建占位图像 + placeholder = Image.new('RGB', tile_size, (80, 80, 80)) + if idx < len(positions): + canvas.paste(placeholder, positions[idx]) + draw.text((positions[idx][0] + 5, positions[idx][1] + 5), + f"Error: {cam_name}", fill=(255, 100, 100), font=font) else: - img = Image.new('RGB', (640, 360), (80, 80, 80)) - - canvas.paste(img, positions[idx]) - draw.text((positions[idx][0] + 5, positions[idx][1] + 5), - cam_name, fill=(255, 255, 200)) + # 创建占位图像 + placeholder = Image.new('RGB', tile_size, (80, 80, 80)) + if idx < len(positions): + canvas.paste(placeholder, positions[idx]) + draw.text((positions[idx][0] + 5, positions[idx][1] + 5), + f"Missing: {cam_name}", fill=(255, 200, 100), font=font) if images_loaded == 0: + Log.warning(f"没有图像加载成功: {view_type}") + self.stats['stitching_errors'] += 1 return False + # 添加帧信息和时间戳 + timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + info_text = f"Frame: {frame_num:06d} | {timestamp} | {view_type} | Images: {images_loaded}/{len(image_items)}" + draw.text((10, canvas_size[1] - 25), info_text, fill=(200, 200, 255), font=font) + + # 保存图像 output_path = os.path.join(self.stitched_dir, f"{view_type}_{frame_num:06d}.jpg") try: @@ -277,36 +677,66 @@ def stitch(self, image_paths, frame_num, view_type="vehicle"): subsampling='4:2:0') else: canvas.save(output_path, "PNG", optimize=True) + + # 计算节省的空间 + original_size = sum([os.path.getsize(p) for p in image_paths.values() if p and os.path.exists(p)]) + stitched_size = os.path.getsize(output_path) + if original_size > 0: + size_saving = (original_size - stitched_size) / (1024 * 1024) + self.stats['saved_size_mb'] += max(0, size_saving) + except Exception as e: Log.error(f"保存图像失败: {e}") + self.stats['stitching_errors'] += 1 return False + # 更新缓存 if self.enable_memory_cache: self.image_cache[cache_key] = canvas.copy() + # 清理 del canvas gc.collect() + # 更新统计 self.stats['images_processed'] += 1 processing_time = time.time() - start_time self.stats['total_processing_time'] += processing_time + # 定期报告 if self.stats['images_processed'] % 50 == 0: - avg_time = self.stats['total_processing_time'] / self.stats['images_processed'] - cache_hit_rate = self.stats['cache_hits'] / max(1, self.stats['cache_hits'] + self.stats['cache_misses']) - Log.performance(f"图像处理统计: 处理{self.stats['images_processed']}张, " - f"平均{avg_time:.3f}秒/张, 缓存命中率{cache_hit_rate:.1%}") + self._report_statistics() return True def _cleanup_cache(self): + """清理缓存""" if len(self.image_cache) > self.max_cache_size: keys_to_remove = list(self.image_cache.keys())[:len(self.image_cache) - self.max_cache_size] for key in keys_to_remove: del self.image_cache[key] - Log.debug(f"清理缓存: 移除了{len(keys_to_remove)}个缓存项") + Log.debug(f"清理图像缓存: 移除了{len(keys_to_remove)}个缓存项") + + def _report_statistics(self): + """报告统计信息""" + total_operations = self.stats['cache_hits'] + self.stats['cache_misses'] + if total_operations > 0: + cache_hit_rate = self.stats['cache_hits'] / total_operations + else: + cache_hit_rate = 0 + + avg_time = 0 + if self.stats['images_processed'] > 0: + avg_time = self.stats['total_processing_time'] / self.stats['images_processed'] + + Log.performance(f"图像处理统计: " + f"处理{self.stats['images_processed']}张, " + f"平均{avg_time:.3f}秒/张, " + f"缓存命中率{cache_hit_rate:.1%}, " + f"节省空间{self.stats['saved_size_mb']:.1f}MB") def get_stats(self): + """获取统计信息""" total_operations = self.stats['cache_hits'] + self.stats['cache_misses'] if total_operations > 0: cache_hit_rate = self.stats['cache_hits'] / total_operations @@ -321,107 +751,170 @@ def get_stats(self): **self.stats, 'cache_hit_rate': cache_hit_rate, 'average_processing_time': avg_time, - 'current_cache_size': len(self.image_cache) + 'current_cache_size': len(self.image_cache), + 'stitching_success_rate': (self.stats['images_processed'] - self.stats['stitching_errors']) / + max(1, self.stats['images_processed']) } class TrafficManager: + """交通管理器(增强版)""" + def __init__(self, world, config): self.world = world self.config = config self.vehicles = [] self.pedestrians = [] + self.bicycles = [] + self.motorcycles = [] - seed = config['scenario'].get('seed', random.randint(1, 1000)) + # 设置随机种子 + seed = config['scenario'].get('seed', random.randint(1, 10000)) random.seed(seed) + np.random.seed(seed) Log.info(f"随机种子: {seed}") self.batch_spawn = config.get('batch_spawn', True) - self.max_spawn_attempts = config.get('max_spawn_attempts', 5) + self.max_spawn_attempts = config.get('max_spawn_attempts', 10) + + # 行人安全相关 + self.pedestrian_safety_zones = config.get('pedestrian_safety_zones', True) + self.crosswalk_density = config.get('crosswalk_density', 'medium') self.spawn_stats = { 'total_attempts': 0, 'successful_spawns': 0, 'failed_spawns': 0, - 'total_spawn_time': 0 + 'total_spawn_time': 0, + 'vehicle_types': {}, + 'pedestrian_types': {} } def spawn_ego_vehicle(self): + """生成主车(增强版)""" blueprint_lib = self.world.get_blueprint_library() - common_vehicles = [ + # 优先选择的车辆类型 + preferred_vehicles = [ 'vehicle.tesla.model3', 'vehicle.audi.tt', - 'vehicle.mini.cooperst', - 'vehicle.nissan.micra' + 'vehicle.nissan.patrol', + 'vehicle.mercedes.coupe', + 'vehicle.bmw.grandtourer', + 'vehicle.lincoln.mkz2017', + 'vehicle.chevrolet.impala' ] - for vtype in common_vehicles: + vehicle_bp = None + for vtype in preferred_vehicles: if blueprint_lib.filter(vtype): vehicle_bp = random.choice(blueprint_lib.filter(vtype)) break - else: + + if not vehicle_bp: vehicle_bp = random.choice(blueprint_lib.filter('vehicle.*')) + # 设置车辆属性 + if vehicle_bp.has_attribute('color'): + colors = vehicle_bp.get_attribute('color').recommended_values + if colors: + vehicle_bp.set_attribute('color', random.choice(colors)) + spawn_points = self.world.get_map().get_spawn_points() if not spawn_points: + Log.error("没有可用的生成点") return None - spawn_point = random.choice(spawn_points) - + # 尝试生成 for attempt in range(self.max_spawn_attempts): self.spawn_stats['total_attempts'] += 1 + spawn_point = random.choice(spawn_points) + try: start_time = time.time() vehicle = self.world.spawn_actor(vehicle_bp, spawn_point) + + # 设置自动驾驶 vehicle.set_autopilot(True) - vehicle.apply_control(carla.VehicleControl(throttle=0.2)) + + # 设置初始控制 + control = carla.VehicleControl() + control.throttle = 0.3 + control.steer = 0.0 + vehicle.apply_control(control) spawn_time = time.time() - start_time self.spawn_stats['total_spawn_time'] += spawn_time self.spawn_stats['successful_spawns'] += 1 - Log.info(f"主车: {vehicle.type_id}, 生成时间: {spawn_time:.3f}秒") + # 记录车辆类型 + vehicle_type = vehicle.type_id + self.spawn_stats['vehicle_types'][vehicle_type] = \ + self.spawn_stats['vehicle_types'].get(vehicle_type, 0) + 1 + + Log.info(f"主车生成: {vehicle_type}, 位置: {spawn_point.location}, 时间: {spawn_time:.3f}秒") + return vehicle + except Exception as e: self.spawn_stats['failed_spawns'] += 1 if attempt == self.max_spawn_attempts - 1: Log.warning(f"主车生成失败: {e}") else: - spawn_point = random.choice(spawn_points) time.sleep(0.1) return None def spawn_traffic(self, center_location): + """生成交通(增强版)""" start_time = time.time() - if self.batch_spawn: - vehicles = self._spawn_vehicles_batch() - else: - vehicles = self._spawn_vehicles() + # 生成车辆 + vehicles_spawned = self._spawn_vehicles_batch() if self.batch_spawn else self._spawn_vehicles() - pedestrians = self._spawn_pedestrians(center_location) + # 生成行人 + pedestrians_spawned = self._spawn_pedestrians(center_location) + + # 生成自行车 + bicycles_spawned = self._spawn_bicycles(center_location) + + # 生成摩托车 + motorcycles_spawned = self._spawn_motorcycles(center_location) total_time = time.time() - start_time - Log.info(f"交通生成: {vehicles}辆车, {pedestrians}个行人, 用时: {total_time:.2f}秒") + Log.info(f"交通生成完成: " + f"{vehicles_spawned}辆车, " + f"{pedestrians_spawned}个行人, " + f"{bicycles_spawned}辆自行车, " + f"{motorcycles_spawned}辆摩托车, " + f"用时: {total_time:.2f}秒") + + # 生成统计报告 success_rate = self.spawn_stats['successful_spawns'] / max(1, self.spawn_stats['total_attempts']) avg_spawn_time = self.spawn_stats['total_spawn_time'] / max(1, self.spawn_stats['successful_spawns']) - Log.performance(f"生成统计: 成功率{success_rate:.1%}, 平均生成时间{avg_spawn_time:.3f}秒") - return vehicles + pedestrians + Log.performance(f"生成统计: " + f"成功率{success_rate:.1%}, " + f"平均生成时间{avg_spawn_time:.3f}秒, " + f"车辆类型: {len(self.spawn_stats['vehicle_types'])}种, " + f"行人类型: {len(self.spawn_stats['pedestrian_types'])}种") + + return vehicles_spawned + pedestrians_spawned + bicycles_spawned + motorcycles_spawned def _spawn_vehicles_batch(self): + """批量生成车辆""" blueprint_lib = self.world.get_blueprint_library() spawn_points = self.world.get_map().get_spawn_points() if not spawn_points: + Log.warning("没有可用的车辆生成点") return 0 - num_vehicles = min(self.config['traffic']['background_vehicles'], 10) + num_vehicles = min(self.config['traffic']['background_vehicles'], 20) spawned = 0 + # 准备批处理命令 batch_commands = [] available_points = spawn_points.copy() random.shuffle(available_points) @@ -431,80 +924,240 @@ def _spawn_vehicles_batch(self): break try: - vehicle_bp = random.choice(blueprint_lib.filter('vehicle.*')) + # 选择车辆类型 + vehicle_types = self.config['traffic']['vehicle_types'] + available_types = [vt for vt in vehicle_types if blueprint_lib.filter(vt)] + + if not available_types: + vehicle_bp = random.choice(blueprint_lib.filter('vehicle.*')) + else: + vehicle_bp = random.choice(blueprint_lib.filter(random.choice(available_types))) + + # 设置颜色 + if vehicle_bp.has_attribute('color'): + colors = vehicle_bp.get_attribute('color').recommended_values + if colors: + vehicle_bp.set_attribute('color', random.choice(colors)) + spawn_point = available_points[i] - batch_commands.append((vehicle_bp, spawn_point)) - except: - pass - for vehicle_bp, spawn_point in batch_commands: + # 添加随机偏移 + offset_x = random.uniform(-3.0, 3.0) + offset_y = random.uniform(-3.0, 3.0) + location = carla.Location( + x=spawn_point.location.x + offset_x, + y=spawn_point.location.y + offset_y, + z=spawn_point.location.z + ) + + # 稍微调整方向 + rotation = carla.Rotation( + pitch=spawn_point.rotation.pitch, + yaw=spawn_point.rotation.yaw + random.uniform(-10, 10), + roll=spawn_point.rotation.roll + ) + + transform = carla.Transform(location, rotation) + batch_commands.append((vehicle_bp, transform)) + + except Exception as e: + Log.debug(f"准备车辆生成命令失败: {e}") + continue + + # 执行批处理生成 + for vehicle_bp, transform in batch_commands: try: - vehicle = self.world.spawn_actor(vehicle_bp, spawn_point) + vehicle = self.world.spawn_actor(vehicle_bp, transform) vehicle.set_autopilot(True) + + # 设置初始速度 + control = carla.VehicleControl() + control.throttle = random.uniform(0.2, 0.5) + vehicle.apply_control(control) + self.vehicles.append(vehicle) spawned += 1 self.spawn_stats['successful_spawns'] += 1 self.spawn_stats['total_attempts'] += 1 - except: + + # 记录类型 + vehicle_type = vehicle.type_id + self.spawn_stats['vehicle_types'][vehicle_type] = \ + self.spawn_stats['vehicle_types'].get(vehicle_type, 0) + 1 + + except Exception as e: self.spawn_stats['failed_spawns'] += 1 self.spawn_stats['total_attempts'] += 1 - pass + Log.debug(f"车辆生成失败: {e}") + # 小延迟以避免过载 if spawned % 3 == 0: time.sleep(0.05) return spawned - def _spawn_vehicles(self): + def _spawn_pedestrians(self, center_location): + """生成行人(增强版)""" blueprint_lib = self.world.get_blueprint_library() - spawn_points = self.world.get_map().get_spawn_points() - if not spawn_points: + num_peds = min(self.config['traffic']['pedestrians'], 30) + spawned = 0 + + # 获取行人类型 + pedestrian_types = self.config['traffic']['pedestrian_types'] + available_types = [pt for pt in pedestrian_types if blueprint_lib.filter(pt)] + + if not available_types: + available_types = list(blueprint_lib.filter('walker.pedestrian.*')) + + if not available_types: + Log.warning("没有可用的行人生成蓝图") return 0 - num_vehicles = min(self.config['traffic']['background_vehicles'], 10) + for i in range(num_peds): + try: + # 选择行人类型 + ped_bp = random.choice(available_types) + + # 根据场景类型调整位置 + scenario_name = self.config['scenario']['name'].lower() + + if 'school' in scenario_name or 'crossing' in scenario_name: + # 学校或人行横道场景:行人在中心区域更密集 + angle = random.uniform(0, 2 * math.pi) + if i < num_peds * 0.7: # 70%的行人在中心区域 + distance = random.uniform(3.0, 10.0) + else: + distance = random.uniform(10.0, 20.0) + else: + # 普通场景 + angle = random.uniform(0, 2 * math.pi) + distance = random.uniform(5.0, 25.0) + + # 计算位置 + location = carla.Location( + x=center_location.x + distance * math.cos(angle), + y=center_location.y + distance * math.sin(angle), + z=center_location.z + 0.5 + ) + + # 确保位置在地面上 + waypoint = self.world.get_map().get_waypoint(location) + if waypoint: + location.z = waypoint.transform.location.z + 0.5 + + # 创建行人 + pedestrian = self.world.spawn_actor(ped_bp, carla.Transform(location)) + + # 设置行人控制器 + controller_bp = blueprint_lib.find('controller.ai.walker') + if controller_bp: + controller = self.world.spawn_actor(controller_bp, carla.Transform(), attach_to=pedestrian) + controller.start() + + # 设置目标位置 + target_location = carla.Location( + x=center_location.x + random.uniform(-20, 20), + y=center_location.y + random.uniform(-20, 20), + z=location.z + ) + controller.go_to_location(target_location) + + # 设置速度 + controller.set_max_speed(random.uniform(0.5, 2.0)) + + self.pedestrians.append(pedestrian) + spawned += 1 + + self.spawn_stats['successful_spawns'] += 1 + self.spawn_stats['total_attempts'] += 1 + + # 记录类型 + pedestrian_type = pedestrian.type_id + self.spawn_stats['pedestrian_types'][pedestrian_type] = \ + self.spawn_stats['pedestrian_types'].get(pedestrian_type, 0) + 1 + + except Exception as e: + self.spawn_stats['failed_spawns'] += 1 + self.spawn_stats['total_attempts'] += 1 + Log.debug(f"行人生成失败: {e}") + + return spawned + + def _spawn_bicycles(self, center_location): + """生成自行车""" + blueprint_lib = self.world.get_blueprint_library() + + num_bikes = min(self.config['traffic'].get('bicycles', 0), 10) spawned = 0 - for _ in range(num_vehicles): + # 查找自行车蓝图 + bike_bps = list(blueprint_lib.filter('vehicle.bh*')) + list(blueprint_lib.filter('vehicle.diamondback*')) + + if not bike_bps: + Log.debug("没有可用的自行车蓝图") + return 0 + + for i in range(num_bikes): try: - vehicle_bp = random.choice(blueprint_lib.filter('vehicle.*')) - spawn_point = random.choice(spawn_points) - vehicle = self.world.spawn_actor(vehicle_bp, spawn_point) - vehicle.set_autopilot(True) - self.vehicles.append(vehicle) + bike_bp = random.choice(bike_bps) + + # 计算位置(在行人附近) + angle = random.uniform(0, 2 * math.pi) + distance = random.uniform(8.0, 15.0) + + location = carla.Location( + x=center_location.x + distance * math.cos(angle), + y=center_location.y + distance * math.sin(angle), + z=center_location.z + 0.3 + ) + + # 确保位置在地面上 + waypoint = self.world.get_map().get_waypoint(location) + if waypoint: + location.z = waypoint.transform.location.z + 0.3 + + # 创建自行车 + rotation = carla.Rotation(0, random.uniform(0, 360), 0) + bicycle = self.world.spawn_actor(bike_bp, carla.Transform(location, rotation)) + + self.bicycles.append(bicycle) spawned += 1 self.spawn_stats['successful_spawns'] += 1 self.spawn_stats['total_attempts'] += 1 - except: + + except Exception as e: self.spawn_stats['failed_spawns'] += 1 self.spawn_stats['total_attempts'] += 1 - pass + Log.debug(f"自行车生成失败: {e}") return spawned - def _spawn_pedestrians(self, center_location): + def _spawn_motorcycles(self, center_location): + """生成摩托车""" blueprint_lib = self.world.get_blueprint_library() - num_peds = min(self.config['traffic']['pedestrians'], 12) # 增加行人数量 + num_motos = min(self.config['traffic'].get('motorcycles', 0), 5) spawned = 0 - for _ in range(num_peds): - try: - ped_bps = list(blueprint_lib.filter('walker.pedestrian.*')) - if not ped_bps: - continue + # 查找摩托车蓝图 + moto_bps = list(blueprint_lib.filter('vehicle.harley-davidson*')) + \ + list(blueprint_lib.filter('vehicle.yamaha*')) + \ + list(blueprint_lib.filter('vehicle.kawasaki*')) - ped_bp = random.choice(ped_bps) + if not moto_bps: + Log.debug("没有可用的摩托车蓝图") + return 0 - # 在学校区域或人行横道场景中,行人更集中 - if self.config.get('scenario', {}).get('name', '').lower() in ['school_zone', 'pedestrian_crossing']: - angle = random.uniform(0, 2 * math.pi) - distance = random.uniform(3.0, 8.0) # 更靠近中心 - else: - angle = random.uniform(0, 2 * math.pi) - distance = random.uniform(5.0, 15.0) + for i in range(num_motos): + try: + moto_bp = random.choice(moto_bps) + + # 计算位置 + angle = random.uniform(0, 2 * math.pi) + distance = random.uniform(10.0, 20.0) location = carla.Location( x=center_location.x + distance * math.cos(angle), @@ -512,48 +1165,125 @@ def _spawn_pedestrians(self, center_location): z=center_location.z + 0.5 ) - pedestrian = self.world.spawn_actor(ped_bp, carla.Transform(location)) - self.pedestrians.append(pedestrian) + # 确保位置在地面上 + waypoint = self.world.get_map().get_waypoint(location) + if waypoint: + location.z = waypoint.transform.location.z + 0.5 + + # 创建摩托车 + rotation = carla.Rotation(0, random.uniform(0, 360), 0) + motorcycle = self.world.spawn_actor(moto_bp, carla.Transform(location, rotation)) + motorcycle.set_autopilot(True) + + self.motorcycles.append(motorcycle) spawned += 1 self.spawn_stats['successful_spawns'] += 1 self.spawn_stats['total_attempts'] += 1 + except Exception as e: self.spawn_stats['failed_spawns'] += 1 self.spawn_stats['total_attempts'] += 1 - Log.debug(f"行人生成失败: {e}") + Log.debug(f"摩托车生成失败: {e}") return spawned + def add_pedestrian_safety_features(self, center_location): + """添加行人安全设施""" + features = [] + + try: + # 添加交通锥 + if self.pedestrian_safety_zones: + from scene_manager import SceneManager + cones = SceneManager.spawn_traffic_cones(self.world, center_location, num_cones=8) + features.extend(cones) + + # 添加行人横道标记 + if self.crosswalk_density in ['medium', 'high']: + from scene_manager import SceneManager + crosswalks = SceneManager.spawn_pedestrian_safety_features( + self.world, center_location, 'crosswalk') + features.extend(crosswalks) + + Log.info(f"添加了 {len(features)} 个行人安全设施") + + except Exception as e: + Log.warning(f"添加行人安全设施失败: {e}") + + return features + def cleanup(self): + """清理交通管理器""" Log.info("开始清理交通管理器...") + cleanup_stats = { + 'vehicles_destroyed': 0, + 'pedestrians_destroyed': 0, + 'bicycles_destroyed': 0, + 'motorcycles_destroyed': 0, + 'errors': 0 + } + try: + # 清理行人 for pedestrian in self.pedestrians: try: if pedestrian and pedestrian.is_alive: pedestrian.destroy() + cleanup_stats['pedestrians_destroyed'] += 1 except: - pass + cleanup_stats['errors'] += 1 self.pedestrians.clear() + # 清理自行车 + for bicycle in self.bicycles: + try: + if bicycle and bicycle.is_alive: + bicycle.destroy() + cleanup_stats['bicycles_destroyed'] += 1 + except: + cleanup_stats['errors'] += 1 + + self.bicycles.clear() + + # 清理摩托车 + for motorcycle in self.motorcycles: + try: + if motorcycle and motorcycle.is_alive: + motorcycle.destroy() + cleanup_stats['motorcycles_destroyed'] += 1 + except: + cleanup_stats['errors'] += 1 + + self.motorcycles.clear() + + # 清理车辆 for vehicle in self.vehicles: try: if vehicle and vehicle.is_alive: vehicle.destroy() + cleanup_stats['vehicles_destroyed'] += 1 except: - pass + cleanup_stats['errors'] += 1 self.vehicles.clear() - Log.info("交通管理器清理完成") + Log.info(f"交通管理器清理完成: " + f"车辆{cleanup_stats['vehicles_destroyed']}, " + f"行人{cleanup_stats['pedestrians_destroyed']}, " + f"自行车{cleanup_stats['bicycles_destroyed']}, " + f"摩托车{cleanup_stats['motorcycles_destroyed']}, " + f"错误{cleanup_stats['errors']}") except Exception as e: Log.error(f"清理交通管理器失败: {e}") class SensorManager: + """传感器管理器(增强版)""" + def __init__(self, world, config, data_dir): self.world = world self.config = config @@ -564,56 +1294,99 @@ def __init__(self, world, config, data_dir): self.last_capture_time = 0 self.last_performance_sample = 0 - self.target_fps = config['performance'].get('frame_rate_limit', 5.0) - self.min_frame_interval = 1.0 / self.target_fps if self.target_fps > 0 else 0.1 + # 帧率控制 + self.target_fps = config['performance'].get('frame_rate_limit', 8.0) + self.min_frame_interval = 1.0 / self.target_fps if self.target_fps > 0 else 0.125 self.last_frame_time = 0 self.frame_skip_count = 0 - self.max_frame_skip = 2 + self.max_frame_skip = 3 + # 数据缓冲 self.vehicle_buffer = {} self.infra_buffer = {} - self.buffer_lock = threading.Lock() + self.buffer_lock = threading.RLock() + # 状态控制 self.is_running = True + self.is_paused = False - self.performance_monitor = PerformanceMonitor() + # 性能监控 + self.performance_monitor = PerformanceMonitor(config) + # 处理器 self.image_processor = ImageProcessor(data_dir, config.get('image_processing', {})) self.lidar_processor = None self.fusion_manager = None - self.batch_size = config['performance'].get('batch_size', 5) - self.enable_async_processing = config.get('enable_async_processing', True) + # 配置参数 + self.batch_size = config['performance'].get('batch_size', 8) + self.enable_async_processing = config['performance'].get('enable_async_processing', True) + self.max_workers = config['performance'].get('max_workers', 4) + # LiDAR处理 if config['sensors'].get('lidar_sensors', 0) > 0: - self.lidar_processor = LidarProcessor(data_dir, config['performance'].get('lidar_processing', {})) + lidar_config = config['performance'].get('lidar_processing', {}) + self.lidar_processor = LidarProcessor(data_dir, lidar_config) + # 融合处理 if config['output'].get('save_fusion', False): - self.fusion_manager = MultiSensorFusion(data_dir, config['performance'].get('fusion', {})) + fusion_config = config['performance'].get('fusion', {}) + self.fusion_manager = MultiSensorFusion(data_dir, fusion_config) + # 统计 self.sensor_stats = { 'total_images': 0, 'total_lidar_frames': 0, 'image_capture_times': [], - 'lidar_processing_times': [] + 'lidar_processing_times': [], + 'sensor_errors': 0, + 'dropped_frames': 0 } def setup_cameras(self, vehicle, center_location, vehicle_id=0): + """设置摄像头(增强版)""" vehicle_cams = self._setup_vehicle_cameras(vehicle, vehicle_id) infra_cams = self._setup_infrastructure_cameras(center_location) - Log.info(f"摄像头: {vehicle_cams}车辆 + {infra_cams}基础设施") + Log.info(f"摄像头设置完成: {vehicle_cams}车辆摄像头 + {infra_cams}基础设施摄像头") return vehicle_cams + infra_cams def _setup_vehicle_cameras(self, vehicle, vehicle_id): + """设置车辆摄像头""" if not vehicle: return 0 camera_configs = { - 'front_wide': {'loc': (2.0, 0, 1.8), 'rot': (0, -3, 0), 'fov': 100}, - 'front_narrow': {'loc': (2.0, 0, 1.6), 'rot': (0, 0, 0), 'fov': 60}, - 'right_side': {'loc': (0.5, 1.0, 1.5), 'rot': (0, -2, 45), 'fov': 90}, - 'left_side': {'loc': (0.5, -1.0, 1.5), 'rot': (0, -2, -45), 'fov': 90} + 'front_wide': { + 'loc': (2.0, 0, 1.8), + 'rot': (0, -3, 0), + 'fov': 120, + 'description': '前视广角摄像头' + }, + 'front_narrow': { + 'loc': (2.0, 0, 1.6), + 'rot': (0, 0, 0), + 'fov': 60, + 'description': '前视窄角摄像头' + }, + 'right_side': { + 'loc': (0.5, 1.0, 1.5), + 'rot': (0, -2, 90), + 'fov': 100, + 'description': '右侧摄像头' + }, + 'left_side': { + 'loc': (0.5, -1.0, 1.5), + 'rot': (0, -2, -90), + 'fov': 100, + 'description': '左侧摄像头' + }, + 'rear': { + 'loc': (-1.5, 0, 1.7), + 'rot': (0, -5, 180), + 'fov': 100, + 'description': '后视摄像头' + } } installed = 0 @@ -624,11 +1397,38 @@ def _setup_vehicle_cameras(self, vehicle, vehicle_id): return installed def _setup_infrastructure_cameras(self, center_location): + """设置基础设施摄像头""" camera_configs = [ - {'name': 'north', 'offset': (0, -20, 12), 'rotation': (0, -25, 180)}, - {'name': 'south', 'offset': (0, 20, 12), 'rotation': (0, -25, 0)}, - {'name': 'east', 'offset': (20, 0, 12), 'rotation': (0, -25, -90)}, - {'name': 'west', 'offset': (-20, 0, 12), 'rotation': (0, -25, 90)} + { + 'name': 'north', + 'offset': (0, -25, 15), + 'rotation': (0, -30, 180), + 'description': '北向路口摄像头' + }, + { + 'name': 'south', + 'offset': (0, 25, 15), + 'rotation': (0, -30, 0), + 'description': '南向路口摄像头' + }, + { + 'name': 'east', + 'offset': (25, 0, 15), + 'rotation': (0, -30, -90), + 'description': '东向路口摄像头' + }, + { + 'name': 'west', + 'offset': (-25, 0, 15), + 'rotation': (0, -30, 90), + 'description': '西向路口摄像头' + }, + { + 'name': 'overhead', + 'offset': (0, 0, 30), + 'rotation': (-90, 0, 0), + 'description': '俯视摄像头' + } ] installed = 0 @@ -640,7 +1440,8 @@ def _setup_infrastructure_cameras(self, center_location): center_location.z + cam_config['offset'][2] ), 'rot': cam_config['rotation'], - 'fov': 90 + 'fov': 90, + 'description': cam_config['description'] } if self._create_camera(cam_config['name'], sensor_config, None, 'infrastructure'): @@ -649,6 +1450,7 @@ def _setup_infrastructure_cameras(self, center_location): return installed def setup_lidar(self, vehicle, vehicle_id=0): + """设置LiDAR传感器""" if not vehicle or not self.config['sensors'].get('lidar_sensors', 0) > 0: return 0 @@ -656,40 +1458,54 @@ def setup_lidar(self, vehicle, vehicle_id=0): blueprint_lib = self.world.get_blueprint_library() lidar_bp = blueprint_lib.find('sensor.lidar.ray_cast') + if not lidar_bp: + Log.warning("未找到LiDAR传感器蓝图") + return 0 + lidar_config = self.config['sensors'].get('lidar_config', {}) + # 设置LiDAR参数 lidar_bp.set_attribute('channels', str(lidar_config.get('channels', 32))) lidar_bp.set_attribute('range', str(lidar_config.get('range', 100))) - lidar_bp.set_attribute('points_per_second', str(lidar_config.get('points_per_second', 56000))) + lidar_bp.set_attribute('points_per_second', str(lidar_config.get('points_per_second', 100000))) lidar_bp.set_attribute('rotation_frequency', str(lidar_config.get('rotation_frequency', 10))) - lidar_bp.set_attribute('upper_fov', '10') - lidar_bp.set_attribute('lower_fov', '-20') + # 设置视野 + lidar_bp.set_attribute('upper_fov', str(lidar_config.get('upper_fov', 15))) + lidar_bp.set_attribute('lower_fov', str(lidar_config.get('lower_fov', -25))) lidar_bp.set_attribute('horizontal_fov', '360') + # 设置位置和方向 lidar_location = carla.Location(x=0, y=0, z=2.5) lidar_rotation = carla.Rotation(0, 0, 0) lidar_transform = carla.Transform(lidar_location, lidar_rotation) + # 生成LiDAR传感器 lidar_sensor = self.world.spawn_actor(lidar_bp, lidar_transform, attach_to=vehicle) def lidar_callback(lidar_data): - if not self.is_running: + """LiDAR数据回调""" + if not self.is_running or self.is_paused: return try: current_time = time.time() frame_start_time = time.time() + # 检查采集间隔 if current_time - self.last_capture_time >= self.config['sensors']['capture_interval']: if self.lidar_processor: try: start_process = time.time() + + # 处理LiDAR数据 metadata = self.lidar_processor.process_lidar_data(lidar_data, self.frame_counter) + process_time = time.time() - start_process self.sensor_stats['lidar_processing_times'].append(process_time) self.sensor_stats['total_lidar_frames'] += 1 + # 创建融合文件 if metadata and self.fusion_manager: vehicle_image_path = None with self.buffer_lock: @@ -706,21 +1522,28 @@ def lidar_callback(lidar_data): if vehicle_image_path: sensor_data['camera'] = vehicle_image_path - self.fusion_manager.create_synchronization_file(self.frame_counter, sensor_data) + self.fusion_manager.create_synchronization_file( + self.frame_counter, sensor_data) + except Exception as e: + self.sensor_stats['sensor_errors'] += 1 if self.is_running: Log.error(f"LiDAR处理失败: {e}") + # 记录帧时间 frame_time = time.time() - frame_start_time self.performance_monitor.record_frame_time(frame_time) + except Exception as e: + self.sensor_stats['sensor_errors'] += 1 if self.is_running: Log.error(f"LiDAR回调错误: {e}") + # 开始监听 lidar_sensor.listen(lidar_callback) self.sensors.append(lidar_sensor) - Log.info("LiDAR传感器已安装") + Log.info(f"LiDAR传感器已安装: {lidar_bp.id}, 通道数: {lidar_config.get('channels', 32)}") return 1 except Exception as e: @@ -728,23 +1551,37 @@ def lidar_callback(lidar_data): return 0 def _create_camera(self, name, config, parent, sensor_type, vehicle_id=0): + """创建摄像头""" try: blueprint = self.world.get_blueprint_library().find('sensor.camera.rgb') + if not blueprint: + Log.warning(f"未找到摄像头蓝图: sensor.camera.rgb") + return False + + # 设置摄像头参数 img_size = self.config['sensors'].get('image_size', [1280, 720]) blueprint.set_attribute('image_size_x', str(img_size[0])) blueprint.set_attribute('image_size_y', str(img_size[1])) blueprint.set_attribute('fov', str(config.get('fov', 90))) + # 设置后处理效果 + camera_config = self.config['sensors'].get('camera_config', {}) + if camera_config.get('post_processing') == 'semantic': + blueprint.set_attribute('enable_postprocess_effects', 'True') + + # 设置位置和方向 location = carla.Location(config['loc'][0], config['loc'][1], config['loc'][2]) rotation = carla.Rotation(config['rot'][0], config['rot'][1], config['rot'][2]) transform = carla.Transform(location, rotation) + # 生成摄像头 if parent: camera = self.world.spawn_actor(blueprint, transform, attach_to=parent) else: camera = self.world.spawn_actor(blueprint, transform) + # 创建保存目录 if sensor_type == 'vehicle' and vehicle_id > 0: save_dir = os.path.join(self.data_dir, "raw", f"vehicle_{vehicle_id}", name) else: @@ -752,10 +1589,13 @@ def _create_camera(self, name, config, parent, sensor_type, vehicle_id=0): os.makedirs(save_dir, exist_ok=True) + # 创建回调函数 callback = self._create_callback(save_dir, name, sensor_type, vehicle_id) camera.listen(callback) self.sensors.append(camera) + + Log.debug(f"摄像头创建成功: {name} ({sensor_type}), 位置: {location}") return True except Exception as e: @@ -763,31 +1603,37 @@ def _create_camera(self, name, config, parent, sensor_type, vehicle_id=0): return False def _create_callback(self, save_dir, name, sensor_type, vehicle_id=0): + """创建回调函数""" capture_interval = self.config['sensors']['capture_interval'] def callback(image): - if not self.is_running: + """摄像头数据回调""" + if not self.is_running or self.is_paused: return try: current_time = time.time() frame_start_time = time.time() + # 帧率控制 time_since_last_frame = current_time - self.last_frame_time if time_since_last_frame < self.min_frame_interval: self.frame_skip_count += 1 if self.frame_skip_count > self.max_frame_skip: self.frame_skip_count = 0 else: + self.sensor_stats['dropped_frames'] += 1 return else: self.frame_skip_count = 0 + # 检查采集间隔 if current_time - self.last_capture_time >= capture_interval: self.frame_counter += 1 self.last_capture_time = current_time self.last_frame_time = current_time + # 保存图像 capture_start = time.time() filename = os.path.join(save_dir, f"{name}_{self.frame_counter:06d}.png") image.save_to_disk(filename, carla.ColorConverter.Raw) @@ -796,103 +1642,142 @@ def callback(image): self.sensor_stats['image_capture_times'].append(capture_time) self.sensor_stats['total_images'] += 1 + # 缓冲图像路径 with self.buffer_lock: if sensor_type == 'vehicle': self.vehicle_buffer[name] = filename + + # 检查是否触发拼接 if len(self.vehicle_buffer) >= 4: - if self.enable_async_processing: - import concurrent.futures - with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor: - future = executor.submit( - self.image_processor.stitch, - self.vehicle_buffer.copy(), - self.frame_counter, - f'vehicle_{vehicle_id}' - ) - else: - self.image_processor.stitch(self.vehicle_buffer, self.frame_counter, - f'vehicle_{vehicle_id}') + self._trigger_stitching(self.vehicle_buffer.copy(), + self.frame_counter, + f'vehicle_{vehicle_id}') self.vehicle_buffer.clear() else: self.infra_buffer[name] = filename + + # 检查是否触发拼接 if len(self.infra_buffer) >= 4: - if self.enable_async_processing: - import concurrent.futures - with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor: - future = executor.submit( - self.image_processor.stitch, - self.infra_buffer.copy(), - self.frame_counter, - 'infrastructure' - ) - else: - self.image_processor.stitch(self.infra_buffer, self.frame_counter, 'infrastructure') + self._trigger_stitching(self.infra_buffer.copy(), + self.frame_counter, + 'infrastructure') self.infra_buffer.clear() + # 性能监控 if current_time - self.last_performance_sample >= 5.0: memory_info = self.performance_monitor.sample_memory() cpu_info = self.performance_monitor.sample_cpu() - if self.frame_counter % 10 == 0: - Log.performance(f"系统监控 - 内存: {memory_info['process_mb']:.1f}MB, " + if self.frame_counter % 20 == 0: + Log.performance(f"系统监控 - " + f"内存: {memory_info['process_mb']:.1f}MB, " f"CPU: {cpu_info['total_percent']:.1f}%, " - f"线程数: {threading.active_count()}") + f"线程数: {threading.active_count()}, " + f"帧数: {self.frame_counter}") self.last_performance_sample = current_time + # 记录帧时间 frame_time = time.time() - frame_start_time self.performance_monitor.record_frame_time(frame_time) + # 定期垃圾回收 if self.frame_counter % 50 == 0: gc.collect() + # 报告传感器统计 if self.sensor_stats['total_images'] > 0: - avg_capture_time = np.mean(self.sensor_stats['image_capture_times'][-100:]) if len( - self.sensor_stats['image_capture_times']) > 0 else 0 - Log.performance(f"传感器统计: 图像{self.sensor_stats['total_images']}张, " - f"平均捕获时间{avg_capture_time:.3f}秒") + avg_capture_time = np.mean(self.sensor_stats['image_capture_times'][-100:]) \ + if len(self.sensor_stats['image_capture_times']) > 0 else 0 + Log.performance(f"传感器统计: " + f"图像{self.sensor_stats['total_images']}张, " + f"平均捕获时间{avg_capture_time:.3f}秒, " + f"错误{self.sensor_stats['sensor_errors']}个") + except Exception as e: + self.sensor_stats['sensor_errors'] += 1 if self.is_running: Log.error(f"传感器回调错误: {e}") return callback + def _trigger_stitching(self, image_buffer, frame_num, view_type): + """触发图像拼接""" + if self.enable_async_processing: + try: + from concurrent.futures import ThreadPoolExecutor + + # 使用线程池异步处理 + with ThreadPoolExecutor(max_workers=min(2, self.max_workers)) as executor: + future = executor.submit( + self.image_processor.stitch, + image_buffer, + frame_num, + view_type + ) + # 可以在这里添加回调处理结果 + except: + # 异步处理失败,回退到同步处理 + self.image_processor.stitch(image_buffer, frame_num, view_type) + else: + # 同步处理 + self.image_processor.stitch(image_buffer, frame_num, view_type) + def get_frame_count(self): + """获取帧计数""" return self.frame_counter + def pause(self): + """暂停传感器""" + self.is_paused = True + + def resume(self): + """恢复传感器""" + self.is_paused = False + def generate_sensor_summary(self): + """生成传感器摘要""" summary = { 'total_sensors': len(self.sensors), 'frame_count': self.frame_counter, 'lidar_data': None, 'fusion_data': None, 'performance': self.performance_monitor.get_performance_summary(), - 'sensor_stats': self.sensor_stats + 'sensor_stats': self.sensor_stats, + 'image_processor_stats': self.image_processor.get_stats() } + # 添加图像捕获统计 if self.sensor_stats['image_capture_times']: - summary['sensor_stats']['avg_image_capture_time'] = np.mean(self.sensor_stats['image_capture_times']) - summary['sensor_stats']['max_image_capture_time'] = np.max(self.sensor_stats['image_capture_times']) + summary['sensor_stats']['avg_image_capture_time'] = \ + np.mean(self.sensor_stats['image_capture_times']) + summary['sensor_stats']['max_image_capture_time'] = \ + max(self.sensor_stats['image_capture_times']) + # 添加LiDAR处理统计 if self.sensor_stats['lidar_processing_times']: - summary['sensor_stats']['avg_lidar_process_time'] = np.mean(self.sensor_stats['lidar_processing_times']) - summary['sensor_stats']['max_lidar_process_time'] = np.max(self.sensor_stats['lidar_processing_times']) + summary['sensor_stats']['avg_lidar_process_time'] = \ + np.mean(self.sensor_stats['lidar_processing_times']) + summary['sensor_stats']['max_lidar_process_time'] = \ + max(self.sensor_stats['lidar_processing_times']) + # LiDAR数据摘要 if self.lidar_processor: summary['lidar_data'] = self.lidar_processor.generate_lidar_summary() + # 融合数据摘要 if self.fusion_manager: summary['fusion_data'] = self.fusion_manager.generate_fusion_report() - summary['image_processor_stats'] = self.image_processor.get_stats() - return summary def cleanup(self): + """清理传感器""" Log.info(f"安全清理 {len(self.sensors)} 个传感器...") self.is_running = False + # 停止所有传感器 for sensor in self.sensors: try: if hasattr(sensor, 'stop'): @@ -903,64 +1788,48 @@ def cleanup(self): time.sleep(0.2) + # 刷新LiDAR批处理数据 if self.lidar_processor: try: self.lidar_processor.flush_batch() + self.lidar_processor.cleanup() except: pass + # 销毁传感器 for i, sensor in enumerate(self.sensors): try: if hasattr(sensor, 'destroy'): sensor.destroy() except: pass + if i % 5 == 0: time.sleep(0.01) self.sensors.clear() + # 清理图像缓存 if hasattr(self.image_processor, 'image_cache'): try: self.image_processor.image_cache.clear() except: pass - self.vehicle_buffer.clear() - self.infra_buffer.clear() + # 清理数据缓冲 + with self.buffer_lock: + self.vehicle_buffer.clear() + self.infra_buffer.clear() + # 垃圾回收 gc.collect() Log.info("传感器清理完成") -class V2XCommunication: - def __init__(self, config): - self.config = config - self.nodes = {} - self.messages = [] - - def register_node(self, node_id, position, capabilities): - self.nodes[node_id] = { - 'position': position, - 'capabilities': capabilities, - 'last_update': time.time() - } - - def broadcast_basic_safety_message(self, sender_id, vehicle_data): - pass - - def get_messages_for_node(self, node_id): - return [] - - def get_network_status(self): - return {'nodes': len(self.nodes), 'messages': len(self.messages)} - - def stop(self): - pass - - class DataCollector: + """数据收集器(增强版)""" + def __init__(self, config): self.config = config self.client = None @@ -968,23 +1837,36 @@ def __init__(self, config): self.ego_vehicles = [] self.scene_center = None + # 设置输出目录 self.setup_directories() + # 管理器 self.traffic_manager = None self.sensor_managers = {} self.multi_vehicle_manager = None self.v2x_communication = None self.safety_monitor = None + self.scene_manager = None + # 状态 self.start_time = None self.is_running = False self.collected_frames = 0 - self.performance_monitor = PerformanceMonitor() + # 性能监控 + self.performance_monitor = PerformanceMonitor(config) + + # 紧急处理 + self.emergency_handler = EmergencyHandler(self) + # 输出格式 self.output_format = config.get('output_format', 'standard') + # 行人安全增强 + self.pedestrian_safety_mode = config['scenario'].get('pedestrian_safety_mode', True) + def setup_directories(self): + """设置输出目录""" scenario = self.config['scenario'] timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") @@ -993,6 +1875,7 @@ def setup_directories(self): f"{scenario['name']}_{scenario['town']}_{timestamp}" ) + # 创建必要目录 directories = [ "raw/vehicle_1", "raw/vehicle_2", @@ -1001,12 +1884,15 @@ def setup_directories(self): "lidar", "fusion", "calibration", - "cooperative", - "v2x_messages", + "cooperative/v2x_messages", + "cooperative/shared_perception", "v2xformer_format", "kitti_format", "metadata", - "safety_reports" + "safety_reports", + "risk_maps", + "emergency_events", + "enhanced" ] for subdir in directories: @@ -1015,54 +1901,72 @@ def setup_directories(self): Log.info(f"数据目录: {self.output_dir}") def connect(self): + """连接到CARLA服务器""" for attempt in range(1, 6): try: self.client = carla.Client('localhost', 2000) - self.client.set_timeout(10.0) + self.client.set_timeout(15.0) town = self.config['scenario']['town'] self.world = self.client.load_world(town) + # 设置世界参数 settings = self.world.get_settings() settings.synchronous_mode = False + settings.fixed_delta_seconds = 0.05 self.world.apply_settings(settings) - Log.info(f"连接成功: {town}") + Log.info(f"连接成功: {town} (尝试 {attempt}/5)") return True except Exception as e: - Log.warning(f"连接尝试 {attempt}/5 失败: {str(e)[:50]}") - time.sleep(2) + Log.warning(f"连接尝试 {attempt}/5 失败: {str(e)[:100]}") + time.sleep(3) return False def setup_scene(self): + """设置场景(增强版)""" + # 设置天气 weather_cfg = self.config['scenario'] weather = WeatherSystem.create_weather(weather_cfg['weather'], weather_cfg['time_of_day']) self.world.set_weather(weather) Log.info(f"天气: {weather_cfg['weather']}, 时间: {weather_cfg['time_of_day']}") + # 获取场景中心 spawn_points = self.world.get_map().get_spawn_points() if spawn_points: self.scene_center = spawn_points[len(spawn_points) // 2].location else: self.scene_center = carla.Location(0, 0, 0) + # 初始化交通管理器 self.traffic_manager = TrafficManager(self.world, self.config) + # 生成主车 num_ego_vehicles = min(self.config['cooperative'].get('num_coop_vehicles', 2) + 1, 3) for i in range(num_ego_vehicles): ego_vehicle = self.traffic_manager.spawn_ego_vehicle() if ego_vehicle: self.ego_vehicles.append(ego_vehicle) Log.info(f"主车 {i + 1} 生成: {ego_vehicle.type_id}") + else: + Log.warning(f"主车 {i + 1} 生成失败") if not self.ego_vehicles: - Log.error("主车生成失败") + Log.error("没有主车生成成功") return False - self.traffic_manager.spawn_traffic(self.scene_center) + # 生成交通 + traffic_count = self.traffic_manager.spawn_traffic(self.scene_center) + Log.info(f"生成交通: {traffic_count} 个对象") + # 添加行人安全设施 + if self.pedestrian_safety_mode: + safety_features = self.traffic_manager.add_pedestrian_safety_features(self.scene_center) + Log.info(f"添加行人安全设施: {len(safety_features)} 个") + + # 初始化V2X通信 if self.config['v2x']['enabled']: self.v2x_communication = V2XCommunication(self.config['v2x']) @@ -1071,9 +1975,10 @@ def setup_scene(self): self.v2x_communication.register_node( f'vehicle_{vehicle.id}', (location.x, location.y, location.z), - {'type': 'vehicle', 'capabilities': ['bsm', 'rsm']} + {'type': 'vehicle', 'capabilities': ['bsm', 'rsm', 'emergency_brake']} ) + # 初始化多车辆管理器 self.multi_vehicle_manager = MultiVehicleManager( self.world, self.config, @@ -1082,9 +1987,12 @@ def setup_scene(self): self.multi_vehicle_manager.ego_vehicles = self.ego_vehicles + # 生成协同车辆 num_coop_vehicles = self.config['cooperative'].get('num_coop_vehicles', 2) coop_vehicles = self.multi_vehicle_manager.spawn_cooperative_vehicles(num_coop_vehicles) + Log.info(f"生成协同车辆: {len(coop_vehicles)} 辆") + # 注册V2X节点 if self.v2x_communication: for vehicle in coop_vehicles: location = vehicle.get_location() @@ -1094,21 +2002,32 @@ def setup_scene(self): {'type': 'vehicle', 'capabilities': ['bsm', 'rsm']} ) + # 初始化行人安全监控器 self.safety_monitor = PedestrianSafetyMonitor(self.world, self.output_dir) - time.sleep(3.0) + # 初始化场景管理器 + self.scene_manager = SceneManager() + + # 等待场景稳定 + Log.info("等待场景稳定...") + time.sleep(5.0) + return True def setup_sensors(self): + """设置传感器(增强版)""" for i, vehicle in enumerate(self.ego_vehicles): sensor_manager = SensorManager(self.world, self.config, self.output_dir) + # 设置摄像头 cameras = sensor_manager.setup_cameras(vehicle, self.scene_center, i + 1) if cameras == 0: Log.error(f"车辆 {i + 1} 没有摄像头安装成功") return False + # 设置LiDAR lidars = sensor_manager.setup_lidar(vehicle, i + 1) + Log.info(f"车辆 {i + 1} 传感器: {cameras}摄像头 + {lidars}LiDAR") self.sensor_managers[vehicle.id] = sensor_manager @@ -1116,12 +2035,14 @@ def setup_sensors(self): return True def collect_data(self): + """收集数据(增强版)""" duration = self.config['scenario']['duration'] Log.info(f"开始数据收集,时长: {duration}秒") self.start_time = time.time() self.is_running = True + # 计时器 last_update = time.time() last_v2x_update = time.time() last_perception_share = time.time() @@ -1129,10 +2050,13 @@ def collect_data(self): last_detailed_log = time.time() last_memory_check = time.time() last_safety_check = time.time() + last_weather_change = time.time() + # 状态标志 memory_warning_issued = False early_stop_triggered = False + # 内存阈值 memory_warning_threshold = 350 memory_critical_threshold = 400 early_stop_threshold = 450 @@ -1142,25 +2066,34 @@ def collect_data(self): current_time = time.time() elapsed = current_time - self.start_time - if current_time - last_memory_check >= 2.0: + # 检查紧急停止 + if self.emergency_handler.check_emergency(): + Log.critical("检测到紧急情况,执行紧急停止...") + self.emergency_handler.emergency_cleanup() + early_stop_triggered = True + break + + # 内存监控 + if current_time - last_memory_check >= 3.0: try: import psutil process = psutil.Process() memory_mb = process.memory_info().rss / (1024 * 1024) if memory_mb > early_stop_threshold: - Log.error( - f"内存使用超过临界值({early_stop_threshold}MB): {memory_mb:.1f}MB,提前结束数据收集") + Log.critical(f"内存使用超过临界值({early_stop_threshold}MB): {memory_mb:.1f}MB") early_stop_triggered = True break elif memory_mb > memory_critical_threshold: - Log.warning(f"内存使用严重过高: {memory_mb:.1f}MB,进行强制清理") - self._force_memory_cleanup() - memory_warning_issued = True - elif memory_mb > memory_warning_threshold and not memory_warning_issued: - Log.warning(f"内存使用较高: {memory_mb:.1f}MB,减少数据处理") - memory_warning_issued = True - elif memory_mb < memory_warning_threshold: + if not memory_warning_issued: + Log.warning(f"内存使用严重过高: {memory_mb:.1f}MB") + self._force_memory_cleanup() + memory_warning_issued = True + elif memory_mb > memory_warning_threshold: + if not memory_warning_issued: + Log.warning(f"内存使用较高: {memory_mb:.1f}MB") + memory_warning_issued = True + elif memory_mb < memory_warning_threshold * 0.8: memory_warning_issued = False except Exception as e: @@ -1168,14 +2101,17 @@ def collect_data(self): last_memory_check = current_time + # 更新车辆状态 if self.multi_vehicle_manager: self.multi_vehicle_manager.update_vehicle_states() - v2x_interval = self.config['v2x'].get('update_interval', 2.0) + # V2X通信更新 + v2x_interval = self.config['v2x'].get('update_interval', 1.0) if self.v2x_communication and current_time - last_v2x_update >= v2x_interval: self._update_v2x_communication() last_v2x_update = current_time + # 协同感知共享 if (not memory_warning_issued and self.config['cooperative'].get('enable_shared_perception', True) and current_time - last_perception_share >= 2.0): @@ -1185,47 +2121,69 @@ def collect_data(self): # 行人安全检查 if current_time - last_safety_check >= 1.0 and self.safety_monitor: safety_report = self.safety_monitor.check_pedestrian_safety() + + # 高风险情况处理 if safety_report['risk_distribution']['high'] > 0: Log.safety(f"行人安全警告: {safety_report['risk_distribution']['high']}个高风险情况") - # 广播行人警告 self._broadcast_pedestrian_warnings(safety_report) + + # 临界情况紧急处理 + if safety_report['risk_distribution']['critical'] > 0: + Log.safety(f"行人安全紧急警告: {safety_report['risk_distribution']['critical']}个临界情况") + self._handle_critical_safety_situation(safety_report) + last_safety_check = current_time + # 动态天气变化 + if current_time - last_weather_change >= 30.0: + self._update_dynamic_weather() + last_weather_change = current_time + + # 性能监控 if current_time - last_performance_sample >= 10.0: memory_info = self.performance_monitor.sample_memory() cpu_info = self.performance_monitor.sample_cpu() if current_time - last_detailed_log >= 60.0: - Log.performance(f"详细性能监控:") + Log.performance("详细性能监控:") Log.performance(f" 进程内存: {memory_info['process_mb']:.1f}MB") Log.performance(f" 系统内存: {memory_info['system_used_percent']:.1f}%使用率") Log.performance(f" CPU: {cpu_info['total_percent']:.1f}% ({cpu_info['count']}核心)") + Log.performance(f" 总帧数: {self.collected_frames}") last_detailed_log = current_time else: - Log.performance(f"系统监控 - 内存: {memory_info['process_mb']:.1f}MB, " + Log.performance(f"系统监控 - " + f"内存: {memory_info['process_mb']:.1f}MB, " f"CPU: {cpu_info['total_percent']:.1f}%, " - f"活跃线程: {threading.active_count()}") + f"活跃线程: {threading.active_count()}, " + f"帧数: {self.collected_frames}") last_performance_sample = current_time + # 垃圾回收 gc.collect() - if current_time - last_update >= 5.0: + # 进度更新 + if current_time - last_update >= 10.0: total_frames = sum(mgr.get_frame_count() for mgr in self.sensor_managers.values()) + self.collected_frames = total_frames progress = (elapsed / duration) * 100 if total_frames > 0: frames_per_second = total_frames / elapsed - remaining_frames = (duration - elapsed) * frames_per_second - eta_seconds = (duration - elapsed) + remaining_time = (duration - elapsed) + eta_minutes = remaining_time / 60 else: - eta_seconds = duration - elapsed + remaining_time = duration - elapsed + eta_minutes = remaining_time / 60 Log.info(f"进度: {elapsed:.0f}/{duration}秒 ({progress:.1f}%) | " f"总帧数: {total_frames} | " - f"ETA: {eta_seconds:.0f}秒") + f"帧率: {frames_per_second:.1f} FPS | " + f"剩余: {eta_minutes:.1f}分钟") last_update = current_time + # 短暂休眠以减少CPU使用 time.sleep(0.01) except KeyboardInterrupt: @@ -1237,10 +2195,10 @@ def collect_data(self): self.is_running = False elapsed = time.time() - self.start_time + # 更新收集的帧数 self.collected_frames = sum(mgr.get_frame_count() for mgr in self.sensor_managers.values()) if self.collected_frames > 0: - total_frames_from_sensors = self.collected_frames performance_summary = self.performance_monitor.get_performance_summary() if early_stop_triggered: @@ -1248,67 +2206,163 @@ def collect_data(self): else: Log.info(f"收集完成: {self.collected_frames}帧, 用时: {elapsed:.1f}秒") - fps = total_frames_from_sensors / max(elapsed, 0.1) + fps = self.collected_frames / max(elapsed, 0.1) Log.info(f"平均帧率: {fps:.2f} FPS") - Log.info(f"最大内存使用: {performance_summary['max_memory_mb']:.1f} MB") - Log.info(f"平均CPU使用: {performance_summary['average_cpu_percent']:.1f}%") + Log.info(f"最大内存使用: {performance_summary['memory_statistics']['max_memory_mb']:.1f} MB") + Log.info(f"平均CPU使用: {performance_summary['cpu_statistics']['average_cpu_percent']:.1f}%") # 生成行人安全报告 if self.safety_monitor: final_report = self.safety_monitor.generate_final_report() - Log.safety( - f"行人安全报告: {final_report['risk_distribution']['high']}高风险, {final_report['risk_distribution']['medium']}中风险") - Log.safety(f"行人安全评分: {final_report['safety_score']:.1f}/100") + Log.safety(f"行人安全最终报告:") + Log.safety(f" 高风险: {final_report['risk_distribution']['high']}次") + Log.safety(f" 临界风险: {final_report['risk_distribution']['critical']}次") + Log.safety(f" 安全评分: {final_report['safety_score']:.1f}/100") else: Log.warning("未收集到任何数据帧") + # 保存元数据和生成摘要 self._save_metadata() self._print_summary() + # 数据格式转换 if self.output_format != 'standard': self._convert_to_target_format() def _broadcast_pedestrian_warnings(self, safety_report): - """广播行人警告""" + """广播行人警告(增强版)""" if not self.multi_vehicle_manager: return - # 检查高风险交互 - if safety_report.get('risk_distribution', {}).get('high', 0) > 0: - for vehicle in self.ego_vehicles + self.multi_vehicle_manager.cooperative_vehicles: + high_risk_count = safety_report.get('risk_distribution', {}).get('high', 0) + critical_count = safety_report.get('risk_distribution', {}).get('critical', 0) + + if high_risk_count > 0 or critical_count > 0: + # 获取所有车辆 + all_vehicles = self.ego_vehicles + self.multi_vehicle_manager.cooperative_vehicles + + for vehicle in all_vehicles: if not hasattr(vehicle, 'is_alive') or not vehicle.is_alive: continue try: location = vehicle.get_location() velocity = vehicle.get_velocity() - speed = math.sqrt(velocity.x ** 2 + velocity.y ** 2 + velocity.z ** 2) + speed = math.sqrt(velocity.x ** 2 + velocity.y ** 2 + velocity.z ** 2) * 3.6 # km/h - # 模拟行人位置(实际应用中应从感知系统获取) - pedestrian_location = ( - location.x + random.uniform(-5, 5), - location.y + random.uniform(-5, 5), - location.z - ) + # 查找附近的潜在行人(模拟) + nearby_pedestrians = [] - distance = math.sqrt( - (location.x - pedestrian_location[0]) ** 2 + - (location.y - pedestrian_location[1]) ** 2 - ) + # 模拟检测:随机生成行人位置 + for _ in range(random.randint(0, 3)): + angle = random.uniform(0, 2 * math.pi) + distance = random.uniform(5.0, 25.0) - if distance < 20.0: # 只广播近距离行人 - self.multi_vehicle_manager.share_pedestrian_warning( - vehicle.id, - pedestrian_location, - distance, - speed + pedestrian_location = ( + location.x + distance * math.cos(angle), + location.y + distance * math.sin(angle), + location.z ) + + # 计算距离和相对速度 + actual_distance = math.sqrt( + (location.x - pedestrian_location[0]) ** 2 + + (location.y - pedestrian_location[1]) ** 2 + ) + + if actual_distance < 30.0: # 只广播近距离行人 + # 评估风险 + if actual_distance < 5.0: + risk_level = 'critical' + elif actual_distance < 10.0: + risk_level = 'high' + elif actual_distance < 20.0: + risk_level = 'medium' + else: + risk_level = 'low' + + nearby_pedestrians.append({ + 'location': pedestrian_location, + 'distance': actual_distance, + 'risk_level': risk_level + }) + + # 广播警告 + for pedestrian in nearby_pedestrians: + if pedestrian['risk_level'] in ['high', 'critical']: + self.multi_vehicle_manager.share_pedestrian_warning( + vehicle.id, + pedestrian['location'], + pedestrian['distance'], + speed, + pedestrian_id=random.randint(1000, 9999) + ) + + except Exception as e: + Log.debug(f"广播行人警告失败: {e}") + + def _handle_critical_safety_situation(self, safety_report): + """处理临界安全情况""" + critical_count = safety_report.get('risk_distribution', {}).get('critical', 0) + + if critical_count > 0: + Log.safety("⚠ 检测到临界安全情况,执行紧急措施") + + # 1. 向所有车辆发送紧急制动警告 + if self.v2x_communication: + for vehicle in self.ego_vehicles: + try: + location = vehicle.get_location() + warning_data = { + 'type': 'emergency_brake', + 'location': (location.x, location.y, location.z), + 'severity': 'critical', + 'reason': 'pedestrian_critical_risk', + 'timestamp': time.time() + } + + self.v2x_communication.broadcast_roadside_safety_message( + f'emergency_system', + warning_data + ) + except: + pass + + # 2. 降低车辆速度 + for vehicle in self.ego_vehicles + self.multi_vehicle_manager.cooperative_vehicles: + try: + if hasattr(vehicle, 'is_alive') and vehicle.is_alive: + control = carla.VehicleControl() + control.throttle = 0.0 + control.brake = 0.5 # 轻度制动 + vehicle.apply_control(control) except: pass + # 3. 记录紧急事件 + emergency_event = { + 'timestamp': datetime.now().isoformat(), + 'type': 'pedestrian_critical_risk', + 'critical_count': critical_count, + 'safety_report': safety_report, + 'actions_taken': ['v2x_warning', 'speed_reduction'] + } + + emergency_dir = os.path.join(self.output_dir, "emergency_events") + os.makedirs(emergency_dir, exist_ok=True) + + event_file = os.path.join(emergency_dir, f"emergency_{int(time.time())}.json") + try: + with open(event_file, 'w', encoding='utf-8') as f: + json.dump(emergency_event, f, indent=2) + except: + pass + def _force_memory_cleanup(self): + """强制内存清理""" Log.info("执行强制内存清理...") + # 清理图像缓存 for sensor_manager in self.sensor_managers.values(): if hasattr(sensor_manager, 'image_processor'): if hasattr(sensor_manager.image_processor, 'image_cache'): @@ -1319,6 +2373,7 @@ def _force_memory_cleanup(self): except: pass + # 刷新LiDAR批处理 for sensor_manager in self.sensor_managers.values(): if hasattr(sensor_manager, 'lidar_processor'): try: @@ -1327,212 +2382,101 @@ def _force_memory_cleanup(self): except: pass - gc.collect() - - Log.info("强制内存清理完成") - - def cleanup(self): - Log.info("开始安全清理场景...") - - cleanup_start = time.time() - - try: - self.is_running = False - - time.sleep(0.2) - - if self.v2x_communication: - try: - Log.info("停止V2X通信...") - self.v2x_communication.stop() - except: - pass - - if self.safety_monitor: - try: - Log.info("保存行人安全数据...") - self.safety_monitor.save_data() - except: - pass - - Log.info(f"清理 {len(self.sensor_managers)} 个传感器管理器...") - for vehicle_id, sensor_manager in self.sensor_managers.items(): - try: - sensor_manager.cleanup() - except: - pass - - self.sensor_managers.clear() - time.sleep(0.1) - - if self.multi_vehicle_manager: - try: - Log.info("清理多车辆管理器...") - self.multi_vehicle_manager.cleanup() - except: - pass - - if self.traffic_manager: - try: - Log.info("清理交通管理器...") - self.traffic_manager.cleanup() - except: - pass - - Log.info(f"清理 {len(self.ego_vehicles)} 个主车...") - for vehicle in self.ego_vehicles: + # 清理融合缓存 + for sensor_manager in self.sensor_managers.values(): + if hasattr(sensor_manager, 'fusion_manager'): try: - if hasattr(vehicle, 'destroy'): - vehicle.destroy() + sensor_manager.fusion_manager.cleanup() + Log.debug("清理融合缓存") except: pass - time.sleep(0.01) - - self.ego_vehicles.clear() - - try: - if self.world: - default_weather = carla.WeatherParameters() - self.world.set_weather(default_weather) - except: - pass - - gc.collect() - - time.sleep(0.3) - - self.traffic_manager = None - self.multi_vehicle_manager = None - self.v2x_communication = None - self.safety_monitor = None - self.scene_center = None - - gc.collect() - - Log.info(f"清理完成,总用时: {time.time() - cleanup_start:.2f}秒") - - except Exception as e: - Log.error(f"清理过程中发生错误: {e}") - try: - self.sensor_managers.clear() - self.ego_vehicles.clear() - self.traffic_manager = None - self.multi_vehicle_manager = None - self.v2x_communication = None - self.safety_monitor = None - gc.collect() - except: - pass - def _convert_to_target_format(self): - Log.info(f"转换为 {self.output_format} 格式...") - - if self.output_format == 'v2xformer': - self._convert_to_v2xformer_format() - elif self.output_format == 'kitti': - self._convert_to_kitti_format() - - def _convert_to_v2xformer_format(self): - try: - v2x_dir = os.path.join(self.output_dir, "v2xformer_format") - - splits = ['train', 'val', 'test'] - for split in splits: - split_dir = os.path.join(v2x_dir, split) - os.makedirs(split_dir, exist_ok=True) - - for subdir in ['image', 'point_cloud', 'calib', 'label']: - os.makedirs(os.path.join(split_dir, subdir), exist_ok=True) - - total_frames = self.collected_frames - train_ratio = 0.7 - val_ratio = 0.2 - test_ratio = 0.1 - - train_frames = int(total_frames * train_ratio) - val_frames = int(total_frames * val_ratio) - test_frames = total_frames - train_frames - val_frames - - splits_info = { - 'train': list(range(0, train_frames)), - 'val': list(range(train_frames, train_frames + val_frames)), - 'test': list(range(train_frames + val_frames, total_frames)) - } - - splits_file = os.path.join(v2x_dir, "splits.json") - with open(splits_file, 'w') as f: - json.dump(splits_info, f, indent=2) - - Log.info(f"V2XFormer格式转换完成: {v2x_dir}") - - except Exception as e: - Log.error(f"V2XFormer格式转换失败: {e}") - - def _convert_to_kitti_format(self): - try: - kitti_dir = os.path.join(self.output_dir, "kitti_format") - - for subdir in ['training', 'testing']: - full_dir = os.path.join(kitti_dir, subdir) - os.makedirs(full_dir, exist_ok=True) - - for subsubdir in ['image_2', 'velodyne', 'calib', 'label_2']: - os.makedirs(os.path.join(full_dir, subsubdir), exist_ok=True) - - self._generate_kitti_calibration(kitti_dir) - - Log.info(f"KITTI格式转换完成: {kitti_dir}") - - except Exception as e: - Log.error(f"KITTI格式转换失败: {e}") - - def _generate_kitti_calibration(self, kitti_dir): - calib_template = """P0: 7.215377e+02 0.000000e+00 6.095593e+02 0.000000e+00 0.000000e+00 7.215377e+02 1.728540e+02 0.000000e+00 0.000000e+00 0.000000e+00 1.000000e+00 0.000000e+00 -P1: 7.215377e+02 0.000000e+00 6.095593e+02 -3.875744e+02 0.000000e+00 7.215377e+02 1.728540e+02 0.000000e+00 0.000000e+00 0.000000e+00 1.000000e+00 0.000000e+00 -P2: 7.215377e+02 0.000000e+00 6.095593e+02 4.485728e+01 0.000000e+00 7.215377e+02 1.728540e+02 2.163791e-01 0.000000e+00 0.000000e+00 1.000000e+00 2.745884e-03 -P3: 7.215377e+02 0.000000e+00 6.095593e+02 -3.341729e+02 0.000000e+00 7.215377e+02 1.728540e+02 2.163791e-01 0.000000e+00 0.000000e+00 1.000000e+00 2.745884e-03 -R0_rect: 9.999239e-01 9.837760e-03 -7.445048e-03 -9.869795e-03 9.999421e-01 -4.278459e-03 7.402527e-03 4.351614e-03 9.999631e-01 -Tr_velo_to_cam: 4.276802e-04 -9.999672e-01 -8.084491e-03 -1.198459e-02 -7.210626e-03 8.081198e-03 -9.999413e-01 -5.403984e-02 9.999738e-01 4.859485e-04 -7.206933e-03 -2.921968e-02 -Tr_imu_to_velo: 9.999976e-01 7.553071e-04 -2.035826e-03 -8.086759e-01 -7.854027e-04 9.998898e-01 -1.482298e-02 3.195559e-01 2.024406e-03 1.482454e-02 9.998881e-01 -7.997231e-01""" + # 垃圾回收 + gc.collect() - for i in range(self.collected_frames): - calib_file = os.path.join(kitti_dir, "training", "calib", f"{i:06d}.txt") - with open(calib_file, 'w') as f: - f.write(calib_template) + Log.info("强制内存清理完成") def _update_v2x_communication(self): + """更新V2X通信""" if not self.v2x_communication: return - for vehicle in self.ego_vehicles + self.multi_vehicle_manager.cooperative_vehicles: + # 更新车辆状态 + all_vehicles = self.ego_vehicles + self.multi_vehicle_manager.cooperative_vehicles + + for vehicle in all_vehicles: if not hasattr(vehicle, 'is_alive') or not vehicle.is_alive: continue try: location = vehicle.get_location() velocity = vehicle.get_velocity() - speed = math.sqrt(velocity.x ** 2 + velocity.y ** 2 + velocity.z ** 2) + transform = vehicle.get_transform() + + speed = math.sqrt(velocity.x ** 2 + velocity.y ** 2 + velocity.z ** 2) * 3.6 # km/h vehicle_data = { 'position': (location.x, location.y, location.z), 'speed': speed, - 'heading': vehicle.get_transform().rotation.yaw, - 'acceleration': (0, 0, 0) + 'heading': transform.rotation.yaw, + 'acceleration': (0, 0, 0), + 'vehicle_id': vehicle.id, + 'timestamp': time.time() } self.v2x_communication.broadcast_basic_safety_message( f'vehicle_{vehicle.id}', vehicle_data ) - except: - pass + except Exception as e: + Log.debug(f"V2X状态更新失败: {e}") + + # 检查并处理接收到的消息 for vehicle in self.ego_vehicles: messages = self.v2x_communication.get_messages_for_node(f'vehicle_{vehicle.id}') - if messages: + if messages and len(messages) > 0: Log.debug(f"车辆 {vehicle.id} 收到 {len(messages)} 条V2X消息") + # 处理安全警告 + safety_messages = [m for m in messages if + m['message']['message_type'] in ['warning', 'emergency_brake']] + if safety_messages: + self._process_v2x_safety_warnings(vehicle, safety_messages) + + def _process_v2x_safety_warnings(self, vehicle, warnings): + """处理V2X安全警告""" + for warning in warnings: + message = warning['message'] + data = message['data'] + + if data.get('type') == 'pedestrian': + Log.safety(f"车辆 {vehicle.id} 收到行人警告: {data.get('severity', 'unknown')}风险") + + # 根据风险等级采取行动 + if data.get('severity') in ['high', 'critical']: + try: + control = carla.VehicleControl() + control.throttle = 0.0 + control.brake = 0.3 + vehicle.apply_control(control) + Log.safety(f"车辆 {vehicle.id} 执行减速") + except: + pass + + elif data.get('type') == 'emergency_brake': + Log.safety(f"车辆 {vehicle.id} 收到紧急制动警告") + + try: + control = carla.VehicleControl() + control.throttle = 0.0 + control.brake = 1.0 # 紧急制动 + vehicle.apply_control(control) + except: + pass + def _share_perception_data(self): + """共享感知数据""" if not self.multi_vehicle_manager or not self.config['cooperative']['enable_shared_perception']: return @@ -1540,38 +2484,95 @@ def _share_perception_data(self): if not hasattr(vehicle, 'is_alive') or not vehicle.is_alive: continue + # 模拟物体检测 detected_objects = self._simulate_object_detection(vehicle) if detected_objects: self.multi_vehicle_manager.share_perception_data(vehicle.id, detected_objects) + # 保存共享感知数据 + frame_num = sum(mgr.get_frame_count() for mgr in self.sensor_managers.values()) + if frame_num % 10 == 0: # 每10帧保存一次 + self.multi_vehicle_manager.save_shared_perception(frame_num) + def _simulate_object_detection(self, vehicle): + """模拟物体检测""" detected_objects = [] - for other_vehicle in self.ego_vehicles + self.multi_vehicle_manager.cooperative_vehicles: + # 检测其他车辆 + all_vehicles = self.ego_vehicles + self.multi_vehicle_manager.cooperative_vehicles + + for other_vehicle in all_vehicles: if other_vehicle.id == vehicle.id or not hasattr(other_vehicle, 'is_alive') or not other_vehicle.is_alive: continue try: location = other_vehicle.get_location() + velocity = other_vehicle.get_velocity() distance = vehicle.get_location().distance(location) - if distance < 50.0: + if distance < 50.0: # 检测范围50米 + speed = math.sqrt(velocity.x ** 2 + velocity.y ** 2 + velocity.z ** 2) * 3.6 + obj_data = { 'class': 'vehicle', + 'type': other_vehicle.type_id, 'position': {'x': location.x, 'y': location.y, 'z': location.z}, - 'velocity': {'x': 0, 'y': 0, 'z': 0}, + 'velocity': {'x': velocity.x, 'y': velocity.y, 'z': velocity.z, 'speed_kmh': speed}, + 'distance': distance, 'confidence': max(0.7, 1.0 - distance / 50.0), - 'size': {'width': 2.0, 'length': 4.5, 'height': 1.5}, - 'id': other_vehicle.id + 'size': {'width': 1.8, 'length': 4.5, 'height': 1.5}, + 'id': other_vehicle.id, + 'timestamp': time.time() } detected_objects.append(obj_data) except: pass + # 检测行人 + if self.traffic_manager and self.traffic_manager.pedestrians: + for pedestrian in self.traffic_manager.pedestrians[:10]: # 只检查前10个行人 + if not hasattr(pedestrian, 'is_alive') or not pedestrian.is_alive: + continue + + try: + location = pedestrian.get_location() + distance = vehicle.get_location().distance(location) + + if distance < 30.0: # 行人检测范围30米 + obj_data = { + 'class': 'pedestrian', + 'type': pedestrian.type_id, + 'position': {'x': location.x, 'y': location.y, 'z': location.z}, + 'distance': distance, + 'confidence': max(0.6, 1.0 - distance / 30.0), + 'size': {'width': 0.5, 'length': 0.5, 'height': 1.7}, + 'id': pedestrian.id, + 'timestamp': time.time(), + 'risk_level': 'high' if distance < 10.0 else 'medium' if distance < 20.0 else 'low' + } + detected_objects.append(obj_data) + except: + pass + return detected_objects + def _update_dynamic_weather(self): + """更新动态天气""" + try: + current_weather = self.world.get_weather() + dynamic_weather = WeatherSystem.create_dynamic_weather(current_weather, variation=0.15) + self.world.set_weather(dynamic_weather) + + Log.debug(f"天气更新: " + f"云量 {dynamic_weather.cloudiness:.0f}%, " + f"降水 {dynamic_weather.precipitation:.0f}%, " + f"雾密度 {dynamic_weather.fog_density:.0f}%") + except: + pass + def _save_metadata(self): + """保存元数据""" total_frames = self.collected_frames metadata = { @@ -1583,31 +2584,52 @@ def _save_metadata(self): 'output_format': self.output_format, 'output': self.config['output'], 'collection': { + 'start_time': self.start_time, 'duration': round(time.time() - self.start_time, 2), 'total_frames': total_frames, 'frame_rate': round(total_frames / max(time.time() - self.start_time, 0.1), - 2) if total_frames > 0 else 0 + 2) if total_frames > 0 else 0, + 'completed': True, + 'early_stop': self.emergency_handler.emergency_stop } } if total_frames > 0: + # 性能摘要 performance_summary = self.performance_monitor.get_performance_summary() metadata['performance'] = performance_summary + # 传感器摘要 sensor_summaries = {} for vehicle_id, sensor_manager in self.sensor_managers.items(): sensor_summaries[vehicle_id] = sensor_manager.generate_sensor_summary() metadata['sensor_summaries'] = sensor_summaries + # V2X状态 if self.v2x_communication: metadata['v2x_status'] = self.v2x_communication.get_network_status() + # 协同摘要 if self.multi_vehicle_manager: metadata['cooperative_summary'] = self.multi_vehicle_manager.generate_summary() + # 安全报告 if self.safety_monitor: metadata['safety_report'] = self.safety_monitor.generate_final_report() + # 场景描述 + if self.scene_manager: + scene_info = { + 'center_location': { + 'x': self.scene_center.x, + 'y': self.scene_center.y, + 'z': self.scene_center.z + } if self.scene_center else None, + 'town': self.config['scenario']['town'], + 'pedestrian_safety_mode': self.pedestrian_safety_mode + } + metadata['scene_info'] = scene_info + meta_path = os.path.join(self.output_dir, "metadata", "collection_info.json") with open(meta_path, 'w', encoding='utf-8') as f: json.dump(metadata, f, indent=2, ensure_ascii=False) @@ -1615,42 +2637,75 @@ def _save_metadata(self): Log.info(f"元数据保存: {meta_path}") def _print_summary(self): + """打印摘要""" print("\n" + "=" * 60) print("数据收集摘要") print("=" * 60) - raw_dirs = [d for d in os.listdir(self.output_dir) if d.startswith('raw')] + # 统计文件 + raw_dirs = [d for d in os.listdir(os.path.join(self.output_dir, "raw")) + if os.path.isdir(os.path.join(self.output_dir, "raw", d))] + total_raw_images = 0 for raw_dir in raw_dirs: - raw_path = os.path.join(self.output_dir, raw_dir) - if os.path.exists(raw_path): - for root, dirs, files in os.walk(raw_path): - total_raw_images += len([f for f in files if f.endswith(('.png', '.jpg', '.jpeg'))]) + raw_path = os.path.join(self.output_dir, "raw", raw_dir) + for root, dirs, files in os.walk(raw_path): + total_raw_images += len([f for f in files if f.endswith(('.png', '.jpg', '.jpeg'))]) print(f"原始图像: {total_raw_images} 张") + # LiDAR数据 lidar_dir = os.path.join(self.output_dir, "lidar") if os.path.exists(lidar_dir): import glob bin_files = glob.glob(os.path.join(lidar_dir, "*.bin")) npy_files = glob.glob(os.path.join(lidar_dir, "*.npy")) - batch_files = glob.glob(os.path.join(lidar_dir, "*batch*.json")) - print(f"LiDAR数据: {len(bin_files)} .bin文件, {len(npy_files)} .npy文件") - print(f"批处理文件: {len(batch_files)} 个") + pcd_files = glob.glob(os.path.join(lidar_dir, "*.pcd")) + print(f"LiDAR数据: {len(bin_files)} .bin文件, " + f"{len(npy_files)} .npy文件, " + f"{len(pcd_files)} .pcd文件") + + # 计算总点数估计 + if bin_files: + sample_files = bin_files[:min(5, len(bin_files))] + total_points = 0 + for file in sample_files: + try: + file_size = os.path.getsize(file) + points_in_file = file_size // 16 # 每个点16字节 + total_points += points_in_file + except: + pass + + if sample_files: + avg_points = total_points / len(sample_files) + estimated_total = avg_points * len(bin_files) + print(f" 估计总点数: {estimated_total:,.0f}") + + # 协同数据 coop_dir = os.path.join(self.output_dir, "cooperative") if os.path.exists(coop_dir): - v2x_files = len( - [f for f in os.listdir(os.path.join(coop_dir, "v2x_messages")) if f.endswith(('.json', '.gz'))]) - perception_files = len( - [f for f in os.listdir(os.path.join(coop_dir, "shared_perception")) if f.endswith('.json')]) - print(f"协同数据: {v2x_files} V2X消息, {perception_files} 共享感知文件") + v2x_files = [] + perception_files = [] + + v2x_dir = os.path.join(coop_dir, "v2x_messages") + if os.path.exists(v2x_dir): + v2x_files = [f for f in os.listdir(v2x_dir) if f.endswith(('.json', '.gz'))] + + perception_dir = os.path.join(coop_dir, "shared_perception") + if os.path.exists(perception_dir): + perception_files = [f for f in os.listdir(perception_dir) if f.endswith('.json')] + print(f"协同数据: {len(v2x_files)} V2X消息, {len(perception_files)} 共享感知文件") + + # 安全数据 safety_dir = os.path.join(self.output_dir, "safety_reports") if os.path.exists(safety_dir): safety_files = len([f for f in os.listdir(safety_dir) if f.endswith('.json')]) print(f"安全报告: {safety_files} 个") + # 格式转换 if self.output_format == 'v2xformer': v2x_dir = os.path.join(self.output_dir, "v2xformer_format") if os.path.exists(v2x_dir): @@ -1661,6 +2716,7 @@ def _print_summary(self): if os.path.exists(kitti_dir): print(f"KITTI格式: 已生成") + # 性能统计 total_frames = self.collected_frames elapsed = time.time() - self.start_time @@ -1671,24 +2727,324 @@ def _print_summary(self): performance = self.performance_monitor.get_performance_summary() print(f" 帧时统计:") - print(f" 平均: {performance['average_frame_time']:.3f}秒") - print(f" P50: {performance['frame_time_stats']['p50']:.3f}秒") - print(f" P95: {performance['frame_time_stats']['p95']:.3f}秒") - print(f" P99: {performance['frame_time_stats']['p99']:.3f}秒") - print(f" 平均内存: {performance['average_memory_mb']:.1f} MB") - print(f" 最大内存: {performance['max_memory_mb']:.1f} MB") - print(f" 平均CPU: {performance['average_cpu_percent']:.1f}%") + print(f" 平均: {performance['frame_statistics']['average_frame_time'] * 1000:.1f}ms") + print(f" P50: {performance['frame_time_stats']['p50'] * 1000:.1f}ms") + print(f" P95: {performance['frame_time_stats']['p95'] * 1000:.1f}ms") + print(f" P99: {performance['frame_time_stats']['p99'] * 1000:.1f}ms") + print(f" 内存使用:") + print(f" 平均: {performance['memory_statistics']['average_memory_mb']:.1f} MB") + print(f" 最大: {performance['memory_statistics']['max_memory_mb']:.1f} MB") + print(f" 警告: {len(performance['warnings'])} 个") print(f" 总帧数: {total_frames}") else: print(" 未收集到有效帧数据") + # 行人安全统计 + if self.safety_monitor: + try: + final_report = self.safety_monitor.generate_final_report() + print(f"\n行人安全统计:") + print(f" 高风险交互: {final_report['risk_distribution']['high']}") + print(f" 临界风险: {final_report['risk_distribution']['critical']}") + print(f" 安全评分: {final_report['safety_score']:.1f}/100") + except: + pass + print(f"\n输出目录: {self.output_dir}") print("=" * 60) + def _convert_to_target_format(self): + """转换为目标格式""" + Log.info(f"转换为 {self.output_format} 格式...") + + if self.output_format == 'v2xformer': + self._convert_to_v2xformer_format() + elif self.output_format == 'kitti': + self._convert_to_kitti_format() + elif self.output_format == 'nuscenes': + self._convert_to_nuscenes_format() + + def _convert_to_v2xformer_format(self): + """转换为V2XFormer格式""" + try: + v2x_dir = os.path.join(self.output_dir, "v2xformer_format") + + # 创建标准目录结构 + splits = ['train', 'val', 'test'] + for split in splits: + split_dir = os.path.join(v2x_dir, split) + os.makedirs(split_dir, exist_ok=True) + + for subdir in ['image', 'point_cloud', 'calib', 'label']: + os.makedirs(os.path.join(split_dir, subdir), exist_ok=True) + + # 计算分割 + total_frames = self.collected_frames + if total_frames == 0: + Log.warning("没有数据帧可转换") + return + + train_ratio = 0.7 + val_ratio = 0.2 + test_ratio = 0.1 + + train_frames = int(total_frames * train_ratio) + val_frames = int(total_frames * val_ratio) + test_frames = total_frames - train_frames - val_frames + + splits_info = { + 'train': list(range(0, train_frames)), + 'val': list(range(train_frames, train_frames + val_frames)), + 'test': list(range(train_frames + val_frames, total_frames)) + } + + # 保存分割信息 + splits_file = os.path.join(v2x_dir, "splits.json") + with open(splits_file, 'w', encoding='utf-8') as f: + json.dump(splits_info, f, indent=2) + + # 创建配置文件 + config = { + 'dataset': { + 'name': 'CVIPS_Pedestrian_Safety', + 'version': '2.0', + 'description': '行人安全增强数据集', + 'splits': splits_info, + 'total_frames': total_frames, + 'created': datetime.now().isoformat() + }, + 'sensors': { + 'cameras': self.config['sensors']['vehicle_cameras'], + 'lidars': self.config['sensors']['lidar_sensors'], + 'image_size': self.config['sensors']['image_size'] + }, + 'classes': { + 'vehicle': ['car', 'truck', 'bus', 'motorcycle', 'bicycle'], + 'pedestrian': ['pedestrian'], + 'traffic': ['traffic_light', 'traffic_sign'] + } + } + + config_file = os.path.join(v2x_dir, "config.json") + with open(config_file, 'w', encoding='utf-8') as f: + json.dump(config, f, indent=2) + + Log.info(f"V2XFormer格式转换完成: {v2x_dir}") + + except Exception as e: + Log.error(f"V2XFormer格式转换失败: {e}") + + def _convert_to_kitti_format(self): + """转换为KITTI格式""" + try: + kitti_dir = os.path.join(self.output_dir, "kitti_format") + + # 创建标准目录结构 + for subdir in ['training', 'testing']: + full_dir = os.path.join(kitti_dir, subdir) + os.makedirs(full_dir, exist_ok=True) + + for subsubdir in ['image_2', 'velodyne', 'calib', 'label_2']: + os.makedirs(os.path.join(full_dir, subsubdir), exist_ok=True) + + # 生成标定文件 + self._generate_kitti_calibration(kitti_dir) + + # 创建映射文件 + mapping = { + 'dataset_name': 'CVIPS_KITTI', + 'original_path': self.output_dir, + 'converted_path': kitti_dir, + 'total_frames': self.collected_frames, + 'conversion_time': datetime.now().isoformat() + } + + mapping_file = os.path.join(kitti_dir, "mapping.json") + with open(mapping_file, 'w', encoding='utf-8') as f: + json.dump(mapping, f, indent=2) + + Log.info(f"KITTI格式转换完成: {kitti_dir}") + + except Exception as e: + Log.error(f"KITTI格式转换失败: {e}") + + def _convert_to_nuscenes_format(self): + """转换为nuScenes格式""" + try: + nuscenes_dir = os.path.join(self.output_dir, "nuscenes_format") + + # 创建标准目录结构 + for subdir in ['samples', 'sweeps', 'maps']: + os.makedirs(os.path.join(nuscenes_dir, subdir), exist_ok=True) + + # 创建基本配置文件 + config = { + 'dataset': { + 'name': 'CVIPS_nuScenes', + 'version': 'v1.0', + 'description': '行人安全增强数据集 - nuScenes格式', + 'created': datetime.now().isoformat() + }, + 'sensors': { + 'cameras': 6, + 'lidars': 1, + 'radars': 0 + }, + 'classes': [ + 'car', 'truck', 'bus', 'trailer', + 'pedestrian', 'bicycle', 'motorcycle', + 'traffic_cone', 'barrier' + ] + } + + config_file = os.path.join(nuscenes_dir, "config.json") + with open(config_file, 'w', encoding='utf-8') as f: + json.dump(config, f, indent=2) + + Log.info(f"nuScenes格式转换开始: {nuscenes_dir}") + + except Exception as e: + Log.error(f"nuScenes格式转换失败: {e}") + + def _generate_kitti_calibration(self, kitti_dir): + """生成KITTI标定文件""" + calib_template = """P0: 7.215377e+02 0.000000e+00 6.095593e+02 0.000000e+00 0.000000e+00 7.215377e+02 1.728540e+02 0.000000e+00 0.000000e+00 0.000000e+00 1.000000e+00 0.000000e+00 +P1: 7.215377e+02 0.000000e+00 6.095593e+02 -3.875744e+02 0.000000e+00 7.215377e+02 1.728540e+02 0.000000e+00 0.000000e+00 0.000000e+00 1.000000e+00 0.000000e+00 +P2: 7.215377e+02 0.000000e+00 6.095593e+02 4.485728e+01 0.000000e+00 7.215377e+02 1.728540e+02 2.163791e-01 0.000000e+00 0.000000e+00 1.000000e+00 2.745884e-03 +P3: 7.215377e+02 0.000000e+00 6.095593e+02 -3.341729e+02 0.000000e+00 7.215377e+02 1.728540e+02 2.163791e-01 0.000000e+00 0.000000e+00 1.000000e+00 2.745884e-03 +R0_rect: 9.999239e-01 9.837760e-03 -7.445048e-03 -9.869795e-03 9.999421e-01 -4.278459e-03 7.402527e-03 4.351614e-03 9.999631e-01 +Tr_velo_to_cam: 4.276802e-04 -9.999672e-01 -8.084491e-03 -1.198459e-02 -7.210626e-03 8.081198e-03 -9.999413e-01 -5.403984e-02 9.999738e-01 4.859485e-04 -7.206933e-03 -2.921968e-02 +Tr_imu_to_velo: 9.999976e-01 7.553071e-04 -2.035826e-03 -8.086759e-01 -7.854027e-04 9.998898e-01 -1.482298e-02 3.195559e-01 2.024406e-03 1.482454e-02 9.998881e-01 -7.997231e-01""" + + # 为每帧生成标定文件 + for i in range(min(self.collected_frames, 1000)): # 限制数量 + calib_file = os.path.join(kitti_dir, "training", "calib", f"{i:06d}.txt") + try: + with open(calib_file, 'w') as f: + f.write(calib_template) + except: + pass + def run_validation(self): + """运行数据验证""" if self.config['output'].get('validate_data', True) and self.collected_frames > 0: Log.info("运行数据验证...") - DataValidator.validate_dataset(self.output_dir) + + detailed = self.config['output'].get('run_quality_check', True) + validation_results = DataValidator.validate_dataset(self.output_dir, detailed) + + # 根据验证结果给出建议 + overall_score = validation_results.get('overall_score', 0) + health_status = validation_results.get('health_status', 'UNKNOWN') + + Log.info(f"验证完成: 评分 {overall_score}/100, 状态 {health_status}") + + if overall_score < 60: + Log.warning("数据集质量较低,建议检查采集过程") + elif overall_score < 80: + Log.info("数据集质量良好,部分项目需要优化") + else: + Log.info("数据集质量优秀") + else: + Log.info("跳过数据验证") + + def cleanup(self): + """清理数据收集器""" + Log.info("开始安全清理场景...") + + cleanup_start = time.time() + + try: + self.is_running = False + + # 短暂等待确保所有处理完成 + time.sleep(0.5) + + # 停止V2X通信 + if self.v2x_communication: + try: + Log.info("停止V2X通信...") + self.v2x_communication.stop() + except: + pass + + # 保存行人安全数据 + if self.safety_monitor: + try: + Log.info("保存行人安全数据...") + self.safety_monitor.save_data() + except: + pass + + # 清理传感器管理器 + Log.info(f"清理 {len(self.sensor_managers)} 个传感器管理器...") + for vehicle_id, sensor_manager in self.sensor_managers.items(): + try: + sensor_manager.cleanup() + except: + pass + + self.sensor_managers.clear() + time.sleep(0.2) + + # 清理多车辆管理器 + if self.multi_vehicle_manager: + try: + Log.info("清理多车辆管理器...") + self.multi_vehicle_manager.cleanup() + except: + pass + + # 清理交通管理器 + if self.traffic_manager: + try: + Log.info("清理交通管理器...") + self.traffic_manager.cleanup() + except: + pass + + # 清理主车 + Log.info(f"清理 {len(self.ego_vehicles)} 个主车...") + for vehicle in self.ego_vehicles: + try: + if hasattr(vehicle, 'destroy'): + vehicle.destroy() + except: + pass + time.sleep(0.01) + + self.ego_vehicles.clear() + + # 恢复默认天气 + try: + if self.world: + default_weather = carla.WeatherParameters() + self.world.set_weather(default_weather) + except: + pass + + # 清理Python对象 + self.traffic_manager = None + self.multi_vehicle_manager = None + self.v2x_communication = None + self.safety_monitor = None + self.scene_manager = None + self.scene_center = None + + # 垃圾回收 + gc.collect() + + Log.info(f"清理完成,总用时: {time.time() - cleanup_start:.2f}秒") + + except Exception as e: + Log.error(f"清理过程中发生错误: {e}") + try: + # 尝试强制清理 + self.sensor_managers.clear() + self.ego_vehicles.clear() + gc.collect() + except: + pass def main():