Advertisement
Guest User

Untitled

a guest
Mar 20th, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.18 KB | None | 0 0
  1. #!/usr/bin/python3
  2. import wiringpi2 as wiringpi
  3. import gi
  4. gi.require_version('Gst', '1.0')
  5. from gi.repository import GObject, Gst, Gtk, Gdk
  6.  
  7. # Needed for window.get_xid(), xvimagesink.set_window_handle(), respectively:
  8. from gi.repository import GdkX11, GstVideo
  9.  
  10.  
  11. GObject.threads_init()
  12. Gst.init(None)
  13.  
  14. window_width = 800
  15. window_height = 480
  16.  
  17. video_width = 320
  18. video_height = 240
  19.  
  20. class Webcam:
  21.     def __init__(self):
  22.    
  23.         wiringpi.wiringPiSetup()  #enable pins
  24.         wiringpi.pinMode(7, 1) # sets GPIO 7 to output  
  25.         wiringpi.pinMode(8, 1) # sets GPIO 8 to output  
  26.         wiringpi.pinMode(9, 1) # sets GPIO 9 to output
  27.        
  28.         self.window = Gtk.Window()
  29.         self.window.set_title("Hello")
  30.  
  31.         self.window.connect('destroy', self.quit)
  32.         self.window.set_default_size(window_width, window_height)
  33.  
  34.         self.geometry = Gdk.Geometry()
  35.         self.geometry.max_width = window_width
  36.         self.geometry.max_height = window_height
  37.         self.geometry.min_width = 0
  38.         self.geometry.min_height = 0
  39.        
  40.         self.hints = Gdk.WindowHints(Gdk.WindowHints.MAX_SIZE)
  41.        
  42.         self.window.set_geometry_hints(self.window,
  43.                                        self.geometry,
  44.                                        self.hints)
  45.                                        
  46.         self.window.set_position(Gtk.WindowPosition.CENTER)
  47.         self.window.fullscreen()
  48.  
  49.         self.drawingarea = Gtk.DrawingArea()
  50.         #self.drawingarea.set_size_request(video_width, video_height)
  51.        
  52.        
  53.        
  54.         grid = Gtk.Grid()
  55.         self.window.add(grid)
  56.  
  57.         button1 = Gtk.Button(label="LED on")
  58.         button1.connect("clicked", self.button1_on_click)
  59.        
  60.         button2 = Gtk.Button(label="LED off")
  61.         button2.connect("clicked", self.button2_on_click)
  62.        
  63.         button3 = Gtk.Button(label="Button 3")
  64.        
  65.        
  66.        
  67.         button4 = Gtk.Button(label="Button 4")
  68.         button5 = Gtk.Button(label="Button 5")
  69.         button6 = Gtk.Button(label="Button 6")
  70.  
  71.         #grid.add(button1)
  72.         grid.attach(button1, 0, 0, 1, 2)
  73.         grid.attach(button2, 0, 2, 2, 2)
  74.         grid.attach(button3, 0, 4, 2, 2)
  75.         grid.attach(button4, 0, 6, 2, 2)
  76.         grid.attach(button5, 0, 8, 2, 2)
  77.        
  78.         grid.attach_next_to(self.drawingarea, button3, Gtk.PositionType.RIGHT, 1, 2)
  79.         self.drawingarea.set_size_request(video_width, video_height)
  80.         #grid.attach_next_to(button4, button3, Gtk.PositionType.RIGHT, 2, 1)
  81.         #grid.attach(self.drawingarea, 1, 2, 1, 1)
  82.         #grid.attach_next_to(button6, button5, Gtk.PositionType.RIGHT, 1, 1)
  83.        
  84.  
  85.         # Create GStreamer pipeline
  86.         #self.pipeline = Gst.Pipeline()
  87.        
  88.         video_pipeline = 'rpicamsrc bitrate=1000000 preview=false ! video/x-h264,width=' + str(video_width) + ',height=' + str(video_height) +' ! decodebin ! queue ! videoconvert ! ximagesink'
  89.         #video_pipeline = 'v4l2src ! autovideosink'
  90.         #video_pipeline = 'rpicamsrc bitrate=1000000 ! decodebin ! queue ! videoconvert ! autovideosink'
  91.  
  92.         print ('pipeline :' + video_pipeline)
  93.         self.pipeline = Gst.parse_launch(video_pipeline)
  94.  
  95.         # Create bus to get events from GStreamer pipeline
  96.         self.bus = self.pipeline.get_bus()
  97.         self.bus.add_signal_watch()
  98.         self.bus.connect('message::error', self.on_error)
  99.  
  100.         # This is needed to make the video output in our DrawingArea:
  101.         self.bus.enable_sync_message_emission()
  102.         self.bus.connect('sync-message::element', self.on_sync_message)
  103.  
  104.     def button1_on_click(self, button):
  105.         wiringpi.digitalWrite(7, 1)
  106.  
  107.            
  108.     def button2_on_click(self, button):
  109.         wiringpi.digitalWrite(7, 0)
  110.        
  111.        
  112.     def run(self):
  113.         self.window.show_all()
  114.         # You need to get the XID after window.show_all().  You shouldn't get it
  115.         # in the on_sync_message() handler because threading issues will cause
  116.         # segfaults there.
  117.         self.xid = self.drawingarea.get_property('window').get_xid()
  118.         print (self.xid)
  119.         self.pipeline.set_state(Gst.State.PLAYING)
  120.         #Gdk.threads_init()
  121.         Gtk.main()
  122.  
  123.     def quit(self, window):
  124.         self.pipeline.set_state(Gst.State.NULL)
  125.         Gtk.main_quit()
  126.  
  127.     def on_sync_message(self, bus, msg):
  128.  
  129.         if msg.get_structure().get_name() == 'prepare-window-handle':
  130.             print('prepare-window-handle')
  131.             msg.src.set_property('force-aspect-ratio', True)
  132.             msg.src.set_window_handle(self.xid)
  133.  
  134.     def on_error(self, bus, msg):
  135.         print('on_error():', msg.parse_error())
  136.  
  137.  
  138. webcam = Webcam()
  139. webcam.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement