optimize api
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import os
|
|
2 |
import shutil
|
3 |
import uuid
|
4 |
import cv2
|
|
|
5 |
import gradio as gr
|
6 |
import torch
|
7 |
from basicsr.archs.rrdbnet_arch import RRDBNet
|
@@ -16,24 +17,23 @@ if not os.path.exists('model_zoo/gan/GFPGANv1.4.pth'):
|
|
16 |
if not os.path.exists('model_zoo/swinir/003_realSR_BSRGAN_DFO_s64w8_SwinIR-M_x4_GAN.pth'):
|
17 |
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')
|
18 |
|
19 |
-
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
|
20 |
-
model_path = 'model_zoo/real/RealESRGAN_x4plus.pth'
|
21 |
-
netscale = 4
|
22 |
-
tile = 400 if torch.cuda.is_available() else 0
|
23 |
-
dni_weight = None
|
24 |
-
# restorer
|
25 |
-
upsampler = RealESRGANer(
|
26 |
-
scale=netscale,
|
27 |
-
model_path=model_path,
|
28 |
-
dni_weight=dni_weight,
|
29 |
-
model=model,
|
30 |
-
tile=tile,
|
31 |
-
tile_pad=10,
|
32 |
-
pre_pad=0,
|
33 |
-
half=False, #Use fp32 precision during inference. Default: fp16 (half precision).
|
34 |
-
gpu_id=None) #gpu device to use (default=None) can be 0,1,2 for multi-gpu
|
35 |
-
|
36 |
def inference(img, scale):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
# background enhancer with RealESRGAN
|
38 |
os.makedirs('output', exist_ok=True)
|
39 |
if scale > 4:
|
@@ -52,22 +52,16 @@ def inference(img, scale):
|
|
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 |
-
try:
|
56 |
-
face_enhancer = GFPGANer(
|
57 |
-
model_path='model_zoo/gan/GFPGANv1.4.pth', upscale=scale, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
|
58 |
-
_, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
|
59 |
|
|
|
|
|
|
|
60 |
|
61 |
-
|
62 |
-
|
|
|
|
|
63 |
|
64 |
-
try:
|
65 |
-
if scale != 2:
|
66 |
-
interpolation = cv2.INTER_AREA if scale < 2 else cv2.INTER_LANCZOS4
|
67 |
-
h, w = img.shape[0:2]
|
68 |
-
output = cv2.resize(output, (int(w * scale / 2), int(h * scale / 2)), interpolation=interpolation)
|
69 |
-
except Exception as error:
|
70 |
-
print('wrong scale input.', error)
|
71 |
if img_mode == 'RGBA': # RGBA images should be saved in png format
|
72 |
extension = 'png'
|
73 |
else:
|
@@ -82,6 +76,23 @@ def inference(img, scale):
|
|
82 |
except Exception as error:
|
83 |
print('global exception', error)
|
84 |
return None, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
title = "Real Esrgan Restore Ai Face Restoration by appsgenz.com"
|
86 |
description = ""
|
87 |
article = "AppsGenz"
|
@@ -97,4 +108,4 @@ grApp = gr.Interface(
|
|
97 |
description=description,
|
98 |
article=article)
|
99 |
grApp.queue(concurrency_count=2)
|
100 |
-
grApp.launch()
|
|
|
2 |
import shutil
|
3 |
import uuid
|
4 |
import cv2
|
5 |
+
import gc
|
6 |
import gradio as gr
|
7 |
import torch
|
8 |
from basicsr.archs.rrdbnet_arch import RRDBNet
|
|
|
17 |
if not os.path.exists('model_zoo/swinir/003_realSR_BSRGAN_DFO_s64w8_SwinIR-M_x4_GAN.pth'):
|
18 |
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')
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
def inference(img, scale):
|
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 |
# background enhancer with RealESRGAN
|
38 |
os.makedirs('output', exist_ok=True)
|
39 |
if scale > 4:
|
|
|
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 |
+
face_enhancer = GFPGANer(
|
57 |
+
model_path='model_zoo/gan/GFPGANv1.4.pth', upscale=scale, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
|
58 |
+
_, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
|
59 |
|
60 |
+
if scale != 2:
|
61 |
+
interpolation = cv2.INTER_AREA if scale < 2 else cv2.INTER_LANCZOS4
|
62 |
+
h, w = img.shape[0:2]
|
63 |
+
output = cv2.resize(output, (int(w * scale / 2), int(h * scale / 2)), interpolation=interpolation)
|
64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
if img_mode == 'RGBA': # RGBA images should be saved in png format
|
66 |
extension = 'png'
|
67 |
else:
|
|
|
76 |
except Exception as error:
|
77 |
print('global exception', error)
|
78 |
return None, None
|
79 |
+
finally:
|
80 |
+
#clean_folder('output')
|
81 |
+
gc.collect()
|
82 |
+
if torch.cuda.is_available():
|
83 |
+
torch.cuda.empty_cache()
|
84 |
+
|
85 |
+
def clean_folder(folder):
|
86 |
+
for filename in os.listdir(folder):
|
87 |
+
file_path = os.path.join(folder, filename)
|
88 |
+
try:
|
89 |
+
if os.path.isfile(file_path) or os.path.islink(file_path):
|
90 |
+
os.unlink(file_path)
|
91 |
+
elif os.path.isdir(file_path):
|
92 |
+
shutil.rmtree(file_path)
|
93 |
+
except Exception as e:
|
94 |
+
print('Failed to delete %s. Reason: %s' % (file_path, e))
|
95 |
+
|
96 |
title = "Real Esrgan Restore Ai Face Restoration by appsgenz.com"
|
97 |
description = ""
|
98 |
article = "AppsGenz"
|
|
|
108 |
description=description,
|
109 |
article=article)
|
110 |
grApp.queue(concurrency_count=2)
|
111 |
+
grApp.launch(share=True)
|