웹 캠에서 비디오 피드를 가져 와서 Tkinter 창에 표시하는 프로그램을 작성 중입니다. 우분투 12.04에서 실행 한 다음 코드를 작성했습니다.Gstreamer 웹캠 비디오 피드 in tkinter
#!/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/#projects 또는 Way to play video files in Tkinter?)이있는 다른 사이트를 살펴 보았지만 코드의 의미에 대한 정보는 매우 제한적이었습니다.
버튼을 작동시키기 위해 제안 된 변경과 몇 가지 다른 작업을 수행 한 후에 프로그램을 실행할 때 디스플레이 창과 기본 창과의 차이점을 알았습니다. tkinter를 사용할 때 비디오를 메인 윈도우에 표시하는 방법이 있습니까 ??
도움을 주셔서 감사합니다. :) – user1696565