尝试使用 PySide2 显示网络摄像头时没有可用的取景器
No viewfinder available while trying to display webcam with PySide2
我正在使用 Python3 和 PySide2 开发 Ubuntu 20.04,Qt 是版本 5。
我编写这个简单的 Python 脚本是为了测试保存网络摄像头视频以供我稍后处理的更大项目使用的能力。
基本上,我希望这个演示脚本首先只是显示来自相机的流,在我命名为“frame”的小部件中,但似乎我遗漏了一些东西,因为一条消息打印在终端机:
在没有可用取景器的情况下启动相机
我在 QtCreator 中制作了 GUI,然后用 pyside-uic 转换了它。
我为名为“frame”的小部件应用了黑色背景色。
当我单击“连接”按钮时,我希望看到视频开始显示,但“框架”保持黑色,我在终端中收到有关取景器的消息。
取景器不是显示视频的地方(又名小部件)吗?
我觉得有问题,但我认为这很容易解决,因为当我点击连接按钮时,网络摄像头的 LED(正确)亮起。
这是python代码:
from PySide2.QtMultimedia import QCamera, QCameraInfo, QCamera
from PySide2.QtMultimedia import *
from PySide2.QtMultimedia import QCameraViewfinderSettings
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
import MainWindow
import sys
from MainWindow import Ui_MainWindow
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setupUi(self)
self.available_cameras = QCameraInfo.availableCameras()
print(self.available_cameras[0].description())
self.camera_combobox.addItem( self.available_cameras[0].description() )
self.camera_combobox.addItem( self.available_cameras[1].description() )
self.connect_btn.clicked.connect(self.connect_btn_clicked)
def connect_btn_clicked(self):
self.camera = QCamera(self.available_cameras[0])
self.frame.setSizePolicy(QSizePolicy.Maximum,QSizePolicy.Maximum)
self.camera.setViewfinder(self.frame)
self.camera.start()
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
这是MainWindow.py代码
# -*- coding: utf-8 -*-
################################################################################
## Form generated from reading UI file 'mainwindow.ui'
##
## Created by: Qt User Interface Compiler version 5.15.1
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
if not MainWindow.objectName():
MainWindow.setObjectName(u"MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName(u"centralwidget")
self.horizontalLayoutWidget = QWidget(self.centralwidget)
self.horizontalLayoutWidget.setObjectName(u"horizontalLayoutWidget")
self.horizontalLayoutWidget.setGeometry(QRect(50, 30, 298, 80))
self.horizontalLayout = QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setObjectName(u"horizontalLayout")
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.label = QLabel(self.horizontalLayoutWidget)
self.label.setObjectName(u"label")
self.horizontalLayout.addWidget(self.label)
self.camera_combobox = QComboBox(self.horizontalLayoutWidget)
self.camera_combobox.setObjectName(u"camera_combobox")
self.horizontalLayout.addWidget(self.camera_combobox)
self.connect_btn = QPushButton(self.horizontalLayoutWidget)
self.connect_btn.setObjectName(u"connect_btn")
self.horizontalLayout.addWidget(self.connect_btn)
self.frame = QFrame(self.centralwidget)
self.frame.setObjectName(u"frame")
self.frame.setGeometry(QRect(60, 210, 301, 171))
sizePolicy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.frame.sizePolicy().hasHeightForWidth())
self.frame.setSizePolicy(sizePolicy)
self.frame.setStyleSheet(u"background-color: rgb(0, 0, 0);")
self.horizontalLayout_2 = QHBoxLayout(self.frame)
self.horizontalLayout_2.setObjectName(u"horizontalLayout_2")
self.horizontalLayoutWidget_2 = QWidget(self.centralwidget)
self.horizontalLayoutWidget_2.setObjectName(u"horizontalLayoutWidget_2")
self.horizontalLayoutWidget_2.setGeometry(QRect(60, 110, 291, 51))
self.horizontalLayout_3 = QHBoxLayout(self.horizontalLayoutWidget_2)
self.horizontalLayout_3.setObjectName(u"horizontalLayout_3")
self.horizontalLayout_3.setContentsMargins(0, 0, 0, 0)
self.capture_btn = QPushButton(self.horizontalLayoutWidget_2)
self.capture_btn.setObjectName(u"capture_btn")
self.horizontalLayout_3.addWidget(self.capture_btn)
self.record_btn = QPushButton(self.horizontalLayoutWidget_2)
self.record_btn.setObjectName(u"record_btn")
self.horizontalLayout_3.addWidget(self.record_btn)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QMenuBar(MainWindow)
self.menubar.setObjectName(u"menubar")
self.menubar.setGeometry(QRect(0, 0, 800, 26))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QStatusBar(MainWindow)
self.statusbar.setObjectName(u"statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QMetaObject.connectSlotsByName(MainWindow)
# setupUi
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
self.label.setText(QCoreApplication.translate("MainWindow", u"Device:", None))
self.connect_btn.setText(QCoreApplication.translate("MainWindow", u"Connect", None))
self.capture_btn.setText(QCoreApplication.translate("MainWindow", u"Capture", None))
self.record_btn.setText(QCoreApplication.translate("MainWindow", u"Record", None))
# retranslateUi
以上代码是
的输出
pyside2-uic mainwindow.ui -o MainWindow.py
这里是mainwindow.ui
的内容
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QWidget" name="horizontalLayoutWidget">
<property name="geometry">
<rect>
<x>50</x>
<y>30</y>
<width>298</width>
<height>80</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Device:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="camera_combobox"/>
</item>
<item>
<widget class="QPushButton" name="connect_btn">
<property name="text">
<string>Connect</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QFrame" name="frame">
<property name="geometry">
<rect>
<x>60</x>
<y>210</y>
<width>301</width>
<height>171</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(0, 0, 0);</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2"/>
</widget>
<widget class="QWidget" name="horizontalLayoutWidget_2">
<property name="geometry">
<rect>
<x>60</x>
<y>110</y>
<width>291</width>
<height>51</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QPushButton" name="capture_btn">
<property name="text">
<string>Capture</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="record_btn">
<property name="text">
<string>Record</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>26</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
QCamera 需要一个 QCameraViewfinder,它是一个允许显示 QCamera 捕获的信息的小部件,因此您不能将 QFrame 传递给它。
import sys
from PySide2.QtWidgets import QApplication, QMainWindow
from PySide2.QtMultimedia import QCamera, QCameraInfo
from PySide2.QtMultimediaWidgets import QCameraViewfinder
from MainWindow import Ui_MainWindow
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setupUi(self)
for info in QCameraInfo.availableCameras():
self.camera_combobox.addItem(info.description(), info)
self.connect_btn.clicked.connect(self.connect_btn_clicked)
self.view_finder = QCameraViewfinder()
self.frame.layout().addWidget(self.view_finder)
def connect_btn_clicked(self):
info = self.camera_combobox.currentData()
self.camera = QCamera(info)
self.camera.setViewfinder(self.view_finder)
self.camera.start()
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
我正在使用 Python3 和 PySide2 开发 Ubuntu 20.04,Qt 是版本 5。
我编写这个简单的 Python 脚本是为了测试保存网络摄像头视频以供我稍后处理的更大项目使用的能力。
基本上,我希望这个演示脚本首先只是显示来自相机的流,在我命名为“frame”的小部件中,但似乎我遗漏了一些东西,因为一条消息打印在终端机:
在没有可用取景器的情况下启动相机
我在 QtCreator 中制作了 GUI,然后用 pyside-uic 转换了它。
我为名为“frame”的小部件应用了黑色背景色。 当我单击“连接”按钮时,我希望看到视频开始显示,但“框架”保持黑色,我在终端中收到有关取景器的消息。 取景器不是显示视频的地方(又名小部件)吗?
我觉得有问题,但我认为这很容易解决,因为当我点击连接按钮时,网络摄像头的 LED(正确)亮起。
这是python代码:
from PySide2.QtMultimedia import QCamera, QCameraInfo, QCamera
from PySide2.QtMultimedia import *
from PySide2.QtMultimedia import QCameraViewfinderSettings
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
import MainWindow
import sys
from MainWindow import Ui_MainWindow
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setupUi(self)
self.available_cameras = QCameraInfo.availableCameras()
print(self.available_cameras[0].description())
self.camera_combobox.addItem( self.available_cameras[0].description() )
self.camera_combobox.addItem( self.available_cameras[1].description() )
self.connect_btn.clicked.connect(self.connect_btn_clicked)
def connect_btn_clicked(self):
self.camera = QCamera(self.available_cameras[0])
self.frame.setSizePolicy(QSizePolicy.Maximum,QSizePolicy.Maximum)
self.camera.setViewfinder(self.frame)
self.camera.start()
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
这是MainWindow.py代码
# -*- coding: utf-8 -*-
################################################################################
## Form generated from reading UI file 'mainwindow.ui'
##
## Created by: Qt User Interface Compiler version 5.15.1
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
if not MainWindow.objectName():
MainWindow.setObjectName(u"MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName(u"centralwidget")
self.horizontalLayoutWidget = QWidget(self.centralwidget)
self.horizontalLayoutWidget.setObjectName(u"horizontalLayoutWidget")
self.horizontalLayoutWidget.setGeometry(QRect(50, 30, 298, 80))
self.horizontalLayout = QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setObjectName(u"horizontalLayout")
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.label = QLabel(self.horizontalLayoutWidget)
self.label.setObjectName(u"label")
self.horizontalLayout.addWidget(self.label)
self.camera_combobox = QComboBox(self.horizontalLayoutWidget)
self.camera_combobox.setObjectName(u"camera_combobox")
self.horizontalLayout.addWidget(self.camera_combobox)
self.connect_btn = QPushButton(self.horizontalLayoutWidget)
self.connect_btn.setObjectName(u"connect_btn")
self.horizontalLayout.addWidget(self.connect_btn)
self.frame = QFrame(self.centralwidget)
self.frame.setObjectName(u"frame")
self.frame.setGeometry(QRect(60, 210, 301, 171))
sizePolicy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.frame.sizePolicy().hasHeightForWidth())
self.frame.setSizePolicy(sizePolicy)
self.frame.setStyleSheet(u"background-color: rgb(0, 0, 0);")
self.horizontalLayout_2 = QHBoxLayout(self.frame)
self.horizontalLayout_2.setObjectName(u"horizontalLayout_2")
self.horizontalLayoutWidget_2 = QWidget(self.centralwidget)
self.horizontalLayoutWidget_2.setObjectName(u"horizontalLayoutWidget_2")
self.horizontalLayoutWidget_2.setGeometry(QRect(60, 110, 291, 51))
self.horizontalLayout_3 = QHBoxLayout(self.horizontalLayoutWidget_2)
self.horizontalLayout_3.setObjectName(u"horizontalLayout_3")
self.horizontalLayout_3.setContentsMargins(0, 0, 0, 0)
self.capture_btn = QPushButton(self.horizontalLayoutWidget_2)
self.capture_btn.setObjectName(u"capture_btn")
self.horizontalLayout_3.addWidget(self.capture_btn)
self.record_btn = QPushButton(self.horizontalLayoutWidget_2)
self.record_btn.setObjectName(u"record_btn")
self.horizontalLayout_3.addWidget(self.record_btn)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QMenuBar(MainWindow)
self.menubar.setObjectName(u"menubar")
self.menubar.setGeometry(QRect(0, 0, 800, 26))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QStatusBar(MainWindow)
self.statusbar.setObjectName(u"statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QMetaObject.connectSlotsByName(MainWindow)
# setupUi
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
self.label.setText(QCoreApplication.translate("MainWindow", u"Device:", None))
self.connect_btn.setText(QCoreApplication.translate("MainWindow", u"Connect", None))
self.capture_btn.setText(QCoreApplication.translate("MainWindow", u"Capture", None))
self.record_btn.setText(QCoreApplication.translate("MainWindow", u"Record", None))
# retranslateUi
以上代码是
的输出pyside2-uic mainwindow.ui -o MainWindow.py
这里是mainwindow.ui
的内容<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QWidget" name="horizontalLayoutWidget">
<property name="geometry">
<rect>
<x>50</x>
<y>30</y>
<width>298</width>
<height>80</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Device:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="camera_combobox"/>
</item>
<item>
<widget class="QPushButton" name="connect_btn">
<property name="text">
<string>Connect</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QFrame" name="frame">
<property name="geometry">
<rect>
<x>60</x>
<y>210</y>
<width>301</width>
<height>171</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(0, 0, 0);</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2"/>
</widget>
<widget class="QWidget" name="horizontalLayoutWidget_2">
<property name="geometry">
<rect>
<x>60</x>
<y>110</y>
<width>291</width>
<height>51</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QPushButton" name="capture_btn">
<property name="text">
<string>Capture</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="record_btn">
<property name="text">
<string>Record</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>26</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
QCamera 需要一个 QCameraViewfinder,它是一个允许显示 QCamera 捕获的信息的小部件,因此您不能将 QFrame 传递给它。
import sys
from PySide2.QtWidgets import QApplication, QMainWindow
from PySide2.QtMultimedia import QCamera, QCameraInfo
from PySide2.QtMultimediaWidgets import QCameraViewfinder
from MainWindow import Ui_MainWindow
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setupUi(self)
for info in QCameraInfo.availableCameras():
self.camera_combobox.addItem(info.description(), info)
self.connect_btn.clicked.connect(self.connect_btn_clicked)
self.view_finder = QCameraViewfinder()
self.frame.layout().addWidget(self.view_finder)
def connect_btn_clicked(self):
info = self.camera_combobox.currentData()
self.camera = QCamera(info)
self.camera.setViewfinder(self.view_finder)
self.camera.start()
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()