OpenCV write frame to file python

…衆ロ難τιáo~ 提交于 2019-12-11 05:05:14

问题


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()

回答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




回答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




回答3:


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()


来源:https://stackoverflow.com/questions/26452909/opencv-write-frame-to-file-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!