OpenCV网络摄像头问题

时间:2019-01-20 16:34:46

标签: python webcam opencv

每当我使用OpenCV尝试打开集成的笔记本电脑摄像头时,都会遇到错误。

例如:

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,resolution = [0,0])

if __name__== "__main__":
    MainApp().run()

给出错误:

 Traceback (most recent call last):
   File "C:/Users/dell/Desktop/KIVY TEST/camera.py", line 13, in <module>
     MainApp().run()
   File "C:\Program Files (x86)\Python36-32\lib\site-packages\kivy\app.py", line 800, in run
     root = self.build()
   File "C:/Users/dell/Desktop/KIVY TEST/camera.py", line 10, in build
     return Camera(play=True,resolution = [0,0])
   File "C:\Program Files (x86)\Python36-32\lib\site-packages\kivy\uix\camera.py", line 91, in __init__
     on_index()
   File "C:\Program Files (x86)\Python36-32\lib\site-packages\kivy\uix\camera.py", line 103, in _on_index
     resolution=self.resolution, stopped=True)
   File "C:\Program Files (x86)\Python36-32\lib\site-packages\kivy\core\camera\camera_opencv.py", line 67, in __init__
     super(CameraOpenCV, self).__init__(**kwargs)
   File "C:\Program Files (x86)\Python36-32\lib\site-packages\kivy\core\camera\__init__.py", line 70, in __init__
     self.init_camera()
   File "C:\Program Files (x86)\Python36-32\lib\site-packages\kivy\core\camera\camera_opencv.py", line 121, in init_camera
     if self.fps <= 0:
 AttributeError: 'CameraOpenCV' object has no attribute 'fps'

另一个例子是:

import cv2


def show_webcam(mirror=False):
    cam = cv2.VideoCapture(0)
    while True:
        ret_val, img = cam.read()
        if mirror:
            img = cv2.flip(img, 1)
        cv2.imshow('my webcam', img)
        if cv2.waitKey(1) == 27:
            break  # esc to quit
    cv2.destroyAllWindows()


def main():
    show_webcam(mirror=True)


if __name__ == '__main__':
    main()

出现错误:

[ WARN:0] terminating async callback
Traceback (most recent call last):
  File "C:/Users/dell/Desktop/KIVY TEST/camera.py", line 26, in <module>
    main()
  File "C:/Users/dell/Desktop/KIVY TEST/camera.py", line 22, in main
    show_webcam(mirror=True)
  File "C:/Users/dell/Desktop/KIVY TEST/camera.py", line 15, in show_webcam
    cv2.imshow('my webcam', img)
cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:350: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

我几乎可以确定这是我的特定笔记本电脑出现的问题,因为我使用的代码可以在网上为其他人使用。

如果您有任何想法如何对它进行排序,那将非常有用!

谢谢!

1 个答案:

答案 0 :(得分:1)

您正在运行OpenCV 4。github master branch中已包含与OpenCV 4的兼容性,但尚未通过pypi提供。尝试从github安装kivy:

pip install https://github.com/kivy/kivy/archive/master.zip