Spaces:
Runtime error
Runtime error
Delete wbc/saved_models/cartoonize.py
Browse files- wbc/saved_models/cartoonize.py +0 -112
wbc/saved_models/cartoonize.py
DELETED
|
@@ -1,112 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import cv2
|
| 3 |
-
import numpy as np
|
| 4 |
-
import tensorflow as tf
|
| 5 |
-
import wbc.network as network
|
| 6 |
-
import wbc.guided_filter as guided_filter
|
| 7 |
-
from tqdm import tqdm
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
def resize_crop(image):
|
| 11 |
-
h, w, c = np.shape(image)
|
| 12 |
-
if min(h, w) > 720:
|
| 13 |
-
if h > w:
|
| 14 |
-
h, w = int(720 * h / w), 720
|
| 15 |
-
else:
|
| 16 |
-
h, w = 720, int(720 * w / h)
|
| 17 |
-
image = cv2.resize(image, (w, h),
|
| 18 |
-
interpolation=cv2.INTER_AREA)
|
| 19 |
-
h, w = (h // 8) * 8, (w // 8) * 8
|
| 20 |
-
image = image[:h, :w, :]
|
| 21 |
-
return image
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
def cartoonize(load_folder, save_folder, model_path):
|
| 25 |
-
print(model_path)
|
| 26 |
-
input_photo = tf.placeholder(tf.float32, [1, None, None, 3])
|
| 27 |
-
network_out = network.unet_generator(input_photo)
|
| 28 |
-
final_out = guided_filter.guided_filter(input_photo, network_out, r=1, eps=5e-3)
|
| 29 |
-
|
| 30 |
-
all_vars = tf.trainable_variables()
|
| 31 |
-
gene_vars = [var for var in all_vars if 'generator' in var.name]
|
| 32 |
-
saver = tf.train.Saver(var_list=gene_vars)
|
| 33 |
-
|
| 34 |
-
config = tf.ConfigProto()
|
| 35 |
-
config.gpu_options.allow_growth = True
|
| 36 |
-
sess = tf.Session(config=config)
|
| 37 |
-
|
| 38 |
-
sess.run(tf.global_variables_initializer())
|
| 39 |
-
saver.restore(sess, tf.train.latest_checkpoint(model_path))
|
| 40 |
-
name_list = os.listdir(load_folder)
|
| 41 |
-
for name in tqdm(name_list):
|
| 42 |
-
try:
|
| 43 |
-
load_path = os.path.join(load_folder, name)
|
| 44 |
-
save_path = os.path.join(save_folder, name)
|
| 45 |
-
image = cv2.imread(load_path)
|
| 46 |
-
image = resize_crop(image)
|
| 47 |
-
batch_image = image.astype(np.float32) / 127.5 - 1
|
| 48 |
-
batch_image = np.expand_dims(batch_image, axis=0)
|
| 49 |
-
output = sess.run(final_out, feed_dict={input_photo: batch_image})
|
| 50 |
-
output = (np.squeeze(output) + 1) * 127.5
|
| 51 |
-
output = np.clip(output, 0, 255).astype(np.uint8)
|
| 52 |
-
cv2.imwrite(save_path, output)
|
| 53 |
-
except:
|
| 54 |
-
print('cartoonize {} failed'.format(load_path))
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
class Cartoonize:
|
| 58 |
-
def __init__(self, model_path):
|
| 59 |
-
print(model_path)
|
| 60 |
-
self.input_photo = tf.placeholder(tf.float32, [1, None, None, 3])
|
| 61 |
-
network_out = network.unet_generator(self.input_photo)
|
| 62 |
-
self.final_out = guided_filter.guided_filter(self.input_photo, network_out, r=1, eps=5e-3)
|
| 63 |
-
|
| 64 |
-
all_vars = tf.trainable_variables()
|
| 65 |
-
gene_vars = [var for var in all_vars if 'generator' in var.name]
|
| 66 |
-
saver = tf.train.Saver(var_list=gene_vars)
|
| 67 |
-
|
| 68 |
-
config = tf.ConfigProto()
|
| 69 |
-
config.gpu_options.allow_growth = True
|
| 70 |
-
self.sess = tf.Session(config=config)
|
| 71 |
-
|
| 72 |
-
self.sess.run(tf.global_variables_initializer())
|
| 73 |
-
saver.restore(self.sess, tf.train.latest_checkpoint(model_path))
|
| 74 |
-
|
| 75 |
-
def run(self, load_folder, save_folder):
|
| 76 |
-
name_list = os.listdir(load_folder)
|
| 77 |
-
for name in tqdm(name_list):
|
| 78 |
-
try:
|
| 79 |
-
load_path = os.path.join(load_folder, name)
|
| 80 |
-
save_path = os.path.join(save_folder, name)
|
| 81 |
-
image = cv2.imread(load_path)
|
| 82 |
-
image = resize_crop(image)
|
| 83 |
-
batch_image = image.astype(np.float32) / 127.5 - 1
|
| 84 |
-
batch_image = np.expand_dims(batch_image, axis=0)
|
| 85 |
-
output = self.sess.run(self.final_out, feed_dict={self.input_photo: batch_image})
|
| 86 |
-
output = (np.squeeze(output) + 1) * 127.5
|
| 87 |
-
output = np.clip(output, 0, 255).astype(np.uint8)
|
| 88 |
-
cv2.imwrite(save_path, output)
|
| 89 |
-
except:
|
| 90 |
-
print('cartoonize {} failed'.format(load_path))
|
| 91 |
-
|
| 92 |
-
def run_sigle(self, load_path, save_path):
|
| 93 |
-
try:
|
| 94 |
-
image = cv2.imread(load_path)
|
| 95 |
-
image = resize_crop(image)
|
| 96 |
-
batch_image = image.astype(np.float32) / 127.5 - 1
|
| 97 |
-
batch_image = np.expand_dims(batch_image, axis=0)
|
| 98 |
-
output = self.sess.run(self.final_out, feed_dict={self.input_photo: batch_image})
|
| 99 |
-
output = (np.squeeze(output) + 1) * 127.5
|
| 100 |
-
output = np.clip(output, 0, 255).astype(np.uint8)
|
| 101 |
-
cv2.imwrite(save_path, output)
|
| 102 |
-
except:
|
| 103 |
-
print('cartoonize {} failed'.format(load_path))
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
if __name__ == '__main__':
|
| 107 |
-
model_path = 'saved_models'
|
| 108 |
-
load_folder = 'test_images'
|
| 109 |
-
save_folder = 'cartoonized_images'
|
| 110 |
-
if not os.path.exists(save_folder):
|
| 111 |
-
os.mkdir(save_folder)
|
| 112 |
-
cartoonize(load_folder, save_folder, model_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|