Kivy 错误相机网络摄像头给出错误 VideoCapture:未找到分辨率

问题描述 投票:0回答:5

我有一个 Kivy 应用程序,我正在尝试从我的网络摄像头拍摄视频并将其放入计算机上的应用程序中。我在网上得到了这个代码:-

from kivy.app import App
from kivy.lang import Builder

kv = '''
BoxLayout:
    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'
'''


class TestCamera(App):
    def build(self):
        return Builder.load_string(kv)

TestCamera().run() 

我收到一条错误消息,指出 VideoCapture:Resolution Not Found in kivy/core/camera/camera_videocaputure。我想出了很多不同的方法,但无法解决这个问题。如果有人能帮助我,那就太好了。谢谢 !

错误回溯是:

Traceback (most recent call last):
 File "C:\Users\User\Desktop\personal work\BinaryHeap.py", line 23, in     <module>
     TestCamera().run()


   File "C:\Users\User\Desktop\personal work\Anaconda3\envs\py27\lib\site-packages\kivy\app.py", line 802, in run
     root = self.build()
   File "C:\Users\User\Desktop\personal work\BinaryHeap.py", line 21, in build
     return Builder.load_string(kv)
   File "C:\Users\User\Desktop\personal work\Anaconda3\envs\py27\lib\site-packages\kivy\lang.py", line 1921, in load_string
     self._apply_rule(widget, parser.root, parser.root)
   File "C:\Users\User\Desktop\personal work\Anaconda3\envs\py27\lib\site-packages\kivy\lang.py", line 2130, in _apply_rule
     e), cause=tb)
 BuilderException: Parser: File "<inline>", line 6:
 ...
       4:    Camera:
       5:        id: camera
 >>    6:        resolution: (640, 480)
       7:        play: False
       8:    ToggleButton:
 ...
 Exception: VideoCapture: Resolution not supported
   File "C:\Users\User\Desktop\personal work\Anaconda3\envs\py27\lib\site-packages\kivy\lang.py", line 2123, in _apply_rule
     setattr(widget_set, key, value)
   File "kivy\weakproxy.pyx", line 22, in kivy.weakproxy.WeakProxy.__setattr__ (kivy\weakproxy.c:1235)
   File "kivy\properties.pyx", line 408, in kivy.properties.Property.__set__ (kivy\properties.c:5114)
   File "kivy\properties.pyx", line 733, in kivy.properties.ListProperty.set (kivy\properties.c:11127)
   File "kivy\properties.pyx", line 446, in kivy.properties.Property.set (kivy\properties.c:5876)
   File "kivy\properties.pyx", line 501, in kivy.properties.Property.dispatch (kivy\properties.c:6557)
   File "kivy\_event.pyx", line 1224, in kivy._event.EventObservers.dispatch (kivy\_event.c:13497)
   File "kivy\_event.pyx", line 1130, in kivy._event.EventObservers._dispatch (kivy\_event.c:12696)
   File "C:\Users\User\Desktop\personal work\Anaconda3\envs\py27\lib\site-packages\kivy\uix\camera.py", line 103, in _on_index
     resolution=self.resolution, stopped=True)
   File "C:\Users\User\Desktop\personal work\Anaconda3\envs\py27\lib\site-packages\kivy\core\camera\camera_videocapture.py", line 26, in __init__
     super(CameraVideoCapture, self).__init__(**kwargs)
   File "C:\Users\User\Desktop\personal work\Anaconda3\envs\py27\lib\site-packages\kivy\core\camera\__init__.py", line 70, in __init__
     self.init_camera()
   File "C:\Users\User\Desktop\personal work\Anaconda3\envs\py27\lib\site-packages\kivy\core\camera\camera_videocapture.py", line 36, in init_camera
     raise Exception('VideoCapture: Resolution not supported')

[-1,-1] 也不起作用,只是给我提供了一个空白屏幕。如果有人尝试过,请告诉我它是否对他们有用?还有其他方法可以让相机正常工作吗?

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

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


CamApp().run()

上面的代码对我有用,但我不知道如何将其更改为 kivy 文件。所以我将不胜感激任何帮助。谢谢。我拍了一张照片,我的相机分辨率是 1920 x 1080。我只是觉得这可能有帮助。

python video camera kivy webcam
5个回答
1
投票

venv/share/kivy-examples/camera/main.py
运行官方相机演示时,程序崩溃并出现以下错误:

kivy.lang.builder.BuilderException: Parser: File "<inline>", line 6:
 ...
       4:    Camera:
       5:        id: camera
 >>    6:        resolution: (640, 480)
       7:        play: False
       8:    ToggleButton:
 ...

6:  resolution: (640, 480)
。这个问题可以通过安装 OpenCV 来解决。

pip install opencv-python

Pip 将自动选择兼容的 OpenCV 进行安装。

测试于:

  • Windows 10、Python 3.6、Kivy==1.11.1
  • Ubuntu 22.04、Python 3.7、Kivy==1.11.1

0
投票

我没有资格发表评论,所以我将其留在这里。 您的两个脚本在我的笔记本电脑上都运行良好。我尝试了几种分辨率,所有分辨率都受支持:1920x1080、640x480、320x240。 检查你的 kivy 和 OpenCV 版本。我的是: OpenCV 2.4.12 基维 v1.9.0 Python v2.7.8


0
投票

嗨,我在 Windows 7 上运行了该程序并且它有效!我不确定这是否是操作系统问题,但它正在工作。因此,如果有人让它适用于 Windows 8 或 10,请发表评论。我浪费了很多时间来调试它,但无法让它在这两个操作系统上运行。无论如何,谢天谢地,它成功了,感谢 dizcza 的回复。这真的很有帮助。我确实给你投票了,但由于我的声誉较低,它没有出现。


0
投票

如果您使用 anaconda spider 应用程序来运行此脚本,那么这就是您收到该错误的真正原因(我也有同样的错误)。尝试使用另一个安装了 kivy 包的编辑器(尝试 Pycharm)


0
投票
def test_camera_resolutions(self, source, size):
    cap = cv2.VideoCapture(source) 
    w, h = size

    cap.set(cv2.CAP_PROP_FRAME_WIDTH, w)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, h)

    if cap is None or not cap.isOpened():
        print('Camera do not effort this resolutions!')
© www.soinside.com 2019 - 2024. All rights reserved.