diff --git a/.github/workflows/Conpute_involvement.yml b/.github/workflows/Conpute_involvement.yml index bba689237c..027ee3bef5 100644 --- a/.github/workflows/Conpute_involvement.yml +++ b/.github/workflows/Conpute_involvement.yml @@ -1,4 +1,3 @@ - name: compute involvement degree # 触发条件:每次 push 代码时运行 diff --git a/.github/workflows/python_test.yml b/.github/workflows/python_test.yml index 05a0feb4fc..10aa0644dc 100644 --- a/.github/workflows/python_test.yml +++ b/.github/workflows/python_test.yml @@ -2,7 +2,6 @@ name: Testing on: [push, pull_request] - jobs: build: name: Test Python script @@ -10,23 +9,22 @@ jobs: steps: - name: Checkout uses: actions/checkout@v2 + # 任务名称:设置Python环境 - name: Set up Python - # This is the version of the action for setting up Python, not the Python version. uses: actions/setup-python@v5 with: - python-version: '3.9' # 指定python版本 - # Optional - x64 or x86 architecture, defaults to x64 + python-version: '3.11' # 保持 3.11 architecture: 'x64' + + # === 恢复原来的样子:只有一步简单的安装 === - name: Install Python dependencies run: python -m pip install -r requirements.txt + - name: Python script testing - # working-directory: ./src run: | cd src/chap01_warmup && python numpy_tutorial.py && cd ../.. cd src/chap02_linear_regression && python exercise-linear_regression.py && cd ../.. cd src/chap02_linear_regression && python linear_regression-tf2.0.py && cd ../.. cd src/chap03_softmax_regression && python logistic_regression-exercise.py && cd ../.. cd src/chap03_softmax_regression && python softmax_regression-exercise.py && cd ../.. - - diff --git a/README.md b/README.md deleted file mode 100644 index d362f30b07..0000000000 --- a/README.md +++ /dev/null @@ -1,54 +0,0 @@ -# 神经网络实现代理 - -利用神经网络/ROS 实现 Carla(车辆、行人的感知、规划、控制)、AirSim、Mujoco 中人和载具的代理。 - -## 环境配置 - -* 平台: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) | - | - - -## 贡献指南 - -准备提交代码之前,请阅读 [贡献指南](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场景中获取神经网络所需数据或将神经网络的结果输出到场景中)。 - -### 约定 - -* 每个模块位于`src/{模块名}`目录下,`模块名`需要用2-3个单词表示,首字母不需要大写,下划线`_`分隔,不能宽泛,越具体越好 -* 每个模块的入口须为`main.`开头,比如:main.py、main.cpp、main.bat、main.sh等,提供的ROS功能以`main.launch`文件作为启动配置文件 -* 每次pull request都需要保证能够通过main脚本直接运行整个模块,在提交信息中提供运行动图或截图;Pull Request的标题不能随意,需要概括具体的修改内容;README.md文档中提供运行环境和运行步骤的说明 -* 仓库尽量保存文本文件,二进制文件需要慎重,如运行需要示例数据,可以保存少量数据,大量数据可以通过提供网盘链接并说明下载链接和运行说明 - - -### 文档生成 - -测试生成的文档: -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 -``` -然后使用浏览器打开 [http://127.0.0.1:8000](http://127.0.0.1:8000),查看文档页面能否正常显示。 - -## 参考 - -* [代理模拟器文档](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) - -通过网盘分享的文件:supercombo.h5 -链接: https://pan.baidu.com/s/1yYc4aPBLnXRuWXEb0hyKMA?pwd=1234 提取码: 1234 \ No newline at end of file diff --git a/main.py b/main.py index 8ac2a835e6..33510d82d5 100644 --- a/main.py +++ b/main.py @@ -1,118 +1,250 @@ -#!/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 sys +import time 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 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] - 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) > 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: - 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] + + # --- 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() - 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..e13323f341 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,141 @@ -python-markdown-math -imageio -numpy -matplotlib -tensorflow \ No newline at end of file +# === 1. 指定 CPU 版本的下载源 (用于 Linux 环境节省空间) === +--extra-index-url https://download.pytorch.org/whl/cpu + +# === 2. PyTorch 配置 (智能切换) === +# Linux (GitHub Actions): 强制下载 CPU 版 (极小) +torch==2.5.1+cpu; sys_platform == 'linux' +torchvision==0.20.1+cpu; sys_platform == 'linux' + +# Windows (你本地): 下载标准版 (带显卡支持) +torch==2.5.1; sys_platform == 'win32' +torchvision==0.20.1; sys_platform == 'win32' + +# === 3. TensorFlow 配置 === +# Linux: 下载 CPU 版 +tensorflow-cpu; sys_platform == 'linux' +# Windows: 下载标准版 +tensorflow; sys_platform == 'win32' + +# === 4. Windows 专用库 (修复 Linux 报错) === +pywinpty==3.0.2; sys_platform == 'win32' + +# === 5. 其他通用依赖 === +anyio==4.11.0 +argon2-cffi==25.1.0 +argon2-cffi-bindings==25.1.0 +arrow==1.4.0 +asttokens==3.0.0 +async-lru==2.0.5 +attrs==25.4.0 +babel==2.17.0 +beautifulsoup4==4.14.2 +bleach==6.2.0 +certifi==2025.10.5 +cffi==2.0.0 +charset-normalizer==3.4.4 +colorama==0.4.6 +comm==0.2.3 +contourpy==1.3.3 +cycler==0.12.1 +debugpy==1.8.17 +decorator==4.4.2 +defusedxml==0.7.1 +executing==2.2.1 +fastjsonschema==2.21.2 +filelock==3.20.1 +fonttools==4.61.0 +fqdn==1.5.1 +fsspec==2025.12.0 +h11==0.16.0 +httpcore==1.0.9 +httpx==0.28.1 +idna==3.11 +ImageIO==2.37.2 +imageio-ffmpeg==0.6.0 +ipykernel==7.0.1 +ipython==9.6.0 +ipython_pygments_lexers==1.1.1 +ipywidgets==8.1.7 +isoduration==20.11.0 +jedi==0.19.2 +Jinja2==3.1.6 +json5==0.12.1 +jsonpointer==3.0.0 +jsonschema==4.25.1 +jsonschema-specifications==2025.9.1 +jupyter==1.1.1 +jupyter-console==6.6.3 +jupyter-events==0.12.0 +jupyter-lsp==2.3.0 +jupyter_client==8.6.3 +jupyter_core==5.9.1 +jupyter_server==2.17.0 +jupyter_server_terminals==0.5.3 +jupyterlab==4.4.9 +jupyterlab_pygments==0.3.0 +jupyterlab_server==2.27.3 +jupyterlab_widgets==3.0.15 +kiwisolver==1.4.9 +lark==1.3.0 +MarkupSafe==3.0.3 +matplotlib==3.10.7 +matplotlib-inline==0.1.7 +mistune==3.1.4 +moviepy==1.0.3 +mpmath==1.3.0 +nbclient==0.10.2 +nbconvert==7.16.6 +nbformat==5.10.4 +nest-asyncio==1.6.0 +networkx==3.6.1 +notebook==7.4.7 +notebook_shim==0.2.4 +numpy==2.2.6 +opencv-python==4.12.0.88 +packaging==25.0 +pandas==2.3.3 +pandocfilters==1.5.1 +parso==0.8.5 +pillow==10.4.0 +platformdirs==4.5.0 +proglog==0.1.12 +prometheus_client==0.23.1 +prompt_toolkit==3.0.52 +psutil==7.1.1 +pure_eval==0.2.3 +pycparser==2.23 +Pygments==2.19.2 +pyparsing==3.2.5 +python-dateutil==2.9.0.post0 +python-dotenv==1.2.1 +python-json-logger==4.0.0 +pytz==2025.2 +PyYAML==6.0.3 +pyzmq==27.1.0 +referencing==0.37.0 +requests==2.32.5 +rfc3339-validator==0.1.4 +rfc3986-validator==0.1.1 +rfc3987-syntax==1.1.0 +rpds-py==0.27.1 +Send2Trash==1.8.3 +setuptools==80.9.0 +six==1.17.0 +sniffio==1.3.1 +soupsieve==2.8 +stack-data==0.6.3 +sympy==1.14.0 +terminado==0.18.1 +tinycss2==1.4.0 +tornado==6.5.2 +tqdm==4.67.1 +traitlets==5.14.3 +typing_extensions==4.15.0 +tzdata==2025.2 +uri-template==1.3.0 +urllib3==2.5.0 +wcwidth==0.2.14 +webcolors==24.11.1 +webencodings==0.5.1 +websocket-client==1.9.0 +widgetsnbextension==4.0.14 \ No newline at end of file diff --git a/yolo_det.py b/yolo_det.py new file mode 100644 index 0000000000..d9e04692c1 --- /dev/null +++ b/yolo_det.py @@ -0,0 +1,158 @@ + + +# 👇 新增优化参数 +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