Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,29 +15,28 @@ img_mode = "RGBA"
|
|
| 15 |
|
| 16 |
@spaces.GPU
|
| 17 |
def realesrgan(img, model_name, denoise_strength, face_enhance, outscale):
|
| 18 |
-
"""Real-ESRGAN function to restore (and upscale) images.
|
| 19 |
-
"""
|
| 20 |
if not img:
|
| 21 |
return
|
| 22 |
|
| 23 |
# Define model parameters
|
| 24 |
-
if model_name == 'RealESRGAN_x4plus':
|
| 25 |
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
|
| 26 |
netscale = 4
|
| 27 |
file_url = ['https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth']
|
| 28 |
-
elif model_name == 'RealESRNet_x4plus':
|
| 29 |
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
|
| 30 |
netscale = 4
|
| 31 |
file_url = ['https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.1/RealESRNet_x4plus.pth']
|
| 32 |
-
elif model_name == 'RealESRGAN_x4plus_anime_6B':
|
| 33 |
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=6, num_grow_ch=32, scale=4)
|
| 34 |
netscale = 4
|
| 35 |
file_url = ['https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth']
|
| 36 |
-
elif model_name == 'RealESRGAN_x2plus':
|
| 37 |
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2)
|
| 38 |
netscale = 2
|
| 39 |
file_url = ['https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth']
|
| 40 |
-
elif model_name == 'realesr-general-x4v3':
|
| 41 |
model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
|
| 42 |
netscale = 4
|
| 43 |
file_url = [
|
|
@@ -45,23 +44,19 @@ def realesrgan(img, model_name, denoise_strength, face_enhance, outscale):
|
|
| 45 |
'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth'
|
| 46 |
]
|
| 47 |
|
| 48 |
-
# Determine model paths
|
| 49 |
model_path = os.path.join('weights', model_name + '.pth')
|
| 50 |
if not os.path.isfile(model_path):
|
| 51 |
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 52 |
for url in file_url:
|
| 53 |
-
# model_path will be updated
|
| 54 |
model_path = load_file_from_url(
|
| 55 |
url=url, model_dir=os.path.join(ROOT_DIR, 'weights'), progress=True, file_name=None)
|
| 56 |
|
| 57 |
-
# Use dni to control the denoise strength
|
| 58 |
dni_weight = None
|
| 59 |
if model_name == 'realesr-general-x4v3' and denoise_strength != 1:
|
| 60 |
wdn_model_path = model_path.replace('realesr-general-x4v3', 'realesr-general-wdn-x4v3')
|
| 61 |
model_path = [model_path, wdn_model_path]
|
| 62 |
dni_weight = [denoise_strength, 1 - denoise_strength]
|
| 63 |
|
| 64 |
-
# Restorer Class
|
| 65 |
upsampler = RealESRGANer(
|
| 66 |
scale=netscale,
|
| 67 |
model_path=model_path,
|
|
@@ -74,7 +69,6 @@ def realesrgan(img, model_name, denoise_strength, face_enhance, outscale):
|
|
| 74 |
gpu_id=None
|
| 75 |
)
|
| 76 |
|
| 77 |
-
# Use GFPGAN for face enhancement
|
| 78 |
if face_enhance:
|
| 79 |
from gfpgan import GFPGANer
|
| 80 |
face_enhancer = GFPGANer(
|
|
@@ -84,11 +78,9 @@ def realesrgan(img, model_name, denoise_strength, face_enhance, outscale):
|
|
| 84 |
channel_multiplier=2,
|
| 85 |
bg_upsampler=upsampler)
|
| 86 |
|
| 87 |
-
# Convert the input PIL image to cv2 image, so that it can be processed by realesrgan
|
| 88 |
cv_img = numpy.array(img)
|
| 89 |
img = cv2.cvtColor(cv_img, cv2.COLOR_RGBA2BGRA)
|
| 90 |
|
| 91 |
-
# Apply restoration
|
| 92 |
try:
|
| 93 |
if face_enhance:
|
| 94 |
_, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
|
|
@@ -98,49 +90,29 @@ def realesrgan(img, model_name, denoise_strength, face_enhance, outscale):
|
|
| 98 |
print('Error', error)
|
| 99 |
print('If you encounter CUDA out of memory, try to set --tile with a smaller number.')
|
| 100 |
else:
|
| 101 |
-
|
| 102 |
-
if img_mode == 'RGBA': # RGBA images should be saved in png format
|
| 103 |
-
extension = 'png'
|
| 104 |
-
else:
|
| 105 |
-
extension = 'jpg'
|
| 106 |
|
| 107 |
out_filename = f"output_{rnd_string(8)}.{extension}"
|
| 108 |
cv2.imwrite(out_filename, output)
|
| 109 |
global last_file
|
| 110 |
last_file = out_filename
|
| 111 |
-
return out_filename
|
| 112 |
|
|
|
|
|
|
|
| 113 |
|
| 114 |
def rnd_string(x):
|
| 115 |
-
"""Returns a string of 'x' random characters
|
| 116 |
-
"""
|
| 117 |
characters = "abcdefghijklmnopqrstuvwxyz_0123456789"
|
| 118 |
-
|
| 119 |
-
return result
|
| 120 |
-
|
| 121 |
|
| 122 |
def reset():
|
| 123 |
-
"""Resets the Image components of the Gradio interface and deletes
|
| 124 |
-
the last processed image
|
| 125 |
-
"""
|
| 126 |
global last_file
|
| 127 |
if last_file:
|
| 128 |
print(f"Deleting {last_file} ...")
|
| 129 |
os.remove(last_file)
|
| 130 |
last_file = None
|
| 131 |
-
return gr.update(value=None), gr.update(value=None)
|
| 132 |
-
|
| 133 |
|
| 134 |
def has_transparency(img):
|
| 135 |
-
"""This function works by first checking to see if a "transparency" property is defined
|
| 136 |
-
in the image's info -- if so, we return "True". Then, if the image is using indexed colors
|
| 137 |
-
(such as in GIFs), it gets the index of the transparent color in the palette
|
| 138 |
-
(img.info.get("transparency", -1)) and checks if it's used anywhere in the canvas
|
| 139 |
-
(img.getcolors()). If the image is in RGBA mode, then presumably it has transparency in
|
| 140 |
-
it, but it double-checks by getting the minimum and maximum values of every color channel
|
| 141 |
-
(img.getextrema()), and checks if the alpha channel's smallest value falls below 255.
|
| 142 |
-
https://stackoverflow.com/questions/43864101/python-pil-check-if-image-is-transparent
|
| 143 |
-
"""
|
| 144 |
if img.info.get("transparency", None) is not None:
|
| 145 |
return True
|
| 146 |
if img.mode == "P":
|
|
@@ -154,69 +126,70 @@ def has_transparency(img):
|
|
| 154 |
return True
|
| 155 |
return False
|
| 156 |
|
| 157 |
-
|
| 158 |
def image_properties(img):
|
| 159 |
"""Returns the dimensions (width and height) and color mode of the input image and
|
| 160 |
also sets the global img_mode variable to be used by the realesrgan function
|
| 161 |
"""
|
| 162 |
global img_mode
|
| 163 |
-
if img:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
if has_transparency(img):
|
| 165 |
img_mode = "RGBA"
|
| 166 |
else:
|
| 167 |
img_mode = "RGB"
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
|
| 172 |
def main():
|
| 173 |
-
# Gradio Interface
|
| 174 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="pink", secondary_hue="rose"), title="Ilaria Upscaler 💖") as app:
|
| 175 |
|
| 176 |
gr.Markdown(
|
| 177 |
"""# <div align="center"> Ilaria Upscaler 💖 </div>
|
| 178 |
"""
|
| 179 |
)
|
| 180 |
-
|
| 181 |
with gr.Accordion("Upscaling option"):
|
| 182 |
with gr.Row():
|
| 183 |
-
model_name = gr.Dropdown(label="
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
minimum=1, maximum=6, step=1, value=4, show_label=True)
|
| 191 |
-
face_enhance = gr.Checkbox(label="Face Enhancement (GFPGAN)",
|
| 192 |
-
)
|
| 193 |
-
|
| 194 |
with gr.Row():
|
| 195 |
with gr.Group():
|
| 196 |
-
input_image = gr.Image(label="Input Image", type="pil"
|
| 197 |
-
|
| 198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
with gr.Row():
|
| 200 |
-
reset_btn = gr.Button("
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
reset_btn.click(fn=reset, inputs=[], outputs=[output_image, input_image])
|
| 209 |
-
# reset_btn.click(None, inputs=[], outputs=[input_image], _js="() => (null)\n")
|
| 210 |
-
# Undocumented method to clear a component's value using Javascript
|
| 211 |
|
| 212 |
gr.Markdown(
|
| 213 |
"""Made with love by Ilaria 💖 | Support me on [Ko-Fi](https://ko-fi.com/ilariaowo) | Using [Real-ESRGAN](https://github.com/xinntao/Real-ESRGAN).
|
| 214 |
-
|
| 215 |
"""
|
| 216 |
)
|
| 217 |
|
| 218 |
app.launch()
|
| 219 |
|
| 220 |
-
|
| 221 |
if __name__ == "__main__":
|
| 222 |
-
main()
|
|
|
|
| 15 |
|
| 16 |
@spaces.GPU
|
| 17 |
def realesrgan(img, model_name, denoise_strength, face_enhance, outscale):
|
| 18 |
+
"""Real-ESRGAN function to restore (and upscale) images."""
|
|
|
|
| 19 |
if not img:
|
| 20 |
return
|
| 21 |
|
| 22 |
# Define model parameters
|
| 23 |
+
if model_name == 'RealESRGAN_x4plus':
|
| 24 |
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
|
| 25 |
netscale = 4
|
| 26 |
file_url = ['https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth']
|
| 27 |
+
elif model_name == 'RealESRNet_x4plus':
|
| 28 |
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
|
| 29 |
netscale = 4
|
| 30 |
file_url = ['https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.1/RealESRNet_x4plus.pth']
|
| 31 |
+
elif model_name == 'RealESRGAN_x4plus_anime_6B':
|
| 32 |
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=6, num_grow_ch=32, scale=4)
|
| 33 |
netscale = 4
|
| 34 |
file_url = ['https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth']
|
| 35 |
+
elif model_name == 'RealESRGAN_x2plus':
|
| 36 |
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2)
|
| 37 |
netscale = 2
|
| 38 |
file_url = ['https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth']
|
| 39 |
+
elif model_name == 'realesr-general-x4v3':
|
| 40 |
model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
|
| 41 |
netscale = 4
|
| 42 |
file_url = [
|
|
|
|
| 44 |
'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth'
|
| 45 |
]
|
| 46 |
|
|
|
|
| 47 |
model_path = os.path.join('weights', model_name + '.pth')
|
| 48 |
if not os.path.isfile(model_path):
|
| 49 |
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 50 |
for url in file_url:
|
|
|
|
| 51 |
model_path = load_file_from_url(
|
| 52 |
url=url, model_dir=os.path.join(ROOT_DIR, 'weights'), progress=True, file_name=None)
|
| 53 |
|
|
|
|
| 54 |
dni_weight = None
|
| 55 |
if model_name == 'realesr-general-x4v3' and denoise_strength != 1:
|
| 56 |
wdn_model_path = model_path.replace('realesr-general-x4v3', 'realesr-general-wdn-x4v3')
|
| 57 |
model_path = [model_path, wdn_model_path]
|
| 58 |
dni_weight = [denoise_strength, 1 - denoise_strength]
|
| 59 |
|
|
|
|
| 60 |
upsampler = RealESRGANer(
|
| 61 |
scale=netscale,
|
| 62 |
model_path=model_path,
|
|
|
|
| 69 |
gpu_id=None
|
| 70 |
)
|
| 71 |
|
|
|
|
| 72 |
if face_enhance:
|
| 73 |
from gfpgan import GFPGANer
|
| 74 |
face_enhancer = GFPGANer(
|
|
|
|
| 78 |
channel_multiplier=2,
|
| 79 |
bg_upsampler=upsampler)
|
| 80 |
|
|
|
|
| 81 |
cv_img = numpy.array(img)
|
| 82 |
img = cv2.cvtColor(cv_img, cv2.COLOR_RGBA2BGRA)
|
| 83 |
|
|
|
|
| 84 |
try:
|
| 85 |
if face_enhance:
|
| 86 |
_, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
|
|
|
|
| 90 |
print('Error', error)
|
| 91 |
print('If you encounter CUDA out of memory, try to set --tile with a smaller number.')
|
| 92 |
else:
|
| 93 |
+
extension = 'png' if img_mode == 'RGBA' else 'jpg'
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
out_filename = f"output_{rnd_string(8)}.{extension}"
|
| 96 |
cv2.imwrite(out_filename, output)
|
| 97 |
global last_file
|
| 98 |
last_file = out_filename
|
|
|
|
| 99 |
|
| 100 |
+
output_img = cv2.cvtColor(output, cv2.COLOR_BGRA2RGBA) if img_mode == "RGBA" else output
|
| 101 |
+
return out_filename, image_properties(output_img)
|
| 102 |
|
| 103 |
def rnd_string(x):
|
|
|
|
|
|
|
| 104 |
characters = "abcdefghijklmnopqrstuvwxyz_0123456789"
|
| 105 |
+
return "".join((random.choice(characters)) for i in range(x))
|
|
|
|
|
|
|
| 106 |
|
| 107 |
def reset():
|
|
|
|
|
|
|
|
|
|
| 108 |
global last_file
|
| 109 |
if last_file:
|
| 110 |
print(f"Deleting {last_file} ...")
|
| 111 |
os.remove(last_file)
|
| 112 |
last_file = None
|
| 113 |
+
return gr.update(value=None), gr.update(value=None), gr.update(value=None)
|
|
|
|
| 114 |
|
| 115 |
def has_transparency(img):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
if img.info.get("transparency", None) is not None:
|
| 117 |
return True
|
| 118 |
if img.mode == "P":
|
|
|
|
| 126 |
return True
|
| 127 |
return False
|
| 128 |
|
|
|
|
| 129 |
def image_properties(img):
|
| 130 |
"""Returns the dimensions (width and height) and color mode of the input image and
|
| 131 |
also sets the global img_mode variable to be used by the realesrgan function
|
| 132 |
"""
|
| 133 |
global img_mode
|
| 134 |
+
if img is None: # Explicitly check for None
|
| 135 |
+
return "No image data available."
|
| 136 |
+
|
| 137 |
+
if isinstance(img, numpy.ndarray): # Handle NumPy array case
|
| 138 |
+
height, width = img.shape[:2]
|
| 139 |
+
channels = img.shape[2] if len(img.shape) > 2 else 1
|
| 140 |
+
img_mode = "RGBA" if channels == 4 else "RGB" if channels == 3 else "Grayscale"
|
| 141 |
+
return f"Resolution: Width: {width}, Height: {height} | Color Mode: {img_mode}"
|
| 142 |
+
|
| 143 |
+
if hasattr(img, "info") and hasattr(img, "mode") and hasattr(img, "size"): # Handle PIL images
|
| 144 |
if has_transparency(img):
|
| 145 |
img_mode = "RGBA"
|
| 146 |
else:
|
| 147 |
img_mode = "RGB"
|
| 148 |
+
return f"Resolution: Width: {img.size[0]}, Height: {img.size[1]} | Color Mode: {img_mode}"
|
| 149 |
+
|
| 150 |
+
return "Unsupported image format."
|
| 151 |
|
| 152 |
def main():
|
|
|
|
| 153 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="pink", secondary_hue="rose"), title="Ilaria Upscaler 💖") as app:
|
| 154 |
|
| 155 |
gr.Markdown(
|
| 156 |
"""# <div align="center"> Ilaria Upscaler 💖 </div>
|
| 157 |
"""
|
| 158 |
)
|
|
|
|
| 159 |
with gr.Accordion("Upscaling option"):
|
| 160 |
with gr.Row():
|
| 161 |
+
model_name = gr.Dropdown(label="Model",
|
| 162 |
+
choices=["RealESRGAN_x4plus", "RealESRNet_x4plus", "RealESRGAN_x4plus_anime_6B", "RealESRGAN_x2plus", "realesr-general-x4v3"],
|
| 163 |
+
value="RealESRGAN_x4plus")
|
| 164 |
+
denoise_strength = gr.Slider(label="Denoise Strength", minimum=0, maximum=1, step=0.1, value=0.5)
|
| 165 |
+
outscale = gr.Slider(label="Resolution Upscale", minimum=1, maximum=6, step=1, value=4)
|
| 166 |
+
face_enhance = gr.Checkbox(label="Face Enhancement")
|
| 167 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
with gr.Row():
|
| 169 |
with gr.Group():
|
| 170 |
+
input_image = gr.Image(label="Input Image", type="pil")
|
| 171 |
+
input_properties = gr.Textbox(label="Input Image Properties", interactive=False)
|
| 172 |
+
|
| 173 |
+
with gr.Group():
|
| 174 |
+
output_image = gr.Image(label="Output Image")
|
| 175 |
+
output_properties = gr.Textbox(label="Output Image Properties", interactive=False)
|
| 176 |
+
|
| 177 |
with gr.Row():
|
| 178 |
+
reset_btn = gr.Button("Reset")
|
| 179 |
+
upscale_btn = gr.Button("Upscale")
|
| 180 |
+
|
| 181 |
+
input_image.change(fn=image_properties, inputs=input_image, outputs=input_properties)
|
| 182 |
+
upscale_btn.click(fn=realesrgan,
|
| 183 |
+
inputs=[input_image, model_name, denoise_strength, face_enhance, outscale],
|
| 184 |
+
outputs=[output_image, output_properties])
|
| 185 |
+
reset_btn.click(fn=reset, inputs=[], outputs=[input_image, output_image, input_properties])
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
gr.Markdown(
|
| 188 |
"""Made with love by Ilaria 💖 | Support me on [Ko-Fi](https://ko-fi.com/ilariaowo) | Using [Real-ESRGAN](https://github.com/xinntao/Real-ESRGAN).
|
|
|
|
| 189 |
"""
|
| 190 |
)
|
| 191 |
|
| 192 |
app.launch()
|
| 193 |
|
|
|
|
| 194 |
if __name__ == "__main__":
|
| 195 |
+
main()
|