From 8cfb210abacefd43580c618e9602d69bfca92811 Mon Sep 17 00:00:00 2001 From: dacun922 <3109383112@qq.com> Date: Thu, 18 Dec 2025 20:14:18 +0800 Subject: [PATCH 1/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=8A=A8?= =?UTF-8?q?=E6=80=81=E6=A3=80=E6=B5=8B/=E8=BF=9B=E5=BA=A6=E6=9D=A1/?= =?UTF-8?q?=E6=89=B9=E9=87=8F=E5=A4=84=E7=90=86/=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/openpilot_model/main.py | 550 +++++++++++++++++++++++++++--------- 1 file changed, 423 insertions(+), 127 deletions(-) diff --git a/src/openpilot_model/main.py b/src/openpilot_model/main.py index d7653f264d..138a40487f 100644 --- a/src/openpilot_model/main.py +++ b/src/openpilot_model/main.py @@ -1,154 +1,450 @@ #!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Lane Line Detection Program (Enhanced Version) +优化点: +1. 新增实际车道线检测(边缘检测+霍夫变换),替代硬编码坐标; +2. 日志同时输出到终端+文件,进度条可视化; +3. 支持批量处理、自定义参数、视频编码自适应; +4. 清理冗余代码,函数解耦,鲁棒性增强; +5. 保留原有中文乱码修复、可视化核心逻辑。 +适用场景:Ubuntu系统下的车道线检测/可视化,支持单视频/批量视频处理 +""" + + import sys import os -import numpy as np +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 -from tqdm import tqdm -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 - -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) +import matplotlib.image as mpimg # 导入image模块,修复类型注解 +from matplotlib.font_manager import FontProperties +from tqdm import tqdm # 关键修改:从tqdm模块导入tqdm函数(而非导入整个模块) -def read_video_with_opencv(video_path, max_frames=10): # 关键:帧数从20减到10,进一步降低压力 - cap = cv2.VideoCapture(video_path) - if not cap.isOpened(): - raise Exception(f"无法打开视频:{video_path},请安装FFmpeg(sudo apt install ffmpeg)") - imgs = [] - raw_frames = [] - for i in range(max_frames): - ret, frame = cap.read() - if not ret: - break - raw_frames.append(frame) - yuv = cv2.cvtColor(frame, cv2.COLOR_BGR2YUV_I420) - yuv_resized = cv2.resize(yuv, (512, 384), interpolation=cv2.INTER_AREA) - imgs.append(yuv_resized) - cap.release() - return imgs, raw_frames -def main(): - if len(sys.argv) != 2: - print("用法: python main.py <视频文件路径>") - sys.exit(1) - video_path = sys.argv[1] - if not os.path.exists(video_path): - print(f"错误:视频文件不存在 - {video_path}") - sys.exit(1) +# 基础路径配置 +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_HIGH_THRESH = 150 +HOUGH_RHO = 1 +HOUGH_THETA = np.pi / 180 +HOUGH_THRESHOLD = 20 +HOUGH_MIN_LINE_LEN = 40 +HOUGH_MAX_LINE_GAP = 20 + + +def setup_logger() -> 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) - model_path = "models/supercombo.h5" - if not os.path.exists(model_path): - print(f"错误:模型文件不存在 - {model_path}") + # 文件处理器 + 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" + ) + file_handler.setFormatter(file_formatter) + + logger.addHandler(stream_handler) + logger.addHandler(file_handler) + return logger + +logger = setup_logger() + + +def init_environment() -> FontProperties: + """ + 初始化Matplotlib环境,修复中文乱码 + 返回:加载好的中文字体对象 + """ + # 适配GUI/无GUI后端 + 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: - print(f"加载模型:{model_path}") - supercombo = load_model(model_path, compile=False) + chinese_font = FontProperties(fname=CHINESE_FONT_PATH, size=12) + logger.info("中文字体加载成功") + return chinese_font except Exception as e: - print(f"模型加载失败:{str(e)}") + logger.error(f"字体加载失败:{str(e)}") sys.exit(1) - # 读取视频(仅10帧) + +def read_video( + video_path: str, + max_frames: int = DEFAULT_MAX_FRAMES +) -> Tuple[List[np.ndarray], Optional[cv2.VideoWriter], Tuple[int, int]]: + """ + 读取视频帧,初始化视频写入器 + 返回:帧列表、视频写入器、视频分辨率 + """ + # 校验视频文件 + if not os.path.exists(video_path): + logger.error(f"视频文件不存在:{video_path}") + return [], None, (0, 0) + + # 打开视频(用with语句自动释放资源) + cap = cv2.VideoCapture(video_path) + if not cap.isOpened(): + logger.error(f"视频打开失败:{video_path} (检查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') + 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}帧)...") + # 修复tqdm调用方式(已正确导入tqdm函数) + 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: + """ + 检测/绘制车道线 + mode: dynamic=动态检测(边缘+霍夫),fixed=固定坐标(兼容原有逻辑) + 返回:绘制了车道线的帧 + """ + 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 + + # 新增:动态车道线检测(边缘检测+霍夫变换) try: - print(f"读取视频:{video_path}(仅10帧,轻量化模式)") - imgs, raw_frames = read_video_with_opencv(video_path) - if not imgs: - print("错误:未读取到帧") - sys.exit(1) + # 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: - print(f"视频读取失败:{str(e)}") - sys.exit(1) + 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 - # 预处理帧 - print("预处理帧数据...") - frame_tensors = preprocess_frames(imgs) - if frame_tensors.size == 0: - print("错误:预处理无有效数据") - sys.exit(1) - # 初始化模型状态 - state = np.zeros((1, 512)) - desire = np.zeros((1, 8)) - - # -------------------------- 轻量化可视化(仅1个窗口,只画车道线) -------------------------- - plt.ion() # 开启交互模式(简化版) - fig, ax = plt.subplots(figsize=(8, 6)) # 单个窗口,避免子图渲染压力 - ax.set_title("车道线预测(蓝=左车道,红=右车道,绿=路径)") - ax.set_ylim(0, 191) # 固定Y轴,减少重绘计算 - ax.invert_xaxis() # 匹配驾驶视角 - ax.grid(alpha=0.3) # 简单网格,不占资源 - - # 初始化三条线(提前创建,避免每次重绘新建) - lll_line, = ax.plot([], [], "b-", linewidth=3, label="左车道线") - rll_line, = ax.plot([], [], "r-", linewidth=3, label="右车道线") - path_line, = ax.plot([], [], "g-", linewidth=2, label="预测路径") - ax.legend() - # ------------------------------------------------------------------- - - - print(f"\n开始推理+可视化(共{len(frame_tensors)-1}帧,按Q键退出)...") - for i in range(len(frame_tensors) - 1): +def init_visualization( + chinese_font: FontProperties, + width: int, + height: int +) -> 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" +): + """批量处理指定目录下的所有视频文件""" + 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}下未找到视频文件") + return + + logger.info(f"批量处理:共{len(video_files)}个视频") + chinese_font = init_environment() + + for video_file in video_files: + logger.info(f"\n开始处理:{video_file}") try: - # 模型推理 - inputs = [np.vstack(frame_tensors[i:i+2])[None], desire, state] - outs = supercombo.predict(inputs, verbose=0) - parsed = parser(outs) - state = outs[-1] - - - lll_line.set_data(parsed["lll"][0], range(192)) # 只更新左车道线数据 - rll_line.set_data(parsed["rll"][0], range(192)) # 只更新右车道线数据 - path_line.set_data(parsed["path"][0], range(192))# 只更新路径数据 - fig.canvas.draw() # 轻量重绘(只更改造变的部分) - fig.canvas.flush_events() # 强制刷新窗口,避免卡住 - # ------------------------------------------------------------------- - - # 显示原始帧(简化版,用Matplotlib显示,避免OpenCV额外窗口) - if i < len(raw_frames): - # 新建一个小窗口显示原始帧,减少渲染压力 - cv2.imshow("原始帧", cv2.resize(raw_frames[i], (480, 270))) # 缩小尺寸 - if cv2.waitKey(100) & 0xFF == ord('q'): # 延长等待时间,给CPU喘息 - print("用户按Q键退出") - break + # 单视频处理逻辑 + frames, writer, (width, height) = read_video(video_file, max_frames) + if not frames: + continue + + fig, ax, img_display = init_visualization(chinese_font, width, height) - print(f"✅ 帧 {i+1}/{len(frame_tensors)-1} 完成") + # 逐帧处理(带进度条) + 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: - print(f"⚠️ 帧 {i+1} 失败:{str(e)}") + logger.error(f"处理{video_file}失败:{str(e)}") continue - print("\n🎉 处理完成!") - plt.ioff() - plt.close() - cv2.destroyAllWindows() +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="最大处理帧数") + 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处理完成!结果视频:{result_video}") + logger.info(f"播放命令:totem {result_video}") + logger.info(f"日志文件:{LOG_FILE}") + return + + logger.error(f"输入无效:{args.input}(请输入视频文件路径或文件夹路径)") + if __name__ == "__main__": - main() + try: + # 校验tqdm是否正确安装并导入 + try: + from tqdm import tqdm # 再次确认导入方式 + except ImportError: + logger.error("缺少依赖tqdm,请安装:pip install tqdm") + sys.exit(1) + main() + except KeyboardInterrupt: + logger.info("程序被用户中断(Ctrl+C)") + except Exception as e: + logger.error(f"程序异常终止:{str(e)}", exc_info=True) # 输出完整异常栈 + sys.exit(1) + From f78180dcd79623d8f93b8520a2d87631a7bc72d5 Mon Sep 17 00:00:00 2001 From: dacun922 <3109383112@qq.com> Date: Thu, 18 Dec 2025 20:23:00 +0800 Subject: [PATCH 2/2] =?UTF-8?q?docs:=20=E4=BC=98=E5=8C=96=E8=BD=A6?= =?UTF-8?q?=E9=81=93=E7=BA=BF=E6=A3=80=E6=B5=8B=E7=A8=8B=E5=BA=8F=E6=B3=A8?= =?UTF-8?q?=E9=87=8A=EF=BC=8C=E8=A7=84=E8=8C=83=E6=96=87=E6=A1=A3=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/openpilot_model/main.py | 280 +++++++++++++++++++++--------------- 1 file changed, 163 insertions(+), 117 deletions(-) diff --git a/src/openpilot_model/main.py b/src/openpilot_model/main.py index 138a40487f..ec34f0b92c 100644 --- a/src/openpilot_model/main.py +++ b/src/openpilot_model/main.py @@ -2,16 +2,17 @@ # -*- coding: utf-8 -*- """ Lane Line Detection Program (Enhanced Version) -优化点: -1. 新增实际车道线检测(边缘检测+霍夫变换),替代硬编码坐标; -2. 日志同时输出到终端+文件,进度条可视化; -3. 支持批量处理、自定义参数、视频编码自适应; -4. 清理冗余代码,函数解耦,鲁棒性增强; -5. 保留原有中文乱码修复、可视化核心逻辑。 -适用场景:Ubuntu系统下的车道线检测/可视化,支持单视频/批量视频处理 -""" +核心功能: +1. 基于边缘检测+霍夫变换实现动态车道线检测,兼容固定坐标绘制降级方案; +2. 支持单视频/批量视频处理,进度条可视化处理状态; +3. 日志双端输出(终端+文件),视频编码自适应,资源自动释放; +4. 修复Matplotlib中文乱码问题,增强程序鲁棒性。 + +适用环境:Ubuntu (Python 3.10 + OpenCV + Matplotlib + tqdm) +""" +# ===================== 模块导入 ===================== import sys import os import time @@ -24,43 +25,50 @@ import numpy as np import matplotlib import matplotlib.pyplot as plt -import matplotlib.image as mpimg # 导入image模块,修复类型注解 +import matplotlib.image as mpimg from matplotlib.font_manager import FontProperties -from tqdm import tqdm # 关键修改:从tqdm模块导入tqdm函数(而非导入整个模块) - +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) # 自动创建结果目录 +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_HIGH_THRESH = 150 -HOUGH_RHO = 1 -HOUGH_THETA = np.pi / 180 -HOUGH_THRESHOLD = 20 -HOUGH_MIN_LINE_LEN = 40 -HOUGH_MAX_LINE_GAP = 20 - - +# 视频处理参数 +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: - """初始化日志:同时输出到终端和文件""" + """初始化日志处理器:同时输出到终端和文件 + + Returns: + logging.Logger: 配置完成的日志实例 + """ logger = logging.getLogger("LaneDetection") logger.setLevel(logging.INFO) - logger.handlers.clear() # 避免重复添加处理器 + logger.handlers.clear() # 清除重复处理器 - # 终端处理器 + # 终端日志处理器 stream_handler = logging.StreamHandler() stream_formatter = logging.Formatter( "%(asctime)s - %(levelname)s - %(message)s", @@ -68,7 +76,7 @@ def setup_logger() -> logging.Logger: ) stream_handler.setFormatter(stream_formatter) - # 文件处理器 + # 文件日志处理器(UTF-8编码) file_handler = logging.FileHandler(LOG_FILE, encoding="utf-8") file_formatter = logging.Formatter( "%(asctime)s - %(name)s - %(levelname)s - %(message)s", @@ -82,13 +90,17 @@ def setup_logger() -> logging.Logger: logger = setup_logger() - +# ===================== 环境初始化 ===================== def init_environment() -> FontProperties: + """初始化Matplotlib环境,修复中文显示乱码 + + Returns: + FontProperties: 加载完成的中文字体实例 + + Raises: + SystemExit: 字体文件不存在或加载失败时退出程序 """ - 初始化Matplotlib环境,修复中文乱码 - 返回:加载好的中文字体对象 - """ - # 适配GUI/无GUI后端 + # 适配GUI/无GUI环境的Matplotlib后端 if os.environ.get('DISPLAY') is None: matplotlib.use('Agg') logger.info("Matplotlib后端:Agg (无GUI模式)") @@ -102,7 +114,7 @@ def init_environment() -> FontProperties: logger.error("安装命令:sudo apt install fonts-wqy-microhei") sys.exit(1) - # 加载字体 + # 加载中文字体 try: chinese_font = FontProperties(fname=CHINESE_FONT_PATH, size=12) logger.info("中文字体加载成功") @@ -111,52 +123,58 @@ def init_environment() -> FontProperties: logger.error(f"字体加载失败:{str(e)}") sys.exit(1) - +# ===================== 视频读取 ===================== def read_video( video_path: str, max_frames: int = DEFAULT_MAX_FRAMES ) -> Tuple[List[np.ndarray], Optional[cv2.VideoWriter], Tuple[int, int]]: + """读取视频帧并初始化视频写入器 + + Args: + video_path: 视频文件路径 + max_frames: 最大读取帧数 + + Returns: + Tuple[List[np.ndarray], Optional[cv2.VideoWriter], Tuple[int, int]]: + - 读取到的视频帧列表 + - 初始化后的视频写入器(失败则为None) + - 视频分辨率(width, height) """ - 读取视频帧,初始化视频写入器 - 返回:帧列表、视频写入器、视频分辨率 - """ - # 校验视频文件 + # 校验视频文件存在性 if not os.path.exists(video_path): logger.error(f"视频文件不存在:{video_path}") return [], None, (0, 0) - # 打开视频(用with语句自动释放资源) + # 打开视频流 cap = cv2.VideoCapture(video_path) if not cap.isOpened(): - logger.error(f"视频打开失败:{video_path} (检查FFmpeg安装)") + 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') + 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}") + logger.error(f"视频写入器初始化失败:{result_video_path}") cap.release() return [], None, (width, height) - # 读取帧(带进度条) + # 读取视频帧(带进度条) frames = [] count = 0 logger.info(f"开始读取视频帧(最大{max_frames}帧)...") - # 修复tqdm调用方式(已正确导入tqdm函数) with tqdm(total=max_frames, desc="读取帧") as pbar: while cap.isOpened() and count < max_frames: ret, frame = cap.read() @@ -167,30 +185,34 @@ def read_video( pbar.update(1) cap.release() - logger.info(f"读取完成:共{len(frames)}帧") + logger.info(f"视频帧读取完成:共读取 {len(frames)} 帧") return frames, writer, (width, height) - +# ===================== 车道线检测 ===================== def detect_lane_lines( frame: np.ndarray, mode: str = "dynamic" ) -> np.ndarray: - """ - 检测/绘制车道线 - mode: dynamic=动态检测(边缘+霍夫),fixed=固定坐标(兼容原有逻辑) - 返回:绘制了车道线的帧 + """检测并绘制车道线 + + Args: + frame: 单帧视频图像(BGR格式) + mode: 检测模式,可选值:dynamic/fixed + + 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) + 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) @@ -198,7 +220,7 @@ def detect_lane_lines( blur = cv2.GaussianBlur(gray, (5, 5), 0) # 3. 边缘检测 edges = cv2.Canny(blur, CANNY_LOW_THRESH, CANNY_HIGH_THRESH) - # 4. 区域掩码(只检测下半部分,聚焦车道) + # 4. 区域掩码(仅检测图像下半部分,聚焦车道区域) mask = np.zeros_like(edges) polygon = np.array([[ (0, h), @@ -223,7 +245,7 @@ def detect_lane_lines( 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) + cv2.line(frame_copy, (x1, y1), (x2, y2), (0, 255, 0), 3) # 车道线(绿) line_count += 1 # 7. 标注检测到的车道线数量 @@ -239,20 +261,32 @@ def detect_lane_lines( return frame_copy except Exception as e: - logger.warning(f"车道线检测失败:{str(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]: # 正确的类型注解 - """初始化可视化窗口,添加增强标注""" - plt.ion() +) -> Tuple[plt.Figure, plt.Axes, mpimg.AxesImage]: + """初始化Matplotlib可视化窗口 + + Args: + chinese_font: 中文字体实例 + width: 视频宽度 + height: 视频高度 + + Returns: + Tuple[plt.Figure, plt.Axes, mpimg.AxesImage]: + - 可视化画布实例 + - 坐标轴实例 + - 图像显示对象 + """ + plt.ion() # 开启交互模式 fig, ax = plt.subplots(figsize=(12, 8)) fig.suptitle( "车道线检测结果(叠加可视化)", @@ -261,7 +295,7 @@ def init_visualization( fontsize=16 ) - # 新增:详细标注说明 + # 标注说明文字 ax.text( 0.02, 0.95, "动态检测车道线(绿色)| 固定左车道(蓝色)| 固定右车道(红色)| 按Q退出", @@ -270,25 +304,31 @@ def init_visualization( bbox=dict(facecolor='black', alpha=0.8, boxstyle='round,pad=0.5'), fontproperties=chinese_font ) - ax.axis('off') + 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" ): - """批量处理指定目录下的所有视频文件""" + """批量处理指定目录下的所有视频文件 + + 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) + # 筛选视频文件(支持mp4/avi/mov) video_extensions = (".mp4", ".avi", ".mov") video_files = [ os.path.join(input_dir, f) @@ -297,31 +337,33 @@ def batch_process( ] if not video_files: - logger.info(f"目录{input_dir}下未找到视频文件") + logger.info(f"目录{input_dir}下未检测到视频文件(支持格式:{video_extensions})") return - logger.info(f"批量处理:共{len(video_files)}个视频") + logger.info(f"批量处理启动:共检测到 {len(video_files)} 个视频文件") chinese_font = init_environment() + # 逐视频处理 for video_file in video_files: - logger.info(f"\n开始处理:{video_file}") + 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", @@ -335,12 +377,12 @@ def batch_process( 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退出 + # 按Q键退出 if cv2.waitKey(10) & 0xFF == ord('q'): - logger.info("用户按Q键退出") + logger.info("用户按下Q键,终止处理") break pbar.update(1) @@ -350,48 +392,50 @@ def batch_process( if writer: writer.release() cv2.destroyAllWindows() - logger.info(f"处理完成:{video_file} → 结果保存到{RESULT_DIR}") + logger.info(f"视频处理完成:{video_file} → 结果保存至 {RESULT_DIR}") except Exception as e: - logger.error(f"处理{video_file}失败:{str(e)}") + logger.error(f"视频处理失败:{video_file} | 错误信息:{str(e)}") continue - +# ===================== 主函数 ===================== 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="最大处理帧数") + 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=固定坐标") + 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", @@ -405,46 +449,48 @@ def main(): 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退出 + # 按Q键退出 if cv2.waitKey(20) & 0xFF == ord('q'): - logger.info("用户按Q键退出") + logger.info("用户按下Q键,终止处理") break pbar.update(1) # 释放资源 - logger.info("释放资源...") + 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处理完成!结果视频:{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}(请输入视频文件路径或文件夹路径)") - + # 输入路径无效 + logger.error(f"输入路径无效:{args.input}(请输入视频文件路径或文件夹路径)") +# ===================== 程序入口 ===================== if __name__ == "__main__": try: - # 校验tqdm是否正确安装并导入 - try: - from tqdm import tqdm # 再次确认导入方式 - except ImportError: - logger.error("缺少依赖tqdm,请安装:pip install tqdm") - sys.exit(1) + # 校验tqdm依赖 + from tqdm import tqdm + except ImportError: + logger.error("缺少依赖库tqdm,请执行安装:pip install tqdm") + sys.exit(1) + + try: main() except KeyboardInterrupt: logger.info("程序被用户中断(Ctrl+C)") except Exception as e: - logger.error(f"程序异常终止:{str(e)}", exc_info=True) # 输出完整异常栈 + logger.error(f"程序异常终止:{str(e)}", exc_info=True) sys.exit(1) -