diff --git a/src/lane_identification/software_package/config.py b/src/lane_identification/software_package/config.py index 521d7aff41..bcdbf12b58 100644 --- a/src/lane_identification/software_package/config.py +++ b/src/lane_identification/software_package/config.py @@ -31,15 +31,20 @@ class AppConfig: # 方向分析参数 deviation_threshold: float = 0.15 width_ratio_threshold: float = 0.7 - confidence_threshold: float = 0.6 + confidence_threshold: float = 0.5 + min_confidence_for_direction: float = 0.25 # 路径预测参数 prediction_steps: int = 10 prediction_distance: float = 0.75 min_prediction_points: int = 4 + # 视频处理参数 + video_frame_skip: int = 1 + video_fps: int = 10 + camera_id: int = 0 + # 置信度参数 - min_confidence_for_direction: float = 0.4 confidence_smoothing_factor: float = 0.7 quality_weight_lane: float = 0.5 quality_weight_road: float = 0.3 @@ -69,7 +74,10 @@ def to_dict(self) -> dict: 'hough_threshold': self.hough_threshold, 'confidence_threshold': self.confidence_threshold, 'prediction_steps': self.prediction_steps, - 'prediction_distance': self.prediction_distance + 'prediction_distance': self.prediction_distance, + 'video_frame_skip': self.video_frame_skip, + 'video_fps': self.video_fps, + 'camera_id': self.camera_id } def save(self, filepath: str): @@ -104,7 +112,8 @@ class SceneConfig: canny_threshold2=180, hough_threshold=35, prediction_distance=0.9, - confidence_threshold=0.7 + confidence_threshold=0.7, + video_fps=15 ) # 城市道路配置 @@ -114,7 +123,8 @@ class SceneConfig: canny_threshold2=120, hough_threshold=25, prediction_distance=0.6, - confidence_threshold=0.5 + confidence_threshold=0.5, + video_fps=10 ) # 乡村道路配置 @@ -126,7 +136,8 @@ class SceneConfig: hough_threshold=20, min_contour_area=0.002, prediction_distance=0.7, - confidence_threshold=0.4 + confidence_threshold=0.4, + video_fps=8 ) @classmethod diff --git a/src/lane_identification/software_package/direction_analyzer.py b/src/lane_identification/software_package/direction_analyzer.py index fbe0052516..18d2c79fbe 100644 --- a/src/lane_identification/software_package/direction_analyzer.py +++ b/src/lane_identification/software_package/direction_analyzer.py @@ -1,6 +1,5 @@ """ -方向分析模块 - 负责分析道路方向并计算置信度 -稳定优化版 +方向分析模块 - 稳定优化版 """ import numpy as np @@ -15,7 +14,7 @@ def __init__(self, config=None): if config is None: # 创建默认配置 class DefaultConfig: - min_confidence_for_direction = 0.25 # 降低阈值以提高判断率 + min_confidence_for_direction = 0.25 confidence_threshold = 0.5 self.config = DefaultConfig() @@ -40,16 +39,11 @@ class DefaultConfig: def analyze(self, road_features: Dict[str, Any], lane_info: Dict[str, Any]) -> Dict[str, Any]: """分析道路方向 - 主方法""" try: - # 调试信息 - # print(f"道路特征可用: {list(road_features.keys())}") - # print(f"车道信息可用: {list(lane_info.keys())}") - # 提取特征 features = self._extract_features(road_features, lane_info) # 如果没有足够特征,使用回退策略 if len(features) < 2: - # print("特征不足,使用回退策略") return self._fallback_direction_analysis(road_features, lane_info) # 方向预测 @@ -77,20 +71,15 @@ def analyze(self, road_features: Dict[str, Any], lane_info: Dict[str, Any]) -> D } # 更新历史 - if confidence > 0.2: # 降低历史记录门槛 + if confidence > 0.2: self.history.append(result) self.direction_history.append(final_direction) self.confidence_history.append(confidence) - # 调试信息 - # print(f"方向分析结果: {final_direction}, 置信度: {confidence:.2f}") - return result except Exception as e: print(f"方向分析失败: {e}") - import traceback - traceback.print_exc() return self._create_default_result() def _extract_features(self, road_features: Dict[str, Any], lane_info: Dict[str, Any]) -> Dict[str, float]: @@ -101,7 +90,6 @@ def _extract_features(self, road_features: Dict[str, Any], lane_info: Dict[str, if 'centroid' in road_features and road_features['centroid'] is not None: try: cx, cy = road_features['centroid'] - # 假设图像宽度为800,进行归一化 centroid_offset = (cx - 400) / 400 features['centroid_offset'] = max(-1.0, min(1.0, centroid_offset)) except: @@ -133,10 +121,6 @@ def _extract_features(self, road_features: Dict[str, Any], lane_info: Dict[str, balance = self._calculate_lane_balance(left_lane, right_lane) features['lane_balance'] = balance - # 车道宽度 - width = self._calculate_lane_width_safe(left_lane, right_lane) - features['lane_width'] = width - except Exception as e: print(f"车道特征提取失败: {e}") @@ -148,9 +132,6 @@ def _extract_features(self, road_features: Dict[str, Any], lane_info: Dict[str, if len(path_points) >= 3: curvature = self._calculate_path_curvature_simple(path_points) features['path_curvature'] = curvature - - straightness = self._calculate_path_straightness_simple(path_points) - features['path_straightness'] = straightness except Exception as e: print(f"路径特征提取失败: {e}") @@ -172,9 +153,8 @@ def _calculate_lane_convergence_safe(self, left_lane: Dict[str, Any], right_lane right_func = right_lane.get('func') if left_func and right_func: - # 在顶部和底部计算宽度 - y_bottom = 600 # 图像底部 - y_top = 240 # 图像顶部 (600*0.4) + y_bottom = 600 + y_top = 240 try: left_bottom = float(left_func(y_bottom)) @@ -187,14 +167,13 @@ def _calculate_lane_convergence_safe(self, left_lane: Dict[str, Any], right_lane if width_bottom > 0: convergence = width_top / width_bottom - # 限制在合理范围内 return max(0.3, min(3.0, convergence)) except: pass except: pass - return 1.0 # 默认值 + return 1.0 def _calculate_lane_symmetry_safe(self, left_lane: Dict[str, Any], right_lane: Dict[str, Any]) -> float: """安全计算车道对称性""" @@ -203,14 +182,12 @@ def _calculate_lane_symmetry_safe(self, left_lane: Dict[str, Any], right_lane: D right_func = right_lane.get('func') if left_func and right_func: - # 在图像中间采样 - y = 450 # 600 * 0.75 + y = 450 try: left_x = float(left_func(y)) right_x = float(right_func(y)) - # 假设图像中心在400 center = 400 left_dist = center - left_x @@ -224,7 +201,7 @@ def _calculate_lane_symmetry_safe(self, left_lane: Dict[str, Any], right_lane: D except: pass - return 0.5 # 默认值 + return 0.5 def _calculate_lane_balance(self, left_lane: Dict[str, Any], right_lane: Dict[str, Any]) -> float: """计算车道平衡性""" @@ -233,21 +210,17 @@ def _calculate_lane_balance(self, left_lane: Dict[str, Any], right_lane: Dict[st right_func = right_lane.get('func') if left_func and right_func: - # 在图像下半部分采样 - y = 525 # 600 * 0.875 + y = 525 try: left_x = float(left_func(y)) right_x = float(right_func(y)) - # 计算车道中心 lane_center = (left_x + right_x) / 2 - image_center = 400 # 假设图像宽度800,中心在400 + image_center = 400 - # 计算偏移比例 - offset_ratio = (lane_center - image_center) / 200 # 归一化到[-2, 2] + offset_ratio = (lane_center - image_center) / 200 - # 转换为平衡分数(1表示居中,0表示严重偏移) balance = 1 - min(1.0, abs(offset_ratio)) return max(0, balance) except: @@ -255,33 +228,7 @@ def _calculate_lane_balance(self, left_lane: Dict[str, Any], right_lane: Dict[st except: pass - return 0.5 # 默认值 - - def _calculate_lane_width_safe(self, left_lane: Dict[str, Any], right_lane: Dict[str, Any]) -> float: - """安全计算车道宽度""" - try: - left_func = left_lane.get('func') - right_func = right_lane.get('func') - - if left_func and right_func: - # 在图像底部采样 - y = 600 - - try: - left_x = float(left_func(y)) - right_x = float(right_func(y)) - - if right_x > left_x: - width = right_x - left_x - # 归一化到0-1范围,假设合理宽度在200-400之间 - normalized = (width - 200) / 200 - return max(0, min(1, normalized)) - except: - pass - except: - pass - - return 0.5 # 默认值 + return 0.5 def _calculate_path_curvature_simple(self, path_points: List[Tuple[int, int]]) -> float: """简单计算路径曲率""" @@ -289,16 +236,13 @@ def _calculate_path_curvature_simple(self, path_points: List[Tuple[int, int]]) - return 0.0 try: - # 使用前中后三个点计算曲率 p1 = np.array(path_points[0]) p2 = np.array(path_points[len(path_points)//2]) p3 = np.array(path_points[-1]) - # 计算向量 v1 = p2 - p1 v2 = p3 - p2 - # 计算夹角 norm1 = np.linalg.norm(v1) norm2 = np.linalg.norm(v2) @@ -307,38 +251,12 @@ def _calculate_path_curvature_simple(self, path_points: List[Tuple[int, int]]) - cos_angle = max(-1, min(1, cos_angle)) angle = np.arccos(cos_angle) - # 归一化到0-1范围 return min(1.0, angle / (np.pi / 3)) except: pass return 0.0 - def _calculate_path_straightness_simple(self, path_points: List[Tuple[int, int]]) -> float: - """简单计算路径直线度""" - if len(path_points) < 2: - return 1.0 - - try: - # 计算首尾距离与总路径长度的比例 - total_length = 0 - for i in range(len(path_points) - 1): - x1, y1 = path_points[i] - x2, y2 = path_points[i+1] - total_length += np.sqrt((x2-x1)**2 + (y2-y1)**2) - - start_x, start_y = path_points[0] - end_x, end_y = path_points[-1] - direct_length = np.sqrt((end_x-start_x)**2 + (end_y-start_y)**2) - - if total_length > 0: - straightness = direct_length / total_length - return max(0, min(1, straightness)) - except: - pass - - return 0.5 - def _calculate_historical_consistency(self) -> float: """计算历史一致性""" if len(self.direction_history) < 2: @@ -346,7 +264,6 @@ def _calculate_historical_consistency(self) -> float: recent_directions = list(self.direction_history) - # 统计方向频率 direction_counts = defaultdict(int) for direction in recent_directions: direction_counts[direction] += 1 @@ -358,41 +275,39 @@ def _calculate_historical_consistency(self) -> float: def _predict_direction_improved(self, features: Dict[str, float]) -> Dict[str, float]: """改进的方向预测""" - # 基础概率 probabilities = {'直行': 0.4, '左转': 0.3, '右转': 0.3} # 1. 基于道路质心偏移 if 'centroid_offset' in features: offset = features['centroid_offset'] - if abs(offset) < 0.15: # 基本居中 + if abs(offset) < 0.15: probabilities['直行'] += 0.25 - elif offset < -0.15: # 偏左 + elif offset < -0.15: probabilities['左转'] += abs(offset) * 0.8 - elif offset > 0.15: # 偏右 + elif offset > 0.15: probabilities['右转'] += offset * 0.8 # 2. 基于车道收敛度 if 'lane_convergence' in features: convergence = features['lane_convergence'] - if convergence < 0.7: # 明显收敛 + if convergence < 0.7: probabilities['左转'] += (0.7 - convergence) * 0.4 probabilities['右转'] += (0.7 - convergence) * 0.4 probabilities['直行'] -= 0.2 - elif convergence > 1.3: # 明显发散 + elif convergence > 1.3: probabilities['直行'] += 0.2 - elif 0.9 <= convergence <= 1.1: # 基本平行 + elif 0.9 <= convergence <= 1.1: probabilities['直行'] += 0.15 # 3. 基于车道对称性 if 'lane_symmetry' in features: symmetry = features['lane_symmetry'] - if symmetry > 0.7: # 对称性好 + if symmetry > 0.7: probabilities['直行'] += 0.15 - elif symmetry < 0.4: # 对称性差 - # 结合质心偏移判断 + elif symmetry < 0.4: if 'centroid_offset' in features: offset = features['centroid_offset'] if offset < -0.1: @@ -404,28 +319,28 @@ def _predict_direction_improved(self, features: Dict[str, float]) -> Dict[str, f if 'lane_balance' in features: balance = features['lane_balance'] - if balance < 0.4: # 严重不平衡 + if balance < 0.4: if 'centroid_offset' in features: offset = features['centroid_offset'] if offset < -0.1: probabilities['左转'] += 0.2 elif offset > 0.1: probabilities['右转'] += 0.2 - elif balance > 0.7: # 平衡性好 + elif balance > 0.7: probabilities['直行'] += 0.1 # 5. 基于路径曲率 if 'path_curvature' in features: curvature = features['path_curvature'] - if curvature > 0.3: # 明显弯曲 + if curvature > 0.3: if 'centroid_offset' in features: offset = features['centroid_offset'] if offset < 0: probabilities['左转'] += curvature * 0.5 else: probabilities['右转'] += curvature * 0.5 - elif curvature < 0.1: # 基本直线 + elif curvature < 0.1: probabilities['直行'] += 0.15 # 确保概率非负 @@ -438,12 +353,6 @@ def _predict_direction_improved(self, features: Dict[str, float]) -> Dict[str, f for direction in probabilities: probabilities[direction] /= total - # 确保总和为1(浮点误差) - total = sum(probabilities.values()) - if total > 0: - for direction in probabilities: - probabilities[direction] /= total - return probabilities def _calculate_confidence_improved(self, features: Dict[str, float], @@ -466,7 +375,6 @@ def _calculate_confidence_improved(self, features: Dict[str, float], # 2. 特征质量 feature_scores = [] - # 检查特征是否明显 if 'centroid_offset' in features: offset = abs(features['centroid_offset']) if offset > 0.2: @@ -512,13 +420,12 @@ def _calculate_confidence_improved(self, features: Dict[str, float], if confidence_factors: confidence = np.mean(confidence_factors) - # 非线性调整 if confidence < 0.3: - confidence = confidence * 0.9 # 稍微降低低置信度 + confidence = confidence * 0.9 elif confidence < 0.6: - confidence = 0.3 + (confidence - 0.3) * 1.3 # 增强中等置信度 + confidence = 0.3 + (confidence - 0.3) * 1.3 else: - confidence = 0.6 + (confidence - 0.6) * 1.1 # 稍微增强高置信度 + confidence = 0.6 + (confidence - 0.6) * 1.1 return min(max(confidence, 0.0), 1.0) else: @@ -530,13 +437,11 @@ def _evaluate_feature_consistency(self, features: Dict[str, float], if not features: return 0.5 - # 确定优势方向 best_direction = max(probabilities.items(), key=lambda x: x[1])[0] consistency_scores = [] if best_direction == '直行': - # 直行特征:居中、平行、对称 if 'centroid_offset' in features: offset = abs(features['centroid_offset']) if offset < 0.1: @@ -556,7 +461,6 @@ def _evaluate_feature_consistency(self, features: Dict[str, float], consistency_scores.append(symmetry) elif best_direction == '左转': - # 左转特征:偏左、收敛、不对称 if 'centroid_offset' in features: offset = features['centroid_offset'] if offset < -0.1: @@ -579,7 +483,6 @@ def _evaluate_feature_consistency(self, features: Dict[str, float], consistency_scores.append(max(0, 1 - (symmetry - 0.6) / 0.4)) elif best_direction == '右转': - # 右转特征:偏右、收敛、不对称 if 'centroid_offset' in features: offset = features['centroid_offset'] if offset > 0.1: @@ -609,56 +512,51 @@ def _get_final_direction_improved(self, probabilities: Dict[str, float], sorted_probs = sorted(probabilities.items(), key=lambda x: x[1], reverse=True) if not sorted_probs: - return '直行' # 默认直行 + return '直行' best_direction, best_prob = sorted_probs[0] second_direction, second_prob = sorted_probs[1] if len(sorted_probs) > 1 else ('', 0) - # 获取最小置信度阈值 min_confidence = getattr(self.config, 'min_confidence_for_direction', 0.25) - # 决策逻辑(更宽松) - if confidence < 0.15: # 极低置信度 + if confidence < 0.15: return '未知' - elif confidence < min_confidence: # 低置信度 - # 如果概率优势明显,仍可判断 + elif confidence < min_confidence: if best_prob > 0.5 and best_prob > second_prob * 1.8: return best_direction - elif best_prob > 0.6: # 概率超过60% + elif best_prob > 0.6: return best_direction else: return '未知' - elif confidence < 0.5: # 中等置信度 + elif confidence < 0.5: if best_prob > second_prob * 1.3: return best_direction - elif best_prob > 0.4: # 概率超过40% + elif best_prob > 0.4: return best_direction else: - # 参考历史 if self.direction_history: historical_direction = self._get_historical_direction() if historical_direction != '未知': return historical_direction - return best_direction # 即使优势不明显,也返回最佳方向 - else: # 高置信度 + return best_direction + else: if best_prob > second_prob * 1.2: return best_direction - elif best_prob > 0.35: # 概率超过35% + elif best_prob > 0.35: return best_direction else: - # 参考历史 if self.direction_history: historical_direction = self._get_historical_direction() if historical_direction != '未知': return historical_direction - return best_direction # 总是返回最佳方向 + return best_direction def _get_historical_direction(self) -> str: """获取历史主要方向""" if not self.direction_history: return '未知' - recent = list(self.direction_history)[-3:] # 最近3次 + recent = list(self.direction_history)[-3:] direction_counts = defaultdict(int) for direction in recent: @@ -668,7 +566,6 @@ def _get_historical_direction(self) -> str: if not direction_counts: return '未知' - # 返回出现最频繁的方向 most_common = max(direction_counts.items(), key=lambda x: x[1]) return most_common[0] @@ -680,7 +577,6 @@ def _apply_historical_smoothing(self, direction: str, confidence: float) -> Tupl recent_directions = list(self.direction_history)[-4:] recent_confidences = list(self.confidence_history)[-4:] - # 统计历史方向 direction_counts = defaultdict(int) for d in recent_directions: if d != '未知': @@ -692,20 +588,16 @@ def _apply_historical_smoothing(self, direction: str, confidence: float) -> Tupl most_common, count = max(direction_counts.items(), key=lambda x: x[1]) frequency = count / len(recent_directions) - # 计算历史平均置信度 if recent_confidences: historical_confidence = np.mean(recent_confidences) else: historical_confidence = 0.5 - # 应用平滑规则 if frequency > 0.75 and confidence < 0.4: - # 历史一致性很高,当前置信度低,信任历史 smoothed_confidence = historical_confidence * 0.8 return most_common, smoothed_confidence elif most_common != direction and frequency > 0.6 and confidence < 0.5: - # 历史与当前不一致,但历史一致性高,当前置信度不高 smoothing_factor = min(0.6, frequency) smoothed_confidence = ( confidence * (1 - smoothing_factor) + @@ -723,7 +615,6 @@ def _generate_detailed_reasoning(self, features: Dict[str, float], """生成详细推理说明""" reasoning_parts = [] - # 添加关键特征说明 key_features = [] if 'centroid_offset' in features: @@ -758,7 +649,6 @@ def _generate_detailed_reasoning(self, features: Dict[str, float], if key_features: reasoning_parts.append("特征:" + ",".join(key_features)) - # 添加概率说明 sorted_probs = sorted(probabilities.items(), key=lambda x: x[1], reverse=True) if len(sorted_probs) >= 2: best_dir, best_prob = sorted_probs[0] @@ -771,7 +661,6 @@ def _generate_detailed_reasoning(self, features: Dict[str, float], else: reasoning_parts.append("方向接近") - # 添加置信度说明 if confidence >= 0.7: confidence_text = "高置信度" elif confidence >= 0.5: @@ -783,7 +672,6 @@ def _generate_detailed_reasoning(self, features: Dict[str, float], reasoning_parts.append(confidence_text) - # 添加最终决策说明 if final_direction != '未知': reasoning_parts.append(f"决策:{final_direction}") else: @@ -794,41 +682,37 @@ def _generate_detailed_reasoning(self, features: Dict[str, float], def _fallback_direction_analysis(self, road_features: Dict[str, Any], lane_info: Dict[str, Any]) -> Dict[str, Any]: """回退策略:当特征不足时使用""" - direction = '直行' # 默认 + direction = '直行' confidence = 0.3 - # 1. 基于道路质心 if 'centroid' in road_features and road_features['centroid'] is not None: cx, cy = road_features['centroid'] - if cx < 350: # 质心偏左 + if cx < 350: direction = '左转' confidence = 0.4 - elif cx > 450: # 质心偏右 + elif cx > 450: direction = '右转' confidence = 0.4 - # 2. 基于车道线 left_lane = lane_info.get('left_lane') right_lane = lane_info.get('right_lane') if left_lane and not right_lane: - direction = '右转' # 只能看到左车道线,可能右转 + direction = '右转' confidence = 0.35 elif right_lane and not left_lane: - direction = '左转' # 只能看到右车道线,可能左转 + direction = '左转' confidence = 0.35 - # 3. 基于检测质量 detection_quality = lane_info.get('detection_quality', 0.0) if detection_quality > 0.7: confidence = min(1.0, confidence * 1.3) - # 创建概率分布 if direction == '直行': probabilities = {'直行': 0.7, '左转': 0.15, '右转': 0.15} elif direction == '左转': probabilities = {'左转': 0.7, '直行': 0.15, '右转': 0.15} - else: # 右转 + else: probabilities = {'右转': 0.7, '直行': 0.15, '左转': 0.15} return { diff --git a/src/lane_identification/software_package/image_processor.py b/src/lane_identification/software_package/image_processor.py index 2cee6827ee..655c511583 100644 --- a/src/lane_identification/software_package/image_processor.py +++ b/src/lane_identification/software_package/image_processor.py @@ -1,5 +1,6 @@ """ 图像处理模块 - 负责图像加载、预处理和缓存 +支持图像、视频和摄像头输入 """ import cv2 @@ -51,6 +52,24 @@ def load_and_preprocess(self, image_path: str) -> Optional[Tuple[np.ndarray, Dic print(f"图像处理失败 {image_path}: {e}") return None + def preprocess_frame(self, frame: np.ndarray) -> Tuple[np.ndarray, Dict]: + """预处理视频帧""" + try: + # 智能调整尺寸 + processed = self._smart_resize(frame) + + # 自适应预处理 + processed = self._adaptive_preprocessing(processed) + + # 计算ROI区域 + roi_info = self._calculate_roi(processed.shape) + + return processed, roi_info + + except Exception as e: + print(f"帧预处理失败: {e}") + return frame, {} + def _smart_resize(self, image: np.ndarray) -> np.ndarray: """智能调整图像尺寸""" height, width = image.shape[:2] diff --git a/src/lane_identification/software_package/lane_detector.py b/src/lane_identification/software_package/lane_detector.py index c84870aa8f..3aef81db8f 100644 --- a/src/lane_identification/software_package/lane_detector.py +++ b/src/lane_identification/software_package/lane_detector.py @@ -163,8 +163,11 @@ def _fit_lane_model(self, lines: List[Dict], image_shape: Tuple[int, ...]) -> Op coeffs = np.polyfit(y_points, x_points, 2) model_type = 'quadratic' except: - coeffs = np.polyfit(y_points, x_points, 1) - model_type = 'linear' + try: + coeffs = np.polyfit(y_points, x_points, 1) + model_type = 'linear' + except: + return None poly_func = np.poly1d(coeffs) @@ -173,24 +176,27 @@ def _fit_lane_model(self, lines: List[Dict], image_shape: Tuple[int, ...]) -> Op y_bottom = height y_top = int(height * 0.4) - x_bottom = int(poly_func(y_bottom)) - x_top = int(poly_func(y_top)) - - # 限制范围 - x_bottom = max(0, min(width, x_bottom)) - x_top = max(0, min(width, x_top)) - - # 计算置信度 - confidence = min(len(lines) / 10.0, 1.0) - - return { - 'func': poly_func, - 'coeffs': coeffs.tolist(), - 'points': [(x_bottom, y_bottom), (x_top, y_top)], - 'model_type': model_type, - 'confidence': confidence, - 'num_lines': len(lines) - } + try: + x_bottom = int(poly_func(y_bottom)) + x_top = int(poly_func(y_top)) + + # 限制范围 + x_bottom = max(0, min(width, x_bottom)) + x_top = max(0, min(width, x_top)) + + # 计算置信度 + confidence = min(len(lines) / 10.0, 1.0) + + return { + 'func': poly_func, + 'coeffs': coeffs.tolist(), + 'points': [(x_bottom, y_bottom), (x_top, y_top)], + 'model_type': model_type, + 'confidence': confidence, + 'num_lines': len(lines) + } + except: + return None def _validate_lanes(self, left_lane: Optional[Dict], right_lane: Optional[Dict], image_shape: Tuple[int, ...]) -> Tuple[Optional[Dict], Optional[Dict]]: @@ -200,35 +206,38 @@ def _validate_lanes(self, left_lane: Optional[Dict], right_lane: Optional[Dict], height, width = image_shape[:2] - # 检查车道宽度 - left_func = left_lane['func'] - right_func = right_lane['func'] - - y_points = np.linspace(height * 0.4, height, 5) - widths = [] - - for y in y_points: - left_x = left_func(y) - right_x = right_func(y) - if right_x > left_x: - widths.append(right_x - left_x) - - if widths: - avg_width = np.mean(widths) - std_width = np.std(widths) + try: + # 检查车道宽度 + left_func = left_lane['func'] + right_func = right_lane['func'] - # 宽度合理性检查 - min_reasonable_width = width * 0.15 - max_reasonable_width = width * 0.8 + y_points = np.linspace(height * 0.4, height, 5) + widths = [] - if avg_width < min_reasonable_width or avg_width > max_reasonable_width or std_width > width * 0.2: - left_lane['confidence'] *= 0.7 - right_lane['confidence'] *= 0.7 - - # 检查车道线交叉 - if left_lane['points'][0][0] > right_lane['points'][0][0]: - left_lane['confidence'] *= 0.6 - right_lane['confidence'] *= 0.6 + for y in y_points: + left_x = left_func(y) + right_x = right_func(y) + if right_x > left_x: + widths.append(right_x - left_x) + + if widths: + avg_width = np.mean(widths) + std_width = np.std(widths) + + # 宽度合理性检查 + min_reasonable_width = width * 0.15 + max_reasonable_width = width * 0.8 + + if avg_width < min_reasonable_width or avg_width > max_reasonable_width or std_width > width * 0.2: + left_lane['confidence'] *= 0.7 + right_lane['confidence'] *= 0.7 + + # 检查车道线交叉 + if left_lane['points'][0][0] > right_lane['points'][0][0]: + left_lane['confidence'] *= 0.6 + right_lane['confidence'] *= 0.6 + except: + pass return left_lane, right_lane @@ -320,14 +329,14 @@ def _calculate_detection_quality(self, left_lane: Optional[Dict], right_lane: Op quality = 0.0 if left_lane is not None: - quality += left_lane['confidence'] * 0.5 + quality += left_lane.get('confidence', 0) * 0.5 if right_lane is not None: - quality += right_lane['confidence'] * 0.5 + quality += right_lane.get('confidence', 0) * 0.5 if left_lane is not None and right_lane is not None: quality += 0.1 - if left_lane['model_type'] == right_lane['model_type']: + if left_lane.get('model_type') == right_lane.get('model_type'): quality += 0.1 return min(quality, 1.0) @@ -341,9 +350,9 @@ def _apply_temporal_smoothing(self, current_result: Dict[str, Any]) -> Dict[str, smoothing_factor = 0.6 # 平滑车道线参数 - if current_result['left_lane']: + if current_result['left_lane'] and current_result['left_lane'].get('coeffs'): for prev_result in list(self.lane_history)[-2:]: - if prev_result['left_lane']: + if prev_result['left_lane'] and prev_result['left_lane'].get('coeffs'): prev_coeffs = np.array(prev_result['left_lane']['coeffs']) curr_coeffs = np.array(current_result['left_lane']['coeffs']) @@ -356,9 +365,9 @@ def _apply_temporal_smoothing(self, current_result: Dict[str, Any]) -> Dict[str, current_result['left_lane']['func'] = np.poly1d(smoothed_coeffs) # 同样处理右车道线 - if current_result['right_lane']: + if current_result['right_lane'] and current_result['right_lane'].get('coeffs'): for prev_result in list(self.lane_history)[-2:]: - if prev_result['right_lane']: + if prev_result['right_lane'] and prev_result['right_lane'].get('coeffs'): prev_coeffs = np.array(prev_result['right_lane']['coeffs']) curr_coeffs = np.array(current_result['right_lane']['coeffs']) diff --git a/src/lane_identification/software_package/main.py b/src/lane_identification/software_package/main.py index cdf44ee198..cd6ee20604 100644 --- a/src/lane_identification/software_package/main.py +++ b/src/lane_identification/software_package/main.py @@ -1,24 +1,27 @@ """ -主应用程序模块 - 负责界面和流程控制 +主应用程序模块 - 支持图像、视频和摄像头实时识别 """ import tkinter as tk from tkinter import filedialog, messagebox, ttk import threading import time -from typing import Dict, Any from collections import deque from PIL import Image, ImageTk import cv2 import numpy as np import os +# 类型注解导入 +from typing import Dict, Any, Optional, Tuple, List + # 导入各个模块 -from config import AppConfig +from config import AppConfig, SceneConfig 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 class LaneDetectionApp: """道路方向识别系统主应用程序""" @@ -36,25 +39,34 @@ def __init__(self, root): self.lane_detector = LaneDetector(self.config) self.direction_analyzer = DirectionAnalyzer(self.config) self.visualizer = Visualizer(self.config) + self.video_processor = VideoProcessor(self.config) # 状态变量 self.current_image = None self.current_image_path = None self.is_processing = False + self.is_video_mode = False self.processing_history = deque(maxlen=10) + # 视频相关变量 + self.video_file_path = None + self.camera_mode = False + # 性能统计 self.processing_times = [] + self.frame_counter = 0 + self.last_fps_update = time.time() + self.current_fps = 0 # 创建界面 self._create_ui() - print("道路方向识别系统已启动") + print("道路方向识别系统已启动(支持视频/摄像头)") def _setup_window(self): """设置窗口""" - self.root.title("道路方向识别系统") - self.root.geometry("1400x800") + self.root.title("道路方向识别系统 - 支持视频/摄像头") + self.root.geometry("1400x850") self.root.minsize(1200, 700) # 设置窗口居中 @@ -64,6 +76,9 @@ def _setup_window(self): x = (self.root.winfo_screenwidth() // 2) - (width // 2) y = (self.root.winfo_screenheight() // 2) - (height // 2) self.root.geometry(f'{width}x{height}+{x}+{y}') + + # 窗口关闭事件 + self.root.protocol("WM_DELETE_WINDOW", self._on_closing) def _create_ui(self): """创建用户界面""" @@ -97,20 +112,20 @@ def _create_title_bar(self, parent): # 标题 title_label = ttk.Label( title_frame, - text="道路方向识别系统", + text="道路方向识别系统(支持视频/摄像头)", font=("微软雅黑", 16, "bold"), foreground="#2c3e50" ) title_label.pack(side="left") - # 版本信息 - version_label = ttk.Label( + # 模式指示器 + self.mode_label = ttk.Label( title_frame, - text="v2.0", + text="[图像模式]", font=("微软雅黑", 10), - foreground="#7f8c8d" + foreground="#3498db" ) - version_label.pack(side="right") + self.mode_label.pack(side="right", padx=(0, 10)) def _create_control_panel(self, parent): """创建控制面板""" @@ -121,24 +136,56 @@ def _create_control_panel(self, parent): relief="groove" ) control_frame.pack_propagate(False) - control_frame.config(width=300) + control_frame.config(width=350) + + # 输入模式选择 + mode_frame = ttk.LabelFrame(control_frame, text="输入模式", padding="10") + mode_frame.pack(fill="x", pady=(0, 15)) + + # 模式选择按钮 + mode_buttons_frame = ttk.Frame(mode_frame) + mode_buttons_frame.pack() + + self.image_mode_btn = ttk.Button( + mode_buttons_frame, + text="图像模式", + command=self._switch_to_image_mode, + width=12 + ) + self.image_mode_btn.pack(side="left", padx=(0, 5)) + + self.video_mode_btn = ttk.Button( + mode_buttons_frame, + text="视频模式", + command=self._switch_to_video_mode, + width=12 + ) + self.video_mode_btn.pack(side="left", padx=(0, 5)) + + self.camera_mode_btn = ttk.Button( + mode_buttons_frame, + text="摄像头模式", + command=self._switch_to_camera_mode, + width=12 + ) + self.camera_mode_btn.pack(side="left") # 文件操作区域 - file_frame = ttk.LabelFrame(control_frame, text="文件操作", padding="10") - file_frame.pack(fill="x", pady=(0, 15)) + self.file_frame = ttk.LabelFrame(control_frame, text="文件操作", padding="10") + self.file_frame.pack(fill="x", pady=(0, 15)) # 选择图片按钮 - select_btn = ttk.Button( - file_frame, + self.select_image_btn = ttk.Button( + self.file_frame, text="选择图片", command=self._select_image, width=20 ) - select_btn.pack(pady=(0, 10)) + self.select_image_btn.pack(pady=(0, 10)) # 重新检测按钮 self.redetect_btn = ttk.Button( - file_frame, + self.file_frame, text="重新检测", command=self._redetect, width=20, @@ -146,11 +193,54 @@ def _create_control_panel(self, parent): ) self.redetect_btn.pack(pady=(0, 10)) + # 视频控制区域(初始隐藏) + self.video_frame = ttk.LabelFrame(control_frame, text="视频控制", padding="10") + + # 选择视频按钮 + self.select_video_btn = ttk.Button( + self.video_frame, + text="选择视频文件", + command=self._select_video, + width=20 + ) + self.select_video_btn.pack(pady=(0, 10)) + + # 视频控制按钮 + self.video_control_frame = ttk.Frame(self.video_frame) + self.video_control_frame.pack() + + self.play_btn = ttk.Button( + self.video_control_frame, + text="开始", + command=self._play_video, + width=8, + state="disabled" + ) + self.play_btn.pack(side="left", padx=(0, 5)) + + self.pause_btn = ttk.Button( + self.video_control_frame, + text="暂停", + command=self._pause_video, + width=8, + state="disabled" + ) + self.pause_btn.pack(side="left", padx=(0, 5)) + + self.stop_btn = ttk.Button( + self.video_control_frame, + text="停止", + command=self._stop_video, + width=8, + state="disabled" + ) + self.stop_btn.pack(side="left") + # 文件信息显示 self.file_info_label = ttk.Label( - file_frame, + self.file_frame, text="未选择图片", - wraplength=250, + wraplength=300, foreground="#3498db" ) self.file_info_label.pack() @@ -169,10 +259,23 @@ def _create_control_panel(self, parent): variable=self.sensitivity_var, orient="horizontal", command=self._on_parameter_change, - length=250 + length=300 ) sensitivity_scale.pack(fill="x", pady=(0, 10)) + # 场景选择 + ttk.Label(param_frame, text="场景模式:").pack(anchor="w", pady=(0, 5)) + self.scene_var = tk.StringVar(value="auto") + scene_combo = ttk.Combobox( + param_frame, + textvariable=self.scene_var, + values=["自动", "高速公路", "城市道路", "乡村道路"], + state="readonly", + width=20 + ) + scene_combo.pack(fill="x", pady=(0, 10)) + scene_combo.bind("<>", self._on_scene_change) + # 结果显示区域 result_frame = ttk.LabelFrame(control_frame, text="检测结果", padding="10") result_frame.pack(fill="x") @@ -203,7 +306,7 @@ def _create_control_panel(self, parent): ) self.quality_label.pack(anchor="w", pady=(0, 5)) - # 处理时间显示 + # 处理时间/FPS显示 self.time_label = ttk.Label( result_frame, text="", @@ -240,7 +343,7 @@ def _create_display_panel(self, parent): self.original_canvas.pack(fill="both", expand=True) self.original_canvas.create_text( 300, 200, - text="请选择道路图片", + text="请选择输入源", font=("微软雅黑", 12), fill="#7f8c8d" ) @@ -292,6 +395,81 @@ def _create_status_bar(self, parent): ) status_label.pack(side="right", padx=(0, 10), pady=5) + def _switch_to_image_mode(self): + """切换到图像模式""" + if self.is_video_mode: + self._stop_video() + + self.is_video_mode = False + self.camera_mode = False + self.mode_label.config(text="[图像模式]", foreground="#3498db") + + # 显示图像控制,隐藏视频控制 + self.file_frame.pack(fill="x", pady=(0, 15)) + self.video_frame.pack_forget() + + # 更新按钮状态 + self.select_image_btn.config(state="normal") + self.redetect_btn.config(state="normal" if self.current_image_path else "disabled") + + self.status_var.set("已切换到图像模式") + + def _switch_to_video_mode(self): + """切换到视频模式""" + if self.is_video_mode and self.camera_mode: + self._stop_video() + + self.is_video_mode = True + self.camera_mode = False + self.mode_label.config(text="[视频模式]", foreground="#e74c3c") + + # 隐藏图像控制,显示视频控制 + self.file_frame.pack_forget() + self.video_frame.pack(fill="x", pady=(0, 15)) + + # 更新按钮状态 + self.select_video_btn.config(state="normal") + self.play_btn.config(state="disabled") + self.pause_btn.config(state="disabled") + self.stop_btn.config(state="disabled") + + self.status_var.set("已切换到视频模式") + + def _switch_to_camera_mode(self): + """切换到摄像头模式""" + if self.is_video_mode: + self._stop_video() + + self.is_video_mode = True + self.camera_mode = True + self.mode_label.config(text="[摄像头模式]", foreground="#9b59b6") + + # 隐藏图像控制,显示视频控制 + self.file_frame.pack_forget() + self.video_frame.pack(fill="x", pady=(0, 15)) + + # 更新按钮状态 + self.select_video_btn.config(state="normal") + self.play_btn.config(state="normal") + self.pause_btn.config(state="disabled") + self.stop_btn.config(state="disabled") + + # 尝试打开摄像头 + self._open_camera() + + def _open_camera(self): + """打开摄像头""" + try: + if self.video_processor.open_camera(): + self.play_btn.config(state="normal") + self.status_var.set("摄像头已打开") + else: + messagebox.showerror("错误", "无法打开摄像头,请检查摄像头连接") + self._switch_to_image_mode() + except Exception as e: + messagebox.showerror("错误", f"打开摄像头失败: {str(e)}") + self._switch_to_image_mode() + def _select_image(self): """选择图片""" if self.is_processing: @@ -312,6 +490,113 @@ def _select_image(self): self.current_image_path = file_path self._load_image(file_path) + def _select_video(self): + """选择视频文件""" + if self.is_processing and self.is_video_mode: + messagebox.showwarning("提示", "正在处理视频,请先停止当前处理") + return + + file_types = [ + ("视频文件", "*.mp4 *.avi *.mov *.mkv *.flv"), + ("所有文件", "*.*") + ] + + file_path = filedialog.askopenfilename( + title="选择道路视频", + filetypes=file_types + ) + + if file_path: + self.video_file_path = file_path + self._open_video(file_path) + + def _open_video(self, file_path: str): + """打开视频文件""" + if self.video_processor.open_video_file(file_path): + self.file_info_label.config(text=os.path.basename(file_path)) + self.play_btn.config(state="normal") + self.pause_btn.config(state="disabled") + self.stop_btn.config(state="disabled") + self.status_var.set(f"视频已加载: {os.path.basename(file_path)}") + else: + messagebox.showerror("错误", "无法打开视频文件") + + def _play_video(self): + """播放视频""" + if self.is_processing and not self.is_video_mode: + return + + if self.camera_mode and self.video_processor.video_capture is None: + self._open_camera() + + if self.video_processor.start_processing(self._process_video_frame): + self.is_processing = True + self.play_btn.config(state="disabled") + self.pause_btn.config(state="normal") + self.stop_btn.config(state="normal") + self.status_var.set("视频处理中...") + else: + messagebox.showerror("错误", "无法开始视频处理") + + def _pause_video(self): + """暂停视频""" + if self.is_video_mode and self.video_processor.is_playing: + self.video_processor.pause() + self.play_btn.config(state="normal") + self.pause_btn.config(state="disabled") + self.status_var.set("视频已暂停") + + def _stop_video(self): + """停止视频""" + if self.is_video_mode: + self.video_processor.stop() + self.is_processing = False + self.play_btn.config(state="normal") + self.pause_btn.config(state="disabled") + self.stop_btn.config(state="disabled") + self.status_var.set("视频已停止") + + # 清空显示 + self._clear_canvas_display() + + def _process_video_frame(self, frame: np.ndarray, frame_info: Dict[str, Any]): + """处理视频帧""" + 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, + is_video=True, frame_info=frame_info + ) + + processing_time = time.time() - start_time + + # 在主线程中更新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) + + except Exception as e: + print(f"视频帧处理失败: {e}") + def _load_image(self, file_path: str): """加载图像""" try: @@ -399,7 +684,7 @@ def _update_processing_state(self, is_processing: bool): def _update_results(self, direction_info: Dict[str, Any], lane_info: Dict[str, Any], visualization: np.ndarray, processing_time: float): - """更新结果""" + """更新结果(图像模式)""" try: # 显示图像 self._display_image(self.current_image, self.original_canvas, "原始图像") @@ -441,6 +726,58 @@ def _update_results(self, direction_info: Dict[str, Any], lane_info: Dict[str, A print(f"更新结果失败: {e}") self.status_var.set("更新结果失败") + def _update_video_results(self, original_frame: np.ndarray, visualization: np.ndarray, + direction_info: Dict[str, Any], lane_info: Dict[str, Any], + processing_time: float, frame_info: Dict[str, Any]): + """更新视频结果""" + try: + # 显示图像 + self._display_image(original_frame, self.original_canvas, "原始视频") + self._display_image(visualization, self.result_canvas, "实时检测") + + # 获取信息 + direction = direction_info['direction'] + confidence = direction_info['confidence'] + quality = lane_info.get('detection_quality', 0.0) + + # 更新方向信息 + self.direction_label.config(text=f"方向: {direction}") + + # 设置置信度文本和颜色 + if confidence > 0.7: + color = "#27ae60" + confidence_text = f"置信度: {confidence:.1%} (高)" + elif confidence > 0.4: + color = "#f39c12" + confidence_text = f"置信度: {confidence:.1%} (中)" + else: + color = "#e74c3c" + confidence_text = f"置信度: {confidence:.1%} (低)" + + self.confidence_label.config(text=confidence_text, foreground=color) + + # 设置检测质量 + self.quality_label.config(text=f"检测质量: {quality:.1%}") + + # 计算FPS + self.frame_counter += 1 + current_time = time.time() + if current_time - self.last_fps_update >= 1.0: + self.current_fps = self.frame_counter / (current_time - self.last_fps_update) + self.last_fps_update = current_time + self.frame_counter = 0 + + # 设置处理时间和FPS + time_text = f"处理时间: {processing_time:.3f}s | FPS: {self.current_fps:.1f}" + self.time_label.config(text=time_text) + + # 更新状态 + video_type = "摄像头" if self.camera_mode else "视频" + self.status_var.set(f"{video_type}处理中 - {direction} | FPS: {self.current_fps:.1f}") + + except Exception as e: + print(f"更新视频结果失败: {e}") + def _display_image(self, image: np.ndarray, canvas: tk.Canvas, title: str): """在Canvas上显示图像""" try: @@ -483,14 +820,32 @@ def _display_image(self, image: np.ndarray, canvas: tk.Canvas, title: str): print(f"显示图像失败: {e}") canvas.create_text(150, 150, text="图像显示失败", fill="red") + def _clear_canvas_display(self): + """清空画布显示""" + self.original_canvas.delete("all") + self.result_canvas.delete("all") + + self.original_canvas.create_text( + 300, 200, + text="请选择输入源", + font=("微软雅黑", 12), + fill="#7f8c8d" + ) + + self.result_canvas.create_text( + 300, 200, + text="检测结果将显示在这里", + font=("微软雅黑", 12), + fill="#7f8c8d" + ) + def _redetect(self): """重新检测""" - if self.current_image_path and not self.is_processing: + if self.current_image_path and not self.is_processing and not self.is_video_mode: self._process_image(self.current_image_path) def _on_parameter_change(self, value): """参数变化回调""" - # 更新配置 sensitivity = self.sensitivity_var.get() # 根据敏感度调整参数 @@ -501,14 +856,53 @@ def _on_parameter_change(self, value): print(f"参数更新: 敏感度={sensitivity:.2f}") # 如果已有图像,自动重新检测 - if self.current_image_path and not self.is_processing: + if self.current_image_path and not self.is_processing and not self.is_video_mode: + self._redetect() + + def _on_scene_change(self, event): + """场景选择变化""" + scene = self.scene_var.get() + + if scene == "高速公路": + config = SceneConfig.get_scene_config('highway') + elif scene == "城市道路": + config = SceneConfig.get_scene_config('urban') + elif scene == "乡村道路": + 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) + + print(f"场景切换为: {scene}") + self.status_var.set(f"场景已切换为: {scene}") + + # 重新检测 + if self.current_image_path and not self.is_processing and not self.is_video_mode: self._redetect() def _show_error(self, error_msg: str): """显示错误""" messagebox.showerror("错误", f"处理失败: {error_msg}") self.status_var.set("处理失败") - + + def _on_closing(self): + """窗口关闭事件""" + if self.is_video_mode: + self._stop_video() + self.video_processor.release() + + self.root.destroy() + print("应用程序已关闭") def main(): """主函数""" @@ -527,4 +921,6 @@ def main(): messagebox.showerror("致命错误", f"应用程序启动失败: {str(e)}") if __name__ == "__main__": + # 导入SceneConfig + from config import SceneConfig main() \ 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 new file mode 100644 index 0000000000..f9a3888490 --- /dev/null +++ b/src/lane_identification/software_package/video_processor.py @@ -0,0 +1,223 @@ +""" +视频处理模块 - 负责视频和摄像头处理 +""" + +import cv2 +import numpy as np +import threading +import time +from typing import Optional, Dict, Any +from config import AppConfig + +class VideoProcessor: + """视频处理器""" + + def __init__(self, config: AppConfig): + self.config = config + self.video_capture = None + self.is_playing = False + self.is_paused = False + self.current_frame = None + self.frame_count = 0 + self.fps = 0 + self.frame_skip = config.video_frame_skip + self.processor_thread = None + + # 性能统计 + self.frame_times = [] + self.processing_times = [] + + print("视频处理器已初始化") + + def open_video_file(self, video_path: str) -> bool: + """打开视频文件""" + try: + self.video_capture = cv2.VideoCapture(video_path) + if not self.video_capture.isOpened(): + print(f"无法打开视频文件: {video_path}") + return False + + # 获取视频信息 + self.frame_count = int(self.video_capture.get(cv2.CAP_PROP_FRAME_COUNT)) + self.fps = self.video_capture.get(cv2.CAP_PROP_FPS) + if self.fps <= 0: + self.fps = self.config.video_fps + + print(f"视频已打开: {video_path}") + print(f"帧数: {self.frame_count}, FPS: {self.fps}") + + return True + + except Exception as e: + print(f"打开视频文件失败: {e}") + return False + + def open_camera(self, camera_id: Optional[int] = None) -> bool: + """打开摄像头""" + try: + cam_id = camera_id if camera_id is not None else self.config.camera_id + self.video_capture = cv2.VideoCapture(cam_id) + + if not self.video_capture.isOpened(): + print(f"无法打开摄像头 {cam_id}") + return False + + # 设置摄像头参数 + self.video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, 640) + self.video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) + self.video_capture.set(cv2.CAP_PROP_FPS, self.config.video_fps) + + self.fps = self.video_capture.get(cv2.CAP_PROP_FPS) + if self.fps <= 0: + self.fps = self.config.video_fps + + print(f"摄像头已打开: {cam_id}") + print(f"FPS: {self.fps}") + + return True + + except Exception as e: + print(f"打开摄像头失败: {e}") + return False + + def start_processing(self, callback: callable): + """开始视频处理""" + if self.video_capture is None: + print("错误:未打开视频源") + return False + + self.is_playing = True + self.is_paused = False + + # 启动处理线程 + self.processor_thread = threading.Thread( + target=self._process_frames, + args=(callback,), + daemon=True + ) + self.processor_thread.start() + + print("视频处理已开始") + return True + + def _process_frames(self, callback: callable): + """处理视频帧""" + frame_number = 0 + skip_counter = 0 + last_time = time.time() + + while self.is_playing and self.video_capture is not None: + if self.is_paused: + time.sleep(0.1) + continue + + # 跳过一些帧以提高性能 + skip_counter += 1 + if skip_counter < self.frame_skip: + # 仍然需要读取帧以保持进度 + self.video_capture.grab() + continue + skip_counter = 0 + + # 读取帧 + ret, frame = self.video_capture.read() + if not ret: + print("视频结束或读取失败") + break + + frame_number += 1 + + # 记录开始时间 + start_time = time.time() + + # 准备帧信息 + frame_info = { + 'frame_number': frame_number, + 'frame_time': start_time, + 'fps': self.fps + } + + # 调用回调函数处理帧 + try: + callback(frame, frame_info) + except Exception as e: + print(f"帧处理回调失败: {e}") + + # 计算处理时间 + processing_time = time.time() - start_time + self.processing_times.append(processing_time) + if len(self.processing_times) > 10: + self.processing_times.pop(0) + + # 计算实际FPS + current_time = time.time() + if current_time - last_time >= 1.0: + self.fps = frame_number / (current_time - last_time) + last_time = current_time + frame_number = 0 + + # 控制处理速度 + target_delay = 1.0 / self.fps + if processing_time < target_delay: + time.sleep(target_delay - processing_time) + + def pause(self): + """暂停视频处理""" + self.is_paused = True + print("视频已暂停") + + def resume(self): + """恢复视频处理""" + self.is_paused = False + print("视频已恢复") + + def stop(self): + """停止视频处理""" + self.is_playing = False + self.is_paused = False + + if self.processor_thread and self.processor_thread.is_alive(): + self.processor_thread.join(timeout=2.0) + + print("视频处理已停止") + + def release(self): + """释放资源""" + self.stop() + + if self.video_capture is not None: + self.video_capture.release() + self.video_capture = None + + print("视频资源已释放") + + def get_frame(self): + """获取当前帧""" + if self.video_capture is None: + return None + + ret, frame = self.video_capture.read() + if ret: + self.current_frame = frame + return frame + + return None + + def get_video_info(self) -> Dict[str, Any]: + """获取视频信息""" + if self.video_capture is None: + return {} + + return { + 'fps': self.fps, + 'frame_width': int(self.video_capture.get(cv2.CAP_PROP_FRAME_WIDTH)), + '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 + } + + def set_frame_position(self, position: int): + """设置帧位置""" + if self.video_capture is not None: + self.video_capture.set(cv2.CAP_PROP_POS_FRAMES, position) \ No newline at end of file diff --git a/src/lane_identification/software_package/visualizer.py b/src/lane_identification/software_package/visualizer.py index 050070f9c8..ac8b466d24 100644 --- a/src/lane_identification/software_package/visualizer.py +++ b/src/lane_identification/software_package/visualizer.py @@ -1,5 +1,6 @@ """ 可视化模块 - 负责结果可视化显示 +支持实时视频显示 """ import cv2 @@ -39,12 +40,19 @@ def _setup_colors(self): # 文本颜色 'text_primary': (255, 255, 255), 'text_secondary': (200, 200, 200), + + # 状态指示器 + 'status_active': (0, 255, 0), + 'status_paused': (255, 165, 0), + 'status_stopped': (255, 0, 0) } def create_visualization(self, image: np.ndarray, road_info: Dict[str, Any], lane_info: Dict[str, Any], - direction_info: Dict[str, Any]) -> np.ndarray: + direction_info: Dict[str, Any], + is_video: bool = False, + frame_info: Dict[str, Any] = None) -> np.ndarray: """创建可视化结果""" try: # 创建副本 @@ -62,7 +70,7 @@ def create_visualization(self, image: np.ndarray, visualization = self._draw_future_path(visualization, lane_info['future_path']) # 4. 绘制信息面板 - visualization = self._draw_info_panel(visualization, direction_info, lane_info) + visualization = self._draw_info_panel(visualization, direction_info, lane_info, is_video, frame_info) # 5. 绘制方向指示器 visualization = self._draw_direction_indicator(visualization, direction_info) @@ -118,7 +126,6 @@ def _draw_lanes(self, image: np.ndarray, lane_info: Dict[str, Any]) -> np.ndarra points = lane['points'] color = self.colors[color_key] - # 根据置信度调整线条粗细 confidence = lane.get('confidence', 0.5) thickness = 2 + int(confidence * 4) @@ -145,25 +152,22 @@ def _draw_future_path(self, image: np.ndarray, future_path: Dict[str, Any]) -> n path_layer = image.copy() color = self.colors['future_path'] - # 绘制渐变路径线 for i in range(len(path_points) - 1): - # 计算透明度渐变 alpha_factor = 0.5 + 0.5 * (i / (len(path_points) - 1)) line_color = tuple(int(c * alpha_factor) for c in color[:3]) - # 线条粗细渐变 thickness = 5 - int(i / len(path_points) * 3) cv2.line(path_layer, path_points[i], path_points[i + 1], line_color, thickness, cv2.LINE_AA) - # 混合路径图层 cv2.addWeighted(path_layer, 0.6, image, 0.4, 0, image) return image def _draw_info_panel(self, image: np.ndarray, direction_info: Dict[str, Any], - lane_info: Dict[str, Any]) -> np.ndarray: + lane_info: Dict[str, Any], is_video: bool = False, + frame_info: Dict[str, Any] = None) -> np.ndarray: """绘制信息面板""" height, width = image.shape[:2] @@ -199,17 +203,26 @@ def _draw_info_panel(self, image: np.ndarray, direction_info: Dict[str, Any], cv2.putText(image, quality_text, (20, 100), font, 0.7, self.colors['text_secondary'], 1) - # 4. 概率分布(右侧) + # 4. 视频信息 + if is_video and frame_info: + fps_text = f"FPS: {frame_info.get('fps', 0):.1f}" + frame_text = f"帧: {frame_info.get('frame_number', 0)}" + + cv2.putText(image, fps_text, (width - 200, 35), + font, 0.7, self.colors['text_primary'], 1) + cv2.putText(image, frame_text, (width - 200, 65), + font, 0.7, self.colors['text_primary'], 1) + + # 5. 概率分布 if 'probabilities' in direction_info: probabilities = direction_info['probabilities'] start_x = width - 200 - start_y = 30 + start_y = 95 if is_video else 30 for i, (dir_name, prob) in enumerate(probabilities.items()): y = start_y + i * 25 prob_text = f"{dir_name}: {prob:.1%}" - # 高亮当前方向 color = self.colors['text_primary'] if dir_name == direction else self.colors['text_secondary'] cv2.putText(image, prob_text, (start_x, y), @@ -224,14 +237,13 @@ def _draw_direction_indicator(self, image: np.ndarray, direction = direction_info.get('direction', '未知') confidence = direction_info.get('confidence', 0.0) - # 指示器位置(底部中央) + # 指示器位置 center_x = width // 2 indicator_y = height - 150 # 创建指示器图层 indicator_layer = np.zeros_like(image) - # 根据方向绘制指示器 if direction == "左转": # 左转箭头 points = np.array([ @@ -244,7 +256,7 @@ def _draw_direction_indicator(self, image: np.ndarray, [center_x - 100, indicator_y + 80], [center_x, indicator_y + 80] ], dtype=np.int32) - base_color = (0, 165, 255) # 橙色 + base_color = (0, 165, 255) elif direction == "右转": # 右转箭头 @@ -258,7 +270,7 @@ def _draw_direction_indicator(self, image: np.ndarray, [center_x + 100, indicator_y + 80], [center_x, indicator_y + 80] ], dtype=np.int32) - base_color = (0, 165, 255) # 橙色 + base_color = (0, 165, 255) else: # 直行或未知 # 直行箭头 @@ -271,7 +283,7 @@ def _draw_direction_indicator(self, image: np.ndarray, [center_x - 40, indicator_y + 120], [center_x - 40, indicator_y + 40] ], dtype=np.int32) - base_color = (0, 255, 0) # 绿色 + base_color = (0, 255, 0) # 根据置信度调整颜色亮度 brightness_factor = 0.5 + confidence * 0.5 @@ -280,7 +292,6 @@ def _draw_direction_indicator(self, image: np.ndarray, # 绘制指示器 cv2.fillPoly(indicator_layer, [points], color) - # 根据置信度调整透明度 alpha = 0.3 + confidence * 0.5 cv2.addWeighted(indicator_layer, alpha, image, 1 - alpha, 0, image) @@ -308,7 +319,6 @@ def _apply_global_effects(self, image: np.ndarray) -> np.ndarray: [-1, -1, -1]]) sharpened = cv2.filter2D(image, -1, kernel) - # 混合原始和锐化图像 cv2.addWeighted(sharpened, 0.3, image, 0.7, 0, image) return image \ No newline at end of file