前言

这篇博客针对《Python路面车道线识别偏离预警》编写代码,代码整洁,规则,易读。 学习与应用推荐首选。


运行结果

Python路面车道线识别偏离预警-LMLPHP


文章目录

一、所需工具软件
二、使用步骤
       1. 主要代码
       2. 运行结果
三、在线协助

一、所需工具软件

       1. Python
       2. Pycharm

二、使用步骤

代码如下(示例):

import os
from PyQt5.QtWidgets import  QMainWindow
import cv2
from .form.mainwindow import Ui_MainWindow
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import qdarkstyle
import json
import time
class MainWinddow(QMainWindow):
    print_debug_signal = pyqtSignal(str) # 用于触发打印调试信息函数
    show_image_signal = pyqtSignal(str) # 用于触发图像处理结果函数
    # 初始化函数
    def __init__(self,parent=None):
        super(MainWinddow,self).__init__(parent)
        self.main_ui = Ui_MainWindow()
        self.main_ui.setupUi(self)
        # 设置软件名称
        self.setWindowTitle("基于TWINLITENET算法的车道线与可行驶区域感知系统")
        # 设置登录界面第一行,第二行文字
        self.main_ui.label_up.setText("基于TWINLITENET算法的")
        self.main_ui.label_down.setText("车道线与可行驶区域感知系统")
        self.main_ui.label_up_2.setText("基于TWINLITENET算法的")
        self.main_ui.label_9.setText("车道线与可行驶区域感知系统")
        # 设置界面起始页
        self.main_ui.stackedWidget.setCurrentIndex(0)

        # # 设置界面初始状态
        # self.clear_label()
        # # 设置背景颜色
        # palette = qdarkstyle.palette.Palette()
        # palette.ID = "dark"
        # # 修改背景颜色为红色
        # palette.background = "#FF0000"  # 这里是十六进制颜色代码,代表红色
        #
        # self.setStyleSheet(qdarkstyle.load_stylesheet(qt_api="pyqt5", palette=palette))



        self.main_ui.textBrowser_debug.document().setMaximumBlockCount(10)
        # 读取系统人员名单(采用本地保存格式)
        # 账号:英文字母、数字组合或中文(最大长度10)
        # 密码:英文字母、数字组合(最大长度6)
        self.count_dict = {}
        with open(r"UI\form\namelist.json","r", encoding='utf-8'  ) as f:
            self.count_dict = json.load(f)
        # 深度学习模型推理类
        self.videoprocess = videoprocess(self.print_debug_signal,self.show_image_signal)

        # 背景图平铺

    def paintEvent(self, event):
        painter = QPainter(self)
        pixmap = QPixmap("background/background5.jpg")
        painter.drawPixmap(self.rect(), pixmap)

    # 背景图平铺结束

    # 登录槽函数
    def login_function(self):
        # 获取界面账户、密码
        count = self.main_ui.lineEdit_count.text()
        secret = self.main_ui.lineEdit_secret.text()
        # 判断用户信息是否存在
        if count in self.count_dict.keys():
            # 判断密码是否正确
            if self.count_dict[count] == secret:
                # 弹出登录成功窗口
                QMessageBox.information(self,"Tip","登录成功!")
                # 切换页面
                self.main_ui.stackedWidget.setCurrentIndex(2)
            else:
                # 弹出密码错误窗口
                QMessageBox.critical(self,"error","密码错误!")
        else:
            # 弹出账户未注册窗口
            QMessageBox.critical(self,"error","账户未注册!")

    def registe_interface_change(self):
        # 切换页面
        self.main_ui.stackedWidget.setCurrentIndex(1)
        print("registe_interface_change")

    # 注册槽函数
    def registe_function(self):

        # 获取界面账户、密码
        count = self.main_ui.lineEdit_count_2.text()
        secret = self.main_ui.lineEdit_secret_2.text()

        # 判断账户、密码输入是否为空字符串
        if len(count) == 0 or len(secret) == 0:
            # 提示用户重新输入
            QMessageBox.warning(self,"warning","信息无效!")
        else:
            # 忽视用户信息是否存在,重新设置用户信息
            self.count_dict[count] = secret
            # 保存至本地,(如需删除账户信息可直接在UI\form\namelist.json中删除即可)
            with open(r"UI\form\namelist.json","w", encoding='utf-8') as f: ## 设置'utf-8'编码
                f.write(json.dumps(self.count_dict, ensure_ascii=False ,indent=4))  
            QMessageBox.information(self,"Tip","注册成功!")

    def backLoginWindow(self):
        # 切换页面
        self.main_ui.stackedWidget.setCurrentIndex(0)

    # 视频源改变槽函数
    def videosoure_change(self):
        #当视频源发生切换如果视频正在推理需要停止
        if not self.videoprocess.stopped:
            self.videoprocess.stopped = True
            self.clear_label()
        if self.main_ui.comboBox_source.currentIndex() == 0:
            self.main_ui.lineEdit_filepath.setVisible(True)
            self.main_ui.pushButton_open.setVisible(True)

        elif self.main_ui.comboBox_source.currentIndex() == 1:
            self.main_ui.lineEdit_filepath.setVisible(True)
            self.main_ui.pushButton_open.setVisible(True)
    # 打开视频与图片槽函数
    def open_video_function(self):
        # 打开本地路径
        if self.main_ui.comboBox_source.currentIndex() == 0:
            fileName, filetype = QFileDialog.getOpenFileName(self,"选取视频","./videos", "'Video Files (*.mp4 *.avi *.mkv)'") 
        elif self.main_ui.comboBox_source.currentIndex() == 1:
            fileName, filetype = QFileDialog.getOpenFileName(self, '选择图片',"./images", 'Image Files (*.png *.jpg *.jpeg *.gif)')
        # 显示本地路径
        self.main_ui.lineEdit_filepath.setText(fileName)
        # 打印信息
        self.print_debug_signal.emit("{}打开成功,请点击开始按钮!!!".format(fileName))

    # 开始测试
    def predicte_function(self):
        if not self.videoprocess.stopped:
            self.print_debug_signal.emit("已经开启,请关闭后再次开启!!")  
            return
        self.main_ui.pushButton_pause.setVisible(True)
        self.main_ui.pushButton_pause.setText("暂停测试")
        # 是否保存视频
        if self.main_ui.checkBox_save.isChecked():
            self.videoprocess.save_out = True
        else:
            self.videoprocess.save_out = False
        self.videoprocess.filename = self.main_ui.lineEdit_filepath.text()
        # 启动深度学习推理线程
        self.videoprocess.start()
        self.main_ui.pushButton_alarm.setStyleSheet("background-color:rgb(0,255,0);border-radius: 10px; border: 2px groove black;border-style: outset;")
            
    # 暂停测试
    def setpause_function(self):
        if self.videoprocess.is_pause:
            # 当前状态已经暂定
            self.videoprocess.is_pause = False
            self.main_ui.pushButton_pause.setText("暂停测试")
        else:
            self.videoprocess.is_pause = True
            self.main_ui.pushButton_pause.setText("继续测试")
    # 停止测试
    def stop_function(self):
        # 停止深度学习推理线程
        if self.videoprocess.stopped:
            self.print_debug_signal.emit("已经关闭!!")
        else:  
            self.videoprocess.stopped = True
            self.clear_label()
    # 清空标签,恢复初始状态
    def clear_label(self):
        self.main_ui.label_image_source.clear()
        self.main_ui.label_image_source.setText("原视频")
        self.main_ui.label_image_lane.clear()
        self.main_ui.label_image_lane.setText("车道线")
        self.main_ui.label_image_driving.clear()
        self.main_ui.label_image_driving.setText("可行驶区域")
        self.main_ui.label_image_result.clear()
        self.main_ui.label_image_result.setText("识别结果")
        self.main_ui.pushButton_alarm.setStyleSheet("background-color:rgb(150, 150, 150);border-radius: 10px; border: 2px groove black;border-style: outset;")
        self.main_ui.label_fps.setText("FPS: 00.00")
        self.main_ui.pushButton_pause.setVisible(False) # 停止运行后暂停按钮失效

    # 显示界面函数

    def showimgae(self,fps):
        if not self.videoprocess.stopped:
            # 转换格式
            img_source = cv2.cvtColor(self.videoprocess.img_source, cv2.COLOR_BGR2RGB)
            color_lane = cv2.cvtColor(self.videoprocess.color_lane, cv2.COLOR_BGR2RGB)
            color_driving = cv2.cvtColor(self.videoprocess.color_driving, cv2.COLOR_BGR2RGB)
            img_rs = cv2.cvtColor(self.videoprocess.img_rs, cv2.COLOR_BGR2RGB)

            # 显示原图 
            self.main_ui.label_image_source.setPixmap(QPixmap(QImage(img_source.data,img_source.shape[1],img_source.shape[0],QImage.Format_RGB888)))
            self.main_ui.label_image_source.setScaledContents(True)

            # 显示车道线
            self.main_ui.label_image_lane.setPixmap(QPixmap(QImage(color_lane.data,color_lane.shape[1],color_lane.shape[0],QImage.Format_RGB888)))
            self.main_ui.label_image_lane.setScaledContents(True)

           
运行结果

Python路面车道线识别偏离预警-LMLPHP

三、在线协助:

如需安装运行环境或远程调试,见文章底部个人 QQ 名片,由专业技术人员远程协助!

当前文章连接:https://blog.csdn.net/alicema1111/article/details/132666851
个人博客主页https://blog.csdn.net/alicema1111?type=blog
博主所有文章点这里:https://blog.csdn.net/alicema1111?type=blog

博主推荐:
Python人脸识别考勤打卡系统:
https://blog.csdn.net/alicema1111/article/details/133434445
Python果树水果识别https://blog.csdn.net/alicema1111/article/details/130862842
Python+Yolov8+Deepsort入口人流量统计:https://blog.csdn.net/alicema1111/article/details/130454430
Python+Qt人脸识别门禁管理系统:https://blog.csdn.net/alicema1111/article/details/130353433
Python+Qt指纹录入识别考勤系统:https://blog.csdn.net/alicema1111/article/details/129338432
Python Yolov5火焰烟雾识别源码分享:https://blog.csdn.net/alicema1111/article/details/128420453
Python+Yolov8路面桥梁墙体裂缝识别:https://blog.csdn.net/alicema1111/article/details/133434445
Python+Yolov5道路障碍物识别:https://blog.csdn.net/alicema1111/article/details/129589741
Python+Yolov5跌倒检测 摔倒检测 人物目标行为 人体特征识别:https://blog.csdn.net/alicema1111/article/details/129272048

04-19 11:58