SudhanshuBlaze commited on
Commit
a0dedd5
1 Parent(s): dc4c2a5

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +62 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+ from PIL import Image
4
+ import numpy as np
5
+ import tensorflow as tf
6
+ import tensorflow_hub as hub
7
+
8
+ st.title("Fast Neural image style transfer")
9
+ st.write("Streamlit demo for Fast arbitrary image style transfer using a pretrained Image Stylization model from TensorFlow Hub. To use it, simply upload a content image and style image. To learn more about the project, please find the references listed below.")
10
+
11
+ # Load image stylization module.
12
+ @st.cache(allow_output_mutation=True)
13
+ def load_model():
14
+ return hub.load("https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2")
15
+
16
+ style_transfer_model = load_model()
17
+
18
+ def perform_style_transfer(content_image, style_image):
19
+ # Convert to float32 numpy array, add batch dimension, and normalize to range [0, 1]
20
+ content_image = tf.convert_to_tensor(content_image, np.float32)[tf.newaxis, ...] / 255.
21
+ style_image = tf.convert_to_tensor(style_image, np.float32)[tf.newaxis, ...] / 255.
22
+
23
+ output = style_transfer_model(content_image, style_image)
24
+ stylized_image = output[0]
25
+
26
+ return Image.fromarray(np.uint8(stylized_image[0] * 255))
27
+
28
+ # Upload content and style images.
29
+ content_image = st.file_uploader("Upload a content image")
30
+ style_image = st.file_uploader("Upload a style image")
31
+
32
+ # default images
33
+ st.write("Or you can choose from the following examples")
34
+ col1, col2, col3 = st.columns(3)
35
+
36
+ if col2.button("Joshua Tree"):
37
+ content_image = "joshua_tree.jpeg"
38
+ style_image = "starry_night.jpeg"
39
+
40
+
41
+
42
+ if style_image and content_image is not None:
43
+ col1, col2 = st.columns(2)
44
+
45
+ content_image = Image.open(content_image)
46
+ # It is recommended that the style image is about 256 pixels (this size was used when training the style transfer network).
47
+ style_image = Image.open(style_image).resize((256, 256))
48
+
49
+ output_image=perform_style_transfer(content_image, style_image)
50
+
51
+ col1.header("Content Image")
52
+ col1.image(content_image, use_column_width=True)
53
+ col2.header("Style Image")
54
+ col2.image(style_image, use_column_width=True)
55
+
56
+ st.header("Output: Style transfer Image")
57
+ st.image(output_image, use_column_width=True)
58
+
59
+ # scroll down to see the references
60
+ st.markdown("**References**")
61
+ st.markdown("<a href='https://www.tensorflow.org/hub/tutorials/tf2_arbitrary_image_stylization' target='_blank'>1. Tutorial to implement Fast Neural Style Transfer using the pretrained model from TensorFlow Hub</a> \n", unsafe_allow_html=True)
62
+ st.markdown("<a href='https://huggingface.co/spaces/luca-martial/neural-style-transfer' target='_blank'>2. The idea to build a neural style transfer application was inspired from this Hugging Face Space </a>", unsafe_allow_html=True)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ numpy==1.21.2
2
+ tensorflow==2.11.0
3
+ tensorflow_hub==0.12.0