I’m new in Python (2.7) and I try to work on video processing (with module openCv “cv2”). Starting with tutorials, I try to use the script of this tutorial : paragraph “Saving a video”. Everything works fine excepting that the video I’m saving is empty. I can find output.avi in my directory but its memory size is 0kb an, of course when I run it, no video is displayed.
After a few changes here is my code :
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
#fourcc = cv2.VideoWriter_fourcc(*'DIVX')
fourcc = cv2.cv.CV_FOURCC(*'DIVX')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
# write the flipped frame
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
Does anyone know why it is not working properly ?
Thanks a lot. Edwin
Advertisement
Answer
I never worked with openCV, but I bet the problem is in
cap = cv2.VideoCapture(0)
This is a C version of the VideoCapture method http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture
Maybe you can try to do the same. Something like
cap = cv2.VideoCapture(0)
if (not cap.isOpened()):
print "Error"
EDIT: just downloaded Python and OpenCV and discovered the problem was the codec. Try to change
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
for
out = cv2.VideoWriter('output.avi', -1, 20.0, (640,480))
and select the codec by hand.