Saving video capture in python using openCV: empty video

I am new to Python (2.7) and I try to work on video processing (with the openCv module "cv2"). Starting with the tutorials, I’m trying to use the script for this lesson : the “Save video” item. Everything works fine except that the video I am saving is empty . I can find output.avi in ​​my directory, but its memory size is 0kb , and of course, when I run it, the video is not 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?

Thank you very much. Edwin

+6
source share
2 answers

I have never worked with openCV, but I'm sure the problem is

 cap = cv2.VideoCapture(0) 

This is the 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. Sort of

 cap = cv2.VideoCapture(0) if (not cap.isOpened()): print "Error" 

EDIT: Just downloaded Python and OpenCV and found that the problem was in 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 manually.

+4
source

Can the output resolution differ from the input. Check the width and height of the cover.

 size = (int(cap.get(3)), int(cap.get(4))) 

Change either your camera or output resolution.

0
source

Source: https://habr.com/ru/post/976100/


All Articles