''' author : Hatcher W Email: hcwang@semi.ac.cn use: 将图像合成视频 input:图像的文件夹路径 output:视频路径 ''' import cv2,os import glob import numpy as np import time def images_to_video(InputPath, OutputPath): img_array = [] img_list = [os.path.join(InputPath,nm) for nm in os.listdir(InputPath) if 1] img_list.sort() i=0 for filename in img_list: print(i) i+=1 img = cv2.imread(filename) if i == 1: ImgHeight = np.shape(img)[0] ImgWidth = np.shape(img)[1] if img is None: print(filename + " is error!") continue img_array.append(img) # 图片的大小需要一致 #img_array, size = resize(img_array, 'largest') fps = 25 out = cv2.VideoWriter(OutputPath, cv2.VideoWriter_fourcc(*'DIVX'), fps, (ImgWidth,ImgHeight)) for i in range(len(img_array)): out.write(img_array[i]) out.release() def images_to_video_big(InputPath, OutputPath): img_array = [] img_list = [os.path.join(InputPath,nm) for nm in os.listdir(InputPath) if 1] img_list.sort() i=0 num_video = 0 for filename in img_list: print(i) i+=1 img = cv2.imread(filename) if i == 1: ImgHeight = np.shape(img)[0] ImgWidth = np.shape(img)[1] if img is None: print(filename + " is error!") continue img_array.append(img) if i % 30000 == 0: fps = 25 out = cv2.VideoWriter(OutputPath.replace("Detection.avi","Detection{}.avi".format(str(num_video))), cv2.VideoWriter_fourcc(*'DIVX'), fps, (ImgWidth, ImgHeight)) for j in range(len(img_array)): out.write(img_array[j]) out.release() img_array = [] num_video+=1 # 图片的大小需要一致 #img_array, size = resize(img_array, 'largest') fps = 25 out = cv2.VideoWriter(OutputPath.replace("Detection.avi","Detection{}.avi".format(str(num_video))), cv2.VideoWriter_fourcc(*'DIVX'), fps, (ImgWidth, ImgHeight)) for i in range(len(img_array)): out.write(img_array[i]) out.release() if __name__ == "__main__": T1 = time.time() images_to_video('../data_MPKFNet/result/images', '../data_MPKFNet/result_vedio/images.avi') T2 = time.time() print("chengxuyunxingshijian is ",(T2-T1)*1000)