From d100839037bafb1ffb8f8fd291293c78da433e8c Mon Sep 17 00:00:00 2001 From: abc123335 <1607504213@qq.com> Date: Sun, 21 Dec 2025 17:44:09 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=B8=BB=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=EF=BC=8C=E5=B9=B6=E6=8F=90=E4=BA=A4=E6=95=88=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lane_visualize/main.py | 307 +++++++++++++++++++++++-------------- 1 file changed, 193 insertions(+), 114 deletions(-) diff --git a/src/lane_visualize/main.py b/src/lane_visualize/main.py index 0415c06c85..33510d82d5 100644 --- a/src/lane_visualize/main.py +++ b/src/lane_visualize/main.py @@ -1,171 +1,250 @@ import cv2 import numpy as np import sys -from collections import deque import time - -# 引入我们刚才写的 YOLO 模块 +import os +import math +from collections import deque 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 +# ================= 配置区 ================= +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 LaneDetector: +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_lines_buffer = deque(maxlen=10) - self.right_lines_buffer = deque(maxlen=10) + self.left_fit_avg = None + self.right_fit_avg = None 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_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) - self.vertices = np.array([[bl, tl, tr, br]], dtype=np.int32) - cv2.fillPoly(mask, self.vertices, 255) - return cv2.bitwise_and(img, mask) + def get_lane_info(self, frame): + """返回: (lane_mask, deviation_percent, curvature_angle)""" + if frame is None: return None, 0, 0 - 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) edges = cv2.Canny(blur, CANNY_LOW, CANNY_HIGH) # 2. ROI - roi = self.region_of_interest(edges) + 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, HOUGH_THRESH, - minLineLength=MIN_LINE_LEN, - maxLineGap=MAX_LINE_GAP) - - # 4. 计算与平滑 - left_raw, right_raw = self.average_slope_intercept(lines) - left_avg, right_avg = self.smooth_lines(left_raw, right_raw) - - # 5. 绘制 - height = frame.shape[0] - 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) - - return self.draw_lane(frame, left_pts, right_pts) - - def average_slope_intercept(self, lines): - left_lines = [] - right_lines = [] + 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) > 10: continue + if abs(slope) < 0.3 or abs(slope) > 5: continue if slope < 0: - left_lines.append((slope, intercept)) + lefts.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 - - 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 - - def make_line_points(self, line, y_min, y_max): + 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 - slope, intercept = line - if abs(slope) < 1e-3: return None + s, i = line + if abs(s) < 1e-3: return None try: - x_min = int((y_min - intercept) / slope) - x_max = int((y_max - intercept) / slope) - return ((x_min, y_min), (x_max, y_max)) + return ((int((y1 - i) / s), y1), (int((y2 - i) / s), y2)) 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) - # 注意:这里我们返回 lane_img (纯遮罩),而不是叠加后的图,方便后面统一叠加 - return lane_img + +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(): - input_source = sys.argv[1] if len(sys.argv) > 1 else "sample.hevc" - cap = cv2.VideoCapture(input_source) - if not cap.isOpened(): - print("无法打开视频") - return + source = sys.argv[1] if len(sys.argv) > 1 else "sample.hevc" + cap = cv2.VideoCapture(source) + if not cap.isOpened(): return - # 1. 初始化两个检测器 - lane_detector = LaneDetector() - yolo_detector = ObjectDetector(model_name='yolov8n.pt') # 首次运行会自动下载 6MB 的模型 + lane_sys = LaneSystem() + yolo_sys = ObjectDetector() + logger = EventLogger() - print("系统启动中...") + print("🚀 AutoPilot V3.0: 启动虚拟控制与取证系统...") + + frame_count = 0 + current_dets = [] while True: - start_time = time.time() + t_start = time.time() ret, frame = cap.read() if not ret: cap.set(cv2.CAP_PROP_POS_FRAMES, 0) continue - # ----------------------------------------------------- - # 步骤 A: 运行 YOLO 车辆检测 (返回画好框的图) - # ----------------------------------------------------- - frame_with_cars = yolo_detector.detect(frame) + 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) - # ----------------------------------------------------- - # 步骤 B: 运行车道线检测 (返回纯车道层) - # ----------------------------------------------------- - lane_layer = lane_detector.process_lane(frame) + # --- 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) - # ----------------------------------------------------- - # 步骤 C: 合并图层 - # ----------------------------------------------------- - # 将半透明车道层 叠加到 画了车的图上 - final_result = cv2.addWeighted(frame_with_cars, 1, lane_layer, 0.4, 0) + # --- 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) - # 计算并显示 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) + # --- 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 System V2.0', final_result) + cv2.imshow('AutoPilot V3.0 - Control Dashboard', display) - if cv2.waitKey(1) & 0xFF == ord('q'): - break + frame_count += 1 + if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() if __name__ == "__main__": - main() + main() \ No newline at end of file From 27cddedfe6bf2d06a24bea2c766fff2de5885523 Mon Sep 17 00:00:00 2001 From: abc123335 <1607504213@qq.com> Date: Sun, 21 Dec 2025 21:38:32 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86=E6=96=B0?= =?UTF-8?q?=E7=9A=84README.md=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lane_visualize/README.md | 113 ++++++++++++++--------------------- 1 file changed, 44 insertions(+), 69 deletions(-) diff --git a/src/lane_visualize/README.md b/src/lane_visualize/README.md index 4eaf7b121c..d3553309e5 100644 --- a/src/lane_visualize/README.md +++ b/src/lane_visualize/README.md @@ -1,125 +1,100 @@ -# AutoLane: OpenCV 车道线检测系统 +# AutoPilot System: Lane & Object Detection -这是一个基于 Python 和 OpenCV 的自动驾驶车道线检测项目。它不依赖深度学习模型,而是通过经典的计算机视觉算法(Canny 边缘检测、霍夫变换、色彩过滤)来实现车道线的实时识别与追踪。 +这是一个结合了**传统计算机视觉**与**深度学习**的自动驾驶辅助系统原型。它利用 OpenCV 进行高精度的车道线检测,并集成 YOLOv8 模型实现对车辆、行人等障碍物的实时识别。 -本项目包含一个强大的**实时调参工具 (`tuner.py`)**,允许开发者在视频播放过程中动态调整算法参数,从而快速适配不同的光照和道路环境。 +![Status](https://img.shields.io/badge/Status-Active-success) +![Python](https://img.shields.io/badge/Python-3.8%2B-blue) -## ✨ 功能特性 +## ✨ 核心功能 -* **视觉管线**: 灰度化 -> 高斯模糊 -> Canny 边缘检测 -> ROI 掩码 -> 霍夫直线变换。 -* **实时调参器**: 提供可视化滑动条界面,实时寻找最佳的 Canny 阈值和 ROI 区域。 -* **鲁棒性优化**: - * **斜率过滤**: 剔除水平线(阴影)和垂直线(路标)。 - * **历史帧平滑**: 使用 `deque` 队列对最近 10 帧数据取平均,消除抖动。 - * **动态 ROI**: 可配置梯形区域,仅关注路面。 +### 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) -* PyCharm 或 VS Code +* 建议使用独立显卡 (GPU) 以获得更高帧率,但在 CPU 上也能运行 (FPS 5-10)。 ### 2. 安装依赖 -请在项目根目录下运行: +请确保安装了最新版本的依赖库(包含 ultralytics): ```bash pip install -r requirements.txt ``` -*(如果尚未创建 `requirements.txt`,手动安装: `pip install opencv-python numpy matplotlib`)* - -### 3. 准备数据 - -请确保项目目录下有测试视频文件(例如 `sample.hevc` 或 `.mp4` 文件)。 - -* 如果你没有视频,可以从 [这里下载 sample.hevc](https://drive.google.com/file/d/1hP-v8lLn1g1jEaJUBYJhv1mEb32hkMvG/view?usp=sharing)。 +## 🚀 使用指南 ---- +### 第一步:视觉参数调优 (可选但推荐) -## 🚀 使用指南 (完整流程) - -### 第一步:使用 Tuner 寻找最佳参数 - -不同的视频(白天/夜晚/阴天)需要不同的参数。运行调参工具来获得最佳效果: +如果发现车道线检测不准(乱飞或消失),请先运行调参工具: ```bash python tuner.py sample.hevc ``` -**操作方法:** - -1. **空格键**: 暂停视频,观察当前帧。 -2. **滑动条**: -* `Canny Low/High`: 调整边缘检测的灵敏度(左侧黑白窗口)。 -* `ROI Top W / Height`: 调整红色梯形框的大小,确保它只包裹车道线,避开天空和树木。 -* `Hough Thresh`: 调整识别直线的严格程度(右侧绿色线条)。 - +* 按 `空格` 暂停,拖动滑动条调整 Canny 阈值和 ROI 区域。 +* 记下最佳参数,并填入 `main.py` 顶部的配置区。 -3. **记录参数**: 记下效果最好时的 6 个数值。 +### 第二步:启动自动驾驶系统 -### 第二步:更新主程序 - -打开 `main.py`,找到顶部的 **【参数配置区】**,将你刚刚记下的数值填入: - -```python -# main.py 顶部 -CANNY_LOW = 50 # <--- 填入你的数值 -CANNY_HIGH = 150 -ROI_TOP_WIDTH = 0.40 -... - -``` - -### 第三步:运行车道检测 - -参数配置完成后,运行主程序查看最终效果: +直接运行主程序,系统将同时加载车道检测器和 YOLO 模型: ```bash python main.py sample.hevc ``` -* **按 `q` 键**: 退出程序。 - ---- +* **首次运行提示**: 程序会自动下载 `yolov8n.pt` 模型权重文件 (约 6MB),请保持网络连接。 ## 📂 项目结构 ```text Project_Root/ -├── main.py # [核心] 车道检测主程序 (需填入调优后的参数) -├── tuner.py # [工具] 可视化调参工具 (含滑动条) -├── requirements.txt # 依赖库列表 +├── main.py # [入口] 主程序,协调车道与YOLO检测 +├── yolo_det.py # [模块] 封装 YOLOv8 目标检测逻辑 +├── tuner.py # [工具] 视觉参数调试器 +├── requirements.txt # 依赖清单 ├── README.md # 项目文档 -└── sample.hevc # 测试视频文件 +└── sample.hevc # 测试数据 ``` ## ⚠️ 常见问题 -**Q1: 运行 `tuner.py` 或 `main.py` 时报错 `Assertion failed` 或无法打开视频?** +**Q1: 画面非常卡顿 (Low FPS)?** -* **原因**: OpenCV 可能缺少 HEVC (H.265) 解码器。 -* **解决**: 将 `sample.hevc` 转换为 `.mp4` 格式,或者重新编译安装带 FFmpeg 支持的 OpenCV。 +* **原因**: 深度学习模型在没有 GPU 加速的电脑上运行较慢。 +* **优化**: 可以在 `main.py` 中实现“跳帧机制”(例如每 3 帧跑一次 YOLO,中间帧沿用结果)。 -**Q2: 检测框乱跳或消失?** +**Q2: 报错 `ModuleNotFoundError: No module named 'ultralytics'`?** -* **原因**: 参数不适合当前视频的光照。 -* **解决**: 请务必先运行 `tuner.py`,针对当前视频调整 `Canny` 阈值和 `ROI` 范围。 +* **解决**: 请运行 `pip install -r requirements.txt` 确保库已安装。 ## 🔮 路线图 (Roadmap) * [x] 基础 OpenCV 车道线检测 -* [x] 增加可视化调参工具 -* [x] 增加帧间平滑 (Smoothing) -* [ ] 集成 YOLOv8 进行车辆/行人目标检测 -* [ ] 结合深度学习实现端到端控制 (类似 comma.ai) +* [x] 可视化参数调试工具 (`tuner.py`) +* [x] 帧间平滑与防抖算法 +* [x] 集成 YOLOv8 目标检测 (`yolo_det.py`) +* [ ] 性能优化:加入多线程或跳帧处理 +* [ ] 车辆距离估算 (基于检测框大小) +* [ ] 偏离预警系统 (LDW) ## 📚 参考资料 -* [OpenCV Documentation](https://www.google.com/search?q=https://docs.opencv.org/) -* [Hough Circle Transform Explained](https://docs.opencv.org/3.4/d4/d70/tutorial_hough_circle.html) +* [Ultralytics YOLOv8 Docs](https://docs.ultralytics.com/) +* [OpenCV Computer Vision](https://opencv.org/) ``` From a8d6cbaf2d8d8c88324e70b838f68e3306521471 Mon Sep 17 00:00:00 2001 From: abc123335 <1607504213@qq.com> Date: Mon, 22 Dec 2025 09:53:07 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E4=BA=86yolo.det.py?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lane_visualize/yolo_det.py | 54 +++++++++------------------------- 1 file changed, 14 insertions(+), 40 deletions(-) diff --git a/src/lane_visualize/yolo_det.py b/src/lane_visualize/yolo_det.py index d37aa118aa..1d9a794176 100644 --- a/src/lane_visualize/yolo_det.py +++ b/src/lane_visualize/yolo_det.py @@ -2,59 +2,33 @@ import cv2 import numpy as np - class ObjectDetector: def __init__(self, model_name='yolov8n.pt'): - """ - 初始化 YOLO 模型 - :param model_name: 模型名称,'yolov8n.pt' 是最快最小的模型,适合CPU - """ - print(f"正在加载 YOLO 模型: {model_name} (首次运行会自动下载)...") self.model = YOLO(model_name) - - # 定义我们关心的类别ID (COCO数据集) - # 2: car, 3: motorcycle, 5: bus, 7: truck + # 目标: 2=Car, 3=Motor, 5=Bus, 7=Truck self.target_classes = [2, 3, 5, 7] def detect(self, frame): """ - 输入一帧图像,返回画好框的图像 + 返回: (original_frame, detections_list) """ - # 运行推理,stream=True 节省内存,verbose=False 关闭打印日志 results = self.model(frame, stream=True, verbose=False) - - # 在原图的副本上绘图 - annotated_frame = frame.copy() + detections = [] for result in results: boxes = result.boxes for box in boxes: - # 获取类别ID cls_id = int(box.cls[0]) - - # 只处理车辆相关的类别 if cls_id in self.target_classes: - # 获取坐标 (x1, y1, x2, y2) - x1, y1, x2, y2 = box.xyxy[0] - x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) - - # 获取置信度 + x1, y1, x2, y2 = map(int, box.xyxy[0]) conf = float(box.conf[0]) - if conf < 0.3: continue # 过滤掉置信度低的 - - # 获取类别名称 - cls_name = self.model.names[cls_id] - - # 绘制矩形框 (红色) - cv2.rectangle(annotated_frame, (x1, y1), (x2, y2), (0, 0, 255), 2) - - # 绘制标签背景 - label = f"{cls_name} {conf:.2f}" - (w, h), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1) - cv2.rectangle(annotated_frame, (x1, y1 - 20), (x1 + w, y1), (0, 0, 255), -1) - - # 绘制文字 - cv2.putText(annotated_frame, label, (x1, y1 - 5), - cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1) - - return annotated_frame \ No newline at end of file + 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