实战YOLOv5-入门篇笔记 YOLOv5 Pyside6可视化界面

pip install pyside6

 

 运行designer.exe

 

保存为main_window.ui文件

Vscode将.ui文件转成.py文件方法:Vscode将.ui文件转成.py文件方法_vscode 将 .ui 转为 .py 输出_流动的星的博客-CSDN博客

编译成.py文件,右键编译:

可能会出现关于utf8编码问题,没有出现可忽略:

 (一)实现检测图片和检测视频的基本代码框架:

import sys
import PySide6
from PyQt5.QtWidgets import QMainWindow,QApplication
# from PySide6.QtWidgets import QMainWindow,QApplication
from Ui_main_window import Ui_MainWindow
import os
dirname = os.path.dirname(PySide6.__file__)
plugin_path = os.path.join(dirname, 'plugins', 'platforms')
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path
# def covert2QImage(img):
#     height,width,channel = img.shape
#     return QImage(img,width,height,width * channel,QImage.Formate_RGB888)
class MainWindow(QMainWindow,Ui_MainWindow):
    def __init__(self):
        super(MainWindow,self).__init__()
        self.setupUi(self)
        self.bind_slots()
    
    def open_image(self):
        print("点击了检测图片")
    
    def open_video(self):
        pass
    
    def bind_slots(self):
        self.det_image.clicked.connect(self.open_image)
        self.det_video.clicked.connect(self.open_video)
if __name__ == "__main__":
    app = QApplication(sys.argv)
    # app = QApplication([])
    window = MainWindow()
    window.show()
    app.exec()

 (二)图片检测代码编写:

import sys
import PySide6
import torch
# from PySide6.QtWidgets import QMainWindow,QApplication,QFileDialog
# from PySide6.QtGui import QPixmap
from PyQt5.QtWidgets import QMainWindow,QApplication,QFileDialog
from PyQt5.QtGui import QPixmap,QImage
from Ui_main_window import Ui_MainWindow
import os
dirname = os.path.dirname(PySide6.__file__)
plugin_path = os.path.join(dirname, 'plugins', 'platforms')
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path
def covert2QImage(img):
    height,width,channel = img.shape
    return QImage(img,width,height,width * channel,QImage.Format_RGB888)
class MainWindow(QMainWindow,Ui_MainWindow):
    def __init__(self):
        super(MainWindow,self).__init__()
        self.setupUi(self)
        self.model = torch.hub.load("./","custom",path="runs/train/exp12/weights/best.pt",source="local")
        self.bind_slots()
    def image_pred(self,file_path):
        results = self.model(file_path)
        image = results.render()[0]
        return covert2QImage(image)
    def open_image(self):
        print("点击了检测图片")
        # file_path = QFileDialog.getOpenFileName(self,dir="./datasets/images/train",filter="*.jpg;*.png;*.jpeg") # PySide6.QtWidgets
        file_path = QFileDialog.getOpenFileName(self,directory="./datasets/images/train",filter="*.jpg;*.png;*.jpeg") # PyQt5.QtWidgets
        if file_path[0]:
            file_path = file_path[0]
            qimage = self.image_pred(file_path)
            self.input.setPixmap(QPixmap(file_path))
            self.output.setPixmap(QPixmap.fromImage(qimage))
    
    def open_video(self):
        pass
    
    def bind_slots(self):
        self.det_image.clicked.connect(self.open_image)
        self.det_video.clicked.connect(self.open_video)
if __name__ == "__main__":
    app = QApplication(sys.argv)
    # app = QApplication([])
    window = MainWindow()
    window.show()
    app.exec()

》》效果图:

 (三)视频检测代码编写

方案一:

import sys
import PySide6
import torch
import cv2
# from PySide6.QtWidgets import QMainWindow,QApplication,QFileDialog
# from PySide6.QtGui import QPixmap
from PyQt5.QtWidgets import QMainWindow,QApplication,QFileDialog
from PyQt5.QtGui import QPixmap,QImage
from Ui_main_window import Ui_MainWindow
import os
dirname = os.path.dirname(PySide6.__file__)
plugin_path = os.path.join(dirname, 'plugins', 'platforms')
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path
def covert2QImage(img):
    height,width,channel = img.shape
    return QImage(img,width,height,width * channel,QImage.Format_RGB888)
class MainWindow(QMainWindow,Ui_MainWindow):
    def __init__(self):
        super(MainWindow,self).__init__()
        self.setupUi(self)
        self.model = torch.hub.load("./","custom",path="runs/train/exp12/weights/best.pt",source="local")
        self.bind_slots()
    def image_pred(self,file_path):
        results = self.model(file_path)
        image = results.render()[0]
        return covert2QImage(image)
    def open_image(self):
        print("点击了检测图片")
        # file_path = QFileDialog.getOpenFileName(self,dir="./datasets/images/train",filter="*.jpg;*.png;*.jpeg") # PySide6.QtWidgets
        file_path = QFileDialog.getOpenFileName(self,directory="./datasets/images/train",filter="*.jpg;*.png;*.jpeg") # PyQt5.QtWidgets
        if file_path[0]:
            file_path = file_path[0]
            qimage = self.image_pred(file_path)
            self.input.setPixmap(QPixmap(file_path))
            self.output.setPixmap(QPixmap.fromImage(qimage))
    def image_pred(self,img):
        results = self.model(img)
        image = results.render()[0]
        return covert2QImage(image)
    
    def open_video(self):
        print("点击了检测视频")
        file_path = QFileDialog.getOpenFileName(self,directory="./datasets",filter="*.mp4") # PyQt5.QtWidgets
        if file_path[0]:
            file_path = file_path[0]
            video = cv2.VideoCapture(file_path)
            while True: # 缺点,引起事件阻塞
                ret,frame = video.read()
                if not ret:
                    break
                frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
                qimage = self.image_pred(frame)
                self.input.setPixmap(QPixmap(covert2QImage(frame)))
                self.output.setPixmap(QPixmap.fromImage(qimage))
    def bind_slots(self):
        self.det_image.clicked.connect(self.open_image)
        self.det_video.clicked.connect(self.open_video)
if __name__ == "__main__":
    app = QApplication(sys.argv)
    # app = QApplication([])
    window = MainWindow()
    window.show()
    app.exec()

存在缺陷: 引起事件阻塞

方案二:设置timer

import sys
import PySide6
import torch
import cv2
# from PySide6.QtWidgets import QMainWindow,QApplication,QFileDialog
# from PySide6.QtGui import QPixmap
from PyQt5.QtWidgets import QMainWindow,QApplication,QFileDialog
from PyQt5.QtGui import QPixmap,QImage
from PyQt5.QtCore import QTimer
from Ui_main_window import Ui_MainWindow
import os
dirname = os.path.dirname(PySide6.__file__)
plugin_path = os.path.join(dirname, 'plugins', 'platforms')
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path
def covert2QImage(img):
    height,width,channel = img.shape
    return QImage(img,width,height,width * channel,QImage.Format_RGB888)
class MainWindow(QMainWindow,Ui_MainWindow):
    def __init__(self):
        super(MainWindow,self).__init__()
        self.setupUi(self)
        self.model = torch.hub.load("./","custom",path="runs/train/exp12/weights/best.pt",source="local")
        
        #创建timer
        self.timer = QTimer()
        self.timer.setInterval(0.05)
        self.video = None
        self.bind_slots()
    def image_pred(self,file_path):
        results = self.model(file_path)
        image = results.render()[0]
        return covert2QImage(image)
    def open_image(self):
        print("点击了检测图片")
        # file_path = QFileDialog.getOpenFileName(self,dir="./datasets/images/train",filter="*.jpg;*.png;*.jpeg") # PySide6.QtWidgets
        file_path = QFileDialog.getOpenFileName(self,directory="./datasets/images/train",filter="*.jpg;*.png;*.jpeg") # PyQt5.QtWidgets
        if file_path[0]:
            file_path = file_path[0]
            qimage = self.image_pred(file_path)
            self.input.setPixmap(QPixmap(file_path))
            self.output.setPixmap(QPixmap.fromImage(qimage))
    def video_pred(self):
        ret,frame = self.video.read()
        if not ret:
            self.timer.stop()
        else:
            frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
            self.input.setPixmap(QPixmap.fromImage(covert2QImage(frame)))
            results = self.model(frame)
            image = results.render()[0]
            self.output.setPixmap(QPixmap.fromImage(covert2QImage(image)))
    
    def open_video(self):
        print("点击了检测视频")
        file_path = QFileDialog.getOpenFileName(self,directory="./datasets",filter="*.mp4") # PyQt5.QtWidgets
        if file_path[0]:
            file_path = file_path[0]
            self.video = cv2.VideoCapture(file_path)
            self.timer.start()
    def bind_slots(self):
        self.det_image.clicked.connect(self.open_image)
        self.det_video.clicked.connect(self.open_video)
        # 绑定timer
        self.timer.timeout.connect(self.video_pred)
if __name__ == "__main__":
    app = QApplication(sys.argv)
    # app = QApplication([])
    window = MainWindow()
    window.show()
    app.exec()

解决一些小Bug:关闭定时器

 (四)最终实现效果演示视频:

演示视频