我正在使用 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>