Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
import os
|
| 2 |
-
|
| 3 |
import sys
|
| 4 |
from torchvision.transforms import functional
|
| 5 |
sys.modules["torchvision.transforms.functional_tensor"] = functional
|
|
@@ -12,8 +11,7 @@ import torch
|
|
| 12 |
import cv2
|
| 13 |
import gradio as gr
|
| 14 |
|
| 15 |
-
|
| 16 |
-
#Download Required Models
|
| 17 |
if not os.path.exists('realesr-general-x4v3.pth'):
|
| 18 |
os.system("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth -P .")
|
| 19 |
if not os.path.exists('GFPGANv1.2.pth'):
|
|
@@ -25,20 +23,14 @@ if not os.path.exists('GFPGANv1.4.pth'):
|
|
| 25 |
if not os.path.exists('RestoreFormer.pth'):
|
| 26 |
os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/RestoreFormer.pth -P .")
|
| 27 |
|
| 28 |
-
|
| 29 |
model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
|
| 30 |
model_path = 'realesr-general-x4v3.pth'
|
| 31 |
half = True if torch.cuda.is_available() else False
|
| 32 |
upsampler = RealESRGANer(scale=4, model_path=model_path, model=model, tile=0, tile_pad=10, pre_pad=0, half=half)
|
| 33 |
|
| 34 |
-
|
| 35 |
-
# Save Image to the Directory
|
| 36 |
-
# os.makedirs('output', exist_ok=True)
|
| 37 |
-
|
| 38 |
def upscaler(img, version, scale):
|
| 39 |
-
|
| 40 |
try:
|
| 41 |
-
|
| 42 |
img = cv2.imread(img, cv2.IMREAD_UNCHANGED)
|
| 43 |
if len(img.shape) == 3 and img.shape[2] == 4:
|
| 44 |
img_mode = 'RGBA'
|
|
@@ -48,26 +40,22 @@ def upscaler(img, version, scale):
|
|
| 48 |
else:
|
| 49 |
img_mode = None
|
| 50 |
|
| 51 |
-
|
| 52 |
h, w = img.shape[0:2]
|
| 53 |
if h < 300:
|
| 54 |
img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
|
| 55 |
|
| 56 |
-
|
| 57 |
face_enhancer = GFPGANer(
|
| 58 |
-
model_path=f'{version}.pth',
|
| 59 |
-
upscale=2,
|
| 60 |
-
arch='RestoreFormer' if version=='RestoreFormer' else 'clean',
|
| 61 |
channel_multiplier=2,
|
| 62 |
bg_upsampler=upsampler
|
| 63 |
)
|
| 64 |
|
| 65 |
-
|
| 66 |
try:
|
| 67 |
_, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
|
| 68 |
except RuntimeError as error:
|
| 69 |
-
print('
|
| 70 |
-
|
| 71 |
|
| 72 |
try:
|
| 73 |
if scale != 2:
|
|
@@ -75,40 +63,25 @@ def upscaler(img, version, scale):
|
|
| 75 |
h, w = img.shape[0:2]
|
| 76 |
output = cv2.resize(output, (int(w * scale / 2), int(h * scale / 2)), interpolation=interpolation)
|
| 77 |
except Exception as error:
|
| 78 |
-
print('
|
| 79 |
-
|
| 80 |
-
# Save Image to the Directory
|
| 81 |
-
# ext = os.path.splitext(os.path.basename(str(img)))[1]
|
| 82 |
-
# if img_mode == 'RGBA':
|
| 83 |
-
# ext = 'png'
|
| 84 |
-
# else:
|
| 85 |
-
# ext = 'jpg'
|
| 86 |
-
#
|
| 87 |
-
# save_path = f'output/out.{ext}'
|
| 88 |
-
# cv2.imwrite(save_path, output)
|
| 89 |
-
# return output, save_path
|
| 90 |
|
| 91 |
output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
|
| 92 |
return output
|
| 93 |
except Exception as error:
|
| 94 |
-
print('global
|
| 95 |
return None, None
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
| 98 |
-
|
| 99 |
-
title = "Image Upscaler & Restoring [GFPGAN Algorithm]"
|
| 100 |
-
|
| 101 |
demo = gr.Interface(
|
| 102 |
upscaler, [
|
| 103 |
-
gr.Image(type="filepath", label="
|
| 104 |
-
gr.Radio(['GFPGANv1.2', 'GFPGANv1.3', 'GFPGANv1.4', 'RestoreFormer'], type="value", label='
|
| 105 |
-
gr.Number(label="
|
| 106 |
], [
|
| 107 |
-
gr.Image(type="numpy", label="
|
| 108 |
],
|
| 109 |
-
title=title,
|
| 110 |
allow_flagging="never"
|
| 111 |
)
|
| 112 |
|
| 113 |
demo.queue()
|
| 114 |
-
demo.launch()
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import sys
|
| 3 |
from torchvision.transforms import functional
|
| 4 |
sys.modules["torchvision.transforms.functional_tensor"] = functional
|
|
|
|
| 11 |
import cv2
|
| 12 |
import gradio as gr
|
| 13 |
|
| 14 |
+
# Baixar Modelos Necessários
|
|
|
|
| 15 |
if not os.path.exists('realesr-general-x4v3.pth'):
|
| 16 |
os.system("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth -P .")
|
| 17 |
if not os.path.exists('GFPGANv1.2.pth'):
|
|
|
|
| 23 |
if not os.path.exists('RestoreFormer.pth'):
|
| 24 |
os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/RestoreFormer.pth -P .")
|
| 25 |
|
|
|
|
| 26 |
model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
|
| 27 |
model_path = 'realesr-general-x4v3.pth'
|
| 28 |
half = True if torch.cuda.is_available() else False
|
| 29 |
upsampler = RealESRGANer(scale=4, model_path=model_path, model=model, tile=0, tile_pad=10, pre_pad=0, half=half)
|
| 30 |
|
| 31 |
+
# Função de Upscaling e Restauração de Imagem
|
|
|
|
|
|
|
|
|
|
| 32 |
def upscaler(img, version, scale):
|
|
|
|
| 33 |
try:
|
|
|
|
| 34 |
img = cv2.imread(img, cv2.IMREAD_UNCHANGED)
|
| 35 |
if len(img.shape) == 3 and img.shape[2] == 4:
|
| 36 |
img_mode = 'RGBA'
|
|
|
|
| 40 |
else:
|
| 41 |
img_mode = None
|
| 42 |
|
|
|
|
| 43 |
h, w = img.shape[0:2]
|
| 44 |
if h < 300:
|
| 45 |
img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
|
| 46 |
|
|
|
|
| 47 |
face_enhancer = GFPGANer(
|
| 48 |
+
model_path=f'{version}.pth',
|
| 49 |
+
upscale=2,
|
| 50 |
+
arch='RestoreFormer' if version == 'RestoreFormer' else 'clean',
|
| 51 |
channel_multiplier=2,
|
| 52 |
bg_upsampler=upsampler
|
| 53 |
)
|
| 54 |
|
|
|
|
| 55 |
try:
|
| 56 |
_, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
|
| 57 |
except RuntimeError as error:
|
| 58 |
+
print('Erro', error)
|
|
|
|
| 59 |
|
| 60 |
try:
|
| 61 |
if scale != 2:
|
|
|
|
| 63 |
h, w = img.shape[0:2]
|
| 64 |
output = cv2.resize(output, (int(w * scale / 2), int(h * scale / 2)), interpolation=interpolation)
|
| 65 |
except Exception as error:
|
| 66 |
+
print('Erro de escala.', error)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
|
| 69 |
return output
|
| 70 |
except Exception as error:
|
| 71 |
+
print('Exceção global', error)
|
| 72 |
return None, None
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
| 75 |
demo = gr.Interface(
|
| 76 |
upscaler, [
|
| 77 |
+
gr.Image(type="filepath", label="Entrada"),
|
| 78 |
+
gr.Radio(['GFPGANv1.2', 'GFPGANv1.3', 'GFPGANv1.4', 'RestoreFormer'], type="value", label='Versão'),
|
| 79 |
+
gr.Number(label="Fator de Redimensionamento"),
|
| 80 |
], [
|
| 81 |
+
gr.Image(type="numpy", label="Saída"),
|
| 82 |
],
|
|
|
|
| 83 |
allow_flagging="never"
|
| 84 |
)
|
| 85 |
|
| 86 |
demo.queue()
|
| 87 |
+
demo.launch()
|