cv2.VideoCapture不返回帧

时间:2016-07-19 09:33:52

标签: python opencv wxpython video-capture raspberry-pi2

我正在尝试使用wx按钮保存网络摄像头视频。 这是我的代码

def OnRecord(self, evt):
    capture = cv2.VideoCapture(0)
    if (not capture.isOpened()):
       print "Error"

    # video recorder
    fourcc = cv2.cv.CV_FOURCC('D', 'I', 'V', 'X')  # cv2.VideoWriter_fourcc() does not exist
    out = cv2.VideoWriter("output.avi", fourcc, 9.0, (640, 480), True)

    # record video
    while (capture.isOpened()):
        ret, frame = capture.read()
        if not ret:
            print "Capture Failed"
        else:
            out.write(frame)
            cv2.imshow('Video', frame)

但它打印Capture Failed直到我自己关闭python shell。所以,我猜capture.read()不会返回帧。可能是什么原因?

我怎样才能让它发挥作用?希望得到一些专家意见:)

1 个答案:

答案 0 :(得分:0)

在阅读捕获之前尝试初始化计数器 对于Eg:

i = 0
while i < 0:
     ret, frame = capture.read()
     if ret:
          out.write(frame)
     else:
          print "Error"