简体   繁体   中英

Kivy error Camera Webcam gives error VideoCapture:Resolution not found

I have a Kivy App and I am trying to take a video from my webcam camera to put it in my application on my computer. I got this code online which was :-

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

I am getting an error stating that VideoCapture:Resolution Not Found in kivy/core/camera/camera_videocaputure. I tied to figure out many different ways yet I could not resolve the query. It would be great if anyone could help me out with it. Thanks !

The error Traceback is :

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] also does not work just provides me with a blank screen. If someone tries it please temme if it worked for them ? Is there any other way I can get the camera working ?

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

The above code works for me but I don't know how to change it to a kivy file. So I would appreciate any help. Thanks. I took a picture and my camera resolution was 1920 x 1080. I just thought it might help.

I don't have the reputation to comment so I'll leave it here. Both your scripts are working fine on my laptop. I've tried several resolutions and all of them are supported: 1920x1080, 640x480, 320x240. Check your kivy and OpenCV versions. Mine is: OpenCV 2.4.12 Kivy v1.9.0 Python v2.7.8

Hi so I ran the program on Windows 7 and it works ! I am not sure if it was an OS problem or not but it is working. So if someone makes it working for Windows 8 or 10 please comment. I wasted a lot of time debugging this and could not get it working on those 2 OS. Well anyway thankfully got it working and thank you dizcza for your reply.It was really helpful. I did upvote you but it does not show up due to my low reputation.

pip install opencv-python

from here: https://pypi.org/project/opencv-python/ fixed it for me.

Windows 10, Python 3.6, Kivy 1.11.1

if you are using anaconda spyder application to run this script then that is the real reason you are getting that error (i had the same error). Try using another editor with kivy package installed (try Pycharm)

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!')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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