繁体   English   中英

我如何使用 Kivy (Python) 相机

[英]How I can use Kivy (Python) camera

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

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

您需要指定分辨率。 就我而言,我还需要指定 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()

除了play=True之外resolution=[x, y]您似乎还需要设置resolution=[x, y]属性,因为默认设置不起作用。

以下是使用相机的Kivy 示例

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

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

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

我尝试使用@Thiago 建议的代码。 它没有用,我怀疑它没有检测到我插入 Raspberry Pi4 的 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM