Sa-m commited on
Commit
f3695f1
·
1 Parent(s): bac96a9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -0
app.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''NEURAL STYLE TRANSFER'''
2
+
3
+
4
+ """##Importing Libraries"""
5
+
6
+
7
+ import gradio as gr
8
+ import tensorflow as tf
9
+ # import os
10
+ import PIL
11
+ from PIL import Image,ImageOps
12
+ import numpy as np
13
+ # import time
14
+ # import requests
15
+ import cv2
16
+ from cv2 import *
17
+
18
+ # !mkdir nstmodel
19
+ # !wget -c https://storage.googleapis.com/tfhub-modules/google/magenta/arbitrary-image-stylization-v1-256/2.tar.gz -O - | tar -xz -C /nstmodel
20
+ # import tensorflow.keras
21
+
22
+ # from PIL import Image, ImageOps
23
+ #import requests
24
+ #import tarfile
25
+ '''
26
+ url = "https://storage.googleapis.com/tfhub-modules/google/magenta/arbitrary-image-stylization-v1-256/2.tar.gz"
27
+ response = requests.get(url,stream=True)
28
+ path_input="./"
29
+ urllib.request.urlretrieve(url, filename=path_input)
30
+ file = tarfile.open(fileobj=response.raw, mode="r|gz")
31
+ file.extractall(path="./nst_model")
32
+ '''
33
+ MODEL_PATH='Nst model'
34
+
35
+ # Disable scientific notation for clarity
36
+ np.set_printoptions(suppress=True)
37
+
38
+ # Load the model
39
+ model = tf.keras.models.load_model(MODEL_PATH)
40
+
41
+ def tensor_to_image(tensor):
42
+ tensor = tensor*255
43
+ tensor = np.array(tensor, dtype=np.uint8)
44
+ if np.ndim(tensor)>3:
45
+ assert tensor.shape[0] == 1
46
+ tensor = tensor[0]
47
+ return PIL.Image.fromarray(tensor)
48
+
49
+ """##Saving unscaled Tensor images."""
50
+
51
+ def save_image(image, filename):
52
+ """
53
+ Saves unscaled Tensor Images.
54
+ Args:
55
+ image: 3D image tensor. [height, width, channels]
56
+ filename: Name of the file to save to.
57
+ """
58
+ if not isinstance(image, Image.Image):
59
+ image = tf.clip_by_value(image, 0, 255)
60
+ image = Image.fromarray(tf.cast(image, tf.uint8).numpy())
61
+ image.save("%s.jpg" % filename)
62
+ print("Saved as %s.jpg" % filename)
63
+
64
+ """## Grayscaling image for testing purpose to check if we could get better results."""
65
+
66
+
67
+
68
+ def gray_scaled(inp_img):
69
+ gray = cv2.cvtColor(inp_img, cv2.COLOR_BGR2GRAY)
70
+ gray_img = np.zeros_like(inp_img)
71
+ gray_img[:,:,0] = gray
72
+ gray_img[:,:,1] = gray
73
+ gray_img[:,:,2] = gray
74
+ return gray_img
75
+
76
+ def transform_mymodel(content_image,style_image):
77
+ # Convert to float32 numpy array, add batch dimension, and normalize to range [0, 1]
78
+ content_image=gray_scaled(content_image)
79
+ content_image = content_image.astype(np.float32)[np.newaxis, ...] / 255.0
80
+ style_image = style_image.astype(np.float32)[np.newaxis, ...] / 255.0
81
+
82
+ #Resizing image
83
+ style_image = tf.image.resize(style_image, (256, 256))
84
+
85
+ # Stylize image
86
+ outputs = model(tf.constant(content_image), tf.constant(style_image))
87
+ stylized_image = outputs[0]
88
+
89
+ # stylized = tf.image.resize(stylized_image, (356, 356))
90
+ stylized_image =tensor_to_image(stylized_image)
91
+ save_image(stylized_image,'stylized')
92
+ return stylized_image
93
+
94
+ def gradio_intrface(mymodel):
95
+ # Initializing the input component
96
+ image1 = gr.inputs.Image() #CONTENT IMAGE
97
+ image2 = gr.inputs.Image() #STYLE IMAGE
98
+ stylizedimg=gr.outputs.Image()
99
+ gr.Interface(fn=mymodel, inputs= [image1,image2] , outputs= stylizedimg,title='Style Transfer').launch()
100
+
101
+ """The function will be launched both Inline and Outline where u need to add a content and style image."""
102
+
103
+
104
+ gradio_intrface(transform_mymodel)
105
+
106
+
107
+