Hexii commited on
Commit
0d9d3ef
1 Parent(s): 6958554

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -39
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 = 1, content_weight = 1, style_blur=False):
36
- content_img = unsharp_mask(content_img,amount=1)
37
- content_img = tf.image.resize(tf.convert_to_tensor(content_img,tf.float32)[tf.newaxis,...] / 255.,(512,512),preserve_aspect_ratio=True)
38
- style_img = tf.convert_to_tensor(style_image,tf.float32)[tf.newaxis,...] / 255.
 
 
 
 
 
 
 
 
 
39
  if style_blur:
40
- style_img= tf.nn.avg_pool(style_img, [3,3], [1,1], "VALID")
41
- style_img = tf.image.adjust_contrast(style_img, style_weight)
42
- content_img = tf.image.adjust_contrast(content_img,content_weight)
43
- content_img = tf.image.adjust_saturation(content_img, 2)
44
- content_img = tf.image.adjust_contrast(content_img,1.5)
 
 
 
 
 
 
45
  stylized_img = model(content_img, style_img)[0]
46
 
47
- return Image.fromarray(np.uint8(stylized_img[0]*255))
 
 
 
 
 
 
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
- content_input = gr.inputs.Image(label="Upload an image to which you want the style to be applied.",)
58
- style_input = gr.inputs.Image( label="Upload Style Image ",shape= (256,256), )
59
- style_slider = gr.inputs.Slider(0,2,label="Adjust Style Density" ,default=1,)
60
- content_slider = gr.inputs.Slider(1,5,label="Content Sharpness" ,default=1,)
61
- # style_checkbox = gr.Checkbox(value=False,label="Tune Style(experimental)")
62
-
63
-
64
- examples = [
65
- ["Content/content_2.jpg","Styles/style_15.jpg",1.20,1.70,""],
66
- ["Content/content_4.jpg","Styles/Scream Edvard Munch.jpg",0.91,2.54,"style_checkbox"]
67
-
68
- ]
69
- interface = gr.Interface(fn=style_transfer,
70
- inputs=[content_input,
71
- style_input,
72
- style_slider ,
73
- content_slider,
74
- # style_checkbox
75
- ],
76
- outputs=gr.outputs.Image(),
77
- title=title,
78
- description=description,
79
- article=article,
80
- examples=examples,
81
- enable_queue=True
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
+