Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,34 @@ import os
|
|
| 7 |
from io import BytesIO
|
| 8 |
import html
|
| 9 |
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
|
|
@@ -174,25 +202,59 @@ def txt2img(prompt, negative_prompt, model, steps, sampler, cfg_scale, width, he
|
|
| 174 |
return job["imageUrl"]
|
| 175 |
|
| 176 |
|
| 177 |
-
def img2img(
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
css = """
|
| 198 |
footer {visibility: hidden !important;}
|
|
@@ -224,11 +286,24 @@ with gr.Blocks(css=css) as demo:
|
|
| 224 |
|
| 225 |
cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, value=7, step=1)
|
| 226 |
seed = gr.Slider(label="Seed", minimum=-1, maximum=10000000, value=-1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
|
| 228 |
with gr.Column():
|
| 229 |
text_button = gr.Button("Создать", variant='primary', elem_id="generate")
|
| 230 |
with gr.Column(scale=2):
|
| 231 |
-
image_output = gr.Image()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
text_button.click(txt2img, inputs=[prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed], outputs=image_output)
|
|
|
|
| 233 |
|
| 234 |
demo.queue(concurrency_count=64, max_size=80, api_open=False).launch(max_threads=256)
|
|
|
|
| 7 |
from io import BytesIO
|
| 8 |
import html
|
| 9 |
import re
|
| 10 |
+
import cv2
|
| 11 |
+
import torch
|
| 12 |
+
from basicsr.archs.srvgg_arch import SRVGGNetCompact
|
| 13 |
+
from gfpgan.utils import GFPGANer
|
| 14 |
+
from realesrgan.utils import RealESRGANer
|
| 15 |
+
|
| 16 |
+
os.system("pip freeze")
|
| 17 |
+
# download weights
|
| 18 |
+
if not os.path.exists('realesr-general-x4v3.pth'):
|
| 19 |
+
os.system("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth -P .")
|
| 20 |
+
if not os.path.exists('GFPGANv1.2.pth'):
|
| 21 |
+
os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.2.pth -P .")
|
| 22 |
+
if not os.path.exists('GFPGANv1.3.pth'):
|
| 23 |
+
os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth -P .")
|
| 24 |
+
if not os.path.exists('GFPGANv1.4.pth'):
|
| 25 |
+
os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth -P .")
|
| 26 |
+
if not os.path.exists('RestoreFormer.pth'):
|
| 27 |
+
os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/RestoreFormer.pth -P .")
|
| 28 |
+
if not os.path.exists('CodeFormer.pth'):
|
| 29 |
+
os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/CodeFormer.pth -P .")
|
| 30 |
+
|
| 31 |
+
# background enhancer with RealESRGAN
|
| 32 |
+
model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
|
| 33 |
+
model_path = 'realesr-general-x4v3.pth'
|
| 34 |
+
half = True if torch.cuda.is_available() else False
|
| 35 |
+
upsampler = RealESRGANer(scale=4, model_path=model_path, model=model, tile=0, tile_pad=10, pre_pad=0, half=half)
|
| 36 |
+
|
| 37 |
+
os.makedirs('output', exist_ok=True)
|
| 38 |
|
| 39 |
|
| 40 |
|
|
|
|
| 202 |
return job["imageUrl"]
|
| 203 |
|
| 204 |
|
| 205 |
+
def img2img(img, version, scale, weight):
|
| 206 |
+
weight /= 100
|
| 207 |
+
print(img, version, scale, weight)
|
| 208 |
+
try:
|
| 209 |
+
extension = os.path.splitext(os.path.basename(str(img)))[1]
|
| 210 |
+
img = cv2.imread(img, cv2.IMREAD_UNCHANGED)
|
| 211 |
+
if len(img.shape) == 3 and img.shape[2] == 4:
|
| 212 |
+
img_mode = 'RGBA'
|
| 213 |
+
elif len(img.shape) == 2: # for gray inputs
|
| 214 |
+
img_mode = None
|
| 215 |
+
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
| 216 |
+
else:
|
| 217 |
+
img_mode = None
|
| 218 |
+
|
| 219 |
+
if version == 'v1.2':
|
| 220 |
+
face_enhancer = GFPGANer(
|
| 221 |
+
model_path='GFPGANv1.2.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
|
| 222 |
+
elif version == 'v1.3':
|
| 223 |
+
face_enhancer = GFPGANer(
|
| 224 |
+
model_path='GFPGANv1.3.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
|
| 225 |
+
elif version == 'v1.4':
|
| 226 |
+
face_enhancer = GFPGANer(
|
| 227 |
+
model_path='GFPGANv1.4.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
|
| 228 |
+
elif version == 'RestoreFormer':
|
| 229 |
+
face_enhancer = GFPGANer(
|
| 230 |
+
model_path='RestoreFormer.pth', upscale=2, arch='RestoreFormer', channel_multiplier=2, bg_upsampler=upsampler)
|
| 231 |
+
elif version == 'CodeFormer':
|
| 232 |
+
face_enhancer = GFPGANer(
|
| 233 |
+
model_path='CodeFormer.pth', upscale=2, arch='CodeFormer', channel_multiplier=2, bg_upsampler=upsampler)
|
| 234 |
+
|
| 235 |
+
try:
|
| 236 |
+
_, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True, weight=weight)
|
| 237 |
+
except RuntimeError as error:
|
| 238 |
+
print('Error', error)
|
| 239 |
+
|
| 240 |
+
try:
|
| 241 |
+
interpolation = cv2.INTER_AREA if scale < 2 else cv2.INTER_LANCZOS4
|
| 242 |
+
h, w = img.shape[0:2]
|
| 243 |
+
output = cv2.resize(output, (int(w * scale), int(h * scale)), interpolation=interpolation)
|
| 244 |
+
except Exception as error:
|
| 245 |
+
print('wrong scale input.', error)
|
| 246 |
+
if img_mode == 'RGBA': # RGBA images should be saved in png format
|
| 247 |
+
extension = 'png'
|
| 248 |
+
else:
|
| 249 |
+
extension = 'jpg'
|
| 250 |
+
save_path = f'output/out.{extension}'
|
| 251 |
+
cv2.imwrite(save_path, output)
|
| 252 |
|
| 253 |
+
output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
|
| 254 |
+
return output
|
| 255 |
+
except Exception as error:
|
| 256 |
+
print('global exception', error)
|
| 257 |
+
return None
|
| 258 |
|
| 259 |
css = """
|
| 260 |
footer {visibility: hidden !important;}
|
|
|
|
| 286 |
|
| 287 |
cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, value=7, step=1)
|
| 288 |
seed = gr.Slider(label="Seed", minimum=-1, maximum=10000000, value=-1)
|
| 289 |
+
with gr.Tab("Настройки апскейлинга"):
|
| 290 |
+
with gr.Row():
|
| 291 |
+
version = gr.inputs.Radio(['v1.2', 'v1.3', 'v1.4', 'RestoreFormer', 'CodeFormer'], type="value", default='v1.4', label='Версия'),
|
| 292 |
+
scale = gr.inputs.Number(label="Коэффициент масштабирования", default=2),
|
| 293 |
+
weight = gr.Slider(0, 100, label='Weight, только для CodeFormer. 0 для лучшего качества, 100 для лучшей идентичности', default=50)
|
| 294 |
+
|
| 295 |
|
| 296 |
with gr.Column():
|
| 297 |
text_button = gr.Button("Создать", variant='primary', elem_id="generate")
|
| 298 |
with gr.Column(scale=2):
|
| 299 |
+
image_output = gr.Image(show_label=True, label='Сгенерированное изображение:')
|
| 300 |
+
|
| 301 |
+
with gr.Column():
|
| 302 |
+
text_button_up = gr.Button("Улучшить качество", variant='secondary', elem_id="upscb")
|
| 303 |
+
with gr.Column(scale=2):
|
| 304 |
+
image_output_up = gr.Image(show_label=True, label='Увеличенное изображение:')
|
| 305 |
+
|
| 306 |
text_button.click(txt2img, inputs=[prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed], outputs=image_output)
|
| 307 |
+
text_button_up.click(img2img, inputs=[image_output, version, scale, weight], outputs=image_output_up)
|
| 308 |
|
| 309 |
demo.queue(concurrency_count=64, max_size=80, api_open=False).launch(max_threads=256)
|