Spaces:
Sleeping
Sleeping
import numpy as np | |
import tensorflow as tf | |
import tensorflow_hub as hub | |
import matplotlib.pyplot as plt | |
import gradio as gr | |
from tensorflow.keras.preprocessing import image | |
from PIL import Image | |
def style_transfer(content_image, style_image): | |
# content_image = plt.imread(content_image) | |
# style_image = plt.imread(style_image) | |
content_image = content_image.astype(np.float32)[np.newaxis, ...] / 255. | |
style_image = style_image.astype(np.float32)[np.newaxis, ...] / 255. | |
try: | |
hub_module = hub.load('https://www.kaggle.com/models/google/arbitrary-image-stylization-v1/TensorFlow1/256/2') | |
except OSError: | |
print("Downloading pre-trained model...") | |
hub_module = tf.saved_model.load('https://tfhub.dev/google/arbitrary-image-stylization-v1/256') | |
outputs = hub_module(tf.constant(content_image), tf.constant(style_image)) | |
stylized_image = outputs[0].numpy() | |
stylized_image = stylized_image[0] * 255. | |
stylized_image = stylized_image.astype(np.uint8) | |
stylized_image = Image.fromarray(stylized_image) | |
return stylized_image | |
interface = gr.Interface( | |
fn=style_transfer, | |
inputs=[ | |
gr.Image(label="Content Image (Upload your photo)"), | |
gr.Image(label="Style Image (Choose an artistic style)"), | |
], | |
outputs="image", | |
title="Style Transfer App", | |
description="Apply artistic styles to your photos using deep learning!", | |
) | |
interface.launch(debug=True) | |