File size: 2,873 Bytes
74b05af
 
 
 
1ad0307
 
6918055
1ad0307
 
 
74b05af
460f6cf
 
 
74b05af
e1b32d6
 
 
 
 
74b05af
460f6cf
e1b32d6
460f6cf
74b05af
460f6cf
 
bed4d70
460f6cf
bed4d70
460f6cf
 
bed4d70
460f6cf
bed4d70
460f6cf
 
 
1ad0307
 
bed4d70
1ad0307
460f6cf
1ad0307
 
bed4d70
1ad0307
bed4d70
1ad0307
460f6cf
 
 
1ad0307
460f6cf
 
 
1ad0307
460f6cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ad0307
460f6cf
1ad0307
 
460f6cf
1ad0307
460f6cf
1ad0307
 
460f6cf
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import streamlit as st
import requests
from PIL import Image
from io import BytesIO
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import StreamingResponse
from tensorflow.keras.models import load_model
import numpy as np
import io
import warnings

# Suppress warnings
warnings.filterwarnings('ignore')

# Set Streamlit page configuration
st.set_page_config(
    page_title="Sketch to Image using GAN",
    layout="centered",
    page_icon="๐Ÿ–Œ๏ธ",
)

# Title and description
st.markdown("<h1 style='text-align: center; color: #ff6347;'>Sketch to Image using GAN ๐Ÿ–Œ๏ธ</h1>", unsafe_allow_html=True)
st.markdown("<h2 style='text-align: center; color: #ff6347;'>Upload your sketch to generate an image!</h2>", unsafe_allow_html=True)

# Upload file widget
uploaded_file = st.file_uploader("Upload a sketch (jpg, jpeg, png):", type=["jpg", "jpeg", "png"])

# Model Loading
try:
    generator_model = load_model('model.h5')  # Update this path to your actual model file
    st.success("Model loaded successfully!")
except Exception as e:
    st.error(f"Error loading the model: {str(e)}")

# Image processing function
def process_and_generate_image(image_data):
    image = Image.open(io.BytesIO(image_data)).convert('RGB')
    image = image.resize((256, 256))

    # Preprocess image
    image_array = np.array(image)
    image_array = (image_array - 127.5) / 127.5  # Normalize to [-1, 1]
    image_array = np.expand_dims(image_array, axis=0)

    # Generate fake image
    fake_image = generator_model.predict(image_array)
    fake_image = (fake_image + 1) / 2.0  # Rescale to [0, 1]
    fake_image = np.squeeze(fake_image)
    fake_image = (fake_image * 255).astype(np.uint8)

    return Image.fromarray(fake_image)

# Display uploaded image and handle generation
if uploaded_file is not None:
    st.image(uploaded_file, caption="Uploaded Sketch", width=300)

    if st.button("Generate Image"):
        with st.spinner('Generating...'):
            try:
                # Generate the image
                generated_image = process_and_generate_image(uploaded_file.getvalue())
                
                # Display the generated image
                st.image(generated_image, caption="Generated Image", width=300)
            except Exception as e:
                st.error(f"Error generating image: {str(e)}")

# FastAPI app for backend
app = FastAPI()

@app.post("/generate-image/")
async def generate_image(file: UploadFile = File(...)):
    contents = await file.read()
    generated_image = process_and_generate_image(contents)
    
    img_io = io.BytesIO()
    generated_image.save(img_io, 'JPEG')
    img_io.seek(0)

    return StreamingResponse(img_io, media_type="image/jpeg")

# Running FastAPI app if script is executed directly
if __name__ == '__main__':
    import uvicorn
    uvicorn.run(app, host="127.0.0.1", port=8000)