I'm tying to create a simple application to monitor a webcam and taking pictures, basically following the Qt for Python Camera Example, which uses PySide6 (Qt6).
However, for my case, I must use PySide2 (Qt5) instead, as the embed computer for this application has i386 architecture (32-bit Debian 11 installed). This is a small (working) sample code:
from pathlib import Path
from tempfile import NamedTemporaryFile
from PySide2 import QtWidgets, QtMultimedia, QtMultimediaWidgets
class SimpleCameraGUI(QtWidgets.QMainWindow):
def __init__(self):
super(SimpleCameraGUI, self).__init__(None)
# main GUI elelements
central_widget = QtWidgets.QWidget()
hlayout = QtWidgets.QHBoxLayout()
vlayout = QtWidgets.QVBoxLayout()
self.start = QtWidgets.QPushButton('Start camera')
self.stop = QtWidgets.QPushButton('Stop camera')
self.take_pic = QtWidgets.QPushButton('Take Picture')
self.camera_widget = QtMultimediaWidgets.QCameraViewfinder()
# elements to Layout
hlayout.addWidget(self.start)
hlayout.addWidget(self.stop)
hlayout.addWidget(self.take_pic)
vlayout.addWidget(self.camera_widget)
vlayout.addLayout(hlayout)
central_widget.setLayout(vlayout)
self.setCentralWidget(central_widget)
# camera elements
self.info = QtMultimedia.QCameraInfo()
self.camera = QtMultimedia.QCamera(self.info.defaultCamera())
self.image = QtMultimedia.QCameraImageCapture(self.camera)
self.camera.setViewfinder(self.camera_widget)
# connections
self.start.clicked.connect(self.camera.start)
self.stop.clicked.connect(self.camera.stop)
self.take_pic.clicked.connect(self.save_temp_picture)
# show GUI
self.show()
def save_temp_picture(self):
filename = NamedTemporaryFile(suffix='.jpg', delete=True)
self.image.capture(filename.name)
QtWidgets.QMessageBox.information(
None,
'Temporary file saved',
f'Location: <a href={Path(filename.name).as_uri()}>{filename.name}</a>'
)
if __name__ == '__main__':
app = QtWidgets.QApplication()
win = SimpleCameraGUI()
win.resize(640, 480)
app.exec_()
The application works fine, but the video inside the QCameraViewfinder widget is extremely slow. I believe it has something to do with frame rate settings, but couldn't find where/how to change the parameters. My PySide2 version is 5.15.2.1.
Tried to use a QVideoWidget, but results are the same. Also tried to use the method setViewfinderSettings of the QCamera, but couldn't figure out how to actually use it.
After some searching I did found a solution.
One must create a QCameraViewfinderSettings object, set the desired frame rate range using setMinimumFrameRate() and setMaximumFrameRate() methods and finally apply those settings to the QCamera using setViewfinderSettings() method. I also discovered that it is important to choose the proper resolution of your camera by using setResolution().
Below is a revised code:
from pathlib import Path
from tempfile import NamedTemporaryFile
from PySide2 import QtWidgets, QtMultimedia, QtMultimediaWidgets
class SimpleCameraGUI(QtWidgets.QMainWindow):
def __init__(self):
super(SimpleCameraGUI, self).__init__(None)
# main GUI elelements
central_widget = QtWidgets.QWidget()
hlayout = QtWidgets.QHBoxLayout()
vlayout = QtWidgets.QVBoxLayout()
self.start = QtWidgets.QPushButton('Start camera')
self.stop = QtWidgets.QPushButton('Stop camera')
self.take_pic = QtWidgets.QPushButton('Take Picture')
self.camera_widget = QtMultimediaWidgets.QCameraViewfinder()
# elements to Layout
hlayout.addWidget(self.start)
hlayout.addWidget(self.stop)
hlayout.addWidget(self.take_pic)
vlayout.addWidget(self.camera_widget)
vlayout.addLayout(hlayout)
central_widget.setLayout(vlayout)
self.setCentralWidget(central_widget)
# camera elements
self.info = QtMultimedia.QCameraInfo()
self.camera = QtMultimedia.QCamera(self.info.defaultCamera())
self.image = QtMultimedia.QCameraImageCapture(self.camera)
self.camera.setViewfinder(self.camera_widget)
# sets resolution and frame rate to camera/viewfinder
self.settings = QtMultimedia.QCameraViewfinderSettings()
self.settings.setResolution(640, 480)
self.settings.setMinimumFrameRate(15)
self.settings.setMaximumFrameRate(30)
self.camera.setViewfinderSettings(self.settings)
# connections
self.start.clicked.connect(self.camera.start)
self.stop.clicked.connect(self.camera.stop)
self.take_pic.clicked.connect(self.save_temp_picture)
# show GUI
self.show()
def save_temp_picture(self):
filename = NamedTemporaryFile(suffix='.jpg', delete=True)
self.image.capture(filename.name)
QtWidgets.QMessageBox.information(
None,
'Temporary file saved',
f'Location: <a href={Path(filename.name).as_uri()}>{filename.name}</a>'
)
if __name__ == '__main__':
app = QtWidgets.QApplication()
win = SimpleCameraGUI()
win.resize(640, 480)
app.exec_()
Related
I've created a gui using PyQt5 in PyCharm and I've managed to get one QLabel with an image in it (Picture1.png) showing up, however, when I try to add a second QLabel with a second image (named Shutter1.png) on the same window, it seems to remove both labels and nothing shows up on the gui. I'm not sure where I'm going wrong and any help would be greatly appreciated, I'm a novice! NB I've doublechecked the filepath for both imagePath and imagePath_1 are correct. See below for attached code:
from PyQt5 import uic, QtWidgets, QtGui, QtCore
import sys
import pkg_resources
import functions.initialisation as inits
import functions.Sig2Open as S2O
import functions.Sig2Close as S2C
class Ui(QtWidgets.QMainWindow):
def __init__(self):
super(Ui, self).__init__()
self.gui = uic.loadUi('Shuttergui.ui', self)
# Creates the path of the image
self.imagePath = "C:/........../Picture1.png"
self.label = QtWidgets.QLabel(self.gui)
self.image = QtGui.QImage(self.imagePath)
self.pixmapImage = QtGui.QPixmap.fromImage(self.image)
self.label.setPixmap(self.pixmapImage)
self.label.resize(self.width(), self.height())
self.label.move(60, 170)
self.imagePath = "C:/....../Shutter1.png"
# Create label that holds the image in imagePath
self.label_1 = QtWidgets.QLabel(self.gui)
self.image_1 = QtGui.QImage(self.imagePath)
self.pixmapImage_1 = QtGui.QPixmap.fromImage(self.image_1)
self.label_1.setPixmap(self.pixmapImage_1)
self.label_1.resize(self.width(), self.height())
self.label_1.move(60, 170)
self.gui.showMaximized()
# redirect closeevent func to main self rather than inside gui
self.gui.closeEvent = self.closeEvent
# Initialise shutter functions
inits.ardopenup(self)
inits.ardshutup(self)
self.gui.show()
def closeEvent(self, event):
import time
time.sleep(0.1)
print("main thread quitting")
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
app.setStyleSheet(pkg_resources.resource_stream(__name__, '/css/darktheme/style.css').read().decode())
window = Ui()
sys.exit(app.exec_())
I am creating a video player and I need to draw some polygons on top of it. I am using a QGraphicsScene to create this and I need to update the polygons on screen after each frame. I am currently using the QMediaPlayer paired up with a QGraphicsVideoItem to create this. The problem I am having is that the QMediaPlayer doesn't have a signal that activates on each frame. It has positionChanged(), but this only seems to trigger once every second.
I tried using QMovie since it does send updates on every frame, but it did not display anything. This is the code I used to implement this.
video_view = QGraphicsView()#view to hold video
video_item = QGraphicsVideoItem()#video item for scene
video_scene = QGraphicsScene()#scene for Qgraphics view
video_view.setScene(video_scene)
label = QLabel()
movie = QMovie(self.video_paths[index]) #contains file path
label.setMovie(movie)
video_scene.addWidget(label)
self.vlayout_main_video.addWidget(video_view)
The video file I am using is a .avi file and it is 72Mb large.
I would really appreciate it if somebody could point me in the right direction on how I could do this. I am currently using PyQt5.
Thank you
There are 2 options:
positionChanged is emited every second because the notifyInterval property of QMediaPlayer is set in that period. So you can change that property, for example to 60 ms.
from PyQt5 import QtCore, QtGui, QtWidgets, QtMultimedia, QtMultimediaWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
scene = QtWidgets.QGraphicsScene(self)
self.video_view = QtWidgets.QGraphicsView(scene)
self.setCentralWidget(self.video_view)
self.player = QtMultimedia.QMediaPlayer(self, QtMultimedia.QMediaPlayer.VideoSurface)
self.video_item = QtMultimediaWidgets.QGraphicsVideoItem()
self.player.setVideoOutput(self.video_item)
scene.addItem(self.video_item)
file = "/path/of/video"
self.player.setMedia(QtMultimedia.QMediaContent(QtCore.QUrl.fromLocalFile(file)))
self.player.positionChanged.connect(self.on_positionChanged)
self.player.setNotifyInterval(60)
self.player.play()
#QtCore.pyqtSlot('qint64')
def on_positionChanged(self, p):
print(p, QtCore.QTime.currentTime().toString("hh:mm:ss.zzz"))
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
Use the VideoFrameProbed signal from QVideoProbe:
from PyQt5 import QtCore, QtGui, QtWidgets, QtMultimedia, QtMultimediaWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
scene = QtWidgets.QGraphicsScene(self)
self.video_view = QtWidgets.QGraphicsView(scene)
self.setCentralWidget(self.video_view)
self.player = QtMultimedia.QMediaPlayer(self, QtMultimedia.QMediaPlayer.VideoSurface)
self.video_item = QtMultimediaWidgets.QGraphicsVideoItem()
self.player.setVideoOutput(self.video_item)
scene.addItem(self.video_item)
file = "/path/of/video"
self.player.setMedia(QtMultimedia.QMediaContent(QtCore.QUrl.fromLocalFile(file)))
self.player.play()
probe = QtMultimedia.QVideoProbe(self)
probe.videoFrameProbed.connect(self.on_videoFrameProbed)
probe.setSource(self.player)
#QtCore.pyqtSlot()
def on_videoFrameProbed(self):
print(QtCore.QTime.currentTime().toString("hh:mm:ss.zzz"))
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
I would like to implement a class to create a simple widget of fixed size with a scrollbar to display one or more (that's crucial to the problem) images at the same time. Here is the (yet complete but working) code:
from PyQt5 import QtCore, QtWidgets, QtGui
class ImageViewWidget(QtWidgets.QScrollArea):
def __init__(self, parent = None):
super(ImageViewWidget, self).__init__(parent)
self.w = QtWidgets.QFrame()
self.l = QtWidgets.QVBoxLayout()
self.w.setLayout(self.l)
self.setWidget(self.w)
def setImages(self, *images):
self.imageLabel = QtWidgets.QLabel()
self.imageLabel.setScaledContents(True)
self.l.addWidget(self.imageLabel)
if not images[0].isNull():
self.imageLabel.setPixmap(QtGui.QPixmap.fromImage(images[0]))
self.normalSize()
## event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, QtCore.Qt.Key_N, QtCore.Qt.NoModifier)
## QtWidgets.QApplication.sendEvent(self, event)
def normalSize(self):
self.w.adjustSize()
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_N:
self.normalSize()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
imageViewer = ImageViewWidget()
imageViewer.resize(800, 600)
imageViewer.show()
image1 = QtGui.QImage('test.png')
imageViewer.setImages(image1)
sys.exit(app.exec_())
The problem is, that the image does not show up at startup resp does not have the desired size. One has to press "n" first, then the image is displayed with its natural size. And of course I would like to have its natural size from the beginning on without the need to press "n" first.
It seems strange to me that pressing "n" and calling self.normalSize() do not have the same effect, and even simulation the key event by the two commented outlines in setImages do not have the same effect as pressing "n" physically.
There are two "solutions":
Show the widget after setting image, that is, move the line imageViewer.show() 2 lines down.
Moving the first 3 lines of the method setImages to the __init__ method.
Both are no reasonable option, since I want to add and remove dynamically QLabels(which is not implemented yet) to display different images, and also the number of images (which are displayed at the same time) can change.
Any suggestions?
Hi I have modified your code.
Added this 2 lines.
self.timerSingleShot = QtCore.QTimer()
self.timerSingleShot.singleShot(1, self.normalSize)
Use with PyQt5 syntax. This syntax is for PyQt4
from PyQt5 import QtCore, QtWidgets, QtGui
class ImageViewWidget(QtWidgets.QScrollArea):
def __init__(self, parent = None):
super(ImageViewWidget, self).__init__(parent)
self.w = QtWidgets.QFrame()
self.l = QtWidgets.QVBoxLayout()
self.w.setLayout(self.l)
self.setWidget(self.w)
# Added this lines
self.timerSingleShot = QtCore.QTimer()
self.timerSingleShot.singleShot(1, self.normalSize)
def setImages(self, *images):
self.imageLabel = QtWidgets.QLabel()
self.imageLabel.setScaledContents(True)
self.l.addWidget(self.imageLabel)
if not images[0].isNull():
self.imageLabel.setPixmap(QtGui.QPixmap.fromImage(images[0]))
#self.normalSize()
## event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, QtCore.Qt.Key_N, QtCore.Qt.NoModifier)
## QtWidgets.QApplication.sendEvent(self, event)
def normalSize(self):
self.w.adjustSize()
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_N:
self.normalSize()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
imageViewer = ImageViewWidget()
imageViewer.resize(800, 600)
imageViewer.show()
image1 = QtGui.QImage('test.png')
imageViewer.setImages(image1)
sys.exit(app.exec_())
This will also work. Shifted line :
imageViewer.show()
in your code.
from PyQt5 import QtCore, QtWidgets, QtGui
class ImageViewWidget(QtWidgets.QScrollArea):
def __init__(self, parent = None):
super(ImageViewWidget, self).__init__(parent)
self.w = QtWidgets.QFrame()
self.l = QtWidgets.QVBoxLayout()
self.w.setLayout(self.l)
self.setWidget(self.w)
def setImages(self, *images):
self.imageLabel = QtWidgets.QLabel()
self.imageLabel.setScaledContents(True)
self.l.addWidget(self.imageLabel)
if not images[0].isNull():
self.imageLabel.setPixmap(QtGui.QPixmap.fromImage(images[0]))
#self.normalSize()
## event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, QtCore.Qt.Key_N, QtCore.Qt.NoModifier)
## QtWidgets.QApplication.sendEvent(self, event)
def normalSize(self):
self.w.adjustSize()
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_N:
self.normalSize()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
imageViewer = ImageViewWidget()
imageViewer.resize(800, 600)
image1 = QtGui.QImage('test.png')
imageViewer.setImages(image1)
imageViewer.show()
sys.exit(app.exec_())
I am pretty new at python but currently I am getting some problem here with the part where I am unable to get my stuff fit within the width of the window.
I am trying to set it in such a way that it is:
Eg. Name Button
by the way, I am using Maya to integrate and run my stuff.
If I set it to central, it fits but it is all over the place as I only wanted it to occupy a portion only. So are there any ways for me to fit it nicely into it?
By the way, if its possible, can it be done using my current codings?
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sip
import maya.OpenMayaUI as mui
import os
class MainWindow(QMainWindow):
def __init__(self, parent = None):
QMainWindow.__init__(self,parent)
self.resize(400,800)
self.setWindowTitle("GetShots")
self.pubDock = SetShotInfo()
self.pubDW = QDockWidget(self.tr(""), self)
self.pubDW.setWidget(self.pubDock)
# self.setCentralWidget(self.pubDW)
def getMayaWindow():
ptr = mui.MQtUtil.mainWindow()
return sip.wrapinstance(long(ptr), QObject)
def main():
global app
global form
app = qApp
form = MainWindow(getMayaWindow())
form.show()
class GetShot(QFrame):
def __init__(self, parent = None, display=None):
QFrame.__init__(self, parent)
self.createWidgets()
self.createLayout()
def createWidgets(self):
self.showLabel = QLabel('Show')
self.showName = QLineEdit()
self.showName.setText(str(os.environ['SHOW']))
self.shotLabel = QLabel('Shot Filter')
self.shotName = QLineEdit()
self.showButton = QPushButton('Set Show')
self.showButton.setMaximumWidth(200)
self.shotButton = QPushButton('Filter Shots')
self.shotButton.setMaximumWidth(200)
self.rootLabel = QLabel('Change Root')
self.rootButton = QComboBox()
def createLayout(self):
# Sets the Layout of Show and Shot
setShowLayout = QHBoxLayout()
setShowLayout.addWidget(self.showLabel)
setShowLayout.addWidget(self.showName)
setShowLayout.addWidget(self.showButton)
setShotLayout = QHBoxLayout()
setShotLayout.addWidget(self.shotLabel)
setShotLayout.addWidget(self.shotName)
setShotLayout.addWidget(self.shotButton)
# Sets the Change Root Layout
chgRootLayout = QHBoxLayout()
chgRootLayout.addWidget(self.rootLabel)
chgRootLayout.addWidget(self.rootButton)
mainLayout = QVBoxLayout()
mainLayout.addLayout(setShowLayout)
mainLayout.addLayout(setShotLayout)
mainLayout.addLayout(chgRootLayout)
self.setLayout(mainLayout)
if __name__ == '__main__':
main()
You need to use Layouts, combine vertical and horizontal and play with the size policy of the widgets to fit them as you need.
Here's a quick example:
import sys
from PyQt4 import QtCore, QtGui
class ButtonContainer(QtGui.QWidget):
def __init__(self):
super(ButtonContainer, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry( 150, 150, 650, 350)
btn = QtGui.QPushButton('Name button', self)
btn.setSizePolicy( QtGui.QSizePolicy( QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed ) )
vbox = QtGui.QVBoxLayout()
vbox.addWidget( btn )
self.setLayout(vbox)
self.show()
app = QtGui.QApplication(sys.argv)
ex = ButtonContainer()
sys.exit(app.exec_())
The commenters are right in suggesting QtDesigner, if you'd rather code it yourself at least have a mock up ui file where you can play interactively with the layouts and size policies, it's really helpful.
I have a small Python script that makes a transparent window for displaying a graphic on screen and I'd like to animate that graphic, but am entirely unsure how or where to even start. Here's what I do have at least:
import sys
from PyQt4 import QtGui, Qt, QtCore
class Transparent(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.setAttribute(Qt.Qt.WA_NoSystemBackground)
self.setAutoFillBackground(True)
pixmap = QtGui.QPixmap("test1.gif")
pixmap2 = QtGui.QPixmap("test2.gif")
width = pixmap.width()
height = pixmap.height()
self.setWindowTitle("Status")
self.resize(width, height)
self.label = QtGui.QLabel(self)
def animateEvent():
imgnumber = 0
try:
if imgnumber == 1:
self.label.setPixmap(QtGui.QPixmap("test1.gif"))
self.setMask(pixmap.mask())
imgnumber = 0
else:
self.label.setPixmap(QtGui.QPixmap("test2.gif"))
self.setMask(pixmap2.mask())
imgnumber = 1
finally:
QtCore.QTimer.singleShot(1000, animateEvent)
animateEvent()
def paintEvent(self,event):
self.setAttribute(Qt.Qt.WA_NoSystemBackground)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
x = Transparent()
x.show()
app.exec_()
This feels like it has the right ingredients, but the pixmap doesn't update.
I tried QMovie, but then the area of the window that is supposed to be transparent is filled with black instead.
check out this code from www.daniweb.com and see if you can modify it to your needs:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class MoviePlayer(QWidget):
def __init__(self, gif, parent=None):
super(MoviePlayer, self).__init__(parent)
self.setGeometry(200, 200, 400, 400)
self.setWindowTitle("QMovie to show animated gif")
self.movie_screen = QLabel()
self.movie_screen.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.movie_screen.setAlignment(Qt.AlignCenter)
btn_start = QPushButton("Start Animation")
btn_start.clicked.connect(self.start)
btn_stop = QPushButton("Stop Animation")
btn_stop.clicked.connect(self.stop)
main_layout = QVBoxLayout()
main_layout.addWidget(self.movie_screen)
main_layout.addWidget(btn_start)
main_layout.addWidget(btn_stop)
self.setLayout(main_layout)
self.movie = QMovie(gif, QByteArray(), self)
self.movie.setCacheMode(QMovie.CacheAll)
self.movie.setSpeed(100)
self.movie_screen.setMovie(self.movie)
def start(self):
"""
Start animation
"""
self.movie.start()
def stop(self):
"""
Stop the animation
"""
self.movie.stop()
app = QApplication(sys.argv)
player = MoviePlayer("/path/to/image.gif")
player.show()
sys.exit(app.exec_())
This ended up being a simple correction of an oversight in the end.
imgnumber needed to be outside of the def as self.imgnumber and needed to be named self.imgnumber each time it was changed.
First, just make sure your animated gif really does have a proper transparent background. The following code works for me, using this fire image as a source:
class Transparent(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.setAttribute(QtCore.Qt.WA_NoSystemBackground)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
filename = "test.gif"
size = QtGui.QImage(filename).size()
self.setWindowTitle("Status")
layout = QtGui.QVBoxLayout(self)
layout.setMargin(0)
self.movie = QtGui.QMovie(filename)
self.label = QtGui.QLabel(self)
self.label.setMovie(self.movie)
layout.addWidget(self.label)
self.resize(size)
self.movie.start()
This will create a completely transparent and frameless window, with the animated gif playing in a QMovie. There is no black being drawn behind the image. It should fully see through to what ever is underneath.
It is not so far off from your original code. You shouldn't need to set the mask, or do a paint event.