|
| 1 | +""" |
| 2 | +Use a simple Plot to display a video frame that can be updated using a QSlider |
| 3 | +""" |
| 4 | +from PyQt6 import QtWidgets, QtCore |
| 5 | +import fastplotlib as fpl |
| 6 | +import imageio.v3 as iio |
| 7 | + |
| 8 | +# Qt app MUST be instantiated before creating any fpl objects, or any other Qt objects |
| 9 | +app = QtWidgets.QApplication([]) |
| 10 | + |
| 11 | +video = iio.imread("imageio:cockatoo.mp4") |
| 12 | + |
| 13 | +# force qt canvas, wgpu will sometimes pick glfw by default even if Qt is present |
| 14 | +plot = fpl.Plot(canvas="qt") |
| 15 | + |
| 16 | +plot.add_image(video[0], name="video") |
| 17 | +plot.camera.local.scale *= -1 |
| 18 | + |
| 19 | + |
| 20 | +def update_frame(ix): |
| 21 | + plot["video"].data = video[ix] |
| 22 | + # you can also do plot.graphics[0].data = video[ix] |
| 23 | + |
| 24 | + |
| 25 | +# create a QMainWindow, set the plot canvas as the main widget |
| 26 | +# The canvas does not have to be in a QMainWindow and it does |
| 27 | +# not have to be the central widget, it will work like any QWidget |
| 28 | +main_window = QtWidgets.QMainWindow() |
| 29 | +main_window.setCentralWidget(plot.canvas) |
| 30 | + |
| 31 | +# Create a QSlider for updating frames |
| 32 | +slider = QtWidgets.QSlider(QtCore.Qt.Orientation.Horizontal) |
| 33 | +slider.setMaximum(video.shape[0] - 1) |
| 34 | +slider.setMinimum(0) |
| 35 | +slider.valueChanged.connect(update_frame) |
| 36 | + |
| 37 | +# put slider in a dock |
| 38 | +dock = QtWidgets.QDockWidget() |
| 39 | +dock.setWidget(slider) |
| 40 | + |
| 41 | +# put the dock in the main window |
| 42 | +main_window.addDockWidget( |
| 43 | + QtCore.Qt.DockWidgetArea.BottomDockWidgetArea, |
| 44 | + dock |
| 45 | +) |
| 46 | + |
| 47 | +# calling plot.show() is required to start the rendering loop |
| 48 | +plot.show() |
| 49 | + |
| 50 | +# set window size from width and height of video |
| 51 | +main_window.resize(video.shape[2], video.shape[1]) |
| 52 | + |
| 53 | +# show the main window |
| 54 | +main_window.show() |
| 55 | + |
| 56 | +# execute Qt app |
| 57 | +app.exec() |
0 commit comments