Search code examples
pythonopencvwxpythonvideo-captureraspberry-pi2

cv2.VideoCapture doesn't return frames


I am trying to save a webcam video with wx Buttons. This is my code

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)

But it prints Capture Failed until i close the python shell myself. So, i guess capture.read() doesn't returns frames. What might be the reason?

How can i make it work? Hope for some expert advice :)


Solution

  • Try initialize counter before reading the capture for Eg:

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