shainis commited on
Commit
b167b38
1 Parent(s): 6b7c188

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import tensorflow as tf
3
+ os.environ['TFHUB_MODEL_LOAD_FORMAT'] = 'COMPRESSED'
4
+ import numpy as np
5
+ import PIL.Image
6
+ import gradio as gr
7
+ import tensorflow_hub as hub
8
+ import matplotlib.pyplot as plt
9
+
10
+
11
+ def tensor_to_image(tensor):
12
+ tensor = tensor*255
13
+ tensor = np.array(tensor, dtype=np.uint8)
14
+ if np.ndim(tensor)>3:
15
+ assert tensor.shape[0] == 1
16
+ tensor = tensor[0]
17
+ return PIL.Image.fromarray(tensor)
18
+
19
+
20
+ style_urls = {
21
+ 'Kanagawa great wave': 'The_Great_Wave_off_Kanagawa.jpg',
22
+ 'Kandinsky composition 7': 'Kandinsky_Composition_7.jpg',
23
+ 'Hubble pillars of creation': 'Pillars_of_creation_2014_HST_WFC3-UVIS_full-res_denoised.jpg',
24
+ 'Van gogh starry night': 'Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg',
25
+ 'Turner nantes': 'JMW_Turner_-_Nantes_from_the_Ile_Feydeau.jpg',
26
+ 'Munch scream': 'Edvard_Munch.jpg',
27
+ 'Picasso demoiselles avignon': 'Les_Demoiselles.jpg',
28
+ 'Picasso violin': 'picaso_violin.jpg',
29
+ 'Picasso bottle of rum': 'picaso_rum.jpg',
30
+ 'Fire': 'Large_bonfire.jpg',
31
+ 'Derkovits woman head': 'Derkovits_Gyula_Woman_head_1922.jpg',
32
+ 'Amadeo style life': 'Amadeo_Souza_Cardoso.jpg',
33
+ 'Derkovtis talig': 'Derkovits_Gyula_Talig.jpg',
34
+ 'Kadishman': 'kadishman.jpeg'
35
+ }
36
+
37
+
38
+ style_images = [k for k, v in style_urls.items()]
39
+
40
+
41
+ content_image_input = gr.inputs.Image(label="Content Image")
42
+ radio_style = gr.Radio(style_images, label="Choose Style")
43
+
44
+
45
+ def perform_neural_transfer(content_image_input, style_image_input):
46
+
47
+ content_image = content_image_input.astype(np.float32)[np.newaxis, ...] / 255.
48
+
49
+ style_image_input = style_urls[style_image_input]
50
+ style_image_input = plt.imread(style_image_input)
51
+ style_image = style_image_input.astype(np.float32)[np.newaxis, ...] / 255.
52
+
53
+ style_image = tf.image.resize(style_image, (256, 256))
54
+
55
+ hub_module = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')
56
+
57
+ outputs = hub_module(tf.constant(content_image), tf.constant(style_image))
58
+ stylized_image = outputs[0]
59
+
60
+ return tensor_to_image(stylized_image)
61
+
62
+
63
+ app_interface = gr.Interface(fn=perform_neural_transfer,
64
+ inputs=[content_image_input, radio_style],
65
+ outputs="image",
66
+ title="Art Generation with Neural Style Transfer",
67
+ )
68
+ app_interface.launch()