Skip to content

Instantly share code, notes, and snippets.

@tito
Last active January 28, 2017 20:29
Show Gist options
  • Save tito/c6f9fb3ed286196f148d112791a42a33 to your computer and use it in GitHub Desktop.
Save tito/c6f9fb3ed286196f148d112791a42a33 to your computer and use it in GitHub Desktop.
Camera using pygst with custom source
"""
UNTESTED !!
camera_options is a dictionnary that is passed to Camera core class directly
so we can use it to pass custom option, such as video_src for pygst (if it's selected)
camera = CustomCamera(camera_options={
"video_src": "udpsrc port=5000 ! application/x-rtp, encoding-name=JPEG,payload=26 ! rtpjpegdepay ! jpegdec"})
"""
from kivy.uix.image import Image
from kivy.core.camera import Camera as CoreCamera
from kivy.properties import NumericProperty, ListProperty, \
BooleanProperty
class CustomCamera(Image):
play = BooleanProperty(True)
index = NumericProperty(-1)
resolution = ListProperty([-1, -1])
camera_options = DictProperty({})
def __init__(self, **kwargs):
self._camera = None
super(CustomCamera, self).__init__(**kwargs)
if self.index == -1:
self.index = 0
on_index = self._on_index
fbind = self.fbind
fbind('index', on_index)
fbind('resolution', on_index)
on_index()
def on_tex(self, *l):
self.canvas.ask_update()
def _on_index(self, *largs):
self._camera = None
if self.index < 0:
return
if self.resolution[0] < 0 or self.resolution[1] < 0:
return
self._camera = CoreCamera(index=self.index,
resolution=self.resolution, stopped=True, **self.camera_options)
self._camera.bind(on_load=self._camera_loaded)
if self.play:
self._camera.start()
self._camera.bind(on_texture=self.on_tex)
def _camera_loaded(self, *largs):
self.texture = self._camera.texture
self.texture_size = list(self.texture.size)
def on_play(self, instance, value):
if not self._camera:
return
if value:
self._camera.start()
else:
self._camera.stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment