forked from axinc-ai/ailia-models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
film.py
289 lines (226 loc) · 8.84 KB
/
film.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import sys
import os
import shutil
import time
import numpy as np
import cv2
import ailia
# import original modules
sys.path.append('../../util')
from arg_utils import get_base_parser, update_parser, get_savepath # noqa
from model_utils import check_and_download_models # noqa
from detector_utils import load_image # noqa
from webcamera_utils import get_capture, get_writer # noqa
# logger
from logging import getLogger # noqa
logger = getLogger(__name__)
# ======================
# Parameters
# ======================
WEIGHT_PATH = 'film_net.onnx'
MODEL_PATH = 'film_net.onnx.prototxt'
REMOTE_PATH = 'https://storage.googleapis.com/ailia-models/film/'
IMAGE_PATH = 'photos'
SAVE_IMAGE_PATH = 'output.png'
NM_EXT = os.path.splitext(SAVE_IMAGE_PATH)
# ======================
# Arguemnt Parser Config
# ======================
parser = get_base_parser(
'FILM: Frame Interpolation for Large Motion', IMAGE_PATH, SAVE_IMAGE_PATH
)
parser.add_argument(
'-i2', '--input2', metavar='IMAGE2', default=None,
help='The second input image path.'
)
parser.add_argument(
'-it', '--interpolate-times', type=int, default=1,
help='The number of times to run recursive midpoint interpolation. '
'The number of output frames will be 2^times_to_interpolate-1.'
)
parser.add_argument(
'--onnx',
action='store_true',
help='execute onnxruntime version.'
)
args = update_parser(parser)
# ======================
# Main functions
# ======================
def preprocess(img):
h, w, _ = img.shape
align = 32
pad_h = pad_w = 0
if h % align != 0 or w % align != 0:
pad_h = (align - h % align) % align
pad_w = (align - w % align) % align
pad_img = np.zeros(shape=(h + pad_h, w + pad_w, 3))
pad_h = pad_h // 2
pad_w = pad_w // 2
pad_img[pad_h:pad_h + h, pad_w:pad_w + w, :] = img
#for x in range(0, pad_w):
# pad_img[:, x, :] = pad_img[:, pad_w, :]
# pad_img[:, pad_w + w + x, :] = pad_img[:, pad_w + w - 1, :]
img = pad_img
img = img / 255
img = np.expand_dims(img, axis=0)
img = img.astype(np.float32)
return img, (pad_h, pad_w)
def predict(net, img1, img2):
h, w, _ = img1.shape
img1 = img1[:, :, ::-1] # BGR -> RGB
img2 = img2[:, :, ::-1] # BGR -> RGB
x0, pad_hw = preprocess(img1)
x1, _ = preprocess(img2)
batch_dt = np.full(shape=(1,), fill_value=0.5, dtype=np.float32)
# feedforward
if not args.onnx:
output = net.predict([batch_dt[..., np.newaxis], x0, x1])
else:
output = net.run(None, {'time': batch_dt[..., np.newaxis], 'x0': x0, 'x1': x1})
mid_img = output[24]
mid_img = np.clip(mid_img[0] * 255, 0, 255)
mid_img = (mid_img + 0.5).astype(np.uint8)
mid_img = mid_img[:, :, ::-1] # RGB -> BGR
pad_h, pad_w = pad_hw
if pad_h or pad_w:
mid_img = mid_img[pad_h:pad_h + h, pad_w:pad_w + w, ...]
return mid_img
def recursive_interpolate(net, img1, img2, num_recursions, no=0, offset=0):
if 0 < num_recursions:
mid_img = predict(net, img1, img2)
save_file = "%s_%03d%s" % (NM_EXT[0], offset + (no + 1) * (2 ** (num_recursions - 1)), NM_EXT[1])
save_path = get_savepath(args.savepath, save_file, post_fix='', ext='.png')
logger.info(f'saved at : {save_path}')
cv2.imwrite(save_path, mid_img)
recursive_interpolate(net, img1, mid_img, num_recursions - 1, no=no * 2, offset=offset)
recursive_interpolate(net, mid_img, img2, num_recursions - 1, no=no * 2 + 2, offset=offset)
def recognize_from_image(net):
inputs = args.input
times_to_interpolate = args.interpolate_times
# Load images
n_input = len(inputs)
if n_input == 1 and args.input2:
inputs.extend([args.input2])
if len(inputs) < 2:
logger.error("Specified input must be at least two or more images")
sys.exit(-1)
no = 0
for image_paths in zip(inputs, inputs[1:]):
logger.info(image_paths)
# prepare input data
images = [load_image(p) for p in image_paths]
img1, img2 = [cv2.cvtColor(im, cv2.COLOR_BGRA2BGR) for im in images]
# inference
logger.info('Start inference...')
if args.benchmark:
logger.info('BENCHMARK mode')
total_time_estimation = 0
for i in range(args.benchmark_count):
start = int(round(time.time() * 1000))
out_img = predict(net, img1, img2)
end = int(round(time.time() * 1000))
estimation_time = (end - start)
# Logging
logger.info(f'\tailia processing estimation time {estimation_time} ms')
if i != 0:
total_time_estimation = total_time_estimation + estimation_time
logger.info(f'\taverage time estimation {total_time_estimation / (args.benchmark_count - 1)} ms')
save_file = "%s_%s%s" % (NM_EXT[0], no, NM_EXT[1])
save_path = get_savepath(args.savepath, save_file, post_fix='', ext='.png')
logger.info(f'saved at : {save_path}')
cv2.imwrite(save_path, out_img)
else:
recursive_interpolate(net, img1, img2, times_to_interpolate, offset=no)
no += 2 ** times_to_interpolate
if image_paths[-1] != inputs[-1]:
save_file = "%s_%03d%s" % (NM_EXT[0], no, NM_EXT[1])
save_path = get_savepath(args.savepath, save_file, post_fix='', ext='.png')
logger.info(f'copy {image_paths[-1]} -> {save_path}')
shutil.copy(image_paths[-1], save_path)
no += 1
logger.info('Script finished successfully.')
def recognize_from_video(net):
video_file = args.video if args.video else args.input[0]
capture = get_capture(video_file)
assert capture.isOpened(), 'Cannot capture source'
video_length = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
if 0 < video_length:
logger.info(f"video_length: {video_length}")
# create video writer if savepath is specified as video format
fps = int(capture.get(cv2.CAP_PROP_FPS))
f_h = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
f_w = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
writer = None
if args.savepath != SAVE_IMAGE_PATH:
writer = get_writer(args.savepath, f_h, f_w, fps=fps)
# create output buffer
n_output = 1
output_buffer = np.zeros((f_h * (n_output + 2), f_w, 3))
output_buffer = output_buffer.astype(np.uint8)
images = []
it = None
try:
import tqdm
if 0 < video_length:
it = iter(tqdm.tqdm(range(video_length)))
next(it)
except ImportError:
pass
frame_shown = False
while True:
if it and 0 < video_length:
try:
next(it)
except StopIteration:
break
ret, frame = capture.read()
if (cv2.waitKey(1) & 0xFF == ord('q')) or not ret:
break
if frame_shown and cv2.getWindowProperty('frame', cv2.WND_PROP_VISIBLE) == 0:
break
# set inputs
images.append(cv2.resize(frame, (f_w, f_h)))
if len(images) < 2:
continue
elif len(images) > 2:
images = images[1:]
# inference
img1, img2 = images
out_img = predict(net, img1, img2)
output_buffer[:f_h, :f_w, :] = images[0]
output_buffer[f_h * 1:f_h * 2, :f_w, :] = out_img
output_buffer[f_h * 2:f_h * 3, :f_w, :] = images[1]
# preview
cv2.imshow('frame', output_buffer)
frame_shown = True
# save results
if writer is not None:
writer.write(images[0])
writer.write(out_img)
capture.release()
cv2.destroyAllWindows()
if writer is not None:
writer.release()
logger.info('Script finished successfully.')
def main():
# model files check and download
check_and_download_models(WEIGHT_PATH, MODEL_PATH, REMOTE_PATH)
env_id = args.env_id
# initialize
if not args.onnx:
logger.info("This model requires 10GB or more memory.")
memory_mode = ailia.get_memory_mode(
reduce_constant=True, ignore_input_with_initializer=True,
reduce_interstage=True, reuse_interstage=True)
# memory_mode = None
net = ailia.Net(MODEL_PATH, WEIGHT_PATH, env_id=env_id, memory_mode=memory_mode)
else:
import onnxruntime
net = onnxruntime.InferenceSession(WEIGHT_PATH)
if args.video is not None:
recognize_from_video(net)
else:
recognize_from_image(net)
if __name__ == '__main__':
main()