I'm trying to convert frames stored in a folder into a video file. I'm using open cv2.VideoWriter() to do this, it doesn't throw any error but the files produced are empty. I was trying to change fourcc and frames per second but nothing works. Would you have a look at teh code and suggest what may cause this issue?
import cv2
import numpy as np
import os
from os.path import isfile, join
import re
import natsort
def convert_frames_to_video(pathIn,pathOut,fps):
frame_array = []
files = [f for f in os.listdir(pathIn) if isfile(join(pathIn, f))]
#for sorting the file names properly
natsort.natsorted(files,reverse=True)
#files.sort(key=lambda f: int(re.sub('\D', '', f)))
for i in range(len(files)):
filename=pathIn + files[i]
#reading each files
img = cv2.imread(filename)
height = 407
width = 724
layers = 3
#height, width, layers = cv2.imread(img).shape
size = (width,height)
print(filename)
#inserting the frames into an image array
frame_array.append(img)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(pathOut,fourcc, fps, size)
for i in range(len(frame_array)):
# writing to a image array
out.write(frame_array[i])
out.release()
def main():
pathIn= '/content/CarND-LaneLines-P1/White Right Detected'
pathOut = '/content/CarND-LaneLines-P1/vid4.mp4'
fps = 8.0
convert_frames_to_video(pathIn, pathOut, fps)
if __name__=="__main__":
main()
Files stored in the folder are all .jpg format with names like: frame1, frame2 etc.