Skip to content Skip to sidebar Skip to footer

Kivy Camera As Kv Language Widget

I am using Kivy with a webcam. I have followed this example by @Arnav of using opencv to form and display the camera as a widget. I have 'extended' the layout within python it to a

Solution 1:

Perhaps this example may help you.

# Import 'kivy.core.text' must be called in entry point script# before import of cv2 to initialize Kivy's text provider.# This fixes crash on app exit.import kivy.core.text
import cv2
from kivy.app import App
from kivy.base import EventLoop
from kivy.uix.image import Image
from kivy.clock import Clock
from kivy.graphics.texture import Texture
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window


classKivyCamera(Image):

    def__init__(self, **kwargs):
        super(KivyCamera, self).__init__(**kwargs)
        self.capture = Nonedefstart(self, capture, fps=30):
        self.capture = capture
        Clock.schedule_interval(self.update, 1.0 / fps)

    defstop(self):
        Clock.unschedule_interval(self.update)
        self.capture = Nonedefupdate(self, dt):
        return_value, frame = self.capture.read()
        if return_value:
            texture = self.texture
            w, h = frame.shape[1], frame.shape[0]
            ifnot texture or texture.width != w or texture.height != h:
                self.texture = texture = Texture.create(size=(w, h))
                texture.flip_vertical()
            texture.blit_buffer(frame.tobytes(), colorfmt='bgr')
            self.canvas.ask_update()


capture = NoneclassQrtestHome(BoxLayout):

    definit_qrtest(self):
        passdefdostart(self, *largs):
        global capture
        capture = cv2.VideoCapture(0)
        self.ids.qrcam.start(capture)

    defdoexit(self):
        global capture
        if capture != None:
            capture.release()
            capture = None
        EventLoop.close()


classqrtestApp(App):

    defbuild(self):
        Window.clearcolor = (.4,.4,.4,1)
        Window.size = (400, 300)
        homeWin = QrtestHome()
        homeWin.init_qrtest()
        return homeWin

    defon_stop(self):
        global capture
        if capture:
            capture.release()
            capture = None

qrtestApp().run()

and the kv file:

<QrtestHome>:BoxLayout:orientation:"vertical"Label:height:20size_hint_y:Nonetext:'Testing the camera'KivyCamera:id:qrcamBoxLayout:orientation:"horizontal"height:20size_hint_y:NoneButton:id:butt_startsize_hint:0.5,1text:"start"on_press:root.dostart()Button:id:butt_exittext:"quit"size_hint:0.5,1on_press:root.doexit()

Post a Comment for "Kivy Camera As Kv Language Widget"