Spaces:
Runtime error
Runtime error
luca-martial
commited on
Commit
·
8b5a1e9
1
Parent(s):
f1b9169
debug
Browse files
app.py
CHANGED
@@ -4,69 +4,17 @@ import tensorflow_hub as hub
|
|
4 |
import matplotlib.pyplot as plt
|
5 |
import numpy as np
|
6 |
|
7 |
-
hub_model = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')
|
8 |
-
|
9 |
-
def tensor_to_image(tensor):
|
10 |
-
tensor = tensor*255
|
11 |
-
tensor = np.array(tensor, dtype=np.uint8)
|
12 |
-
if np.ndim(tensor)>3:
|
13 |
-
assert tensor.shape[0] == 1
|
14 |
-
tensor = tensor[0]
|
15 |
-
return PIL.Image.fromarray(tensor)
|
16 |
-
|
17 |
-
def load_img(path_to_img):
|
18 |
-
max_dim = 512
|
19 |
-
img = tf.io.read_file(path_to_img)
|
20 |
-
img = tf.image.decode_image(img, channels=3)
|
21 |
-
img = tf.image.convert_image_dtype(img, tf.float32)
|
22 |
-
|
23 |
-
shape = tf.cast(tf.shape(img)[:-1], tf.float32)
|
24 |
-
long_dim = max(shape)
|
25 |
-
scale = max_dim / long_dim
|
26 |
-
|
27 |
-
new_shape = tf.cast(shape * scale, tf.int32)
|
28 |
-
|
29 |
-
img = tf.image.resize(img, new_shape)
|
30 |
-
img = img[tf.newaxis, :]
|
31 |
-
return img
|
32 |
-
|
33 |
|
34 |
|
35 |
def stylize(content_image, style_image):
|
36 |
content_image = content_image.astype(np.float32) / 255.
|
37 |
style_image = style_image.astype(np.float32) / 255.
|
38 |
|
39 |
-
|
40 |
-
style_image = tf.image.resize(style_image, (256, 256))
|
41 |
|
42 |
-
#content_image = load_img(content_path)
|
43 |
-
#style_image = load_img(style_path)
|
44 |
-
#stylized_image = hub_model(tf.constant(content_image), tf.constant(style_image))[0]
|
45 |
return style_image
|
46 |
|
47 |
-
#content_image = plt.imread(content_image_path)
|
48 |
-
#style_image = plt.imread(style_image_path)
|
49 |
-
#content_image = content_image.astype(np.float32)[np.newaxis, ...] / 255.
|
50 |
-
#style_image = style_image.astype(np.float32)[np.newaxis, ...] / 255.
|
51 |
-
#style_image = tf.image.resize(style_image, (256, 256))
|
52 |
-
|
53 |
-
#stylized_image = hub_model(tf.constant(content_image), tf.constant(style_image))[0]
|
54 |
-
#return tensor_to_image(stylized_image)
|
55 |
-
|
56 |
-
|
57 |
-
# Load content and style images (see example in the attached colab).
|
58 |
-
#content_image = plt.imread(content_image_path)
|
59 |
-
#style_image = plt.imread(style_image_path)
|
60 |
-
|
61 |
-
# Convert to float32 numpy array, add batch dimension, and normalize to range [0, 1]. Example using numpy:
|
62 |
-
#content_image = content_image.astype(np.float32)[np.newaxis, ...] / 255.
|
63 |
-
#style_image = style_image.astype(np.float32)[np.newaxis, ...] / 255.
|
64 |
-
|
65 |
-
# Optionally resize the images. It is recommended that the style image is about
|
66 |
-
# 256 pixels (this size was used when training the style transfer network).
|
67 |
-
# The content image can be any size.
|
68 |
-
# style_image = tf.image.resize(style_image, (256, 256))
|
69 |
-
|
70 |
|
71 |
iface = gr.Interface(fn=stylize, inputs=["image", "image"], outputs="image")
|
72 |
iface.launch()
|
|
|
4 |
import matplotlib.pyplot as plt
|
5 |
import numpy as np
|
6 |
|
7 |
+
hub_model = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
|
10 |
def stylize(content_image, style_image):
|
11 |
content_image = content_image.astype(np.float32) / 255.
|
12 |
style_image = style_image.astype(np.float32) / 255.
|
13 |
|
14 |
+
#style_image = tf.image.resize(style_image, (256, 256))
|
|
|
15 |
|
|
|
|
|
|
|
16 |
return style_image
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
iface = gr.Interface(fn=stylize, inputs=["image", "image"], outputs="image")
|
20 |
iface.launch()
|