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()
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
The main problem is that you are not coding safely:
So when
VideoCapture()
orVideoWriter()
fails, the program knows immediately that it can't go on.Also, notice how the legacy
cv2.cv.CV_FOURCC()
call is replaced by thecv2.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:
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