File size: 1,408 Bytes
61e3be9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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)