Skip to content Skip to sidebar Skip to footer

Opencv Write Frame To File Python

Hey so im starting to play around with OpenCV and I cant get my webcam output saved to a file. Here is what I have. This runs fine, launches the webcam and creates 'output.avi' The

Solution 1:

Just need to change

fourcc = cv2.cv.CV_FOURCC(*'XVID')

to

fourcc = cv2.cv.CV_FOURCC('m', 'p', '4', 'v')

Found answer here: opencv VideoWriter under OSX producing no output

Solution 2:

Organize your code into class and separate clear functions, find several functions for saving your results in API OpenCV, try other formats and run your code on several OS.

You can also turn to C++ or a Java/C# with OpenCV

I guess there is a chapter on your problem in a Computer Vison book http://www.amazon.com/s/ref=nb_sb_noss_1?url=search-alias%3Dstripbooks&field-keywords=Cassandra%20NoSQL#/ref=nb_sb_noss_2?url=search-alias%3Dstripbooks&field-keywords=python+computer+vision+open+cv&rh=n%3A283155%2Ck%3Apython+computer+vision+open+cv

That is all I could for helping you

Solution 3:

The main problem is that you are not coding safely:

path = ('/full/path/Directory/output.avi')

cap = cv2.VideoCapture(0)
ifnot cap:
    print"!!! Failed VideoCapture: invalid parameter!"
    sys.exit(1)

cap.set(1, 20.0) #Match fps
cap.set(3,640)   #Match width
cap.set(4,480)   #Match height# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')

video_writer = cv2.VideoWriter(path, fourcc, 20.0, (640,480))
ifnot video_writer :
    print"!!! Failed VideoWriter: invalid parameters"
    sys.exit(1)

# ...

So when VideoCapture() or VideoWriter() fails, the program knows immediately that it can't go on.

Also, notice how the legacy cv2.cv.CV_FOURCC() call is replaced by the cv2.VideoWriter_fourcc(). I did this because this page shows up-to-date samples on how to do this stuff with Python. You could also try all the FourCC codes until you find one that works in your system.

Another important thing to realize is that setting the frame size of the capture interface may not work simply because the camera might not support that resolution. The same can be said for the FPS. Why is this a problem? Since we need to define these settings in the VideoWriter constructor, all frames sent to this object must have that exact dimension, else the writer won't be able to write the frames to the file.

This is how you should go about this:

path = ('/full/path/Directory/output.avi')

cap = cv2.VideoCapture(0)
ifnot cap:
    print"!!! Failed VideoCapture: invalid parameter!"
    sys.exit(1)

# The following might fail if the device doesn't support these values
cap.set(1, 20.0) #Match fps
cap.set(3,640)   #Match width
cap.set(4,480)   #Match height# So it's always safer to retrieve it afterwards
fps = cap.get(CV_CAP_PROP_FPS)
w = cap.get(CV_CAP_PROP_FRAME_WIDTH);
h = cap.get(CV_CAP_PROP_FRAME_HEIGHT);

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')

video_writer = cv2.VideoWriter(path, fourcc, fps, (w, h))
ifnot video_writer :
    print"!!! Failed VideoWriter: invalid parameters"
    sys.exit(1)

while (cap.isOpened()):
    ret, frame = cap.read()
    if ret == False:
         break

    cv2.imshow('frame',frame)         
    video_writer.write(frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
         break

cap.release()
video_writer.release()

Post a Comment for "Opencv Write Frame To File Python"