Spaces:
Runtime error
Runtime error
add some diff
Browse files
app.py
CHANGED
@@ -22,6 +22,31 @@ def tensor_to_image(tensor):
|
|
22 |
return PIL.Image.fromarray(tensor)
|
23 |
|
24 |
import tensorflow_hub as hub
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
hub_model = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')
|
26 |
stylized_image = hub_model(tf.constant(content_image), tf.constant(style_image))[0]
|
27 |
print("结果:", stylized_image)
|
|
|
22 |
return PIL.Image.fromarray(tensor)
|
23 |
|
24 |
import tensorflow_hub as hub
|
25 |
+
|
26 |
+
content_path = tf.keras.utils.get_file('YellowLabradorLooking_new.jpg',
|
27 |
+
'https://storage.googleapis.com/download.tensorflow.org/example_images/YellowLabradorLooking_new.jpg')
|
28 |
+
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')
|
29 |
+
|
30 |
+
def load_img(path_to_img):
|
31 |
+
max_dim = 512
|
32 |
+
img = tf.io.read_file(path_to_img)
|
33 |
+
img = tf.image.decode_image(img, channels=3)
|
34 |
+
img = tf.image.convert_image_dtype(img, tf.float32)
|
35 |
+
|
36 |
+
shape = tf.cast(tf.shape(img)[:-1], tf.float32)
|
37 |
+
long_dim = max(shape)
|
38 |
+
scale = max_dim / long_dim
|
39 |
+
|
40 |
+
new_shape = tf.cast(shape * scale, tf.int32)
|
41 |
+
|
42 |
+
img = tf.image.resize(img, new_shape)
|
43 |
+
img = img[tf.newaxis, :]
|
44 |
+
return img
|
45 |
+
|
46 |
+
content_image = load_img(content_path)
|
47 |
+
style_image = load_img(style_path)
|
48 |
+
|
49 |
+
|
50 |
hub_model = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')
|
51 |
stylized_image = hub_model(tf.constant(content_image), tf.constant(style_image))[0]
|
52 |
print("结果:", stylized_image)
|