Files
MPKFNet/imatovideo.py
2026-07-17 18:55:31 +08:00

107 lines
3.2 KiB
Python
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'''
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_bigtosmall(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)
if (i % 1500)==0:
# 图片的大小需要一致
#img_array, size = resize(img_array, 'largest')
fps = 25
out = cv2.VideoWriter(OutputPath.replace("Detectionnew1","Detectionnew1{}".format(str(i))), cv2.VideoWriter_fourcc(*'DIVX'), fps, (ImgWidth,ImgHeight))
for j in range(len(img_array)):
out.write(img_array[j])
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('/data3/DEMO/images/1/', '/data3/DEMO/images/test.avi')
T2 = time.time()
print("chengxuyunxingshijian is ",(T2-T1)*1000)