0

I found a possible solution to getting what I need however the program Freezes up when I click the Quit Button. There must be a more elegant way of getting this to work without it crashing. Is there a need for all the use of global? I'm using python3.6 and using Kivy 1.10.0

Kivy Camera as KV language widget

Gerald Leese
  • 355
  • 4
  • 13

1 Answers1

0

Well I haven't added a quit function however It seems to work navigating to other screens and back without crashing. I put the fps and capture part right into the KivyCamera class.

import kivy.core.text
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.scrollview import ScrollView
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.base import runTouchApp
from kivy.clock import Clock
from kivy.uix.image import Image
import time
import cv2
from kivy.uix.boxlayout import BoxLayout
from kivy.graphics.texture import Texture
class KivyCamera(Image):

    def __init__(self, **kwargs):

    super(KivyCamera, self).__init__(**kwargs)

    self.capture = cv2.VideoCapture(0)

    self.fps = 30

    Clock.schedule_interval(self.update, 1.0 / self.fps)





    def update(self, dt):

        ret, frame = self.capture.read()

        if ret:

            # convert it to texture

            buf1 = cv2.flip(frame, 0)

            buf = buf1.tostring()

            image_texture = Texture.create(

            size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')

            image_texture.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')

        # display image from the texture

            self.texture = image_texture

            self.canvas.ask_update()
class MainScreen(Screen):

    pass

class OptionsScreen(Screen):
    pass


class AlarmScreen(Screen):
    pass

class Project_1App(App):

    def build(self):
        screenManager = ScreenManager()
        screenManager.add_widget(MainScreen(name="main"))
        screenManager.add_widget(OptionsScreen(name="options"))
        screenManager.add_widget(AlarmScreen(name="alarm"))

        return screenManager




Project_1App().run()

and the kivy bit:

<MainScreen>:               

    BoxLayout:
        orientation: "vertical"

        KivyCamera:

        Label:
            height: 20
            size_hint_y: None
            text: 'Cam_01'


    BoxLayout:
        orientation: "horizontal"
        height: 50
        size_hint_y: None


        Button:
            text: "Options"
            size_hint_y: None

            width: 150
            height: 50

            on_release: root.manager.current = 'options' 

<OptionsScreen>
    GridLayout:
        rows:3
        cols:1
        padding: 10
        spacing: 10

        Button:
            text: "Main Screen"
            size_hint_y: None

            width: 150
            height: 50

            on_release: root.manager.current = 'main'

        Button:
            text: "Alarm"
            size_hint_y: None

            width: 150
            height: 50


            on_release: root.manager.current = 'alarm'


        Button:
            text: "test"
            size_hint_y: None

            width: 150
            height: 50

<AlarmScreen>
    GridLayout:
        rows:3
        cols:1
        padding: 10
        spacing: 10

        Button:
            text: "Main Screen"
            size_hint_y: None

            width: 150
            height: 50

            on_release: root.manager.current = 'main'

        Button:
            text: "Options"
            size_hint_y: None

            width: 150
            height: 50


            on_release: root.manager.current = 'options'


        Button:
            text: "test"
            size_hint_y: None

            width: 150
            height: 50
Gerald Leese
  • 355
  • 4
  • 13