Kims12 commited on
Commit
ebf18af
ยท
verified ยท
1 Parent(s): 1b1ee27

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -11
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
- 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
- h, w = img.shape[0:2]
46
  if h < 300:
47
- img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
 
 
 
 
 
 
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(img, has_aligned=False, only_center_face=False, paste_back=True)
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 = img.shape[0:2]
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
- output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
71
- return output
 
 
 
 
 
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
- gr.Image(type="numpy", label="์ถœ๋ ฅ"),
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]],