使用 PySide 在 QLabel 中显示视频流 [英] Displaying a video stream in QLabel with PySide

查看:246
本文介绍了使用 PySide 在 QLabel 中显示视频流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能为我指出如何在 PySide 中创建新的 QMovie提供程序"的正确方向?

Can anybody point me in the right direction on how to create a new QMovie "provider" in PySide?

我有一个视频流,我想尽可能简单地显示(没有音频,只有帧序列未知且帧速率可变).这个例子似乎很完美,除了我的视频来自非常规来源.它不是文件,而是格式不规范的网络流.我可以轻松编写接收每一帧的代码,我的想法是创建一个QMovie 提供程序",以便我可以像上面的示例一样在标签上显示此流.

I have a video stream that I want to display as simply as possible (no audio, just a sequence of frames with an unknown and variable framerate). This example seems perfect except that my video is coming from an unconventional source. It's not a file but a network stream in a format that is not standardized. I can easily write code that receives each frame and my idea is to create a "QMovie provider" so that I can just display this stream on a label like in the example above.

我的第一个想法是子类化 QMovie 并覆盖那里的一些函数,但在阅读 文档 因为我不知道我应该如何处理我的实例将从中读取的设备".

My first thought was to just subclass QMovie and overwrite a few functions there but I started having second thoughts about that when reading the documentation since I don't know what I should do about the "device" my instance would be reading from.

我在上述文档中注意到 QMovie 使用 QImageReader,所以我的下一个想法是扩展该类并让它从我的流中读取帧.但是,这提出了类似的问题,例如,我应该如何处理supportedImageFormats()"函数?

I noticed in the aforementioned documentation that QMovie uses QImageReader so my next thought was to extend that class and have it read frames from my stream. That poses similar questions however, for example, what should I do with the "supportedImageFormats()" function?

我一直在尝试在每次收到新帧时直接更新 QLabel 上的图像,但后来我收到错误QPixmap:在 GUI 线程之外使用像素图不安全".

I've been experimenting with just directly updating the image on my QLabel every time I receive a new frame but then I've been getting the error "QPixmap: It is not safe to use pixmaps outside the GUI thread".

所以基本上我有点难受,我真的很感激任何关于如何让 QLabel 在 PySide 应用程序中显示我的视频流的指示或教程.

So basically I'm a little stumped and would really appreciate any pointers or tutorials on how to get a QLabel to display my video stream in a PySide application.

推荐答案

为了将来参考,这里是我如何设法使这个工作.

For future reference here's how I managed to get this working.

使用信号和槽机制,下面的应用程序工作.信号/槽机制似乎发现在 up_camera_callback 函数内部创建并发送到 CameraDisplay.updateFrame 函数的图像来自不同的线程并采取了必要的预防措施.

Using the signals and slot mechanism, the following application works. The signal/slot mechanism seems to figure out that the image that gets created inside the up_camera_callback function and emitted to the CameraDisplay.updateFrame function is coming from a different thread and takes the necessary precautions.

class CameraDisplay(QtGui.QLabel):
  def __init__(self):
    super(CameraDisplay, self).__init__()

  def updateFrame(self, image):
    self.setPixmap(QtGui.QPixmap.fromImage(image))

class ControlCenter(QtGui.QWidget):
  up_camera_signal = QtCore.Signal(QtGui.QImage)
  up_camera = None

  def __init__(self):
    super(ControlCenter, self).__init__()
    self.up_camera = CameraDisplay()
    self.up_camera_signal.connect(self.up_camera.updateFrame)

    grid = QtGui.QGridLayout()
    grid.setSpacing(10)

    grid.addWidget(self.up_camera, 0, 0)

    self.setLayout(grid)

    self.setGeometry(300, 300, 350, 300)
    self.setWindowTitle('Control Center')
    self.show()

  def up_camera_callback(self, data):
    '''This function gets called by an external thread'''
    try:
      image = QtGui.QImage(data.data, data.width, data.height, QtGui.QImage.Format_RGB888)
      self.up_camera_signal.emit(image)

    except Exception, e:
      print(e)

if __name__ == "__main__":
  app = QtGui.QApplication(sys.argv)
  ex = ControlCenter()
  sys.exit(app.exec_())

这篇关于使用 PySide 在 QLabel 中显示视频流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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