Skip to content

Instantly share code, notes, and snippets.

@snegovick snegovick/gtkgst.py
Last active Jan 23, 2018

Embed
What would you like to do?
GTK widget to show gstreamer captured video
#!/usr/bin/env python
# Copyright (C) 2014 by Kirik Konstantin <snegovick>
try:
import gtk
import gobject
from gtk import gdk
except:
raise SystemExit
import pygtk
if gtk.pygtk_version < (2, 0):
print "PyGtk 2.0 or later required for this widget"
raise SystemExit
import Image
import cairo
import camera_source
from os.path import exists, relpath
from sys import exit
class GtkGst(gtk.DrawingArea):
__gsignals__ = { "expose-event": "override", "unrealize": "override" }
def __init__(self, device, resolution):
gtk.DrawingArea.__init__(self)
self.connect("expose-event", self.do_expose_event)
self.connect("unrealize", self.do_unrealize)
self.resolution = resolution
if not exists(device):
print device, 'not detected. Fall back to default camera (/dev/video0)'
self.device = '/dev/video0'
if not exists(self.device):
print "No webcam detected: /dev/video0 cannot be found.\n The program is now exiting."
exit()
else:
self.device = device # device used for video input
self.set_size_request(resolution[0], resolution[1])
self.cs = camera_source.camerasrc(resolution[0], resolution[1], self.device)
self.stopped = False
def do_expose_event(self, widget, event):
if self.stopped == False:
self.img = self.cs.get_image()
self.context = widget.window.cairo_create()
self.context.rectangle(event.area.x, event.area.y,
event.area.width, event.area.height)
self.context.clip()
self.img.putalpha(255) # create alpha channel
arr = numpy.array(self.img)
height, width, channels = arr.shape
surface = cairo.ImageSurface.create_for_data(arr, cairo.FORMAT_RGB24, width, height)
self.context.set_source_surface(surface, 0, 0)
self.context.paint()
gobject.timeout_add(100, self.queue_draw)
return False
def do_unrealize(self, arg):
self.cs.stop()
def take_snapshot(self):
print "img:", self.img
self.cs.stop()
self.stopped = True
return self.img
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.