-
Notifications
You must be signed in to change notification settings - Fork 3
/
gen_vid.py
147 lines (103 loc) · 4.92 KB
/
gen_vid.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import argparse
import os
import sys
import cv2
import numpy as np
from PIL import Image
import torchvision.utils as vutils
import torch
import torchvision
def torch_to_cv2_image(image):
pil_image = torchvision.transforms.ToPILImage()(image)
open_cv_image = np.array(pil_image)
# Convert RGB to BGR
open_cv_image = open_cv_image[:, :, ::-1].copy()
return open_cv_image
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--img_path', type=str, default='./')
parser.add_argument('--name', type=str, default='vid.avi')
parser.add_argument('--ext_a', type=str, default='.jpg')
parser.add_argument('--ext_b', type=str, default='.jpg')
parser.add_argument('--out', type=str, default='./')
parser.add_argument('--prefix_a', type=str, default='a_')
parser.add_argument('--prefix_b', type=str, default='b_')
parser.add_argument('--prefix_c', type=str, default='a_')
parser.add_argument('--prefix_d', type=str, default='b_')
parser.add_argument('--start_a', type=int, default=0)
parser.add_argument('--end_a', type=int, default=10)
parser.add_argument('--start_b', type=int, default=0)
parser.add_argument('--end_b', type=int, default=10)
parser.add_argument('--stride_a', type=int, default=1)
parser.add_argument('--stride_b', type=int, default=1)
parser.add_argument('--w', type=int, default=256)
parser.add_argument('--h', type=int, default=256)
parser.add_argument('--fps', type=float, default=15.0)
parser.add_argument('--resize', dest='resize', action='store_true')
parser.set_defaults(resize=False)
parser.add_argument('--same_length', dest='same_length', action='store_true')
parser.set_defaults(same_length=False)
parser.add_argument('--crop', dest='crop', action='store_true')
parser.set_defaults(crop=False)
parser.add_argument('--crop_w', type=int, default=256)
parser.add_argument('--crop_h', type=int, default=256)
args = parser.parse_args()
return args
if __name__ == '__main__':
args = parse_args()
if not os.path.exists(args.out):
os.makedirs(args.out)
to_tensor = torchvision.transforms.ToTensor()
if args.same_length:
args.start_b = args.start_a
args.end_b = args.end_a
frame_array = []
for i, j in zip(range(args.start_a, args.end_a, args.stride_a), range(args.start_b, args.end_b, args.stride_b)):
print(i,j)
name_a = os.path.join(args.img_path, "%s%0d%s" % (args.prefix_a, i, args.ext_a))
name_b = os.path.join(args.img_path, "%s%0d%s" % (args.prefix_b, i, args.ext_a))
name_c = os.path.join(args.img_path, "%s%0d%s" % (args.prefix_c, j, args.ext_b))
name_d = os.path.join(args.img_path, "%s%0d%s" % (args.prefix_d, j, args.ext_b))
try:
img_a = Image.open(name_a)
img_b = Image.open(name_b)
img_c = Image.open(name_c)
img_d = Image.open(name_d)
except FileNotFoundError:
print("ERROR! ")
print(name_a, name_b, name_c, name_d)
continue
if args.crop:
width, height = img_a.size # Get dimensions
left = (width - args.crop_w) / 2
top = (height - args.crop_h) / 2
right = (width + args.crop_w) / 2
bottom = (height + args.crop_h) / 2
img_a = img_a.crop((left, top, right, bottom))
img_b = img_b.crop((left, top, right, bottom))
img_c = img_c.crop((left, top, right, bottom))
img_d = img_d.crop((left, top, right, bottom))
if args.resize:
img_a = img_a.resize((args.w, args.h))
img_b = img_b.resize((args.w, args.h))
img_c = img_c.resize((args.w, args.h))
img_d = img_d.resize((args.w, args.h))
img_a = to_tensor(img_a)
img_b = to_tensor(img_b)
img_c = to_tensor(img_c)
img_d = to_tensor(img_d)
frame1 = vutils.make_grid([img_a, img_b], normalize=False, nrow=1, pad_value=1, padding=0)
frame2 = vutils.make_grid([img_c, img_d], normalize=False, nrow=1, pad_value=1, padding=0)
frame = vutils.make_grid([frame1, frame2], normalize=False, pad_value=1, padding=24)
frame = torch_to_cv2_image(frame)
frame = cv2.copyMakeBorder(frame, 0, 0, 60, 0, borderType=cv2.BORDER_CONSTANT, value=[255, 255, 255])
cv2.putText(frame, 'Input', (15,int((frame.shape[0]) / 4 + 26)), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0,0,0), 2)
cv2.putText(frame, 'Ours', (15, int((frame.shape[0] * 3) / 4 )), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 0), 2)
frame_array.append(frame)
height, width, layers = frame.shape
size = (width, height)
out = cv2.VideoWriter(os.path.join(args.out, "%0d_%s" % (int(args.fps), args.name)), cv2.VideoWriter_fourcc(*'DIVX'), args.fps, size)
for i in range(len(frame_array)):
# writing to a image array
out.write(frame_array[i])
out.release()