当前位置:   article > 正文

实战YOLOv5-入门篇笔记 YOLOv5 Pyside6可视化界面_pyside6部署yolo

pyside6部署yolo

pip install pyside6

 

 运行designer.exe

 

保存为main_window.ui文件

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

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

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

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

  1. import sys
  2. import PySide6
  3. from PyQt5.QtWidgets import QMainWindow,QApplication
  4. # from PySide6.QtWidgets import QMainWindow,QApplication
  5. from Ui_main_window import Ui_MainWindow
  6. import os
  7. dirname = os.path.dirname(PySide6.__file__)
  8. plugin_path = os.path.join(dirname, 'plugins', 'platforms')
  9. os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path
  10. # def covert2QImage(img):
  11. #     height,width,channel = img.shape
  12. #     return QImage(img,width,height,width * channel,QImage.Formate_RGB888)
  13. class MainWindow(QMainWindow,Ui_MainWindow):
  14.     def __init__(self):
  15.         super(MainWindow,self).__init__()
  16.         self.setupUi(self)
  17.         self.bind_slots()
  18.    
  19.     def open_image(self):
  20.         print("点击了检测图片")
  21.    
  22.     def open_video(self):
  23.         pass
  24.    
  25.     def bind_slots(self):
  26.         self.det_image.clicked.connect(self.open_image)
  27.         self.det_video.clicked.connect(self.open_video)
  28. if __name__ == "__main__":
  29.     app = QApplication(sys.argv)
  30.     # app = QApplication([])
  31.     window = MainWindow()
  32.     window.show()
  33.     app.exec()

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

  1. import sys
  2. import PySide6
  3. import torch
  4. # from PySide6.QtWidgets import QMainWindow,QApplication,QFileDialog
  5. # from PySide6.QtGui import QPixmap
  6. from PyQt5.QtWidgets import QMainWindow,QApplication,QFileDialog
  7. from PyQt5.QtGui import QPixmap,QImage
  8. from Ui_main_window import Ui_MainWindow
  9. import os
  10. dirname = os.path.dirname(PySide6.__file__)
  11. plugin_path = os.path.join(dirname, 'plugins', 'platforms')
  12. os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path
  13. def covert2QImage(img):
  14.     height,width,channel = img.shape
  15.     return QImage(img,width,height,width * channel,QImage.Format_RGB888)
  16. class MainWindow(QMainWindow,Ui_MainWindow):
  17.     def __init__(self):
  18.         super(MainWindow,self).__init__()
  19.         self.setupUi(self)
  20.         self.model = torch.hub.load("./","custom",path="runs/train/exp12/weights/best.pt",source="local")
  21.         self.bind_slots()
  22.     def image_pred(self,file_path):
  23.         results = self.model(file_path)
  24.         image = results.render()[0]
  25.         return covert2QImage(image)
  26.     def open_image(self):
  27.         print("点击了检测图片")
  28.         # file_path = QFileDialog.getOpenFileName(self,dir="./datasets/images/train",filter="*.jpg;*.png;*.jpeg") # PySide6.QtWidgets
  29.         file_path = QFileDialog.getOpenFileName(self,directory="./datasets/images/train",filter="*.jpg;*.png;*.jpeg") # PyQt5.QtWidgets
  30.         if file_path[0]:
  31.             file_path = file_path[0]
  32.             qimage = self.image_pred(file_path)
  33.             self.input.setPixmap(QPixmap(file_path))
  34.             self.output.setPixmap(QPixmap.fromImage(qimage))
  35.    
  36.     def open_video(self):
  37.         pass
  38.    
  39.     def bind_slots(self):
  40.         self.det_image.clicked.connect(self.open_image)
  41.         self.det_video.clicked.connect(self.open_video)
  42. if __name__ == "__main__":
  43.     app = QApplication(sys.argv)
  44.     # app = QApplication([])
  45.     window = MainWindow()
  46.     window.show()
  47.     app.exec()

》》效果图:

 (三)视频检测代码编写

方案一:

  1. import sys
  2. import PySide6
  3. import torch
  4. import cv2
  5. # from PySide6.QtWidgets import QMainWindow,QApplication,QFileDialog
  6. # from PySide6.QtGui import QPixmap
  7. from PyQt5.QtWidgets import QMainWindow,QApplication,QFileDialog
  8. from PyQt5.QtGui import QPixmap,QImage
  9. from Ui_main_window import Ui_MainWindow
  10. import os
  11. dirname = os.path.dirname(PySide6.__file__)
  12. plugin_path = os.path.join(dirname, 'plugins', 'platforms')
  13. os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path
  14. def covert2QImage(img):
  15.     height,width,channel = img.shape
  16.     return QImage(img,width,height,width * channel,QImage.Format_RGB888)
  17. class MainWindow(QMainWindow,Ui_MainWindow):
  18.     def __init__(self):
  19.         super(MainWindow,self).__init__()
  20.         self.setupUi(self)
  21.         self.model = torch.hub.load("./","custom",path="runs/train/exp12/weights/best.pt",source="local")
  22.         self.bind_slots()
  23.     def image_pred(self,file_path):
  24.         results = self.model(file_path)
  25.         image = results.render()[0]
  26.         return covert2QImage(image)
  27.     def open_image(self):
  28.         print("点击了检测图片")
  29.         # file_path = QFileDialog.getOpenFileName(self,dir="./datasets/images/train",filter="*.jpg;*.png;*.jpeg") # PySide6.QtWidgets
  30.         file_path = QFileDialog.getOpenFileName(self,directory="./datasets/images/train",filter="*.jpg;*.png;*.jpeg") # PyQt5.QtWidgets
  31.         if file_path[0]:
  32.             file_path = file_path[0]
  33.             qimage = self.image_pred(file_path)
  34.             self.input.setPixmap(QPixmap(file_path))
  35.             self.output.setPixmap(QPixmap.fromImage(qimage))
  36.     def image_pred(self,img):
  37.         results = self.model(img)
  38.         image = results.render()[0]
  39.         return covert2QImage(image)
  40.    
  41.     def open_video(self):
  42.         print("点击了检测视频")
  43.         file_path = QFileDialog.getOpenFileName(self,directory="./datasets",filter="*.mp4") # PyQt5.QtWidgets
  44.         if file_path[0]:
  45.             file_path = file_path[0]
  46.             video = cv2.VideoCapture(file_path)
  47.             while True: # 缺点,引起事件阻塞
  48.                 ret,frame = video.read()
  49.                 if not ret:
  50.                     break
  51.                 frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
  52.                 qimage = self.image_pred(frame)
  53.                 self.input.setPixmap(QPixmap(covert2QImage(frame)))
  54.                 self.output.setPixmap(QPixmap.fromImage(qimage))
  55.     def bind_slots(self):
  56.         self.det_image.clicked.connect(self.open_image)
  57.         self.det_video.clicked.connect(self.open_video)
  58. if __name__ == "__main__":
  59.     app = QApplication(sys.argv)
  60.     # app = QApplication([])
  61.     window = MainWindow()
  62.     window.show()
  63.     app.exec()

存在缺陷: 引起事件阻塞

方案二:设置timer

  1. import sys
  2. import PySide6
  3. import torch
  4. import cv2
  5. # from PySide6.QtWidgets import QMainWindow,QApplication,QFileDialog
  6. # from PySide6.QtGui import QPixmap
  7. from PyQt5.QtWidgets import QMainWindow,QApplication,QFileDialog
  8. from PyQt5.QtGui import QPixmap,QImage
  9. from PyQt5.QtCore import QTimer
  10. from Ui_main_window import Ui_MainWindow
  11. import os
  12. dirname = os.path.dirname(PySide6.__file__)
  13. plugin_path = os.path.join(dirname, 'plugins', 'platforms')
  14. os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path
  15. def covert2QImage(img):
  16.     height,width,channel = img.shape
  17.     return QImage(img,width,height,width * channel,QImage.Format_RGB888)
  18. class MainWindow(QMainWindow,Ui_MainWindow):
  19.     def __init__(self):
  20.         super(MainWindow,self).__init__()
  21.         self.setupUi(self)
  22.         self.model = torch.hub.load("./","custom",path="runs/train/exp12/weights/best.pt",source="local")
  23.        
  24.         #创建timer
  25.         self.timer = QTimer()
  26.         self.timer.setInterval(0.05)
  27.         self.video = None
  28.         self.bind_slots()
  29.     def image_pred(self,file_path):
  30.         results = self.model(file_path)
  31.         image = results.render()[0]
  32.         return covert2QImage(image)
  33.     def open_image(self):
  34.         print("点击了检测图片")
  35.         # file_path = QFileDialog.getOpenFileName(self,dir="./datasets/images/train",filter="*.jpg;*.png;*.jpeg") # PySide6.QtWidgets
  36.         file_path = QFileDialog.getOpenFileName(self,directory="./datasets/images/train",filter="*.jpg;*.png;*.jpeg") # PyQt5.QtWidgets
  37.         if file_path[0]:
  38.             file_path = file_path[0]
  39.             qimage = self.image_pred(file_path)
  40.             self.input.setPixmap(QPixmap(file_path))
  41.             self.output.setPixmap(QPixmap.fromImage(qimage))
  42.     def video_pred(self):
  43.         ret,frame = self.video.read()
  44.         if not ret:
  45.             self.timer.stop()
  46.         else:
  47.             frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
  48.             self.input.setPixmap(QPixmap.fromImage(covert2QImage(frame)))
  49.             results = self.model(frame)
  50.             image = results.render()[0]
  51.             self.output.setPixmap(QPixmap.fromImage(covert2QImage(image)))
  52.    
  53.     def open_video(self):
  54.         print("点击了检测视频")
  55.         file_path = QFileDialog.getOpenFileName(self,directory="./datasets",filter="*.mp4") # PyQt5.QtWidgets
  56.         if file_path[0]:
  57.             file_path = file_path[0]
  58.             self.video = cv2.VideoCapture(file_path)
  59.             self.timer.start()
  60.     def bind_slots(self):
  61.         self.det_image.clicked.connect(self.open_image)
  62.         self.det_video.clicked.connect(self.open_video)
  63.         # 绑定timer
  64.         self.timer.timeout.connect(self.video_pred)
  65. if __name__ == "__main__":
  66.     app = QApplication(sys.argv)
  67.     # app = QApplication([])
  68.     window = MainWindow()
  69.     window.show()
  70.     app.exec()

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

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

演示视频

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/495154
推荐阅读
相关标签
  

闽ICP备14008679号