我有一个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:在kivy / core / camera / camera_videocaputure中找不到分辨率。我想出了许多不同的方法但我无法解决查询。如果有人能帮我解决它会很棒。谢谢 !
错误Traceback是:
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.我只是觉得它可能会有所帮助。
答案 0 :(得分:0)
我没有评论的声誉所以我会留在这里。 你的脚本在我的笔记本电脑上运行正常。我已经尝试了几种分辨率并支持所有分辨率:1920x1080,640x480,320x240。 检查您的kivy和OpenCV版本。我的是: OpenCV 2.4.12 Kivy v1.9.0 Python v2.7.8
答案 1 :(得分:0)
嗨,所以我在Windows 7上运行该程序,它的工作原理!我不确定它是否是操作系统问题,但它正在工作。因此,如果某人使其适用于Windows 8或10,请发表评论。我浪费了很多时间来调试这个,并且无法让它在这两个操作系统上运行。好吧无论如何,谢天谢地让它工作,谢谢你dizcza你的回复。这真的很有帮助。我做了你的投票,但由于我的低声誉而没有出现。
答案 2 :(得分:0)
pip安装opencv-python
从这里:https://pypi.org/project/opencv-python/为我修复了它。
Windows 10,Python 3.6,Kivy 1.11.1
答案 3 :(得分:0)
如果您正在使用anaconda spyder应用程序运行此脚本,那么这就是您收到该错误的真正原因(我有同样的错误)。尝试使用安装了kivy软件包的其他编辑器(尝试Pycharm)
答案 4 :(得分: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!')