File size: 1,220 Bytes
5bb2dd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4253cdf
5bb2dd4
386f89a
5bb2dd4
 
 
 
 
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
from io import BytesIO

st.title("Colorize black and white image using an AI model trained on Flickr images with the Pix2pix architecture.")
image = st.file_uploader("Upload an image", type=["jpg", "png","jpeg"])
model = tf.keras.models.load_model('generator_color.keras')

if image :
  button = st.button("Colore")
  image = Image.open(image)
  image = image.convert("L")
  image = image.resize((128,128))
  image = np.array(image)
  if button:
    image = image - 127.5
    image = image / 127.5
    image.shape = (1,128,128,1)

    result = model(image,training = True)
    result = (result * 127.5) + 127.5
    numpy_array = np.array(result.numpy()[0] , dtype=np.uint8)
    pillow_image = Image.fromarray(numpy_array)
    output_path = "output_image.jpg"
    pillow_image.save(output_path)
    st.image([output_path], caption='Colored Image', use_column_width=False)
    st.download_button(
            label="Download Colored Image",
            data=BytesIO(numpy_array.tobytes()),
            file_name="output_image.jpg",
            key="download_button",
            help="Click to download the colored image.",
        )