当前位置:   article > 正文

yolov8可视化界面,基于pyqt5,兼容官方源码,可打包成软件_基于yolov8的可视化界面

基于yolov8的可视化界面

1.确保Ultralytics源码无误且环境正常配置

环境异常请先搜索环境配置,这里不展开讲

建议使用Anaconda3+pycharm

2.界面实现

2.1 相关库

pip install PyQt5

2.2 导入包

我这里导入的是以下这些,同官方源码兼容

  1. import sys
  2. from PyQt5.QtCore import Qt
  3. from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QWidget, QPushButton, QHBoxLayout, QMessageBox, QFileDialog
  4. from PyQt5.QtGui import QImage, QPixmap
  5. import cv2
  6. from ultralytics import YOLO

2.3 功能实现

这里只实现了简单的权重选择和图片检测功能并添加了对应的按钮

  1. # 添加模型选择按钮
  2. self.load_model_button = QPushButton("模型选择")
  3. self.load_model_button.clicked.connect(self.load_model)
  4. hbox_buttons.addWidget(self.load_model_button)
  5. # 添加图片检测按钮
  6. self.image_detect_button = QPushButton("图片检测")
  7. self.image_detect_button.clicked.connect(self.detect_image)
  8. hbox_buttons.addWidget(self.image_detect_button)
  9. # 添加退出按钮
  10. self.exit_button = QPushButton("退出")
  11. self.exit_button.clicked.connect(self.exit_application)
  12. hbox_buttons.addWidget(self.exit_button)
  13. #加载模型文件
  14. def load_model(self):
  15. model_path, _ = QFileDialog.getOpenFileName(None, "选择模型文件", "", "模型文件 (*.pt)")
  16. if model_path:
  17. self.model = YOLO(model_path)
  18. if self.model:
  19. return True
  20. else:
  21. return False
  22. #加载待检测图片文件
  23. def detect_image(self):
  24. image_path, _ = QFileDialog.getOpenFileName(None, "选择图片文件", "", "图片文件 (*.jpg *.jpeg *.png)")
  25. if image_path:
  26. results = self.worker.detect_image(image_path)
  27. if results:
  28. annotated_image = results[0].plot()
  29. height, width, channel = annotated_image.shape
  30. bytesPerLine = 3 * width
  31. qimage = QImage(annotated_image.data, width, height, bytesPerLine, QImage.Format_BGR888)
  32. pixmap = QPixmap.fromImage(qimage)
  33. self.label.setPixmap(pixmap.scaled(self.label.size(), Qt.KeepAspectRatio))
  34. else:
  35. QMessageBox.critical(self, "错误", "请选择图片文件")
  36. def exit_application(self):
  37. # 终止程序运行
  38. sys.exit()

模型加载前图片是不允许被检测的,所以把按钮先禁用,等模型加载后再启用

  1. self.image_detect_button.setEnabled(False)
  2. def load_model(self):
  3. if self.worker.load_model():
  4. self.image_detect_button.setEnabled(True)

2.4 界面展示

这样一个简单的ui界面就写好了

2.5 完整代码

  1. import sys
  2. from PyQt5.QtCore import Qt
  3. from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QWidget, QPushButton, QHBoxLayout, QMessageBox, QFileDialog
  4. from PyQt5.QtGui import QImage, QPixmap
  5. import cv2
  6. from ultralytics import YOLO
  7. class Worker:
  8. def __init__(self):
  9. self.model = None
  10. def load_model(self):
  11. model_path, _ = QFileDialog.getOpenFileName(None, "选择模型文件", "", "模型文件 (*.pt)")
  12. if model_path:
  13. self.model = YOLO(model_path)
  14. if self.model:
  15. return True
  16. else:
  17. return False
  18. def detect_image(self, image_path):
  19. image = cv2.imread(image_path)
  20. if image is not None:
  21. results = self.model.predict(image)
  22. return results
  23. else:
  24. return None
  25. class MainWindow(QMainWindow):
  26. def __init__(self):
  27. super().__init__()
  28. self.setWindowTitle("Numb753")
  29. self.setGeometry(100, 100, 800, 600)
  30. self.label = QLabel()
  31. self.label.setAlignment(Qt.AlignCenter)
  32. layout = QVBoxLayout()
  33. layout.addWidget(self.label)
  34. central_widget = QWidget()
  35. central_widget.setLayout(layout)
  36. self.setCentralWidget(central_widget)
  37. self.worker = Worker()
  38. # 创建按钮布局
  39. hbox_buttons = QHBoxLayout()
  40. layout.addLayout(hbox_buttons)
  41. # 添加模型选择按钮
  42. self.load_model_button = QPushButton("模型选择")
  43. self.load_model_button.clicked.connect(self.load_model)
  44. hbox_buttons.addWidget(self.load_model_button)
  45. # 添加图片检测按钮
  46. self.image_detect_button = QPushButton("图片检测")
  47. self.image_detect_button.clicked.connect(self.detect_image)
  48. self.image_detect_button.setEnabled(False)
  49. hbox_buttons.addWidget(self.image_detect_button)
  50. # 添加退出按钮
  51. self.exit_button = QPushButton("退出")
  52. self.exit_button.clicked.connect(self.exit_application)
  53. hbox_buttons.addWidget(self.exit_button)
  54. def detect_image(self):
  55. image_path, _ = QFileDialog.getOpenFileName(None, "选择图片文件", "", "图片文件 (*.jpg *.jpeg *.png)")
  56. if image_path:
  57. results = self.worker.detect_image(image_path)
  58. if results:
  59. annotated_image = results[0].plot()
  60. height, width, channel = annotated_image.shape
  61. bytesPerLine = 3 * width
  62. qimage = QImage(annotated_image.data, width, height, bytesPerLine, QImage.Format_BGR888)
  63. pixmap = QPixmap.fromImage(qimage)
  64. self.label.setPixmap(pixmap.scaled(self.label.size(), Qt.KeepAspectRatio))
  65. else:
  66. QMessageBox.critical(self, "错误", "请选择图片文件")
  67. def load_model(self):
  68. if self.worker.load_model():
  69. self.image_detect_button.setEnabled(True)
  70. def exit_application(self):
  71. # 终止程序运行
  72. sys.exit()
  73. if __name__ == '__main__':
  74. app = QApplication(sys.argv)
  75. window = MainWindow()
  76. window.show()
  77. sys.exit(app.exec_())

完整代码附上,其他需求可以私聊,遇到报错可以评论交流~

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

闽ICP备14008679号