Spaces:
Runtime error
Runtime error
File size: 2,164 Bytes
5450593 412612e f29c5e8 aed8aca 5450593 041a6b3 5450593 99399cc 5450593 e3a4b64 5450593 |
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
import os
os.system("pip install tensorflow==2.3.0")
os.system("pip install tensorflow_hub")
os.system("pip install numpy==1.20.3")
import tensorflow as tf
# Load compressed models from tensorflow_hub
os.environ['TFHUB_MODEL_LOAD_FORMAT'] = 'COMPRESSED'
import numpy as np
import PIL.Image
import time
import functools
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)
import tensorflow_hub as hub
content_path = tf.keras.utils.get_file('YellowLabradorLooking_new.jpg',
'https://storage.googleapis.com/download.tensorflow.org/example_images/YellowLabradorLooking_new.jpg')
style_path = tf.keras.utils.get_file('kandinsky5.jpg','https://storage.googleapis.com/download.tensorflow.org/example_images/Vassily_Kandinsky%2C_1913_-_Composition_7.jpg')
def load_img(path_to_img):
max_dim = 512
img = tf.io.read_file(path_to_img)
img = tf.image.decode_image(img, channels=3)
img = tf.image.convert_image_dtype(img, tf.float32)
shape = tf.cast(tf.shape(img)[:-1], tf.float32)
long_dim = max(shape)
scale = max_dim / long_dim
new_shape = tf.cast(shape * scale, tf.int32)
img = tf.image.resize(img, new_shape)
img = img[tf.newaxis, :]
return img
content_image = load_img(content_path)
style_image = load_img(style_path)
hub_model = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')
stylized_image = hub_model(tf.constant(content_image), tf.constant(style_image))[0]
print("结果:", stylized_image)
import gradio as gr
def inference(content_image, style_image):
stylized_image = hub_model(tf.constant(content_image), tf.constant(style_image))[0]
img = tensor_to_image(stylized_image)
return img
title = "TTT"
gr.Interface(
inference,
gr.inputs.Image(type="pil", label="content_image"),
gr.inputs.Image(type="pil", label="style_image"),
gr.outputs.Image(type="pil", label="Output"),
title=title,
description="",
enable_queue=True,
allow_flagging=False
).launch()
|