capture not remaining open

asked 2016-07-11 16:04:14 -0600

I'm going through the tutorial code to get a good grip on the opencv functionality. I've run into a bit of an issue with my capture apparently not wanting to remain open. The code I'm using has been lifted from the tutorial pages at: link:http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_video_display/py_video_display.html#exercises.

I get a single frame showing up in the output video, but as far as I can tell the code should just continue to capture.

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,0)

        # write the flipped frame
        out.write(frame)

        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break
cap.release()
out.release()
cv2.destroyAllWindows()

Ideas or hints about possible solutions would be appreciated. Running Ubuntu 16.xx.

edit retag flag offensive close merge delete

Comments

Hmm for me your code works fine. Maybe your cap.read() returns False for ret . This would break your while-loop and end the program. You could try to change the break in your else-path to pass

Missing gravatar imageMissing ( 2016-07-12 02:03:53 -0600 )edit

Finally got back to looking at this and found the problem right away. For some reason the bottom three lines were tabbed in and were being included in the while-loop. In my code I have a comment between the loop and those three; I must have tabbed after the comment. Thanks for taking a look anyway.

Exponential_Sinusoid gravatar imageExponential_Sinusoid ( 2016-08-15 12:10:04 -0600 )edit