Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
20e276a
更新代码
183899 Dec 1, 2025
91bf847
Merge branch 'OpenHUTB:main' into main
183899 Dec 1, 2025
08bccf0
Merge branch 'OpenHUTB:main' into main
183899 Dec 8, 2025
4a2ccc8
将原模块名更改为更加详细的名字
183899 Dec 8, 2025
da1e375
Merge branch 'OpenHUTB:main' into main
183899 Dec 15, 2025
1678aeb
Merge branch 'OpenHUTB:main' into main
183899 Dec 15, 2025
2b3e4b2
更新了控制器功能,增添了速度显示效果
183899 Dec 15, 2025
10f7798
Merge branch 'OpenHUTB:main' into main
183899 Dec 15, 2025
1f64807
补充并在原有的 drawer.py 中添加了 draw_point 方法
183899 Dec 16, 2025
b21c1f2
Merge branch 'OpenHUTB:main' into main
183899 Dec 16, 2025
c9d3f89
添加传感器数据采集模块
Dec 18, 2025
0d5b42a
模块化PID控制优化
0023csw Dec 18, 2025
9f0723c
Merge branch 'OpenHUTB:main' into main
pppzzz222 Dec 18, 2025
737c4bd
增加传感器数据采集模型及运行效果
Dec 18, 2025
cebf093
Merge branch 'OpenHUTB:main' into main
183899 Dec 18, 2025
417561d
添加新的转弯感知模块,修改main函数实现转弯时自主检测刹车
183899 Dec 18, 2025
8728dc7
Merge branch 'OpenHUTB:main' into main
pppzzz222 Dec 18, 2025
c15f438
修改readme文件
Dec 18, 2025
34dd8d3
Merge branch 'OpenHUTB:main' into main
pppzzz222 Dec 19, 2025
10fb819
添加LiDAR-IMU紧耦合里程计模块
Dec 19, 2025
837902a
简洁化与环境管理优化
0023csw Dec 19, 2025
a091bb1
Merge branch 'OpenHUTB:main' into main
pppzzz222 Dec 19, 2025
170d62e
Merge branch 'OpenHUTB:main' into main
pppzzz222 Dec 19, 2025
9027bb5
Merge branch 'OpenHUTB:main' into main
pppzzz222 Dec 19, 2025
a92adb9
更新readme文件
Dec 19, 2025
f756b9f
提交选题
ou-yang220 Dec 19, 2025
4ced0d4
Merge branch 'OpenHUTB:main' into main
pppzzz222 Dec 19, 2025
3b3f9b8
添加因子图优化后端模块代码
Dec 19, 2025
c24646f
docs: 新增README.md,记录车路协同与边缘智能研究方向
ou-yang220 Dec 19, 2025
0a46f15
docs: 修改文件名
ou-yang220 Dec 19, 2025
dcd6cb6
feat: 车路协同与边缘智能 路侧YOLOv7轻量化模型
ou-yang220 Dec 19, 2025
033223e
feat: 车路协同与边缘智能 路侧YOLOv7轻量化模型
ou-yang220 Dec 19, 2025
7dfbfea
PID控制器再度提升清晰度优化
0023csw Dec 19, 2025
90502cf
Merge branch 'main' into main
0023csw Dec 19, 2025
b989886
config: 新增.idea/忽略规则,解决中文配置文件追踪问题
ou-yang220 Dec 19, 2025
1326c3e
Merge branch 'OpenHUTB:main' into main
pppzzz222 Dec 19, 2025
3ac7328
更新README文件
Dec 19, 2025
8be8c36
fix: 解决.gitignore和dataloader.py合并冲突,适配车路协同主题
ou-yang220 Dec 19, 2025
c6797db
feat: 车路协同与边缘智能 路侧YOLOv7模型新增V2X通信功能
ou-yang220 Dec 19, 2025
8a066a7
fix: 解决.idea/中文路径解析异常,忽略车路协同相关伪路径
ou-yang220 Dec 19, 2025
ebbb588
Merge branch 'OpenHUTB:main' into main
183899 Dec 19, 2025
532b4ab
新增预测功能,提前预测转弯效果
183899 Dec 19, 2025
05bd688
fix: 忽略.idea/文件夹,清理车路协同主题项目状态
ou-yang220 Dec 19, 2025
ae3ff1e
FadingText透明度计算优化
0023csw Dec 19, 2025
02776d0
feat: 车路协同与边缘智能 路侧YOLOv7模型新增V2X通信功能
ou-yang220 Dec 19, 2025
3a4253b
车路协同(V2X)+边缘智能 路侧感知轻量化模型
ou-yang220 Dec 19, 2025
0cd6a4c
添加新的检测模块
abc123335 Dec 19, 2025
3269feb
Merge branch 'main' of https://github.com/abc123335/nn
abc123335 Dec 19, 2025
ce54974
Merge pull request #3955 from 0023csw/main
William4861 Dec 19, 2025
aa9599b
Merge pull request #3959 from abc123335/main
William4861 Dec 19, 2025
fa24369
重新更新了主程序文件
abc123335 Dec 19, 2025
763de6f
Merge branch 'main' of https://github.com/abc123335/nn
abc123335 Dec 19, 2025
f5f36d5
Merge pull request #3949 from pppzzz222/main
Arrebol01 Dec 19, 2025
896affd
Merge pull request #3961 from abc123335/main
William4861 Dec 19, 2025
9a72249
Merge pull request #3957 from ou-yang220/main
William4861 Dec 19, 2025
39c052d
Merge pull request #3954 from 183899/main
William4861 Dec 19, 2025
c4e7579
Add files via upload
heeyusike Dec 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .gitignore
Binary file not shown.
258 changes: 152 additions & 106 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,118 +1,164 @@
#!/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

# 引入我们刚才写的 YOLO 模块
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
# ==========================================

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_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 process_lane(self, frame):
if frame is None: return frame

# 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)

# 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 = []
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 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))
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):
if line is None: return None
slope, intercept = line
if abs(slope) < 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))
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 main():
input_source = sys.argv[1] if len(sys.argv) > 1 else "sample.hevc"
cap = cv2.VideoCapture(input_source)
if not cap.isOpened():
print(f"错误:无法打开视频 → {video_path}")
print("无法打开视频")
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:

# 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:
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'):
if not ret:
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
continue

# -----------------------------------------------------
# 步骤 A: 运行 YOLO 车辆检测 (返回画好框的图)
# -----------------------------------------------------
frame_with_cars = yolo_detector.detect(frame)

# -----------------------------------------------------
# 步骤 B: 运行车道线检测 (返回纯车道层)
# -----------------------------------------------------
lane_layer = lane_detector.process_lane(frame)

# -----------------------------------------------------
# 步骤 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

count += 1

# 释放资源

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()
29 changes: 29 additions & 0 deletions src/Driving_Accident_Video_Recognition/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
全局配置文件:所有可配置参数集中管理,新手只需修改这里
"""
# YOLOv8模型配置
YOLO_MODEL_PATH = "yolov8n.pt" # 轻量化模型(自动下载)
CONFIDENCE_THRESHOLD = 0.5 # 目标检测置信度阈值(0-1)

# 检测源配置
DETECTION_SOURCE = 0 # 0=电脑摄像头,可改为视频路径如"test_accident.mp4"

# 事故识别配置
ACCIDENT_CLASSES = [0, 2, 7] # YOLOv8类别:0=人,2=汽车,7=卡车
MIN_VEHICLE_COUNT = 2 # 至少2辆车判定为事故
PERSON_VEHICLE_CONTACT = True # 行人和车辆同时出现判定为事故

# 帧处理优化配置(低配电脑推荐)
RESIZE_WIDTH = 640
RESIZE_HEIGHT = 480

# 依赖包配置
REQUIRED_PACKAGES = [
"ultralytics>=8.0.0",
"opencv-python>=4.8.0",
"numpy>=1.24.0",
"torch>=2.0.0"
]

# 清华镜像源(加速依赖下载)
PYPI_MIRROR = "https://pypi.tuna.tsinghua.edu.cn/simple"
55 changes: 55 additions & 0 deletions src/Driving_Accident_Video_Recognition/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""
主程序入口:整合所有模块,支持命令行参数控制
"""
import sys
import os
import argparse # 新增:导入命令行解析模块
# 关键:将当前脚本所在目录加入Python模块搜索路径
current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(current_dir)

from config import REQUIRED_PACKAGES, PYPI_MIRROR, SUPPORTED_LANGUAGES
from utils.dependencies import install_dependencies
from core.detector import AccidentDetector

def parse_args():
"""新增:解析命令行参数"""
parser = argparse.ArgumentParser(description="驾驶事故视频识别工具")
# 检测源:-s/--source,支持摄像头(数字)或视频路径(字符串)
parser.add_argument(
"--source", "-s",
default=None,
help=f"检测源(0=摄像头,或视频路径如'test.mp4'),默认使用config.py配置"
)
# 语言:-l/--lang,支持中文(zh)、英文(en)
parser.add_argument(
"--lang", "-l",
default="zh",
choices=SUPPORTED_LANGUAGES,
help=f"标注语言,支持{SUPPORTED_LANGUAGES},默认中文(zh)"
)
return parser.parse_args()

def main():
"""主函数:执行依赖安装 → 解析参数 → 启动检测"""
args = parse_args() # 新增:解析命令行参数
try:
print("🚀 启动驾驶事故视频识别工具...")
# 第一步:自动安装依赖
install_dependencies(REQUIRED_PACKAGES, PYPI_MIRROR)
# 第二步:初始化检测器
detector = AccidentDetector()
# 第三步:启动检测(传递命令行参数:检测源、语言)
detector.run_detection(
source=args.source, # 优先使用命令行指定的检测源
language=args.lang # 传递语言参数
)
except KeyboardInterrupt:
print("\n🛑 用户强制中断程序")
except Exception as e:
print(f"\n❌ 程序运行出错:{e}")
finally:
print("👋 程序正常退出")

if __name__ == "__main__":
main()
Loading
Loading