Spaces:
Runtime error
Runtime error
import numpy as np | |
import cv2 | |
from PIL import Image | |
import ffmpeg | |
def noise_process(numpy_image, steps=149): | |
noisy_image_list = [] | |
noisy_image = numpy_image | |
noisy_image_list.append(noisy_image) | |
for step in range(steps): | |
noise = 255 * np.random.normal(0, 0.07*(step+1) * 0.1, numpy_image.size).reshape(numpy_image.shape) | |
noisy_image = noisy_image + noise | |
noisy_image_list.append(noisy_image) | |
return noisy_image_list | |
def generate_video(numpy_image): | |
save_path = "result.mp4" | |
width = 256 | |
height = 256 | |
fps = 30 | |
sec = 5 | |
image_lst = noise_process(numpy_image) | |
image_lst = np.array([(i-np.min(i))/(np.max(i)-np.min(i)) for i in image_lst]) | |
image_lst = np.round(image_lst * 255).astype(np.uint8) | |
copies = int((sec * fps) / len(image_lst)) | |
spill_over = sec * fps - copies * len(image_lst) | |
image_lst = np.repeat(image_lst, copies, axis=0) | |
image_lst = np.concatenate((image_lst, image_lst[:spill_over]), axis=0) | |
image_lst = image_lst[::-1] | |
for i, img in enumerate(image_lst): | |
Image.fromarray(img).save(f"video/{i}.jpg") | |
ffmpeg.input('video/*.jpg', pattern_type='glob', framerate=25).output(save_path).run() | |
# fourcc = cv2.VideoWriter_fourcc(*'avc1') | |
# video = cv2.VideoWriter(save_path, fourcc, float(fps), (width, height)) | |
# for frame_count in range(fps * sec): | |
# img = np.expand_dims(image_lst[frame_count], 2) | |
# video.write(img.astype(np.uint8)) | |
# video.release() | |
return save_path | |