Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import tensorflow_addons as tfa
|
| 6 |
+
import cv2
|
| 7 |
+
import tensorflow_hub as hub
|
| 8 |
+
import tensorflow as tf
|
| 9 |
+
from tensorflow.keras.utils import custom_object_scope
|
| 10 |
+
|
| 11 |
+
os.environ['TFHUB_MODEL_LOAD_FORMAT'] = 'COMPRESSED'
|
| 12 |
+
|
| 13 |
+
def tensor_to_image(tensor):
|
| 14 |
+
tensor = tensor * 255
|
| 15 |
+
tensor = np.array(tensor, dtype=np.uint8)
|
| 16 |
+
if np.ndim(tensor) > 3:
|
| 17 |
+
assert tensor.shape[0] == 1
|
| 18 |
+
tensor = tensor[0]
|
| 19 |
+
return Image.fromarray(tensor)
|
| 20 |
+
|
| 21 |
+
def load(file_uploader):
|
| 22 |
+
max_dim = 512
|
| 23 |
+
image = file_uploader
|
| 24 |
+
image = tf.image.decode_image(image.read(), channels=3)
|
| 25 |
+
image = tf.image.convert_image_dtype(image, tf.float32)
|
| 26 |
+
|
| 27 |
+
shape = tf.cast(tf.shape(image)[:-1], tf.float32)
|
| 28 |
+
long_dim = max(shape)
|
| 29 |
+
scale = max_dim / long_dim
|
| 30 |
+
|
| 31 |
+
new_shape = tf.cast(shape * scale, tf.int32)
|
| 32 |
+
|
| 33 |
+
image = tf.image.resize(image, (256, 256)) # Resize to a consistent shape (256, 256)
|
| 34 |
+
image = image[tf.newaxis, :]
|
| 35 |
+
return image
|
| 36 |
+
|
| 37 |
+
st.title("Sketch to Real Image Pix2Pix")
|
| 38 |
+
inp = st.file_uploader("Sketch input")
|
| 39 |
+
style = load('')
|
| 40 |
+
|
| 41 |
+
if inp is not None:
|
| 42 |
+
img = load(inp)
|
| 43 |
+
hub_model = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')
|
| 44 |
+
|
| 45 |
+
stylized_image = hub_model(img, img)[0]
|
| 46 |
+
pred = tensor_to_image(stylized_image)
|
| 47 |
+
|
| 48 |
+
toshow = np.array(img[0].numpy() * 255, dtype=np.uint8) # Convert to uint8
|
| 49 |
+
st.image(toshow, caption="Uploaded Image")
|
| 50 |
+
st.image(pred, caption="Generated Real Face")
|