Gstreamer摄像头视频输入在tkinter

时间:2012-09-25 08:26:00

标签: python tkinter gstreamer

我正在编写一个程序来从网络摄像头获取视频,并将其显示在Tkinter窗口中。我编写了以下代码,我在Ubuntu 12.04上运行。

#!/usr/bin/env python

import sys, os, gobject
from Tkinter import *
import pygst
pygst.require("0.10")
import gst

# Goto GUI Class
class Prototype(Frame):
    def __init__(self, parent):
        gobject.threads_init()
        Frame.__init__(self, parent)    

        # Parent Object
        self.parent = parent
        self.parent.title("WebCam")
        self.parent.geometry("640x560+0+0")
        self.parent.resizable(width=FALSE, height=FALSE)

        # Video Box
        self.movie_window = Canvas(self, width=640, height=480, bg="black")
        self.movie_window.pack(side=TOP, expand=YES, fill=BOTH)

        # Buttons Box
        self.ButtonBox = Frame(self, relief=RAISED, borderwidth=1)
        self.ButtonBox.pack(side=BOTTOM, expand=YES, fill=BOTH)

        self.closeButton = Button(self.ButtonBox, text="Close", command=self.quit)
        self.closeButton.pack(side=RIGHT, padx=5, pady=5)

        gotoButton = Button(self.ButtonBox, text="Start", command=self.start_stop)
        gotoButton.pack(side=RIGHT, padx=5, pady=5)

        # Set up the gstreamer pipeline
        self.player = gst.parse_launch ("v4l2src ! video/x-raw-yuv,width=640,height=480 ! ffmpegcolorspace ! xvimagesink")

        bus = self.player.get_bus()
        bus.add_signal_watch()
        bus.enable_sync_message_emission()
        bus.connect("message", self.on_message)
        bus.connect("sync-message::element", self.on_sync_message)

    def start_stop(self):
        if self.gotoButton["text"] == "Start":
            self.gotoButton["text"] = "Stop"
            self.player.set_state(gst.STATE_PLAYING)
        else:
            self.player.set_state(gst.STATE_NULL)
            self.gotoButton["text"] = "Start"

    def on_message(self, bus, message):
        t = message.type
        if t == gst.MESSAGE_EOS:
            self.player.set_state(gst.STATE_NULL)
            self.button.set_label("Start")
        elif t == gst.MESSAGE_ERROR:
            err, debug = message.parse_error()
            print "Error: %s" % err, debug
            self.player.set_state(gst.STATE_NULL)
            self.button.set_label("Start")

    def on_sync_message(self, bus, message):
        if message.structure is None:
            return
        message_name = message.structure.get_name()
        if message_name == "prepare-xwindow-id":
            # Assign the viewport
            imagesink = message.src
            imagesink.set_property("force-aspect-ratio", True)
            imagesink.set_xwindow_id(self.movie_window.window.xid)

def main():
    root = Tk()
    app = Prototype(root)
    app.pack(expand=YES, fill=BOTH)
    root.mainloop()  


if __name__ == '__main__':
     main()

程序运行时,我的问题既不是ButtonBox也不是输出窗口中的VideoBox显示。我怎样才能解决这个问题?我确实在其他网站上查找了可能的解决方案(例如http://pygstdocs.berlios.de/#projectsWay to play video files in Tkinter?),但是他们的代码含义非常有限。

在进行建议的更改和其他几个按钮以使按钮工作后,我意识到当我运行程序时,显示窗口与主窗口不同。有没有办法在使用tkinter时将视频显示在主窗口中?

2 个答案:

答案 0 :(得分:2)

看起来你的Prototype类是一个Tkinter框架,但你似乎没有把它打包/放在任何地方。

...
app = Prototype(root)
app.pack(expand=YES, fill=BOTH)
root.mainloop()

答案 1 :(得分:1)

我终于找到了问题的解决方案。我意识到错误是在线              imagesink.set_xwindow_id(self.movi​​e_window.window.xid)

我改为              imagesink.set_xwindow_id(self.movi​​e_window.winfo_id())

错误在于我使用了window.xid,它是gtk小部件的一个属性。在tkinter中,winfo_id()返回tkinter小部件的窗口标识符。有关详细信息http://effbot.org/tkinterbook/widget.htm#Tkinter.Widget.winfo_id-method