Exemple #1
3
class TestApp(App):
    
    def build(self):
        print("Building self")

        self.camera = Camera(resolution=(640,480))
        self.camera.play=True
        self.camera.bind(on_texture=self.reloadTexture)

        self.tex = self.camera.texture

        self.canvas = RenderContext()
        self.canvas.add(Rectangle(texure=self.tex))

        self.captureButton = Button(text='Capture')
        self.captureButton.bind(on_press=self.capture)
        self.captureButton.height='48dp'

        layout = BoxLayout(orientation='vertical')
        #layout.add_widget(self.canvas)
        layout.add_widget(self.captureButton)
        
        return layout

    def reloadTexture(self, *largs):
        self.tex = self.camera.texture
        self.canvas.ask_update()

    def capture(self, event):
        print("catpured")
        self.tex = self.camera.texture
        img = Image(texture=self.tex)
        img.save("capture.png")
Exemple #2
0
class TestApp(App):
    def build(self):
        print("Building self")

        self.camera = Camera(resolution=(640, 480))
        self.camera.play = True
        self.camera.bind(on_texture=self.reloadTexture)

        self.tex = self.camera.texture

        self.canvas = RenderContext()
        self.canvas.add(Rectangle(texure=self.tex))

        self.captureButton = Button(text='Capture')
        self.captureButton.bind(on_press=self.capture)
        self.captureButton.height = '48dp'

        layout = BoxLayout(orientation='vertical')
        #layout.add_widget(self.canvas)
        layout.add_widget(self.captureButton)

        return layout

    def reloadTexture(self, *largs):
        self.tex = self.camera.texture
        self.canvas.ask_update()

    def capture(self, event):
        print("catpured")
        self.tex = self.camera.texture
        img = Image(texture=self.tex)
        img.save("capture.png")
Exemple #3
0
 def _on_enter(self, instance_program):
     self.app = instance_program
     # print('enter')
     if self.camera is None:
         self.camera = Camera()
     self.camera.play = True
     self.camera.start()
     self.clear_images()
     self.ids.first.texture = None
     self.ids.second.texture = None
Exemple #4
0
 def _on_index(self, *largs):
     self._camera = None
     if self.index < 0:
         return
     if self.resolution[0] < 0 or self.resolution[1] < 0:
         self._camera = CoreCamera(index=self.index, stopped=True)
     else:
         self._camera = CoreCamera(index=self.index,
                                   resolution=self.resolution,
                                   stopped=True)
     if self.play:
         self._camera.start()
         self._camera.bind(on_texture=self.on_tex)
Exemple #5
0
 def _on_index(self, *largs):
     self._camera = None
     if self.index < 0:
         return
     self._camera = CoreCamera(index=self.index,
         resolution=self.resolution, stopped=True)
     self._camera.bind(on_load=self._camera_loaded)
     if self.play:
         self._camera.start()
Exemple #6
0
 def _on_index(self, *largs):
     self._camera = None
     if self.index < 0:
         return
     self._camera = CoreCamera(index=self.index,
                               resolution=self.resolution,
                               stopped=True)
     self._camera.bind(on_load=self._camera_loaded)
     if self.play:
         self._camera.start()
 def reconfigure_camera(self):
     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, port=self.port, stopped=True)
     self._camera.bind(on_load=self._camera_loaded)
     if self.play:
         self._camera.start()
         self._camera.bind(on_texture=self.on_tex)
Exemple #8
0
class StartWorker(Screen, Utils):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def _on_enter(self, instance_program):
        self.app = instance_program
        # print('enter')
        if self.camera is None:
            self.camera = Camera()
        self.camera.play = True
        self.camera.start()
        self.clear_images()
        self.ids.first.texture = None
        self.ids.second.texture = None

    def _on_exit(self):
        # print('exit')
        # camera = self.ids['camera']
        self.camera.play = False
        self.camera.stop()
Exemple #9
0
    def build(self):
        print("Building self")

        self.camera = Camera(resolution=(640, 480))
        self.camera.play = True
        self.camera.bind(on_texture=self.reloadTexture)

        self.tex = self.camera.texture

        self.canvas = RenderContext()
        self.canvas.add(Rectangle(texure=self.tex))

        self.captureButton = Button(text='Capture')
        self.captureButton.bind(on_press=self.capture)
        self.captureButton.height = '48dp'

        layout = BoxLayout(orientation='vertical')
        #layout.add_widget(self.canvas)
        layout.add_widget(self.captureButton)

        return layout
 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,zoom=self.zoom, focusmode=self.focusmode)  # edit by ableity
     self._camera.bind(on_load=self._camera_loaded)
     if self.play:
         self._camera.start()
         self._camera.bind(on_texture=self.on_tex)
Exemple #11
0
 def _on_index(self, *largs):
     self._camera = None
     if self.index < 0:
         return
     if self.resolution[0] < 0 or self.resolution[1] < 0:
         print('No default resolution')
         self._camera = CoreCamera(index=self.index, stopped=True)
     else:
         print('Getting global camera')
         self._camera = camera
     self._camera.bind(on_load=self._camera_loaded)
     if self.play:
         self._camera.start()
         self._camera.bind(on_texture=self.on_tex)
Exemple #12
0
class CameraBundle:
    def __init__(self):
        self.cam = CoreCamera(index=0, resolution=(640, 480))
        self.caminstance = self.cam._android_camera

    def start(self):
        self.cam.start()

    def stop(self):
        self.cam.stop()

    def fps(self):
        return self.cam.fps

    def capture(self, n):
        frames = []
        st = 1.0 / 29.97

        self._flash_on()

        for i in range(0, n):
            frames.append(self.cam.grab_frame())
            sleep(st)

        self._flash_off()

        return frames

    def decode(self, frames):
        r = np.zeros((1, len(frames)))
        for i in range(0, len(frames)):
            frame = self.cam.decode_frame(frames[i])
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            r[0, i] = np.mean(frame[180:260:5, 270:340:5])
            print r[0, i]
        return r

    def _flash_on(self):
        p = self.caminstance.getParameters()
        p.setFlashMode(Parameters.FLASH_MODE_TORCH)
        p.setExposureCompensation(-2)
        self.caminstance.setParameters(p)

    def _flash_off(self):
        p = self.caminstance.getParameters()
        p.setFlashMode(Parameters.FLASH_MODE_OFF)
        p.setExposureCompensation(0)
        self.caminstance.setParameters(p)
Exemple #13
0
    def build(self):
        print("Building self")

        self.camera = Camera(resolution=(640,480))
        self.camera.play=True
        self.camera.bind(on_texture=self.reloadTexture)

        self.tex = self.camera.texture

        self.canvas = RenderContext()
        self.canvas.add(Rectangle(texure=self.tex))

        self.captureButton = Button(text='Capture')
        self.captureButton.bind(on_press=self.capture)
        self.captureButton.height='48dp'

        layout = BoxLayout(orientation='vertical')
        #layout.add_widget(self.canvas)
        layout.add_widget(self.captureButton)
        
        return layout
Exemple #14
0
class Camera(Image):
    '''Camera class. See module documentation for more information.
    '''

    play = BooleanProperty(True)
    '''Boolean indicate if the camera is playing.
    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

    :data:`play` is a :class:`~kivy.properties.BooleanProperty`, default to
    True.
    '''

    index = NumericProperty(-1)
    '''Index of the used camera, starting from 0.

    :data:`index` is a :class:`~kivy.properties.NumericProperty`, default to -1
    to allow auto selection.
    '''

    resolution = ListProperty([-1, -1])
    '''Prefered 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 of the implementation, the camera may not respect this
        property.

    :data:`resolution` is a :class:`~kivy.properties.ListProperty`, default to
    [-1, -1]
    '''

    def __init__(self, **kwargs):
        self._camera = None
        super(Camera, self).__init__(**kwargs)
        if self.index == -1:
            self.index = 0
        self.bind(index=self._on_index,
                  resolution=self._on_index)
        self._on_index()

    def _on_index(self, *largs):
        self._camera = None
        if self.index < 0:
            return
        self._camera = CoreCamera(index=self.index,
            resolution=self.resolution, stopped=True)
        self._camera.bind(on_load=self._camera_loaded)
        if self.play:
            self._camera.start()

    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()
Exemple #15
0
class Camera(Image):
    '''Camera class. See module documentation for more informations.
    '''

    play = BooleanProperty(False)
    '''Boolean indicate if the camera is playing.
    You can start/stop the camera by setting this property. ::

        # start the camera playing at creation
        video = Camera(source='movie.mkv', play=True)

        # create the camera, and start later
        video = Camera(source='movie.mkv')
        # and later
        video.play = True

    :data:`play` is a :class:`~kivy.properties.BooleanProperty`, default to
    True.
    '''

    index = NumericProperty(-1)
    '''Index of the used camera, starting from 0.

    :data:`index` is a :class:`~kivy.properties.NumericProperty`, default to -1
    to allow auto selection.
    '''

    resolution = ListProperty([-1, -1])
    '''Prefered 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 of the implementation, the camera may not respect this
        property.

    :data:`resolution` is a :class:`~kivy.properties.ListProperty`, default to
    [-1, -1]
    '''
    def __init__(self, **kwargs):
        self._camera = None
        super(Camera, self).__init__(**kwargs)
        if self.index == -1:
            self.index = 0
        self.bind(index=self._on_index, resolution=self._on_index)
        self._on_index()

    def _on_index(self, *largs):
        self._camera = None
        if self.index < 0:
            return
        self._camera = CoreCamera(index=self.index,
                                  resolution=self.resolution,
                                  stopped=True)
        self._camera.bind(on_load=self._camera_loaded)
        if self.play:
            self._camera.start()

    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.play()
        else:
            self._camera.stop()
Exemple #16
0
 def __init__(self):
     self.cam = CoreCamera(index=0, resolution=(640, 480))
     self.caminstance = self.cam._android_camera
class Camera(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 = None
        super(Camera, 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.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()
class CustomVideoPlayer(Camera):
    fullscreen = BooleanProperty(False)
    '''Switch to fullscreen view. This should be used with care. When
    activated, the widget will remove itself from its parent, remove all
    children from the window and will add itself to it. When fullscreen is
    unset, all the previous children are restored and the widget is restored to
    its previous parent.

    .. warning::

        The re-add operation doesn't care about the index position of it's
        children within the parent.

    :attr:`fullscreen` is a :class:`~kivy.properties.BooleanProperty`
    and defaults to False.
    '''

    allow_fullscreen = BooleanProperty(True)
    '''By default, you can double-tap on the video to make it fullscreen. Set
    this property to False to prevent this behavior.

    :attr:`allow_fullscreen` is a :class:`~kivy.properties.BooleanProperty`
    defaults to True.
    '''

    port = NumericProperty(0)

    _fullscreen_state = None

    def __init__(self, **kwargs):
        self._camera = None
        super(Camera, self).__init__(**kwargs)
        if self.index == -1:
            self.index = 0

        self.play = True
        self.allow_stretch = True
        self.reconfigure_camera()

    # noinspection PyUnusedLocal
    def on_index(self, instance, value):
        self.reconfigure_camera()

    # noinspection PyUnusedLocal
    def on_port(self, instance, value):
        self.reconfigure_camera()

    # noinspection PyUnusedLocal
    def on_resolution(self, instance, value):
        self.reconfigure_camera()

    def reconfigure_camera(self):
        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, port=self.port, stopped=True)
        self._camera.bind(on_load=self._camera_loaded)
        if self.play:
            self._camera.start()
            self._camera.bind(on_texture=self.on_tex)

    # noinspection PyUnusedLocal
    def on_fullscreen(self, instance, value):
        window = self.get_parent_window()
        if not window:
            Logger.warning('AliveVideoPlayer: Cannot switch to fullscreen, window not found.')
            if value:
                self.fullscreen = False
            return
        if not self.parent:
            Logger.warning('AliveVideoPlayer: Cannot switch to fullscreen, no parent.')
            if value:
                self.fullscreen = False
            return

        if value:
            self._fullscreen_state = state = {
                'parent': self.parent,
                'pos': self.pos,
                'size': self.size,
                'pos_hint': self.pos_hint,
                'size_hint': self.size_hint,
                'window_children': window.children[:]}

            # remove all window children
            for child in window.children[:]:
                window.remove_widget(child)

            # put the video in fullscreen
            if state['parent'] is not window:
                state['parent'].remove_widget(self)
            window.add_widget(self)

            # ensure the video widget is in 0, 0, and the size will be
            # reajusted
            self.pos = (0, 0)
            self.size = (100, 100)
            self.pos_hint = {}
            self.size_hint = (1, 1)
        else:
            state = self._fullscreen_state
            window.remove_widget(self)
            for child in state['window_children']:
                window.add_widget(child)
            self.pos_hint = state['pos_hint']
            self.size_hint = state['size_hint']
            self.pos = state['pos']
            self.size = state['size']
            if state['parent'] is not window:
                state['parent'].add_widget(self)