I'm trying to use VideoWriter
to save the retrieved frames as video file but it seems it does not work. I have checked the while loop and print the output is working. I don't know why the output of the video is only O kb.
def detect_video(self, path, output_path):
# Set output video writer with codec
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_path, fourcc, 25.0, (1920, 1080))
# Read the video
vidcap = cv2.VideoCapture(path)
frame_read, image = vidcap.read()
count = 0
# Iterate over frames and pass each for prediction
while frame_read:
# Perform object detection and add to output file
output_file = self.detect(image)
#print(output_file)
# Write frame with predictions to video
out.write(output_file)
# Read next frame
frame_read, image = vidcap.read()
count = 1
#print(count)
# Release video file when we're ready
out.release()
CodePudding user response:
It seems you didn't ask the question correctly. It has nothing to do with either TensorFlow or Object Detection. Unfortunately I got this from the comments, not from the question.
As for the problem, everything is okay but the resolution. Make sure the frame width is 1920 and the height is 1080. Otherwise, of course the video size will be 0 kb. If you've passed 1920x1080 to cv2.VideoWriter
as output resolution, the frame size you get should also match. So, you should either resize the
retrieved frame:
width = 1920
height = 1080
output_size = (width, height)
out.write(cv2.resize(output_file, output_size ))
or use these to get the frame width and height:
width = vidcap.get(cv2.CAP_PROP_FRAME_WIDTH )
height = vidcap.get(cv2.CAP_PROP_FRAME_HEIGHT )
fps = vidcap.get(cv2.CAP_PROP_FPS)
cv2.VideoWriter(output_path, fourcc, fps, (width, height))
P.S. Video size can not be changed while writing the video!