两个不同的屏幕,每个屏幕都有摄像头

Two differents screens with camera for each one

在我的应用程序中,我无法为每个屏幕创建 2 个带摄像头的不同屏幕。 但是,我只能用相机创建一个屏幕

我的Python代码

from kivymd.app import MDApp
from kivy.uix.camera import Camera
from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition

class ZTest(MDApp):
    def build(self):
        manager = ScreenManager()
        manager.add_widget(FirstLayout(name='First'))
        manager.add_widget(SecondLayout(name='Second'))
        manager.add_widget(ThirdLayout(name='Third'))
        return manager


class FirstLayout(Screen):
    def btn1(self):
        self.manager.transition = SlideTransition(direction="left")
        self.manager.current = 'Second'

    def btn2(self):
        self.manager.transition = SlideTransition(direction="left")
        self.manager.current = 'Third'


class SecondLayout(Screen):
    cam = Camera()

    def on_enter(self, *largs):
        self.manager.get_screen('Second').ids.cam1.clear_widgets()
        self.cam.play = True
        self.manager.get_screen('Second').ids.cam1.add_widget(self.cam)

    def press(self):
        self.cam.play = False
        self.manager.transition = SlideTransition(direction="right")
        self.manager.current = 'First'


class ThirdLayout(Screen):
    cam = Camera()

    def on_enter(self, *largs):
        self.manager.get_screen('Third').ids.cam2.clear_widgets()
        self.cam.play = True
        self.manager.get_screen('Third').ids.cam2.add_widget(self.cam)

    def press(self):
        self.cam.play = False
        self.manager.transition = SlideTransition(direction="right")
        self.manager.current = 'First'


if __name__ == "__main__":
    ZTest().run()

我的基维代码'Ztest.kv'

<FirstLayout>:
    MDBoxLayout:
        orientation: 'vertical'
        spacing: 10
        size_hint_y: 0.5

        MDBoxLayout:
            size_hint_y: 0.5

            MDRaisedButton:
                id: B1
                text:"Second"
                md_bg_color: (0, 0, 1, 1)
                font_size: 40
                size_hint: (0.8, None)
                pos_hint: {'center_x':0.5}
                on_press: root.btn1()

        MDBoxLayout:
            size_hint_y: 0.5

            MDRaisedButton:
                id: B2
                text:"Third"
                md_bg_color: (1, 0, 0, 1)
                font_size: 40
                size_hint: (0.8, None)
                pos_hint: {'center_x':0.5}
                on_press: root.btn2()


<SecondLayout>:
    BoxLayout:
        orientation: 'vertical'

        MDToolbar:
            id: bar2
            title: "Second Layout"
            md_bg_color: (0, 0, 1, 1)
            right_action_items:[["arrow-left", lambda x: root.press()]]

        BoxLayout:
            id: cam1

        BoxLayout:
            orientation: 'vertical'

            MDFloatingActionButton:
                icon: "camera"
                md_bg_color: (0, 0, 1, 1)
                pos_hint: {'center_x':0.5}


<ThirdLayout>:
    BoxLayout:
        orientation: 'vertical'

        MDToolbar:
            id: bar2
            title: "Third Layout"
            md_bg_color: (1, 0, 0, 1)
            right_action_items:[["arrow-left", lambda x: root.press()]]

        BoxLayout:
            id: cam2

        BoxLayout:
            orientation: 'vertical'

            MDFloatingActionButton:
                icon: "camera"
                md_bg_color: (1, 0, 0, 1)
                pos_hint: {'center_x':0.5}

我收到此错误消息

[ERROR  ] [OpenCV      ] Couldn't get image from Camera
Traceback (most recent call last):
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\kivy\core\camera\camera_opencv.py", line 144, in _update
    self._buffer = frame.imageData
AttributeError: 'NoneType' object has no attribute 'imageData'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\kivy\core\camera\camera_opencv.py", line 148, in _update
    self._buffer = frame.reshape(-1)
AttributeError: 'NoneType' object has no attribute 'reshape'

但是当我删除 ThirdLayout class 时,代码和相机运行正常。 所以,我不明白问题是与创建另一个屏幕的方法有关,还是与调用Camera的方法有关。

这是因为您创建了 2 个相机,我建议您创建 Camera class 的 1 个实例,然后在任何您想要的地方使用它,不要忘记删除上一个发射位置的相机(布局)。

from kivymd.app import MDApp
from kivy.uix.camera import Camera
from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition
from kivy.lang.builder import Builder

Builder.load_string("""
<FirstLayout>:
    MDBoxLayout:
        orientation: 'vertical'
        spacing: 10
        size_hint_y: 0.5

        MDBoxLayout:
            size_hint_y: 0.5

            MDRaisedButton:
                id: B1
                text:"Second"
                md_bg_color: (0, 0, 1, 1)
                font_size: 40
                size_hint: (0.8, None)
                pos_hint: {'center_x':0.5}
                on_press: root.btn1()

        MDBoxLayout:
            size_hint_y: 0.5

            MDRaisedButton:
                id: B2
                text:"Third"
                md_bg_color: (1, 0, 0, 1)
                font_size: 40
                size_hint: (0.8, None)
                pos_hint: {'center_x':0.5}
                on_press: root.btn2()


<SecondLayout>:
    BoxLayout:
        orientation: 'vertical'

        MDToolbar:
            id: bar2
            title: "Second Layout"
            md_bg_color: (0, 0, 1, 1)
            right_action_items:[["arrow-left", lambda x: root.press()]]

        BoxLayout:
            id: cam1

        BoxLayout:
            orientation: 'vertical'

            MDFloatingActionButton:
                icon: "camera"
                md_bg_color: (0, 0, 1, 1)
                pos_hint: {'center_x':0.5}


<ThirdLayout>:
    BoxLayout:
        orientation: 'vertical'

        MDToolbar:
            id: bar2
            title: "Third Layout"
            md_bg_color: (1, 0, 0, 1)
            right_action_items:[["arrow-left", lambda x: root.press()]]

        BoxLayout:
            id: cam2

        BoxLayout:
            orientation: 'vertical'

            MDFloatingActionButton:
                icon: "camera"
                md_bg_color: (1, 0, 0, 1)
                pos_hint: {'center_x':0.5}
""")

camera = None


class ZTest(MDApp):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        global camera
        camera = Camera()

    def build(self):
        manager = ScreenManager()
        manager.add_widget(FirstLayout(name='First'))
        manager.add_widget(SecondLayout(name='Second'))
        manager.add_widget(ThirdLayout(name='Third'))
        return manager


class FirstLayout(Screen):
    def btn1(self):
        self.manager.transition = SlideTransition(direction="left")
        self.manager.current = 'Second'

    def btn2(self):
        self.manager.transition = SlideTransition(direction="left")
        self.manager.current = 'Third'


class SecondLayout(Screen):
    def __init__(self, **kw):
        super().__init__(**kw)
        global camera
        self.cam = camera

    def on_enter(self, *largs):
        self.manager.get_screen('Second').ids.cam1.clear_widgets()
        self.manager.get_screen('Third').ids.cam2.clear_widgets()
        
        self.cam.play = True
        self.manager.get_screen('Second').ids.cam1.add_widget(self.cam)

    def press(self):
        self.cam.play = False
        self.manager.transition = SlideTransition(direction="right")
        self.manager.current = 'First'


class ThirdLayout(Screen):
    def __init__(self, **kw):
        super().__init__(**kw)
        global camera
        self.cam = camera

    def on_enter(self, *largs):
        self.manager.get_screen('Second').ids.cam1.clear_widgets()
        self.manager.get_screen('Third').ids.cam2.clear_widgets()
        
        self.cam.play = True
        self.manager.get_screen('Third').ids.cam2.add_widget(self.cam)

    def press(self):
        self.cam.play = False
        self.manager.transition = SlideTransition(direction="right")
        self.manager.current = 'First'


if __name__ == "__main__":
    ZTest().run()