环境配置:python=3.6,opencv-python=4.4.0,torch=1.7.1(cuda=11.0),PyQt5=5.15.1(缺啥补啥)
PyQt5功能:实现选择视频文件、播放、中止、暂停,继续播放
检测功能:障碍物检测(YOLOv3),车道线检测(LANEATT)
简单流程:对读取的每一帧图片分别进行障碍物检测和车道线检测,然后利用PyQt5将检测结果进行可视化
1、car_detector文件夹,YOLOv3检测模型
2、cfgs文件夹,包含car.yml与lane.yml文件
如:car.yml文件内容
# Model settings
seed: 0
model:
weights: 'weights/car/yolov3.weights' # pre-trained weights path
type: 'car_detector/yolov3.cfg' # model configuration type path
classes: 'car_detector/coco.names' # path to CoCo-class-label file
conf_thres: 0.6 # object detection confidence threshold
nms_thres: 0.6 # non-maximal-suppression threshold
im_size: 640 # test image size
3、data文件夹,输入文件路径,包括视频和图片
4、lane_detector文件夹,LANEATT车道线检测模型
5、output文件夹,输出路径
6、utils文件夹,组件文件,如预处理程序等
7、weights文件夹,放置权重文件路径
8、_init_paths.py,可以避免不同目录的py调用出现模块不存在情况
9、app.py,调用GUI界面,通过可视化操作进行检测
10、couter.py,实现GUI播放-停止-暂停-继续播放的关系连接(在同一个视频中),同时调用检测函数
11、detect.py,无GUI界面检测程序(在原代码文件为main.py)
12、detect_def.py,实现检测的代码,通过输入图像、模型参数等,输出检测结果图像
13、gui.py,GUI界面制作
注意:针对于无GUI的原代码和先前写的GUI界面调用opencv车道线检测的修改
无GUI的原障碍物于车道线检测代码链接:https://github.com/v18nguye/Joint_Lane_And_Vehicle_Detection
先前写的GUI界面调用opencv车道线检测链接:https://blog.csdn.net/weixin_45679938/article/details/119087412?spm=1001.2014.3001.5501
修改部分:counter.py、detect_def.py
1、counter.py:
修改部分:添加了调用模块、_init_添加了初始化函数、run()中更改了检测函数、结尾添加了parse_args()
# coding:utf-8
# 可自己在run()进行修改,得到自己想要的函数
# ## 原生GUI部分
import cv2
from PyQt5.QtCore import QThread, pyqtSignal
import time
import numpy as np
# 检测车道线与车辆的模块
from detect_def import main
import argparse
import torch
from car_detector.config import CarConfig
from car_detector.model import CarDetector
from lane_detector.config import LaneConfig
from lane_detector.model import LaneDetector
class CounterThread(QThread):
sin_Result = pyqtSignal(np.ndarray)
sin_runningFlag = pyqtSignal(int)
sin_videoList = pyqtSignal(list)
sin_done = pyqtSignal(int)
sin_pauseFlag = pyqtSignal(int)
def __init__(self):
super(CounterThread, self).__init__()
self.running_flag = 0
self.pause_flag = 0
self.videoList = []
self.sin_runningFlag.connect(self.update_flag)
self.sin_videoList.connect(self.update_videoList)
self.sin_pauseFlag.connect(self.update_pauseFlag)
# #################
# ## 添加车道线检测与YOLO检测的初始化功能
# #################
self.args = self.parse_args()
# Config torch device
self.device = torch.device('cuda')
# Load a pre-trained lane detection model
print('Loading lane detection model and its configuration: ', self.args.lane_cfg_path)
self.lane_cfg = LaneConfig(self.args.lane_cfg_path)
self.lane = LaneDetector(self.lane_cfg, self.device)
# Load a pre-trained car detection model
print('Loading car detection model and its configuration: ', self.args.lane_cfg_path)
self.car_cfg = CarConfig(self.args.car_cfg_path)
self.car = CarDetector(self.car_cfg, self.device)
def run(self):
for video in self.videoList:
cap = cv2.VideoCapture(video)
frame_count = 0
while cap.isOpened():
if self.running_flag:
if not self.pause_flag:
ret, frame = cap.read()
if ret:
if frame_count % 1 == 0:
a1 = time.time()
# 检测函数
frame = main(frame, self.lane_cfg, self.car_cfg, self.lane, self.car)
self.sin_Result.emit(frame)
# out.write(frame)
a2 = time.time()
# a2与a1差值太小也报错
# print(f"fps: {1 / (a2 - a1):.2f}") # 代码运行帧率
frame_count += 1
else:
break
else:
time.sleep(0.1)
else:
break
cap.release()
# out.release()
cv2.destroyAllWindows()
if not self.running_flag:
break
if self.running_flag:
self.sin_done.emit(1)
def update_pauseFlag(self, flag):
self.pause_flag = flag
def update_flag(self, flag):
self.running_flag = flag
def update_videoList(self, videoList):
print("Update videoList!")
self.videoList = videoList
# #########
# 以下部分为车道检测与YOLO功能函数部分
# #########
def parse_args(self):
"""Argument Parser"""
self.parser = argparse.ArgumentParser(description="Car Lane Joint Detection")
self.parser.add_argument("-m", "--mode", choices=["image", "video"], default="image")
self.parser.add_argument("--fps", type=int, default=20, help='registered frames-per-second for videos')
self.parser.add_argument("-dp", "--data_path", default="data/images",
help="path to an image directory or a explicit path to a video")
self.parser.add_argument("-lcf", "--lane_cfg_path", default="cfgs/lane.yml",
help="Path to lane-model-config file")
self.parser.add_argument("-ccf", "--car_cfg_path", default="cfgs/car.yml",
help="Path to car-model-config file")
self.parser.add_argument("-odr", "--out_dir", default="output", help="Saving directory")
return self.parser.parse_args()
if __name__ == '__main__':
print('')
2、detect_def.py:
修改部分:将原检测代码detect.py中的图像检测进行了保留,其余删除,修改为一个函数,输入图像检测后,返回一个检测结果图像
import _init_paths
import cv2
from annotator import lane_ann, car_ann
from pre_processor import lane_prx2, car_prx2
def main(im, lane_cfg, car_cfg, lane, car):
####
# Process the image for lane detection
lane_im = lane_prx2(im, lane_cfg['model']['parameters']['img_h'],
lane_cfg['model']['parameters']['img_w'])
# Process the image for car detection
car_im = car_prx2(im, car_cfg['im_size'])
# Running detection on the processed image
lane_pred = lane.detect(lane_im)[0]
car_pred = car.detect(car_im)[0]
# Annotate the prediction
ann_im, lines = lane_ann(im, lane_pred)
ann_im = car_ann(ann_im, car_pred, car_cfg, car.coco, lines)
return ann_im
if __name__ == '__main__':
print('')
注意:
环境配置可参考车道线及障碍物检测的环境配置
以上代码文件链接:
链接:https://pan.baidu.com/s/1qllO_MunuCrSTDYl5it7EQ
提取码:aqfp
参考资料:
车道线及障碍物检测源代码:
https://github.com/v18nguye/Joint_Lane_And_Vehicle_Detection