简体   繁体   English

我如何使用 Kivy (Python) 相机

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

I try to use uix.camera widget and show some wideo from my web-camera.我尝试使用 uix.camera 小部件并从我的网络摄像头显示一些wideo。 I looked into the documentation and try to use this simply code.我查看了文档并尝试使用这个简单的代码。 But it's just show me a white creen withoud any video (I enabled playing).但它只是向我展示一个没有任何视频的白色屏幕(我启用了播放)。 What I'm doing wrong?我做错了什么? Maybe some useful docs\\tutorial exist (because from official documentation I understanding a little from many).也许存在一些有用的文档\\教程(因为从官方文档中我了解了很多)。 Thanks for any help.感谢您的帮助。

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

You need to specify resolution.您需要指定分辨率。 In my case, I also needed to specify index=1, that is the second camera plugged in my computer.就我而言,我还需要指定 index=1,即插入我计算机的第二个摄像头。

Example:示例:

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]属性,因为默认设置不起作用。

The following are the Kivy examples to use camera.以下是使用相机的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()

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

I just faced the same problem and found out that Kivy is very slow when creating widget for usb webcam device.我刚刚遇到了同样的问题,发现 Kivy 在为 USB 网络摄像头设备创建小部件时非常慢。 If you have set the index and other parameters properly, maybe just waiting a little longer for Kivy to create the video widget then you could see the webcam view shows in the window, but I am still trying to find out the reason why Kivy takes so long time (about a minute) to create usb webcam widget, hope someone could give some advices about this problem.如果您已经正确设置了索引和其他参数,也许只是等待 Kivy 创建视频小部件的时间更长一些,那么您可以在窗口中看到网络摄像头视图,但我仍在尝试找出 Kivy 这样做的原因花了很长时间(大约一分钟)来创建 USB 网络摄像头小部件,希望有人可以就这个问题提供一些建议。

I tried using the code as suggested by @Thiago.我尝试使用@Thiago 建议的代码。 It didnt work, I suspect its not detecting my USB camera that I have plugged into my Raspberry Pi4.它没有用,我怀疑它没有检测到我插入 Raspberry Pi4 的 USB 摄像头。

Following is the code I am using and I also ran a query in the terminal to obtain the camera type that is plugged in (only one plugged in).以下是我正在使用的代码,我还在终端中运行了一个查询以获取插入的相机类型(仅插入一个)。

The Code:代码:

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

Terminal Output:终端输出:

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