在标签中显示视频

时间:2015-07-15 14:46:04

标签: python opencv tkinter raspberry-pi

我目前正在开展一个项目,其中包括在gui中显示视频。 该软件运行在raspberry pi上,用python编写。我正在使用tkinter作为gui。

当我运行播放视频功能时,只显示最后一帧。 首先我认为树莓是要慢,但也是在插入睡眠后(3)没有任何改变。

有没有人看到我的错误?

    def play_video():
        cap = cv2.VideoCapture('video.h264')
        ret = True
        while(ret):
            ret, frame = cap.read()
            time.sleep(3)
            if ret:
                print "in"
                img = Image.fromarray(frame)
                imgtk = ImageTk.PhotoImage(image=img)
                show_video_label .imgtk = imgtk
                show_video_label .configure(image=imgtk)
                show_video_label._image_cache = imgtk 

        cap.release()

非常感谢!!!

1 个答案:

答案 0 :(得分:0)

您不能在UI应用程序中使用这样的while循环,因为它会阻止处理系统事件。您必须保持mainloop处理事件才能继续处于活动状态。

在这种情况下,你应该创建一个捕获并显示单个帧然后在'start_video'函数中的函数,使用after方法来调度捕获帧的调用,每次捕获帧时使用after再次安排下一次捕获。如果保留stop_video调用返回的令牌,则after方法可以取消最后一个after