-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_video.py
36 lines (31 loc) · 1.17 KB
/
generate_video.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import cv2
import numpy as np
import torch
import torchvision.transforms as transforms
unloader = transforms.ToPILImage()
def tensor_to_video(data, output_file):
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
video = cv2.VideoWriter(output_file, fourcc, 8.0, (112, 112))
data = np.uint8(data.permute(0, 2, 3, 1).cpu().numpy() * 255)
for i in range(16):
frame = cv2.cvtColor(data[i], cv2.COLOR_BGR2RGB)
video.write(frame)
cv2.destroyAllWindows()
video.release()
def npy_to_video(data, output_file):
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
video = cv2.VideoWriter(output_file, fourcc, 8.0, (112, 112))
data = np.uint8(data.transpose(0, 2, 3, 1) * 255)
for i in range(16):
frame = cv2.cvtColor(data[i], cv2.COLOR_BGR2RGB)
video.write(frame)
cv2.destroyAllWindows()
video.release()
def npy_to_png(data):
vid = torch.from_numpy(np.load(data))
vid = vid.permute(0, 3, 1, 2) / 255
for i in range(16):
image = vid[i, :, :, :].cuda().clone() # clone the tensor
image = image.squeeze(0) # remove the fake batch dimension
image = unloader(image)
image.save('test/' + str(i) + '.png')