Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# mostly borrowed from TheStinger/Ilaria_Upscaler spaces.
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
import cv2
|
5 |
+
import numpy
|
6 |
+
import os
|
7 |
+
import random
|
8 |
+
from basicsr.archs.rrdbnet_arch import RRDBNet
|
9 |
+
from basicsr.utils.download_util import load_file_from_url
|
10 |
+
|
11 |
+
|
12 |
+
from realesrgan import RealESRGANer
|
13 |
+
from realesrgan.archs.srvgg_arch import SRVGGNetCompact
|
14 |
+
|
15 |
+
|
16 |
+
def model_params(model_name):
|
17 |
+
if model_name == 'RealESRGAN_x4plus': # x4 RRDBNet model
|
18 |
+
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
|
19 |
+
netscale = 4
|
20 |
+
file_url = ['https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth']
|
21 |
+
elif model_name == 'RealESRNet_x4plus': # x4 RRDBNet model
|
22 |
+
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
|
23 |
+
netscale = 4
|
24 |
+
file_url = ['https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.1/RealESRNet_x4plus.pth']
|
25 |
+
elif model_name == 'RealESRGAN_x4plus_anime_6B': # x4 RRDBNet model with 6 blocks
|
26 |
+
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=6, num_grow_ch=32, scale=4)
|
27 |
+
netscale = 4
|
28 |
+
file_url = ['https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth']
|
29 |
+
elif model_name == 'RealESRGAN_x2plus': # x2 RRDBNet model
|
30 |
+
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2)
|
31 |
+
netscale = 2
|
32 |
+
file_url = ['https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth']
|
33 |
+
elif model_name == 'realesr-general-x4v3': # x4 VGG-style model (S size)
|
34 |
+
model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
|
35 |
+
netscale = 4
|
36 |
+
file_url = [
|
37 |
+
'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-wdn-x4v3.pth',
|
38 |
+
'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth'
|
39 |
+
]
|
40 |
+
|
41 |
+
model, netscale, file_url
|
42 |
+
|
43 |
+
|
44 |
+
def upscale(image, model_name, tile, denoise, face_enhance, scale):
|
45 |
+
if not image: return
|
46 |
+
|
47 |
+
model, netscale, file_url = model_params(model_name)
|
48 |
+
|
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 |
+
dni_weight = None
|
58 |
+
if model_name == 'realesr-general-x4v3' and denoise_strength != 1:
|
59 |
+
wdn_model_path = model_path.replace('realesr-general-x4v3', 'realesr-general-wdn-x4v3')
|
60 |
+
model_path = [model_path, wdn_model_path]
|
61 |
+
dni_weight = [denoise_strength, 1 - denoise_strength]
|
62 |
+
|
63 |
+
upsampler = RealESRGANer(
|
64 |
+
scale=netscale,
|
65 |
+
model_path=model_path,
|
66 |
+
dni_weight=dni_weight,
|
67 |
+
model=model,
|
68 |
+
tile=tile,
|
69 |
+
tile_pad=10,
|
70 |
+
pre_pad=10,
|
71 |
+
half=False,
|
72 |
+
gpu_id=None
|
73 |
+
)
|
74 |
+
|
75 |
+
if face_enhance:
|
76 |
+
from gfpgan import GFPGANer
|
77 |
+
face_enhancer = GFPGANer(
|
78 |
+
model_path='https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth',
|
79 |
+
upscale=outscale,
|
80 |
+
arch='clean',
|
81 |
+
channel_multiplier=2,
|
82 |
+
bg_upsampler=upsampler)
|
83 |
+
|
84 |
+
################
|
85 |
+
|
86 |
+
cv_img = numpy.array(image)
|
87 |
+
img = cv2.cvtColor(cv_img, cv2.COLOR_RGBA2BGRA)
|
88 |
+
|
89 |
+
try:
|
90 |
+
if face_enhance:
|
91 |
+
_, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
|
92 |
+
else:
|
93 |
+
output, _ = upsampler.enhance(img, outscale=outscale)
|
94 |
+
except RuntimeError as error:
|
95 |
+
print('Error', error)
|
96 |
+
print('If you encounter CUDA out of memory, try to set --tile with a smaller number.')
|
97 |
+
|
98 |
+
return output
|
99 |
+
|
100 |
+
|
101 |
+
app = gr.Interface(
|
102 |
+
title='Real-ESRGAN Upscaler',
|
103 |
+
description='Yet another Real-ESRGAN upscaler that uses gradio `Interface`, because why not? It’s not like there are any other options for simplicity and backward compatibility. Oh wait, there are. Never mind.',
|
104 |
+
fn=upscale,
|
105 |
+
inputs=[
|
106 |
+
gr.Image(label='Source Image', type='pil', image_mode='RGBA'),
|
107 |
+
gr.Dropdown(
|
108 |
+
label='Model',
|
109 |
+
choices=["RealESRGAN_x4plus", "RealESRNet_x4plus", "RealESRGAN_x4plus_anime_6B","RealESRGAN_x2plus", "realesr-general-x4v3"],
|
110 |
+
show_label=True,
|
111 |
+
value='RealESRGAN_x4plus_anime_6B'
|
112 |
+
),
|
113 |
+
gr.Slider(
|
114 |
+
label='Tile',
|
115 |
+
minimum=0,
|
116 |
+
maximum=1024,
|
117 |
+
step=32,
|
118 |
+
value=256
|
119 |
+
),
|
120 |
+
gr.Slider(
|
121 |
+
label='Denoise Strength',
|
122 |
+
minimum=0,
|
123 |
+
maximum=1,
|
124 |
+
step=0.1,
|
125 |
+
value=0.5
|
126 |
+
),
|
127 |
+
gr.Checkbox(
|
128 |
+
label='Face Enhancement (GFPGAN)',
|
129 |
+
value=False,
|
130 |
+
show_label=True
|
131 |
+
),
|
132 |
+
gr.Slider(
|
133 |
+
minimum=1,
|
134 |
+
maximum=4,
|
135 |
+
step=1,
|
136 |
+
value=2,
|
137 |
+
show_label=True
|
138 |
+
)
|
139 |
+
],
|
140 |
+
outputs=[
|
141 |
+
gr.Image(label='Upscaled Image', image_mode='RGBA')
|
142 |
+
]
|
143 |
+
)
|
144 |
+
|
145 |
+
app.launch(show_api=True)
|