PyQtGraph - 显示视频
在本文中,我们将看到如何在 PyQTGaph 中显示视频。 PyQtGraph 是一个用于Python的图形和用户界面库,它提供了设计和科学应用程序中通常需要的功能。它的主要目标是提供用于显示数据(绘图、视频等)的快速交互式图形。用于显示和分析图像数据的小部件。实现许多功能,例如显示 2D 和 3D 图像数据。对于 3D 数据,会显示一个 z 轴滑块,允许用户选择要显示的帧。显示具有定义暗/亮级别的可移动区域的图像数据直方图,可编辑渐变提供颜色查找表。使用图像视图对象显示视频并非常及时地更新帧。
我们可以在下面给出的命令的帮助下创建一个图像视图
# creating a pyqtgraph image view object
imv = pg.ImageView()
Syntax: ImageView(parent=None, name=’ImageView’, view=None, imageItem=None, levelMode=’mono’, *args)
Parameters:
- parent (QWidget): Specifies the parent widget to which this ImageView will belong. If None, then the ImageView is created with no parent.
- name (str): The name used to register both the internal ViewBox and the PlotItem used to display ROI data.
- view (ViewBox or PlotItem): If specified, this will be used as the display area that contains the displayed image.
- imageItem (ImageItem): If specified, this object will be used to display the image. Must be an instance of ImageItem or other compatible object.
- levelMode: specifies the *levelMode* argument
Returns: Object of class ImageView
为了绘制视频,我们必须执行以下操作
- 导入 pyqtgrah、pyqt5 和 numpy 模块
- 创建一个主窗口类
- 创建一个图形布局小部件并向其添加图像视图框
- 创建一个图像项对象并将其添加到图像视图中
- 为图像项创建随机数据
- 创建一个更新数据方法,不断更新图像的数据,使其看起来像视频并调用该方法
- 将图形布局添加到主窗口的网格布局并将此小部件设置为中央小部件
下面是实现
Python3
# importing Qt widgets
from PyQt5.QtWidgets import *
# importing system
import sys
# importing numpy as np
import numpy as np
# importing pyqtgraph as pg
import pyqtgraph as pg
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import pyqtgraph.ptime as ptime
# Image View class
class ImageView(pg.ImageView):
# constructor which inherit original
# ImageView
def __init__(self, *args, **kwargs):
pg.ImageView.__init__(self, *args, **kwargs)
class Window(QMainWindow):
def __init__(self):
super().__init__()
# setting title
self.setWindowTitle("PyQtGraph")
# setting geometry
self.setGeometry(100, 100, 600, 500)
# icon
icon = QIcon("skin.png")
# setting icon to the window
self.setWindowIcon(icon)
# calling method
self.UiComponents()
# showing all the widgets
self.show()
# method for components
def UiComponents(self):
# creating a widget object
widget = QWidget()
# creating a label
label = QLabel("Geeksforgeeks Video")
# setting minimum width
label.setMinimumWidth(130)
# making label do word wrap
label.setWordWrap(True)
# setting configuration options
pg.setConfigOptions(antialias = True)
# creating a graphics layout widget
win = pg.GraphicsLayoutWidget()
# adding view box object to graphic window
view = win.addViewBox()
##lock the aspect ratio so pixels are always square
view.setAspectLocked(True)
# Create image item
self.img = pg.ImageItem(border='w')
# adding image item to the view box
view.addItem(self.img)
# Set initial view bounds
view.setRange(QRectF(0, 0, 600, 600))
# Create random image
self.data = np.random.normal(size=(15, 600, 600),
loc = 1024, scale = 64).astype(np.uint16)
# helps in incrementing
self.i = 0
# getting time
self.updateTime = ptime.time()
# fps
self.fps = 0
# method to update the data of image
def updateData():
## Display the data
self.img.setImage(self.data[self.i])
# creating new value of i
self.i = (self.i + 1) % self.data.shape[0]
# creating a qtimer
QTimer.singleShot(1, updateData)
# getting current time
now = ptime.time()
# temporary fps
fps2 = 1.0 / (now - self.updateTime)
# updating the time
self.updateTime = now
# setting original fps value
self.fps = self.fps * 0.9 + fps2 * 0.1
# call the update method
updateData()
# Creating a grid layout
layout = QGridLayout()
# minimum width value of the label
label.setMinimumWidth(130)
# setting this layout to the widget
widget.setLayout(layout)
# adding label in the layout
layout.addWidget(label, 1, 0)
# plot window goes on right side, spanning 3 rows
layout.addWidget(win, 0, 1, 3, 1)
# setting this widget as central widget of the main widow
self.setCentralWidget(widget)
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
输出 :