nahidalam commited on
Commit
96eb944
·
1 Parent(s): 223671b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -1
app.py CHANGED
@@ -1,6 +1,10 @@
1
  import gradio as gr
2
  import numpy as np
 
 
 
3
 
 
4
  def sepia(input_img):
5
  sepia_filter = np.array([[.393, .769, .189],
6
  [.349, .686, .168],
@@ -8,7 +12,33 @@ def sepia(input_img):
8
  sepia_img = input_img.dot(sepia_filter.T)
9
  sepia_img /= sepia_img.max()
10
  return sepia_img
 
 
 
 
 
 
11
 
12
- iface = gr.Interface(sepia, gr.inputs.Image(shape=(200, 200)), "image")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  iface.launch(share = True)
 
1
  import gradio as gr
2
  import numpy as np
3
+ import tensorflow as tf
4
+ import PIL
5
+ import os
6
 
7
+ '''
8
  def sepia(input_img):
9
  sepia_filter = np.array([[.393, .769, .189],
10
  [.349, .686, .168],
 
12
  sepia_img = input_img.dot(sepia_filter.T)
13
  sepia_img /= sepia_img.max()
14
  return sepia_img
15
+ '''
16
+
17
+ def normalize_img(img):
18
+ img = tf.cast(img, dtype=tf.float32)
19
+ # Map values in the range [-1, 1]
20
+ return (img / 127.5) - 1.0
21
 
22
+ def predict_and_save(img, generator_model):
23
+ img = normalize_img(img)
24
+ prediction = generator_model(img, training=False)[0].numpy()
25
+ prediction = (prediction * 127.5 + 127.5).astype(np.uint8)
26
+ im = PIL.Image.fromarray(prediction)
27
+ return im
28
+
29
+ def run(image_path):
30
+ model = tf.keras.models.load_model('generator')
31
+ img = tf.keras.preprocessing.image.load_img(
32
+ image_path, target_size=(256, 256)
33
+ )
34
+
35
+ #https://www.tensorflow.org/api_docs/python/tf/keras/utils/load_img
36
+
37
+ img_array = tf.keras.preprocessing.image.img_to_array(img)
38
+ img_array = tf.expand_dims(img_array, 0)
39
+
40
+ predict_and_save(img_array, model)
41
+
42
+ iface = gr.Interface(sepia, gr.inputs.Image(shape=(256, 256)), "image")
43
 
44
  iface.launch(share = True)