Hexii commited on
Commit
1ce4793
1 Parent(s): a9aa3c9

first commit

Browse files
Content/content_2.jpg ADDED
Content/content_4.jpg ADDED
Styles/Scream Edvard Munch.jpg ADDED
Styles/style_15.jpg ADDED
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''
2
+ Neural Style Transfer using TensorFlow's Pretrained Style Transfer Model
3
+ https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2
4
+
5
+ '''
6
+
7
+
8
+ import gradio as gr
9
+ import tensorflow as tf
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
+
17
+
18
+
19
+ model = hub.load("https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2")
20
+
21
+
22
+ # source: https://stackoverflow.com/questions/4993082/how-can-i-sharpen-an-image-in-opencv
23
+ def unsharp_mask(image, kernel_size=(5, 5), sigma=1.0, amount=1.0, threshold=0):
24
+ """Return a sharpened version of the image, using an unsharp mask."""
25
+ blurred = cv2.GaussianBlur(image, kernel_size, sigma)
26
+ sharpened = float(amount + 1) * image - float(amount) * blurred
27
+ sharpened = np.maximum(sharpened, np.zeros(sharpened.shape))
28
+ sharpened = np.minimum(sharpened, 255 * np.ones(sharpened.shape))
29
+ sharpened = sharpened.round().astype(np.uint8)
30
+ if threshold > 0:
31
+ low_contrast_mask = np.absolute(image - blurred) < threshold
32
+ np.copyto(sharpened, image, where=low_contrast_mask)
33
+ return sharpened
34
+
35
+
36
+ def style_transfer(content_img,style_image, style_weight = 1, content_weight = 1, style_blur=False):
37
+ content_img = unsharp_mask(content_img,amount=1)
38
+ content_img = tf.image.resize(tf.convert_to_tensor(content_img,tf.float32)[tf.newaxis,...] / 255.,(512,512),preserve_aspect_ratio=True)
39
+ style_img = tf.convert_to_tensor(style_image,tf.float32)[tf.newaxis,...] / 255.
40
+ if style_blur:
41
+ style_img= tf.nn.avg_pool(style_img, [3,3], [1,1], "VALID")
42
+ style_img = tf.image.adjust_contrast(style_img, style_weight)
43
+ content_img = tf.image.adjust_contrast(content_img,content_weight)
44
+ content_img = tf.image.adjust_saturation(content_img, 2)
45
+ content_img = tf.image.adjust_contrast(content_img,1.5)
46
+ stylized_img = model(content_img, style_img)[0]
47
+
48
+ return Image.fromarray(np.uint8(stylized_img[0]*255))
49
+
50
+
51
+
52
+
53
+ title = "Artistic Neural Style Transfer Demo 🖼️"
54
+ description = "Gradio Demo for Artistic Neural Style Transfer. To use it, simply upload a content image and a style image. [Learn More](https://www.tensorflow.org/tutorials/generative/style_transfer)."
55
+ article = "</br><p style='text-align: center'><a href='https://github.com/Mr-Hexi' target='_blank'>GitHub</a></p> "
56
+
57
+
58
+ content_input = gr.inputs.Image(label="Upload an image to which you want the style to be applied.", )
59
+ style_input = gr.inputs.Image( label="Upload Style Image ",shape= (256,256), )
60
+ style_slider = gr.inputs.Slider(0,2,label="Adjust Style Density" ,default=1,)
61
+ content_slider = gr.inputs.Slider(1,5,label="Content Sharpness" ,default=1,)
62
+ style_checkbox = gr.Checkbox(value=False,label="Tune Style(experimental)")
63
+
64
+
65
+ examples = [
66
+ ["Content/content_2.jpg","Styles\style_15.jpg",1.20,1.70,""],
67
+ ["Content/content_4.jpg","Styles\style_10.jpg",0.91,2.54,"style_checkbox"]
68
+
69
+ ]
70
+ interface = gr.Interface(fn=style_transfer,
71
+ inputs=[content_input,
72
+ style_input,
73
+ style_slider ,
74
+ content_slider,
75
+ style_checkbox],
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()