diff --git a/src/Yolov4_Vehicle_inspection/train.py b/src/Yolov4_Vehicle_inspection/train.py index fed8859680..151f3870cb 100644 --- a/src/Yolov4_Vehicle_inspection/train.py +++ b/src/Yolov4_Vehicle_inspection/train.py @@ -422,4 +422,4 @@ def fit_one_epoch(net, yolo_loss, epoch, epoch_size, epoch_size_val, gen, genval for epoch in range(Freeze_Epoch, Unfreeze_Epoch): fit_one_epoch(net, yolo_loss, epoch, epoch_size, epoch_size_val, gen, gen_val, Unfreeze_Epoch, Cuda) # 更新学习率 - lr_scheduler.step() \ No newline at end of file + lr_scheduler.step() diff --git a/src/minicarsim/main.py b/src/minicarsim/main.py index e87a5dc4f0..c2495af86e 100644 --- a/src/minicarsim/main.py +++ b/src/minicarsim/main.py @@ -957,6 +957,7 @@ def run_simulation(self): # 新增:模拟温度数据 temperature = self.simulate_temperature_data() + # 检查并控制空调 ac_status = self.check_and_control_ac(temperature) diff --git a/src/tracking_car/config.yaml b/src/tracking_car/config.yaml index a092fc5836..42ba05942b 100644 --- a/src/tracking_car/config.yaml +++ b/src/tracking_car/config.yaml @@ -8,7 +8,7 @@ conf_thres: 0.5 iou_thres: 0.3 max_age: 5 min_hits: 3 -yolo_model: '"D:/harudt/yolov8n.pt"' +yolo_model: "yolov8n.pt" yolo_imgsz_max: 320 yolo_iou: 0.45 yolo_quantize: false @@ -46,7 +46,7 @@ save_screenshots: false use_3d_visualization: true pcd_view_size: 800 -# 检测框样式配置(匹配截图) +# 检测框样式配置 det_box_color: [255, 0, 0] # 蓝色框 det_box_thickness: 2 det_label_bg_color: [0, 0, 0] # 黑色背景 diff --git a/src/tracking_car/main.py b/src/tracking_car/main.py index 13f899b9a5..a2403060d4 100644 --- a/src/tracking_car/main.py +++ b/src/tracking_car/main.py @@ -1,6 +1,6 @@ """ -main.py - CARLA多目标跟踪系统主程序 -入口文件,协调各个模块运行 +main.py - CARLA Multi-Object Tracking System +Enhanced version: Color ID encoding + Independent statistics window """ import sys @@ -12,45 +12,46 @@ import carla import torch import queue +import psutil -# 添加当前目录到路径,确保可以导入模块 +# Add current directory to path to ensure modules can be imported sys.path.append(os.path.dirname(os.path.abspath(__file__))) -# 导入自定义模块 +# Import custom modules try: import utils import sensors import tracker from loguru import logger except ImportError as e: - print(f"❌ 导入模块失败: {e}") - print("请确保以下文件在同一目录下:") + print(f"[ERROR] Import module failed: {e}") + print("Please ensure the following files are in the same directory:") print(" - utils.py") print(" - sensors.py") print(" - tracker.py") sys.exit(1) -# ======================== 配置管理 ======================== +# ======================== Configuration Management ======================== def load_config(config_path=None): """ - 加载配置 + Load configuration Args: - config_path: 配置文件路径 + config_path: Path to configuration file Returns: - dict: 配置字典 + dict: Configuration dictionary """ - # 默认配置 + # Default configuration default_config = { - # CARLA连接 + # CARLA connection 'host': 'localhost', 'port': 2000, 'timeout': 20.0, - # 传感器 + # Sensors 'img_width': 640, 'img_height': 480, 'fov': 90, @@ -60,20 +61,20 @@ def load_config(config_path=None): 'lidar_range': 100.0, 'lidar_points_per_second': 500000, - # 检测 + # Detection 'yolo_model': 'yolov8n.pt', 'conf_thres': 0.5, 'iou_thres': 0.3, 'device': 'cuda' if torch.cuda.is_available() else 'cpu', 'yolo_imgsz_max': 320, - # 跟踪 + # Tracking 'max_age': 5, 'min_hits': 3, 'kf_dt': 0.05, 'max_speed': 50.0, - # 行为分析 + # Behavior analysis 'stop_speed_thresh': 1.0, 'stop_frames_thresh': 5, 'overtake_speed_ratio': 1.5, @@ -85,60 +86,60 @@ def load_config(config_path=None): 'predict_frames': 10, 'track_history_len': 20, - # 可视化 + # Visualization 'window_width': 1280, 'window_height': 720, 'display_fps': 30, - # 天气 + # Weather 'weather': 'clear', 'num_npcs': 20, - # 自车 + # Ego vehicle 'ego_vehicle_filter': 'vehicle.tesla.model3', 'ego_vehicle_color': '255,0,0', } - # 如果提供了配置文件,尝试加载 + # If config file is provided, try to load it if config_path and os.path.exists(config_path): loaded_config = utils.load_yaml_config(config_path) if loaded_config: - # 合并配置(加载的配置覆盖默认配置) + # Merge configurations (loaded config overrides default) for key, value in loaded_config.items(): if isinstance(value, dict) and key in default_config and isinstance(default_config[key], dict): - # 递归合并字典 + # Recursive dictionary merge default_config[key].update(value) else: default_config[key] = value - logger.info(f"✅ 已加载配置文件: {config_path}") + logger.info(f"[OK] Configuration file loaded: {config_path}") return default_config def setup_carla_client(config): """ - 设置CARLA客户端 + Setup CARLA client Args: - config: 配置字典 + config: Configuration dictionary Returns: tuple: (client, world) or (None, None) """ try: - logger.info(f"正在连接CARLA服务器 {config['host']}:{config['port']}...") + logger.info(f"Connecting to CARLA server {config['host']}:{config['port']}...") client = carla.Client(config['host'], config['port']) client.set_timeout(config['timeout']) world = client.get_world() - # 设置同步模式 + # Set synchronous mode settings = world.get_settings() settings.synchronous_mode = True settings.fixed_delta_seconds = 0.05 world.apply_settings(settings) - # 设置交通管理器 + # Setup traffic manager try: tm = client.get_trafficmanager(8000) tm.set_global_distance_to_leading_vehicle(2.0) @@ -147,23 +148,23 @@ def setup_carla_client(config): tm.set_hybrid_physics_radius(50.0) tm.global_percentage_speed_difference(0) except Exception as e: - logger.warning(f"交通管理器设置失败: {e}") + logger.warning(f"Traffic manager setup failed: {e}") - logger.info("✅ CARLA客户端连接成功") + logger.info("[OK] CARLA client connected successfully") return client, world except Exception as e: - logger.error(f"❌ 连接CARLA服务器失败: {e}") + logger.error(f"[ERROR] Failed to connect to CARLA server: {e}") return None, None def set_weather(world, weather_name): """ - 设置天气 + Set weather Args: - world: CARLA世界对象 - weather_name: 天气名称 + world: CARLA world object + weather_name: Weather name """ weather_presets = { 'clear': carla.WeatherParameters.ClearNoon, @@ -177,74 +178,104 @@ def set_weather(world, weather_name): if weather_name in weather_presets: world.set_weather(weather_presets[weather_name]) - logger.info(f"🌤️ 天气已设置为: {weather_name}") + logger.info(f"[WEATHER] Weather set to: {weather_name}") else: - logger.warning(f"未知天气: {weather_name}, 使用晴天") + logger.warning(f"Unknown weather: {weather_name}, using clear weather") -# ======================== 可视化(英文版) ======================== +# ======================== Visualization (Enhanced: Independent stats window) ======================== class Visualizer: - """可视化管理器(英文版,解决乱码问题)""" + """Visualization manager (Enhanced: Color ID encoding + Independent stats window)""" def __init__(self, config): self.config = config self.window_name = "CARLA Object Tracking" + self.stats_window_name = "Statistics Panel" - # 创建窗口 + # Create main window cv2.namedWindow(self.window_name, cv2.WINDOW_NORMAL) cv2.resizeWindow(self.window_name, config.get('window_width', 1280), config.get('window_height', 720)) - # 车辆类别颜色映射 + # Create independent statistics window + cv2.namedWindow(self.stats_window_name, cv2.WINDOW_NORMAL) + # Set statistics window size + stats_width = 600 + stats_height = 800 + cv2.resizeWindow(self.stats_window_name, stats_width, stats_height) + + # Move statistics window position (avoid overlapping main window) + cv2.moveWindow(self.stats_window_name, + config.get('window_width', 1280) + 50, # Place to the right of main window + 100) # Vertical position + + # Statistics panel state + self.show_stats_window = True # Whether to show independent stats window + self.stats_image = None # Statistics panel image + self.stats_update_interval = 2 # Statistics update interval (frames) + self.stats_frame_counter = 0 # Frame counter + + # Vehicle class color mapping self.class_colors = { - 'car': (255, 0, 0), # 蓝色 - 小汽车 - 'bus': (0, 255, 0), # 绿色 - 公交车 - 'truck': (0, 0, 255), # 红色 - 卡车 - 'default': (255, 255, 0) # 青色 - 默认 + 'car': (255, 0, 0), # Blue - Car + 'bus': (0, 255, 0), # Green - Bus + 'truck': (0, 0, 255), # Red - Truck + 'default': (255, 255, 0) # Cyan - Default } - # 行为状态颜色映射(优先级从高到低) + # Behavior state color mapping (priority from high to low) self.behavior_colors = { - 'dangerous': (0, 0, 255), # 红色 - 危险 - 'stopped': (0, 255, 255), # 黄色 - 停车 - 'overtaking': (255, 0, 255), # 紫色 - 超车 - 'lane_changing': (0, 255, 255), # 青色 - 变道 - 'turning': (0, 255, 255), # 青色 - 转弯 - 'accelerating': (255, 0, 0), # 蓝色 - 加速 - 'braking': (0, 165, 255), # 橙色 - 刹车 - 'normal': (0, 255, 0) # 绿色 - 正常行驶 + 'dangerous': (0, 0, 255), # Red - Dangerous (too close) + 'stopped': (0, 255, 255), # Yellow - Stopped + 'overtaking': (255, 0, 255), # Purple - Overtaking + 'lane_changing': (0, 255, 255), # Cyan - Lane changing + 'turning': (0, 255, 255), # Cyan - Turning + 'accelerating': (255, 0, 0), # Blue - Accelerating + 'braking': (0, 165, 255), # Orange - Braking + 'normal': (0, 255, 0) # Green - Normal driving } - # 行为状态文本映射(使用英文) - self.behavior_texts = { - 'dangerous': 'DANGER', - 'stopped': 'STOP', - 'overtaking': 'OVERTAKE', - 'lane_changing': 'LANE CHANGE', - 'turning': 'TURNING', - 'accelerating': 'ACCEL', - 'braking': 'BRAKE', - 'normal': 'NORMAL' + # Behavior state icon mapping (ASCII only) + self.behavior_icons = { + 'dangerous': '[!]', # Warning + 'stopped': '[S]', # Stopped + 'overtaking': '[F]', # Fast + 'lane_changing': '[<>]', # Lane change + 'turning': '[->]', # Turn + 'accelerating': '[A]', # Accelerating + 'braking': '[B]', # Braking + 'normal': '[-]' # Normal } - logger.info("✅ 可视化器初始化完成(英文版)") + # Performance data history + self.fps_history = [] + self.detection_time_history = [] + self.tracking_time_history = [] + self.max_history_length = 100 # Increased history length for more detailed charts + + # State history (for trend analysis) + self.object_count_history = [] + self.cpu_usage_history = [] + self.memory_usage_history = [] + + logger.info("[OK] Visualizer initialized (Color ID encoding + Independent statistics window)") def _get_behavior_color(self, track_info): """ - 根据行为状态返回对应颜色 + Get color based on behavior state Args: - track_info: 跟踪目标信息字典 + track_info: Tracking target information dictionary Returns: - tuple: BGR颜色值 + tuple: BGR color value """ if not track_info: return self.behavior_colors['normal'] - # 优先级:危险 > 停车 > 超车 > 变道/转弯 > 加速/刹车 > 正常 + # Priority: dangerous > stopped > overtaking > lane changing/turning > accelerating/braking > normal if track_info.get('is_dangerous', False): return self.behavior_colors['dangerous'] elif track_info.get('is_stopped', False): @@ -262,46 +293,46 @@ def _get_behavior_color(self, track_info): else: return self.behavior_colors['normal'] - def _get_behavior_text(self, track_info): + def _get_behavior_icon(self, track_info): """ - 根据行为状态返回对应文本 + Get icon based on behavior state Args: - track_info: 跟踪目标信息字典 + track_info: Tracking target information dictionary Returns: - str: 行为文本 + str: Behavior icon """ if not track_info: - return self.behavior_texts['normal'] + return self.behavior_icons['normal'] - # 优先级:危险 > 停车 > 超车 > 变道/转弯 > 加速/刹车 > 正常 + # Priority: dangerous > stopped > overtaking > lane changing/turning > accelerating/braking > normal if track_info.get('is_dangerous', False): - return self.behavior_texts['dangerous'] + return self.behavior_icons['dangerous'] elif track_info.get('is_stopped', False): - return self.behavior_texts['stopped'] + return self.behavior_icons['stopped'] elif track_info.get('is_overtaking', False): - return self.behavior_texts['overtaking'] + return self.behavior_icons['overtaking'] elif track_info.get('is_lane_changing', False): - return self.behavior_texts['lane_changing'] + return self.behavior_icons['lane_changing'] elif track_info.get('is_turning', False): - return self.behavior_texts['turning'] + return self.behavior_icons['turning'] elif track_info.get('is_accelerating', False): - return self.behavior_texts['accelerating'] + return self.behavior_icons['accelerating'] elif track_info.get('is_braking', False): - return self.behavior_texts['braking'] + return self.behavior_icons['braking'] else: - return self.behavior_texts['normal'] + return self.behavior_icons['normal'] def _get_class_name(self, class_id): """ - 根据类别ID获取类别名称 + Get class name based on class ID Args: - class_id: 类别ID + class_id: Class ID Returns: - str: 类别名称 + str: Class name """ class_map = { 2: 'car', @@ -312,74 +343,421 @@ def _get_class_name(self, class_id): def _adjust_color_brightness(self, color, factor): """ - 调整颜色亮度 + Adjust color brightness Args: - color: 原始颜色 (B, G, R) - factor: 亮度因子 (0.0-1.0) + color: Original color (B, G, R) + factor: Brightness factor (0.0-1.0) Returns: - tuple: 调整后的颜色 + tuple: Adjusted color """ return tuple(int(c * factor) for c in color) + def update_performance_data(self, fps, detection_time, tracking_time, stats_data=None): + """ + Update performance data (enhanced, supports more data) + + Args: + fps: Current frame rate + detection_time: Detection time (seconds) + tracking_time: Tracking time (seconds) + stats_data: Statistics data dictionary + """ + self.fps_history.append(fps) + self.detection_time_history.append(detection_time * 1000) # Convert to milliseconds + self.tracking_time_history.append(tracking_time * 1000) # Convert to milliseconds + + # If there is statistics data, also update state history + if stats_data: + self.object_count_history.append(stats_data.get('total_objects', 0)) + self.cpu_usage_history.append(stats_data.get('cpu_usage', 0)) + self.memory_usage_history.append(stats_data.get('memory_usage', 0)) + + # Keep history data length + for history_list in [ + self.fps_history, + self.detection_time_history, + self.tracking_time_history, + self.object_count_history, + self.cpu_usage_history, + self.memory_usage_history + ]: + if len(history_list) > self.max_history_length: + history_list.pop(0) + + def create_stats_window_image(self, stats_data): + """ + Create image for independent statistics window + + Args: + stats_data: Statistics data dictionary + + Returns: + np.ndarray: Statistics panel image + """ + # Create statistics panel image (light gray background) + stats_width = 600 + stats_height = 800 + stats_image = np.ones((stats_height, stats_width, 3), dtype=np.uint8) * 240 # Light gray background + + # 1. Title area + title_height = 80 + cv2.rectangle(stats_image, (0, 0), (stats_width, title_height), (50, 50, 80), -1) + + title = "CARLA Statistics Panel" + cv2.putText(stats_image, title, + (stats_width // 2 - 150, title_height // 2 + 10), + cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 255, 255), 2) + + subtitle = "Press T to toggle display" + cv2.putText(stats_image, subtitle, + (stats_width // 2 - 140, title_height // 2 + 40), + cv2.FONT_HERSHEY_SIMPLEX, 0.5, (200, 200, 200), 1) + + y_offset = title_height + 20 + + # 2. System status section + y_offset = self._draw_stats_section(stats_image, "System Status", y_offset, stats_data, self._draw_system_stats) + + # 3. Object statistics section + y_offset = self._draw_stats_section(stats_image, "Object Statistics", y_offset, stats_data, self._draw_object_stats) + + # 4. Performance charts section + y_offset = self._draw_stats_section(stats_image, "Performance Charts", y_offset, stats_data, self._draw_performance_charts) + + # 5. History trends section + if len(self.fps_history) > 5: + y_offset = self._draw_stats_section(stats_image, "History Trends", y_offset, stats_data, self._draw_trend_charts) + + # 6. Bottom information + bottom_y = stats_height - 30 + timestamp = time.strftime("%H:%M:%S") + cv2.putText(stats_image, f"Update time: {timestamp}", + (20, bottom_y), + cv2.FONT_HERSHEY_SIMPLEX, 0.5, (100, 100, 100), 1) + + frame_info = f"Total frames: {stats_data.get('total_frames', 0)}" + cv2.putText(stats_image, frame_info, + (stats_width - 150, bottom_y), + cv2.FONT_HERSHEY_SIMPLEX, 0.5, (100, 100, 100), 1) + + return stats_image + + def _draw_stats_section(self, image, title, y_start, stats_data, draw_function): + """ + General template for drawing statistics section + + Returns: + int: Starting Y coordinate of next section + """ + section_height = 200 # Default height for each section + + # Section background + cv2.rectangle(image, (10, y_start), (590, y_start + section_height), (255, 255, 255), -1) + cv2.rectangle(image, (10, y_start), (590, y_start + section_height), (220, 220, 220), 2) + + # Section title + cv2.putText(image, title, (20, y_start + 25), + cv2.FONT_HERSHEY_SIMPLEX, 0.7, (50, 50, 50), 2) + + # Draw dividing line + cv2.line(image, (20, y_start + 35), (580, y_start + 35), (200, 200, 200), 1) + + # Call specific drawing function + content_y = y_start + 50 + content_y = draw_function(image, content_y, stats_data) + + # If drawing function returned new Y coordinate, use it; otherwise use default height + if content_y > y_start + section_height: + section_height = content_y - y_start + + return y_start + section_height + 20 + + def _draw_system_stats(self, image, y_start, stats_data): + """ + Draw system status information + """ + x_left = 30 + x_right = 300 + y = y_start + + # Define status items + status_items = [ + ("FPS", f"{stats_data.get('fps', 0):.1f}", + (0, 255, 0) if stats_data.get('fps', 0) > 20 else (0, 165, 255)), + ("Runtime", f"{stats_data.get('run_time', 0):.0f}s", (100, 100, 100)), + ("CPU Usage", f"{stats_data.get('cpu_usage', 0):.1f}%", + (0, 255, 0) if stats_data.get('cpu_usage', 0) < 70 else (0, 165, 255) if stats_data.get('cpu_usage', 0) < 90 else (0, 0, 255)), + ("Memory Usage", f"{stats_data.get('memory_usage', 0):.1f}%", + (0, 255, 0) if stats_data.get('memory_usage', 0) < 70 else (0, 165, 255) if stats_data.get('memory_usage', 0) < 90 else (0, 0, 255)), + ("Detection Thread", stats_data.get('detection_thread', 'Unknown'), + (0, 255, 0) if stats_data.get('detection_thread') == 'Running' else (0, 0, 255)), + ("Avg Frame Time", f"{stats_data.get('avg_frame_time', 0):.1f}ms", + (0, 255, 0) if stats_data.get('avg_frame_time', 0) < 33 else (0, 165, 255) if stats_data.get('avg_frame_time', 0) < 50 else (0, 0, 255)), + ] + + # Draw in two columns + for i, (label, value, color) in enumerate(status_items): + x = x_left if i % 2 == 0 else x_right + current_y = y + (i // 2) * 30 + + # Label + cv2.putText(image, f"{label}:", (x, current_y), + cv2.FONT_HERSHEY_SIMPLEX, 0.6, (80, 80, 80), 1) + + # Value + cv2.putText(image, value, (x + 120, current_y), + cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2) + + return y + (len(status_items) // 2 + 1) * 30 + + def _draw_object_stats(self, image, y_start, stats_data): + """ + Draw object statistics information + """ + y = y_start + + # Total objects + total_objects = stats_data.get('total_objects', 0) + cv2.putText(image, f"Total Objects: {total_objects}", (30, y), + cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 50), 2) + y += 30 + + # Vehicle type distribution (horizontal bar chart) + vehicle_counts = stats_data.get('vehicle_counts', {}) + if vehicle_counts: + cv2.putText(image, "Vehicle Type Distribution:", (30, y), + cv2.FONT_HERSHEY_SIMPLEX, 0.5, (100, 100, 100), 1) + y += 25 + + max_count = max(vehicle_counts.values()) if vehicle_counts.values() else 1 + bar_width = 200 + + types = ['car', 'bus', 'truck'] + type_names = {'car': 'Car', 'bus': 'Bus', 'truck': 'Truck'} + + for i, v_type in enumerate(types): + count = vehicle_counts.get(v_type, 0) + # Bar chart + bar_length = int((count / max_count) * bar_width) if max_count > 0 else 0 + color = self.class_colors.get(v_type, (100, 100, 100)) + + cv2.rectangle(image, (150, y - 10), (150 + bar_length, y + 5), color, -1) + + # Text + cv2.putText(image, type_names[v_type], (30, y), + cv2.FONT_HERSHEY_SIMPLEX, 0.5, (50, 50, 50), 1) + cv2.putText(image, f"{count}", (370, y), + cv2.FONT_HERSHEY_SIMPLEX, 0.5, (50, 50, 50), 1) + + y += 25 + y += 10 + + # Behavior distribution + behavior_counts = stats_data.get('behavior_counts', {}) + if behavior_counts: + cv2.putText(image, "Behavior Distribution:", (30, y), + cv2.FONT_HERSHEY_SIMPLEX, 0.5, (100, 100, 100), 1) + y += 25 + + # Only show non-zero behaviors + displayed_behaviors = 0 + for behavior, count in behavior_counts.items(): + if count > 0 and behavior in self.behavior_colors: + color = self.behavior_colors[behavior] + icon = self.behavior_icons.get(behavior, '•') + + cv2.putText(image, f"{icon} {behavior}: {count}", (50, y), + cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1) + y += 20 + displayed_behaviors += 1 + + y += 10 if displayed_behaviors > 0 else 0 + + return y + + def _draw_performance_charts(self, image, y_start, stats_data): + """ + Draw performance charts + """ + chart_x = 30 + chart_y = y_start + chart_width = 540 + chart_height = 120 + + # Chart background + cv2.rectangle(image, (chart_x, chart_y), + (chart_x + chart_width, chart_y + chart_height), + (250, 250, 250), -1) + cv2.rectangle(image, (chart_x, chart_y), + (chart_x + chart_width, chart_y + chart_height), + (200, 200, 200), 1) + + if len(self.fps_history) > 1: + # Draw FPS curve (green) + self._draw_chart_curve(image, chart_x, chart_y, chart_width, chart_height, + self.fps_history, (0, 180, 0), "FPS", 60) + + # Draw detection time curve (red) + if self.detection_time_history: + self._draw_chart_curve(image, chart_x, chart_y, chart_width, chart_height, + self.detection_time_history, (200, 0, 0), "Detect(ms)", 100) + + # Draw tracking time curve (blue) + if self.tracking_time_history: + self._draw_chart_curve(image, chart_x, chart_y, chart_width, chart_height, + self.tracking_time_history, (0, 0, 200), "Track(ms)", 50) + + # Chart title + cv2.putText(image, "Real-time Performance Trend (Last 100 frames)", + (chart_x + 10, chart_y + 15), + cv2.FONT_HERSHEY_SIMPLEX, 0.5, (50, 50, 50), 1) + + return chart_y + chart_height + 20 + + def _draw_trend_charts(self, image, y_start, stats_data): + """ + Draw history trend charts + """ + chart_x = 30 + chart_y = y_start + chart_width = 540 + chart_height = 100 + + # Object count trend + if len(self.object_count_history) > 1: + cv2.putText(image, "Object Count Trend:", (chart_x, chart_y - 10), + cv2.FONT_HERSHEY_SIMPLEX, 0.5, (100, 100, 100), 1) + + # Chart background + cv2.rectangle(image, (chart_x, chart_y), + (chart_x + chart_width, chart_y + chart_height), + (250, 250, 250), -1) + cv2.rectangle(image, (chart_x, chart_y), + (chart_x + chart_width, chart_y + chart_height), + (200, 200, 200), 1) + + # Draw object count curve + self._draw_chart_curve(image, chart_x, chart_y, chart_width, chart_height, + self.object_count_history, (100, 0, 200), "Objects", + max(self.object_count_history) if self.object_count_history else 20) + + chart_y += chart_height + 30 + + # System resource trend + cv2.putText(image, "System Resource Trend:", (chart_x, chart_y - 10), + cv2.FONT_HERSHEY_SIMPLEX, 0.5, (100, 100, 100), 1) + + # Chart background + cv2.rectangle(image, (chart_x, chart_y), + (chart_x + chart_width, chart_y + chart_height), + (250, 250, 250), -1) + cv2.rectangle(image, (chart_x, chart_y), + (chart_x + chart_width, chart_y + chart_height), + (200, 200, 200), 1) + + # Draw CPU and memory curves + if len(self.cpu_usage_history) > 1: + self._draw_chart_curve(image, chart_x, chart_y, chart_width, chart_height, + self.cpu_usage_history, (200, 100, 0), "CPU%", 100) + + if len(self.memory_usage_history) > 1: + self._draw_chart_curve(image, chart_x, chart_y, chart_width, chart_height, + self.memory_usage_history, (0, 100, 200), "Memory%", 100) + + return chart_y + chart_height + 20 + + def _draw_chart_curve(self, image, x, y, width, height, data, color, label, max_value): + """ + Draw chart curve (enhanced, with label) + """ + if len(data) < 2: + return + + points = [] + data_len = len(data) + + for i, value in enumerate(data): + # Normalize to 0-1 range + normalized = min(1.0, value / max_value) if max_value > 0 else 0 + + # Calculate coordinates + point_x = int(x + (i / (data_len - 1)) * width) if data_len > 1 else x + point_y = int(y + height - normalized * height) + + points.append((point_x, point_y)) + + # Draw curve + for i in range(1, len(points)): + cv2.line(image, points[i-1], points[i], color, 2) + + # Draw label + label_x = x + width - 80 + label_y = y + 15 + + # Color marker + cv2.circle(image, (label_x - 10, label_y), 4, color, -1) + cv2.putText(image, label, (label_x, label_y), + cv2.FONT_HERSHEY_SIMPLEX, 0.4, (50, 50, 50), 1) + def draw_detections(self, image, boxes, ids, classes, tracks_info=None): """ - 绘制检测和跟踪结果 + Draw detection and tracking results Args: - image: 原始图像 - boxes: 边界框数组 - ids: 跟踪ID数组 - classes: 类别数组 - tracks_info: 跟踪详细信息 + image: Original image + boxes: Bounding box array + ids: Tracking ID array + classes: Class array + tracks_info: Tracking detailed information Returns: - np.ndarray: 绘制后的图像 + np.ndarray: Drawn image """ if not utils.valid_img(image): return image result = image.copy() - # 绘制顶部信息栏 + # Draw top information panel result = self._draw_info_panel(result, len(boxes)) - # 绘制边界框和ID + # Draw bounding boxes and IDs for i, (bbox, track_id, class_id) in enumerate(zip(boxes, ids, classes)): try: x1, y1, x2, y2 = map(int, bbox) - # 确保坐标有效 + # Ensure coordinates are valid if x1 >= x2 or y1 >= y2: continue - # 获取当前目标的详细信息 + # Get current target's detailed information track_info = None if tracks_info and i < len(tracks_info): track_info = tracks_info[i] - # 根据行为状态选择颜色 + # Select color based on behavior state behavior_color = self._get_behavior_color(track_info) - # 根据车辆类别选择基础颜色 + # Select base color based on vehicle class class_name = self._get_class_name(class_id) class_color = self.class_colors.get(class_name, self.class_colors['default']) - # 融合颜色:70%行为颜色 + 30%类别颜色 + # Blend colors: 70% behavior color + 30% class color color = tuple( int(behavior_color[j] * 0.7 + class_color[j] * 0.3) for j in range(3) ) - # 绘制渐变色边框(外深内浅) + # Draw gradient border (dark outside, light inside) border_width = 3 for thickness in range(border_width, 0, -1): - # 计算当前层的颜色亮度 + # Calculate current layer's color brightness brightness = 0.3 + 0.7 * (thickness / border_width) layer_color = self._adjust_color_brightness(color, brightness) - # 绘制边框层 + # Draw border layer offset = border_width - thickness cv2.rectangle(result, (x1 - offset, y1 - offset), @@ -387,13 +765,13 @@ def draw_detections(self, image, boxes, ids, classes, tracks_info=None): layer_color, 1) - # 绘制ID标签背景(使用行为颜色) + # Draw ID label background (use behavior color) id_text = f"ID:{track_id}" (text_width, text_height), baseline = cv2.getTextSize( id_text, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1 ) - # 标签背景 + # Label background label_bg_top = y1 - text_height - 8 label_bg_bottom = y1 label_bg_right = x1 + text_width + 8 @@ -403,43 +781,44 @@ def draw_detections(self, image, boxes, ids, classes, tracks_info=None): (label_bg_right, label_bg_bottom), behavior_color, -1) - # 标签边框 + # Label border cv2.rectangle(result, (x1, label_bg_top), (label_bg_right, label_bg_bottom), (255, 255, 255), 1) - # 绘制ID文本 + # Draw ID text cv2.putText(result, id_text, (x1 + 4, y1 - 4), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1) - # 绘制行为状态(如果可用) + # Draw behavior icon (if available) if track_info: - # 获取行为文本 - behavior_text = self._get_behavior_text(track_info) + # Get behavior icon + behavior_icon = self._get_behavior_icon(track_info) - # 在右上角绘制行为状态 - (text_width, text_height), _ = cv2.getTextSize( - behavior_text, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1 + # Draw behavior status at top-right corner + behavior_text = behavior_icon + (icon_width, icon_height), _ = cv2.getTextSize( + behavior_text, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 2 ) - # 文本位置(右上角) - text_x = x2 - text_width - 5 - text_y = y1 + text_height + 5 + # Icon position (top-right corner) + icon_x = x2 - icon_width - 5 + icon_y = y1 + icon_height + 5 - # 绘制文本背景 + # Draw icon background cv2.rectangle(result, - (text_x - 3, text_y - text_height - 3), - (text_x + text_width + 3, text_y + 3), + (icon_x - 3, icon_y - icon_height - 3), + (icon_x + icon_width + 3, icon_y + 3), behavior_color, -1) - # 绘制文本 + # Draw icon cv2.putText(result, behavior_text, - (text_x, text_y), - cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1) + (icon_x, icon_y), + cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2) - # 绘制速度信息(如果可用) + # Draw speed information (if available) if 'speed' in track_info: speed = track_info['speed'] speed_text = f"{speed:.1f}m/s" @@ -447,50 +826,50 @@ def draw_detections(self, image, boxes, ids, classes, tracks_info=None): speed_text, cv2.FONT_HERSHEY_SIMPLEX, 0.4, 1 ) - # 速度显示在左下角 + # Speed display at bottom-left corner speed_x = x1 + 5 speed_y = y2 - 5 - # 速度背景 + # Speed background cv2.rectangle(result, (speed_x - 2, speed_y - speed_height - 2), (speed_x + speed_width + 2, speed_y + 2), (0, 0, 0), -1) - # 速度文本 + # Speed text cv2.putText(result, speed_text, (speed_x, speed_y), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1) except Exception as e: - logger.debug(f"绘制边界框时出错: {e}") + logger.debug(f"Error drawing bounding box: {e}") continue return result def _draw_info_panel(self, image, track_count): - """绘制信息面板(英文)""" + """Draw information panel""" h, w = image.shape[:2] - # 信息面板背景(半透明黑色) + # Information panel background (semi-transparent black) panel_height = 80 overlay = image.copy() cv2.rectangle(overlay, (0, 0), (w, panel_height), (0, 0, 0), -1) image = cv2.addWeighted(overlay, 0.7, image, 0.3, 0) - # 标题(英文) + # Title title = "CARLA Multi-Object Tracking System" cv2.putText(image, title, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 255, 0), 2) - # 状态信息(英文) + # Status information status_lines = [ - f"Tracking: {track_count} objects", + f"Tracked Objects: {track_count}", f"ESC: Exit | W: Weather | S: Screenshot", - f"P: Pause | M: Show/Hide Legend" + f"P: Pause | T: Stats Window | M: Color Legend" ] - # 绘制状态信息 + # Draw status information font = cv2.FONT_HERSHEY_SIMPLEX for i, line in enumerate(status_lines): y_pos = 55 + i * 20 @@ -501,17 +880,17 @@ def _draw_info_panel(self, image, track_count): def draw_color_legend(self, image): """ - 绘制颜色说明图例(英文) + Draw color legend Args: - image: 原始图像 + image: Original image Returns: - np.ndarray: 添加了图例的图像 + np.ndarray: Image with legend added """ h, w = image.shape[:2] - # 图例背景(右侧半透明) + # Legend background (right side semi-transparent) legend_width = 200 legend_height = 300 legend_x = w - legend_width - 20 @@ -524,58 +903,58 @@ def draw_color_legend(self, image): (40, 40, 40), -1) image = cv2.addWeighted(overlay, 0.8, image, 0.2, 0) - # 图例标题(英文) + # Legend title cv2.putText(image, "Color Legend", (legend_x + 10, legend_y + 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2) - # 行为状态颜色说明(英文) + # Behavior state color explanation behaviors = [ - ('dangerous', 'Dangerous'), - ('stopped', 'Stopped'), - ('overtaking', 'Overtaking'), - ('lane_changing', 'Lane Change'), - ('accelerating', 'Accelerating'), - ('braking', 'Braking'), - ('normal', 'Normal') + ('dangerous', 'Danger', '[!]'), + ('stopped', 'Stopped', '[S]'), + ('overtaking', 'Overtaking', '[F]'), + ('lane_changing', 'Lane Change', '[<>]'), + ('accelerating', 'Accelerating', '[A]'), + ('braking', 'Braking', '[B]'), + ('normal', 'Normal', '[-]') ] y_offset = 60 - for behavior_key, behavior_name in behaviors: - # 颜色方块 + for behavior_key, behavior_name, icon in behaviors: + # Color block color = self.behavior_colors.get(behavior_key, (255, 255, 255)) cv2.rectangle(image, (legend_x + 10, legend_y + y_offset), (legend_x + 30, legend_y + y_offset + 15), color, -1) - # 行为名称 - text = behavior_name + # Behavior name + text = f"{icon} {behavior_name}" cv2.putText(image, text, (legend_x + 40, legend_y + y_offset + 12), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1) y_offset += 25 - # 车辆类别说明(英文) + # Vehicle class explanation cv2.putText(image, "Vehicle Types:", (legend_x + 10, legend_y + y_offset + 20), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 1) classes = [ - ('car', 'Car'), - ('bus', 'Bus'), - ('truck', 'Truck') + ('car', 'Car', ''), + ('bus', 'Bus', ''), + ('truck', 'Truck', '') ] y_offset += 40 - for class_key, class_name in classes: - # 颜色方块 + for class_key, class_name, icon in classes: + # Color block color = self.class_colors.get(class_key, (255, 255, 255)) cv2.rectangle(image, (legend_x + 10, legend_y + y_offset), (legend_x + 30, legend_y + y_offset + 15), color, -1) - # 类别名称 + # Class name text = class_name cv2.putText(image, text, (legend_x + 40, legend_y + y_offset + 12), @@ -585,35 +964,50 @@ def draw_color_legend(self, image): return image - def show(self, image, wait_key=1): + def show(self, image, stats_data=None): """ - 显示图像 + Display image and statistics window Args: - image: 要显示的图像 - wait_key: 等待时间(毫秒) + image: Main window image + stats_data: Statistics data (for updating statistics window) + + Returns: + int: Key value """ + # Display main window if utils.valid_img(image): cv2.imshow(self.window_name, image) - return cv2.waitKey(wait_key) - return -1 + + # Update statistics window (update every few frames to avoid performance impact) + if self.show_stats_window and stats_data is not None: + self.stats_frame_counter += 1 + + if self.stats_frame_counter >= self.stats_update_interval: + self.stats_image = self.create_stats_window_image(stats_data) + if self.stats_image is not None: + cv2.imshow(self.stats_window_name, self.stats_image) + self.stats_frame_counter = 0 + + # Wait for key (brief wait to maintain responsiveness) + return cv2.waitKey(1) def destroy(self): - """销毁窗口""" + """Destroy all windows""" cv2.destroyAllWindows() - logger.info("✅ 可视化窗口已关闭") + logger.info("[OK] All visualization windows closed") -# ======================== 主程序 ======================== +# ======================== Main Program ======================== class CarlaTrackingSystem: - """CARLA跟踪系统主类""" + """CARLA tracking system main class""" def __init__(self, config): self.config = config self.running = False - # 核心组件 + # Core components self.client = None self.world = None self.ego_vehicle = None @@ -622,91 +1016,92 @@ def __init__(self, config): self.tracker = None self.visualizer = None - # 性能监控 + # Performance monitoring self.fps_counter = utils.FPSCounter(window_size=15) self.perf_monitor = utils.PerformanceMonitor() - # 状态变量 + # State variables self.current_weather = config.get('weather', 'clear') self.frame_count = 0 - self.show_legend = True # 是否显示颜色说明 + self.show_legend = True # Whether to show color legend + self.start_time = time.time() # Program start time - # 检测线程相关 + # Detection thread related self.detection_thread = None self.image_queue = None self.result_queue = None - logger.info("✅ 跟踪系统初始化完成(英文版)") + logger.info("[OK] Tracking system initialized (Color ID encoding + Independent statistics window)") def initialize(self): - """初始化系统""" + """Initialize system""" try: - # 1. 连接CARLA + # 1. Connect to CARLA self.client, self.world = setup_carla_client(self.config) if not self.client or not self.world: return False - # 等待CARLA世界稳定 - logger.info("等待CARLA世界稳定...") + # Wait for CARLA world to stabilize + logger.info("Waiting for CARLA world to stabilize...") for i in range(10): self.world.tick() time.sleep(0.1) - # 2. 设置天气 + # 2. Set weather set_weather(self.world, self.current_weather) - # 3. 清理现有的车辆 - logger.info("清理现有车辆...") + # 3. Clean up existing vehicles + logger.info("Clearing existing vehicles...") sensors.clear_all_actors(self.world, []) time.sleep(1.0) - # 4. 创建自车 + # 4. Create ego vehicle self.ego_vehicle = sensors.create_ego_vehicle(self.world, self.config) if not self.ego_vehicle: - logger.error("❌ 创建自车失败") + logger.error("[ERROR] Failed to create ego vehicle") return False - # 等待自车稳定 + # Wait for ego vehicle to stabilize time.sleep(0.5) - # 5. 生成NPC车辆 + # 5. Generate NPC vehicles npc_count = sensors.spawn_npc_vehicles(self.world, self.config) - logger.info(f"✅ 生成 {npc_count} 个NPC车辆") + logger.info(f"[OK] Spawned {npc_count} NPC vehicles") - # 等待NPC车辆生成 + # Wait for NPC vehicles to spawn time.sleep(0.5) - # 6. 初始化传感器 + # 6. Initialize sensors self.sensor_manager = sensors.SensorManager(self.world, self.ego_vehicle, self.config) if not self.sensor_manager.setup(): - logger.error("❌ 传感器初始化失败") + logger.error("[ERROR] Sensor initialization failed") return False - # 7. 初始化检测器 + # 7. Initialize detector self.detector = tracker.YOLODetector(self.config) - # 8. 初始化跟踪器 + # 8. Initialize tracker self.tracker = tracker.SORTTracker(self.config) - # 9. 初始化可视化器 + # 9. Initialize visualizer self.visualizer = Visualizer(self.config) - # 10. 设置检测线程 + # 10. Setup detection thread use_async = self.config.get('use_async_detection', True) if use_async: self._setup_detection_thread() - logger.info("🎉 系统初始化完成,准备开始跟踪") + logger.info("[OK] System initialization complete, ready to start tracking") return True except Exception as e: - logger.error(f"❌ 系统初始化失败: {e}") + logger.error(f"[ERROR] System initialization failed: {e}") import traceback traceback.print_exc() return False def _setup_detection_thread(self): - """设置检测线程""" + """Setup detection thread""" try: import queue self.image_queue = queue.Queue(maxsize=2) @@ -719,46 +1114,135 @@ def _setup_detection_thread(self): maxsize=2 ) self.detection_thread.start() - logger.info("✅ 检测线程已启动") + logger.info("[OK] Detection thread started") except Exception as e: - logger.warning(f"检测线程设置失败,使用同步模式: {e}") + logger.warning(f"Detection thread setup failed, using synchronous mode: {e}") self.detection_thread = None + def _collect_statistics_data(self, fps, detection_time, tracking_time, tracks_info): + """ + Collect statistics data + + Args: + fps: Current frame rate + detection_time: Detection time + tracking_time: Tracking time + tracks_info: Tracking information list + + Returns: + dict: Statistics data + """ + # Get system performance data + cpu_usage = psutil.cpu_percent() + memory_usage = psutil.virtual_memory().percent + + # Get GPU usage (if available) + try: + if torch.cuda.is_available(): + gpu_usage = torch.cuda.utilization() + else: + gpu_usage = 0 + except: + gpu_usage = 0 + + # Count vehicle types + vehicle_counts = {'car': 0, 'bus': 0, 'truck': 0} + for track in tracks_info: + class_name = track.get('class_name', '').lower() + if class_name in vehicle_counts: + vehicle_counts[class_name] += 1 + + # Count behavior types + behavior_counts = { + 'dangerous': 0, 'stopped': 0, 'overtaking': 0, + 'lane_changing': 0, 'turning': 0, 'accelerating': 0, + 'braking': 0, 'normal': 0 + } + + for track in tracks_info: + if track.get('is_dangerous', False): + behavior_counts['dangerous'] += 1 + elif track.get('is_stopped', False): + behavior_counts['stopped'] += 1 + elif track.get('is_overtaking', False): + behavior_counts['overtaking'] += 1 + elif track.get('is_lane_changing', False): + behavior_counts['lane_changing'] += 1 + elif track.get('is_turning', False): + behavior_counts['turning'] += 1 + elif track.get('is_accelerating', False): + behavior_counts['accelerating'] += 1 + elif track.get('is_braking', False): + behavior_counts['braking'] += 1 + else: + behavior_counts['normal'] += 1 + + # Get performance monitoring data + perf_stats = self.perf_monitor.get_stats() + + # Detection thread status + detection_thread_status = 'Running' if self.detection_thread and self.detection_thread.is_alive() else 'Not running' + + return { + # System status + 'fps': fps, + 'total_frames': self.frame_count, + 'run_time': time.time() - self.start_time, + 'cpu_usage': cpu_usage, + 'memory_usage': memory_usage, + 'gpu_usage': gpu_usage, + 'detection_thread': detection_thread_status, + + # Object statistics + 'total_objects': len(tracks_info), + 'vehicle_counts': vehicle_counts, + 'behavior_counts': {k: v for k, v in behavior_counts.items() if v > 0}, + + # Performance metrics + 'avg_detection_time': detection_time * 1000, # Convert to milliseconds + 'avg_tracking_time': tracking_time * 1000, # Convert to milliseconds + 'avg_frame_time': perf_stats.get('avg_frame_time', 0), + + # Raw data (for charts) + 'detection_time': detection_time, + 'tracking_time': tracking_time, + } + def run(self): - """运行主循环""" + """Run main loop""" import time import queue if not self.initialize(): - logger.error("❌ 系统初始化失败,无法运行") + logger.error("[ERROR] System initialization failed, cannot run") return self.running = True - logger.info("🚀 开始跟踪...") + logger.info("[START] Starting tracking...") try: while self.running: - # 开始帧计时 + # Start frame timing self.perf_monitor.start_frame() - # 1. 更新CARLA世界 + # 1. Update CARLA world self.world.tick() - # 2. 获取传感器数据 + # 2. Get sensor data sensor_data = self.sensor_manager.get_sensor_data() image = sensor_data.get('image') if not utils.valid_img(image): - logger.warning("获取到无效图像,跳过本帧") + logger.warning("Invalid image received, skipping frame") time.sleep(0.1) continue - # 3. 执行检测(同步或异步) + # 3. Execute detection (synchronous or asynchronous) detections = [] detection_start = time.time() if self.detection_thread and self.detection_thread.is_alive(): - # 异步检测 + # Asynchronous detection if not self.image_queue.full(): self.image_queue.put(image.copy()) @@ -767,19 +1251,19 @@ def run(self): if processed_image is not None: image = processed_image except queue.Empty: - # 队列为空,使用上一次的检测结果 + # Queue empty, use previous detection result pass else: - # 同步检测 + # Synchronous detection detections = self.detector.detect(image) detection_time = time.time() - detection_start self.perf_monitor.record_detection_time(detection_time) - # 4. 更新跟踪器 + # 4. Update tracker ego_center = (self.config['img_width'] // 2, self.config['img_height'] // 2) - # 获取LiDAR检测结果(如果可用) + # Get LiDAR detection results (if available) lidar_detections = sensor_data.get('lidar_objects', []) tracking_start = time.time() @@ -791,13 +1275,19 @@ def run(self): tracking_time = time.time() - tracking_start self.perf_monitor.record_tracking_time(tracking_time) - # 5. 获取跟踪详细信息 + # 5. Get tracking detailed information tracks_info = self.tracker.get_tracks_info() - # 6. 更新FPS + # 6. Update FPS fps = self.fps_counter.update() - # 7. 可视化 + # 7. Collect statistics data + stats_data = self._collect_statistics_data(fps, detection_time, tracking_time, tracks_info) + + # 8. Update visualizer's performance data + self.visualizer.update_performance_data(fps, detection_time, tracking_time, stats_data) + + # 9. Visualization result_image = self.visualizer.draw_detections( image=image, boxes=boxes, @@ -806,76 +1296,89 @@ def run(self): tracks_info=tracks_info ) - # 添加颜色说明图例(如果启用) + # Add color legend (if enabled) if self.show_legend: result_image = self.visualizer.draw_color_legend(result_image) - # 在图像上显示FPS + # Display FPS on image (top) if utils.valid_img(result_image): fps_text = f"FPS: {fps:.1f}" cv2.putText(result_image, fps_text, (self.config['img_width'] - 100, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2) - # 8. 显示结果 - key = self.visualizer.show(result_image, wait_key=1) + # 10. Display results (pass statistics data for updating statistics window) + key = self.visualizer.show(result_image, stats_data=stats_data) - # 9. 处理键盘输入 + # 11. Handle keyboard input self._handle_keyboard_input(key) - # 10. 帧率控制 + # 12. Frame rate control self._control_frame_rate(fps) - # 11. 更新状态 + # 13. Update state self.frame_count += 1 self.perf_monitor.end_frame() - # 12. 定期打印状态 + # 14. Periodically print status if self.frame_count % 100 == 0: - self._print_status() + self._print_status(stats_data) except KeyboardInterrupt: - logger.info("🛑 用户中断程序") + logger.info("[STOP] User interrupted program") except Exception as e: - logger.error(f"❌ 运行错误: {e}") + logger.error(f"[ERROR] Runtime error: {e}") import traceback traceback.print_exc() finally: self.cleanup() def _handle_keyboard_input(self, key): - """处理键盘输入""" - # ESC键退出 + """Handle keyboard input""" + # ESC key to exit if key == 27: # ESC - logger.info("🛑 ESC键按下,退出程序") + logger.info("[STOP] ESC pressed, exiting program") self.running = False - # W键切换天气 + # W key to switch weather elif key == ord('w') or key == ord('W'): weather_list = ['clear', 'cloudy', 'rain', 'fog', 'night'] current_idx = weather_list.index(self.current_weather) if self.current_weather in weather_list else 0 next_idx = (current_idx + 1) % len(weather_list) self.current_weather = weather_list[next_idx] set_weather(self.world, self.current_weather) - logger.info(f"🌤️ 天气切换到: {self.current_weather}") + logger.info(f"[WEATHER] Weather switched to: {self.current_weather}") - # S键保存截图 + # S key to save screenshot elif key == ord('s') or key == ord('S'): self._save_screenshot() - # P键暂停/继续 + # P key to pause/resume elif key == ord('p') or key == ord('P'): - logger.info("⏸️ 程序暂停,按任意键继续...") + logger.info("[PAUSE] Program paused, press any key to continue...") cv2.waitKey(0) - logger.info("▶️ 程序继续") + logger.info("[RESUME] Program resumed") - # M键切换颜色说明显示 + # T key to toggle statistics window display + elif key == ord('t') or key == ord('T'): + self.visualizer.show_stats_window = not self.visualizer.show_stats_window + status = "Show" if self.visualizer.show_stats_window else "Hide" + logger.info(f"[STATS] Statistics window: {status}") + + # If hiding window, need to close it + if not self.visualizer.show_stats_window: + try: + cv2.destroyWindow(self.visualizer.stats_window_name) + except: + pass # Window may already be closed + + # M key to toggle color legend display elif key == ord('m') or key == ord('M'): self.show_legend = not self.show_legend - status = "显示" if self.show_legend else "隐藏" - logger.info(f"🎨 颜色说明图例: {status}") + status = "Show" if self.show_legend else "Hide" + logger.info(f"[LEGEND] Color legend: {status}") def _control_frame_rate(self, current_fps): - """控制帧率""" + """Control frame rate""" import time target_fps = self.config.get('display_fps', 30) if target_fps <= 0: @@ -883,145 +1386,121 @@ def _control_frame_rate(self, current_fps): target_interval = 1.0 / target_fps - # 如果帧率过高,适当休眠 - if current_fps > target_fps * 1.2: # 允许20%的波动 + # If frame rate is too high, sleep appropriately + if current_fps > target_fps * 1.2: # Allow 20% fluctuation sleep_time = max(0, target_interval - (1.0 / current_fps)) time.sleep(sleep_time) def _save_screenshot(self): - """保存截图""" + """Save screenshot""" try: import time timestamp = time.strftime("%Y%m%d_%H%M%S") filename = f"screenshot_{timestamp}_{self.frame_count:06d}.png" - # 获取当前显示的图像 + # Get currently displayed image screenshot = self.sensor_manager.get_camera_image() if utils.valid_img(screenshot): utils.save_image(screenshot, filename) - logger.info(f"📸 截图已保存: {filename}") + logger.info(f"[SCREENSHOT] Screenshot saved: {filename}") except Exception as e: - logger.warning(f"保存截图失败: {e}") + logger.warning(f"Screenshot save failed: {e}") - def _print_status(self): - """打印系统状态""" - stats = self.perf_monitor.get_stats() - tracks_info = self.tracker.get_tracks_info() - - # 统计行为类型 - behaviors = { - 'stopped': 0, - 'overtaking': 0, - 'lane_changing': 0, - 'turning': 0, - 'accelerating': 0, - 'braking': 0, - 'dangerous': 0, - 'normal': 0 - } - - for track in tracks_info: - if track.get('is_dangerous', False): - behaviors['dangerous'] += 1 - elif track.get('is_stopped', False): - behaviors['stopped'] += 1 - elif track.get('is_overtaking', False): - behaviors['overtaking'] += 1 - elif track.get('is_lane_changing', False): - behaviors['lane_changing'] += 1 - elif track.get('is_turning', False): - behaviors['turning'] += 1 - elif track.get('is_accelerating', False): - behaviors['accelerating'] += 1 - elif track.get('is_braking', False): - behaviors['braking'] += 1 - else: - behaviors['normal'] += 1 - - logger.info(f"📊 状态: 帧数={self.frame_count}, " - f"FPS={stats['avg_fps']:.1f}, " - f"目标数={len(tracks_info)}, " - f"危险={behaviors['dangerous']}, " - f"停车={behaviors['stopped']}, " - f"超车={behaviors['overtaking']}") + def _print_status(self, stats_data): + """Print system status""" + total_objects = stats_data.get('total_objects', 0) + fps = stats_data.get('fps', 0) + cpu_usage = stats_data.get('cpu_usage', 0) + + logger.info(f"[STATUS] Frames={self.frame_count}, " + f"FPS={fps:.1f}, " + f"Objects={total_objects}, " + f"CPU={cpu_usage:.1f}%") def cleanup(self): - """清理资源""" - logger.info("🧹 正在清理资源...") + """Clean up resources""" + logger.info("[CLEANUP] Cleaning up resources...") - # 停止检测线程 + # Stop detection thread if self.detection_thread: self.detection_thread.stop() self.detection_thread.join(timeout=2.0) - # 销毁可视化器 + # Destroy visualizer if self.visualizer: self.visualizer.destroy() - # 销毁传感器 + # Destroy sensors if self.sensor_manager: self.sensor_manager.destroy() - # 清理CARLA演员 + # Clean up CARLA actors if self.world: - # 排除自车ID(如果存在) + # Exclude ego vehicle ID (if exists) exclude_ids = [self.ego_vehicle.id] if self.ego_vehicle and self.ego_vehicle.is_alive else [] sensors.clear_all_actors(self.world, exclude_ids) - # 恢复CARLA设置 + # Restore CARLA settings if self.world: settings = self.world.get_settings() settings.synchronous_mode = False self.world.apply_settings(settings) - # 打印最终性能统计 + # Print final performance statistics if self.perf_monitor: self.perf_monitor.print_stats() - logger.info("✅ 资源清理完成") + # Print final runtime + total_time = time.time() - self.start_time + logger.info(f"[TIME] Total runtime: {total_time:.1f} seconds") + logger.info(f"[STATS] Average FPS: {self.frame_count/total_time:.1f}" if total_time > 0 else "") + + logger.info("[OK] Resource cleanup complete") -# ======================== 主函数 ======================== +# ======================== Main Function ======================== def main(): - """主函数""" - # 解析命令行参数 - parser = argparse.ArgumentParser(description='CARLA多目标跟踪系统') + """Main function""" + # Parse command line arguments + parser = argparse.ArgumentParser(description='CARLA Multi-Object Tracking System') parser.add_argument('--config', type=str, default='config.yaml', - help='配置文件路径 (默认: config.yaml)') + help='Configuration file path (default: config.yaml)') parser.add_argument('--host', type=str, default='localhost', - help='CARLA服务器地址 (默认: localhost)') + help='CARLA server address (default: localhost)') parser.add_argument('--port', type=int, default=2000, - help='CARLA服务器端口 (默认: 2000)') + help='CARLA server port (default: 2000)') parser.add_argument('--weather', type=str, default='clear', choices=['clear', 'cloudy', 'rain', 'fog', 'night'], - help='初始天气 (默认: clear)') + help='Initial weather (default: clear)') parser.add_argument('--model', type=str, default='yolov8n.pt', - help='YOLO模型路径 (默认: yolov8n.pt)') + help='YOLO model path (default: yolov8n.pt)') parser.add_argument('--conf-thres', type=float, default=0.5, - help='检测置信度阈值 (默认: 0.5)') + help='Detection confidence threshold (default: 0.5)') parser.add_argument('--no-lidar', action='store_true', - help='禁用LiDAR') + help='Disable LiDAR') + parser.add_argument('--no-stats', action='store_true', + help='Do not show statistics window at startup') args = parser.parse_args() - # 配置日志 + # Configure logging logger.remove() logger.add(sys.stdout, format="{time:HH:mm:ss} | {level: <8} | {message}", level="INFO") - # 记录开始时间 + # Record start time start_time = time.time() logger.info("=" * 50) - logger.info("🚗 CARLA多目标跟踪系统启动(英文版)") + logger.info("CARLA Multi-Object Tracking System (Enhanced)") logger.info("=" * 50) try: - # 1. 加载配置 + # 1. Load configuration config = load_config(args.config) - # 2. 用命令行参数覆盖配置 + # 2. Override configuration with command line arguments if args.host: config['host'] = args.host if args.port: @@ -1035,38 +1514,49 @@ def main(): if args.no_lidar: config['use_lidar'] = False - # 3. 创建并运行跟踪系统 + # 3. Create and run tracking system system = CarlaTrackingSystem(config) + + # Set initial display state + if args.no_stats: + system.visualizer.show_stats_window = False + system.run() except Exception as e: - logger.error(f"❌ 程序运行异常: {e}") + logger.error(f"[ERROR] Program runtime error: {e}") import traceback traceback.print_exc() finally: - # 计算运行时间 + # Calculate runtime run_time = time.time() - start_time logger.info("=" * 50) - logger.info(f"⏱️ 程序运行时间: {run_time:.1f}秒") - logger.info("👋 程序结束") + logger.info(f"[TIME] Program runtime: {run_time:.1f} seconds") + logger.info("[END] Program ended") logger.info("=" * 50) if __name__ == "__main__": - # 检查必要的导入 + # 检查配置 try: import torch except ImportError: - print("❌ 未找到PyTorch,请安装: pip install torch") + print("[ERROR] PyTorch not found, please install: pip install torch") sys.exit(1) try: import carla except ImportError: - print("❌ 未找到CARLA Python API") - print("请从CARLA安装目录复制PythonAPI/carla到项目目录") + print("[ERROR] CARLA Python API not found") + print("Please copy PythonAPI/carla from CARLA installation directory to project directory") + sys.exit(1) + + try: + import psutil + except ImportError: + print("[ERROR] psutil not found, please install: pip install psutil") sys.exit(1) - # 运行主程序 + # Run main program main() \ No newline at end of file