【问题标题】:How I can use Kivy (Python) camera我如何使用 Kivy (Python) 相机
【发布时间】:2016-11-05 11:12:57
【问题描述】:

我尝试使用 uix.camera 小部件并从我的网络摄像头中显示一些宽幅图像。 我查看了文档并尝试使用这个简单的代码。但它只是给我看一个没有任何视频的白色屏幕(我启用了播放)。 我做错了什么? 也许存在一些有用的文档\教程(因为从官方文档中我了解了很多)。感谢您的帮助。

import kivy
kivy.require('1.9.1')

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.camera import Camera

class MainApp(App):
    def build(self):
        return Camera(play=True)

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

【问题讨论】:

    标签: python camera kivy


    【解决方案1】:

    您需要指定分辨率。就我而言,我还需要指定 index=1,即插入我计算机的第二个摄像头。

    例子:

    import kivy
    kivy.require('1.9.1')
    
    from kivy.app import App
    from kivy.uix.label import Label
    from kivy.uix.camera import Camera
    
    class MainApp(App):
        def build(self):
            return Camera(play=True, index=1, resolution=(640,480))
    
    if __name__== "__main__":
        MainApp().run()
    

    【讨论】:

      【解决方案2】:

      好像除了play=True之外还需要设置resolution=[x, y]属性,因为默认的不行。

      【讨论】:

      • 嗨。感谢您的回答。当我尝试使用此代码 return Camera(resolution=(320, 240), play=True) 时出现错误:第 103 行,在 _on_index resolution=self.resolution,stopped=True ) TypeError: 'NoneType' 对象不可调用
      【解决方案3】:

      以下是Kivy examples使用摄像头。

      from kivy.app import App
      from kivy.lang import Builder
      
      
      kv = ''' 
      BoxLayout:
          orientation: 'vertical'
      
          Camera:
              id: camera
              resolution: 399, 299
      
          BoxLayout:
              orientation: 'horizontal'
              size_hint_y: None
              height: '48dp'
              Button:
                  text: 'Start'
                  on_release: camera.play = True
      
              Button:
                  text: 'Stop'
                  on_release: camera.play = False
      '''
      
      
      class CameraApp(App):
          def build(self):
              return Builder.load_string(kv)
      
      
      if __name__ == '__main__':
          CameraApp().run()
      

      Example 2:-

      from kivy.app import App
      from kivy.lang import Builder
      from kivy.uix.boxlayout import BoxLayout
      import time
      Builder.load_string('''
      <CameraClick>:
          orientation: 'vertical'
          Camera:
              id: camera
              resolution: (640, 480)
              play: False
          ToggleButton:
              text: 'Play'
              on_press: camera.play = not camera.play
              size_hint_y: None
              height: '48dp'
          Button:
              text: 'Capture'
              size_hint_y: None
              height: '48dp'
              on_press: root.capture()
      ''')
      
      
      class CameraClick(BoxLayout):
          def capture(self):
              '''
              Function to capture the images and give them the names
              according to their captured time and date.
              '''
              camera = self.ids['camera']
              timestr = time.strftime("%Y%m%d_%H%M%S")
              camera.export_to_png("IMG_" + timestr)
              print("Captured")
      
      
      class TestCamera(App):
      
          def build(self):
              return CameraClick()
      
      
      TestCamera().run()
      

      【讨论】:

      • 在我的笔记本电脑上,我需要另外安装 opencv-python。此外,删除有关分辨率的代码。这两个步骤使它工作。 (kivy 2.0.0)
      【解决方案4】:

      我刚遇到同样的问题,发现 Kivy 在为 USB 网络摄像头设备创建小部件时非常慢。 如果您已正确设置索引和其他参数,也许只是等待 Kivy 创建视频小部件的时间稍长,然后您可以在窗口中看到网络摄像头视图,但我仍在尝试找出 Kivy 这样做的原因很长一段时间(大约一分钟)来创建 USB 网络摄像头小部件,希望有人能就这个问题提供一些建议。

      【讨论】:

      • 这不是答案。
      【解决方案5】:

      我尝试使用@Thiago 建议的代码。它没有用,我怀疑它没有检测到我插入树莓派 4 的 USB 摄像头。

      以下是我正在使用的代码,我还在终端中运行了一个查询以获取插入的相机类型(仅插入一个)。

      代码:

      import kivy
      
      from kivy.app import App
      from kivy.uix.label import Label
      from kivy.uix.camera import Camera
      
      class MainApp(App):
          def build(self):
              return Camera(play=True, index=0, resolution=(640,480))
      
      if __name__== "__main__":
          MainApp().run()
      

      终端输出:

      pi@raspberrypi:~ $ v4l2-ctl --list-formats
      ioctl: VIDIOC_ENUM_FMT
      Type: Video Capture
      [0]: 'MJPG' (Motion-JPEG, compressed)
      [1]: 'YUYV' (YUYV 4:2:2)
      

      【讨论】:

      • 在 lattop 而不是 rasperry 上运行 python 代码时。比如带有kivy 2.0.0的Lenovn小新Pro 13。它将得到 [CRITICAL] 错误,称为 picamera - ModuleNotFoundError: No module named 'picamera'
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-07
      • 1970-01-01
      • 2016-08-23
      • 2021-10-22
      • 2022-07-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多