From 16016b1fc5b1abd776b5be8f554a68fbe4b16d55 Mon Sep 17 00:00:00 2001 From: abc123335 <1607504213@qq.com> Date: Fri, 19 Dec 2025 21:25:25 +0800 Subject: [PATCH 01/11] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86=E4=B8=BB?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lane_visualize/main.py | 133 ++++++++++++++++++++----------------- 1 file changed, 72 insertions(+), 61 deletions(-) diff --git a/src/lane_visualize/main.py b/src/lane_visualize/main.py index dde08e0182..0415c06c85 100644 --- a/src/lane_visualize/main.py +++ b/src/lane_visualize/main.py @@ -2,24 +2,27 @@ import numpy as np import sys from collections import deque +import time + +# 引入我们刚才写的 YOLO 模块 +from yolo_det import ObjectDetector # ========================================== -# 👇 请在这里填入你在 Tuner 中调出的“最好”的数值 +# 👇 参数配置区 (填入你之前 Tuner 调好的参数) # ========================================== -CANNY_LOW = 50 # 你的 Canny Low -CANNY_HIGH = 150 # 你的 Canny High +CANNY_LOW = 50 +CANNY_HIGH = 150 +ROI_TOP_WIDTH = 0.40 +ROI_HEIGHT_POS = 0.60 +HOUGH_THRESH = 20 +MIN_LINE_LEN = 20 +MAX_LINE_GAP = 100 -ROI_TOP_WIDTH = 0.40 # 你的 ROI Top W (例如滑动条是40,这里写 0.40) -ROI_HEIGHT_POS = 0.60 # 你的 ROI Height (例如滑动条是60,这里写 0.60) -HOUGH_THRESH = 20 # 你的 Hough Thresh -MIN_LINE_LEN = 20 # 你的 Min Length -MAX_LINE_GAP = 100 # 你的 Max Gap # ========================================== class LaneDetector: def __init__(self): - # 历史缓存 (用于平滑防抖) self.left_lines_buffer = deque(maxlen=10) self.right_lines_buffer = deque(maxlen=10) self.vertices = None @@ -28,51 +31,40 @@ def region_of_interest(self, img): mask = np.zeros_like(img) if self.vertices is None: height, width = img.shape - - # 使用填入的参数计算梯形 top_w = width * ROI_TOP_WIDTH top_x_center = width * 0.5 - - # 梯形四个顶点 - bl = (0, height) # 左下 - tl = (int(top_x_center - top_w/2), int(height * ROI_HEIGHT_POS)) # 左上 - tr = (int(top_x_center + top_w/2), int(height * ROI_HEIGHT_POS)) # 右上 - br = (width, height) # 右下 - + bl = (0, height) + tl = (int(top_x_center - top_w / 2), int(height * ROI_HEIGHT_POS)) + tr = (int(top_x_center + top_w / 2), int(height * ROI_HEIGHT_POS)) + br = (width, height) self.vertices = np.array([[bl, tl, tr, br]], dtype=np.int32) - cv2.fillPoly(mask, self.vertices, 255) return cv2.bitwise_and(img, mask) - def process_frame(self, frame): - if frame is None: return None + def process_lane(self, frame): + if frame is None: return frame - # 1. 图像预处理 + # 1. 边缘检测 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) blur = cv2.GaussianBlur(gray, (5, 5), 0) - - # 2. 边缘检测 (使用你的参数) edges = cv2.Canny(blur, CANNY_LOW, CANNY_HIGH) - - # 3. ROI 裁剪 + + # 2. ROI roi = self.region_of_interest(edges) - - # 4. 霍夫变换 (使用你的参数) - lines = cv2.HoughLinesP(roi, 1, np.pi/180, HOUGH_THRESH, - minLineLength=MIN_LINE_LEN, + + # 3. 霍夫变换 + lines = cv2.HoughLinesP(roi, 1, np.pi / 180, HOUGH_THRESH, + minLineLength=MIN_LINE_LEN, maxLineGap=MAX_LINE_GAP) - - # 5. 计算平均线 + + # 4. 计算与平滑 left_raw, right_raw = self.average_slope_intercept(lines) - - # 6. 平滑处理 left_avg, right_avg = self.smooth_lines(left_raw, right_raw) - # 7. 绘制 + # 5. 绘制 height = frame.shape[0] - y_min = int(height * ROI_HEIGHT_POS) + 50 # 稍微画低一点,不要画到消失点 + y_min = int(height * ROI_HEIGHT_POS) + 50 y_max = height - left_pts = self.make_line_points(left_avg, y_min, y_max) right_pts = self.make_line_points(right_avg, y_min, y_max) @@ -82,21 +74,16 @@ def average_slope_intercept(self, lines): left_lines = [] right_lines = [] if lines is None: return None, None - for line in lines: x1, y1, x2, y2 = line[0] if x2 == x1: continue - slope = (y2 - y1) / (x2 - x1) intercept = y1 - slope * x1 - - # 斜率过滤:排除水平线和垂直线 - if abs(slope) < 0.3 or abs(slope) > 10: - continue - - if slope < 0: left_lines.append((slope, intercept)) - else: right_lines.append((slope, intercept)) - + if abs(slope) < 0.3 or abs(slope) > 10: continue + if slope < 0: + left_lines.append((slope, intercept)) + else: + right_lines.append((slope, intercept)) left_avg = np.average(left_lines, axis=0) if left_lines else None right_avg = np.average(right_lines, axis=0) if right_lines else None return left_avg, right_avg @@ -104,7 +91,6 @@ def average_slope_intercept(self, lines): def smooth_lines(self, left_current, right_current): if left_current is not None: self.left_lines_buffer.append(left_current) if right_current is not None: self.right_lines_buffer.append(right_current) - left_smooth = np.average(self.left_lines_buffer, axis=0) if self.left_lines_buffer else None right_smooth = np.average(self.right_lines_buffer, axis=0) if self.right_lines_buffer else None return left_smooth, right_smooth @@ -117,44 +103,69 @@ def make_line_points(self, line, y_min, y_max): x_min = int((y_min - intercept) / slope) x_max = int((y_max - intercept) / slope) return ((x_min, y_min), (x_max, y_max)) - except: return None + except: + return None def draw_lane(self, img, left_pts, right_pts): lane_img = np.zeros_like(img) if left_pts is not None and right_pts is not None: pts = np.array([left_pts[0], left_pts[1], right_pts[1], right_pts[0]], dtype=np.int32) cv2.fillPoly(lane_img, [pts], (0, 255, 0)) - if left_pts: cv2.line(lane_img, left_pts[0], left_pts[1], (255, 0, 0), 10) if right_pts: cv2.line(lane_img, right_pts[0], right_pts[1], (0, 0, 255), 10) - return cv2.addWeighted(img, 1, lane_img, 0.3, 0) + # 注意:这里我们返回 lane_img (纯遮罩),而不是叠加后的图,方便后面统一叠加 + return lane_img + def main(): - # 自动读取 sample.hevc 或摄像头 input_source = sys.argv[1] if len(sys.argv) > 1 else "sample.hevc" cap = cv2.VideoCapture(input_source) if not cap.isOpened(): - print("无法打开视频,请检查路径。") + print("无法打开视频") return - detector = LaneDetector() - print("正在运行... 按 'q' 退出") + # 1. 初始化两个检测器 + lane_detector = LaneDetector() + yolo_detector = ObjectDetector(model_name='yolov8n.pt') # 首次运行会自动下载 6MB 的模型 + + print("系统启动中...") while True: + start_time = time.time() ret, frame = cap.read() - if not ret: - # 循环播放 + if not ret: cap.set(cv2.CAP_PROP_POS_FRAMES, 0) continue - result = detector.process_frame(frame) - cv2.imshow('Final Lane Detection', result) + # ----------------------------------------------------- + # 步骤 A: 运行 YOLO 车辆检测 (返回画好框的图) + # ----------------------------------------------------- + frame_with_cars = yolo_detector.detect(frame) + + # ----------------------------------------------------- + # 步骤 B: 运行车道线检测 (返回纯车道层) + # ----------------------------------------------------- + lane_layer = lane_detector.process_lane(frame) - if cv2.waitKey(20) & 0xFF == ord('q'): + # ----------------------------------------------------- + # 步骤 C: 合并图层 + # ----------------------------------------------------- + # 将半透明车道层 叠加到 画了车的图上 + final_result = cv2.addWeighted(frame_with_cars, 1, lane_layer, 0.4, 0) + + # 计算并显示 FPS + fps = 1.0 / (time.time() - start_time) + cv2.putText(final_result, f"FPS: {fps:.1f}", (20, 50), + cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2) + + cv2.imshow('AutoPilot System V2.0', final_result) + + if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() + if __name__ == "__main__": - main() \ No newline at end of file + main() From 5d83d5e18a4afcd4434b8f1df326591ab774c779 Mon Sep 17 00:00:00 2001 From: abc123335 <1607504213@qq.com> Date: Sat, 20 Dec 2025 02:09:25 +0800 Subject: [PATCH 02/11] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86README.md?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 119 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 83 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index d362f30b07..d3553309e5 100644 --- a/README.md +++ b/README.md @@ -1,54 +1,101 @@ -# 神经网络实现代理 +# AutoPilot System: Lane & Object Detection -利用神经网络/ROS 实现 Carla(车辆、行人的感知、规划、控制)、AirSim、Mujoco 中人和载具的代理。 +这是一个结合了**传统计算机视觉**与**深度学习**的自动驾驶辅助系统原型。它利用 OpenCV 进行高精度的车道线检测,并集成 YOLOv8 模型实现对车辆、行人等障碍物的实时识别。 -## 环境配置 +![Status](https://img.shields.io/badge/Status-Active-success) +![Python](https://img.shields.io/badge/Python-3.8%2B-blue) -* 平台:Windows 10/11,Ubuntu 20.04/22.04 -* 软件:Python 3.7-3.12(需支持3.7)、Pytorch(尽量不使用Tensorflow) -* 相关软件下载 [链接](https://pan.baidu.com/s/1IFhCd8X9lI24oeYQm5-Edw?pwd=hutb) +## ✨ 核心功能 -## 功能模块表 -| 模块类别 | 模块名 | 链接 | 其他 | -|-------------------|------------|-------------------------------------------------------------------------------|---------------------------------------------------------------------| -| 规划 | 车辆全局路径规划 | [carla_slam_gmapping](https://github.com/OpenHUTB/nn/tree/main/src/carla_slam_gmapping) | - | +### 1. 🛣️ 车道线检测 (Lane Detection) +* **视觉管线**: 灰度化 -> 高斯模糊 -> Canny 边缘检测 -> 动态 ROI -> 霍夫变换。 +* **算法优化**: + * **智能平滑**: 使用 `deque` 历史队列消除车道线抖动。 + * **斜率过滤**: 自动剔除路面阴影和垂直路标干扰。 + * **可视化调参**: 提供 `tuner.py` 工具,实时寻找针对当前天气的最佳参数。 +### 2. 🚗 目标检测 (Object Detection) +* **深度学习**: 集成 **YOLOv8 (Nano)** 模型。 +* **识别对象**: 实时框出前方车辆 (Car)、卡车 (Truck)、巴士 (Bus) 和行人 (Person)。 +* **性能**: 针对 CPU 优化,轻量级推理。 -## 贡献指南 +## 🛠️ 环境准备 -准备提交代码之前,请阅读 [贡献指南](https://github.com/OpenHUTB/.github/blob/master/CONTRIBUTING.md) 。 -代码的优化包括:注释、[PEP 8 风格调整](https://peps.pythonlang.cn/pep-0008/) 、将神经网络应用到Carla模拟器中、撰写对应 [文档](https://openhutb.github.io/nn/) 、添加 [源代码对应的自动化测试](https://docs.github.com/zh/actions/use-cases-and-examples/building-and-testing/building-and-testing-python) 等(从Carla场景中获取神经网络所需数据或将神经网络的结果输出到场景中)。 +### 1. 运行环境 +* Python 3.8+ (推荐 Python 3.13) +* 建议使用独立显卡 (GPU) 以获得更高帧率,但在 CPU 上也能运行 (FPS 5-10)。 -### 约定 +### 2. 安装依赖 +请确保安装了最新版本的依赖库(包含 ultralytics): +```bash +pip install -r requirements.txt -* 每个模块位于`src/{模块名}`目录下,`模块名`需要用2-3个单词表示,首字母不需要大写,下划线`_`分隔,不能宽泛,越具体越好 -* 每个模块的入口须为`main.`开头,比如:main.py、main.cpp、main.bat、main.sh等,提供的ROS功能以`main.launch`文件作为启动配置文件 -* 每次pull request都需要保证能够通过main脚本直接运行整个模块,在提交信息中提供运行动图或截图;Pull Request的标题不能随意,需要概括具体的修改内容;README.md文档中提供运行环境和运行步骤的说明 -* 仓库尽量保存文本文件,二进制文件需要慎重,如运行需要示例数据,可以保存少量数据,大量数据可以通过提供网盘链接并说明下载链接和运行说明 +``` +## 🚀 使用指南 -### 文档生成 +### 第一步:视觉参数调优 (可选但推荐) + +如果发现车道线检测不准(乱飞或消失),请先运行调参工具: + +```bash +python tuner.py sample.hevc -测试生成的文档: -1. 安装python 3.11,并使用以下命令安装`mkdocs`和相关依赖: -```shell -pip install mkdocs -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com -pip install -r requirements.txt ``` -(可选)安装完成后使用`mkdocs --version`查看是否安装成功。 -2. 在命令行中进入`nn`目录下,运行: -```shell -mkdocs build -mkdocs serve +* 按 `空格` 暂停,拖动滑动条调整 Canny 阈值和 ROI 区域。 +* 记下最佳参数,并填入 `main.py` 顶部的配置区。 + +### 第二步:启动自动驾驶系统 + +直接运行主程序,系统将同时加载车道检测器和 YOLO 模型: + +```bash +python main.py sample.hevc + ``` -然后使用浏览器打开 [http://127.0.0.1:8000](http://127.0.0.1:8000),查看文档页面能否正常显示。 -## 参考 +* **首次运行提示**: 程序会自动下载 `yolov8n.pt` 模型权重文件 (约 6MB),请保持网络连接。 -* [代理模拟器文档](https://openhutb.github.io) -* 已有相关 [无人车](https://openhutb.github.io/doc/used_by/) 、[无人机](https://openhutb.github.io/air_doc/third/used_by/) 、[具身人](https://openhutb.github.io/doc/pedestrian/humanoid/) 的实现 -* [神经网络原理](https://github.com/OpenHUTB/neuro) +## 📂 项目结构 + +```text +Project_Root/ +├── main.py # [入口] 主程序,协调车道与YOLO检测 +├── yolo_det.py # [模块] 封装 YOLOv8 目标检测逻辑 +├── tuner.py # [工具] 视觉参数调试器 +├── requirements.txt # 依赖清单 +├── README.md # 项目文档 +└── sample.hevc # 测试数据 + +``` + +## ⚠️ 常见问题 + +**Q1: 画面非常卡顿 (Low FPS)?** + +* **原因**: 深度学习模型在没有 GPU 加速的电脑上运行较慢。 +* **优化**: 可以在 `main.py` 中实现“跳帧机制”(例如每 3 帧跑一次 YOLO,中间帧沿用结果)。 + +**Q2: 报错 `ModuleNotFoundError: No module named 'ultralytics'`?** + +* **解决**: 请运行 `pip install -r requirements.txt` 确保库已安装。 + +## 🔮 路线图 (Roadmap) + +* [x] 基础 OpenCV 车道线检测 +* [x] 可视化参数调试工具 (`tuner.py`) +* [x] 帧间平滑与防抖算法 +* [x] 集成 YOLOv8 目标检测 (`yolo_det.py`) +* [ ] 性能优化:加入多线程或跳帧处理 +* [ ] 车辆距离估算 (基于检测框大小) +* [ ] 偏离预警系统 (LDW) + +## 📚 参考资料 + +* [Ultralytics YOLOv8 Docs](https://docs.ultralytics.com/) +* [OpenCV Computer Vision](https://opencv.org/) + +``` -通过网盘分享的文件:supercombo.h5 -链接: https://pan.baidu.com/s/1yYc4aPBLnXRuWXEb0hyKMA?pwd=1234 提取码: 1234 \ No newline at end of file +``` \ No newline at end of file From d500174feb5bc4bb24af3b1bb8be5729934dab3b Mon Sep 17 00:00:00 2001 From: abc123335 <1607504213@qq.com> Date: Sat, 20 Dec 2025 16:16:09 +0800 Subject: [PATCH 03/11] =?UTF-8?q?=E4=B8=BA=E7=B3=BB=E7=BB=9F=E5=A2=9E?= =?UTF-8?q?=E6=B7=BB=E4=BA=86=E7=A2=B0=E6=92=9E=E6=A3=80=E6=B5=8B=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 272 ++++++++++++++++++++++++++++------------------- requirements.txt | 9 +- 2 files changed, 168 insertions(+), 113 deletions(-) diff --git a/main.py b/main.py index 8ac2a835e6..98d8ed8690 100644 --- a/main.py +++ b/main.py @@ -1,118 +1,174 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -Lane Line Detection (精简版) -核心功能:Canny边缘检测 + 霍夫变换实现车道线检测,单视频处理+结果保存 -适用环境:Ubuntu (Python 3.10 + OpenCV + numpy) -""" - import cv2 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: +import sys +from collections import deque +import time +from yolo_det import ObjectDetector + +# ========================================== +# 👇 参数配置区 (保持你的 Tuner 参数) +# ========================================== +CANNY_LOW = 50 +CANNY_HIGH = 150 +ROI_TOP_WIDTH = 0.40 +ROI_HEIGHT_POS = 0.60 +HOUGH_THRESH = 20 +MIN_LINE_LEN = 20 +MAX_LINE_GAP = 100 + +# 👇 新增优化参数 +SKIP_FRAMES = 3 # 每 4 帧跑一次 YOLO (极大提升 FPS) +WARNING_RATIO = 0.20 # 当车宽占画面 20% 时,变红预警 + + +# ========================================== + +class LaneDetector: + def __init__(self): + self.left_lines_buffer = deque(maxlen=10) + self.right_lines_buffer = deque(maxlen=10) + self.vertices = None + + def region_of_interest(self, img): + mask = np.zeros_like(img) + if self.vertices is None: + height, width = img.shape + top_w = width * ROI_TOP_WIDTH + top_x = width * 0.5 + self.vertices = np.array([[ + (0, height), + (int(top_x - top_w / 2), int(height * ROI_HEIGHT_POS)), + (int(top_x + top_w / 2), int(height * ROI_HEIGHT_POS)), + (width, height) + ]], dtype=np.int32) + cv2.fillPoly(mask, self.vertices, 255) + return cv2.bitwise_and(img, mask) + + def process_lane(self, frame): + if frame is None: return None + gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) + blur = cv2.GaussianBlur(gray, (5, 5), 0) + edges = cv2.Canny(blur, CANNY_LOW, CANNY_HIGH) + roi = self.region_of_interest(edges) + lines = cv2.HoughLinesP(roi, 1, np.pi / 180, HOUGH_THRESH, + minLineLength=MIN_LINE_LEN, maxLineGap=MAX_LINE_GAP) + + left_raw, right_raw = self.average_slope_intercept(lines) + left_avg, right_avg = self.smooth_lines(left_raw, right_raw) + + h = frame.shape[0] + y_min, y_max = int(h * ROI_HEIGHT_POS) + 50, h + left_pts = self.make_points(left_avg, y_min, y_max) + right_pts = self.make_points(right_avg, y_min, y_max) + + return self.draw_lane(frame, left_pts, right_pts) + + def average_slope_intercept(self, lines): + lefts, rights = [], [] + if lines is None: return None, 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(): - print(f"错误:无法打开视频 → {video_path}") - return - - # 获取视频基础信息 - 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: + if x2 == x1: continue + slope = (y2 - y1) / (x2 - x1) + intercept = y1 - slope * x1 + if abs(slope) < 0.3 or abs(slope) > 10: continue + if slope < 0: + lefts.append((slope, intercept)) + else: + rights.append((slope, intercept)) + return (np.average(lefts, axis=0) if lefts else None, + np.average(rights, axis=0) if rights else None) + + def smooth_lines(self, l, r): + if l is not None: self.left_lines_buffer.append(l) + if r is not None: self.right_lines_buffer.append(r) + return (np.average(self.left_lines_buffer, axis=0) if self.left_lines_buffer else None, + np.average(self.right_lines_buffer, axis=0) if self.right_lines_buffer else None) + + def make_points(self, line, y1, y2): + if line is None: return None + s, i = line + if abs(s) < 1e-3: return None + try: + return ((int((y1 - i) / s), y1), (int((y2 - i) / s), y2)) + except: + return None + + def draw_lane(self, img, l, r): + overlay = np.zeros_like(img) + if l and r: + pts = np.array([l[0], l[1], r[1], r[0]], dtype=np.int32) + cv2.fillPoly(overlay, [pts], (0, 255, 0)) + if l: cv2.line(overlay, l[0], l[1], (255, 0, 0), 10) + if r: cv2.line(overlay, r[0], r[1], (0, 0, 255), 10) + return overlay + + +def main(): + source = sys.argv[1] if len(sys.argv) > 1 else "sample.hevc" + cap = cv2.VideoCapture(source) + if not cap.isOpened(): return + + lane_det = LaneDetector() + yolo_det = ObjectDetector() + print("🚀 系统优化版启动 - 跳帧检测 & 碰撞预警") + + frame_count = 0 + current_detections = [] # 缓存当前的检测结果 + + while True: + t_start = time.time() ret, frame = cap.read() if not ret: - break - - # 核心:检测车道线 - 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 - - count += 1 - - # 释放资源 + cap.set(cv2.CAP_PROP_POS_FRAMES, 0) + continue + + display = frame.copy() + h, w = frame.shape[:2] + + # --- 1. 跳帧检测逻辑 --- + # 只有当 帧数 能被 (SKIP_FRAMES + 1) 整除时才跑 YOLO + if frame_count % (SKIP_FRAMES + 1) == 0: + _, current_detections = yolo_det.detect(frame) + + # --- 2. 绘制检测结果 (每一帧都画,利用缓存) --- + for det in current_detections: + x1, y1, x2, y2 = det['box'] + width_ratio = det['width'] / w + + # 智能预警逻辑 + color = (0, 255, 0) # 默认绿色 + msg = "" + + if width_ratio > WARNING_RATIO: + color = (0, 0, 255) # 危险红色 + msg = " WARNING!" + cv2.putText(display, "BRAKE!", (x1, y1 - 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3) + + cv2.rectangle(display, (x1, y1), (x2, y2), color, 2) + cv2.putText(display, f"{det['class']}{msg}", (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1) + + # --- 3. 车道线叠加 --- + lane_layer = lane_det.process_lane(frame) + if lane_layer is not None: + display = cv2.addWeighted(display, 1, lane_layer, 0.4, 0) + + # --- 4. 仪表盘 --- + fps = 1.0 / (time.time() - t_start) + # 显示当前是在检测(Detect)还是在追踪(Track/Skip) + status = "Detecting" if frame_count % (SKIP_FRAMES + 1) == 0 else "Skipping" + + cv2.putText(display, f"FPS: {fps:.1f}", (20, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2) + cv2.putText(display, f"Mode: {status}", (20, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (200, 200, 200), 1) + + cv2.imshow('AutoPilot V2.5', display) + + frame_count += 1 + if cv2.waitKey(1) & 0xFF == ord('q'): break + cap.release() - writer.release() cv2.destroyAllWindows() - print(f"处理完成!结果保存至 → {result_path}") -# ===================== 主函数(精简参数) ===================== + if __name__ == "__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) + main() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 2d3d95d455..97bfe852dd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,4 @@ -python-markdown-math -imageio -numpy -matplotlib -tensorflow \ No newline at end of file +opencv-python>=4.8.0 +numpy>=1.24.0 +matplotlib>=3.7.0 +ultralytics>=8.0.0 From f2f6b183503931111c56061fca0c4b76ba582b13 Mon Sep 17 00:00:00 2001 From: abc123335 <1607504213@qq.com> Date: Sat, 20 Dec 2025 18:31:44 +0800 Subject: [PATCH 04/11] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E7=A2=B0=E6=92=9E?= =?UTF-8?q?=E6=A3=80=E6=B5=8B=E5=8A=9F=E8=83=BD=E6=9D=BF=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 97bfe852dd..fb53d2a3e8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ opencv-python>=4.8.0 numpy>=1.24.0 matplotlib>=3.7.0 ultralytics>=8.0.0 +tensorflow \ No newline at end of file From a23bd55538a201b439bd285999ecd698414c91e2 Mon Sep 17 00:00:00 2001 From: abc123335 <1607504213@qq.com> Date: Sat, 20 Dec 2025 18:41:36 +0800 Subject: [PATCH 05/11] Fix: remove tensorflow to save disk space --- requirements.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index fb53d2a3e8..39a9d4f3a7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,4 @@ opencv-python>=4.8.0 numpy>=1.24.0 matplotlib>=3.7.0 -ultralytics>=8.0.0 -tensorflow \ No newline at end of file +ultralytics>=8.0.0 \ No newline at end of file From 195edb7908b382ca75fb2e5d06f7c2ca1624977f Mon Sep 17 00:00:00 2001 From: abc123335 <1607504213@qq.com> Date: Sat, 20 Dec 2025 19:10:58 +0800 Subject: [PATCH 06/11] Delete main.py --- main.py | 174 -------------------------------------------------------- 1 file changed, 174 deletions(-) delete mode 100644 main.py diff --git a/main.py b/main.py deleted file mode 100644 index 98d8ed8690..0000000000 --- a/main.py +++ /dev/null @@ -1,174 +0,0 @@ -import cv2 -import numpy as np -import sys -from collections import deque -import time -from yolo_det import ObjectDetector - -# ========================================== -# 👇 参数配置区 (保持你的 Tuner 参数) -# ========================================== -CANNY_LOW = 50 -CANNY_HIGH = 150 -ROI_TOP_WIDTH = 0.40 -ROI_HEIGHT_POS = 0.60 -HOUGH_THRESH = 20 -MIN_LINE_LEN = 20 -MAX_LINE_GAP = 100 - -# 👇 新增优化参数 -SKIP_FRAMES = 3 # 每 4 帧跑一次 YOLO (极大提升 FPS) -WARNING_RATIO = 0.20 # 当车宽占画面 20% 时,变红预警 - - -# ========================================== - -class LaneDetector: - def __init__(self): - self.left_lines_buffer = deque(maxlen=10) - self.right_lines_buffer = deque(maxlen=10) - self.vertices = None - - def region_of_interest(self, img): - mask = np.zeros_like(img) - if self.vertices is None: - height, width = img.shape - top_w = width * ROI_TOP_WIDTH - top_x = width * 0.5 - self.vertices = np.array([[ - (0, height), - (int(top_x - top_w / 2), int(height * ROI_HEIGHT_POS)), - (int(top_x + top_w / 2), int(height * ROI_HEIGHT_POS)), - (width, height) - ]], dtype=np.int32) - cv2.fillPoly(mask, self.vertices, 255) - return cv2.bitwise_and(img, mask) - - def process_lane(self, frame): - if frame is None: return None - gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) - blur = cv2.GaussianBlur(gray, (5, 5), 0) - edges = cv2.Canny(blur, CANNY_LOW, CANNY_HIGH) - roi = self.region_of_interest(edges) - lines = cv2.HoughLinesP(roi, 1, np.pi / 180, HOUGH_THRESH, - minLineLength=MIN_LINE_LEN, maxLineGap=MAX_LINE_GAP) - - left_raw, right_raw = self.average_slope_intercept(lines) - left_avg, right_avg = self.smooth_lines(left_raw, right_raw) - - h = frame.shape[0] - y_min, y_max = int(h * ROI_HEIGHT_POS) + 50, h - left_pts = self.make_points(left_avg, y_min, y_max) - right_pts = self.make_points(right_avg, y_min, y_max) - - return self.draw_lane(frame, left_pts, right_pts) - - def average_slope_intercept(self, lines): - lefts, rights = [], [] - if lines is None: return None, None - for line in lines: - x1, y1, x2, y2 = line[0] - if x2 == x1: continue - slope = (y2 - y1) / (x2 - x1) - intercept = y1 - slope * x1 - if abs(slope) < 0.3 or abs(slope) > 10: continue - if slope < 0: - lefts.append((slope, intercept)) - else: - rights.append((slope, intercept)) - return (np.average(lefts, axis=0) if lefts else None, - np.average(rights, axis=0) if rights else None) - - def smooth_lines(self, l, r): - if l is not None: self.left_lines_buffer.append(l) - if r is not None: self.right_lines_buffer.append(r) - return (np.average(self.left_lines_buffer, axis=0) if self.left_lines_buffer else None, - np.average(self.right_lines_buffer, axis=0) if self.right_lines_buffer else None) - - def make_points(self, line, y1, y2): - if line is None: return None - s, i = line - if abs(s) < 1e-3: return None - try: - return ((int((y1 - i) / s), y1), (int((y2 - i) / s), y2)) - except: - return None - - def draw_lane(self, img, l, r): - overlay = np.zeros_like(img) - if l and r: - pts = np.array([l[0], l[1], r[1], r[0]], dtype=np.int32) - cv2.fillPoly(overlay, [pts], (0, 255, 0)) - if l: cv2.line(overlay, l[0], l[1], (255, 0, 0), 10) - if r: cv2.line(overlay, r[0], r[1], (0, 0, 255), 10) - return overlay - - -def main(): - source = sys.argv[1] if len(sys.argv) > 1 else "sample.hevc" - cap = cv2.VideoCapture(source) - if not cap.isOpened(): return - - lane_det = LaneDetector() - yolo_det = ObjectDetector() - print("🚀 系统优化版启动 - 跳帧检测 & 碰撞预警") - - frame_count = 0 - current_detections = [] # 缓存当前的检测结果 - - while True: - t_start = time.time() - ret, frame = cap.read() - if not ret: - cap.set(cv2.CAP_PROP_POS_FRAMES, 0) - continue - - display = frame.copy() - h, w = frame.shape[:2] - - # --- 1. 跳帧检测逻辑 --- - # 只有当 帧数 能被 (SKIP_FRAMES + 1) 整除时才跑 YOLO - if frame_count % (SKIP_FRAMES + 1) == 0: - _, current_detections = yolo_det.detect(frame) - - # --- 2. 绘制检测结果 (每一帧都画,利用缓存) --- - for det in current_detections: - x1, y1, x2, y2 = det['box'] - width_ratio = det['width'] / w - - # 智能预警逻辑 - color = (0, 255, 0) # 默认绿色 - msg = "" - - if width_ratio > WARNING_RATIO: - color = (0, 0, 255) # 危险红色 - msg = " WARNING!" - cv2.putText(display, "BRAKE!", (x1, y1 - 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3) - - cv2.rectangle(display, (x1, y1), (x2, y2), color, 2) - cv2.putText(display, f"{det['class']}{msg}", (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1) - - # --- 3. 车道线叠加 --- - lane_layer = lane_det.process_lane(frame) - if lane_layer is not None: - display = cv2.addWeighted(display, 1, lane_layer, 0.4, 0) - - # --- 4. 仪表盘 --- - fps = 1.0 / (time.time() - t_start) - # 显示当前是在检测(Detect)还是在追踪(Track/Skip) - status = "Detecting" if frame_count % (SKIP_FRAMES + 1) == 0 else "Skipping" - - cv2.putText(display, f"FPS: {fps:.1f}", (20, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2) - cv2.putText(display, f"Mode: {status}", (20, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (200, 200, 200), 1) - - cv2.imshow('AutoPilot V2.5', display) - - frame_count += 1 - if cv2.waitKey(1) & 0xFF == ord('q'): break - - cap.release() - cv2.destroyAllWindows() - - -if __name__ == "__main__": - main() \ No newline at end of file From 683856da92a855fbf97608ddaef697e64bbf9e1d Mon Sep 17 00:00:00 2001 From: abc123335 <1607504213@qq.com> Date: Sat, 20 Dec 2025 19:11:33 +0800 Subject: [PATCH 07/11] Delete requirements.txt --- requirements.txt | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index 39a9d4f3a7..0000000000 --- a/requirements.txt +++ /dev/null @@ -1,4 +0,0 @@ -opencv-python>=4.8.0 -numpy>=1.24.0 -matplotlib>=3.7.0 -ultralytics>=8.0.0 \ No newline at end of file From de767dcbc5e935aee0a88964d4510891fda01f4c Mon Sep 17 00:00:00 2001 From: abc123335 <1607504213@qq.com> Date: Sat, 20 Dec 2025 19:23:56 +0800 Subject: [PATCH 08/11] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E6=96=B0?= =?UTF-8?q?=E7=9A=84=E5=8A=9F=E8=83=BD=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yolo_det.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 yolo_det.py diff --git a/yolo_det.py b/yolo_det.py new file mode 100644 index 0000000000..1d9a794176 --- /dev/null +++ b/yolo_det.py @@ -0,0 +1,34 @@ +from ultralytics import YOLO +import cv2 +import numpy as np + +class ObjectDetector: + def __init__(self, model_name='yolov8n.pt'): + self.model = YOLO(model_name) + # 目标: 2=Car, 3=Motor, 5=Bus, 7=Truck + self.target_classes = [2, 3, 5, 7] + + def detect(self, frame): + """ + 返回: (original_frame, detections_list) + """ + results = self.model(frame, stream=True, verbose=False) + detections = [] + + for result in results: + boxes = result.boxes + for box in boxes: + cls_id = int(box.cls[0]) + if cls_id in self.target_classes: + x1, y1, x2, y2 = map(int, box.xyxy[0]) + conf = float(box.conf[0]) + if conf < 0.4: continue + + detections.append({ + 'box': [x1, y1, x2, y2], + 'class': self.model.names[cls_id], + 'conf': conf, + 'width': x2 - x1, + 'center': ((x1+x2)//2, (y1+y2)//2) + }) + return frame, detections \ No newline at end of file From 0cb805a0e46044fd5d640c0eb73dc133c15f4e7e Mon Sep 17 00:00:00 2001 From: abc123335 <1607504213@qq.com> Date: Sat, 20 Dec 2025 19:30:27 +0800 Subject: [PATCH 09/11] Delete README.md --- README.md | 101 ------------------------------------------------------ 1 file changed, 101 deletions(-) delete mode 100644 README.md diff --git a/README.md b/README.md deleted file mode 100644 index d3553309e5..0000000000 --- a/README.md +++ /dev/null @@ -1,101 +0,0 @@ -# AutoPilot System: Lane & Object Detection - -这是一个结合了**传统计算机视觉**与**深度学习**的自动驾驶辅助系统原型。它利用 OpenCV 进行高精度的车道线检测,并集成 YOLOv8 模型实现对车辆、行人等障碍物的实时识别。 - -![Status](https://img.shields.io/badge/Status-Active-success) -![Python](https://img.shields.io/badge/Python-3.8%2B-blue) - -## ✨ 核心功能 - -### 1. 🛣️ 车道线检测 (Lane Detection) -* **视觉管线**: 灰度化 -> 高斯模糊 -> Canny 边缘检测 -> 动态 ROI -> 霍夫变换。 -* **算法优化**: - * **智能平滑**: 使用 `deque` 历史队列消除车道线抖动。 - * **斜率过滤**: 自动剔除路面阴影和垂直路标干扰。 - * **可视化调参**: 提供 `tuner.py` 工具,实时寻找针对当前天气的最佳参数。 - -### 2. 🚗 目标检测 (Object Detection) -* **深度学习**: 集成 **YOLOv8 (Nano)** 模型。 -* **识别对象**: 实时框出前方车辆 (Car)、卡车 (Truck)、巴士 (Bus) 和行人 (Person)。 -* **性能**: 针对 CPU 优化,轻量级推理。 - -## 🛠️ 环境准备 - -### 1. 运行环境 -* Python 3.8+ (推荐 Python 3.13) -* 建议使用独立显卡 (GPU) 以获得更高帧率,但在 CPU 上也能运行 (FPS 5-10)。 - -### 2. 安装依赖 -请确保安装了最新版本的依赖库(包含 ultralytics): -```bash -pip install -r requirements.txt - -``` - -## 🚀 使用指南 - -### 第一步:视觉参数调优 (可选但推荐) - -如果发现车道线检测不准(乱飞或消失),请先运行调参工具: - -```bash -python tuner.py sample.hevc - -``` - -* 按 `空格` 暂停,拖动滑动条调整 Canny 阈值和 ROI 区域。 -* 记下最佳参数,并填入 `main.py` 顶部的配置区。 - -### 第二步:启动自动驾驶系统 - -直接运行主程序,系统将同时加载车道检测器和 YOLO 模型: - -```bash -python main.py sample.hevc - -``` - -* **首次运行提示**: 程序会自动下载 `yolov8n.pt` 模型权重文件 (约 6MB),请保持网络连接。 - -## 📂 项目结构 - -```text -Project_Root/ -├── main.py # [入口] 主程序,协调车道与YOLO检测 -├── yolo_det.py # [模块] 封装 YOLOv8 目标检测逻辑 -├── tuner.py # [工具] 视觉参数调试器 -├── requirements.txt # 依赖清单 -├── README.md # 项目文档 -└── sample.hevc # 测试数据 - -``` - -## ⚠️ 常见问题 - -**Q1: 画面非常卡顿 (Low FPS)?** - -* **原因**: 深度学习模型在没有 GPU 加速的电脑上运行较慢。 -* **优化**: 可以在 `main.py` 中实现“跳帧机制”(例如每 3 帧跑一次 YOLO,中间帧沿用结果)。 - -**Q2: 报错 `ModuleNotFoundError: No module named 'ultralytics'`?** - -* **解决**: 请运行 `pip install -r requirements.txt` 确保库已安装。 - -## 🔮 路线图 (Roadmap) - -* [x] 基础 OpenCV 车道线检测 -* [x] 可视化参数调试工具 (`tuner.py`) -* [x] 帧间平滑与防抖算法 -* [x] 集成 YOLOv8 目标检测 (`yolo_det.py`) -* [ ] 性能优化:加入多线程或跳帧处理 -* [ ] 车辆距离估算 (基于检测框大小) -* [ ] 偏离预警系统 (LDW) - -## 📚 参考资料 - -* [Ultralytics YOLOv8 Docs](https://docs.ultralytics.com/) -* [OpenCV Computer Vision](https://opencv.org/) - -``` - -``` \ No newline at end of file From 4bc826f2c87f4fe543772d640ba2833a6325d1ee Mon Sep 17 00:00:00 2001 From: abc123335 <1607504213@qq.com> Date: Sat, 20 Dec 2025 19:43:10 +0800 Subject: [PATCH 10/11] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=96=B0=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E6=9D=BF=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yolo_det.py | 192 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 158 insertions(+), 34 deletions(-) diff --git a/yolo_det.py b/yolo_det.py index 1d9a794176..d9e04692c1 100644 --- a/yolo_det.py +++ b/yolo_det.py @@ -1,34 +1,158 @@ -from ultralytics import YOLO -import cv2 -import numpy as np - -class ObjectDetector: - def __init__(self, model_name='yolov8n.pt'): - self.model = YOLO(model_name) - # 目标: 2=Car, 3=Motor, 5=Bus, 7=Truck - self.target_classes = [2, 3, 5, 7] - - def detect(self, frame): - """ - 返回: (original_frame, detections_list) - """ - results = self.model(frame, stream=True, verbose=False) - detections = [] - - for result in results: - boxes = result.boxes - for box in boxes: - cls_id = int(box.cls[0]) - if cls_id in self.target_classes: - x1, y1, x2, y2 = map(int, box.xyxy[0]) - conf = float(box.conf[0]) - if conf < 0.4: continue - - detections.append({ - 'box': [x1, y1, x2, y2], - 'class': self.model.names[cls_id], - 'conf': conf, - 'width': x2 - x1, - 'center': ((x1+x2)//2, (y1+y2)//2) - }) - return frame, detections \ No newline at end of file + + +# 👇 新增优化参数 +SKIP_FRAMES = 3 # 每 4 帧跑一次 YOLO (极大提升 FPS) +WARNING_RATIO = 0.20 # 当车宽占画面 20% 时,变红预警 + + +# ========================================== + +class LaneDetector: + def __init__(self): + self.left_lines_buffer = deque(maxlen=10) + self.right_lines_buffer = deque(maxlen=10) + self.vertices = None + + def region_of_interest(self, img): + mask = np.zeros_like(img) + if self.vertices is None: + height, width = img.shape + top_w = width * ROI_TOP_WIDTH + top_x = width * 0.5 + self.vertices = np.array([[ + (0, height), + (int(top_x - top_w / 2), int(height * ROI_HEIGHT_POS)), + (int(top_x + top_w / 2), int(height * ROI_HEIGHT_POS)), + (width, height) + ]], dtype=np.int32) + cv2.fillPoly(mask, self.vertices, 255) + return cv2.bitwise_and(img, mask) + + def process_lane(self, frame): + if frame is None: return None + gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) + blur = cv2.GaussianBlur(gray, (5, 5), 0) + edges = cv2.Canny(blur, CANNY_LOW, CANNY_HIGH) + roi = self.region_of_interest(edges) + lines = cv2.HoughLinesP(roi, 1, np.pi / 180, HOUGH_THRESH, + minLineLength=MIN_LINE_LEN, maxLineGap=MAX_LINE_GAP) + + left_raw, right_raw = self.average_slope_intercept(lines) + left_avg, right_avg = self.smooth_lines(left_raw, right_raw) + + h = frame.shape[0] + y_min, y_max = int(h * ROI_HEIGHT_POS) + 50, h + left_pts = self.make_points(left_avg, y_min, y_max) + right_pts = self.make_points(right_avg, y_min, y_max) + + return self.draw_lane(frame, left_pts, right_pts) + + def average_slope_intercept(self, lines): + lefts, rights = [], [] + if lines is None: return None, None + for line in lines: + x1, y1, x2, y2 = line[0] + if x2 == x1: continue + slope = (y2 - y1) / (x2 - x1) + intercept = y1 - slope * x1 + if abs(slope) < 0.3 or abs(slope) > 10: continue + if slope < 0: + lefts.append((slope, intercept)) + else: + rights.append((slope, intercept)) + return (np.average(lefts, axis=0) if lefts else None, + np.average(rights, axis=0) if rights else None) + + def smooth_lines(self, l, r): + if l is not None: self.left_lines_buffer.append(l) + if r is not None: self.right_lines_buffer.append(r) + return (np.average(self.left_lines_buffer, axis=0) if self.left_lines_buffer else None, + np.average(self.right_lines_buffer, axis=0) if self.right_lines_buffer else None) + + def make_points(self, line, y1, y2): + if line is None: return None + s, i = line + if abs(s) < 1e-3: return None + try: + return ((int((y1 - i) / s), y1), (int((y2 - i) / s), y2)) + except: + return None + + def draw_lane(self, img, l, r): + overlay = np.zeros_like(img) + if l and r: + pts = np.array([l[0], l[1], r[1], r[0]], dtype=np.int32) + cv2.fillPoly(overlay, [pts], (0, 255, 0)) + if l: cv2.line(overlay, l[0], l[1], (255, 0, 0), 10) + if r: cv2.line(overlay, r[0], r[1], (0, 0, 255), 10) + return overlay + + +def main(): + source = sys.argv[1] if len(sys.argv) > 1 else "sample.hevc" + cap = cv2.VideoCapture(source) + if not cap.isOpened(): return + + lane_det = LaneDetector() + yolo_det = ObjectDetector() + print("🚀 系统优化版启动 - 跳帧检测 & 碰撞预警") + + frame_count = 0 + current_detections = [] # 缓存当前的检测结果 + + while True: + t_start = time.time() + ret, frame = cap.read() + if not ret: + cap.set(cv2.CAP_PROP_POS_FRAMES, 0) + continue + + display = frame.copy() + h, w = frame.shape[:2] + + # --- 1. 跳帧检测逻辑 --- + # 只有当 帧数 能被 (SKIP_FRAMES + 1) 整除时才跑 YOLO + if frame_count % (SKIP_FRAMES + 1) == 0: + _, current_detections = yolo_det.detect(frame) + + # --- 2. 绘制检测结果 (每一帧都画,利用缓存) --- + for det in current_detections: + x1, y1, x2, y2 = det['box'] + width_ratio = det['width'] / w + + # 智能预警逻辑 + color = (0, 255, 0) # 默认绿色 + msg = "" + + if width_ratio > WARNING_RATIO: + color = (0, 0, 255) # 危险红色 + msg = " WARNING!" + cv2.putText(display, "BRAKE!", (x1, y1 - 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3) + + cv2.rectangle(display, (x1, y1), (x2, y2), color, 2) + cv2.putText(display, f"{det['class']}{msg}", (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1) + + # --- 3. 车道线叠加 --- + lane_layer = lane_det.process_lane(frame) + if lane_layer is not None: + display = cv2.addWeighted(display, 1, lane_layer, 0.4, 0) + + # --- 4. 仪表盘 --- + fps = 1.0 / (time.time() - t_start) + # 显示当前是在检测(Detect)还是在追踪(Track/Skip) + status = "Detecting" if frame_count % (SKIP_FRAMES + 1) == 0 else "Skipping" + + cv2.putText(display, f"FPS: {fps:.1f}", (20, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2) + cv2.putText(display, f"Mode: {status}", (20, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (200, 200, 200), 1) + + cv2.imshow('AutoPilot V2.5', display) + + frame_count += 1 + if cv2.waitKey(1) & 0xFF == ord('q'): break + + cap.release() + cv2.destroyAllWindows() + + +if __name__ == "__main__": + main() \ No newline at end of file From b82ac9c6eb520c0abae09d2a23fbc1ccf5d180b7 Mon Sep 17 00:00:00 2001 From: abc123335 <1607504213@qq.com> Date: Sat, 20 Dec 2025 21:00:54 +0800 Subject: [PATCH 11/11] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=96=B0=E7=9A=84?= =?UTF-8?q?=E7=A2=B0=E6=92=9E=E6=A3=80=E6=B5=8B=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 250 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 250 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000000..33510d82d5 --- /dev/null +++ b/main.py @@ -0,0 +1,250 @@ +import cv2 +import numpy as np +import sys +import time +import os +import math +from collections import deque +from yolo_det import ObjectDetector + +# ================= 配置区 ================= +CANNY_LOW, CANNY_HIGH = 50, 150 +ROI_TOP, ROI_HEIGHT = 0.40, 0.60 +SKIP_FRAMES = 3 # 跳帧数 +WARNING_RATIO = 0.20 # 碰撞预警阈值 +STEER_SENSITIVITY = 1.5 # 转向灵敏度 (越大方向盘转得越快) + + +# ========================================== + +class EventLogger: + """黑匣子:负责记录危险事件""" + + def __init__(self, save_dir="events"): + self.save_dir = save_dir + if not os.path.exists(save_dir): + os.makedirs(save_dir) + self.last_save_time = 0 + self.cooldown = 2.0 # 两次抓拍最小间隔(秒) + + def log_danger(self, frame, obj_name): + now = time.time() + if now - self.last_save_time > self.cooldown: + timestamp = time.strftime("%Y%m%d_%H%M%S") + filename = f"{self.save_dir}/danger_{timestamp}_{obj_name}.jpg" + cv2.imwrite(filename, frame) + print(f"📸 危险已抓拍: {filename}") + self.last_save_time = now + return True + return False + + +class LaneSystem: + def __init__(self): + self.left_fit_avg = None + self.right_fit_avg = None + self.vertices = None + + def get_lane_info(self, frame): + """返回: (lane_mask, deviation_percent, curvature_angle)""" + if frame is None: return None, 0, 0 + + # 1. 视觉处理 + gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) + blur = cv2.GaussianBlur(gray, (5, 5), 0) + edges = cv2.Canny(blur, CANNY_LOW, CANNY_HIGH) + + # 2. ROI + h, w = frame.shape[:2] + if self.vertices is None: + top_w = w * ROI_TOP + self.vertices = np.array([[ + (0, h), + (int(w * 0.5 - top_w / 2), int(h * ROI_HEIGHT)), + (int(w * 0.5 + top_w / 2), int(h * ROI_HEIGHT)), + (w, h) + ]], dtype=np.int32) + + mask = np.zeros_like(edges) + cv2.fillPoly(mask, self.vertices, 255) + roi = cv2.bitwise_and(edges, mask) + + # 3. 霍夫变换 + lines = cv2.HoughLinesP(roi, 1, np.pi / 180, 20, minLineLength=20, maxLineGap=100) + + # 4. 计算拟合线 + l_fit, r_fit = self.avg_lines(lines) + self.left_fit_avg = self.smooth(self.left_fit_avg, l_fit) + self.right_fit_avg = self.smooth(self.right_fit_avg, r_fit) + + # 5. 计算绘制点 + y_min = int(h * ROI_HEIGHT) + 40 + l_pts = self.make_pts(self.left_fit_avg, y_min, h) + r_pts = self.make_pts(self.right_fit_avg, y_min, h) + + # 6. 计算偏离度与转向角 + deviation = 0 + angle = 0 + if l_pts and r_pts: + lane_center = (l_pts[0][0] + r_pts[1][0]) / 2 + screen_center = w / 2 + # 偏离度: +右偏, -左偏 + deviation = (lane_center - screen_center) / w + + # 简单估算弯道角度 (基于左右线斜率的平均值) + l_slope = self.left_fit_avg[0] + r_slope = self.right_fit_avg[0] + # 将斜率转换为角度 (这是一个近似值) + angle = math.degrees(math.atan((l_slope + r_slope) / 2)) + + # 7. 绘制 + lane_layer = np.zeros_like(frame) + if l_pts and r_pts: + pts = np.array([l_pts[0], l_pts[1], r_pts[1], r_pts[0]], dtype=np.int32) + cv2.fillPoly(lane_layer, [pts], (0, 255, 0)) + + return lane_layer, deviation, angle + + def avg_lines(self, lines): + lefts, rights = [], [] + if lines is None: return None, None + for line in lines: + x1, y1, x2, y2 = line[0] + if x2 == x1: continue + slope = (y2 - y1) / (x2 - x1) + intercept = y1 - slope * x1 + if abs(slope) < 0.3 or abs(slope) > 5: continue + if slope < 0: + lefts.append((slope, intercept)) + else: + rights.append((slope, intercept)) + return (np.mean(lefts, axis=0) if lefts else None, + np.mean(rights, axis=0) if rights else None) + + def smooth(self, curr, new_val): + return curr * 0.8 + new_val * 0.2 if curr is not None and new_val is not None else new_val + + def make_pts(self, line, y1, y2): + if line is None: return None + s, i = line + if abs(s) < 1e-3: return None + try: + return ((int((y1 - i) / s), y1), (int((y2 - i) / s), y2)) + except: + return None + + +def draw_dashboard(img, deviation, steer_angle, fps, status): + """绘制高科技仪表盘 (方向盘 + 数据)""" + h, w = img.shape[:2] + + # 1. 底部黑色面板 + cv2.rectangle(img, (0, h - 80), (w, h), (0, 0, 0), -1) + + # 2. 虚拟方向盘 (Steering Wheel) + center = (w // 2, h - 40) + radius = 30 + # 计算旋转后的终点 + # steer_angle 是基于路面斜率的,我们需要把它放大一点显示 + display_angle = steer_angle * 5 * STEER_SENSITIVITY + # 限制最大转角 + display_angle = max(-90, min(90, display_angle)) + + rad = math.radians(display_angle - 90) # -90因为OpenCV里0度是3点钟方向 + end_x = int(center[0] + radius * math.cos(rad)) + end_y = int(center[1] + radius * math.sin(rad)) + + # 画圆和指针 + cv2.circle(img, center, radius, (200, 200, 200), 2) + cv2.line(img, center, (end_x, end_y), (0, 0, 255), 3) + cv2.putText(img, "STEER", (center[0] - 20, center[1] + 45), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1) + + # 3. 偏离指示条 + bar_width = 200 + cv2.rectangle(img, (w // 2 - 100, h - 75), (w // 2 + 100, h - 70), (50, 50, 50), -1) + # 指示点 + marker_x = int(w // 2 + deviation * w) # 偏差值映射到像素 + marker_x = max(w // 2 - 100, min(w // 2 + 100, marker_x)) + color = (0, 255, 0) if abs(deviation) < 0.05 else (0, 0, 255) + cv2.circle(img, (marker_x, h - 72), 6, color, -1) + + # 4. 数据显示 + cv2.putText(img, f"FPS: {fps:.1f}", (20, h - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 255), 1) + cv2.putText(img, f"STATUS: {status}", (w - 150, h - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (200, 200, 200), 1) + + +def main(): + source = sys.argv[1] if len(sys.argv) > 1 else "sample.hevc" + cap = cv2.VideoCapture(source) + if not cap.isOpened(): return + + lane_sys = LaneSystem() + yolo_sys = ObjectDetector() + logger = EventLogger() + + print("🚀 AutoPilot V3.0: 启动虚拟控制与取证系统...") + + frame_count = 0 + current_dets = [] + + while True: + t_start = time.time() + ret, frame = cap.read() + if not ret: + cap.set(cv2.CAP_PROP_POS_FRAMES, 0) + continue + + display = frame.copy() + h, w = frame.shape[:2] + + # --- A. 感知层: YOLO检测 (跳帧) --- + if frame_count % (SKIP_FRAMES + 1) == 0: + _, current_dets = yolo_sys.detect(frame) + + is_danger = False + danger_obj = "" + + # --- B. 决策层: 碰撞分析 --- + for det in current_dets: + x1, y1, x2, y2 = det['box'] + width_ratio = det['width'] / w + + color = (0, 255, 0) + if width_ratio > WARNING_RATIO: + color = (0, 0, 255) + is_danger = True + danger_obj = det['class'] + # 画面中心大字警告 + cv2.putText(display, "BRAKE!", (x1, y1 - 30), cv2.FONT_HERSHEY_SIMPLEX, 1, color, 3) + + cv2.rectangle(display, (x1, y1), (x2, y2), color, 2) + cv2.putText(display, det['class'], (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1) + + # --- C. 黑匣子: 自动抓拍 --- + if is_danger: + # 传入原始未画框的帧,还是画了框的?画了框的更直观 + if logger.log_danger(display, danger_obj): + cv2.putText(display, "SNAPSHOT SAVED", (w // 2 - 100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.8, + (255, 255, 255), 2) + + # --- D. 控制层: 车道与转向 --- + lane_layer, deviation, steer_angle = lane_sys.get_lane_info(frame) + if lane_layer is not None: + display = cv2.addWeighted(display, 1, lane_layer, 0.4, 0) + + # --- E. 交互层: 仪表盘 --- + fps = 1.0 / (time.time() - t_start) + status = "DANGER" if is_danger else "CRUISING" + draw_dashboard(display, deviation, steer_angle, fps, status) + + cv2.imshow('AutoPilot V3.0 - Control Dashboard', display) + + frame_count += 1 + if cv2.waitKey(1) & 0xFF == ord('q'): break + + cap.release() + cv2.destroyAllWindows() + + +if __name__ == "__main__": + main() \ No newline at end of file