使用Tkinter(画布)和线程播放视频时出错

时间:2018-11-02 03:17:12

标签: python multithreading opencv tkinter

我正在尝试制作一个GUI,该GUI允许我播放视频并在某个点停止播放以标记一些点并再次播放。

我想用Tkinter和一个读取框架的线程(我将它们放在缓冲区中)和另一个在画布上更新它们的线程来实现它,我的代码是:

import tkinter
import threading
import queue
import cv2
import PIL.Image, PIL.ImageTk

class GuiPart:
    def __init__(self, window, queue, w,h):
        self.queue = queue
        self.stopped=False

        self.canvas = tkinter.Canvas(window, width = w, height = h)
        self.canvas.pack()

        self.backgroudThread=threading.Thread(target=self.processIncomingFrames)
        self.backgroudThread.start()

    def processIncomingFrames(self):
        while True:
            try:
                frame = self.queue.get()
                frameImage = PIL.ImageTk.PhotoImage(image = PIL.Image.fromarray(frame))
                self.canvas.create_image(0, 0, image = frameImage, anchor = tkinter.NW)
            except queue.Empty:
                pass

class AppManager:

    def __init__(self, window, video=0):
        self.window = window
        self.video = video

        self.capFile = cv2.VideoCapture(self.video)
        self.fps = self.capFile.get(cv2.CAP_PROP_FPS)
        self.widthVideo = self.capFile.get(cv2.CAP_PROP_FRAME_WIDTH)
        self.heightVideo = self.capFile.get(cv2.CAP_PROP_FRAME_HEIGHT)

        self.queue = queue.Queue(20)
        self.gui = GuiPart(window, self.queue, self.widthVideo, self.heightVideo)

        self.running=True

        self.readFileThread = threading.Thread(target=self.readFileThread)
        self.readFileThread.start()

    def readFileThread(self):
        while self.running:
                ret, frame = self.capFile.read()
                if not ret:
                    self.running = False
                while self.running:
                    try:
                        self.queue.put(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), timeout=1)
                        break
                    except queue.Full:
                        pass

    def endApplication(self):
        self.running = False

window = tkinter.Tk()

manager = AppManager(window,"videoplayback.mp4")
window.mainloop()

问题在于它可以很好地重现视频(目前我对FPS并不感兴趣,我知道如何对其进行校正),但是每隔一定数量的帧都会出现初始帧,就好像它已加载到内存中一样并出现了。如有必要,我可以录制屏幕视频,但是在此link中,我留下.gif以了解我的意思。

您知道什么可能导致这种情况发生吗?

已更新

为解决该问题,我在processIncomingFrames()函数中编写了以下代码:

def processIncomingFrames(self):
        while True:
            try:
                frame = self.queue.get()
                frameImage = PIL.ImageTk.PhotoImage(image = PIL.Image.fromarray(frame))
                if self.idFrame is None:
                    self.idFrame = self.canvas.create_image(0, 0, image = frameImage, anchor = tkinter.NW)
                else:
                    self.canvas.itemconfig(self.idFrame, image = frameImage)
                    self.canvas.image=frameImage
            except queue.Empty:
                pass

0 个答案:

没有答案