File size: 1,939 Bytes
1848a98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import streamlit as st
from keras.models import load_model
import numpy as np
from PIL import Image
from matplotlib import pyplot as plt
import requests


# Load your trained model
model = load_model('Demarker_v1.h5')

# Load and preprocess your custom image
def load_and_preprocess_image(image_path, target_shape=(256, 256)):
    # Open the image using PIL
    custom_image = Image.open(image_path)
    
    # Resize the image to the desired shape
    custom_image = custom_image.resize(target_shape)
    
    # Convert the image to an array
    custom_image = np.array(custom_image)
    
    # Assuming the input image is in RGB format
    # Check if the image needs normalization (pixel values in [0, 255] range)
    if custom_image.max() > 1.0:
        # Normalize the pixel values to the range [-1, 1]
        custom_image = (custom_image.astype(np.float32) - 127.5) / 127.50
    
    # Add a batch dimension
    custom_image = np.expand_dims(custom_image, axis=0)
    
    return custom_image


# Plot source and generated images
def plot_images(src_img, gen_img):
    images = np.vstack((src_img, gen_img))
    # Scale from [-1,1] to [0,1]
    images = (images + 1) / 2.0
    titles = ['Original', 'Generated']
    # Plot images row by row
    for i in range(len(images)):
        # Define subplot
        plt.subplot(1, 2, 1 + i)
        # Turn off axis
        plt.axis('off')
        # Plot raw pixel data
        plt.imshow(images[i])
        # Show title
        plt.title(titles[i])
    st.pyplot()

# Streamlit app
st.title("GAN Image Generator")

# Upload custom image
custom_image = st.file_uploader("Upload a custom image", type=["png", "jpg", "jpeg"])

# Check if an image is uploaded
if custom_image:
    # Generate an image from your custom source
    gen_image = model.predict(load_and_preprocess_image(custom_image))

    # Plot both images together
    plot_images(load_and_preprocess_image(custom_image), gen_image)