Python 3.x 无法使用opencv读取相机捕获

Python 3.x 无法使用opencv读取相机捕获,python-3.x,opencv,video-capture,Python 3.x,Opencv,Video Capture,我有以下代码: import numpy as np import cv2 cap = cv2.VideoCapture(0) while(True): # Capture frame-by-frame ret, frame = cap.read() if ret: # Our operations on the frame come here gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY

我有以下代码:

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    if ret:
        # Our operations on the frame come here
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        # Display the resulting frame
        cv2.imshow('frame',gray)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        print(ret)

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
这是打开网络摄像头的示例代码。几个月前,我可以毫无问题地运行它,而且QuickTime的网络摄像头工作正常

但是现在,cap.read()总是返回False,不管发生什么。 我在VideoCapture()尝试使用不同的数字,从-1到3,没有一个有效

我很确定这不是代码的错。但一定是环境出了问题。 目前我正在使用python 3.5,打开cv 3.1.0 这个python解释器是/usr/local/ceral/python3/


您对如何修复此问题有何建议?

重建并安装opencv修复了此问题