Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -10,6 +10,7 @@ import tensorflow as tf
|
|
10 |
import tensorflow_hub as hub
|
11 |
from PIL import Image
|
12 |
import numpy as np
|
|
|
13 |
import cv2
|
14 |
import os
|
15 |
|
@@ -32,19 +33,40 @@ def unsharp_mask(image, kernel_size=(5, 5), sigma=1.0, amount=1.0, threshold=0):
|
|
32 |
return sharpened
|
33 |
|
34 |
|
35 |
-
def style_transfer(content_img,style_image, style_weight
|
36 |
-
|
37 |
-
content_img =
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
if style_blur:
|
40 |
-
style_img=
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
stylized_img = model(content_img, style_img)[0]
|
46 |
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
|
50 |
|
@@ -54,32 +76,30 @@ description = "Gradio Demo for Artistic Neural Style Transfer. To use it, simply
|
|
54 |
article = "</br><p style='text-align: center'><a href='https://github.com/Mr-Hexi' target='_blank'>GitHub</a></p> "
|
55 |
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
examples
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
interface.launch()
|
|
|
10 |
import tensorflow_hub as hub
|
11 |
from PIL import Image
|
12 |
import numpy as np
|
13 |
+
import functools
|
14 |
import cv2
|
15 |
import os
|
16 |
|
|
|
33 |
return sharpened
|
34 |
|
35 |
|
36 |
+
def style_transfer(content_img, style_image, style_weight=1, content_weight=1, style_blur=False):
|
37 |
+
# Resize and preprocess the content image
|
38 |
+
content_img = unsharp_mask(content_img, amount=1)
|
39 |
+
content_img = tf.image.resize(
|
40 |
+
tf.convert_to_tensor(content_img, dtype=tf.float32)[tf.newaxis, ...] / 255.0,
|
41 |
+
(512, 512),
|
42 |
+
preserve_aspect_ratio=True
|
43 |
+
)
|
44 |
+
|
45 |
+
# Resize and preprocess the style image
|
46 |
+
style_image = Image.fromarray(style_image).resize((256, 256))
|
47 |
+
style_img = tf.convert_to_tensor(np.array(style_image), dtype=tf.float32)[tf.newaxis, ...] / 255.0
|
48 |
+
|
49 |
if style_blur:
|
50 |
+
style_img = tf.nn.avg_pool(style_img, ksize=[3, 3], strides=[1, 1], padding="VALID")
|
51 |
+
|
52 |
+
# Apply style weight to the style image
|
53 |
+
style_img = tf.image.adjust_contrast(style_img, style_weight)
|
54 |
+
|
55 |
+
# Apply content weight and other adjustments to the content image
|
56 |
+
content_img = tf.image.adjust_contrast(content_img, content_weight)
|
57 |
+
content_img = tf.image.adjust_saturation(content_img, 2)
|
58 |
+
content_img = tf.image.adjust_contrast(content_img, 1.5)
|
59 |
+
|
60 |
+
# Stylize the content image using the style image
|
61 |
stylized_img = model(content_img, style_img)[0]
|
62 |
|
63 |
+
# Convert the stylized image tensor to a NumPy array
|
64 |
+
stylized_img = tf.squeeze(stylized_img).numpy()
|
65 |
+
|
66 |
+
# Convert the NumPy array to an image
|
67 |
+
stylized_img = np.clip(stylized_img * 255.0, 0, 255).astype(np.uint8)
|
68 |
+
|
69 |
+
return Image.fromarray(stylized_img)
|
70 |
|
71 |
|
72 |
|
|
|
76 |
article = "</br><p style='text-align: center'><a href='https://github.com/Mr-Hexi' target='_blank'>GitHub</a></p> "
|
77 |
|
78 |
|
79 |
+
# Define inputs
|
80 |
+
content_input = gr.Image(label="Upload an image to which you want the style to be applied.")
|
81 |
+
style_input = gr.Image(label="Upload Style Image") # Removed the shape parameter
|
82 |
+
style_slider = gr.Slider(0, 2, label="Adjust Style Density", value=1)
|
83 |
+
content_slider = gr.Slider(1, 5, label="Content Sharpness", value=1)
|
84 |
+
style_checkbox = gr.Checkbox(value=False, label="Tune Style (experimental)")
|
85 |
+
|
86 |
+
# Define examples
|
87 |
+
examples = [
|
88 |
+
["Content/content_2.jpg", "Styles/style_15.jpg", 1.20, 1.70, ""],
|
89 |
+
["Content/content_4.jpg", "Styles/style_10.jpg", 0.91, 2.54, "style_checkbox"]
|
90 |
+
]
|
91 |
+
|
92 |
+
# Define the interface
|
93 |
+
interface = gr.Interface(
|
94 |
+
fn=style_transfer,
|
95 |
+
inputs=[content_input, style_input, style_slider, content_slider, style_checkbox],
|
96 |
+
outputs=gr.Image(),
|
97 |
+
title=title,
|
98 |
+
description=description,
|
99 |
+
article=article,
|
100 |
+
examples=examples,
|
101 |
+
allow_flagging="never",
|
102 |
+
)
|
103 |
+
# Launch the interface
|
104 |
+
interface.launch(debug=True)
|
105 |
+
|
|
|
|