Update app.py
Browse files
app.py
CHANGED
@@ -1,103 +1,3 @@
|
|
1 |
-
import os
|
2 |
-
import sys
|
3 |
-
from torchvision.transforms import functional
|
4 |
-
sys.modules["torchvision.transforms.functional_tensor"] = functional
|
5 |
-
|
6 |
-
from basicsr.archs.srvgg_arch import SRVGGNetCompact
|
7 |
-
from gfpgan.utils import GFPGANer
|
8 |
-
from realesrgan.utils import RealESRGANer
|
9 |
-
|
10 |
-
import torch
|
11 |
-
import cv2
|
12 |
-
import gradio as gr
|
13 |
-
|
14 |
-
# ํ์ํ ๋ชจ๋ธ ๋ค์ด๋ก๋
|
15 |
-
def download_upscaler_models():
|
16 |
-
if not os.path.exists('realesr-general-x4v3.pth'):
|
17 |
-
os.system("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth -P .")
|
18 |
-
if not os.path.exists('GFPGANv1.4.pth'):
|
19 |
-
os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth -P .")
|
20 |
-
|
21 |
-
# ์
์ค์ผ์ผ๋ฌ ๋ชจ๋ธ ์ด๊ธฐํ
|
22 |
-
def init_upscaler_model():
|
23 |
-
model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
|
24 |
-
model_path = 'realesr-general-x4v3.pth'
|
25 |
-
half = True if torch.cuda.is_available() else False
|
26 |
-
upsampler = RealESRGANer(scale=4, model_path=model_path, model=model, tile=0, tile_pad=10, pre_pad=0, half=half)
|
27 |
-
|
28 |
-
return upsampler
|
29 |
-
|
30 |
-
# ์ด๋ฏธ์ง ์
์ค์ผ์ผ๋ง ํจ์
|
31 |
-
def upscale_image(img):
|
32 |
-
try:
|
33 |
-
upsampler = init_upscaler_model()
|
34 |
-
|
35 |
-
# ์ด๋ฏธ์ง ํ์ผ ์ฝ๊ธฐ
|
36 |
-
img = cv2.imread(img, cv2.IMREAD_UNCHANGED)
|
37 |
-
if len(img.shape) == 3 and img.shape[2] == 4:
|
38 |
-
img_mode = 'RGBA'
|
39 |
-
elif len(img.shape) == 2:
|
40 |
-
img_mode = None
|
41 |
-
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
42 |
-
else:
|
43 |
-
img_mode = None
|
44 |
-
|
45 |
-
# ์์ ์ด๋ฏธ์ง ํฌ๊ธฐ ์กฐ์
|
46 |
-
h, w = img.shape[0:2]
|
47 |
-
if h < 300:
|
48 |
-
img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
|
49 |
-
|
50 |
-
# ๊ณ ์ ๋ GFPGANv1.4 ๋ชจ๋ธ ์ฌ์ฉ
|
51 |
-
face_enhancer = GFPGANer(
|
52 |
-
model_path='GFPGANv1.4.pth',
|
53 |
-
upscale=2,
|
54 |
-
arch='clean',
|
55 |
-
channel_multiplier=2,
|
56 |
-
bg_upsampler=upsampler
|
57 |
-
)
|
58 |
-
|
59 |
-
# ์ด๋ฏธ์ง ํฅ์
|
60 |
-
try:
|
61 |
-
_, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
|
62 |
-
except RuntimeError as error:
|
63 |
-
print('์ค๋ฅ', error)
|
64 |
-
return None
|
65 |
-
|
66 |
-
# RGB๋ก ๋ณํํ์ฌ ๋ฐํ
|
67 |
-
output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
|
68 |
-
return output
|
69 |
-
except Exception as error:
|
70 |
-
print('๊ธ๋ก๋ฒ ์์ธ', error)
|
71 |
-
return None
|
72 |
-
|
73 |
-
# ์
์ค์ผ์ผ๋ฌ ํญ UI ์์ฑ ํจ์
|
74 |
-
def create_upscaler_tab():
|
75 |
-
# ํ์ํ ๋ชจ๋ธ ๋ค์ด๋ก๋
|
76 |
-
download_upscaler_models()
|
77 |
-
|
78 |
-
with gr.Tab("์ด๋ฏธ์ง ์
์ค์ผ์ผ๋ฌ"):
|
79 |
-
gr.Markdown(
|
80 |
-
"""
|
81 |
-
# ์ด๋ฏธ์ง ์
์ค์ผ์ผ๋ฌ
|
82 |
-
|
83 |
-
์
๋ก๋ํ ์ด๋ฏธ์ง๋ฅผ ๊ณ ํด์๋๋ก ๋ณํํฉ๋๋ค. ํนํ ์ธ๋ฌผ ์ฌ์ง์ ์ผ๊ตด ๋ํ
์ผ์ ํฅ์์ํต๋๋ค.
|
84 |
-
"""
|
85 |
-
)
|
86 |
-
|
87 |
-
with gr.Row():
|
88 |
-
with gr.Column():
|
89 |
-
input_image = gr.Image(type="filepath", label="์
๋ ฅ ์ด๋ฏธ์ง")
|
90 |
-
upscale_btn = gr.Button("์ด๋ฏธ์ง ์
์ค์ผ์ผ", variant="primary")
|
91 |
-
|
92 |
-
with gr.Column():
|
93 |
-
output_image = gr.Image(type="numpy", label="๊ฒฐ๊ณผ ์ด๋ฏธ์ง")
|
94 |
-
|
95 |
-
upscale_btn.click(
|
96 |
-
fn=upscale_image,
|
97 |
-
inputs=[input_image],
|
98 |
-
outputs=[output_image]
|
99 |
-
)
|
100 |
-
|
101 |
import os
|
102 |
import tempfile
|
103 |
from PIL import Image
|
@@ -130,6 +30,13 @@ load_dotenv()
|
|
130 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
131 |
logger = logging.getLogger(__name__)
|
132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
def save_binary_file(file_name, data):
|
134 |
with open(file_name, "wb") as f:
|
135 |
f.write(data)
|
@@ -411,13 +318,7 @@ def generate_multiple_images(image1, image2, image3, prompt, progress=gr.Progres
|
|
411 |
|
412 |
return results[0], results[1], results[2], results[3], combined_status, combined_prompts
|
413 |
|
414 |
-
# ์ด๋ฏธ์ง
|
415 |
-
def download_upscaler_models():
|
416 |
-
if not os.path.exists('realesr-general-x4v3.pth'):
|
417 |
-
os.system("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth -P .")
|
418 |
-
if not os.path.exists('GFPGANv1.4.pth'):
|
419 |
-
os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth -P .")
|
420 |
-
|
421 |
def upscale_image(img):
|
422 |
try:
|
423 |
# ๋ชจ๋ธ ์ด๊ธฐํ
|
@@ -464,6 +365,9 @@ def upscale_image(img):
|
|
464 |
print('๊ธ๋ก๋ฒ ์์ธ', error)
|
465 |
return None
|
466 |
|
|
|
|
|
|
|
467 |
# Gradio ์ธํฐํ์ด์ค
|
468 |
with gr.Blocks() as demo:
|
469 |
gr.HTML(
|
@@ -475,7 +379,7 @@ with gr.Blocks() as demo:
|
|
475 |
"""
|
476 |
)
|
477 |
|
478 |
-
with gr.Tabs():
|
479 |
# ์ฒซ ๋ฒ์งธ ํญ: ์ด๋ฏธ์ง ์์ฑ๊ธฐ
|
480 |
with gr.Tab("์ด๋ฏธ์ง ์์ฑ๊ธฐ"):
|
481 |
with gr.Row():
|
@@ -532,8 +436,7 @@ with gr.Blocks() as demo:
|
|
532 |
|
533 |
gr.Examples(
|
534 |
examples=examples,
|
535 |
-
inputs=[image1_input, image2_input, image3_input, prompt_input]
|
536 |
-
elem_id="examples-grid"
|
537 |
)
|
538 |
|
539 |
# ์ค๋ฅธ์ชฝ ์ถ๋ ฅ ์์ญ
|
@@ -571,7 +474,7 @@ with gr.Blocks() as demo:
|
|
571 |
outputs=prompt_input
|
572 |
)
|
573 |
clothes_change_btn1.click(
|
574 |
-
fn=lambda: "(#1์
|
575 |
inputs=[],
|
576 |
outputs=prompt_input
|
577 |
)
|
@@ -604,9 +507,6 @@ with gr.Blocks() as demo:
|
|
604 |
|
605 |
# ๋ ๋ฒ์งธ ํญ: ์ด๋ฏธ์ง ์
์ค์ผ์ผ๋ฌ
|
606 |
with gr.Tab("์ด๋ฏธ์ง ์
์ค์ผ์ผ๋ฌ"):
|
607 |
-
# ํ์ํ ๋ชจ๋ธ ๋ค์ด๋ก๋ ํธ์ถ
|
608 |
-
download_upscaler_models()
|
609 |
-
|
610 |
gr.Markdown(
|
611 |
"""
|
612 |
# ์ด๋ฏธ์ง ์
์ค์ผ์ผ๋ฌ
|
@@ -630,4 +530,4 @@ with gr.Blocks() as demo:
|
|
630 |
)
|
631 |
|
632 |
if __name__ == "__main__":
|
633 |
-
demo.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import tempfile
|
3 |
from PIL import Image
|
|
|
30 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
31 |
logger = logging.getLogger(__name__)
|
32 |
|
33 |
+
# ํ์ํ ๋ชจ๋ธ ๋ค์ด๋ก๋
|
34 |
+
def download_upscaler_models():
|
35 |
+
if not os.path.exists('realesr-general-x4v3.pth'):
|
36 |
+
os.system("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth -P .")
|
37 |
+
if not os.path.exists('GFPGANv1.4.pth'):
|
38 |
+
os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth -P .")
|
39 |
+
|
40 |
def save_binary_file(file_name, data):
|
41 |
with open(file_name, "wb") as f:
|
42 |
f.write(data)
|
|
|
318 |
|
319 |
return results[0], results[1], results[2], results[3], combined_status, combined_prompts
|
320 |
|
321 |
+
# ์ด๋ฏธ์ง ์
์ค์ผ์ผ ํจ์
|
|
|
|
|
|
|
|
|
|
|
|
|
322 |
def upscale_image(img):
|
323 |
try:
|
324 |
# ๋ชจ๋ธ ์ด๊ธฐํ
|
|
|
365 |
print('๊ธ๋ก๋ฒ ์์ธ', error)
|
366 |
return None
|
367 |
|
368 |
+
# ์์ ์ ์
์ค์ผ์ผ๋ฌ ๋ชจ๋ธ ๋ค์ด๋ก๋
|
369 |
+
download_upscaler_models()
|
370 |
+
|
371 |
# Gradio ์ธํฐํ์ด์ค
|
372 |
with gr.Blocks() as demo:
|
373 |
gr.HTML(
|
|
|
379 |
"""
|
380 |
)
|
381 |
|
382 |
+
with gr.Tabs() as tabs:
|
383 |
# ์ฒซ ๋ฒ์งธ ํญ: ์ด๋ฏธ์ง ์์ฑ๊ธฐ
|
384 |
with gr.Tab("์ด๋ฏธ์ง ์์ฑ๊ธฐ"):
|
385 |
with gr.Row():
|
|
|
436 |
|
437 |
gr.Examples(
|
438 |
examples=examples,
|
439 |
+
inputs=[image1_input, image2_input, image3_input, prompt_input]
|
|
|
440 |
)
|
441 |
|
442 |
# ์ค๋ฅธ์ชฝ ์ถ๋ ฅ ์์ญ
|
|
|
474 |
outputs=prompt_input
|
475 |
)
|
476 |
clothes_change_btn1.click(
|
477 |
+
fn=lambda: "(#1์ ์ฌ์ฑ๋ชจ๋ธ)์ด ์ ์ฒด ๋น์จ๊ณผ ํฌ์ฆ๋ ์ ์นํ ์ฒด (#2์ ์ ๊ธ๋ผ์ค)์ (#3์ ์ฒญ๋ฐ์ง)๋ฅผ ์ง์ ๋ชจ๋ธ์ด ์ฐฉ์ฉํ๊ฒ ์ฒ๋ผ ์์ฐ์ค๋ฝ๊ฒ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ๋ผ.",
|
478 |
inputs=[],
|
479 |
outputs=prompt_input
|
480 |
)
|
|
|
507 |
|
508 |
# ๋ ๋ฒ์งธ ํญ: ์ด๋ฏธ์ง ์
์ค์ผ์ผ๋ฌ
|
509 |
with gr.Tab("์ด๋ฏธ์ง ์
์ค์ผ์ผ๋ฌ"):
|
|
|
|
|
|
|
510 |
gr.Markdown(
|
511 |
"""
|
512 |
# ์ด๋ฏธ์ง ์
์ค์ผ์ผ๋ฌ
|
|
|
530 |
)
|
531 |
|
532 |
if __name__ == "__main__":
|
533 |
+
demo.launch(share=True)์ ๊ธ๋ผ์ค)์ (#3์ ์ฒญ๋ฐ์ง)๋ฅผ ์ง์ ๋ชจ๋ธ์ด ์ฐฉ์ฉํ๊ฒ ์ฒ๋ผ ์์ฐ์ค๋ฝ๊ฒ ์ด๋ฏธ
|