OpenCV,网络摄像头窗口无法打开

时间:2016-10-01 02:34:42

标签: python-2.7 opencv computer-vision

我对计算机视觉非常陌生,并且使用OpenCV库来实现一些基本功能,例如打开相机的窗口。我使用了OpenCV书中的代码,我从那里运行代码。部分如下所示:

def run(self):
        """Run the main loop"""
        self._windowManager.createWindow()
        while self._windowManager.isWindowCreated:
            self._captureManager.enterFrame()
            frame = self._captureManager.frame

            self._captureManager.exitFrame()
            self._windowManager.processEvents()

我收到以下错误:

'module' object has no attribute 'nameWindow'

这就是它所指的那条线:

    139     def createWindow (self):
    140            cv2.namedWindow(self._windowName)
--> 141            self._isWindowCreated = True
    142     def show(self, frame):
    143            cv2.imshow(self._windowName, frame)

有人可以帮助我做些什么吗?

1 个答案:

答案 0 :(得分:3)

很难从代码中说出问题是什么,但我认为cv2.namedWindow()不是nameWindow。另外,在cv2.waitKey(1)函数调用后添加imshow()

这是使用python和opencv打开网络摄像头的简单方法:

import cv2
video_capture = cv2.VideoCapture(0)

cv2.namedWindow("Window")

while True:
    ret, frame = video_capture.read()
    cv2.imshow("Window", frame)

    #This breaks on 'q' key
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

video_capture.release()
cv2.destroyAllWindows()