# vision **Repository Path**: elfbobo_admin_admin/vision ## Basic Information - **Project Name**: vision - **Description**: 基于 YOLOv8 的多功能视觉识别项目,支持车道线检测、车辆检测、行人检测等功能。 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2025-11-19 - **Last Updated**: 2025-11-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 视觉识别系统 - Vision Detection System 基于 YOLOv8 的多功能视觉识别项目,支持车道线检测、车辆检测、行人检测等功能。 --- ## 📋 项目简介 本项目采用模块化设计,方便扩展多种视觉识别功能: - ✅ **车道线检测**:基于 YOLOv8 分割模型,提取车道线并计算拟合参数(c0/c1/c2/c3) - 🚧 **车辆检测**(预留) - 🚧 **行人检测**(预留) --- ## 📁 项目结构 ``` vision/ ├── configs/ # 配置文件 │ ├── config.yaml # 全局配置 │ └── lane_detection_config.yaml # 车道线检测配置 │ ├── datasets/ # 数据集目录 │ ├── lane_detection/ # 车道线数据集 │ ├── vehicle_detection/ # 车辆数据集(预留) │ └── pedestrian_detection/ # 行人数据集(预留) │ ├── models/ # 模型文件 │ └── lane_yolov8n-seg.pt # 车道线检测模型(需自行准备) │ ├── src/ # 源代码 │ ├── lane_detection/ # 车道线检测模块 │ │ ├── detector.py # YOLO检测器 │ │ └── processor.py # 车道线拟合处理 │ │ │ ├── vehicle_detection/ # 车辆检测模块(预留) │ ├── pedestrian_detection/ # 行人检测模块(预留) │ │ │ └── utils/ # 通用工具 │ ├── camera.py # 相机坐标转换 │ ├── visualization.py # 可视化工具 │ └── file_utils.py # 文件处理工具 │ ├── scripts/ # 运行脚本 │ └── run_lane_detection.py # 车道线检测主程序 │ ├── outputs/ # 输出结果 │ ├── lane_detection/ # 车道线检测结果 │ ├── vehicle_detection/ # 车辆检测结果(预留) │ └── pedestrian_detection/ # 行人检测结果(预留) │ ├── docs/ # 文档 ├── requirements.txt # 依赖包列表 └── README.md # 本文件 ``` --- ## 🚀 快速开始 ### 1. 环境要求 - Python >= 3.8 - OpenCV - PyTorch - Ultralytics YOLOv8 ### 2. 安装依赖 ```bash pip install -r requirements.txt ``` ### 3. 准备模型 将你的 YOLOv8 车道线分割模型放到 `models/` 目录: ```bash models/lane_yolov8n-seg.pt ``` > ⚠️ **重要提示**: > - 官方 `yolov8n-seg.pt` 模型不包含车道线类别 > - 需要使用**自己训练**或**专用的车道线分割模型** > - 模型必须输出车道线的分割掩码(segmentation masks) ### 4. 配置相机参数(可选) 编辑 `configs/lane_detection_config.yaml`,填入你的相机标定参数: ```yaml camera: intrinsic_matrix: # 相机内参 K (3x3) distortion_coeffs: # 畸变系数 [k1, k2, p1, p2, k3] translation: # 相机位置 [x, y, z] rotation_matrix: # 旋转矩阵 R (3x3) ``` ### 5. 运行车道线检测 #### 检测图片 ```bash python scripts/run_lane_detection.py /path/to/image.jpg ``` #### 检测视频 ```bash python scripts/run_lane_detection.py /path/to/video.mp4 ``` #### 使用摄像头 ```bash python scripts/run_lane_detection.py 0 ``` #### 更多选项 ```bash # 不显示窗口,仅保存结果 python scripts/run_lane_detection.py input.jpg --no-show # 不保存结果,仅实时显示 python scripts/run_lane_detection.py video.mp4 --no-save # 自定义配置文件 python scripts/run_lane_detection.py input.jpg --config my_config.yaml # 自定义输出目录 python scripts/run_lane_detection.py input.jpg --output /path/to/output ``` --- ## 📊 车道线检测原理 ### 1. 检测流程 ``` 输入图像 ↓ YOLOv8 分割检测 ↓ 提取车道线掩码 ↓ 转换为车身坐标系点集 ↓ 三次多项式拟合 ↓ 计算参数 c0/c1/c2/c3 ↓ 绘制曲线和参数 ``` ### 2. 参数说明 拟合方程:**y = c0 + c1·x + c2·x² + c3·x³** - **c0**:横向距离(米) 车辆正前方(x=0)处的横向偏移量 - **c1**:航向角(弧度) 车道线在起点的切线方向,`航向角 = arctan(c1)` - **c2**:曲率(1/米) 车道线的弯曲程度,越大表示转弯越急 - **c3**:曲率变化率(1/米²) 曲率随距离的变化速度 ### 3. 坐标系说明 - **图像坐标系**:像素 (u, v) - **车身坐标系**: - X轴:车辆前向 - Y轴:车辆左向 - Z轴:垂直向上 - 原点:车辆后轴中心 --- ## ⚙️ 配置文件详解 ### 全局配置 (`configs/config.yaml`) ```yaml project: name: "vision_detection_system" version: "1.0.0" models: lane_detection: "models/lane_yolov8n-seg.pt" detection: conf_threshold: 0.4 # 置信度阈值 device: "cpu" # cpu / cuda / mps visualization: show_real_time: true colors: lane_left: [0, 0, 255] # 左车道线:红色 lane_right: [255, 0, 0] # 右车道线:蓝色 ``` ### 车道线检测配置 (`configs/lane_detection_config.yaml`) ```yaml # 模型配置 model: weights: "models/lane_yolov8n-seg.pt" conf_threshold: 0.4 # 车道线类别过滤 lane_classes: class_ids: [] # 例如 [0, 1] class_names: # 例如 ["lane", "laneline"] - "lane" # 相机标定参数 camera: intrinsic_matrix: [[1200, 0, 640], [0, 1200, 360], [0, 0, 1]] distortion_coeffs: [0, 0, 0, 0, 0] translation: [0.5, 0, 1.5] rotation_matrix: [[0, 0, 1], [-1, 0, 0], [0, -1, 0]] # 拟合参数 lane_fitting: polynomial_degree: 3 min_points: 4 x_range: [0, 50] # 车前方有效范围(米) y_range: [-5, 5] # 左右有效范围(米) ``` --- ## 🔧 自定义开发 ### 添加新的检测功能 1. 在 `src/` 下创建新模块(如 `vehicle_detection/`) 2. 实现检测器和处理器类 3. 在 `scripts/` 创建运行脚本 4. 在 `configs/` 添加配置文件 ### 示例:添加车辆检测 ```python # src/vehicle_detection/detector.py from ultralytics import YOLO class VehicleDetector: def __init__(self, model_path): self.model = YOLO(model_path) def detect(self, img): results = self.model(img) return results ``` --- ## 📝 常见问题 ### Q1: 为什么检测不到车道线? **A**: 请检查: 1. 模型是否包含车道线类别(官方模型不支持) 2. `lane_classes` 配置是否正确 3. 输入图像是否清晰、视角合适 4. 置信度阈值是否过高 ### Q2: 如何获取车道线检测模型? **A**: 三种方式: 1. 使用开源数据集(TuSimple、CULane等)自行训练 2. 使用已有的车道线分割模型转换为 YOLOv8 格式 3. 收集数据并使用 YOLOv8 训练分割模型 ### Q3: 参数 c0/c1/c2/c3 都是 0? **A**: 说明没有检测到车道线点,检查: 1. 模型是否正确加载 2. 类别过滤配置是否正确 3. 图像中是否确实存在车道线 ### Q4: 如何调整相机参数? **A**: 需要进行相机标定: 1. 使用 OpenCV 的棋盘格标定方法 2. 获取内参矩阵 K 和畸变系数 3. 测量相机在车身的安装位置和角度 4. 更新配置文件 --- ## 🎓 模型训练 ### 训练自己的车道线检测模型 本项目提供完整的模型训练功能,从数据准备到模型导出一站式解决。 #### 快速开始 1. **准备数据集** ```bash # 自动划分训练集/验证集 python scripts/prepare_dataset.py /path/to/raw_data \ --output datasets/lane_detection \ --train-ratio 0.8 \ --validate ``` 2. **配置训练参数** 编辑 `configs/train_config.yaml`: ```yaml base_model: weights: "yolov8n-seg.pt" # 预训练模型 training: epochs: 100 batch: 16 device: "0" # GPU设备 ``` 3. **开始训练** ```bash # 使用默认配置训练 python scripts/train_lane_detection.py # 使用TensorBoard监控 tensorboard --logdir runs/train ``` 4. **验证和导出** ```bash # 验证模型 python scripts/train_lane_detection.py --mode val # 导出为ONNX python scripts/train_lane_detection.py --mode export --format onnx ``` #### 详细教程 完整的训练指南请参考:[docs/training_guide.md](docs/training_guide.md) 包括: - 数据标注格式详解 - 超参数调优技巧 - 多GPU训练配置 - 常见问题解决方案 - 公开数据集推荐 --- ## 📚 参考资料 - [YOLOv8 官方文档](https://docs.ultralytics.com/) - [OpenCV 相机标定教程](https://docs.opencv.org/4.x/dc/dbb/tutorial_py_calibration.html) - [车道线检测数据集](https://github.com/amusi/awesome-lane-detection) --- ## 📄 许可证 本项目仅供学习研究使用。 --- ## 🤝 贡献 欢迎提交 Issue 和 Pull Request! --- ## 📧 联系方式 如有问题,请通过 Issue 反馈。 --- **祝你使用愉快!🎉**