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) diff --git a/src/openpilot_model/main.py b/src/openpilot_model/main.py index ec34f0b92c..b0ff694165 100644 --- a/src/openpilot_model/main.py +++ b/src/openpilot_model/main.py @@ -1,496 +1,415 @@ #!/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, matplotlib """ -# ===================== 模块导入 ===================== -import sys -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 argparse +import os +import time import matplotlib.pyplot as plt -import matplotlib.image as mpimg -from matplotlib.font_manager import FontProperties -from tqdm import tqdm +from pathlib import Path -# ===================== 全局配置 ===================== -# 项目根路径 -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" +# 1. 检测参数(全流程关键参数,不省略) +# 颜色阈值(HLS空间,筛选白色/黄色车道线) +WHITE_LOW = np.array([0, 200, 0], dtype=np.uint8) +WHITE_HIGH = np.array([255, 255, 255], dtype=np.uint8) +YELLOW_LOW = np.array([10, 0, 100], dtype=np.uint8) +YELLOW_HIGH = np.array([40, 255, 255], dtype=np.uint8) +# Canny边缘检测 +CANNY_LOW_THRESH = 50 +CANNY_HIGH_THRESH = 150 +# 滑动窗口拟合参数 +WINDOW_NUM = 9 +WINDOW_MARGIN = 100 +MIN_PIXELS = 50 +# 透视变换参数(鸟瞰视角) +SRC_POINTS = np.float32([[200, 720], [1100, 720], [595, 450], [685, 450]]) +DST_POINTS = np.float32([[300, 720], [980, 720], [300, 0], [980, 0]]) +# 物理尺寸转换(像素→米) +YM_PER_PIX = 30 / 720 # 纵向:30米/720像素 +XM_PER_PIX = 3.7 / 700 # 横向:3.7米/700像素 + +# 2. 可视化参数(高级且信息完整) +COLOR_LEFT_LANE = (0, 0, 255) # 左车道:红 +COLOR_RIGHT_LANE = (255, 0, 0) # 右车道:蓝 +COLOR_LANE_AREA = (0, 255, 0) # 车道区域:绿(半透明) +COLOR_TEXT = (255, 255, 255) # 文本:白 +FONT = cv2.FONT_HERSHEY_SIMPLEX +FONT_SCALE = 0.8 +FONT_THICKNESS = 2 +PANEL_ALPHA = 0.7 # 信息面板透明度 +FRAME_DELAY = 300 # 每帧显示时间(ms) + + +def preprocess_frame(frame): + """完整预处理流程:颜色阈值过滤→灰度→高斯模糊→边缘检测""" + # 1. 转换为HLS颜色空间,筛选白色/黄色车道线 + hls = cv2.cvtColor(frame, cv2.COLOR_BGR2HLS) + white_mask = cv2.inRange(hls, WHITE_LOW, WHITE_HIGH) + yellow_mask = cv2.inRange(hls, YELLOW_LOW, YELLOW_HIGH) + color_mask = cv2.bitwise_or(white_mask, yellow_mask) + + # 2. 灰度化 + gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) + + # 3. 高斯模糊去噪 + blur = cv2.GaussianBlur(gray, (5, 5), 0) + + # 4. Canny边缘检测 + edges = cv2.Canny(blur, CANNY_LOW_THRESH, CANNY_HIGH_THRESH) + + # 5. 融合颜色掩码和边缘检测结果(提升鲁棒性) + combined = cv2.bitwise_and(edges, color_mask) + + return combined -# 中文字体路径(微黑字体) -CHINESE_FONT_PATH = "/usr/share/fonts/truetype/wqy/wqy-microhei.ttc" -# 视频处理参数 -DEFAULT_MAX_FRAMES = 10 # 默认最大处理帧数 -FPS = 25 # 默认帧率(自适应原视频帧率) -DEFAULT_DETECT_MODE = "dynamic" # 默认检测模式:dynamic-动态检测,fixed-固定坐标 +def warp_frame(frame): + """透视变换:将图像转换为鸟瞰视角,便于检测车道线""" + h, w = frame.shape[:2] + M = cv2.getPerspectiveTransform(SRC_POINTS, DST_POINTS) + Minv = cv2.getPerspectiveTransform(DST_POINTS, SRC_POINTS) + warped = cv2.warpPerspective(frame, M, (w, h), flags=cv2.INTER_LINEAR) + return warped, Minv + +def fit_lane_lines(binary_warped): + """滑动窗口法拟合车道线,计算车道线参数+曲率+偏移""" + h, w = binary_warped.shape + + # 1. 计算底部直方图,找到车道线起始点 + histogram = np.sum(binary_warped[h//2:, :], axis=0) + midpoint = w // 2 + left_x_base = np.argmax(histogram[:midpoint]) + right_x_base = np.argmax(histogram[midpoint:]) + midpoint + + # 2. 滑动窗口初始化 + window_height = h // WINDOW_NUM + left_x_current = left_x_base + right_x_current = right_x_base + + # 3. 收集车道线像素点 + left_lane_inds = [] + right_lane_inds = [] + nonzero = binary_warped.nonzero() + nonzeroy = np.array(nonzero[0]) + nonzerox = np.array(nonzero[1]) + + # 4. 滑动窗口遍历 + for window in range(WINDOW_NUM): + # 计算窗口边界 + win_y_low = h - (window + 1) * window_height + win_y_high = h - window * window_height + win_xleft_low = left_x_current - WINDOW_MARGIN + win_xleft_high = left_x_current + WINDOW_MARGIN + win_xright_low = right_x_current - WINDOW_MARGIN + win_xright_high = right_x_current + WINDOW_MARGIN + + # 收集窗口内的像素点 + good_left_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) & + (nonzerox >= win_xleft_low) & (nonzerox < win_xleft_high)).nonzero()[0] + good_right_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) & + (nonzerox >= win_xright_low) & (nonzerox < win_xright_high)).nonzero()[0] + + left_lane_inds.append(good_left_inds) + right_lane_inds.append(good_right_inds) + + # 更新窗口中心 + if len(good_left_inds) > MIN_PIXELS: + left_x_current = int(np.mean(nonzerox[good_left_inds])) + if len(good_right_inds) > MIN_PIXELS: + right_x_current = int(np.mean(nonzerox[good_right_inds])) + + # 合并像素点 + left_lane_inds = np.concatenate(left_lane_inds) + right_lane_inds = np.concatenate(right_lane_inds) + + # 提取车道线像素坐标 + left_x = nonzerox[left_lane_inds] + left_y = nonzeroy[left_lane_inds] + right_x = nonzerox[right_lane_inds] + right_y = nonzeroy[right_lane_inds] + + # 5. 多项式拟合(2次多项式) + left_fit = np.polyfit(left_y, left_x, 2) if len(left_x) > 0 else None + right_fit = np.polyfit(right_y, right_x, 2) if len(right_x) > 0 else None + + # 6. 生成拟合线的x/y坐标 + ploty = np.linspace(0, h - 1, h) + left_fitx = None + right_fitx = None + if left_fit is not None: + left_fitx = left_fit[0] * ploty**2 + left_fit[1] * ploty + left_fit[2] + if right_fit is not None: + right_fitx = right_fit[0] * ploty**2 + right_fit[1] * ploty + right_fit[2] + + # 7. 计算曲率(物理尺寸,关键指标不省略) + left_curverad, right_curverad = calculate_curvature(left_x, left_y, right_x, right_y) + + # 8. 计算车道偏移(车辆相对于车道中心的偏移) + center_offset = calculate_offset(left_fit, right_fit, w, h) + + return left_fitx, right_fitx, ploty, left_curverad, right_curverad, center_offset + +def calculate_curvature(left_x, left_y, right_x, right_y): + """计算车道线曲率(单位:米)""" + h = 720 # 参考高度 + # 转换为物理尺寸 + left_fit_cr = np.polyfit(left_y * YM_PER_PIX, left_x * XM_PER_PIX, 2) if len(left_x) > 0 else None + right_fit_cr = np.polyfit(right_y * YM_PER_PIX, right_x * XM_PER_PIX, 2) if len(right_x) > 0 else None + + # 计算曲率 + left_curverad = ((1 + (2 * left_fit_cr[0] * h * YM_PER_PIX + left_fit_cr[1])**2)**1.5) / np.absolute(2 * left_fit_cr[0]) if left_fit_cr is not None else 0 + right_curverad = ((1 + (2 * right_fit_cr[0] * h * YM_PER_PIX + right_fit_cr[1])**2)**1.5) / np.absolute(2 * right_fit_cr[0]) if right_fit_cr is not None else 0 + + return left_curverad, right_curverad -# 车道线检测参数(霍夫变换+边缘检测) -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 calculate_offset(left_fit, right_fit, frame_width, frame_height): + """计算车辆相对于车道中心的偏移(单位:米)""" + if left_fit is None or right_fit is None: + return 0.0 + + # 计算底部车道线的x坐标 + y_eval = frame_height - 1 + left_x = left_fit[0] * y_eval**2 + left_fit[1] * y_eval + left_fit[2] + right_x = right_fit[0] * y_eval**2 + right_fit[1] * y_eval + right_fit[2] + + # 车道中心x坐标 + lane_center_x = (left_x + right_x) / 2 + # 车辆中心x坐标(假设在图像中心) + car_center_x = frame_width / 2 + + # 转换为物理偏移 + offset = (car_center_x - lane_center_x) * XM_PER_PIX + return offset -# ===================== 日志初始化 ===================== -def setup_logger() -> logging.Logger: - """初始化日志处理器:同时输出到终端和文件 - - 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) - # 文件日志处理器(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" - ) - file_handler.setFormatter(file_formatter) +def draw_lane(frame, warped, Minv, left_fitx, right_fitx, ploty): + """绘制拟合后的车道线+车道区域""" + h, w = frame.shape[:2] + # 1. 创建车道区域蒙版 + warp_zero = np.zeros_like(warped).astype(np.uint8) + color_warp = np.dstack((warp_zero, warp_zero, warp_zero)) + + # 2. 构建车道区域多边形 + if left_fitx is not None and right_fitx is not None: + pts_left = np.array([np.transpose(np.vstack([left_fitx, ploty]))]) + pts_right = np.array([np.flipud(np.transpose(np.vstack([right_fitx, ploty])))]) + pts = np.hstack((pts_left, pts_right)) + + # 绘制车道区域(半透明) + cv2.fillPoly(color_warp, np.int_([pts]), COLOR_LANE_AREA) + # 绘制车道线 + cv2.polylines(color_warp, np.int_([pts_left]), isClosed=False, color=COLOR_LEFT_LANE, thickness=10) + cv2.polylines(color_warp, np.int_([pts_right]), isClosed=False, color=COLOR_RIGHT_LANE, thickness=10) + + # 3. 逆透视变换,将车道线投影回原图 + newwarp = cv2.warpPerspective(color_warp, Minv, (w, h)) + # 4. 融合原图和车道线 + result = cv2.addWeighted(frame, 1, newwarp, 0.3, 0) + + return result - logger.addHandler(stream_handler) - logger.addHandler(file_handler) - return logger -logger = setup_logger() +def draw_info_panel(frame, frame_idx, total_frames, fps, left_curv, right_curv, offset): + """绘制完整信息面板:包含帧数、FPS、曲率、偏移等关键指标""" + h, w = frame.shape[:2] + # 黑色半透明背景 + panel = np.zeros_like(frame) + cv2.rectangle(panel, (10, 10), (400, 250), (0, 0, 0), -1) + frame = cv2.addWeighted(panel, PANEL_ALPHA, frame, 1 - PANEL_ALPHA, 0) + + # 完整信息文本 + info_texts = [ + f"Frame: {frame_idx}/{total_frames}", + f"FPS: {fps:.1f}", + f"Left Lane Curvature: {left_curv:.1f} m", + f"Right Lane Curvature: {right_curv:.1f} m", + f"Center Offset: {offset:.2f} m", + "=== Key Controls ===", + "Space: Single Step (Next Frame)", + "P: Pause/Resume", + "Q: Quit" + ] + + y_offset = 40 + for text in info_texts: + cv2.putText( + frame, text, (20, y_offset), FONT, + FONT_SCALE, COLOR_TEXT, FONT_THICKNESS + ) + y_offset += 30 + + return frame -# ===================== 环境初始化 ===================== -def init_environment() -> FontProperties: - """初始化Matplotlib环境,修复中文显示乱码 - - Returns: - FontProperties: 加载完成的中文字体实例 - - 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) +def create_split_screen(original, detected, warped): + """三分屏:原图 + 检测结果 + 鸟瞰视角(功能齐全,不省略)""" + h, w = original.shape[:2] + # 统一尺寸 + detected = cv2.resize(detected, (w, h), interpolation=cv2.INTER_LINEAR) + warped = cv2.cvtColor(warped, cv2.COLOR_GRAY2BGR) # 转换为彩色 + warped = cv2.resize(warped, (w, h), interpolation=cv2.INTER_LINEAR) + + # 拼接分屏(左:原图,中:检测结果,右:鸟瞰视角) + top_row = np.hstack((original, detected)) + bottom_row = np.hstack((warped, np.zeros_like(original))) + split_frame = np.vstack((top_row, bottom_row))[:h*2, :w*2] # 裁剪为2x2布局 + + # 添加标题 + cv2.putText(split_frame, "Original", (20, 30), FONT, 1, COLOR_TEXT, 3) + cv2.putText(split_frame, "Lane Detection", (w + 20, 30), FONT, 1, COLOR_TEXT, 3) + cv2.putText(split_frame, "Bird's Eye View", (20, h + 30), FONT, 1, COLOR_TEXT, 3) + + return split_frame - # 加载中文字体 - 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) -# ===================== 视频读取 ===================== -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) - """ - # 校验视频文件存在性 +def process_video(video_path, max_frames=10): + """完整视频处理流程:预处理→透视变换→拟合→绘制→可视化""" + # 1. 校验文件 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 + + # 2. 打开视频流 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}") - - # 初始化视频写入器(输出带车道线的视频) + print(f"Error: Failed to open video → {video_path}") + return + + # 3. 获取视频基础信息 + 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) + + # 4. 初始化结果保存(支持分屏尺寸) 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: + result_path = f"{video_name}_lane_detected_full.mp4" + fourcc = cv2.VideoWriter_fourcc(*'mp4v') + writer = cv2.VideoWriter(result_path, fourcc, fps, (w*2, h*2)) + + # 5. 控制变量(功能齐全,交互完整) + paused = False + step_mode = False + frame_idx = 0 + print("=== Lane Detection (Full Version) ===") + print("Key Controls:") + print(" Space: Single Step (Next Frame)") + print(" P: Pause/Resume") + print(" Q: Quit") + + # 6. 创建可调整窗口(高级可视化) + cv2.namedWindow("Lane Detection (Full Screen)", cv2.WINDOW_NORMAL) + cv2.resizeWindow("Lane Detection (Full Screen)", w*2, h*2) + + # 7. 全流程处理(步骤无省略) + 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 - frames.append(frame) - count += 1 - pbar.update(1) - + + # 步骤1:预处理 + preprocessed = preprocess_frame(frame) + + # 步骤2:透视变换 + warped, Minv = warp_frame(preprocessed) + + # 步骤3:车道线拟合 + left_fitx, right_fitx, ploty, left_curv, right_curv, offset = fit_lane_lines(warped) + + # 步骤4:绘制车道线 + detected_frame = draw_lane(frame, warped, Minv, left_fitx, right_fitx, ploty) + + # 步骤5:绘制信息面板 + start_time = time.time() + fps_real = 1 / (time.time() - start_time) + detected_frame = draw_info_panel( + detected_frame, frame_idx+1, total_frames, + fps_real, left_curv, right_curv, offset + ) + + # 步骤6:分屏可视化 + split_frame = create_split_screen(frame, detected_frame, warped) + + # 保存结果 + writer.write(split_frame) + frame_idx += 1 + + # 显示窗口 + cv2.imshow("Lane Detection (Full 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 + + # 8. 释放资源(完整收尾,不省略) cap.release() - logger.info(f"视频帧读取完成:共读取 {len(frames)} 帧") - return frames, writer, (width, height) + writer.release() + cv2.destroyAllWindows() + print(f"\n=== Process Finished ===") + print(f"Result Video: {result_path}") + print(f"Key Metrics: Curvature (m), Offset (m)") -# ===================== 车道线检测 ===================== -def detect_lane_lines( - frame: np.ndarray, - mode: str = "dynamic" -) -> np.ndarray: - """检测并绘制车道线 - - 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) # 中心线(绿) - 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可视化窗口 - - Args: - chinese_font: 中文字体实例 - width: 视频宽度 - height: 视频高度 - - 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" -): - """批量处理指定目录下的所有视频文件 - - Args: - input_dir: 视频目录路径 - max_frames: 单视频最大处理帧数 - detect_mode: 检测模式(dynamic/fixed) - """ +def batch_process(input_dir, max_frames=10): + """批量处理视频,功能扩展不省略""" if not os.path.isdir(input_dir): - logger.error(f"输入目录不存在:{input_dir}") + print(f"Error: Input directory not found → {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) - ] - + 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})") + print(f"No video files found in {input_dir} (supported: {video_extensions})") return - - logger.info(f"批量处理启动:共检测到 {len(video_files)} 个视频文件") - chinese_font = init_environment() - - # 逐视频处理 + + print(f"Batch processing: {len(video_files)} videos") 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) + print(f"\nProcessing: {video_file}") + process_video(video_file, max_frames) - # 释放资源 - 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)}") - 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="最大处理帧数(默认: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 (Full Version - Complete Steps)") + parser.add_argument("input", type=str, help="Video file path or directory (batch mode)") + parser.add_argument("--max-frames", type=int, default=10, help="Max frames to process (default: 10)") + parser.add_argument("--batch", action="store_true", help="Batch process directory") + 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 → 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) + # 执行处理(功能齐全,支持单/批量) + if args.batch or os.path.isdir(args.input): + batch_process(args.input, args.max_frames) + else: + process_video(args.input, args.max_frames)