Aditya757864 commited on
Commit
3a76a8c
·
1 Parent(s): 617d322

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ hub_module = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')
11
+
12
+ def tensor_to_image(tensor):
13
+ tensor = tensor*255
14
+ tensor = np.array(tensor, dtype=np.uint8)
15
+ if np.ndim(tensor) > 3:
16
+ assert tensor.shape[0] == 1
17
+ tensor = tensor[0]
18
+ return PIL.Image.fromarray(tensor)
19
+
20
+ content_image_input = gr.Image(label="Content Image")
21
+ style_image_input = gr.Image(label="Style Image")
22
+
23
+ text = "#Developer: Aditya Jadhav #College: Government Polytechnic Nagpur #Branch: AIML (Artificial Intelligence and Machine Learning), 3rd year #Date: 17th November 2023 #LinkedIn: Aditya Jadhav's LinkedIn #GitHub: Aditya Jadhav's GitHub"
24
+ my_list = text.split("\n")
25
+ my_string = "\n".join(my_list)
26
+
27
+ text2 = "The application focuses on neural style transfer, where the style from a style image is applied to a content image."
28
+
29
+ def perform_neural_transfer(content_image_input, style_image_input):
30
+ # Load content images
31
+ content_image = content_image_input.astype(np.float32)[np.newaxis, ...] / 255.
32
+ style_image = style_image_input.astype(np.float32)[np.newaxis, ...] / 255.
33
+
34
+ # Apply neural style transfer
35
+ outputs = hub_module(tf.constant(content_image), tf.constant(style_image))
36
+ stylized_image = outputs[0]
37
+
38
+ return tensor_to_image(stylized_image)
39
+
40
+
41
+ app_interface = gr.Interface(
42
+ fn=perform_neural_transfer,
43
+ inputs=[content_image_input, style_image_input],
44
+ outputs="image",
45
+ title="Art Generation with Neural Style Transfer",
46
+ article=my_string,
47
+ description=text2,
48
+ theme = 'Default',
49
+ concurrency_limit =2,
50
+ examples=[
51
+ ['content1.jpg','image1.jpg'],
52
+ [None,'image2.jpg'],
53
+ [None,'image4.jpg'],
54
+ [None,'image5.jpg'],
55
+ [None,'image6.jpg'],
56
+ ]
57
+ )
58
+
59
+ app_interface.launch(debug =True)