gpt4 book ai didi

python - 尝试在 QGraphicsScene 和 QGraphicsView 中显示 opencv 视频,但没有显示任何内容

转载 作者:太空宇宙 更新时间:2023-11-03 22:51:15 26 4
gpt4 key购买 nike

我有一个用 Python 编写的 GUI 应用程序,我正尝试在 Qt 应用程序中显示视频。

它使用 QGraphicsScene 和 QGraphicsView 来显示图像,但现在我必须显示视频。当我尝试在此处显示视频时,我会这样做:

首先,我从 cv2 包创建 VideoCapture。之后,我在每次迭代中运行循环并读取帧,然后将其转换为 QPixmap(这是正确完成的,我检查了单个帧)。我将该 QPixmap 对象返回到包含 QGraphicsScene 和 QGraphicsView 的类,并尝试将其添加到场景中。

事情是,只有当视频结束时,才会显示最后一帧,在整个视频播放期间,我很高兴看到这一幕

A blank non responsive screen

我之前提到的循环看起来像这样:

    self.imageViewer.start_capturing()
self.timer = QtCore.QTimer()
self.fps = 24

while True:
retVal = self.imageViewer.read_frame()
if not retVal:
self.timer.stop()
break
self.timer.start(1000./self.fps)
self.imageViewer.stop_capturing()

self.ImageViewer 是一个包含 QGraphicsScene 和 QGraphicsView 的组件我还在此处放置了计时器,以便它显示适当的 fps 数量,但这没有帮助。

GUI 应用本身显示带有一些按钮的 QGraphicsView

middleLayout.addWidget(self.imageViewer.view)

这就是我的意思。所以它不显示 imageViewer 但它显示 QGraphicsView

的子类

这是 ImageViewer 类中 read_frame 方法的代码。

def read_frame(self):
"""
Reads a frame from the video, if video ended, then False is returned.
If there is a successful reading then True is returned
:return: True if video is read successfully, False otherwise.
"""
retVal, self.current_frame = self.capture.getFrame()
if not retVal:
return False
else:
self.pixmap = retVal
self.scene.addPixmap(self.pixmap)
return True

self.capture.getFrame() 方法只返回 QPixmap 项和 CV::mat 项。

整个过程是正确完成的,因为我尝试逐帧手动读取并且一切正常,但是当我尝试显示视频时,应用程序卡住,如上图所示。所以我手动尝试通过手动单击一个按钮来完成整个描述的过程,该按钮加载我的框架并将其放到 QGraphicsScene 上,所以我假设应用程序的核心工作正常(我什至尝试通过单击获得大约 5-7 fps快:D)

我希望我能清楚地说明我的问题并包括所有需要的代码。

最佳答案

我认为你的计时逻辑是错误的..你没有正确使用计时器..你可以使用 connect或者你可以使用 sleep 。

我会做这样的事情:

def grabFrame
retVal = self.imageViewer.read_frame()
if not retVal:
self.timer.stop()
self.imageViewer.stop_capturing()

在你的主要逻辑或类的一些初始化函数中的某个地方:

yourObject.connect(timer,SIGNAL("timeout()"),yourObject,SLOT("grabFrame()"))
timer.start(1000./self.fps)

或者你可以在之后使用某种 sleep 时间模块

import time
...
while True:
retVal = self.imageViewer.read_frame()
if not retVal:
break
time.sleep(1./self.fps)//in fraction of second
self.imageViewer.stop_capturing()

关于python - 尝试在 QGraphicsScene 和 QGraphicsView 中显示 opencv 视频,但没有显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37263218/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com