Spaces:
Running
Running
Alican Akca
commited on
Commit
·
b0c5c57
1
Parent(s):
8a23f94
For Version 2
Browse files- app.py +42 -46
- examples/pixelArt/white_box_cartoonizer/__pycache__/cartoonize.cpython-37.pyc +0 -0
- examples/pixelArt/white_box_cartoonizer/__pycache__/cartoonize.cpython-38.pyc +0 -0
- examples/pixelArt/white_box_cartoonizer/__pycache__/guided_filter.cpython-37.pyc +0 -0
- examples/pixelArt/white_box_cartoonizer/__pycache__/network.cpython-37.pyc +0 -0
- examples/pixelArt/white_box_cartoonizer/cartoonize.py +0 -83
- examples/pixelArt/white_box_cartoonizer/components/__pycache__/guided_filter.cpython-38.pyc +0 -0
- examples/pixelArt/white_box_cartoonizer/components/__pycache__/network.cpython-38.pyc +0 -0
- examples/pixelArt/white_box_cartoonizer/components/guided_filter.py +0 -73
- examples/pixelArt/white_box_cartoonizer/components/network.py +0 -72
- examples/pixelArt/white_box_cartoonizer/saved_models/checkpoint +0 -3
- examples/pixelArt/white_box_cartoonizer/saved_models/model-33999.index +0 -0
- examples/pixelArt/white_box_cartoonizer/test.jpg +0 -0
- output/result_0.png +0 -0
- output/result_mask_0.png +0 -0
- requirements.txt +1 -0
app.py
CHANGED
@@ -1,69 +1,65 @@
|
|
1 |
import cv2
|
|
|
2 |
import numpy as np
|
3 |
import gradio as gr
|
4 |
-
from PIL import Image
|
5 |
import paddlehub as hub
|
|
|
6 |
from methods.img2pixl import pixL
|
7 |
from examples.pixelArt.combine import combine
|
8 |
-
|
9 |
model = hub.Module(name='U2Net')
|
10 |
-
|
11 |
-
|
|
|
12 |
|
13 |
def GIF(fname,pixel_size):
|
14 |
-
print(fname)
|
15 |
gif = Image.open(fname)
|
16 |
-
frames = []
|
17 |
-
for i in range(gif.n_frames):
|
18 |
gif.seek(i)
|
19 |
frame = Image.new('RGB', gif.size)
|
20 |
frame.paste(gif)
|
21 |
frame = np.array(frame)
|
22 |
frames.append(frame)
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
frames = []
|
28 |
-
for frame in result:
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
return Image.open('cache.gif')
|
36 |
|
37 |
-
def
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
output_dir='output',
|
53 |
-
visualization=True)
|
54 |
-
result = combine.combiner(images = pixl.toThePixL([result[0]['front'][:,:,::-1], result[0]['mask']],
|
55 |
pixel_size),
|
56 |
background_image = image)
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
|
61 |
-
|
62 |
gr.Slider(4, 100, value=12, step = 2, label="Pixel Size"),
|
63 |
gr.Checkbox(label="Object-Oriented Inference", value=False)]
|
64 |
-
|
65 |
-
|
66 |
-
gr.Interface(fn = func_tab1,
|
67 |
-
inputs = inputs_tab1,
|
68 |
-
outputs = outputs_tab1).launch()
|
69 |
|
|
|
|
|
|
|
|
1 |
import cv2
|
2 |
+
import torch
|
3 |
import numpy as np
|
4 |
import gradio as gr
|
|
|
5 |
import paddlehub as hub
|
6 |
+
from PIL import Image
|
7 |
from methods.img2pixl import pixL
|
8 |
from examples.pixelArt.combine import combine
|
9 |
+
|
10 |
model = hub.Module(name='U2Net')
|
11 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
+
face2paint = torch.hub.load("bryandlee/animegan2-pytorch:main", "face2paint", device=device, size=512)
|
13 |
+
model = torch.hub.load("bryandlee/animegan2-pytorch", "generator", device=device).eval()
|
14 |
|
15 |
def GIF(fname,pixel_size):
|
|
|
16 |
gif = Image.open(fname)
|
17 |
+
frames = []
|
18 |
+
for i in range(gif.n_frames): #First Step: Splitting the GIF into frames
|
19 |
gif.seek(i)
|
20 |
frame = Image.new('RGB', gif.size)
|
21 |
frame.paste(gif)
|
22 |
frame = np.array(frame)
|
23 |
frames.append(frame)
|
24 |
+
result = pixL().toThePixL(frames, pixel_size)
|
25 |
+
for frame in result: #Second Step: Adding Cartoon Effect to each frame
|
26 |
+
frame = Image.fromarray(frame)
|
27 |
+
frame = cv2.cvtColor(np.asarray(face2paint(model, frame)), cv2.COLOR_BGR2RGB)
|
28 |
frames = []
|
29 |
+
for frame in result: #Third Step: Combining the frames into a GIF
|
30 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
31 |
+
frame = Image.fromarray(frame)
|
32 |
+
frames.append(frame)
|
33 |
+
frames[0].save('cache.gif', append_images=frames, save_all=True, loop=1)
|
34 |
+
cache = Image.open('cache.gif')
|
35 |
+
return cache
|
|
|
36 |
|
37 |
+
def initilize(image,pixel_size,checkbox1):
|
38 |
+
if image.name.endswith('.gif'):
|
39 |
+
GIF(image.name,pixel_size)
|
40 |
+
else:
|
41 |
+
image = Image.open(image.name).convert("RGB")
|
42 |
+
image = cv2.cvtColor(np.asarray(face2paint(model, image)), cv2.COLOR_BGR2RGB)
|
43 |
+
if checkbox1:
|
44 |
+
result = model.Segmentation(
|
45 |
+
images=[image],
|
46 |
+
paths=None,
|
47 |
+
batch_size=1,
|
48 |
+
input_size=320,
|
49 |
+
output_dir='output',
|
50 |
+
visualization=True)
|
51 |
+
result = combine.combiner(images = pixL().toThePixL([result[0]['front'][:,:,::-1], result[0]['mask']],
|
|
|
|
|
|
|
52 |
pixel_size),
|
53 |
background_image = image)
|
54 |
+
else:
|
55 |
+
result = pixL().toThePixL([image], pixel_size)
|
56 |
+
return Image.fromarray(result)
|
57 |
|
58 |
+
inputs = ["file",
|
59 |
gr.Slider(4, 100, value=12, step = 2, label="Pixel Size"),
|
60 |
gr.Checkbox(label="Object-Oriented Inference", value=False)]
|
61 |
+
outputs = [gr.Image(type="pil",label="Front")]
|
|
|
|
|
|
|
|
|
62 |
|
63 |
+
gr.Interface(fn = initilize,
|
64 |
+
inputs = inputs,
|
65 |
+
outputs = outputs).launch()
|
examples/pixelArt/white_box_cartoonizer/__pycache__/cartoonize.cpython-37.pyc
DELETED
Binary file (4.2 kB)
|
|
examples/pixelArt/white_box_cartoonizer/__pycache__/cartoonize.cpython-38.pyc
DELETED
Binary file (3.18 kB)
|
|
examples/pixelArt/white_box_cartoonizer/__pycache__/guided_filter.cpython-37.pyc
DELETED
Binary file (2.52 kB)
|
|
examples/pixelArt/white_box_cartoonizer/__pycache__/network.cpython-37.pyc
DELETED
Binary file (1.9 kB)
|
|
examples/pixelArt/white_box_cartoonizer/cartoonize.py
DELETED
@@ -1,83 +0,0 @@
|
|
1 |
-
"""
|
2 |
-
Internal code snippets were obtained from https://github.com/SystemErrorWang/White-box-Cartoonization/
|
3 |
-
|
4 |
-
For it to work tensorflow version 2.x changes were obtained from https://github.com/steubk/White-box-Cartoonization
|
5 |
-
"""
|
6 |
-
import os
|
7 |
-
import uuid
|
8 |
-
import time
|
9 |
-
import subprocess
|
10 |
-
import sys
|
11 |
-
|
12 |
-
import cv2
|
13 |
-
import numpy as np
|
14 |
-
|
15 |
-
try:
|
16 |
-
import tensorflow.compat.v1 as tf
|
17 |
-
except ImportError:
|
18 |
-
import tensorflow as tf
|
19 |
-
|
20 |
-
from methods.white_box_cartoonizer.components.guided_filter import gf
|
21 |
-
from methods.white_box_cartoonizer.components.network import nk
|
22 |
-
|
23 |
-
weights_dir = f'{os.getcwd()}/methods/white_box_cartoonizer/saved_models'
|
24 |
-
gpu = len(sys.argv) < 2 or sys.argv[1] != '--cpu'
|
25 |
-
|
26 |
-
class WB_Cartoonize:
|
27 |
-
def __init__(self):
|
28 |
-
if not os.path.exists(weights_dir):
|
29 |
-
raise FileNotFoundError("Weights Directory not found, check path")
|
30 |
-
|
31 |
-
def resize_crop(self, image):
|
32 |
-
h, w, c = np.shape(image)
|
33 |
-
if min(h, w) > 720:
|
34 |
-
if h > w:
|
35 |
-
h, w = int(720*h/w), 720
|
36 |
-
else:
|
37 |
-
h, w = 720, int(720*w/h)
|
38 |
-
image = cv2.resize(image, (w, h),
|
39 |
-
interpolation=cv2.INTER_AREA)
|
40 |
-
h, w = (h//8)*8, (w//8)*8
|
41 |
-
image = image[:h, :w, :]
|
42 |
-
return image
|
43 |
-
|
44 |
-
def load_model(self, weights_dir, gpu):
|
45 |
-
try:
|
46 |
-
tf.disable_eager_execution()
|
47 |
-
except:
|
48 |
-
None
|
49 |
-
|
50 |
-
tf.reset_default_graph()
|
51 |
-
|
52 |
-
self.input_photo = tf.placeholder(tf.float32, [1, None, None, 3], name='input_image')
|
53 |
-
network_out = nk.unet_generator(self.input_photo)
|
54 |
-
self.final_out = gf.guided_filter(self.input_photo, network_out, r=1, eps=5e-3)
|
55 |
-
|
56 |
-
all_vars = tf.trainable_variables()
|
57 |
-
gene_vars = [var for var in all_vars if 'generator' in var.name]
|
58 |
-
saver = tf.train.Saver(var_list=gene_vars)
|
59 |
-
|
60 |
-
if gpu:
|
61 |
-
gpu_options = tf.GPUOptions(allow_growth=True)
|
62 |
-
device_count = {'GPU':1}
|
63 |
-
else:
|
64 |
-
gpu_options = None
|
65 |
-
device_count = {'GPU':0}
|
66 |
-
|
67 |
-
config = tf.ConfigProto(gpu_options=gpu_options, device_count=device_count)
|
68 |
-
|
69 |
-
self.sess = tf.Session(config=config)
|
70 |
-
|
71 |
-
self.sess.run(tf.global_variables_initializer())
|
72 |
-
saver.restore(self.sess, tf.train.latest_checkpoint(weights_dir))
|
73 |
-
|
74 |
-
def infer(self, image):
|
75 |
-
self.input_photo = image
|
76 |
-
self.load_model(weights_dir, gpu)
|
77 |
-
image = self.resize_crop(image)
|
78 |
-
batch_image = image.astype(np.float32)/127.5 - 1
|
79 |
-
batch_image = np.expand_dims(batch_image, axis=0)
|
80 |
-
output = self.sess.run(self.final_out, feed_dict={self.input_photo: batch_image})
|
81 |
-
output = (np.squeeze(output)+1)*127.5
|
82 |
-
output = np.clip(output, 0, 255).astype(np.uint8)
|
83 |
-
return output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
examples/pixelArt/white_box_cartoonizer/components/__pycache__/guided_filter.cpython-38.pyc
DELETED
Binary file (2.18 kB)
|
|
examples/pixelArt/white_box_cartoonizer/components/__pycache__/network.cpython-38.pyc
DELETED
Binary file (2.16 kB)
|
|
examples/pixelArt/white_box_cartoonizer/components/guided_filter.py
DELETED
@@ -1,73 +0,0 @@
|
|
1 |
-
"""
|
2 |
-
Code copyrights are with: https://github.com/SystemErrorWang/White-box-Cartoonization/
|
3 |
-
|
4 |
-
To adapt the code with tensorflow v2 changes obtained from: https://github.com/steubk/White-box-Cartoonization
|
5 |
-
"""
|
6 |
-
try:
|
7 |
-
import tensorflow.compat.v1 as tf
|
8 |
-
except ImportError:
|
9 |
-
import tensorflow as tf
|
10 |
-
|
11 |
-
import numpy as np
|
12 |
-
|
13 |
-
class gf:
|
14 |
-
def tf_box_filter(x, r):
|
15 |
-
k_size = int(2*r+1)
|
16 |
-
ch = x.get_shape().as_list()[-1]
|
17 |
-
weight = 1/(k_size**2)
|
18 |
-
box_kernel = weight*np.ones((k_size, k_size, ch, 1))
|
19 |
-
box_kernel = np.array(box_kernel).astype(np.float32)
|
20 |
-
output = tf.nn.depthwise_conv2d(x, box_kernel, [1, 1, 1, 1], 'SAME')
|
21 |
-
return output
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
def guided_filter(x, y, r, eps=1e-2):
|
26 |
-
|
27 |
-
x_shape = tf.shape(x)
|
28 |
-
#y_shape = tf.shape(y)
|
29 |
-
|
30 |
-
N = gf.tf_box_filter(tf.ones((1, x_shape[1], x_shape[2], 1), dtype=x.dtype), r)
|
31 |
-
|
32 |
-
mean_x = gf.tf_box_filter(x, r) / N
|
33 |
-
mean_y = gf.tf_box_filter(y, r) / N
|
34 |
-
cov_xy = gf.tf_box_filter(x * y, r) / N - mean_x * mean_y
|
35 |
-
var_x = gf.tf_box_filter(x * x, r) / N - mean_x * mean_x
|
36 |
-
|
37 |
-
A = cov_xy / (var_x + eps)
|
38 |
-
b = mean_y - A * mean_x
|
39 |
-
|
40 |
-
mean_A = gf.tf_box_filter(A, r) / N
|
41 |
-
mean_b = gf.tf_box_filter(b, r) / N
|
42 |
-
|
43 |
-
output = tf.add(mean_A * x, mean_b, name='final_add')
|
44 |
-
|
45 |
-
return output
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
def fast_guided_filter(lr_x, lr_y, hr_x, r=1, eps=1e-8):
|
50 |
-
|
51 |
-
#assert lr_x.shape.ndims == 4 and lr_y.shape.ndims == 4 and hr_x.shape.ndims == 4
|
52 |
-
|
53 |
-
lr_x_shape = tf.shape(lr_x)
|
54 |
-
#lr_y_shape = tf.shape(lr_y)
|
55 |
-
hr_x_shape = tf.shape(hr_x)
|
56 |
-
|
57 |
-
N = gf.tf_box_filter(tf.ones((1, lr_x_shape[1], lr_x_shape[2], 1), dtype=lr_x.dtype), r)
|
58 |
-
|
59 |
-
mean_x = gf.tf_box_filter(lr_x, r) / N
|
60 |
-
mean_y = gf.tf_box_filter(lr_y, r) / N
|
61 |
-
cov_xy = gf.tf_box_filter(lr_x * lr_y, r) / N - mean_x * mean_y
|
62 |
-
var_x = gf.tf_box_filter(lr_x * lr_x, r) / N - mean_x * mean_x
|
63 |
-
|
64 |
-
A = cov_xy / (var_x + eps)
|
65 |
-
b = mean_y - A * mean_x
|
66 |
-
|
67 |
-
mean_A = tf.image.resize_images(A, hr_x_shape[1: 3])
|
68 |
-
mean_b = tf.image.resize_images(b, hr_x_shape[1: 3])
|
69 |
-
|
70 |
-
output = mean_A * hr_x + mean_b
|
71 |
-
|
72 |
-
return output
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
examples/pixelArt/white_box_cartoonizer/components/network.py
DELETED
@@ -1,72 +0,0 @@
|
|
1 |
-
"""
|
2 |
-
Code copyrights are with: https://github.com/SystemErrorWang/White-box-Cartoonization/
|
3 |
-
|
4 |
-
To adapt the code with tensorflow v2 changes obtained from: https://github.com/steubk/White-box-Cartoonization
|
5 |
-
"""
|
6 |
-
try:
|
7 |
-
import tensorflow.compat.v1 as tf
|
8 |
-
import tf_slim as slim
|
9 |
-
except ImportError:
|
10 |
-
import tensorflow as tf
|
11 |
-
import tensorflow.contrib.slim as slim
|
12 |
-
|
13 |
-
import numpy as np
|
14 |
-
|
15 |
-
|
16 |
-
class nk:
|
17 |
-
def resblock(inputs, out_channel=32, name='resblock'):
|
18 |
-
|
19 |
-
with tf.variable_scope(name):
|
20 |
-
|
21 |
-
x = slim.convolution2d(inputs, out_channel, [3, 3],
|
22 |
-
activation_fn=None, scope='conv1')
|
23 |
-
x = tf.nn.leaky_relu(x)
|
24 |
-
x = slim.convolution2d(x, out_channel, [3, 3],
|
25 |
-
activation_fn=None, scope='conv2')
|
26 |
-
|
27 |
-
return x + inputs
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
def unet_generator(inputs, channel=32, num_blocks=4, name='generator', reuse=False):
|
33 |
-
with tf.variable_scope(name, reuse=reuse):
|
34 |
-
|
35 |
-
x0 = slim.convolution2d(inputs, channel, [7, 7], activation_fn=None)
|
36 |
-
x0 = tf.nn.leaky_relu(x0)
|
37 |
-
|
38 |
-
x1 = slim.convolution2d(x0, channel, [3, 3], stride=2, activation_fn=None)
|
39 |
-
x1 = tf.nn.leaky_relu(x1)
|
40 |
-
x1 = slim.convolution2d(x1, channel*2, [3, 3], activation_fn=None)
|
41 |
-
x1 = tf.nn.leaky_relu(x1)
|
42 |
-
|
43 |
-
x2 = slim.convolution2d(x1, channel*2, [3, 3], stride=2, activation_fn=None)
|
44 |
-
x2 = tf.nn.leaky_relu(x2)
|
45 |
-
x2 = slim.convolution2d(x2, channel*4, [3, 3], activation_fn=None)
|
46 |
-
x2 = tf.nn.leaky_relu(x2)
|
47 |
-
|
48 |
-
for idx in range(num_blocks):
|
49 |
-
x2 = nk.resblock(x2, out_channel=channel*4, name='block_{}'.format(idx))
|
50 |
-
|
51 |
-
x2 = slim.convolution2d(x2, channel*2, [3, 3], activation_fn=None)
|
52 |
-
x2 = tf.nn.leaky_relu(x2)
|
53 |
-
|
54 |
-
h1, w1 = tf.shape(x2)[1], tf.shape(x2)[2]
|
55 |
-
x3 = tf.image.resize_bilinear(x2, (h1*2, w1*2))
|
56 |
-
x3 = slim.convolution2d(x3+x1, channel*2, [3, 3], activation_fn=None)
|
57 |
-
x3 = tf.nn.leaky_relu(x3)
|
58 |
-
x3 = slim.convolution2d(x3, channel, [3, 3], activation_fn=None)
|
59 |
-
x3 = tf.nn.leaky_relu(x3)
|
60 |
-
|
61 |
-
h2, w2 = tf.shape(x3)[1], tf.shape(x3)[2]
|
62 |
-
x4 = tf.image.resize_bilinear(x3, (h2*2, w2*2))
|
63 |
-
x4 = slim.convolution2d(x4+x0, channel, [3, 3], activation_fn=None)
|
64 |
-
x4 = tf.nn.leaky_relu(x4)
|
65 |
-
x4 = slim.convolution2d(x4, 3, [7, 7], activation_fn=None)
|
66 |
-
|
67 |
-
return x4
|
68 |
-
|
69 |
-
if __name__ == '__main__':
|
70 |
-
|
71 |
-
|
72 |
-
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
examples/pixelArt/white_box_cartoonizer/saved_models/checkpoint
DELETED
@@ -1,3 +0,0 @@
|
|
1 |
-
model_checkpoint_path: "model-33999"
|
2 |
-
all_model_checkpoint_paths: "model-33999"
|
3 |
-
all_model_checkpoint_paths: "model-37499"
|
|
|
|
|
|
|
|
examples/pixelArt/white_box_cartoonizer/saved_models/model-33999.index
DELETED
Binary file (1.56 kB)
|
|
examples/pixelArt/white_box_cartoonizer/test.jpg
DELETED
Binary file (189 kB)
|
|
output/result_0.png
DELETED
Binary file (168 kB)
|
|
output/result_mask_0.png
DELETED
Binary file (25.6 kB)
|
|
requirements.txt
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
pip
|
|
|
2 |
tensorflow
|
3 |
Flask
|
4 |
gunicorn
|
|
|
1 |
pip
|
2 |
+
torch
|
3 |
tensorflow
|
4 |
Flask
|
5 |
gunicorn
|