Spaces:
Running
Running
File size: 6,027 Bytes
a1074ad |
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
import streamlit as st
from PIL import Image
import torch
from RealESRGAN import RealESRGAN
from io import BytesIO
import base64
import streamlit.components.v1 as components
# Function to load the model based on scale and anime toggle
def load_model(scale, anime=False):
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = RealESRGAN(device, scale=scale, anime=anime)
model_path = {
(2, False): 'model/RealESRGAN_x2.pth',
(4, False): 'model/RealESRGAN_x4plus.pth',
(8, False): 'model/RealESRGAN_x8.pth',
(4, True): 'model/RealESRGAN_x4plus_anime_6B.pth'
}[(scale, anime)]
model.load_weights(model_path)
return model
def enhance_image(image, scale, anime):
model = load_model(scale, anime=anime)
sr_image = model.predict(image)
buffer = BytesIO()
sr_image.save(buffer, format="PNG")
buffer.seek(0)
return sr_image, buffer
def get_base64_image(image):
buffered = BytesIO()
image.save(buffered, format="PNG")
return base64.b64encode(buffered.getvalue()).decode()
def image_comparison_slider(original_base64, enhanced_base64):
slider_html = f"""
<style>
.img-comp-container {{
position: relative;
width: 100%;
max-width: 800px;
margin: auto;
}}
.img-comp-img {{
position: absolute;
width: 100%;
height: 100%;
overflow: hidden;
}}
.img-comp-img img {{
width: 100%;
height: auto;
display: block;
}}
.img-comp-overlay {{
position: absolute;
top: 0;
left: 0;
width: 50%; /* Start at 50% for better default view */
height: 100%;
overflow: hidden;
background-color: rgba(0,0,0,0.5);
transition: 0.4s;
}}
.img-comp-slider {{
position: absolute;
cursor: ew-resize;
width: 40px;
height: 40px;
background-color: #2196F3;
border-radius: 50%;
transform: translateY(-50%);
top: 50%;
left: 50%;
margin-left: -20px;
margin-top: -20px;
z-index: 9;
opacity: 0.7;
transition: 0.3s;
}}
.img-comp-slider:hover {{
opacity: 1;
}}
</style>
<div class="img-comp-container">
<div class="img-comp-img">
<img src="data:image/png;base64,{original_base64}" />
</div>
<div class="img-comp-img">
<img src="data:image/png;base64,{enhanced_base64}" />
</div>
<div class="img-comp-overlay"></div>
<div class="img-comp-slider"></div>
</div>
<script>
</script>
"""
components.html(slider_html, height=600, scrolling=True)
def main():
st.title("Generative AI Image Restoration")
# Image upload
uploaded_image = st.file_uploader("Upload Image", type=["png", "jpg", "jpeg"])
if uploaded_image is not None:
image = Image.open(uploaded_image)
st.image(image, caption="Original Image", use_column_width=True)
# Anime toggle
anime = st.checkbox("Anime Image", value=False)
# Conditional scale options
if anime:
scale = "4x" # Set to 4x automatically when anime is selected
else:
scale = st.radio("Upscaling Factor", ["2x", "4x", "8x"], index=0)
scale_value = int(scale.replace('x', ''))
# Enhance button
if st.button("Restore Image"):
enhanced_image, buffer = enhance_image(image, scale_value, anime)
# Convert images to base64 for comparison slider
original_base64 = get_base64_image(image)
enhanced_base64 = get_base64_image(enhanced_image)
# Show comparison slider
image_comparison_slider(original_base64, enhanced_base64)
# Download button
st.download_button(
label="Download Enhanced Image",
data=buffer,
file_name="enhanced_image.png",
mime="image/png"
)
if __name__ == "__main__":
main()
|