Spaces:
Sleeping
Sleeping
| import os | |
| import tensorflow as tf | |
| os.environ['TFHUB_MODEL_LOAD_FORMAT'] = 'COMPRESSED' | |
| import numpy as np | |
| import PIL.Image | |
| import gradio as gr | |
| import tensorflow_hub as hub | |
| import matplotlib.pyplot as plt | |
| hub_module = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2') | |
| def tensor_to_image(tensor): | |
| tensor = tensor*255 | |
| tensor = np.array(tensor, dtype=np.uint8) | |
| if np.ndim(tensor) > 3: | |
| assert tensor.shape[0] == 1 | |
| tensor = tensor[0] | |
| return PIL.Image.fromarray(tensor) | |
| content_image_input = gr.Image(label="Content Image", sources = ('upload', 'webcam')) | |
| style_image_input = gr.Image(label="Style Image") | |
| text = "#Developer: Aditya Jadhav #College: Government Polytechnic Nagpur #Branch: AIML (Artificial Intelligence and Machine Learning), 3rd year #Date: 17th November 2023 #LinkedIn: https://www.linkedin.com/in/aditya-jadhav-27a790269 #GitHub: https://github.com/AdityaJ9801 #Introduction: The application focuses on neural style transfer, where the style from a style image is applied to a content image." | |
| def perform_neural_transfer(content_image_input, style_image_input): | |
| if content_image_input is None: | |
| # Handle the case when content image is not provided | |
| return PIL.Image.fromarray(np.zeros((1, 1, 3), dtype=np.uint8)) | |
| # Load content images | |
| content_image = content_image_input.astype(np.float32)[np.newaxis, ...] / 255. | |
| if style_image_input is None: | |
| # Handle the case when style image is not provided | |
| return tensor_to_image(content_image) | |
| style_image = style_image_input.astype(np.float32)[np.newaxis, ...] / 255. | |
| # Apply neural style transfer | |
| outputs = hub_module(tf.constant(content_image), tf.constant(style_image)) | |
| stylized_image = outputs[0] | |
| return tensor_to_image(stylized_image) | |
| app_interface = gr.Interface( | |
| fn=perform_neural_transfer, | |
| inputs=[content_image_input, style_image_input], | |
| outputs="image", | |
| title="Art Generation with Neural Style Transfer", | |
| description=text, | |
| concurrency_limit = 5, | |
| theme = gr.themes.Default(), | |
| examples=[ | |
| ['content1.jpg','image1.jpg'], | |
| [None,'image2.jpg'], | |
| [None,'image4.jpg'], | |
| [None,'image5.jpg'], | |
| [None,'image6.jpg'], | |
| [None,'image7.jpg'], | |
| [None,'image8.jpg'], | |
| ] | |
| ) | |
| app_interface.launch(debug =True) | |