create app run with Real Esrgan
Browse files- .gitignore +2 -1
- app.py +102 -0
- download-weights.sh +26 -0
- requirement.txt +16 -0
.gitignore
CHANGED
@@ -9,4 +9,5 @@
|
|
9 |
/gfpgan
|
10 |
/output
|
11 |
*/__pycache__/*
|
12 |
-
/utils/__pycache__/*
|
|
|
|
9 |
/gfpgan
|
10 |
/output
|
11 |
*/__pycache__/*
|
12 |
+
/utils/__pycache__/*
|
13 |
+
__pycache__/*
|
app.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
import os
|
3 |
+
import shutil
|
4 |
+
import uuid
|
5 |
+
import cv2
|
6 |
+
import gradio as gr
|
7 |
+
import torch
|
8 |
+
from basicsr.archs.rrdbnet_arch import RRDBNet
|
9 |
+
from gfpgan.utils import GFPGANer
|
10 |
+
from realesrgan.utils import RealESRGANer
|
11 |
+
import uvicorn
|
12 |
+
|
13 |
+
# download weights for RealESRGAN
|
14 |
+
if not os.path.exists('model_zoo/real/RealESRGAN_x4plus.pth'):
|
15 |
+
os.system("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth -P model_zoo/real")
|
16 |
+
if not os.path.exists('model_zoo/gan/GFPGANv1.4.pth'):
|
17 |
+
os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth -P model_zoo/gan")
|
18 |
+
if not os.path.exists('model_zoo/swinir/003_realSR_BSRGAN_DFO_s64w8_SwinIR-M_x4_GAN.pth'):
|
19 |
+
os.system('wget https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/003_realSR_BSRGAN_DFO_s64w8_SwinIR-M_x4_GAN.pth -P model_zoo/swinir')
|
20 |
+
|
21 |
+
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
|
22 |
+
model_path = 'model_zoo/real/RealESRGAN_x4plus.pth'
|
23 |
+
netscale = 4
|
24 |
+
tile = 400 if torch.cuda.is_available() else 0
|
25 |
+
dni_weight = None
|
26 |
+
# restorer
|
27 |
+
upsampler = RealESRGANer(
|
28 |
+
scale=netscale,
|
29 |
+
model_path=model_path,
|
30 |
+
dni_weight=dni_weight,
|
31 |
+
model=model,
|
32 |
+
tile=tile,
|
33 |
+
tile_pad=10,
|
34 |
+
pre_pad=0,
|
35 |
+
half=False, #Use fp32 precision during inference. Default: fp16 (half precision).
|
36 |
+
gpu_id=None) #gpu device to use (default=None) can be 0,1,2 for multi-gpu
|
37 |
+
|
38 |
+
def inference(img, scale):
|
39 |
+
# background enhancer with RealESRGAN
|
40 |
+
os.makedirs('output', exist_ok=True)
|
41 |
+
if scale > 4:
|
42 |
+
scale = 4 # avoid too large scale value
|
43 |
+
try:
|
44 |
+
extension = os.path.splitext(os.path.basename(str(img)))[1]
|
45 |
+
img = cv2.imread(img, cv2.IMREAD_UNCHANGED)
|
46 |
+
if len(img.shape) == 3 and img.shape[2] == 4:
|
47 |
+
img_mode = 'RGBA'
|
48 |
+
elif len(img.shape) == 2: # for gray inputs
|
49 |
+
img_mode = None
|
50 |
+
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
51 |
+
else:
|
52 |
+
img_mode = None
|
53 |
+
|
54 |
+
h, w = img.shape[0:2]
|
55 |
+
if h < 300:
|
56 |
+
img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
|
57 |
+
try:
|
58 |
+
face_enhancer = GFPGANer(
|
59 |
+
model_path='model_zoo/gan/GFPGANv1.4.pth', upscale=scale, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
|
60 |
+
_, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
|
61 |
+
|
62 |
+
|
63 |
+
except RuntimeError as error:
|
64 |
+
print('Error', error)
|
65 |
+
|
66 |
+
try:
|
67 |
+
if scale != 2:
|
68 |
+
interpolation = cv2.INTER_AREA if scale < 2 else cv2.INTER_LANCZOS4
|
69 |
+
h, w = img.shape[0:2]
|
70 |
+
output = cv2.resize(output, (int(w * scale / 2), int(h * scale / 2)), interpolation=interpolation)
|
71 |
+
except Exception as error:
|
72 |
+
print('wrong scale input.', error)
|
73 |
+
if img_mode == 'RGBA': # RGBA images should be saved in png format
|
74 |
+
extension = 'png'
|
75 |
+
else:
|
76 |
+
extension = 'jpg'
|
77 |
+
|
78 |
+
filename = str(uuid.uuid4())
|
79 |
+
save_path = f'output/out_{filename}.{extension}'
|
80 |
+
cv2.imwrite(save_path, output)
|
81 |
+
|
82 |
+
output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
|
83 |
+
return output, save_path
|
84 |
+
except Exception as error:
|
85 |
+
print('global exception', error)
|
86 |
+
return None, None
|
87 |
+
title = "Real Esrgan Restore Ai Face Restoration by appsgenz.com"
|
88 |
+
description = ""
|
89 |
+
article = "AppsGenz"
|
90 |
+
grApp = gr.Interface(
|
91 |
+
inference, [
|
92 |
+
gr.Image(type="filepath", label="Input"),
|
93 |
+
gr.Number(label="Rescaling factor. Note max rescaling factor is 4", value=2),
|
94 |
+
], [
|
95 |
+
gr.Image(type="numpy", label="Output (The whole image)"),
|
96 |
+
gr.File(label="Download the output image")
|
97 |
+
],
|
98 |
+
title=title,
|
99 |
+
description=description,
|
100 |
+
article=article)
|
101 |
+
grApp.queue(concurrency_count=2)
|
102 |
+
grApp.launch()
|
download-weights.sh
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
wget https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/001_classicalSR_DIV2K_s48w8_SwinIR-M_x2.pth -P model_zoo/swinir
|
3 |
+
wget https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/001_classicalSR_DIV2K_s48w8_SwinIR-M_x3.pth -P model_zoo/swinir
|
4 |
+
wget https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/001_classicalSR_DIV2K_s48w8_SwinIR-M_x4.pth -P model_zoo/swinir
|
5 |
+
wget https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/001_classicalSR_DIV2K_s48w8_SwinIR-M_x8.pth -P model_zoo/swinir
|
6 |
+
wget https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/001_classicalSR_DF2K_s64w8_SwinIR-M_x2.pth -P model_zoo/swinir
|
7 |
+
wget https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/001_classicalSR_DF2K_s64w8_SwinIR-M_x3.pth -P model_zoo/swinir
|
8 |
+
wget https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/001_classicalSR_DF2K_s64w8_SwinIR-M_x4.pth -P model_zoo/swinir
|
9 |
+
wget https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/001_classicalSR_DF2K_s64w8_SwinIR-M_x8.pth -P model_zoo/swinir
|
10 |
+
wget https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/002_lightweightSR_DIV2K_s64w8_SwinIR-S_x2.pth -P model_zoo/swinir
|
11 |
+
wget https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/002_lightweightSR_DIV2K_s64w8_SwinIR-S_x3.pth -P model_zoo/swinir
|
12 |
+
wget https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/002_lightweightSR_DIV2K_s64w8_SwinIR-S_x4.pth -P model_zoo/swinir
|
13 |
+
wget https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/002_lightweightSR_DIV2K_s64w8_SwinIR-S_x4.pth -P model_zoo/swinir
|
14 |
+
wget https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/003_realSR_BSRGAN_DFO_s64w8_SwinIR-M_x4_GAN.pth -P model_zoo/swinir
|
15 |
+
wget https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/003_realSR_BSRGAN_DFO_s64w8_SwinIR-M_x2_GAN.pth -P model_zoo/swinir
|
16 |
+
wget https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/003_realSR_BSRGAN_DFOWMFC_s64w8_SwinIR-L_x4_GAN.pth -P model_zoo/swinir
|
17 |
+
wget https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/004_grayDN_DFWB_s128w8_SwinIR-M_noise15.pth -P model_zoo/swinir
|
18 |
+
wget https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/004_grayDN_DFWB_s128w8_SwinIR-M_noise25.pth -P model_zoo/swinir
|
19 |
+
wget https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/004_grayDN_DFWB_s128w8_SwinIR-M_noise50.pth -P model_zoo/swinir
|
20 |
+
wget https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/005_colorDN_DFWB_s128w8_SwinIR-M_noise15.pth -P model_zoo/swinir
|
21 |
+
wget https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/005_colorDN_DFWB_s128w8_SwinIR-M_noise25.pth -P model_zoo/swinir
|
22 |
+
wget https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/005_colorDN_DFWB_s128w8_SwinIR-M_noise50.pth -P model_zoo/swinir
|
23 |
+
wget https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/006_CAR_DFWB_s126w7_SwinIR-M_jpeg10.pth -P model_zoo/swinir
|
24 |
+
wget https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/006_CAR_DFWB_s126w7_SwinIR-M_jpeg20.pth -P model_zoo/swinir
|
25 |
+
wget https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/006_CAR_DFWB_s126w7_SwinIR-M_jpeg30.pth -P model_zoo/swinir
|
26 |
+
wget https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/006_CAR_DFWB_s126w7_SwinIR-M_jpeg40.pth -P model_zoo/swinir
|
requirement.txt
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch>=1.7
|
2 |
+
basicsr>=1.4.2
|
3 |
+
facexlib>=0.2.5
|
4 |
+
gfpgan>=1.3.7
|
5 |
+
realesrgan>=0.2.5
|
6 |
+
numpy
|
7 |
+
opencv-python
|
8 |
+
torchvision
|
9 |
+
scipy
|
10 |
+
tqdm
|
11 |
+
lmdb
|
12 |
+
pyyaml
|
13 |
+
yapf
|
14 |
+
fastapi
|
15 |
+
gradio
|
16 |
+
uuid
|