File size: 1,127 Bytes
550384e
 
 
 
 
fa3671e
550384e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fab79c8
 
550384e
fab79c8
550384e
 
 
 
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
import streamlit as st
import tensorflow as tf
import numpy as np
from PIL import Image
import tensorflow_addons as tfa
import cv2
import tensorflow as tf
from tensorflow.keras.utils import custom_object_scope

# Define a function to create the InstanceNormalization layer
def create_in():
    return tfa.layers.InstanceNormalization()


def model_out(model_path,img):
  with custom_object_scope({'InstanceNormalization': create_in}):
    model = tf.keras.models.load_model(model_path)
  img = (img-127.5)/127.5
  img = np.expand_dims(img, 0)
  pred = model.predict(img)
  pred = np.asarray(pred)
  return pred[0]

st.title("GrayScale to Colorized Image Pix2Pix")
day_inp = st.file_uploader("Grayscale image input")

if day_inp is not None:
  file_bytes = day_inp.read()
  img = cv2.imdecode(np.frombuffer(file_bytes, np.uint8), cv2.IMREAD_GRAYSCALE)
  img = cv2.resize(img, (256, 256))
  img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
  img = np.array(img)
  pred = model_out('colorizer.h5', img)
  st.image(img, caption="Uploaded Image")
  st.image(((pred + 1) * 127.5).astype(np.uint8), caption="Generated Colorized Painting")