Menu

30 August 2011

Wxpython and GST based webcam viewer

Screenshot-video_test_1

Fork @ https://gist.github.com/6111868f00736fd4575c#


#!/usr/bin/env python
# Wxpython Port of Example 8.1 http://pygstdocs.berlios.de/pygst-tutorial/webcam-viewer.html

import wx
import gst

import gobject
gobject.threads_init()


class WxWebCamTest(wx.Frame):
def __init__(self, parent, ID, title):

wx.Frame.__init__(self, parent, ID, title)
self.SetSize((500, 400))
self.Bind(wx.EVT_CLOSE,self.destroy)
vbox = wx.BoxSizer(wx.VERTICAL)
hbox = wx.BoxSizer(wx.HORIZONTAL)

self.button = wx.Button(self,label="Start")
hbox.Add(self.button, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4)
self.button.Bind(wx.EVT_BUTTON, self.start_stop)

vbox.Add(hbox, 0, wx.EXPAND, 0)
self.movie_window = wx.Panel(self)
vbox.Add(self.movie_window,1,wx.ALL|wx.EXPAND,4)
self.SetSizer(vbox)
self.Layout()
self.Show()

self.player = gst.parse_launch ("v4l2src ! autovideosink")
bus = self.player.get_bus()
print 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, event):
if self.button.GetLabel() == "Start":
self.button.SetLabel("Stop")
self.player.set_state(gst.STATE_PLAYING)
else:
self.player.set_state(gst.STATE_NULL)
self.button.SetLabel("Start")

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

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':
imagesink = message.src
imagesink.set_property('force-aspect-ratio', True)
imagesink.set_xwindow_id(self.movie_window.GetHandle())

def destroy(self,event):
#Stop the player pipeline to prevent a X Window System error
self.player.set_state(gst.STATE_NULL)
event.Skip()

if __name__=="__main__":
app = wx.App()
f = WxWebCamTest(None, -1, "Video Test 1")
f.Centre()
f.Show(True)
app.MainLoop()


By- Narendra Sisodiya

No comments: