OpenCV write frame to file python

2019-08-08 17:40发布

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 issue is output.avi is tiny(414 bytes) and the same exact bytes each time I run the program. Im guessing the issue is with the fourcc encoding but I havent been able to find what works in my case. I am running on Mac OS X. Let me know if you need anymore information.

import numpy as np
import cv2

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

cap = cv2.VideoCapture(0)
cap.set(1, 20.0) #Match fps
cap.set(3,640)   #Match width
cap.set(4,480)   #Match height

fourcc = cv2.cv.CV_FOURCC(*'XVID')
video_writer = cv2.VideoWriter(path,fourcc, 20.0, (640,480))

while(cap.isOpened()):
    #read the frame
    ret, frame = cap.read()
    if ret==True:
        #show the frame
        cv2.imshow('frame',frame)
        #Write the frame
        video_writer.write(frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# Release everything if job is finished
cap.release()
video_writer.release()
cv2.destroyAllWindows()

3条回答
Animai°情兽
2楼-- · 2019-08-08 17:52

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

查看更多
我想做一个坏孩纸
3楼-- · 2019-08-08 17:53

The main problem is that you are not coding safely:

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

cap = cv2.VideoCapture(0)
if not 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))
if not 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)
if not 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))
if not 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()
查看更多
你好瞎i
4楼-- · 2019-08-08 17:57

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

查看更多
登录 后发表回答