gpt4 book ai didi

python - 视频播放速度慢 - Python 3.6、OpenCV 3、PyQT 5

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

我正在使用 Python 3.6、OpenCV3 和 PyQT5 在 Windows 中编写一个专门的视频播放器。但是,播放以每秒 30.0 帧录制的视频文件(从我的笔记本电脑网络摄像头录制)时,播放速度很慢。

我将我的应用程序剥离到播放视频的最基本部分。然后,我将应用程序中的回放循环从每帧 15 毫秒更改为每帧 34 毫秒,并记录每个循环时间。接下来,我计算了在每个循环速度下播放 232 帧的平均循环时间。结果令人费解。对于 15 毫秒到 19 毫秒的循环,应用程序的行为符合预期,但当应用程序指定循环时间为 20 毫秒到 31 毫秒时,该应用程序的行为稳定为 31.2 毫秒。然后,当应用程序指定的循环速度低于 31 毫秒时,循环时间再次跳跃。

Application = 15msec,   Average Result = 15.0msec,   Difference = 0.0msec  
Application = 16msec, Average Result = 16.0msec, Difference = 0.0msec
Application = 17msec, Average Result = 17.0msec, Difference = 0.0msec
Application = 18msec, Average Result = 18.0msec, Difference = 0.0msec
Application = 19msec, Average Result = 19.0msec, Difference = 0.0msec
Application = 20msec, Average Result = 31.2msec, Difference = 11.2msec
Application = 21msec, Average Result = 31.2msec, Difference = 10.2msec
Application = 22msec, Average Result = 31.2msec, Difference = 9.2msec
Application = 23msec, Average Result = 31.2msec, Difference = 8.2msec
Application = 24msec, Average Result = 31.2msec, Difference = 7.2msec
Application = 25msec, Average Result = 31.2msec, Difference = 6.2msec
Application = 26msec, Average Result = 31.2msec, Difference = 5.2msec
Application = 27msec, Average Result = 31.2msec, Difference = 4.2msec
Application = 28msec, Average Result = 31.2msec, Difference = 3.2msec
Application = 29msec, Average Result = 31.2msec, Difference = 2.2msec
Application = 30msec, Average Result = 31.2msec, Difference = 1.2msec
Application = 31msec, Average Result = 31.2msec, Difference = 0.2msec
Application = 32msec, Average Result = 39.1msec, Difference = 7.1msec
Application = 33msec, Average Result = 46.8msec, Difference = 13.8msec
Application = 34msec, Average Result = 46.8msec, Difference = 12.8msec

我还计算了执行 nextFrameSlot(self) 方法所需的时间。执行平均需要 6 毫秒,因此它不会对循环造成任何延迟。

我希望播放速度为真实速率,应该是 1/(帧速率)。

有没有人对发生这种情况有任何建议?这是代码。 (我没有包含 pydesigner 创建的 GUI 代码)。

from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtCore import QTimer
from time import time
import sys
import cv2
import vidtest # GUI Module created by pydesigner

class VideoCapture(QMainWindow, vidtest.Ui_MainWindow):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self) # This is defined in design.py file automatically
self.btnLoadFile.clicked.connect(self.loadVideoFile)
self.btnStartPlay.clicked.connect(self.start)
self.btnStop.clicked.connect(self.closeApplication)
self.durations = []

def nextFrameSlot(self):
ret, frame = self.cap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
x = frame.shape[1]
y = frame.shape[0]
img = QImage(frame, x, y, QImage.Format_RGB888)
pix = QPixmap.fromImage(img)
self.vidWindow.setPixmap(pix)
a = time() # Used to record loop time
self.durations.append(a) # Used to record loop time

def start(self):
self.timer = QTimer()
print("Rate = ", self.vid_rate)
self.timer.timeout.connect(self.nextFrameSlot)
self.timer.start(self.vid_rate)

def loadVideoFile(self):
self.videoFileName = "stopwatch.avi"
self.cap = cv2.VideoCapture(str(self.videoFileName))
self.frame_rate = self.cap.get(cv2.CAP_PROP_FPS)
self.vid_rate = 34 # reset this integer from 15 through 34
self.nextFrameSlot()

def closeApplication(self):
for i in self.durations:
print (i)
self.cap.release()
sys.exit(0)

def main():
app = QApplication(sys.argv)
form = VideoCapture()
form.show()
app.exec_()

if __name__ == '__main__':
main()

最佳答案

谢谢 user3419537!你的建议奏效了。

我需要在函数 start(self) )。默认情况下,QTimer() 使用粗定时器。

关于python - 视频播放速度慢 - Python 3.6、OpenCV 3、PyQT 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43845642/

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