在OpenCv中使用Kivy。此解决方案在退出时崩溃

时间:2018-10-08 18:56:16

标签: kivy python-3.6

我找到了可能的解决方案,但是单击退出按钮时程序冻结。必须有一种更优雅的方法来使其工作而不会崩溃。是否需要全部使用全局? 我正在使用python3.6和Kivy 1.10.0

Kivy Camera as KV language widget

1 个答案:

答案 0 :(得分:0)

嗯,我还没有添加退出功能,但是它似乎可以导航到其他屏幕并返回而不会崩溃。我将fps并捕获了一部分,直接放入KivyCamera类中。

    float dY;         @Override
public boolean onTouchEvent(MotionEvent event) {switch (event.getActionMasked()) {

        case MotionEvent.ACTION_DOWN:              
            dY = getY() - event.getRawY();
            return false;

        case MotionEvent.ACTION_MOVE:
            final float distance = event.getRawY() + dY ;
            animate()                     
                    .y(distance)
                    .setDuration(0)
                    .start();
            return true;


        default:
            return super.onTouchEvent(event);
    }


}

和猕猴桃:

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()