Spaces:
Sleeping
Sleeping
Commit
•
61e3be9
1
Parent(s):
dd35df7
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import tensorflow as tf
|
3 |
+
import tensorflow_hub as hub
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import gradio as gr
|
6 |
+
from tensorflow.keras.preprocessing import image
|
7 |
+
from PIL import Image
|
8 |
+
|
9 |
+
def style_transfer(content_image, style_image):
|
10 |
+
# content_image = plt.imread(content_image)
|
11 |
+
# style_image = plt.imread(style_image)
|
12 |
+
content_image = content_image.astype(np.float32)[np.newaxis, ...] / 255.
|
13 |
+
style_image = style_image.astype(np.float32)[np.newaxis, ...] / 255.
|
14 |
+
try:
|
15 |
+
hub_module = hub.load('https://www.kaggle.com/models/google/arbitrary-image-stylization-v1/TensorFlow1/256/2')
|
16 |
+
except OSError:
|
17 |
+
print("Downloading pre-trained model...")
|
18 |
+
hub_module = tf.saved_model.load('https://tfhub.dev/google/arbitrary-image-stylization-v1/256')
|
19 |
+
outputs = hub_module(tf.constant(content_image), tf.constant(style_image))
|
20 |
+
stylized_image = outputs[0].numpy()
|
21 |
+
stylized_image = stylized_image[0] * 255.
|
22 |
+
stylized_image = stylized_image.astype(np.uint8)
|
23 |
+
stylized_image = Image.fromarray(stylized_image)
|
24 |
+
return stylized_image
|
25 |
+
|
26 |
+
interface = gr.Interface(
|
27 |
+
fn=style_transfer,
|
28 |
+
inputs=[
|
29 |
+
gr.Image(label="Content Image (Upload your photo)"),
|
30 |
+
gr.Image(label="Style Image (Choose an artistic style)"),
|
31 |
+
],
|
32 |
+
outputs="image",
|
33 |
+
title="Style Transfer App",
|
34 |
+
description="Apply artistic styles to your photos using deep learning!",
|
35 |
+
)
|
36 |
+
interface.launch(debug=True)
|
37 |
+
|