Spaces:
Running
Running
Upload 8 files
Browse files- .gitattributes +2 -0
- Content/content_1.jpg +0 -0
- Content/content_2.jpg +0 -0
- Content/content_3.png +3 -0
- Styles/style_1.jpg +0 -0
- Styles/style_2.jpg +0 -0
- Styles/style_3.jpg +3 -0
- app.py +85 -0
- requirements.txt +6 -0
.gitattributes
CHANGED
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
Content/content_3.png filter=lfs diff=lfs merge=lfs -text
|
37 |
+
Styles/style_3.jpg filter=lfs diff=lfs merge=lfs -text
|
Content/content_1.jpg
ADDED
Content/content_2.jpg
ADDED
Content/content_3.png
ADDED
Git LFS Details
|
Styles/style_1.jpg
ADDED
Styles/style_2.jpg
ADDED
Styles/style_3.jpg
ADDED
Git LFS Details
|
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 cv2
|
14 |
+
import os
|
15 |
+
|
16 |
+
|
17 |
+
|
18 |
+
model = hub.load("https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2")
|
19 |
+
|
20 |
+
|
21 |
+
# source: https://stackoverflow.com/questions/4993082/how-can-i-sharpen-an-image-in-opencv
|
22 |
+
def unsharp_mask(image, kernel_size=(5, 5), sigma=1.0, amount=1.0, threshold=0):
|
23 |
+
"""Return a sharpened version of the image, using an unsharp mask."""
|
24 |
+
blurred = cv2.GaussianBlur(image, kernel_size, sigma)
|
25 |
+
sharpened = float(amount + 1) * image - float(amount) * blurred
|
26 |
+
sharpened = np.maximum(sharpened, np.zeros(sharpened.shape))
|
27 |
+
sharpened = np.minimum(sharpened, 255 * np.ones(sharpened.shape))
|
28 |
+
sharpened = sharpened.round().astype(np.uint8)
|
29 |
+
if threshold > 0:
|
30 |
+
low_contrast_mask = np.absolute(image - blurred) < threshold
|
31 |
+
np.copyto(sharpened, image, where=low_contrast_mask)
|
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 |
+
|
51 |
+
|
52 |
+
title = "PixelFusion🧬"
|
53 |
+
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)."
|
54 |
+
article = "</br><p style='text-align: center'><a href='https://github.com/0xsynapse' 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_1.jpg","Styles/style_1.jpg",1.20,1.70,"style_checkbox"],
|
66 |
+
["Content/content_2.jpg","Styles/style_2.jpg",0.91,2.54,"style_checkbox"],
|
67 |
+
["Content/content_3.png","Styles/style_3.jpg",1.02,2.47,"style_checkbox"]
|
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()
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
tensorflow==2.12.0
|
2 |
+
tensorflow_hub==0.12.0
|
3 |
+
gradio==3.1.4
|
4 |
+
opencv-python==4.6.0.66
|
5 |
+
protobuf==3.20.0
|
6 |
+
|