Skip to content Skip to sidebar Skip to footer

Kivy Camera On Multiple Screen

I'm trying to create a simples app in kivy that have two screen and i need it load the a custom camera in each screen dont need do in same time. I tryed load in gui.kv the cam in m

Solution 1:

I took source code of class Camera

https://github.com/kivy/kivy/blob/master/kivy/uix/camera.py

and created own class MyCamera

I create instance of CoreCamera outside class MyCamera and all instances of MyCamera use the same one instance of CoreCamera

core_camera = CoreCamera(index=0, resolution=(640, 480), stopped=True)

classMyCamera(Image):

    def_on_index(self, *largs):
        # ... 

        self._camera = core_camera

instead of

classMyCamera(Image):

    def_on_index(self, *largs):
        # ... 

        self._camera = CoreCamera(index=self.index, resolution=self.resolution, stopped=True)

It has still some problems

  • in CoreCamera is set resolution and all MyClass use the same resolution - but they still need resolution: (640, 480) in gui.kv (but they don't use this value)
  • play starts/stops animation for all MyClass because CoreCamera control it.

I used example from

https://kivy.org/doc/stable/examples/gen__camera__main__py.html

to create example with two widgets displaying the same camera

# https://kivy.org/doc/stable/examples/gen__camera__main__py.html# https://github.com/kivy/kivy/blob/master/kivy/uix/camera.pyfrom kivy.uix.image import Image
from kivy.core.camera import Camera as CoreCamera
from kivy.properties import NumericProperty, ListProperty, BooleanProperty

# access to camera
core_camera = CoreCamera(index=0, resolution=(640, 480), stopped=True)

# Widget to display cameraclassMyCamera(Image):
    '''Camera class. See module documentation for more information.
    '''

    play = BooleanProperty(True)
    '''Boolean indicating whether the camera is playing or not.
    You can start/stop the camera by setting this property::
        # start the camera playing at creation (default)
        cam = Camera(play=True)
        # create the camera, and start later
        cam = Camera(play=False)
        # and later
        cam.play = True
    :attr:`play` is a :class:`~kivy.properties.BooleanProperty` and defaults to
    True.
    '''

    index = NumericProperty(-1)
    '''Index of the used camera, starting from 0.
    :attr:`index` is a :class:`~kivy.properties.NumericProperty` and defaults
    to -1 to allow auto selection.
    '''

    resolution = ListProperty([-1, -1])
    '''Preferred resolution to use when invoking the camera. If you are using
    [-1, -1], the resolution will be the default one::
        # create a camera object with the best image available
        cam = Camera()
        # create a camera object with an image of 320x240 if possible
        cam = Camera(resolution=(320, 240))
    .. warning::
        Depending on the implementation, the camera may not respect this
        property.
    :attr:`resolution` is a :class:`~kivy.properties.ListProperty` and defaults
    to [-1, -1].
    '''def__init__(self, **kwargs):
        self._camera = Nonesuper(MyCamera, self).__init__(**kwargs)  # `MyCamera` instead of `Camera`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()

    defon_tex(self, *l):
        self.canvas.ask_update()

    def_on_index(self, *largs):
        self._camera = Noneif self.index < 0:
            returnif self.resolution[0] < 0or self.resolution[1] < 0:
            return

        self._camera = core_camera # `core_camera` instead of `CoreCamera(index=self.index, resolution=self.resolution, stopped=True)`

        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)

    defon_play(self, instance, value):
        ifnot self._camera:
            returnif value:
            self._camera.start()
        else:
            self._camera.stop()


from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
import time

Builder.load_string('''
<CameraClick>:
    orientation: 'vertical'
    MyCamera:
        id: camera1
        resolution: (640, 480)
    MyCamera:
        id: camera2
        resolution: (640, 480)
    ToggleButton:
        text: 'Play'
        on_press: camera1.play = not camera1.play
        size_hint_y: None
        height: '48dp'
    Button:
        text: 'Capture'
        size_hint_y: None
        height: '48dp'
        on_press: root.capture()
''')


classCameraClick(BoxLayout):
    defcapture(self):
        '''
        Function to capture the images and give them the names
        according to their captured time and date.
        '''
        camera = self.ids['camera1']
        timestr = time.strftime("%Y%m%d_%H%M%S")
        camera.export_to_png("IMG_{}.png".format(timestr))
        print("Captured")

classTestCamera(App):

    defbuild(self):
        return CameraClick()

TestCamera().run()

enter image description here

Post a Comment for "Kivy Camera On Multiple Screen"