当前位置: 首页 > news >正文

Yolov8可视化界面使用说明,含代码


⭐⭐ YOLOv8改进专栏|包含主干、模块、注意力机制、检测头等前沿创新 ​ ⭐⭐


YOLOv8可视化界面如下

        使用需要安装opencv-python、torch、numpy及PySide6(python版本>=3.9)

pip install PySide6
pip install numpy
pip install opencv-python

 使用说明

        运行下方代码,会出现如图所示界面,选择视频文件,左边即可播放视频。选择摄像头。左侧开始实时展示摄像头画面(需要电脑含有摄像头),选择模型即开始检测(可先选择模型),第一次检测会加载模型,会有一点卡顿。

import os
import cv2
import torch
import numpy as npfrom PySide6.QtGui import QIcon
from PySide6 import QtWidgets, QtCore, QtGuifrom ultralytics import YOLOclass MyWindow(QtWidgets.QMainWindow):def __init__(self):super().__init__()self.init_gui()self.model = Noneself.timer = QtCore.QTimer()self.timer1 = QtCore.QTimer()self.cap = Noneself.video = Noneself.timer.timeout.connect(self.camera_show)self.timer1.timeout.connect(self.video_show)def init_gui(self):self.setFixedSize(960, 440)self.setWindowTitle('Bilibili:秋芒时不知')self.setWindowIcon(QIcon("🅱️ "))centralWidget = QtWidgets.QWidget(self)self.setCentralWidget(centralWidget)mainLayout = QtWidgets.QVBoxLayout(centralWidget)topLayout = QtWidgets.QHBoxLayout()self.oriVideoLabel = QtWidgets.QLabel(self)self.detectlabel = QtWidgets.QLabel(self)self.oriVideoLabel.setMinimumSize(448, 336)self.detectlabel.setMinimumSize(448, 336)self.oriVideoLabel.setStyleSheet('border:1px solid #D7E2F9;')self.detectlabel.setStyleSheet('border:1px solid #D7E2F9;')# 960 540  1920 960topLayout.addWidget(self.oriVideoLabel)topLayout.addWidget(self.detectlabel)mainLayout.addLayout(topLayout)# 界面下半部分: 输出框 和 按钮groupBox = QtWidgets.QGroupBox(self)bottomLayout = QtWidgets.QVBoxLayout(groupBox)mainLayout.addWidget(groupBox)btnLayout = QtWidgets.QHBoxLayout()self.selectModel = QtWidgets.QPushButton('📂选择模型')self.selectModel.setFixedSize(100, 50)self.selectModel.clicked.connect(self.load_model)self.openVideoBtn = QtWidgets.QPushButton('🎞️视频文件')self.openVideoBtn.setFixedSize(100, 50)self.openVideoBtn.clicked.connect(self.start_video)self.openVideoBtn.setEnabled(False)self.openCamBtn = QtWidgets.QPushButton('📹摄像头')self.openCamBtn.setFixedSize(100, 50)self.openCamBtn.clicked.connect(self.start_camera)self.stopDetectBtn = QtWidgets.QPushButton('🛑停止')self.stopDetectBtn.setFixedSize(100, 50)self.stopDetectBtn.setEnabled(False)self.stopDetectBtn.clicked.connect(self.stop_detect)self.exitBtn = QtWidgets.QPushButton('⏹退出')self.exitBtn.setFixedSize(100, 50)self.exitBtn.clicked.connect(self.close)btnLayout.addWidget(self.selectModel)btnLayout.addWidget(self.openVideoBtn)btnLayout.addWidget(self.openCamBtn)btnLayout.addWidget(self.stopDetectBtn)btnLayout.addWidget(self.exitBtn)bottomLayout.addLayout(btnLayout)def start_camera(self):self.timer1.stop()if self.cap is None:self.cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)if self.cap.isOpened():# exit()self.timer.start(50)passself.stopDetectBtn.setEnabled(True)def camera_show(self):ret, frame = self.cap.read()if ret:if self.model is not None:frame = cv2.resize(frame, (448, 352))frame1 = self.model(frame, imgsz=[448, 352], device='cuda') if torch.cuda.is_available() \else self.model(frame, imgsz=[448, 352], device='cpu')frame1 = cv2.cvtColor(frame1[0].plot(), cv2.COLOR_RGB2BGR)frame1 = QtGui.QImage(frame1.data, frame1.shape[1], frame.shape[0], QtGui.QImage.Format_RGB888)self.detectlabel.setPixmap(QtGui.QPixmap.fromImage(frame1))frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)frame = QtGui.QImage(frame.data, frame.shape[1], frame.shape[0], QtGui.QImage.Format_RGB888)self.oriVideoLabel.setPixmap(QtGui.QPixmap.fromImage(frame))self.oriVideoLabel.setScaledContents(True)else:passdef start_video(self):if self.timer.isActive():self.timer.stop()fileName, fileType = QtWidgets.QFileDialog.getOpenFileName(self, "选取视频文件", filter='*.mp4')if os.path.isfile(fileName):# capture = cv2.VideoCapture(fileName)# frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))self.video = cv2.VideoCapture(fileName)fps = self.video.get(cv2.CAP_PROP_FPS)self.timer1.start(int(1/fps))else:print("Reselect video")def video_show(self):ret, frame = self.video.read()if ret:if self.model is not None:frame = cv2.resize(frame, (448, 352))frame1 = self.model(frame, imgsz=[448, 352], device='cuda') if torch.cuda.is_available() \else self.model(frame, imgsz=[448, 352], device='cpu')frame1 = cv2.cvtColor(frame1[0].plot(), cv2.COLOR_RGB2BGR)frame1 = QtGui.QImage(frame1.data, frame1.shape[1], frame.shape[0], QtGui.QImage.Format_RGB888)self.detectlabel.setPixmap(QtGui.QPixmap.fromImage(frame1))frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)frame = QtGui.QImage(frame.data, frame.shape[1], frame.shape[0], QtGui.QImage.Format_RGB888)self.oriVideoLabel.setPixmap(QtGui.QPixmap.fromImage(frame))self.oriVideoLabel.setScaledContents(True)else:self.timer1.stop()img = cv2.cvtColor(np.zeros((500, 500), np.uint8), cv2.COLOR_BGR2RGB)img = QtGui.QImage(img.data, img.shape[1], img.shape[0], QtGui.QImage.Format_RGB888)self.oriVideoLabel.setPixmap(QtGui.QPixmap.fromImage(img))self.detectlabel.setPixmap(QtGui.QPixmap.fromImage(img))self.video.release()self.video = Nonedef load_model(self):fileName, fileType = QtWidgets.QFileDialog.getOpenFileName(self, "选取模型权重", filter='*.pt')if fileName.endswith('.pt'):self.model = YOLO(fileName)else:print("Reselect model")self.openVideoBtn.setEnabled(True)self.stopDetectBtn.setEnabled(True)def stop_detect(self):if self.timer.isActive():self.timer.stop()if self.timer1.isActive():self.timer1.stop()if self.cap is not None:self.cap.release()self.cap = Noneself.video = Noneimg = cv2.cvtColor(np.zeros((500, 500), np.uint8), cv2.COLOR_BGR2RGB)img = QtGui.QImage(img.data, img.shape[1], img.shape[0], QtGui.QImage.Format_RGB888)self.oriVideoLabel.setPixmap(QtGui.QPixmap.fromImage(img))self.detectlabel.setPixmap(QtGui.QPixmap.fromImage(img))def close(self):if self.cap is not None:self.cap.release()self.cap = Noneif self.timer.isActive():self.timer.stop()exit()if __name__ == '__main__':app = QtWidgets.QApplication()window = MyWindow()window.show()app.exec()

效果展示

相关文章:

  • FastAPI 基本路由
  • 新能源行业知识体系-------主目录-----持续更新
  • Java校园跑腿小程序校园代买帮忙外卖源码社区外卖源码
  • C语言的数据结构:图的操作
  • 不要再被骗了!电脑无法进入系统的原因可能是这个硬件坏了而已……
  • lodash.js 工具库
  • Follow Carl To Grow|【LeetCode】93.复原IP地址,78.子集,90.子集II
  • 小红书多账号管理平台哪个好用?可以快速监测多个小红书账号的数据吗?
  • Python 提取图片主色调
  • Canvas 指纹:它是什么以及如何绕过它
  • 聊聊etsy平台,一个年入百万的项目
  • 在编译 PHP 8.3.8 时遇到 configure: error: Package requirements (libxml-2.0 >= 2.9.0)
  • Linux-笔记 全志T113移植正点4.3寸RGB屏幕笔记
  • Spring之事务失效的场景
  • 【推荐】Prometheus+Grafana企业级监控预警实战
  • CEF与代理
  • css属性的继承、初识值、计算值、当前值、应用值
  • docker python 配置
  • HTTP--网络协议分层,http历史(二)
  • Leetcode 27 Remove Element
  • Map集合、散列表、红黑树介绍
  • Nodejs和JavaWeb协助开发
  • PHP 程序员也能做的 Java 开发 30分钟使用 netty 轻松打造一个高性能 websocket 服务...
  • Python进阶细节
  • springboot_database项目介绍
  • 复杂数据处理
  • 开年巨制!千人千面回放技术让你“看到”Flutter用户侧问题
  • 使用Maven插件构建SpringBoot项目,生成Docker镜像push到DockerHub上
  • 树莓派用上kodexplorer也能玩成私有网盘
  • ​520就是要宠粉,你的心头书我买单
  • ​第20课 在Android Native开发中加入新的C++类
  • ​水经微图Web1.5.0版即将上线
  • (145)光线追踪距离场柔和阴影
  • (C语言)深入理解指针2之野指针与传值与传址与assert断言
  • (k8s中)docker netty OOM问题记录
  • (ros//EnvironmentVariables)ros环境变量
  • (Ruby)Ubuntu12.04安装Rails环境
  • (备忘)Java Map 遍历
  • (附源码)spring boot公选课在线选课系统 毕业设计 142011
  • (附源码)springboot 房产中介系统 毕业设计 312341
  • (一)认识微服务
  • **PHP分步表单提交思路(分页表单提交)
  • . Flume面试题
  • .htaccess配置重写url引擎
  • .net 桌面开发 运行一阵子就自动关闭_聊城旋转门家用价格大约是多少,全自动旋转门,期待合作...
  • .NET技术成长路线架构图
  • .NET使用HttpClient以multipart/form-data形式post上传文件及其相关参数
  • ??如何把JavaScript脚本中的参数传到java代码段中
  • @JoinTable会自动删除关联表的数据
  • @manytomany 保存后数据被删除_[Windows] 数据恢复软件RStudio v8.14.179675 便携特别版...
  • [ C++ ] 继承
  • [ 渗透测试面试篇 ] 渗透测试面试题大集合(详解)(十)RCE (远程代码/命令执行漏洞)相关面试题
  • [ 云计算 | Azure 实践 ] 在 Azure 门户中创建 VM 虚拟机并进行验证
  • [20171102]视图v$session中process字段含义
  • [AI Google] 使用 Gemini 取得更多成就:试用 1.5 Pro 和更多智能功能