diff --git a/src/lane_identification/software_package/config.py b/src/lane_identification/software_package/config.py index bcdbf12b58..0a25a8c058 100644 --- a/src/lane_identification/software_package/config.py +++ b/src/lane_identification/software_package/config.py @@ -1,11 +1,16 @@ """ 配置管理模块 - 集中管理所有系统参数 +添加配置热重载功能 """ from dataclasses import dataclass -from typing import Tuple, List +from typing import Tuple, List, Optional, Callable import json import os +import threading +import time +from collections import defaultdict + @dataclass class AppConfig: @@ -57,6 +62,18 @@ class AppConfig: # 车道线检测参数 lane_detection_methods: List[str] = None + # 性能优化参数(新增) + enable_frame_buffer: bool = True + frame_buffer_size: int = 5 + adaptive_skip_frames: bool = True + max_processing_time: float = 0.1 # 最大处理时间(秒) + min_fps: int = 5 # 最小FPS + + # 错误恢复参数(新增) + max_error_count: int = 5 + recovery_timeout: int = 10 + auto_recovery: bool = True + def __post_init__(self): if self.lane_detection_methods is None: self.lane_detection_methods = ['canny', 'sobel', 'gradient'] @@ -77,28 +94,43 @@ def to_dict(self) -> dict: 'prediction_distance': self.prediction_distance, 'video_frame_skip': self.video_frame_skip, 'video_fps': self.video_fps, - 'camera_id': self.camera_id + 'camera_id': self.camera_id, + 'enable_frame_buffer': self.enable_frame_buffer, + 'frame_buffer_size': self.frame_buffer_size, + 'adaptive_skip_frames': self.adaptive_skip_frames, + 'max_processing_time': self.max_processing_time, + 'min_fps': self.min_fps, + 'max_error_count': self.max_error_count, + 'recovery_timeout': self.recovery_timeout, + 'auto_recovery': self.auto_recovery } def save(self, filepath: str): """保存配置到文件""" - with open(filepath, 'w') as f: - json.dump(self.to_dict(), f, indent=2) + with open(filepath, 'w', encoding='utf-8') as f: + json.dump(self.to_dict(), f, indent=2, ensure_ascii=False) + print(f"配置已保存到: {filepath}") @classmethod def load(cls, filepath: str) -> 'AppConfig': """从文件加载配置""" if os.path.exists(filepath): - with open(filepath, 'r') as f: - data = json.load(f) - - # 从字典创建配置对象 - config = cls() - for key, value in data.items(): - if hasattr(config, key): - setattr(config, key, value) - return config + try: + with open(filepath, 'r', encoding='utf-8') as f: + data = json.load(f) + + # 从字典创建配置对象 + config = cls() + for key, value in data.items(): + if hasattr(config, key): + setattr(config, key, value) + print(f"配置已从 {filepath} 加载") + return config + except Exception as e: + print(f"加载配置失败: {e}") + return cls() else: + print(f"配置文件不存在: {filepath}") return cls() @@ -113,7 +145,8 @@ class SceneConfig: hough_threshold=35, prediction_distance=0.9, confidence_threshold=0.7, - video_fps=15 + video_fps=15, + max_processing_time=0.08 ) # 城市道路配置 @@ -124,7 +157,8 @@ class SceneConfig: hough_threshold=25, prediction_distance=0.6, confidence_threshold=0.5, - video_fps=10 + video_fps=10, + max_processing_time=0.12 ) # 乡村道路配置 @@ -137,7 +171,8 @@ class SceneConfig: min_contour_area=0.002, prediction_distance=0.7, confidence_threshold=0.4, - video_fps=8 + video_fps=8, + max_processing_time=0.15 ) @classmethod @@ -148,4 +183,140 @@ def get_scene_config(cls, scene_type: str) -> AppConfig: 'urban': cls.URBAN, 'rural': cls.RURAL } - return config_map.get(scene_type, AppConfig()) \ No newline at end of file + return config_map.get(scene_type, AppConfig()) + + +class ConfigManager: + """配置管理器 - 支持热重载""" + + _instance = None + _lock = threading.Lock() + + def __new__(cls): + with cls._lock: + if cls._instance is None: + cls._instance = super(ConfigManager, cls).__new__(cls) + return cls._instance + + def __init__(self, config_file: str = "app_config.json"): + if not hasattr(self, 'initialized'): + self.config_file = config_file + self.config = AppConfig() + self.last_modified = 0 + self.change_callbacks = defaultdict(list) + self.watch_thread = None + self.is_watching = False + self.initialized = True + + # 自动加载配置 + self._load_config() + + def _load_config(self): + """加载配置""" + if os.path.exists(self.config_file): + try: + self.config = AppConfig.load(self.config_file) + self.last_modified = os.path.getmtime(self.config_file) + print(f"配置管理器初始化完成,使用配置文件: {self.config_file}") + except Exception as e: + print(f"配置管理器初始化失败: {e}") + else: + print("配置文件不存在,使用默认配置") + + def add_change_callback(self, callback: Callable[[AppConfig], None], + component: str = "global"): + """添加配置变更回调""" + self.change_callbacks[component].append(callback) + + def remove_change_callback(self, callback: Callable[[AppConfig], None], + component: str = "global"): + """移除配置变更回调""" + if component in self.change_callbacks: + self.change_callbacks[component] = [ + cb for cb in self.change_callbacks[component] if cb != callback + ] + + def notify_change(self, component: str = "global"): + """通知配置变更""" + if component in self.change_callbacks: + for callback in self.change_callbacks[component]: + try: + callback(self.config) + except Exception as e: + print(f"配置变更回调执行失败: {e}") + + def save_current_config(self) -> bool: + """保存当前配置""" + try: + self.config.save(self.config_file) + self.last_modified = os.path.getmtime(self.config_file) + print("当前配置已保存") + return True + except Exception as e: + print(f"保存配置失败: {e}") + return False + + def check_and_reload(self) -> bool: + """检查并重新加载配置""" + try: + if os.path.exists(self.config_file): + mtime = os.path.getmtime(self.config_file) + if mtime > self.last_modified: + print("检测到配置文件变更,重新加载...") + self._load_config() + self.notify_change() + return True + except Exception as e: + print(f"配置重载失败: {e}") + return False + + def start_watching(self, interval: float = 2.0): + """开始监控配置文件变化""" + if self.is_watching: + return + + self.is_watching = True + self.watch_thread = threading.Thread( + target=self._watch_config_file, + args=(interval,), + daemon=True + ) + self.watch_thread.start() + print(f"开始监控配置文件变化,检查间隔: {interval}秒") + + def stop_watching(self): + """停止监控配置文件变化""" + self.is_watching = False + if self.watch_thread: + self.watch_thread.join(timeout=1.0) + print("停止监控配置文件变化") + + def _watch_config_file(self, interval: float): + """监控配置文件变化""" + while self.is_watching: + self.check_and_reload() + time.sleep(interval) + + def update_config(self, updates: dict, save_to_file: bool = True): + """更新配置""" + for key, value in updates.items(): + if hasattr(self.config, key): + old_value = getattr(self.config, key) + setattr(self.config, key, value) + print(f"配置更新: {key} = {old_value} -> {value}") + + if save_to_file: + self.save_current_config() + + self.notify_change() + + def reset_to_defaults(self): + """重置为默认配置""" + self.config = AppConfig() + self.save_current_config() + self.notify_change() + print("配置已重置为默认值") + + +# 全局配置管理器实例 +config_manager = ConfigManager() \ No newline at end of file diff --git a/src/lane_identification/software_package/main.py b/src/lane_identification/software_package/main.py index f385405a53..bd07b5d126 100644 --- a/src/lane_identification/software_package/main.py +++ b/src/lane_identification/software_package/main.py @@ -1,43 +1,59 @@ """ 主应用程序模块 - 支持图像、视频和摄像头实时识别 -修复摄像头调用问题,移除日志功能 +添加异常恢复、性能监控和配置热重载 """ import tkinter as tk -from tkinter import filedialog, messagebox, ttk +from tkinter import filedialog, messagebox, ttk, scrolledtext import threading import time +import traceback from collections import deque from PIL import Image, ImageTk import cv2 import numpy as np import os +import sys # 导入各个模块 -from config import AppConfig, SceneConfig +from config import AppConfig, SceneConfig, config_manager from image_processor import SmartImageProcessor, RoadDetector from lane_detector import LaneDetector from direction_analyzer import DirectionAnalyzer from visualizer import Visualizer from video_processor import VideoProcessor +from utils import PerformanceMonitor, Timer, safe_resize, calculate_fps + class LaneDetectionApp: - """道路方向识别系统主应用程序""" + """道路方向识别系统主应用程序 - 优化版本""" def __init__(self, root): self.root = root self._setup_window() - # 初始化配置 - self.config = AppConfig() + # 初始化配置管理器 + self.config_manager = config_manager + self.config = self.config_manager.config + + # 注册配置变更回调 + self.config_manager.add_change_callback(self._on_config_changed, "main_app") + + # 启动配置监控 + self.config_manager.start_watching(interval=2.0) + + # 性能监控器 + self.performance_monitor = PerformanceMonitor(max_samples=100) + + # 错误处理 + self.error_count = 0 + self.last_error_time = 0 + self.recovery_mode = False + self.max_errors = self.config.max_error_count + self.recovery_timeout = self.config.recovery_timeout # 初始化各个模块 - self.image_processor = SmartImageProcessor(self.config) - self.road_detector = RoadDetector(self.config) - self.lane_detector = LaneDetector(self.config) - self.direction_analyzer = DirectionAnalyzer(self.config) - self.visualizer = Visualizer(self.config) - self.video_processor = VideoProcessor(self.config) + self._initialize_modules() # 状态变量 self.current_image = None @@ -60,14 +76,100 @@ def __init__(self, root): # 创建界面 self._create_ui() - print("道路方向识别系统已启动(支持视频/摄像头)") + # 创建性能监控窗口(隐藏) + self._create_performance_window() + + print("道路方向识别系统已启动(优化版本)") + print(f"配置: {self.config.to_dict()}") + + def _initialize_modules(self): + """初始化各个模块""" + try: + self.image_processor = SmartImageProcessor(self.config) + self.road_detector = RoadDetector(self.config) + self.lane_detector = LaneDetector(self.config) + self.direction_analyzer = DirectionAnalyzer(self.config) + self.visualizer = Visualizer(self.config) + self.video_processor = VideoProcessor(self.config) + print("所有模块初始化完成") + except Exception as e: + print(f"模块初始化失败: {e}") + self._recover_from_error() + + def _recover_from_error(self): + """从错误中恢复""" + if self.recovery_mode: + return + + self.recovery_mode = True + print("进入恢复模式...") + + try: + # 延迟后重新初始化 + self.root.after(1000, self._perform_recovery) + except Exception as e: + print(f"恢复失败: {e}") + + def _perform_recovery(self): + """执行恢复操作""" + try: + # 重新初始化模块 + self._initialize_modules() + + # 重置状态 + self.error_count = 0 + self.recovery_mode = False + + # 更新状态显示 + self.status_var.set("系统已恢复") + print("系统恢复完成") + + # 如果是视频模式,尝试重新开始 + if self.is_video_mode and self.video_file_path: + self._open_video(self.video_file_path) + + except Exception as e: + print(f"恢复操作失败: {e}") + # 如果恢复失败,等待更长时间后重试 + self.root.after(5000, self._perform_recovery) + + def _on_config_changed(self, new_config): + """配置变更回调""" + print("检测到配置变更,重新初始化模块...") + self.config = new_config + + # 重新初始化模块 + self._initialize_modules() + + # 更新UI + self._update_ui_from_config() + + # 重新检测当前图像 + if self.current_image_path and not self.is_processing and not self.is_video_mode: + self.root.after(500, self._redetect) + + def _update_ui_from_config(self): + """根据配置更新UI""" + # 更新敏感度滑块 + if hasattr(self, 'sensitivity_var'): + # 从配置计算敏感度值(0-1范围) + sensitivity = (self.config.canny_threshold1 - 30) / 40.0 + sensitivity = max(0.0, min(1.0, sensitivity)) + self.sensitivity_var.set(sensitivity) def _setup_window(self): """设置窗口""" - self.root.title("道路方向识别系统 - 支持视频/摄像头") - self.root.geometry("1400x850") + self.root.title("道路方向识别系统 - 优化版本") + self.root.geometry("1400x900") self.root.minsize(1200, 700) + # 设置窗口图标(如果有) + try: + if os.path.exists("icon.ico"): + self.root.iconbitmap("icon.ico") + except: + pass + # 设置窗口居中 self.root.update_idletasks() width = self.root.winfo_width() @@ -102,6 +204,9 @@ def _create_ui(self): # 状态栏 self._create_status_bar(main_container) + + # 性能监控按钮 + self._create_performance_button(main_container) def _create_title_bar(self, parent): """创建标题栏""" @@ -111,7 +216,7 @@ def _create_title_bar(self, parent): # 标题 title_label = ttk.Label( title_frame, - text="道路方向识别系统(支持视频/摄像头)", + text="道路方向识别系统 - 优化版本", font=("微软雅黑", 16, "bold"), foreground="#2c3e50" ) @@ -125,6 +230,15 @@ def _create_title_bar(self, parent): foreground="#3498db" ) self.mode_label.pack(side="right", padx=(0, 10)) + + # 配置状态指示器 + self.config_status_label = ttk.Label( + title_frame, + text="✓", + font=("微软雅黑", 10), + foreground="#27ae60" + ) + self.config_status_label.pack(side="right", padx=(0, 10)) def _create_control_panel(self, parent): """创建控制面板""" @@ -218,7 +332,7 @@ def _create_control_panel(self, parent): camera_frame.pack(fill="x", pady=(0, 10)) ttk.Label(camera_frame, text="摄像头索引:").pack(side="left") - self.camera_index_var = tk.StringVar(value="0") + self.camera_index_var = tk.StringVar(value=str(self.config.camera_id)) self.camera_index_combo = ttk.Combobox( camera_frame, textvariable=self.camera_index_var, @@ -268,6 +382,29 @@ def _create_control_panel(self, parent): ) self.stop_btn.pack(side="left") + # 高级设置区域 + advanced_frame = ttk.LabelFrame(control_frame, text="高级设置", padding="10") + advanced_frame.pack(fill="x", pady=(0, 15)) + + # 性能优化选项 + self.enable_buffer_var = tk.BooleanVar(value=self.config.enable_frame_buffer) + buffer_check = ttk.Checkbutton( + advanced_frame, + text="启用帧缓冲", + variable=self.enable_buffer_var, + command=self._on_advanced_option_change + ) + buffer_check.pack(anchor="w", pady=(0, 5)) + + self.adaptive_skip_var = tk.BooleanVar(value=self.config.adaptive_skip_frames) + skip_check = ttk.Checkbutton( + advanced_frame, + text="自适应跳帧", + variable=self.adaptive_skip_var, + command=self._on_advanced_option_change + ) + skip_check.pack(anchor="w", pady=(0, 10)) + # 参数调节区域 param_frame = ttk.LabelFrame(control_frame, text="参数调节", padding="10") param_frame.pack(fill="x", pady=(0, 15)) @@ -338,6 +475,15 @@ def _create_control_panel(self, parent): ) self.time_label.pack(anchor="w") + # 性能信息显示 + self.performance_label = ttk.Label( + result_frame, + text="", + font=("微软雅黑", 8), + foreground="#bdc3c7" + ) + self.performance_label.pack(anchor="w", pady=(5, 0)) + return control_frame def _create_display_panel(self, parent): @@ -417,6 +563,171 @@ def _create_status_bar(self, parent): font=("微软雅黑", 9) ) status_label.pack(side="right", padx=(0, 10), pady=5) + + # 错误计数显示 + self.error_label = ttk.Label( + status_frame, + text="错误: 0", + font=("微软雅黑", 9), + foreground="#e74c3c" + ) + self.error_label.pack(side="right", padx=(0, 20), pady=5) + + def _create_performance_button(self, parent): + """创建性能监控按钮""" + perf_button = ttk.Button( + parent, + text="性能监控", + command=self._show_performance_window, + width=10 + ) + perf_button.pack(side="right", padx=(0, 10), pady=5) + + def _create_performance_window(self): + """创建性能监控窗口""" + self.perf_window = None + + def _show_performance_window(self): + """显示性能监控窗口""" + if self.perf_window is not None and self.perf_window.winfo_exists(): + self.perf_window.deiconify() + self.perf_window.lift() + return + + self.perf_window = tk.Toplevel(self.root) + self.perf_window.title("性能监控") + self.perf_window.geometry("600x500") + self.perf_window.transient(self.root) + + # 创建选项卡 + notebook = ttk.Notebook(self.perf_window) + notebook.pack(fill="both", expand=True, padx=10, pady=10) + + # 实时监控标签页 + realtime_frame = ttk.Frame(notebook) + notebook.add(realtime_frame, text="实时监控") + + # 性能指标显示 + self.perf_text = scrolledtext.ScrolledText( + realtime_frame, + wrap=tk.WORD, + font=("Consolas", 10), + height=20 + ) + self.perf_text.pack(fill="both", expand=True, padx=10, pady=10) + + # 配置标签页 + config_frame = ttk.Frame(notebook) + notebook.add(config_frame, text="配置查看") + + self.config_text = scrolledtext.ScrolledText( + config_frame, + wrap=tk.WORD, + font=("Consolas", 9), + height=20 + ) + self.config_text.pack(fill="both", expand=True, padx=10, pady=10) + + # 更新配置显示 + self._update_config_display() + + # 按钮框架 + button_frame = ttk.Frame(self.perf_window) + button_frame.pack(fill="x", padx=10, pady=(0, 10)) + + ttk.Button( + button_frame, + text="刷新", + command=self._update_performance_display + ).pack(side="left", padx=(0, 10)) + + ttk.Button( + button_frame, + text="重置统计", + command=self._reset_performance_stats + ).pack(side="left", padx=(0, 10)) + + ttk.Button( + button_frame, + text="保存配置", + command=self._save_current_config + ).pack(side="left", padx=(0, 10)) + + ttk.Button( + button_frame, + text="关闭", + command=self.perf_window.destroy + ).pack(side="right") + + # 开始定时更新 + self._start_performance_update() + + def _update_performance_display(self): + """更新性能显示""" + if self.perf_window is None or not self.perf_window.winfo_exists(): + return + + summary = self.performance_monitor.get_summary() + + self.perf_text.delete(1.0, tk.END) + self.perf_text.insert(tk.END, "=== 性能监控数据 ===\n\n") + + for metric, stats in summary.items(): + self.perf_text.insert(tk.END, f"{metric}:\n") + self.perf_text.insert(tk.END, f" 平均值: {stats['avg']:.4f}s\n") + self.perf_text.insert(tk.END, f" 最小值: {stats['min']:.4f}s\n") + self.perf_text.insert(tk.END, f" 最大值: {stats['max']:.4f}s\n") + self.perf_text.insert(tk.END, f" 标准差: {stats['std']:.4f}s\n") + self.perf_text.insert(tk.END, f" 95%分位: {stats['p95']:.4f}s\n") + self.perf_text.insert(tk.END, f" 样本数: {stats['count']}\n\n") + + # 添加视频处理信息 + if self.is_video_mode: + video_info = self.video_processor.get_video_info() + self.perf_text.insert(tk.END, "=== 视频处理信息 ===\n\n") + for key, value in video_info.items(): + if isinstance(value, float): + self.perf_text.insert(tk.END, f"{key}: {value:.3f}\n") + else: + self.perf_text.insert(tk.END, f"{key}: {value}\n") + + def _update_config_display(self): + """更新配置显示""" + if self.perf_window is None or not self.perf_window.winfo_exists(): + return + + self.config_text.delete(1.0, tk.END) + config_dict = self.config.to_dict() + + self.config_text.insert(tk.END, "=== 当前配置 ===\n\n") + for key, value in config_dict.items(): + self.config_text.insert(tk.END, f"{key}: {value}\n") + + def _start_performance_update(self): + """开始性能数据更新""" + if self.perf_window is not None and self.perf_window.winfo_exists(): + self._update_performance_display() + self.root.after(2000, self._start_performance_update) + + def _reset_performance_stats(self): + """重置性能统计""" + self.performance_monitor.reset() + self._update_performance_display() + + def _save_current_config(self): + """保存当前配置""" + if self.config_manager.save_current_config(): + messagebox.showinfo("成功", "配置已保存") + self._update_config_display() + + def _on_advanced_option_change(self): + """高级选项变更""" + updates = { + 'enable_frame_buffer': self.enable_buffer_var.get(), + 'adaptive_skip_frames': self.adaptive_skip_var.get() + } + + self.config_manager.update_config(updates, save_to_file=True) def _switch_to_image_mode(self): """切换到图像模式""" @@ -496,7 +807,7 @@ def _show_camera_preview(self): try: self.camera_index = int(self.camera_index_var.get()) except: - self.camera_index = 0 + self.camera_index = self.config.camera_id # 尝试打开摄像头 self.status_var.set(f"正在打开摄像头 {self.camera_index}...") @@ -712,40 +1023,71 @@ def _stop_video(self): def _process_video_frame(self, frame, frame_info): """处理视频帧""" try: - start_time = time.time() - - # 预处理帧 - processed_frame, roi_info = self.image_processor.preprocess_frame(frame) - - # 道路检测 - road_info = self.road_detector.detect_road(processed_frame, roi_info.get('mask', np.ones(processed_frame.shape[:2], dtype=np.uint8))) - - # 车道线检测 - lane_info = self.lane_detector.detect(processed_frame, roi_info.get('mask', np.ones(processed_frame.shape[:2], dtype=np.uint8))) - - # 方向分析 - direction_info = self.direction_analyzer.analyze(road_info, lane_info) - - # 创建可视化 - visualization = self.visualizer.create_visualization( - processed_frame, road_info, lane_info, direction_info, - True, frame_info - ) + with self.performance_monitor.start_timer("total_processing") as timer: + # 预处理帧 + processed_frame, roi_info = self.image_processor.preprocess_frame(frame) + + # 道路检测 + with self.performance_monitor.start_timer("road_detection"): + road_info = self.road_detector.detect_road( + processed_frame, + roi_info.get('mask', np.ones(processed_frame.shape[:2], dtype=np.uint8)) + ) + + # 车道线检测 + with self.performance_monitor.start_timer("lane_detection"): + lane_info = self.lane_detector.detect( + processed_frame, + roi_info.get('mask', np.ones(processed_frame.shape[:2], dtype=np.uint8)) + ) + + # 方向分析 + with self.performance_monitor.start_timer("direction_analysis"): + direction_info = self.direction_analyzer.analyze(road_info, lane_info) + + # 创建可视化 + with self.performance_monitor.start_timer("visualization"): + visualization = self.visualizer.create_visualization( + processed_frame, road_info, lane_info, direction_info, + True, frame_info + ) - processing_time = time.time() - start_time + processing_time = timer.stop() # 在主线程中更新UI self.root.after(0, self._update_video_results, processed_frame, visualization, direction_info, lane_info, processing_time, frame_info) - # 更新性能统计 - self.processing_times.append(processing_time) - if len(self.processing_times) > 10: - self.processing_times.pop(0) + # 更新错误计数 + self.error_count = 0 except Exception as e: print(f"视频帧处理失败: {e}") + self.error_count += 1 + self._handle_error(e) + + def _handle_error(self, error): + """处理错误""" + current_time = time.time() + + # 更新错误显示 + self.root.after(0, lambda: self.error_label.config(text=f"错误: {self.error_count}")) + + # 检查是否需要进入恢复模式 + if (self.error_count >= self.max_errors and + current_time - self.last_error_time < self.recovery_timeout): + + self.last_error_time = current_time + + # 自动恢复 + if self.config.auto_recovery: + print("错误次数过多,触发自动恢复...") + self._recover_from_error() + else: + messagebox.showwarning("警告", "错误次数过多,建议重启应用程序") + + self.last_error_time = current_time def _load_image(self, file_path): """加载图像""" @@ -756,51 +1098,66 @@ def _load_image(self, file_path): self.redetect_btn.config(state="normal") # 在后台线程中处理 - thread = threading.Thread(target=self._process_image, args=(file_path,)) + thread = threading.Thread(target=self._process_image_with_recovery, args=(file_path,)) thread.daemon = True thread.start() except Exception as e: + self._handle_error(e) messagebox.showerror("错误", f"加载图片失败: {str(e)}") self.status_var.set("加载失败") + def _process_image_with_recovery(self, file_path): + """带恢复机制的图像处理""" + try: + self._process_image(file_path) + self.error_count = 0 + self.recovery_mode = False + + except Exception as e: + self._handle_error(e) + traceback.print_exc() + def _process_image(self, file_path): """处理图像""" - start_time = time.time() + # 标记为处理中 + self.is_processing = True + self.root.after(0, self._update_processing_state, True) try: - # 标记为处理中 - self.is_processing = True - self.root.after(0, self._update_processing_state, True) - - # 1. 图像预处理 - result = self.image_processor.load_and_preprocess(file_path) - if result is None: - raise ValueError("无法处理图像") - - self.current_image, roi_info = result - - # 2. 道路检测 - road_info = self.road_detector.detect_road(self.current_image, roi_info['mask']) - - # 3. 车道线检测 - lane_info = self.lane_detector.detect(self.current_image, roi_info['mask']) - - # 4. 方向分析 - direction_info = self.direction_analyzer.analyze(road_info, lane_info) - - # 5. 创建可视化 - visualization = self.visualizer.create_visualization( - self.current_image, road_info, lane_info, direction_info - ) + with self.performance_monitor.start_timer("total_processing") as timer: + # 1. 图像预处理 + result = self.image_processor.load_and_preprocess(file_path) + if result is None: + raise ValueError("无法处理图像") + + self.current_image, roi_info = result + + # 2. 道路检测 + with self.performance_monitor.start_timer("road_detection"): + road_info = self.road_detector.detect_road(self.current_image, roi_info['mask']) + + # 3. 车道线检测 + with self.performance_monitor.start_timer("lane_detection"): + lane_info = self.lane_detector.detect(self.current_image, roi_info['mask']) + + # 4. 方向分析 + with self.performance_monitor.start_timer("direction_analysis"): + direction_info = self.direction_analyzer.analyze(road_info, lane_info) + + # 5. 创建可视化 + with self.performance_monitor.start_timer("visualization"): + visualization = self.visualizer.create_visualization( + self.current_image, road_info, lane_info, direction_info + ) - processing_time = time.time() - start_time + processing_time = timer.stop() # 在主线程中更新UI self.root.after(0, self._update_results, direction_info, lane_info, visualization, processing_time) - # 更新统计信息 + # 更新性能统计 self.processing_times.append(processing_time) if len(self.processing_times) > 10: self.processing_times.pop(0) @@ -864,7 +1221,18 @@ def _update_results(self, direction_info, lane_info, visualization, processing_t self.quality_label.config(text=f"检测质量: {quality:.1%}") # 设置处理时间 - self.time_label.config(text=f"处理时间: {processing_time:.3f}秒") + avg_time = self.performance_monitor.get_statistics("total_processing_time")['avg'] + self.time_label.config(text=f"处理时间: {processing_time:.3f}s (平均: {avg_time:.3f}s)") + + # 更新性能信息 + perf_stats = self.performance_monitor.get_summary() + if perf_stats: + recent_avg = np.mean(list(self.processing_times[-5:])) if len(self.processing_times) >= 5 else 0 + self.performance_label.config( + text=f"模块耗时: 道路{perf_stats.get('road_detection_time', {}).get('avg', 0):.3f}s, " + f"车道{perf_stats.get('lane_detection_time', {}).get('avg', 0):.3f}s, " + f"方向{perf_stats.get('direction_analysis_time', {}).get('avg', 0):.3f}s" + ) # 更新状态 self.status_var.set(f"分析完成 - {direction}") @@ -914,13 +1282,42 @@ def _update_video_results(self, original_frame, visualization, direction_info, l self.last_fps_update = current_time self.frame_counter = 0 + # 获取视频信息 + video_info = self.video_processor.get_video_info() + adaptive_skip = video_info.get('adaptive_frame_skip', 1) + buffer_usage = video_info.get('buffer_usage', '0/5') + # 设置处理时间和FPS - time_text = f"处理时间: {processing_time:.3f}s | FPS: {self.current_fps:.1f}" + time_text = f"处理: {processing_time:.3f}s | FPS: {self.current_fps:.1f} | 跳帧: {adaptive_skip} | 缓冲: {buffer_usage}" self.time_label.config(text=time_text) + # 更新性能信息 + perf_stats = self.performance_monitor.get_summary() + if perf_stats: + module_times = [] + for module in ['road_detection_time', 'lane_detection_time', 'direction_analysis_time', 'visualization_time']: + if module in perf_stats: + module_times.append(f"{module.split('_')[0]}:{perf_stats[module]['avg']:.3f}s") + + if module_times: + self.performance_label.config(text=" | ".join(module_times)) + # 更新状态 video_type = "摄像头" if self.camera_mode else "视频" - self.status_var.set(f"{video_type}处理中 - {direction} | FPS: {self.current_fps:.1f}") + status_text = f"{video_type}处理中 - {direction} | FPS: {self.current_fps:.1f}" + + # 添加缓冲信息 + if hasattr(self.video_processor, 'frame_buffer'): + buffer_len = len(self.video_processor.frame_buffer) + max_len = self.video_processor.frame_buffer.maxlen + buffer_percent = (buffer_len / max_len) * 100 + + if buffer_percent < 20: + status_text += f" | 缓冲低({buffer_percent:.0f}%)" + elif buffer_percent > 80: + status_text += f" | 缓冲高({buffer_percent:.0f}%)" + + self.status_var.set(status_text) except Exception as e: print(f"更新视频结果失败: {e}") @@ -947,7 +1344,7 @@ def _display_image(self, image, canvas, title): # 计算缩放比例 img_width, img_height = pil_image.size - scale = min(canvas_width / img_width, canvas_height / img_height) + scale = min(canvas_width / img_width, canvas_height / img_height) * 0.95 # 留出边距 if scale < 1: new_size = (int(img_width * scale), int(img_height * scale)) @@ -963,6 +1360,14 @@ def _display_image(self, image, canvas, title): canvas.create_image(x, y, anchor="nw", image=photo) canvas.image = photo # 保持引用 + # 添加标题 + canvas.create_text( + canvas_width // 2, 15, + text=title, + font=("微软雅黑", 10, "bold"), + fill="#2c3e50" + ) + except Exception as e: print(f"显示图像失败: {e}") canvas.create_text(150, 150, text="图像显示失败", fill="red") @@ -989,18 +1394,23 @@ def _clear_canvas_display(self): def _redetect(self): """重新检测""" if self.current_image_path and not self.is_processing and not self.is_video_mode: - self._process_image(self.current_image_path) + self._process_image_with_recovery(self.current_image_path) def _on_parameter_change(self, value): """参数变化回调""" sensitivity = self.sensitivity_var.get() # 根据敏感度调整参数 - self.config.canny_threshold1 = int(30 + sensitivity * 40) - self.config.canny_threshold2 = int(80 + sensitivity * 100) - self.config.hough_threshold = int(20 + (1 - sensitivity) * 30) + updates = { + 'canny_threshold1': int(30 + sensitivity * 40), + 'canny_threshold2': int(80 + sensitivity * 100), + 'hough_threshold': int(20 + (1 - sensitivity) * 30) + } + + # 更新配置 + self.config_manager.update_config(updates, save_to_file=True) - print(f"参数更新: 敏感度={sensitivity:.2f}") + print(f"参数更新: 敏感度={sensitivity:.2f}, 更新={updates}") # 如果已有图像,自动重新检测 if self.current_image_path and not self.is_processing and not self.is_video_mode: @@ -1011,24 +1421,16 @@ def _on_scene_change(self, event): scene = self.scene_var.get() if scene == "高速公路": - config = SceneConfig.get_scene_config('highway') + new_config = SceneConfig.get_scene_config('highway') elif scene == "城市道路": - config = SceneConfig.get_scene_config('urban') + new_config = SceneConfig.get_scene_config('urban') elif scene == "乡村道路": - config = SceneConfig.get_scene_config('rural') + new_config = SceneConfig.get_scene_config('rural') else: # 自动 return - # 更新配置 - self.config = config - - # 重新初始化模块 - self.image_processor = SmartImageProcessor(self.config) - self.road_detector = RoadDetector(self.config) - self.lane_detector = LaneDetector(self.config) - self.direction_analyzer = DirectionAnalyzer(self.config) - self.visualizer = Visualizer(self.config) - self.video_processor = VideoProcessor(self.config) + # 更新配置管理器 + self.config_manager.update_config(new_config.to_dict(), save_to_file=True) print(f"场景切换为: {scene}") self.status_var.set(f"场景已切换为: {scene}") @@ -1041,10 +1443,16 @@ def _show_error(self, error_msg): """显示错误""" messagebox.showerror("错误", f"处理失败: {error_msg}") self.status_var.set("处理失败") + self.error_count += 1 + self.error_label.config(text=f"错误: {self.error_count}") def _on_closing(self): """窗口关闭事件""" try: + # 停止配置监控 + if hasattr(self, 'config_manager'): + self.config_manager.stop_watching() + # 停止视频处理 if self.is_video_mode: self._stop_video() @@ -1054,6 +1462,10 @@ def _on_closing(self): print("正在释放摄像头资源...") self.video_processor.release() + # 关闭性能监控窗口 + if self.perf_window is not None and self.perf_window.winfo_exists(): + self.perf_window.destroy() + # 关闭窗口 self.root.destroy() print("应用程序已关闭") @@ -1062,6 +1474,7 @@ def _on_closing(self): print(f"关闭应用程序时出错: {e}") self.root.destroy() + def main(): """主函数""" try: @@ -1076,7 +1489,9 @@ def main(): except Exception as e: print(f"应用程序启动失败: {e}") + traceback.print_exc() messagebox.showerror("致命错误", f"应用程序启动失败: {str(e)}") + if __name__ == "__main__": main() \ No newline at end of file diff --git a/src/lane_identification/software_package/utils.py b/src/lane_identification/software_package/utils.py new file mode 100644 index 0000000000..0484b6f81e --- /dev/null +++ b/src/lane_identification/software_package/utils.py @@ -0,0 +1,343 @@ +""" +工具函数模块 - 提供通用函数和工具类 +""" + +import cv2 +import numpy as np +import time +from typing import Tuple, List, Optional, Dict, Any +from collections import deque +from dataclasses import dataclass +from enum import Enum + + +class Timer: + """计时器类""" + + def __init__(self, name: str = ""): + self.name = name + self.start_time = 0 + self.end_time = 0 + self.total_time = 0 + self.call_count = 0 + + def start(self): + """开始计时""" + self.start_time = time.time() + return self + + def stop(self): + """停止计时并返回耗时""" + self.end_time = time.time() + elapsed = self.end_time - self.start_time + self.total_time += elapsed + self.call_count += 1 + return elapsed + + def avg_time(self) -> float: + """计算平均耗时""" + return self.total_time / self.call_count if self.call_count > 0 else 0 + + def __enter__(self): + self.start() + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + self.stop() + + +class PerformanceMonitor: + """性能监控器""" + + def __init__(self, max_samples: int = 100): + self.metrics: Dict[str, deque] = {} + self.max_samples = max_samples + self.timers: Dict[str, Timer] = {} + + def start_timer(self, name: str) -> Timer: + """开始计时器""" + if name not in self.timers: + self.timers[name] = Timer(name) + return self.timers[name].start() + + def stop_timer(self, name: str) -> float: + """停止计时器并返回耗时""" + if name in self.timers: + elapsed = self.timers[name].stop() + self.record_metric(f"{name}_time", elapsed) + return elapsed + return 0.0 + + def record_metric(self, metric_name: str, value: float): + """记录性能指标""" + if metric_name not in self.metrics: + self.metrics[metric_name] = deque(maxlen=self.max_samples) + self.metrics[metric_name].append(value) + + def get_statistics(self, metric_name: str) -> Dict[str, float]: + """获取统计信息""" + if metric_name not in self.metrics or not self.metrics[metric_name]: + return {'avg': 0, 'min': 0, 'max': 0, 'std': 0, 'p95': 0} + + values = list(self.metrics[metric_name]) + avg_val = np.mean(values) + min_val = np.min(values) + max_val = np.max(values) + std_val = np.std(values) + p95_val = np.percentile(values, 95) if len(values) >= 2 else avg_val + + return { + 'avg': avg_val, + 'min': min_val, + 'max': max_val, + 'std': std_val, + 'p95': p95_val, + 'count': len(values) + } + + def get_summary(self) -> Dict[str, Dict[str, float]]: + """获取性能摘要""" + summary = {} + for metric in self.metrics: + summary[metric] = self.get_statistics(metric) + return summary + + def reset(self): + """重置监控器""" + self.metrics.clear() + self.timers.clear() + + +def safe_resize(image: np.ndarray, target_size: Tuple[int, int], + keep_aspect_ratio: bool = True) -> np.ndarray: + """安全的图像缩放""" + if image is None: + return None + + if image.shape[:2] == target_size: + return image.copy() + + if keep_aspect_ratio: + h, w = image.shape[:2] + target_w, target_h = target_size + + # 计算缩放比例 + scale = min(target_w / w, target_h / h) + new_w = int(w * scale) + new_h = int(h * scale) + + # 缩放图像 + resized = cv2.resize(image, (new_w, new_h), interpolation=cv2.INTER_AREA) + + # 如果尺寸不匹配,填充到目标尺寸 + if new_w != target_w or new_h != target_h: + padded = np.zeros((target_h, target_w, 3) if len(resized.shape) == 3 else (target_h, target_w), + dtype=resized.dtype) + y_offset = (target_h - new_h) // 2 + x_offset = (target_w - new_w) // 2 + + if len(resized.shape) == 3: + padded[y_offset:y_offset+new_h, x_offset:x_offset+new_w, :] = resized + else: + padded[y_offset:y_offset+new_h, x_offset:x_offset+new_w] = resized + + return padded + return resized + else: + return cv2.resize(image, target_size, interpolation=cv2.INTER_AREA) + + +def calculate_fps(frame_times: List[float], window: int = 10) -> float: + """计算FPS""" + if not frame_times or len(frame_times) < 2: + return 0.0 + + recent_times = frame_times[-window:] + avg_time = np.mean(recent_times) + return 1.0 / avg_time if avg_time > 0 else 0.0 + + +def create_transparent_overlay(base_image: np.ndarray, + overlay_image: np.ndarray, + alpha: float = 0.5, + position: Tuple[int, int] = (0, 0)) -> np.ndarray: + """创建透明覆盖层""" + if base_image is None or overlay_image is None: + return base_image + + # 创建副本 + result = base_image.copy() + + # 获取覆盖层尺寸和位置 + oh, ow = overlay_image.shape[:2] + bh, bw = base_image.shape[:2] + + x, y = position + x_end = min(x + ow, bw) + y_end = min(y + oh, bh) + + # 计算实际覆盖区域 + if x_end <= x or y_end <= y: + return result + + overlay_region = overlay_image[0:y_end-y, 0:x_end-x] + base_region = base_image[y:y_end, x:x_end] + + # 混合图像 + if len(base_region.shape) == 3 and len(overlay_region.shape) == 3: + blended = cv2.addWeighted(base_region, 1-alpha, overlay_region, alpha, 0) + else: + # 灰度图像处理 + if len(base_region.shape) == 2: + base_region = cv2.cvtColor(base_region, cv2.COLOR_GRAY2BGR) + if len(overlay_region.shape) == 2: + overlay_region = cv2.cvtColor(overlay_region, cv2.COLOR_GRAY2BGR) + blended = cv2.addWeighted(base_region, 1-alpha, overlay_region, alpha, 0) + + result[y:y_end, x:x_end] = blended + return result + + +def normalize_vector(v: np.ndarray, epsilon: float = 1e-10) -> np.ndarray: + """向量归一化""" + norm = np.linalg.norm(v) + return v / (norm + epsilon) + + +def calculate_angle_between_vectors(v1: np.ndarray, v2: np.ndarray) -> float: + """计算两个向量之间的角度(度)""" + norm1 = np.linalg.norm(v1) + norm2 = np.linalg.norm(v2) + + if norm1 == 0 or norm2 == 0: + return 0.0 + + dot_product = np.dot(v1, v2) + cos_angle = dot_product / (norm1 * norm2) + cos_angle = np.clip(cos_angle, -1.0, 1.0) + + return np.arccos(cos_angle) * 180 / np.pi + + +def calculate_iou(box1: Tuple[int, int, int, int], box2: Tuple[int, int, int, int]) -> float: + """计算两个矩形框的IoU""" + x1 = max(box1[0], box2[0]) + y1 = max(box1[1], box2[1]) + x2 = min(box1[2], box2[2]) + y2 = min(box1[3], box2[3]) + + if x2 <= x1 or y2 <= y1: + return 0.0 + + intersection = (x2 - x1) * (y2 - y1) + area1 = (box1[2] - box1[0]) * (box1[3] - box1[1]) + area2 = (box2[2] - box2[0]) * (box2[3] - box2[1]) + union = area1 + area2 - intersection + + return intersection / union if union > 0 else 0.0 + + +def adaptive_threshold(image: np.ndarray, block_size: int = 11, c: int = 2) -> np.ndarray: + """自适应阈值处理""" + if len(image.shape) == 3: + gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) + else: + gray = image.copy() + + return cv2.adaptiveThreshold( + gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, + cv2.THRESH_BINARY, block_size, c + ) + + +def smooth_polynomial(coeffs_history: List[np.ndarray], smoothing_factor: float = 0.7) -> np.ndarray: + """平滑多项式系数""" + if not coeffs_history: + return np.array([]) + + weights = np.array([smoothing_factor ** i for i in range(len(coeffs_history))]) + weights = weights / weights.sum() + + smoothed = np.zeros_like(coeffs_history[0]) + for i, coeffs in enumerate(coeffs_history): + smoothed += coeffs * weights[len(coeffs_history) - i - 1] + + return smoothed + + +def create_gaussian_kernel(size: int, sigma: float) -> np.ndarray: + """创建高斯核""" + kernel = np.zeros((size, size)) + center = size // 2 + + for i in range(size): + for j in range(size): + x, y = i - center, j - center + kernel[i, j] = np.exp(-(x**2 + y**2) / (2 * sigma**2)) + + return kernel / kernel.sum() + + +def draw_text_with_background(image: np.ndarray, text: str, position: Tuple[int, int], + font_scale: float = 1.0, color: Tuple[int, int, int] = (255, 255, 255), + bg_color: Tuple[int, int, int] = (0, 0, 0, 128), + thickness: int = 2, padding: int = 5) -> np.ndarray: + """在图像上绘制带背景的文字""" + if image is None: + return None + + result = image.copy() + font = cv2.FONT_HERSHEY_SIMPLEX + + # 获取文字尺寸 + (text_width, text_height), baseline = cv2.getTextSize( + text, font, font_scale, thickness + ) + + # 计算背景矩形位置 + x, y = position + bg_top_left = (x - padding, y - text_height - padding) + bg_bottom_right = (x + text_width + padding, y + padding) + + # 绘制背景 + cv2.rectangle(result, bg_top_left, bg_bottom_right, bg_color, -1) + + # 绘制文字 + cv2.putText(result, text, (x, y - baseline), font, font_scale, color, thickness) + + return result + + +def calculate_histogram_similarity(hist1: np.ndarray, hist2: np.ndarray, + method: int = cv2.HISTCMP_CORREL) -> float: + """计算直方图相似度""" + if hist1 is None or hist2 is None: + return 0.0 + + # 归一化直方图 + hist1_norm = cv2.normalize(hist1, hist1).flatten() + hist2_norm = cv2.normalize(hist2, hist2).flatten() + + return cv2.compareHist(hist1_norm, hist2_norm, method) + + +@dataclass +class KalmanFilter1D: + """一维卡尔曼滤波器""" + process_variance: float = 1e-5 + measurement_variance: float = 1e-1 + estimate: float = 0.0 + error_covariance: float = 1.0 + + def update(self, measurement: float) -> float: + """更新滤波器""" + # 预测 + prediction = self.estimate + error_covariance_pred = self.error_covariance + self.process_variance + + # 更新 + kalman_gain = error_covariance_pred / (error_covariance_pred + self.measurement_variance) + self.estimate = prediction + kalman_gain * (measurement - prediction) + self.error_covariance = (1 - kalman_gain) * error_covariance_pred + + return self.estimate \ No newline at end of file diff --git a/src/lane_identification/software_package/video_processor.py b/src/lane_identification/software_package/video_processor.py index dedac1b700..a614b72d48 100644 --- a/src/lane_identification/software_package/video_processor.py +++ b/src/lane_identification/software_package/video_processor.py @@ -1,15 +1,19 @@ """ 视频处理模块 - 负责视频和摄像头处理 -修复摄像头调用问题,移除日志功能 +添加帧缓冲和自适应跳帧优化 """ import cv2 import numpy as np import threading import time +from collections import deque +from typing import Optional, Callable +import sys + class VideoProcessor: - """视频处理器""" + """视频处理器 - 优化版本""" def __init__(self, config): self.config = config @@ -18,13 +22,32 @@ def __init__(self, config): self.is_paused = False self.current_frame = None self.frame_count = 0 - self.fps = 0 + self.fps = config.video_fps self.frame_skip = config.video_frame_skip self.processor_thread = None + # 帧缓冲(新增) + self.frame_buffer = deque(maxlen=config.frame_buffer_size if hasattr(config, 'frame_buffer_size') else 5) + self.buffer_lock = threading.Lock() + self.frame_producer_thread = None + self.stop_producer = threading.Event() + + # 自适应跳帧(新增) + self.adaptive_skip_frames = getattr(config, 'adaptive_skip_frames', True) + self.max_processing_time = getattr(config, 'max_processing_time', 0.1) + self.min_fps = getattr(config, 'min_fps', 5) + self.last_processing_time = 0 + self.adaptive_frame_skip = 1 + # 性能统计 self.frame_times = [] self.processing_times = [] + self.frame_drop_count = 0 + self.total_frames_processed = 0 + + # 错误处理 + self.error_count = 0 + self.max_retries = 3 def open_video_file(self, video_path): """打开视频文件""" @@ -40,6 +63,9 @@ def open_video_file(self, video_path): if self.fps <= 0: self.fps = self.config.video_fps + # 重置状态 + self.reset_stats() + print(f"视频已打开: {video_path}") print(f"帧数: {self.frame_count}, FPS: {self.fps}") @@ -67,7 +93,6 @@ def open_camera(self, camera_id=None): (cam_id, cv2.CAP_DSHOW), # Windows DirectShow (cam_id, cv2.CAP_MSMF), # Windows Media Foundation (cam_id, cv2.CAP_ANY), # 自动选择 - (cam_id, cv2.CAP_V4L2), # Linux Video4Linux2 ] for backend in backends_to_try: @@ -86,6 +111,9 @@ def open_camera(self, camera_id=None): self.fps = actual_fps if actual_fps > 0 else 30 + # 重置状态 + self.reset_stats() + print(f"分辨率: {actual_width}x{actual_height}") print(f"FPS: {self.fps}") @@ -121,10 +149,20 @@ def start_processing(self, callback): self.is_playing = True self.is_paused = False + self.stop_producer.clear() + + # 如果启用了帧缓冲,启动生产者线程 + if getattr(self.config, 'enable_frame_buffer', True): + self.frame_producer_thread = threading.Thread( + target=self._frame_producer, + daemon=True + ) + self.frame_producer_thread.start() + print("帧缓冲生产者线程已启动") # 启动处理线程 self.processor_thread = threading.Thread( - target=self._process_frames, + target=self._process_frames_optimized, args=(callback,), daemon=True ) @@ -133,32 +171,74 @@ def start_processing(self, callback): print("视频处理已开始") return True - def _process_frames(self, callback): - """处理视频帧""" + def _frame_producer(self): + """帧生产者线程 - 填充帧缓冲""" + while self.is_playing and not self.stop_producer.is_set(): + if self.is_paused: + time.sleep(0.1) + continue + + # 检查缓冲是否已满 + with self.buffer_lock: + if len(self.frame_buffer) >= self.frame_buffer.maxlen: + time.sleep(0.01) + continue + + # 读取帧 + ret, frame = self.video_capture.read() + if not ret: + # 视频结束,等待一段时间后重试 + time.sleep(0.5) + if not self.camera_mode: + break + continue + + # 添加到缓冲 + with self.buffer_lock: + self.frame_buffer.append(frame) + + def _process_frames_optimized(self, callback): + """优化版的视频帧处理""" frame_number = 0 - skip_counter = 0 last_time = time.time() + skip_counter = 0 while self.is_playing and self.video_capture is not None: if self.is_paused: time.sleep(0.1) continue - # 跳过一些帧以提高性能 + # 更新自适应跳帧 + self._update_adaptive_skip() + + # 自适应跳帧逻辑 skip_counter += 1 - if skip_counter < self.frame_skip: - # 仍然需要读取帧以保持进度 - self.video_capture.grab() + if skip_counter < self.adaptive_frame_skip: + # 跳过帧,但保持进度 + if not self.camera_mode: + self.video_capture.grab() continue skip_counter = 0 - # 读取帧 - ret, frame = self.video_capture.read() - if not ret: - print("视频结束或读取失败") - break + # 获取帧 + frame = None + if getattr(self.config, 'enable_frame_buffer', True) and self.frame_buffer: + with self.buffer_lock: + if self.frame_buffer: + frame = self.frame_buffer.popleft() + else: + # 直接读取 + ret, frame = self.video_capture.read() + if not ret: + print("视频结束或读取失败") + break + + if frame is None: + time.sleep(0.01) + continue frame_number += 1 + self.total_frames_processed += 1 # 记录开始时间 start_time = time.time() @@ -167,7 +247,9 @@ def _process_frames(self, callback): frame_info = { 'frame_number': frame_number, 'frame_time': start_time, - 'fps': self.fps + 'fps': self.fps, + 'frame_skip': self.adaptive_frame_skip, + 'buffer_size': len(self.frame_buffer) } # 调用回调函数处理帧 @@ -175,9 +257,16 @@ def _process_frames(self, callback): callback(frame, frame_info) except Exception as e: print(f"帧处理回调失败: {e}") + self.error_count += 1 + if self.error_count > self.max_retries: + print("错误次数过多,停止处理") + break # 计算处理时间 processing_time = time.time() - start_time + self.last_processing_time = processing_time + + # 记录处理时间 self.processing_times.append(processing_time) if len(self.processing_times) > 10: self.processing_times.pop(0) @@ -185,14 +274,45 @@ def _process_frames(self, callback): # 计算实际FPS current_time = time.time() if current_time - last_time >= 1.0: - self.fps = frame_number / (current_time - last_time) + processed_frames = frame_number + if processed_frames > 0: + actual_fps = processed_frames / (current_time - last_time) + self.fps = actual_fps + print(f"实际FPS: {actual_fps:.1f}, 跳帧: {self.adaptive_frame_skip}, " + f"缓冲: {len(self.frame_buffer)}/{self.frame_buffer.maxlen}") last_time = current_time frame_number = 0 - # 控制处理速度 - target_delay = 1.0 / self.fps - if processing_time < target_delay: - time.sleep(target_delay - processing_time) + # 控制处理速度(仅限视频文件) + if not self.camera_mode: + target_delay = 1.0 / self.fps + if processing_time < target_delay: + sleep_time = target_delay - processing_time + if sleep_time > 0.001: # 只休眠有意义的时间 + time.sleep(sleep_time) + + def _update_adaptive_skip(self): + """更新自适应跳帧参数""" + if not self.adaptive_skip_frames: + self.adaptive_frame_skip = self.frame_skip + return + + # 基于最近的处理时间调整跳帧 + if len(self.processing_times) >= 3: + avg_processing_time = np.mean(self.processing_times[-3:]) + + if avg_processing_time > self.max_processing_time: + # 处理时间过长,增加跳帧 + new_skip = min(10, self.adaptive_frame_skip + 1) + if new_skip != self.adaptive_frame_skip: + self.adaptive_frame_skip = new_skip + print(f"增加跳帧到 {self.adaptive_frame_skip} (处理时间: {avg_processing_time:.3f}s)") + elif avg_processing_time < self.max_processing_time * 0.5: + # 处理时间充足,减少跳帧 + new_skip = max(1, self.adaptive_frame_skip - 1) + if new_skip != self.adaptive_frame_skip: + self.adaptive_frame_skip = new_skip + print(f"减少跳帧到 {self.adaptive_frame_skip} (处理时间: {avg_processing_time:.3f}s)") def pause(self): """暂停视频处理""" @@ -208,10 +328,15 @@ def stop(self): """停止视频处理""" self.is_playing = False self.is_paused = False + self.stop_producer.set() + # 等待线程结束 if self.processor_thread and self.processor_thread.is_alive(): self.processor_thread.join(timeout=2.0) + if self.frame_producer_thread and self.frame_producer_thread.is_alive(): + self.frame_producer_thread.join(timeout=1.0) + print("视频处理已停止") def release(self): @@ -222,6 +347,10 @@ def release(self): self.video_capture.release() self.video_capture = None + # 清空缓冲 + with self.buffer_lock: + self.frame_buffer.clear() + print("视频资源已释放") def get_frame(self): @@ -247,10 +376,33 @@ def get_video_info(self): 'frame_height': int(self.video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT)), 'frame_count': self.frame_count, 'is_playing': self.is_playing, - 'is_paused': self.is_paused + 'is_paused': self.is_paused, + 'adaptive_frame_skip': self.adaptive_frame_skip, + 'buffer_usage': f"{len(self.frame_buffer)}/{self.frame_buffer.maxlen}", + 'avg_processing_time': np.mean(self.processing_times) if self.processing_times else 0, + 'frame_drop_rate': self.frame_drop_count / max(self.total_frames_processed, 1) } def set_frame_position(self, position): """设置帧位置""" if self.video_capture is not None: - self.video_capture.set(cv2.CAP_PROP_POS_FRAMES, position) \ No newline at end of file + self.video_capture.set(cv2.CAP_PROP_POS_FRAMES, position) + # 清空缓冲 + with self.buffer_lock: + self.frame_buffer.clear() + + def reset_stats(self): + """重置统计信息""" + self.frame_times.clear() + self.processing_times.clear() + self.frame_drop_count = 0 + self.total_frames_processed = 0 + self.error_count = 0 + with self.buffer_lock: + self.frame_buffer.clear() + + @property + def camera_mode(self): + """是否处于摄像头模式""" + return self.video_capture is not None and not hasattr(self, '_is_file') or \ + (hasattr(self, '_is_file') and not self._is_file) \ No newline at end of file