-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstyle_video.py
350 lines (278 loc) · 11.7 KB
/
style_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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import functools
import io
import os
import time
import cv2
import numpy as np
import tensorflow as tf
import tensorflow_hub as hub
from PIL import Image
# VIDEO PROCESSING =====================================================
def slice_frames(video_file):
""" Slices a video into its frames and saves the result in test_frames/
Args
video_file (str): path name of video to slice up
Output
Returns the number of frames in the video
"""
cap = cv2.VideoCapture(video_file)
idx = 0
framecount = 0
frame_skip = 0
while cap.isOpened():
ret, frame = cap.read()
if ret:
if idx == frame_skip:
filename = "test_frames/testframe" + str(framecount) + ".jpg"
cv2.imwrite(filename, frame)
framecount += 1
idx = 0
else:
idx += 1
else:
break
cap.release()
cv2.destroyAllWindows()
return framecount
def combine_frames(frame_paths, output_video_folder, output_filename):
"""images -> frames"""
frame = cv2.imread(frame_paths[0])
height, width, layers = frame.shape
fourcc_codec = cv2.VideoWriter_fourcc(*"avc1")
fps = 25
output_path = os.path.join(output_video_folder, output_filename)
video = cv2.VideoWriter(output_path, fourcc_codec, fps, (width, height))
for frame_path in frame_paths:
frame = cv2.imread(frame_path)
video.write(frame)
cv2.destroyAllWindows()
video.release()
return output_path
# /VIDEO PROCESSING ==========================================================
def crop_center(image):
"""Returns a cropped square image."""
shape = image.shape
new_shape = min(shape[1], shape[2])
offset_y = max(shape[1] - shape[2], 0) // 2
offset_x = max(shape[2] - shape[1], 0) // 2
image = tf.image.crop_to_bounding_box(
image, offset_y, offset_x, new_shape, new_shape
)
return image
@functools.lru_cache(maxsize=None)
def load_image(image_path, image_size=(256, 256), preserve_aspect_ratio=True):
"""Loads and preprocesses images."""
# deal with possible RGBA vs RGB issues
png = Image.open(image_path).convert("RGBA")
background = Image.new("RGBA", png.size, (255, 255, 255))
img = Image.alpha_composite(background, png).convert("RGB")
img = np.array([np.asarray(img)])
if img.max() > 1.0:
img = img / 255.0
if len(img.shape) == 3:
img = tf.stack([img, img, img], axis=-1)
img = crop_center(img)
img = tf.image.resize(img, image_size, preserve_aspect_ratio=True)
return img
def preprocesses_style_image(style_image_path=None):
style_img_size = (256, 256)
if not style_image_path:
style_image_url = "https://upload.wikimedia.org/wikipedia/commons/0/0a/The_Great_Wave_off_Kanagawa.jpg"
style_image_path = tf.keras.utils.get_file(
os.path.basename(style_image_url)[-128:], style_image_url
)
style_image = load_image(style_image_path, style_img_size)
style_image = tf.nn.avg_pool(
style_image, ksize=[3, 3], strides=[1, 1], padding="SAME"
)
return style_image
def get_image_path_from_url(image_url):
image_path = tf.keras.utils.get_file(os.path.basename(image_url)[-128:], image_url)
return image_path
def get_content_image_from_path(content_image_path):
output_image_size = 384
content_img_size = (output_image_size, output_image_size)
content_image = load_image(content_image_path, content_img_size)
return content_image
# TF Lite Functions
def run_style_transform(
style_bottleneck, preprocessed_content_image, style_transform_path
):
# Load the model.
interpreter = tf.lite.Interpreter(model_path=style_transform_path)
# Set model input.
input_details = interpreter.get_input_details()
interpreter.allocate_tensors()
# Set model inputs.
interpreter.set_tensor(input_details[0]["index"], preprocessed_content_image)
interpreter.set_tensor(input_details[1]["index"], style_bottleneck)
interpreter.invoke()
# Transform content image.
stylized_image = interpreter.tensor(interpreter.get_output_details()[0]["index"])()
return stylized_image
# Function to run style prediction on preprocessed style image.
def run_style_predict(preprocessed_style_image, style_predict_path):
# Load the model.
interpreter = tf.lite.Interpreter(model_path=style_predict_path)
# Set model input.
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
interpreter.set_tensor(input_details[0]["index"], preprocessed_style_image)
# Calculate style bottleneck.
interpreter.invoke()
style_bottleneck = interpreter.tensor(
interpreter.get_output_details()[0]["index"]
)()
return style_bottleneck
def get_style_transfer(
content_image, nframe, style_image, use_tflite=False, send_image=False
):
"""
Performs style transfer on a content image and style image. This function is intended for use when styling one single image,
not a video. If send_image is True, will return the pillow image object itself, otherwise will save to output_frames.
"""
try:
fin = open("path_info.txt", "r+")
path = fin.readline().strip()
if len(path) > 0:
os.environ["TFHUB_CACHE_DIR"] = path # Any folder that you can access
except:
pass
if use_tflite:
style_predict_path = tf.keras.utils.get_file(
"style_predict.tflite",
"https://tfhub.dev/google/lite-model/magenta/arbitrary-image-stylization-v1-256/int8/prediction/1?lite-format=tflite",
)
style_transform_path = tf.keras.utils.get_file(
"style_transform.tflite",
"https://tfhub.dev/google/lite-model/magenta/arbitrary-image-stylization-v1-256/int8/transfer/1?lite-format=tflite",
)
start_time = time.time()
style_bottleneck = run_style_predict(style_image, style_predict_path)
print(f"Style prediction time is {time.time() - start_time}")
start_time = time.time()
stylized_image = run_style_transform(
style_bottleneck, content_image, style_transform_path
)
print(f"Style transfer time is {time.time() - start_time}")
img = tf.keras.preprocessing.image.array_to_img(
tf.squeeze(stylized_image).numpy(), data_format=None, scale=True, dtype=None
)
else:
hub_handle = (
"https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2"
)
hub_module = hub.load(hub_handle)
outputs = hub_module(tf.constant(content_image), tf.constant(style_image))
stylized_image = outputs[0]
img = tf.keras.preprocessing.image.array_to_img(
tf.squeeze(stylized_image).numpy(), data_format=None, scale=True, dtype=None
)
# write PNG in file-object
if not send_image:
img.save("output_frames/outputframe" + str(nframe) + ".jpg")
# img.save("TEST.jpg")
else:
return img
def style_transfer_video_lite(n_frames, style_image_path=None):
"""
Video style transfer for given number of frames using tf.lite version of the model.
Args
- n_frames (int): number of frames to style from test frames
"""
style_predict_path = tf.keras.utils.get_file(
"style_predict.tflite",
"https://tfhub.dev/google/lite-model/magenta/arbitrary-image-stylization-v1-256/int8/prediction/1?lite-format=tflite",
)
style_transform_path = tf.keras.utils.get_file(
"style_transform.tflite",
"https://tfhub.dev/google/lite-model/magenta/arbitrary-image-stylization-v1-256/int8/transfer/1?lite-format=tflite",
)
start_time = time.time()
style_image = preprocesses_style_image(style_image_path=style_image_path)
style_bottleneck = run_style_predict(style_image, style_predict_path)
print(f"Style prediction time is {time.time() - start_time}")
print(style_bottleneck)
frame_paths = []
for i in range(n_frames):
content_path = f"test_frames/testframe{i}.jpg"
content_image = tf.convert_to_tensor(
get_content_image_from_path(content_path), np.float32
)
print(f"on iteration {i}")
interpreter = tf.lite.Interpreter(model_path=style_transform_path)
# Set model input.
input_details = interpreter.get_input_details()
interpreter.allocate_tensors()
# print(input_details)
start_time = time.time()
# Set model inputs.
interpreter.set_tensor(input_details[0]["index"], content_image)
interpreter.set_tensor(input_details[1]["index"], style_bottleneck)
interpreter.invoke()
# Transform content image.
stylized_image = interpreter.tensor(
interpreter.get_output_details()[0]["index"]
)()
print(f"Transfer time for frame {i} is {time.time() - start_time}")
interpreter.reset_all_variables()
img = tf.keras.preprocessing.image.array_to_img(
tf.squeeze(stylized_image).numpy(), data_format=None, scale=True, dtype=None
)
img.save(f"output_frames/outputframe{i}.jpg")
frame_paths.append(f"output_frames/outputframe{i}.jpg")
# print(f"Frame {i} completed")
return frame_paths
def style_transfer_video(n_frames, style_image_path=None):
"""
Video style transfer for given number of frames using magenta model
Args
- n_frames (int): number of frames to style from test frames
"""
style_image = preprocesses_style_image(style_image_path=style_image_path)
start_time = time.time()
hub_handle = "https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2"
hub_module = hub.load(hub_handle)
print(f"Time to load hub module {time.time() - start_time}")
frame_paths = []
for i in range(n_frames):
content_path = f"test_frames/testframe{i}.jpg"
content_image = get_content_image_from_path(content_path)
outputs = hub_module(tf.constant(content_image), tf.constant(style_image))
stylized_image = outputs[0]
img = tf.keras.preprocessing.image.array_to_img(
tf.squeeze(stylized_image).numpy(), data_format=None, scale=True, dtype=None
)
img.save(f"output_frames/outputframe{i}.jpg")
frame_paths.append(f"output_frames/outputframe{i}.jpg")
# print(f"Frame {i} completed")
return frame_paths
def style_transfer_video_file(content_video_path, style_image_path, use_tflite):
"""
Video style transfer for a given content video file fname
Args
- content_video_path (str): filepath of content video
- style_image_path (str): filepath of style image
"""
output_folder = "output_videos"
video_filename = "output.mp4"
start_time = time.time()
n_frames = slice_frames(content_video_path)
print(f"Time to slice up {time.time() - start_time} for {n_frames} frames")
start_time = time.time()
if use_tflite:
frame_paths = style_transfer_video_lite(
n_frames, style_image_path=style_image_path
)
else:
frame_paths = style_transfer_video(n_frames, style_image_path=style_image_path)
print(f"Time to style transfer {time.time() - start_time}")
start_time = time.time()
output_path = combine_frames(frame_paths, output_folder, video_filename)
print(f"Time to combine {time.time() - start_time}")
return video_filename
if __name__ == "__main__":
# frame_paths = [f"output_frames/outputframe{i}.jpg" for i in range(50)]
# combine_frames(frame_paths, "output_videos", "output.mp4")
style_transfer_video_lite(48, style_image_path="static/udnie.jpg")
# style_transfer_video_file("trimmed.mp4", "static/udnie.jpg")