我有一个根文件夹/ train_images,它有/ train_images / 1,/ train_images / 2等子文件夹。子文件夹的Ech中包含图像。如果num(images)> 4,我想制作与每个子文件夹相对应的视频。下面是我为此编写的代码。如果只有一个子文件夹,它会工作,并在创建第二个Videowriter对象时停止。请帮忙
import cv2
import os
root_folder = 'train_images'
video_folder = 'output_video'
folders = [folder for folder in os.listdir(root_folder)]
for folder in folders:
child = os.path.join(root_folder,folder)
images = [img for img in os.listdir(child) if img.endswith(".jpg")]
print('%d images in the path %s' % (len(images),child))
if(len(images)>=4):
video_path ='{}.avi'.format(folder)
print(video_path)
print(os.path.join(video_folder, video_path))
video = cv2.VideoWriter(os.path.join(video_folder, video_path), -1, 5, (224, 320))
for img in images:
frame = cv2.imread(os.path.join(child, img))
print('writing from %s' %(os.path.join(child, img)) )
frame_new=cv2.resize(frame,(224,320))
#print(frame_new.shape)
video.write(frame_new)
cv2.destroyAllWindows()
video.release()