Python/Kivy相机小部件打开时出错

2024-09-29 19:25:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试制作一个可以打开设备摄像头的应用程序,但遇到以下错误:

    [CRITICAL          ] [Camera      ] Unable to find any valuable Camera provider at all!
videocapture - ImportError: No module named VideoCapture
  File "C:\Users\Gaston\Downloads\Kivy-1.9.0-py2.7-win32-x86\kivy27\kivy\core\__init__.py", line 57, in core_select_lib
    fromlist=[modulename], level=0)
  File "C:\Users\Gaston\Downloads\Kivy-1.9.0-py2.7-win32-x86\kivy27\kivy\core\camera\camera_videocapture.py", line 15, in <module>
    from VideoCapture import Device

opencv - ImportError: No module named cv
  File "C:\Users\Gaston\Downloads\Kivy-1.9.0-py2.7-win32-x86\kivy27\kivy\core\__init__.py", line 57, in core_select_lib
    fromlist=[modulename], level=0)
  File "C:\Users\Gaston\Downloads\Kivy-1.9.0-py2.7-win32-x86\kivy27\kivy\core\camera\camera_opencv.py", line 20, in <module>
    import cv

尝试安装opencv,但仍然无法工作。我的代码只是kivy文档中的代码示例: http://kivy.org/docs/examples/gen__camera__main__py.html


Tags: inpycoredownloadslineusersx86file
1条回答
网友
1楼 · 发布于 2024-09-29 19:25:54

很抱歉,我没有足够的声誉来评论,但我相信你应该检查你是否有任何图书馆可以支持相机。我相信你是在进口cv而不是cv2。检查如果问题仍然存在,请检查文件位置是否安装了必要的库。我从网上得到这个密码。在我的电脑上运行可能会对你有所帮助:

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.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.widget import Widget
from kivy.uix.button import Button

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(1)
        self.my_camera = KivyCamera(capture=self.capture, fps=10,resolution=(1280,960))

        return self.my_camera

    def on_stop(self):
        #without this, app will not exit even if the window is closed
        self.capture.release()


if __name__ == '__main__':
    CamApp().run()

相关问题 更多 >

    热门问题