Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
import os
|
2 |
-
|
3 |
import sys
|
4 |
from torchvision.transforms import functional
|
5 |
sys.modules["torchvision.transforms.functional_tensor"] = functional
|
@@ -12,7 +11,6 @@ import torch
|
|
12 |
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 .")
|
@@ -25,18 +23,15 @@ if not os.path.exists('GFPGANv1.4.pth'):
|
|
25 |
if not os.path.exists('RestoreFormer.pth'):
|
26 |
os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/RestoreFormer.pth -P .")
|
27 |
|
28 |
-
|
29 |
model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
|
30 |
model_path = 'realesr-general-x4v3.pth'
|
31 |
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 |
try:
|
|
|
40 |
img = cv2.imread(img, cv2.IMREAD_UNCHANGED)
|
41 |
if len(img.shape) == 3 and img.shape[2] == 4:
|
42 |
img_mode = 'RGBA'
|
@@ -50,6 +45,7 @@ def upscaler(img, version, scale):
|
|
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,
|
@@ -61,38 +57,40 @@ def upscaler(img, version, scale):
|
|
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('
|
65 |
|
66 |
try:
|
67 |
-
#
|
68 |
-
if scale
|
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('์๋ชป๋ ๋ฐฐ์จ
|
74 |
|
75 |
output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
|
76 |
return output
|
77 |
except Exception as error:
|
78 |
-
print('์ ์ญ
|
79 |
return None, None
|
80 |
|
81 |
if __name__ == "__main__":
|
82 |
title = "์ด๋ฏธ์ง ์
์ค์ผ์ผ๋ฌ ๋ฐ ๋ณต์ [GFPGAN ์๊ณ ๋ฆฌ์ฆ]"
|
83 |
|
84 |
demo = gr.Interface(
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
|
|
96 |
|
97 |
demo.queue()
|
98 |
demo.launch()
|
|
|
1 |
import os
|
|
|
2 |
import sys
|
3 |
from torchvision.transforms import functional
|
4 |
sys.modules["torchvision.transforms.functional_tensor"] = functional
|
|
|
11 |
import cv2
|
12 |
import gradio as gr
|
13 |
|
|
|
14 |
# ํ์ํ ๋ชจ๋ธ ๋ค์ด๋ก๋
|
15 |
if not os.path.exists('realesr-general-x4v3.pth'):
|
16 |
os.system("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth -P .")
|
|
|
23 |
if not os.path.exists('RestoreFormer.pth'):
|
24 |
os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/RestoreFormer.pth -P .")
|
25 |
|
26 |
+
# Real-ESRGAN ๋ชจ๋ธ ์ด๊ธฐํ
|
27 |
model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
|
28 |
model_path = 'realesr-general-x4v3.pth'
|
29 |
half = True if torch.cuda.is_available() else False
|
30 |
upsampler = RealESRGANer(scale=4, model_path=model_path, model=model, tile=0, tile_pad=10, pre_pad=0, half=half)
|
31 |
|
|
|
|
|
|
|
|
|
32 |
def upscaler(img, version, scale):
|
33 |
try:
|
34 |
+
# ์ด๋ฏธ์ง ๋ถ๋ฌ์ค๊ธฐ
|
35 |
img = cv2.imread(img, cv2.IMREAD_UNCHANGED)
|
36 |
if len(img.shape) == 3 and img.shape[2] == 4:
|
37 |
img_mode = 'RGBA'
|
|
|
45 |
if h < 300:
|
46 |
img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
|
47 |
|
48 |
+
# ์ผ๊ตด ๋ณต์ ๋ชจ๋ธ ์ด๊ธฐํ (์ฝ๋์ ์ ํ๋ ๋ฒ์ : GFPGANv1.4 ๋ฑ)
|
49 |
face_enhancer = GFPGANer(
|
50 |
model_path=f'{version}.pth',
|
51 |
upscale=2,
|
|
|
57 |
try:
|
58 |
_, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
|
59 |
except RuntimeError as error:
|
60 |
+
print('์ค๋ฅ', error)
|
61 |
|
62 |
try:
|
63 |
+
# ๋ฐฐ์จ์ด 0์ด๋ฉด ์ถ๊ฐ ๋ฆฌ์ฌ์ด์ฆํ์ง ์์ (๊ธฐ๋ณธ ์
์ค์ผ์ผ์ GFPGAN์์ ์งํ๋จ)
|
64 |
+
if scale and scale != 2:
|
65 |
interpolation = cv2.INTER_AREA if scale < 2 else cv2.INTER_LANCZOS4
|
66 |
h, w = img.shape[0:2]
|
67 |
output = cv2.resize(output, (int(w * scale / 2), int(h * scale / 2)), interpolation=interpolation)
|
68 |
except Exception as error:
|
69 |
+
print('์๋ชป๋ ๋ฐฐ์จ ์
๋ ฅ์
๋๋ค.', error)
|
70 |
|
71 |
output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
|
72 |
return output
|
73 |
except Exception as error:
|
74 |
+
print('์ ์ญ ์์ธ', error)
|
75 |
return None, None
|
76 |
|
77 |
if __name__ == "__main__":
|
78 |
title = "์ด๋ฏธ์ง ์
์ค์ผ์ผ๋ฌ ๋ฐ ๋ณต์ [GFPGAN ์๊ณ ๋ฆฌ์ฆ]"
|
79 |
|
80 |
demo = gr.Interface(
|
81 |
+
upscaler,
|
82 |
+
[
|
83 |
+
gr.Image(type="filepath", label="์
๋ ฅ"),
|
84 |
+
gr.Radio(['GFPGANv1.2', 'GFPGANv1.3', 'GFPGANv1.4', 'RestoreFormer'], value="GFPGANv1.4", visible=False, label="๋ฒ์ "),
|
85 |
+
gr.Number(value=0, visible=False, label="๋ฐฐ์จ")
|
86 |
+
],
|
87 |
+
[
|
88 |
+
gr.Image(type="numpy", label="์ถ๋ ฅ")
|
89 |
+
],
|
90 |
+
title=title,
|
91 |
+
examples=[["example.png"]],
|
92 |
+
allow_flagging="never"
|
93 |
+
)
|
94 |
|
95 |
demo.queue()
|
96 |
demo.launch()
|