在本文中,我将介绍如何使用PyQt5创建一个YOLOv8V7/V5目标检测的可视化界面,可以根据需求选择YOLOv8V7/V5的权重。
该可视化界面的功能丰富,包含内容:
1.GUI+目标计数+视频目标检测与跟踪
2.完整的OLO数据格式制作流程以及代码
3.PCB缺陷数据集和训练好的权重
4.包含完整的GUI界面代码和使用说明,适合做毕业设计。
代码地址:下载地址
本文将涵盖界面的设计、功能实现以及一些核心代码的解释。以下是代码和详细介绍。
环境准备
首先,需要确保你的YOLOv8环境中已经安装了PyQt5和其他依赖库。你可以使用以下命令来安装:
pip install pyqt5 pyqt5-tools
pip install opencv-python-headless
pip install pyyaml
代码实现
界面设计
界面的设计通过Qt Designer完成,并生成gui.py
文件。你可以使用以下命令将.ui
文件转换为Python代码:
核心代码
以下是实现YOLOv8目标检测的核心代码:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.6
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
# 设置对话框的对象名和大小
Dialog.setObjectName("Dialog")
Dialog.resize(1584, 864)
Dialog.setStyleSheet("QDialog { background-color: #ccffcc; }")# 背景颜色设置
# 设置原始图像显示标签对象
self.label_ori = QtWidgets.QLabel(Dialog)
# 20: 控件左上角的 x 坐标,即距离左边界的像素数。140: 控件左上角的 y 坐标,即距离上边界的像素数。500: 控件的宽度。500: 控件的高度。
self.label_ori.setGeometry(QtCore.QRect(20, 120, 500, 500))
self.label_ori.setText("")
self.label_ori.setObjectName("label_ori")
self.label_ori.setStyleSheet("background-color: #ffffff;border: 1px solid black;")
# 设置文本说明标签对象
self.ori_label_instruction = QtWidgets.QLabel(Dialog)
self.ori_label_instruction.setGeometry(QtCore.QRect(20, 80, 100, 40)) # 调整位置和大小
font = QtGui.QFont()
font.setPointSize(15)
self.ori_label_instruction.setFont(font)
self.ori_label_instruction.setObjectName("text_label_instruction")
self.ori_label_instruction.setText("原始图像")
# 设置检测结果标签对象
self.label_det = QtWidgets.QLabel(Dialog)
self.label_det.setGeometry(QtCore.QRect(650, 120, 500, 500))
self.label_det.setText("")
self.label_det.setObjectName("label_det")
self.label_det.setStyleSheet("background-color: #ffffff;border: 1px solid black;")
# 设置文本说明标签对象
self.det_label_instruction = QtWidgets.QLabel(Dialog)
self.det_label_instruction.setGeometry(QtCore.QRect(650, 80, 100, 40)) # 调整位置和大小
font = QtGui.QFont()
font.setPointSize(15)
self.det_label_instruction.setFont(font)
self.det_label_instruction.setObjectName("text_label_instruction")
self.det_label_instruction.setText("检测图像")
# 设置模型按钮对象
self.pushButton_Model = QtWidgets.QPushButton(Dialog)
self.pushButton_Model.setGeometry(QtCore.QRect(1200, 150, 130, 50))
font = QtGui.QFont()
font.setPointSize(15)
self.pushButton_Model.setFont(font)
self.pushButton_Model.setObjectName("pushButton_Model")
self.pushButton_Model.setStyleSheet("background-color: #00FFFF;")
# 设置退出按钮对象
self.pushButton_Exit = QtWidgets.QPushButton(Dialog)
self.pushButton_Exit.setGeometry(QtCore.QRect(1400, 430, 130, 50))
font = QtGui.QFont()
font.setPointSize(15)
self.pushButton_Exit.setFont(font)
self.pushButton_Exit.setObjectName("pushButton_Exit")
self.pushButton_Exit.setStyleSheet("background-color: #00FFFF;")
# 设置打开图片按钮对象
self.pushButton_Img = QtWidgets.QPushButton(Dialog)
self.pushButton_Img.setGeometry(QtCore.QRect(1200, 220, 130, 50))
font = QtGui.QFont()
font.setPointSize(15)
self.pushButton_Img.setFont(font)
self.pushButton_Img.setObjectName("pushButton_Img")
self.pushButton_Img.setStyleSheet("background-color: #00FFFF;")
# 设置打开文件夹按钮对象
self.pushButton_ImgFolder = QtWidgets.QPushButton(Dialog)
self.pushButton_ImgFolder.setGeometry(QtCore.QRect(1200, 290, 130, 50))
font = QtGui.QFont()
font.setPointSize(15)
self.pushButton_ImgFolder.setFont(font)
self.pushButton_ImgFolder.setObjectName("pushButton_ImgFolder")
self.pushButton_ImgFolder.setStyleSheet("background-color: #00FFFF;")
# 设置打开视频按钮对象
self.pushButton_Video = QtWidgets.QPushButton(Dialog)
self.pushButton_Video.setGeometry(QtCore.QRect(1200, 360, 130, 50))
font = QtGui.QFont()
font.setPointSize(15)
self.pushButton_Video.setFont(font)
self.pushButton_Video.setObjectName("pushButton_Video")
self.pushButton_Video.setStyleSheet("background-color: #00FFFF;")
# 设置打开摄像头按钮对象
self.pushButton_Camera = QtWidgets.QPushButton(Dialog)
self.pushButton_Camera.setGeometry(QtCore.QRect(1200, 430, 130, 50))
font = QtGui.QFont()
font.setPointSize(15)
self.pushButton_Camera.setFont(font)
self.pushButton_Camera.setObjectName("pushButton_Camera")
self.pushButton_Camera.setStyleSheet("background-color: #00FFFF;")
# 设置标题标签对象
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(100, 5, 1341, 70))
font = QtGui.QFont()
font.setPointSize(25) # 增加字体大小以突出标题
font.setBold(True)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
# 设置文本浏览器对象
self.textBrowser = QtWidgets.QTextBrowser(Dialog)
self.textBrowser.setGeometry(QtCore.QRect(20, 670, 1400, 180))
self.textBrowser.setObjectName("textBrowser")
self.textBrowser.setStyleSheet("QTextBrowser { border: 1px solid black; }")
# 设置文本说明标签对象
self.text_label_instruction = QtWidgets.QLabel(Dialog)
self.text_label_instruction.setGeometry(QtCore.QRect(20, 630, 100, 40)) # 调整位置和大小
font = QtGui.QFont()
font.setPointSize(15)
self.text_label_instruction.setFont(font)
self.text_label_instruction.setObjectName("text_label_instruction")
self.text_label_instruction.setText("结果显示:")
# 设置开始检测按钮对象
self.pushButton_BegDet = QtWidgets.QPushButton(Dialog)
self.pushButton_BegDet.setGeometry(QtCore.QRect(1400, 150, 130, 50))
font = QtGui.QFont()
font.setPointSize(15)
self.pushButton_BegDet.setFont(font)
self.pushButton_BegDet.setObjectName("pushButton_BegDet")
self.pushButton_BegDet.setStyleSheet("background-color: #00FFFF;")
# 设置停止检测按钮对象
self.pushButton_StopDet = QtWidgets.QPushButton(Dialog)
self.pushButton_StopDet.setGeometry(QtCore.QRect(1400, 220, 130, 50))
font = QtGui.QFont()
font.setPointSize(15)
self.pushButton_StopDet.setFont(font)
self.pushButton_StopDet.setObjectName("pushButton_StopDet")
self.pushButton_StopDet.setStyleSheet("background-color: #00FFFF;")
# 设置设置保存路径按钮对象
self.pushButton_SavePath = QtWidgets.QPushButton(Dialog)
self.pushButton_SavePath.setGeometry(QtCore.QRect(1400, 290, 130, 50))
font = QtGui.QFont()
font.setPointSize(15)
self.pushButton_SavePath.setFont(font)
self.pushButton_SavePath.setObjectName("pushButton_SavePath")
self.pushButton_SavePath.setStyleSheet("background-color: #00FFFF;")
# 设置下拉框对象
self.comboBox = QtWidgets.QComboBox(Dialog)
self.comboBox.setGeometry(QtCore.QRect(1400, 360, 130, 50))
font = QtGui.QFont()
font.setPointSize(15)
self.comboBox.setFont(font)
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.setStyleSheet("background-color: #00FFFF;")
# 调用翻译函数
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
# # 创建操作区框架
# self.operation_frame = QtWidgets.QFrame(Dialog)
# self.operation_frame.setGeometry(QtCore.QRect(1180, 110, 380, 450))
# self.operation_frame.setObjectName("operation_frame")
# self.operation_frame.setStyleSheet("background-color: transparent; border: 1px solid black;")
# # 在操作区框架中放置“操作”标签
self.operation_label = QtWidgets.QLabel(Dialog)
self.operation_label.setGeometry(QtCore.QRect(1185, 100, 60, 30))
font = QtGui.QFont()
font.setPointSize(15)
self.operation_label.setFont(font)
self.operation_label.setObjectName("operation_label")
self.operation_label.setText("操作")
self.operation_label.setStyleSheet("background-color:#ccffcc;")
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "YOLOV8/7/5-PYQT"))
self.pushButton_Model.setText(_translate("Dialog", "选择配置文件"))
self.pushButton_Exit.setText(_translate("Dialog", "退出"))
self.pushButton_Img.setText(_translate("Dialog", "打开图片"))
self.pushButton_ImgFolder.setText(_translate("Dialog", "打开文件夹"))
self.pushButton_Video.setText(_translate("Dialog", "打开视频"))
self.pushButton_Camera.setText(_translate("Dialog", "打开摄像头"))
self.label.setText(_translate("Dialog", "深度学习目标检测")) # 修改标题文字,在run_gui.py中修改
self.pushButton_BegDet.setText(_translate("Dialog", "开始检测"))
self.pushButton_StopDet.setText(_translate("Dialog", "停止检测"))
self.pushButton_SavePath.setText(_translate("Dialog", "设置保存路径"))
self.comboBox.setItemText(0, _translate("Dialog", "无追踪")) # 修改下拉框选项
self.comboBox.setItemText(1, _translate("Dialog", "ByteTrack"))
self.text_label_instruction.setText(_translate("Dialog", "结果显示:"))
self.ori_label_instruction.setText(_translate("Dialog", "原始图像"))
self.det_label_instruction.setText(_translate("Dialog", "检测图像"))
功能展示:
首先要点击选择配置文件,然后有三种模式可以使用,分别是打开图片,打开文件夹和打开视频,然后点击开始检测按钮就可以检测,结果会显示在文本框中,对每个目标进行计数。
检测视频可以使用目标跟踪模式。
视频目标追踪检测效果: