Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,7 @@ import cv2
|
|
| 13 |
import gradio as gr
|
| 14 |
|
| 15 |
|
| 16 |
-
#
|
| 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'):
|
|
@@ -32,13 +32,11 @@ 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 |
-
#
|
| 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,12 +46,10 @@ 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,
|
|
@@ -62,53 +58,41 @@ def upscaler(img, version, scale):
|
|
| 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 |
-
|
|
|
|
| 74 |
interpolation = cv2.INTER_AREA if scale < 2 else cv2.INTER_LANCZOS4
|
| 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('
|
| 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()
|
|
|
|
| 13 |
import gradio as gr
|
| 14 |
|
| 15 |
|
| 16 |
+
# ํ์ํ ๋ชจ๋ธ ๋ค์ด๋ก๋
|
| 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'):
|
|
|
|
| 32 |
upsampler = RealESRGANer(scale=4, model_path=model_path, model=model, tile=0, tile_pad=10, pre_pad=0, half=half)
|
| 33 |
|
| 34 |
|
| 35 |
+
# ์ด๋ฏธ์ง ์ ์ฅ ๋๋ ํ ๋ฆฌ์ ์ ์ฅํ๋ ๋ถ๋ถ (ํ์ฌ ์ฃผ์ ์ฒ๋ฆฌ๋จ)
|
| 36 |
# os.makedirs('output', exist_ok=True)
|
| 37 |
|
| 38 |
def upscaler(img, version, scale):
|
|
|
|
| 39 |
try:
|
|
|
|
| 40 |
img = cv2.imread(img, cv2.IMREAD_UNCHANGED)
|
| 41 |
if len(img.shape) == 3 and img.shape[2] == 4:
|
| 42 |
img_mode = 'RGBA'
|
|
|
|
| 46 |
else:
|
| 47 |
img_mode = None
|
| 48 |
|
|
|
|
| 49 |
h, w = img.shape[0:2]
|
| 50 |
if h < 300:
|
| 51 |
img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
|
| 52 |
|
|
|
|
| 53 |
face_enhancer = GFPGANer(
|
| 54 |
model_path=f'{version}.pth',
|
| 55 |
upscale=2,
|
|
|
|
| 58 |
bg_upsampler=upsampler
|
| 59 |
)
|
| 60 |
|
|
|
|
| 61 |
try:
|
| 62 |
_, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
|
| 63 |
except RuntimeError as error:
|
| 64 |
+
print('์ค๋ฅ:', error)
|
|
|
|
| 65 |
|
| 66 |
try:
|
| 67 |
+
# ๋ฐฐ์จ ์กฐ์ ๊ณ์๊ฐ 2 ๋๋ 0์ธ ๊ฒฝ์ฐ ์ถ๊ฐ ์ค์ผ์ผ๋ง์ ์ํํ์ง ์์
|
| 68 |
+
if scale != 2 and scale != 0:
|
| 69 |
interpolation = cv2.INTER_AREA if scale < 2 else cv2.INTER_LANCZOS4
|
| 70 |
h, w = img.shape[0:2]
|
| 71 |
output = cv2.resize(output, (int(w * scale / 2), int(h * scale / 2)), interpolation=interpolation)
|
| 72 |
except Exception as error:
|
| 73 |
+
print('์๋ชป๋ ๋ฐฐ์จ ์
๋ ฅ:', error)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
|
| 76 |
return output
|
| 77 |
except Exception as error:
|
| 78 |
+
print('์ ์ญ ์์ธ:', error)
|
| 79 |
return None, None
|
| 80 |
|
| 81 |
if __name__ == "__main__":
|
| 82 |
+
title = "์ด๋ฏธ์ง ์
์ค์ผ์ผ๋ฌ ๋ฐ ๋ณต์ [GFPGAN ์๊ณ ๋ฆฌ์ฆ]"
|
|
|
|
| 83 |
|
| 84 |
demo = gr.Interface(
|
| 85 |
upscaler, [
|
| 86 |
+
gr.Image(type="filepath", label="์
๋ ฅ ์ด๋ฏธ์ง"),
|
| 87 |
+
gr.Radio(['GFPGANv1.2', 'GFPGANv1.3', 'GFPGANv1.4', 'RestoreFormer'], type="value", label="๋ฒ์ ", value="GFPGANv1.4", visible=False),
|
| 88 |
+
gr.Number(label="๋ฐฐ์จ ์กฐ์ ๊ณ์", value=0, visible=False),
|
| 89 |
], [
|
| 90 |
+
gr.Image(type="numpy", label="์ถ๋ ฅ ์ด๋ฏธ์ง"),
|
| 91 |
],
|
| 92 |
title=title,
|
| 93 |
+
allow_flagging="never",
|
| 94 |
+
examples=[["์์ .png", "GFPGANv1.4", 0]]
|
| 95 |
)
|
| 96 |
|
| 97 |
demo.queue()
|
| 98 |
+
demo.launch()
|