From 4194baa37c50669937277aeebd771bc04456b19b Mon Sep 17 00:00:00 2001 From: dacun922 <3109383112@qq.com> Date: Thu, 18 Dec 2025 21:43:05 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E7=B2=BE=E7=AE=80=E7=89=88?= =?UTF-8?q?=E8=BD=A6=E9=81=93=E7=BA=BF=E6=A3=80=E6=B5=8B=20-=20=E4=BB=85?= =?UTF-8?q?=E4=BF=9D=E7=95=99Canny+=E9=9C=8D=E5=A4=AB=E5=8F=98=E6=8D=A2?= =?UTF-8?q?=E6=A0=B8=E5=BF=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 276 ++++++++++++++++++++++---------------------------------- 1 file changed, 106 insertions(+), 170 deletions(-) diff --git a/main.py b/main.py index 5d447efb56..8ac2a835e6 100644 --- a/main.py +++ b/main.py @@ -1,182 +1,118 @@ -import sys -import os -# 替换绝对路径为相对路径:基于main.py所在目录向上找src文件夹(适配任意部署环境) -sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')) +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Lane Line Detection (精简版) +核心功能:Canny边缘检测 + 霍夫变换实现车道线检测,单视频处理+结果保存 +适用环境:Ubuntu (Python 3.10 + OpenCV + numpy) +""" -import numpy as np import cv2 -from tensorflow.keras.models import load_model -from common.transformations.camera import transform_img, eon_intrinsics -from common.transformations.model import medmodel_intrinsics -from common.tools.lib.parser import parser - -# 关闭TensorFlow所有冗余警告 -os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' - -# -------------------------- 核心工具函数 -------------------------- -def frames_to_tensor(frames): - if len(frames) == 0: - return np.array([]) - H = (frames.shape[1] * 2) // 3 - W = frames.shape[2] - tensor = np.zeros((frames.shape[0], 6, H//2, W//2), dtype=np.float32) - tensor[:, 0] = frames[:, 0:H:2, 0::2] - tensor[:, 1] = frames[:, 1:H:2, 0::2] - tensor[:, 2] = frames[:, 0:H:2, 1::2] - tensor[:, 3] = frames[:, 1:H:2, 1::2] - tensor[:, 4] = frames[:, H:H+H//4].reshape((-1, H//2, W//2)) - tensor[:, 5] = frames[:, H+H//4:H+H//2].reshape((-1, H//2, W//2)) - return tensor / 128.0 - 1.0 - -def preprocess_frames(imgs): - if not imgs: - return np.array([]) - processed = np.zeros((len(imgs), 384, 512), dtype=np.uint8) - for i, img in enumerate(imgs): - try: - processed[i] = transform_img(img, from_intr=eon_intrinsics, to_intr=medmodel_intrinsics, yuv=True, output_size=(512, 256)) - except: - processed[i] = np.zeros((384, 512), dtype=np.uint8) - return frames_to_tensor(processed) - -# -------------------------- 主函数(无参数、全英文、车道线优化) -------------------------- -def main(): - # 1. 初始化显示窗口(800x600,固定尺寸) - win_name = "Lane Line Prediction (Blue=Left | Red=Right | Green=Path)" - cv2.namedWindow(win_name, cv2.WINDOW_NORMAL) - cv2.resizeWindow(win_name, 800, 600) - - # 2. 读取视频(修改为相对路径:main.py所在文件夹下的sample.hevc) - video_path = "./sample.hevc" # 仅改这里:绝对路径→相对路径 +import numpy as np +import argparse +import os +from pathlib import Path + +# ===================== 核心参数(仅保留必要的) ===================== +# 车道线检测核心参数 +CANNY_LOW_THRESH = 50 # Canny边缘检测低阈值 +CANNY_HIGH_THRESH = 150 # Canny边缘检测高阈值 +HOUGH_RHO = 1 # 霍夫变换rho步长 +HOUGH_THETA = np.pi / 180 # 霍夫变换theta步长 +HOUGH_THRESHOLD = 20 # 霍夫变换阈值 +HOUGH_MIN_LINE_LEN = 40 # 最小线段长度 +HOUGH_MAX_LINE_GAP = 20 # 最大线段间隙 + +# ===================== 核心函数:车道线检测 ===================== +def detect_lane_lines(frame): + """核心:检测并绘制车道线(Canny + 霍夫变换)""" + # 1. 灰度化 + 高斯模糊 + gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) + blur = cv2.GaussianBlur(gray, (5, 5), 0) + + # 2. Canny边缘检测 + edges = cv2.Canny(blur, CANNY_LOW_THRESH, CANNY_HIGH_THRESH) + + # 3. 区域掩码(仅检测图像下半部分,聚焦车道) + h, w = frame.shape[:2] + mask = np.zeros_like(edges) + polygon = np.array([[(0, h), (w//2, h//2), (w, h)]], np.int32) + cv2.fillPoly(mask, polygon, 255) + masked_edges = cv2.bitwise_and(edges, mask) + + # 4. 霍夫变换检测直线 + lines = cv2.HoughLinesP( + masked_edges, HOUGH_RHO, HOUGH_THETA, HOUGH_THRESHOLD, + minLineLength=HOUGH_MIN_LINE_LEN, maxLineGap=HOUGH_MAX_LINE_GAP + ) + + # 5. 绘制车道线 + frame_with_lane = frame.copy() + if lines is not None: + for line in lines: + x1, y1, x2, y2 = line[0] + cv2.line(frame_with_lane, (x1, y1), (x2, y2), (0, 255, 0), 3) + + return frame_with_lane + +# ===================== 核心函数:视频处理 ===================== +def process_video(video_path, max_frames=10): + """处理单视频:读取帧→检测车道线→保存结果""" + # 校验视频文件 + if not os.path.exists(video_path): + print(f"错误:视频文件不存在 → {video_path}") + return + + # 打开视频流 cap = cv2.VideoCapture(video_path) if not cap.isOpened(): - empty_frame = np.ones((600, 800, 3), dtype=np.uint8) * 255 - cv2.putText(empty_frame, "Cannot open video", (150, 300), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 255), 3) - cv2.imshow(win_name, empty_frame) - cv2.waitKey(0) - cv2.destroyAllWindows() + print(f"错误:无法打开视频 → {video_path}") return - - # 读取前10帧(用于推理,保留原始帧和模型输入帧) - raw_display_frames = [] # 用于显示的800x600帧 - model_input_imgs = [] # 用于模型的512x384 YUV帧 - for _ in range(10): + + # 获取视频基础信息 + width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) + height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) + fps = int(cap.get(cv2.CAP_PROP_FPS)) or 25 + + # 初始化结果视频写入器 + video_name = Path(video_path).stem + result_path = f"{video_name}_lane_detected.mp4" + fourcc = cv2.VideoWriter_fourcc(*'mp4v') + writer = cv2.VideoWriter(result_path, fourcc, fps, (width, height)) + + # 逐帧处理 + count = 0 + print(f"开始处理视频(最大{max_frames}帧)...") + while cap.isOpened() and count < max_frames: ret, frame = cap.read() if not ret: break - # 缩放为显示尺寸(800x600) - display_frame = cv2.resize(frame, (800, 600)) - raw_display_frames.append(display_frame) - # 转换为模型需要的YUV格式并缩放 - yuv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2YUV_I420) - model_frame = cv2.resize(yuv_frame, (512, 384), cv2.INTER_AREA) - model_input_imgs.append(model_frame) - cap.release() - - # 校验帧数是否足够 - if len(raw_display_frames) < 2: - empty_frame = np.ones((600, 800, 3), dtype=np.uint8) * 255 - cv2.putText(empty_frame, "Insufficient video frames", (100, 300), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 0, 255), 3) - cv2.imshow(win_name, empty_frame) - cv2.waitKey(0) - cv2.destroyAllWindows() - return - - # 3. 加载模型(显示英文提示,无乱码) - load_frame = np.ones((600, 800, 3), dtype=np.uint8) * 255 - cv2.putText(load_frame, "Loading model...", (200, 300), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 255, 0), 3) - cv2.imshow(win_name, load_frame) - cv2.waitKey(200) # 刷新显示 - - # 模型路径(修改为相对路径:main.py所在文件夹下的models/supercombo.h5) - model_path = "./models/supercombo.h5" # 仅改这里:绝对路径→相对路径 - try: - supercombo_model = load_model(model_path, compile=False) - except Exception as e: - empty_frame = np.ones((600, 800, 3), dtype=np.uint8) * 255 - cv2.putText(empty_frame, "Model load failed", (180, 300), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 255), 3) - cv2.imshow(win_name, empty_frame) - cv2.waitKey(0) - cv2.destroyAllWindows() - return - - # 4. 预处理帧(显示英文提示) - preprocess_frame = np.ones((600, 800, 3), dtype=np.uint8) * 255 - cv2.putText(preprocess_frame, "Preprocessing frames...", (150, 300), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 255, 0), 3) - cv2.imshow(win_name, preprocess_frame) - cv2.waitKey(200) - - frame_tensors = preprocess_frames(model_input_imgs) - if frame_tensors.size == 0: - empty_frame = np.ones((600, 800, 3), dtype=np.uint8) * 255 - cv2.putText(empty_frame, "Preprocessing failed", (180, 300), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 255), 3) - cv2.imshow(win_name, empty_frame) - cv2.waitKey(0) - cv2.destroyAllWindows() - return - - # 5. 模型状态初始化 - model_state = np.zeros((1, 512)) - model_desire = np.zeros((1, 8)) - - # 6. 逐帧推理+绘制(核心优化:车道线右移+放大圆点) - print("✅ Start inference and display (Press Q to exit)") - for i in range(len(frame_tensors) - 1): - # 确保帧存在,避免索引越界 - if i >= len(raw_display_frames): - current_frame = np.ones((600, 800, 3), dtype=np.uint8) * 255 - else: - current_frame = raw_display_frames[i].copy() # 复制原始帧,避免修改 - - try: - # 模型推理(连续两帧作为输入) - input_data = [np.vstack(frame_tensors[i:i+2])[None], model_desire, model_state] - model_output = supercombo_model.predict(input_data, verbose=0) - parsed_result = parser(model_output) - model_state = model_output[-1] - - # -------------------------- 车道线绘制优化 -------------------------- - # 提取模型输出的车道线/路径x坐标 - left_lane_x = parsed_result["lll"][0] - right_lane_x = parsed_result["rll"][0] - path_x = parsed_result["path"][0] - - # 窗口尺寸 - win_h, win_w = 600, 800 - # y坐标映射(0-191 → 0-599) - y_points = np.linspace(0, win_h - 1, 192).astype(int) - # x坐标映射(0-512 → 0-799)+ 右移100像素(解决偏左问题)+ 放大圆点到8px - left_x_mapped = (left_lane_x / 512 * win_w + 100).astype(int) - right_x_mapped = (right_lane_x / 512 * win_w + 100).astype(int) - path_x_mapped = (path_x / 512 * win_w + 100).astype(int) - - # 绘制左车道线(蓝色,8px实心圆) - for x, y in zip(left_x_mapped, y_points): - if 0 <= x < win_w and 0 <= y < win_h: - cv2.circle(current_frame, (x, y), 8, (255, 0, 0), -1) - # 绘制右车道线(红色,8px实心圆) - for x, y in zip(right_x_mapped, y_points): - if 0 <= x < win_w and 0 <= y < win_h: - cv2.circle(current_frame, (x, y), 8, (0, 0, 255), -1) - # 绘制预测路径(绿色,6px实心圆) - for x, y in zip(path_x_mapped, y_points): - if 0 <= x < win_w and 0 <= y < win_h: - cv2.circle(current_frame, (x, y), 6, (0, 255, 0), -1) - - except Exception as e: - # 推理失败时仅打印错误,仍显示原始帧 - print(f"⚠️ Frame {i+1} inference error: {str(e)[:30]}") - - # 强制显示当前帧 - cv2.imshow(win_name, current_frame) - # 按Q退出 - if cv2.waitKey(100) & 0xFF == ord('q'): - print("🛑 Exit by user (Q pressed)") + + # 核心:检测车道线 + frame_with_lane = detect_lane_lines(frame) + + # 写入结果视频 + writer.write(frame_with_lane) + + # 实时显示(可选,按Q退出) + cv2.imshow("Lane Detection", frame_with_lane) + if cv2.waitKey(10) & 0xFF == ord('q'): break - - # 7. 程序收尾 + + count += 1 + + # 释放资源 + cap.release() + writer.release() cv2.destroyAllWindows() - print("🎉 All frames processed successfully!") + print(f"处理完成!结果保存至 → {result_path}") +# ===================== 主函数(精简参数) ===================== if __name__ == "__main__": - main() + # 简化命令行参数:仅保留输入视频、最大帧数 + parser = argparse.ArgumentParser(description="车道线检测(精简版)") + parser.add_argument("video_path", type=str, help="输入视频文件路径") + parser.add_argument("--max-frames", type=int, default=10, help="最大处理帧数(默认10)") + args = parser.parse_args() + + # 执行核心逻辑 + process_video(args.video_path, args.max_frames) From 03c37b025d9c46629701c814745b2d10a33f3278 Mon Sep 17 00:00:00 2001 From: dacun922 <3109383112@qq.com> Date: Thu, 18 Dec 2025 21:56:04 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=E8=BD=A6=E9=81=93=E7=BA=BF?= =?UTF-8?q?=E6=A3=80=E6=B5=8B=E5=A2=9E=E5=BC=BA=E7=89=88=20-=20=E5=88=86?= =?UTF-8?q?=E5=B1=8F=E6=98=BE=E7=A4=BA+=E5=8D=95=E6=AD=A5=E6=92=AD?= =?UTF-8?q?=E6=94=BE+=E9=80=9F=E5=BA=A6=E5=8F=AF=E6=8E=A7+=E8=BD=A6?= =?UTF-8?q?=E9=81=93=E5=88=86=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/openpilot_model/main.py | 700 ++++++++++++------------------------ 1 file changed, 235 insertions(+), 465 deletions(-) diff --git a/src/openpilot_model/main.py b/src/openpilot_model/main.py index ec34f0b92c..286b5321f3 100644 --- a/src/openpilot_model/main.py +++ b/src/openpilot_model/main.py @@ -1,496 +1,266 @@ +# 1. 进入项目目录(必做!确保路径正确) +cd /home/dacun/nn + +# 2. 备份旧文件(防止误操作) +cp main.py main_old.py + +# 3. 强制清空并写入最终修复版代码(复制下面整段命令执行) +cat > main.py << 'EOF' #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ -Lane Line Detection Program (Enhanced Version) - -核心功能: -1. 基于边缘检测+霍夫变换实现动态车道线检测,兼容固定坐标绘制降级方案; -2. 支持单视频/批量视频处理,进度条可视化处理状态; -3. 日志双端输出(终端+文件),视频编码自适应,资源自动释放; -4. 修复Matplotlib中文乱码问题,增强程序鲁棒性。 - -适用环境:Ubuntu (Python 3.10 + OpenCV + Matplotlib + tqdm) +Lane Line Detection (最终增强版) +核心:分屏显示+速度可控+车道分类+高级可视化 +依赖:opencv-python, numpy """ -# ===================== 模块导入 ===================== -import sys +import cv2 +import numpy as np +import argparse import os import time -import logging -import argparse from pathlib import Path -from typing import List, Tuple, Optional - -import cv2 -import numpy as np -import matplotlib -import matplotlib.pyplot as plt -import matplotlib.image as mpimg -from matplotlib.font_manager import FontProperties -from tqdm import tqdm - -# ===================== 全局配置 ===================== -# 项目根路径 -PROJECT_ROOT = Path(os.path.expanduser("~/nn")) -# 默认视频路径 -VIDEO_PATH = PROJECT_ROOT / "sample.mp4" -# 结果输出目录(自动创建) -RESULT_DIR = PROJECT_ROOT / "lane_detection_results" -RESULT_DIR.mkdir(exist_ok=True) -# 日志文件路径 -LOG_FILE = PROJECT_ROOT / "lane_detection.log" - -# 中文字体路径(微黑字体) -CHINESE_FONT_PATH = "/usr/share/fonts/truetype/wqy/wqy-microhei.ttc" -# 视频处理参数 -DEFAULT_MAX_FRAMES = 10 # 默认最大处理帧数 -FPS = 25 # 默认帧率(自适应原视频帧率) -DEFAULT_DETECT_MODE = "dynamic" # 默认检测模式:dynamic-动态检测,fixed-固定坐标 - -# 车道线检测参数(霍夫变换+边缘检测) -CANNY_LOW_THRESH = 50 # Canny边缘检测低阈值 -CANNY_HIGH_THRESH = 150 # Canny边缘检测高阈值 -HOUGH_RHO = 1 # 霍夫变换极坐标rho步长 -HOUGH_THETA = np.pi / 180 # 霍夫变换极坐标theta步长 -HOUGH_THRESHOLD = 20 # 霍夫变换检测阈值 -HOUGH_MIN_LINE_LEN = 40 # 最小线段长度 -HOUGH_MAX_LINE_GAP = 20 # 最大线段间隙 - -# ===================== 日志初始化 ===================== -def setup_logger() -> logging.Logger: - """初始化日志处理器:同时输出到终端和文件 +# ===================== 核心参数 ===================== +# 检测参数 +CANNY_LOW_THRESH = 50 +CANNY_HIGH_THRESH = 150 +HOUGH_RHO = 1 +HOUGH_THETA = np.pi / 180 +HOUGH_THRESHOLD = 20 +HOUGH_MIN_LINE_LEN = 40 +HOUGH_MAX_LINE_GAP = 20 + +# 可视化参数 +COLOR_LEFT_LANE = (0, 0, 255) # 左车道:红 +COLOR_RIGHT_LANE = (255, 0, 0) # 右车道:蓝 +COLOR_CENTER_LANE = (0, 255, 0) # 中心线:绿 +COLOR_MASK = (0, 255, 0) # 车道蒙版:绿(半透明) +FONT = cv2.FONT_HERSHEY_SIMPLEX +FONT_SCALE = 0.6 +FONT_THICKNESS = 2 +PANEL_ALPHA = 0.7 # 信息面板透明度 +FRAME_DELAY = 500 # 每帧显示时间(ms),越大越慢 + +# ===================== 核心功能:车道线分类与检测 ===================== +def classify_lane_lines(lines, frame_width): + """分类左/右车道线(基于斜率+位置)""" + left_lines = [] # 左车道:斜率负+左侧 + right_lines = [] # 右车道:斜率正+右侧 - Returns: - logging.Logger: 配置完成的日志实例 - """ - logger = logging.getLogger("LaneDetection") - logger.setLevel(logging.INFO) - logger.handlers.clear() # 清除重复处理器 - - # 终端日志处理器 - stream_handler = logging.StreamHandler() - stream_formatter = logging.Formatter( - "%(asctime)s - %(levelname)s - %(message)s", - datefmt="%Y-%m-%d %H:%M:%S" - ) - stream_handler.setFormatter(stream_formatter) + if lines is None: + return left_lines, right_lines + + for line in lines: + x1, y1, x2, y2 = line[0] + if x2 - x1 == 0: # 避免除零 + continue + slope = (y2 - y1) / (x2 - x1) + mid_x = frame_width / 2 + + # 分类逻辑(斜率阈值0.3,避免误检) + if slope < -0.3 and (x1 < mid_x or x2 < mid_x): + left_lines.append(line[0]) + elif slope > 0.3 and (x1 > mid_x or x2 > mid_x): + right_lines.append(line[0]) + + return left_lines, right_lines - # 文件日志处理器(UTF-8编码) - file_handler = logging.FileHandler(LOG_FILE, encoding="utf-8") - file_formatter = logging.Formatter( - "%(asctime)s - %(name)s - %(levelname)s - %(message)s", - datefmt="%Y-%m-%d %H:%M:%S" +def detect_and_draw_lane(frame): + """核心:检测+分类+绘制+蒙版""" + h, w = frame.shape[:2] + frame_copy = frame.copy() + + # 1. 预处理:灰度→模糊→边缘检测 + gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) + blur = cv2.GaussianBlur(gray, (5, 5), 0) + edges = cv2.Canny(blur, CANNY_LOW_THRESH, CANNY_HIGH_THRESH) + + # 2. 区域掩码(聚焦车道区域) + mask = np.zeros_like(edges) + roi_vertices = np.array([[(0, h), (w//2, h//2), (w, h)]], np.int32) + cv2.fillPoly(mask, roi_vertices, 255) + masked_edges = cv2.bitwise_and(edges, mask) + + # 3. 霍夫变换检测直线 + lines = cv2.HoughLinesP( + masked_edges, HOUGH_RHO, HOUGH_THETA, HOUGH_THRESHOLD, + minLineLength=HOUGH_MIN_LINE_LEN, maxLineGap=HOUGH_MAX_LINE_GAP ) - file_handler.setFormatter(file_formatter) - - logger.addHandler(stream_handler) - logger.addHandler(file_handler) - return logger - -logger = setup_logger() - -# ===================== 环境初始化 ===================== -def init_environment() -> FontProperties: - """初始化Matplotlib环境,修复中文显示乱码 - Returns: - FontProperties: 加载完成的中文字体实例 + # 4. 分类+绘制车道线 + left_lines, right_lines = classify_lane_lines(lines, w) + for line in left_lines: + cv2.line(frame_copy, (line[0], line[1]), (line[2], line[3]), COLOR_LEFT_LANE, 4) + for line in right_lines: + cv2.line(frame_copy, (line[0], line[1]), (line[2], line[3]), COLOR_RIGHT_LANE, 4) - Raises: - SystemExit: 字体文件不存在或加载失败时退出程序 - """ - # 适配GUI/无GUI环境的Matplotlib后端 - if os.environ.get('DISPLAY') is None: - matplotlib.use('Agg') - logger.info("Matplotlib后端:Agg (无GUI模式)") - else: - matplotlib.use('TkAgg') - logger.info("Matplotlib后端:TkAgg (GUI模式)") - - # 校验字体文件 - if not os.path.exists(CHINESE_FONT_PATH): - logger.error(f"中文字体文件不存在:{CHINESE_FONT_PATH}") - logger.error("安装命令:sudo apt install fonts-wqy-microhei") - sys.exit(1) - - # 加载中文字体 - try: - chinese_font = FontProperties(fname=CHINESE_FONT_PATH, size=12) - logger.info("中文字体加载成功") - return chinese_font - except Exception as e: - logger.error(f"字体加载失败:{str(e)}") - sys.exit(1) + # 5. 绘制车道蒙版(半透明填充) + if left_lines and right_lines: + # 提取车道线关键点,构建蒙版区域 + left_pts = [(l[0], l[1]) for l in left_lines] + [(l[2], l[3]) for l in left_lines] + right_pts = [(l[0], l[1]) for l in right_lines] + [(l[2], l[3]) for l in right_lines] + + left_bottom = max(left_pts, key=lambda p: p[1]) + right_bottom = max(right_pts, key=lambda p: p[1]) + left_top = min(left_pts, key=lambda p: p[1]) + right_top = min(right_pts, key=lambda p: p[1]) + + mask_pts = np.array([left_bottom, left_top, right_top, right_bottom], np.int32) + mask_layer = frame_copy.copy() + cv2.fillPoly(mask_layer, [mask_pts], COLOR_MASK) + cv2.addWeighted(mask_layer, 0.2, frame_copy, 0.8, 0, frame_copy) + + return frame_copy, len(left_lines), len(right_lines) -# ===================== 视频读取 ===================== -def read_video( - video_path: str, - max_frames: int = DEFAULT_MAX_FRAMES -) -> Tuple[List[np.ndarray], Optional[cv2.VideoWriter], Tuple[int, int]]: - """读取视频帧并初始化视频写入器 +# ===================== 高级可视化:分屏+信息面板 ===================== +def draw_info_panel(frame, frame_idx, total_frames, fps, left_count, right_count): + """绘制半透明信息面板(不遮挡画面)""" + h, w = frame.shape[:2] + # 黑色半透明背景 + panel = np.zeros_like(frame) + cv2.rectangle(panel, (10, 10), (350, 200), (0, 0, 0), -1) + frame = cv2.addWeighted(panel, PANEL_ALPHA, frame, 1 - PANEL_ALPHA, 0) - Args: - video_path: 视频文件路径 - max_frames: 最大读取帧数 + # 绘制信息文本 + info_texts = [ + f"Frame: {frame_idx}/{total_frames}", + f"FPS: {fps:.1f}", + f"Left Lane: {left_count} lines", + f"Right Lane: {right_count} lines", + "=== Key Controls ===", + "Space: Next Frame (Step)", + "P: Pause/Resume", + "Q: Quit" + ] + + y_offset = 40 + for text in info_texts: + cv2.putText( + frame, text, (20, y_offset), FONT, + FONT_SCALE, (255, 255, 255), FONT_THICKNESS + ) + y_offset += 25 - Returns: - Tuple[List[np.ndarray], Optional[cv2.VideoWriter], Tuple[int, int]]: - - 读取到的视频帧列表 - - 初始化后的视频写入器(失败则为None) - - 视频分辨率(width, height) - """ - # 校验视频文件存在性 + return frame + +def create_split_screen(original_frame, detected_frame): + """修复分屏:强制统一尺寸,确保左右拼接正常""" + # 统一尺寸(避免尺寸不一致导致分屏失败) + h, w = original_frame.shape[:2] + detected_frame = cv2.resize(detected_frame, (w, h), interpolation=cv2.INTER_LINEAR) + # 拼接分屏(左:原图,右:检测结果) + split_frame = np.hstack((original_frame, detected_frame)) + # 添加分屏标题 + cv2.putText(split_frame, "Original", (20, 30), FONT, 1, (255, 255, 255), 3) + cv2.putText(split_frame, "Lane Detection", (w + 20, 30), FONT, 1, (255, 255, 255), 3) + return split_frame + +# ===================== 主处理函数(速度可控+单步播放) ===================== +def process_video(video_path, max_frames=10): + """核心:分屏显示+速度可控+单步播放+截图友好""" + # 校验文件 if not os.path.exists(video_path): - logger.error(f"视频文件不存在:{video_path}") - return [], None, (0, 0) - - # 打开视频流 + print(f"Error: Video file not found → {video_path}") + return + + # 打开视频 cap = cv2.VideoCapture(video_path) if not cap.isOpened(): - logger.error(f"视频打开失败:{video_path} (请检查FFmpeg是否安装:sudo apt install ffmpeg)") - cap.release() - return [], None, (0, 0) - - # 获取视频基础信息 - width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) - height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) - fps = int(cap.get(cv2.CAP_PROP_FPS)) or FPS # 自适应原视频帧率 - logger.info(f"视频信息:{video_path} | 分辨率 {width}x{height} | 帧率 {fps}") - - # 初始化视频写入器(输出带车道线的视频) - video_name = Path(video_path).stem - result_video_path = RESULT_DIR / f"{video_name}_lane_detected.mp4" - fourcc = cv2.VideoWriter_fourcc(*'mp4v') # MP4编码格式 - writer = cv2.VideoWriter( - str(result_video_path), fourcc, fps, (width, height) - ) - - if not writer.isOpened(): - logger.error(f"视频写入器初始化失败:{result_video_path}") - cap.release() - return [], None, (width, height) - - # 读取视频帧(带进度条) - frames = [] - count = 0 - logger.info(f"开始读取视频帧(最大{max_frames}帧)...") - with tqdm(total=max_frames, desc="读取帧") as pbar: - while cap.isOpened() and count < max_frames: - ret, frame = cap.read() - if not ret: - break - frames.append(frame) - count += 1 - pbar.update(1) - - cap.release() - logger.info(f"视频帧读取完成:共读取 {len(frames)} 帧") - return frames, writer, (width, height) - -# ===================== 车道线检测 ===================== -def detect_lane_lines( - frame: np.ndarray, - mode: str = "dynamic" -) -> np.ndarray: - """检测并绘制车道线 + print(f"Error: Failed to open video → {video_path}") + return - Args: - frame: 单帧视频图像(BGR格式) - mode: 检测模式,可选值:dynamic/fixed + # 视频基础信息 + w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) + h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) + fps = int(cap.get(cv2.CAP_PROP_FPS)) or 25 + total_frames = min(int(cap.get(cv2.CAP_PROP_FRAME_COUNT)), max_frames) - Returns: - np.ndarray: 绘制了车道线的帧图像 - """ - frame_copy = frame.copy() - h, w = frame.shape[:2] - - # 固定坐标绘制车道线(降级方案) - if mode == "fixed": - cv2.line(frame_copy, (w//3, h), (w//3, h//2), (255, 0, 0), 5) # 左车道线(蓝) - cv2.line(frame_copy, (2*w//3, h), (2*w//3, h//2), (0, 0, 255), 5) # 右车道线(红) - cv2.line(frame_copy, (w//2, h), (w//2, h//2), (0, 255, 0), 3) # 中心线(绿) - return frame_copy - - # 动态检测车道线(Canny边缘检测 + 霍夫变换) - try: - # 1. 灰度化 - gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) - # 2. 高斯模糊去噪 - blur = cv2.GaussianBlur(gray, (5, 5), 0) - # 3. 边缘检测 - edges = cv2.Canny(blur, CANNY_LOW_THRESH, CANNY_HIGH_THRESH) - # 4. 区域掩码(仅检测图像下半部分,聚焦车道区域) - mask = np.zeros_like(edges) - polygon = np.array([[ - (0, h), - (w//2, h//2), - (w, h) - ]], np.int32) - cv2.fillPoly(mask, polygon, 255) - masked_edges = cv2.bitwise_and(edges, mask) - # 5. 霍夫变换检测直线 - lines = cv2.HoughLinesP( - masked_edges, - HOUGH_RHO, - HOUGH_THETA, - HOUGH_THRESHOLD, - np.array([]), - minLineLength=HOUGH_MIN_LINE_LEN, - maxLineGap=HOUGH_MAX_LINE_GAP - ) - - # 6. 绘制检测到的车道线 - line_count = 0 - if lines is not None: - for line in lines: - x1, y1, x2, y2 = line[0] - cv2.line(frame_copy, (x1, y1), (x2, y2), (0, 255, 0), 3) # 车道线(绿) - line_count += 1 - - # 7. 标注检测到的车道线数量 - cv2.putText( - frame_copy, - f"检测到车道线:{line_count}条", - (20, 40), - cv2.FONT_HERSHEY_SIMPLEX, - 1, - (255, 255, 0), - 2 - ) - return frame_copy - - except Exception as e: - logger.warning(f"动态车道线检测失败:{str(e)},自动切换为固定坐标绘制") - # 降级到固定坐标绘制 - cv2.line(frame_copy, (w//3, h), (w//3, h//2), (255, 0, 0), 5) - cv2.line(frame_copy, (2*w//3, h), (2*w//3, h//2), (0, 0, 255), 5) - return frame_copy - -# ===================== 可视化初始化 ===================== -def init_visualization( - chinese_font: FontProperties, - width: int, - height: int -) -> Tuple[plt.Figure, plt.Axes, mpimg.AxesImage]: - """初始化Matplotlib可视化窗口 + # 结果保存(分屏尺寸:宽×2) + video_name = Path(video_path).stem + result_path = f"{video_name}_lane_detected.mp4" + fourcc = cv2.VideoWriter_fourcc(*'mp4v') + writer = cv2.VideoWriter(result_path, fourcc, fps, (w*2, h)) - Args: - chinese_font: 中文字体实例 - width: 视频宽度 - height: 视频高度 + # 控制变量 + paused = False + step_mode = False # 单步模式:按空格走一帧 + frame_idx = 0 + print("=== Lane Detection (Enhanced Version) ===") + print("Key Controls:") + print(" Space: Single Step (next frame)") + print(" P: Pause/Resume") + print(" Q: Quit") - Returns: - Tuple[plt.Figure, plt.Axes, mpimg.AxesImage]: - - 可视化画布实例 - - 坐标轴实例 - - 图像显示对象 - """ - plt.ion() # 开启交互模式 - fig, ax = plt.subplots(figsize=(12, 8)) - fig.suptitle( - "车道线检测结果(叠加可视化)", - fontproperties=chinese_font, - fontweight='bold', - fontsize=16 - ) - - # 标注说明文字 - ax.text( - 0.02, 0.95, - "动态检测车道线(绿色)| 固定左车道(蓝色)| 固定右车道(红色)| 按Q退出", - transform=ax.transAxes, - color='white', - bbox=dict(facecolor='black', alpha=0.8, boxstyle='round,pad=0.5'), - fontproperties=chinese_font - ) - ax.axis('off') # 隐藏坐标轴 - - # 初始化空图像 - dummy_frame = np.zeros((height, width, 3), dtype=np.uint8) - img_display = ax.imshow(cv2.cvtColor(dummy_frame, cv2.COLOR_BGR2RGB)) - return fig, ax, img_display - -# ===================== 批量处理 ===================== -def batch_process( - input_dir: str, - max_frames: int = DEFAULT_MAX_FRAMES, - detect_mode: str = "dynamic" -): - """批量处理指定目录下的所有视频文件 + # 创建可调整大小的窗口 + cv2.namedWindow("Lane Detection (Split Screen)", cv2.WINDOW_NORMAL) + cv2.resizeWindow("Lane Detection (Split Screen)", w*2, h) - Args: - input_dir: 视频目录路径 - max_frames: 单视频最大处理帧数 - detect_mode: 检测模式(dynamic/fixed) - """ - if not os.path.isdir(input_dir): - logger.error(f"输入目录不存在:{input_dir}") - return - - # 筛选视频文件(支持mp4/avi/mov) - video_extensions = (".mp4", ".avi", ".mov") - video_files = [ - os.path.join(input_dir, f) - for f in os.listdir(input_dir) - if f.lower().endswith(video_extensions) - ] - - if not video_files: - logger.info(f"目录{input_dir}下未检测到视频文件(支持格式:{video_extensions})") - return - - logger.info(f"批量处理启动:共检测到 {len(video_files)} 个视频文件") - chinese_font = init_environment() - - # 逐视频处理 - for video_file in video_files: - logger.info(f"\n开始处理视频:{video_file}") - try: - # 读取视频帧 - frames, writer, (width, height) = read_video(video_file, max_frames) - if not frames: - continue - - # 初始化可视化窗口 - fig, ax, img_display = init_visualization(chinese_font, width, height) - - # 逐帧处理(带进度条) - with tqdm(total=len(frames), desc=f"处理{Path(video_file).stem}") as pbar: - for i, frame in enumerate(frames): - start_time = time.time() - # 检测并绘制车道线 - frame_with_lane = detect_lane_lines(frame, detect_mode) - # 计算单帧处理耗时 - process_time = time.time() - start_time - # 标注帧信息 - cv2.putText( - frame_with_lane, - f"帧:{i+1}/{len(frames)} | 耗时:{process_time:.3f}s", - (20, 80), - cv2.FONT_HERSHEY_SIMPLEX, - 1, - (0, 255, 255), - 2 - ) - # 更新可视化 - img_display.set_data(cv2.cvtColor(frame_with_lane, cv2.COLOR_BGR2RGB)) - fig.canvas.draw() - fig.canvas.flush_events() - # 写入视频文件 - if writer: - writer.write(frame_with_lane) - # 按Q键退出 - if cv2.waitKey(10) & 0xFF == ord('q'): - logger.info("用户按下Q键,终止处理") - break - pbar.update(1) - - # 释放资源 - plt.ioff() - plt.close(fig) - if writer: - writer.release() - cv2.destroyAllWindows() - logger.info(f"视频处理完成:{video_file} → 结果保存至 {RESULT_DIR}") - - except Exception as e: - logger.error(f"视频处理失败:{video_file} | 错误信息:{str(e)}") + while cap.isOpened() and frame_idx < total_frames: + # 单步模式:仅按空格键才处理下一帧 + if step_mode: + key = cv2.waitKey(0) & 0xFF # 阻塞等待按键 + if key == ord(' '): + step_mode = False # 退出单步,处理下一帧 + elif key == ord('q'): + break continue + + # 非单步模式:正常读取帧 + if not paused: + ret, frame = cap.read() + if not ret: + break + + # 核心检测 + start_time = time.time() + detected_frame, left_count, right_count = detect_and_draw_lane(frame) + fps_real = 1 / (time.time() - start_time) + + # 高级可视化 + detected_frame = draw_info_panel( + detected_frame, frame_idx+1, total_frames, + fps_real, left_count, right_count + ) + split_frame = create_split_screen(frame, detected_frame) + + # 保存结果 + writer.write(split_frame) + frame_idx += 1 + + # 显示分屏窗口 + cv2.imshow("Lane Detection (Split Screen)", split_frame) + + # 按键控制(核心:单步/暂停/退出) + key = cv2.waitKey(FRAME_DELAY) & 0xFF + if key == ord('q'): + break + elif key == ord('p'): + paused = not paused # 切换暂停/继续 + elif key == ord(' '): + step_mode = True # 进入单步模式(按空格走一帧) + + # 释放资源 + cap.release() + writer.release() + cv2.destroyAllWindows() + print(f"\n=== Process Finished ===") + print(f"Result Video: {result_path}") -# ===================== 主函数 ===================== -def main(): - """程序主入口:解析命令行参数,执行视频处理""" - # 解析命令行参数 - parser = argparse.ArgumentParser(description="车道线检测程序(增强版)") - parser.add_argument("input", type=str, help="输入路径:视频文件路径或文件夹路径(批量处理)") - parser.add_argument("--max-frames", type=int, default=DEFAULT_MAX_FRAMES, help="最大处理帧数(默认:10)") - parser.add_argument("--detect-mode", type=str, default=DEFAULT_DETECT_MODE, - choices=["dynamic", "fixed"], help="检测模式(dynamic-动态检测,fixed-固定坐标)") - parser.add_argument("--batch", action="store_true", help="批量处理模式(输入为文件夹时启用)") - args = parser.parse_args() - - # 初始化环境(加载中文字体) - chinese_font = init_environment() - - # 批量处理模式 - if args.batch or os.path.isdir(args.input): - batch_process(args.input, args.max_frames, args.detect_mode) - return - - # 单视频处理模式 - if os.path.isfile(args.input): - frames, writer, (width, height) = read_video(args.input, args.max_frames) - if not frames: - return - - # 初始化可视化窗口 - fig, ax, img_display = init_visualization(chinese_font, width, height) - - # 逐帧处理(带进度条) - with tqdm(total=len(frames), desc="处理帧") as pbar: - for i, frame in enumerate(frames): - start_time = time.time() - # 检测并绘制车道线 - frame_with_lane = detect_lane_lines(frame, args.detect_mode) - # 计算单帧处理耗时 - process_time = time.time() - start_time - # 标注帧信息 - cv2.putText( - frame_with_lane, - f"帧:{i+1}/{len(frames)} | 耗时:{process_time:.3f}s", - (20, 80), - cv2.FONT_HERSHEY_SIMPLEX, - 1, - (0, 255, 255), - 2 - ) - # 更新可视化 - img_display.set_data(cv2.cvtColor(frame_with_lane, cv2.COLOR_BGR2RGB)) - fig.canvas.draw() - fig.canvas.flush_events() - # 写入视频文件 - if writer: - writer.write(frame_with_lane) - # 按Q键退出 - if cv2.waitKey(20) & 0xFF == ord('q'): - logger.info("用户按下Q键,终止处理") - break - pbar.update(1) - - # 释放资源 - logger.info("开始释放资源...") - plt.ioff() - plt.close(fig) - if writer: - writer.release() - cv2.destroyAllWindows() - - # 输出结果信息 - result_video = RESULT_DIR / f"{Path(args.input).stem}_lane_detected.mp4" - if os.path.exists(result_video): - logger.info(f"\n处理完成!") - logger.info(f"结果视频:{result_video}") - logger.info(f"播放命令:totem {result_video}") - logger.info(f"日志文件:{LOG_FILE}") - return - - # 输入路径无效 - logger.error(f"输入路径无效:{args.input}(请输入视频文件路径或文件夹路径)") - -# ===================== 程序入口 ===================== +# ===================== 入口函数 ===================== if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Lane Detection (Enhanced + Split Screen)") + parser.add_argument("video_path", type=str, help="Path to video file") + parser.add_argument("--max-frames", type=int, default=10, help="Max frames to process (default: 10)") + args = parser.parse_args() + + # 检查依赖 try: - # 校验tqdm依赖 - from tqdm import tqdm + import cv2 + import numpy as np except ImportError: - logger.error("缺少依赖库tqdm,请执行安装:pip install tqdm") - sys.exit(1) + print("Error: Install dependencies first → pip install opencv-python numpy") + exit(1) - try: - main() - except KeyboardInterrupt: - logger.info("程序被用户中断(Ctrl+C)") - except Exception as e: - logger.error(f"程序异常终止:{str(e)}", exc_info=True) - sys.exit(1) + process_video(args.video_path, args.max_frames) +EOF