尝试使用 PySide2 显示网络摄像头时没有可用的取景器 [英] No viewfinder available while trying to display webcam with PySide2

查看:55
本文介绍了尝试使用 PySide2 显示网络摄像头时没有可用的取景器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python3 和 PySide2 开发 Ubuntu 20.04,Qt 是版本 5.

I am working on Ubuntu 20.04 with Python3 and PySide2, Qt is version 5.

我编写了这个简单的 Python 脚本是为了测试保存网络摄像头视频的能力,以便我稍后会处理一个更大的项目.

I wrote this simple Python script in order to test the ability to save webcam video for a larger project I will later work on.

基本上,我首先想要这个演示脚本,并在我命名为frame"的小部件中简单地显示来自相机的流,但似乎我遗漏了一些东西,因为在终端中打印了一条消息:

Basically, I would like that this demo script at first and simply displays the stream coming from the camera, in a widget I named "frame", but it seems I am missing something, because a message gets printed in the terminal:

在没有取景器的情况下启动相机

Starting camera without viewfinder available

我在 QtCreator 中制作了 GUI,然后使用 pyside-uic 进行了转换.

I made the GUI within QtCreator, then converted it with pyside-uic.

我为名为frame"的小部件应用了黑色背景色.当我点击连接"时按钮我希望看到视频开始和显示,但框架"没有显示.保持黑色,我收到有关终端取景器的消息.取景器不是显示视频的地方(又名小部件)吗?

I applied a black background-color to the widget called "frame". When I click the "Connect" button I would expect to see the video starting and displaying, but the "frame" keeps black and I get the message about the viewfinder in the terminal. Isn't it the viewfinder the place (aka widget ) where the video is displayed?

我觉得有问题,但我认为它可以轻松解决,因为当我单击按钮进行连接时,网络摄像头的 LED(正确)亮起.

I feel something is wrong, but I think it could be solved easily, because when I click the button to connect, the webcam's LED (correctly) lights up.

这是python代码:

Here it is the python code:

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 代码

Here it is the MainWindow.py code

# -*- 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的内容

Here is the content of 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 传递给它.

QCamera needs a QCameraViewfinder which is a widget that allows to display the information captured by QCamera, so you cannot pass a QFrame to it.

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_()

这篇关于尝试使用 PySide2 显示网络摄像头时没有可用的取景器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆