Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
276 changes: 106 additions & 170 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,182 +1,118 @@
import sys
import os
# 替换绝对路径为相对路径:基于main.py所在目录向上找src文件夹(适配任意部署环境)
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..'))
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Lane Line Detection (精简版)
核心功能:Canny边缘检测 + 霍夫变换实现车道线检测,单视频处理+结果保存
适用环境:Ubuntu (Python 3.10 + OpenCV + numpy)
"""

import numpy as np
import cv2
from tensorflow.keras.models import load_model
from common.transformations.camera import transform_img, eon_intrinsics
from common.transformations.model import medmodel_intrinsics
from common.tools.lib.parser import parser

# 关闭TensorFlow所有冗余警告
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

# -------------------------- 核心工具函数 --------------------------
def frames_to_tensor(frames):
if len(frames) == 0:
return np.array([])
H = (frames.shape[1] * 2) // 3
W = frames.shape[2]
tensor = np.zeros((frames.shape[0], 6, H//2, W//2), dtype=np.float32)
tensor[:, 0] = frames[:, 0:H:2, 0::2]
tensor[:, 1] = frames[:, 1:H:2, 0::2]
tensor[:, 2] = frames[:, 0:H:2, 1::2]
tensor[:, 3] = frames[:, 1:H:2, 1::2]
tensor[:, 4] = frames[:, H:H+H//4].reshape((-1, H//2, W//2))
tensor[:, 5] = frames[:, H+H//4:H+H//2].reshape((-1, H//2, W//2))
return tensor / 128.0 - 1.0

def preprocess_frames(imgs):
if not imgs:
return np.array([])
processed = np.zeros((len(imgs), 384, 512), dtype=np.uint8)
for i, img in enumerate(imgs):
try:
processed[i] = transform_img(img, from_intr=eon_intrinsics, to_intr=medmodel_intrinsics, yuv=True, output_size=(512, 256))
except:
processed[i] = np.zeros((384, 512), dtype=np.uint8)
return frames_to_tensor(processed)

# -------------------------- 主函数(无参数、全英文、车道线优化) --------------------------
def main():
# 1. 初始化显示窗口(800x600,固定尺寸)
win_name = "Lane Line Prediction (Blue=Left | Red=Right | Green=Path)"
cv2.namedWindow(win_name, cv2.WINDOW_NORMAL)
cv2.resizeWindow(win_name, 800, 600)

# 2. 读取视频(修改为相对路径:main.py所在文件夹下的sample.hevc)
video_path = "./sample.hevc" # 仅改这里:绝对路径→相对路径
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:
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():
empty_frame = np.ones((600, 800, 3), dtype=np.uint8) * 255
cv2.putText(empty_frame, "Cannot open video", (150, 300), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 255), 3)
cv2.imshow(win_name, empty_frame)
cv2.waitKey(0)
cv2.destroyAllWindows()
print(f"错误:无法打开视频 → {video_path}")
return

# 读取前10帧(用于推理,保留原始帧和模型输入帧)
raw_display_frames = [] # 用于显示的800x600帧
model_input_imgs = [] # 用于模型的512x384 YUV帧
for _ in range(10):

# 获取视频基础信息
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:
ret, frame = cap.read()
if not ret:
break
# 缩放为显示尺寸(800x600)
display_frame = cv2.resize(frame, (800, 600))
raw_display_frames.append(display_frame)
# 转换为模型需要的YUV格式并缩放
yuv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2YUV_I420)
model_frame = cv2.resize(yuv_frame, (512, 384), cv2.INTER_AREA)
model_input_imgs.append(model_frame)
cap.release()

# 校验帧数是否足够
if len(raw_display_frames) < 2:
empty_frame = np.ones((600, 800, 3), dtype=np.uint8) * 255
cv2.putText(empty_frame, "Insufficient video frames", (100, 300), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 0, 255), 3)
cv2.imshow(win_name, empty_frame)
cv2.waitKey(0)
cv2.destroyAllWindows()
return

# 3. 加载模型(显示英文提示,无乱码)
load_frame = np.ones((600, 800, 3), dtype=np.uint8) * 255
cv2.putText(load_frame, "Loading model...", (200, 300), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 255, 0), 3)
cv2.imshow(win_name, load_frame)
cv2.waitKey(200) # 刷新显示

# 模型路径(修改为相对路径:main.py所在文件夹下的models/supercombo.h5)
model_path = "./models/supercombo.h5" # 仅改这里:绝对路径→相对路径
try:
supercombo_model = load_model(model_path, compile=False)
except Exception as e:
empty_frame = np.ones((600, 800, 3), dtype=np.uint8) * 255
cv2.putText(empty_frame, "Model load failed", (180, 300), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 255), 3)
cv2.imshow(win_name, empty_frame)
cv2.waitKey(0)
cv2.destroyAllWindows()
return

# 4. 预处理帧(显示英文提示)
preprocess_frame = np.ones((600, 800, 3), dtype=np.uint8) * 255
cv2.putText(preprocess_frame, "Preprocessing frames...", (150, 300), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 255, 0), 3)
cv2.imshow(win_name, preprocess_frame)
cv2.waitKey(200)

frame_tensors = preprocess_frames(model_input_imgs)
if frame_tensors.size == 0:
empty_frame = np.ones((600, 800, 3), dtype=np.uint8) * 255
cv2.putText(empty_frame, "Preprocessing failed", (180, 300), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 255), 3)
cv2.imshow(win_name, empty_frame)
cv2.waitKey(0)
cv2.destroyAllWindows()
return

# 5. 模型状态初始化
model_state = np.zeros((1, 512))
model_desire = np.zeros((1, 8))

# 6. 逐帧推理+绘制(核心优化:车道线右移+放大圆点)
print("✅ Start inference and display (Press Q to exit)")
for i in range(len(frame_tensors) - 1):
# 确保帧存在,避免索引越界
if i >= len(raw_display_frames):
current_frame = np.ones((600, 800, 3), dtype=np.uint8) * 255
else:
current_frame = raw_display_frames[i].copy() # 复制原始帧,避免修改

try:
# 模型推理(连续两帧作为输入)
input_data = [np.vstack(frame_tensors[i:i+2])[None], model_desire, model_state]
model_output = supercombo_model.predict(input_data, verbose=0)
parsed_result = parser(model_output)
model_state = model_output[-1]

# -------------------------- 车道线绘制优化 --------------------------
# 提取模型输出的车道线/路径x坐标
left_lane_x = parsed_result["lll"][0]
right_lane_x = parsed_result["rll"][0]
path_x = parsed_result["path"][0]

# 窗口尺寸
win_h, win_w = 600, 800
# y坐标映射(0-191 → 0-599)
y_points = np.linspace(0, win_h - 1, 192).astype(int)
# x坐标映射(0-512 → 0-799)+ 右移100像素(解决偏左问题)+ 放大圆点到8px
left_x_mapped = (left_lane_x / 512 * win_w + 100).astype(int)
right_x_mapped = (right_lane_x / 512 * win_w + 100).astype(int)
path_x_mapped = (path_x / 512 * win_w + 100).astype(int)

# 绘制左车道线(蓝色,8px实心圆)
for x, y in zip(left_x_mapped, y_points):
if 0 <= x < win_w and 0 <= y < win_h:
cv2.circle(current_frame, (x, y), 8, (255, 0, 0), -1)
# 绘制右车道线(红色,8px实心圆)
for x, y in zip(right_x_mapped, y_points):
if 0 <= x < win_w and 0 <= y < win_h:
cv2.circle(current_frame, (x, y), 8, (0, 0, 255), -1)
# 绘制预测路径(绿色,6px实心圆)
for x, y in zip(path_x_mapped, y_points):
if 0 <= x < win_w and 0 <= y < win_h:
cv2.circle(current_frame, (x, y), 6, (0, 255, 0), -1)

except Exception as e:
# 推理失败时仅打印错误,仍显示原始帧
print(f"⚠️ Frame {i+1} inference error: {str(e)[:30]}")

# 强制显示当前帧
cv2.imshow(win_name, current_frame)
# 按Q退出
if cv2.waitKey(100) & 0xFF == ord('q'):
print("🛑 Exit by user (Q pressed)")

# 核心:检测车道线
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

# 7. 程序收尾

count += 1

# 释放资源
cap.release()
writer.release()
cv2.destroyAllWindows()
print("🎉 All frames processed successfully!")
print(f"处理完成!结果保存至 → {result_path}")

# ===================== 主函数(精简参数) =====================
if __name__ == "__main__":
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)
Loading
Loading