Opencv网络摄像头和Kivy GUI动作栏同时进行

时间:2016-08-17 15:57:09

标签: python opencv kivy webcam kivy-language

嘿所以我正在尝试使用opencv和kivy。我的代码返回一个名为KivyCamera的类,它无法在屏幕顶部添加操作栏。在那儿 我可以在Kivy GUI框架中同时添加操作栏和opencv网络摄像头的任何方式吗?谢谢。如果你这样做

def build:    通过 在python脚本而不是camApp中的构建函数中,您可以看到操作栏。只是我不知道如何将它们组合在一起。

Python文件:

from kivy.app import App
from kivy.uix.image import Image
from kivy.clock import Clock
from kivy.graphics.texture import Texture
import cv2

class KivyCamera(Image):
    def __init__(self, capture, fps, **kwargs):
        super(KivyCamera, self).__init__(**kwargs)
        self.capture = capture
        Clock.schedule_interval(self.update, 1.0 / 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


class CamApp(App):
    def build(self):
        self.capture = cv2.VideoCapture(1)
        self.my_camera = KivyCamera(capture=self.capture, fps=30)
        return self.my_camera

    def on_stop(self):
        #without this, app will not exit even if the window is closed
        self.capture.release()


if __name__ == '__main__':
    CamApp().run()

kv文件:

#:kivy 1.8.0
#:import KivyLexer kivy.extras.highlight.KivyLexer
#:import Factory kivy.factory.Factory

<ActionSpinnerOptions@SpinnerOption>
    background_color: .4, .4, .4, 1

<ActionSpinner@Spinner+ActionItem>
    canvas.before:
        Color:
            rgba: 0.128, 0.128, 0.128, 1
        Rectangle:
            size: self.size
            pos: self.pos
    border: 27, 20, 12, 12
    background_normal: 'atlas://data/images/defaulttheme/action_group'
    option_cls: Factory.ActionSpinnerOptions

<ActionDropdown>:
    on_size: self.width = '220dp'

<ShowcaseScreen>:
    ScrollView:
        do_scroll_x: False
        do_scroll_y: False if root.fullscreen else (content.height > root.height - dp(16))
        AnchorLayout:
            size_hint_y: None
            height: root.height if root.fullscreen else max(root.height, content.height)
            GridLayout:
                id: content
                cols: 1
                spacing: '8dp'
                padding: '8dp'
                size_hint: (1, 1) if root.fullscreen else (.8, None)
                height: self.height if root.fullscreen else self.minimum_height


BoxLayout:
    orientation: 'vertical'

    canvas.before:
        Color:
            rgb: .6, .6, .6
        Rectangle:
            size: self.size
            source: 'data/background.png'

    ActionBar:

        ActionView:
            id: av
            ActionPrevious:
                with_previous: (False if sm.current_screen.name == 'button' else True) if sm.current_screen else False
                title: 'Showcase' + ('' if not app.current_title else ' - {}'.format(app.current_title))
                on_release: app.go_hierarchy_previous()

            ActionSpinner:
                id: spnr
                important: True
                text: 'Jump to Screen'
                values: app.screen_names
                on_text:
                    if sm.current != args[1]:\
                    idx = app.screen_names.index(args[1]);\
                    app.go_screen(idx)
            ActionToggleButton:
                text: 'Toggle sourcecode'
                icon: 'data/icons/sourcecode.png'
                on_release: app.toggle_source_code()
            ActionButton:
                text: 'Previous screen'
                icon: 'data/icons/prev.png'
                on_release: app.go_previous_screen()

            ActionButton:
                text: 'Next screen'
                icon: 'data/icons/next.png'
                on_release: app.go_next_screen()
                important: True

    ScrollView:
        id: sv
        size_hint_y: None
        height: 0

        CodeInput:
            id: sourcecode
            lexer: KivyLexer()
            text: app.sourcecode
            readonly: True
            size_hint_y: None
            font_size: '12sp'
            height: self.minimum_height

    ScreenManager:
        id: sm
        on_current_screen:
            spnr.text = args[1].name
            idx = app.screen_names.index(args[1].name)
            if idx > -1: app.hierarchy.append(idx)

1 个答案:

答案 0 :(得分:0)

(注意:我还没有使用opencv,我在pypi上找不到它)

我看到您的班级KivyCamera只是Kivy中使用的休闲小部件。执行def build(self): pass时会发生什么,它会打开kv文件并找到可用作根窗口小部件的内容 - 在您的情况下可能是BoxLayout:下的<ShowcaseScreen>:

您可以做的是:尝试将<KivyCamera>:放入kv文件中。就是这样,没有别的。然后将KivyCamera:放在BoxLayout:的某个位置(最好将ScreenManager作为单独的Screen)并显示它。调整大小不应该是一个问题,因为它是BoxLayout,默认情况下应该禁止ActionBarKivyCamera的重叠。