Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,8 @@ from realesrgan.utils import RealESRGANer
|
|
| 10 |
import torch
|
| 11 |
import cv2
|
| 12 |
import gradio as gr
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# ํ์ ๋ชจ๋ธ ๋ค์ด๋ก๋
|
| 15 |
if not os.path.exists('realesr-general-x4v3.pth'):
|
|
@@ -33,18 +35,29 @@ upsampler = RealESRGANer(scale=4, model_path=model_path, model=model, tile=0, ti
|
|
| 33 |
|
| 34 |
def upscaler(img, version, scale):
|
| 35 |
try:
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
img_mode = 'RGBA'
|
| 39 |
-
elif len(
|
| 40 |
img_mode = None
|
| 41 |
-
|
| 42 |
else:
|
| 43 |
img_mode = None
|
| 44 |
|
| 45 |
-
h, w =
|
| 46 |
if h < 300:
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
face_enhancer = GFPGANer(
|
| 50 |
model_path=f'{version}.pth',
|
|
@@ -55,20 +68,26 @@ def upscaler(img, version, scale):
|
|
| 55 |
)
|
| 56 |
|
| 57 |
try:
|
| 58 |
-
_, _, output = face_enhancer.enhance(
|
| 59 |
except RuntimeError as error:
|
| 60 |
print('์ค๋ฅ', error)
|
|
|
|
| 61 |
|
| 62 |
try:
|
| 63 |
if scale != 2:
|
| 64 |
interpolation = cv2.INTER_AREA if scale < 2 else cv2.INTER_LANCZOS4
|
| 65 |
-
h, w =
|
| 66 |
output = cv2.resize(output, (int(w * scale / 2), int(h * scale / 2)), interpolation=interpolation)
|
| 67 |
except Exception as error:
|
| 68 |
print('์๋ชป๋ ์ฌ์ค์ผ์ผ๋ง ์
๋ ฅ.', error)
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
except Exception as error:
|
| 73 |
print('์ ์ญ ์์ธ', error)
|
| 74 |
return None, None
|
|
@@ -82,7 +101,7 @@ if __name__ == "__main__":
|
|
| 82 |
gr.Radio(['GFPGANv1.2', 'GFPGANv1.3', 'GFPGANv1.4', 'RestoreFormer'], type="value", label="๋ฒ์ ", value="GFPGANv1.4", visible=False),
|
| 83 |
gr.Number(label="์ฌ์ค์ผ์ผ๋ง ๊ณ์", value=0, visible=False),
|
| 84 |
], [
|
| 85 |
-
|
| 86 |
],
|
| 87 |
title=title,
|
| 88 |
examples=[["์์ .png", "GFPGANv1.4", 0]],
|
|
|
|
| 10 |
import torch
|
| 11 |
import cv2
|
| 12 |
import gradio as gr
|
| 13 |
+
from gradio_imageslider import ImageSlider # ์ฌ๋ผ์ด๋ ์ปดํฌ๋ํธ ์ถ๊ฐ
|
| 14 |
+
from PIL import Image # PIL์ ์ฌ์ฉํ์ฌ numpy ์ด๋ฏธ์ง๋ฅผ PIL ์ด๋ฏธ์ง๋ก ๋ณํ
|
| 15 |
|
| 16 |
# ํ์ ๋ชจ๋ธ ๋ค์ด๋ก๋
|
| 17 |
if not os.path.exists('realesr-general-x4v3.pth'):
|
|
|
|
| 35 |
|
| 36 |
def upscaler(img, version, scale):
|
| 37 |
try:
|
| 38 |
+
# ์
๋ ฅ๋ img๋ ํ์ผ ๊ฒฝ๋ก์
|
| 39 |
+
image_array = cv2.imread(img, cv2.IMREAD_UNCHANGED)
|
| 40 |
+
if image_array is None:
|
| 41 |
+
print("์ด๋ฏธ์ง ๋ก๋ ์คํจ")
|
| 42 |
+
return None, None
|
| 43 |
+
|
| 44 |
+
if len(image_array.shape) == 3 and image_array.shape[2] == 4:
|
| 45 |
img_mode = 'RGBA'
|
| 46 |
+
elif len(image_array.shape) == 2:
|
| 47 |
img_mode = None
|
| 48 |
+
image_array = cv2.cvtColor(image_array, cv2.COLOR_GRAY2BGR)
|
| 49 |
else:
|
| 50 |
img_mode = None
|
| 51 |
|
| 52 |
+
h, w = image_array.shape[0:2]
|
| 53 |
if h < 300:
|
| 54 |
+
image_array = cv2.resize(image_array, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
|
| 55 |
+
|
| 56 |
+
# ๋ณ๊ฒฝ ์ ์๋ณธ ์ด๋ฏธ์ง๋ฅผ RGB (๋๋ RGBA)๋ก ๋ณํ
|
| 57 |
+
if img_mode == 'RGBA':
|
| 58 |
+
original_rgb = cv2.cvtColor(image_array, cv2.COLOR_BGRA2RGBA)
|
| 59 |
+
else:
|
| 60 |
+
original_rgb = cv2.cvtColor(image_array, cv2.COLOR_BGR2RGB)
|
| 61 |
|
| 62 |
face_enhancer = GFPGANer(
|
| 63 |
model_path=f'{version}.pth',
|
|
|
|
| 68 |
)
|
| 69 |
|
| 70 |
try:
|
| 71 |
+
_, _, output = face_enhancer.enhance(image_array, has_aligned=False, only_center_face=False, paste_back=True)
|
| 72 |
except RuntimeError as error:
|
| 73 |
print('์ค๋ฅ', error)
|
| 74 |
+
return None, None
|
| 75 |
|
| 76 |
try:
|
| 77 |
if scale != 2:
|
| 78 |
interpolation = cv2.INTER_AREA if scale < 2 else cv2.INTER_LANCZOS4
|
| 79 |
+
h, w = image_array.shape[0:2]
|
| 80 |
output = cv2.resize(output, (int(w * scale / 2), int(h * scale / 2)), interpolation=interpolation)
|
| 81 |
except Exception as error:
|
| 82 |
print('์๋ชป๋ ์ฌ์ค์ผ์ผ๋ง ์
๋ ฅ.', error)
|
| 83 |
|
| 84 |
+
output_rgb = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
|
| 85 |
+
|
| 86 |
+
# numpy ์ด๋ฏธ์ง๋ฅผ PIL ์ด๋ฏธ์ง๋ก ๋ณํ (์ฌ๋ผ์ด๋์์ type="pil" ์ฌ์ฉ)
|
| 87 |
+
original_pil = Image.fromarray(original_rgb)
|
| 88 |
+
output_pil = Image.fromarray(output_rgb)
|
| 89 |
+
# ๋ณ๊ฒฝ ์ /ํ ์ด๋ฏธ์ง๋ฅผ ํํ๋ก ๋ฐํํ์ฌ ์ฌ๋ผ์ด๋์ ํ์
|
| 90 |
+
return original_pil, output_pil
|
| 91 |
except Exception as error:
|
| 92 |
print('์ ์ญ ์์ธ', error)
|
| 93 |
return None, None
|
|
|
|
| 101 |
gr.Radio(['GFPGANv1.2', 'GFPGANv1.3', 'GFPGANv1.4', 'RestoreFormer'], type="value", label="๋ฒ์ ", value="GFPGANv1.4", visible=False),
|
| 102 |
gr.Number(label="์ฌ์ค์ผ์ผ๋ง ๊ณ์", value=0, visible=False),
|
| 103 |
], [
|
| 104 |
+
ImageSlider(label="์ถ๋ ฅ", type="pil")
|
| 105 |
],
|
| 106 |
title=title,
|
| 107 |
examples=[["์์ .png", "GFPGANv1.4", 0]],
|