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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -21
app.py CHANGED
@@ -11,7 +11,7 @@ import torch
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 .")
17
  if not os.path.exists('GFPGANv1.2.pth'):
@@ -23,15 +23,16 @@ if not os.path.exists('GFPGANv1.4.pth'):
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,7 +46,6 @@ def upscaler(img, version, scale):
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,
@@ -60,13 +60,12 @@ def upscaler(img, version, scale):
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
@@ -75,22 +74,20 @@ def upscaler(img, version, scale):
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()
 
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 .")
17
  if not os.path.exists('GFPGANv1.2.pth'):
 
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
  model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
27
  model_path = 'realesr-general-x4v3.pth'
28
  half = True if torch.cuda.is_available() else False
29
  upsampler = RealESRGANer(scale=4, model_path=model_path, model=model, tile=0, tile_pad=10, pre_pad=0, half=half)
30
 
31
+ # ์ด๋ฏธ์ง€ ์ €์žฅ ๋””๋ ‰ํ† ๋ฆฌ ์ƒ์„ฑ (ํ•„์š”์‹œ ์ฃผ์„ ํ•ด์ œ)
32
+ # os.makedirs('output', exist_ok=True)
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'
 
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',
51
  upscale=2,
 
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
 
74
  return None, None
75
 
76
  if __name__ == "__main__":
77
+ title = "์ด๋ฏธ์ง€ ์—…์Šค์ผ€์ผ ๋ฐ ๋ณต์› [GFPGAN ์•Œ๊ณ ๋ฆฌ์ฆ˜]"
78
 
79
  demo = gr.Interface(
80
+ upscaler, [
81
+ gr.Image(type="filepath", label="์ž…๋ ฅ"),
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]],
89
+ allow_flagging="never"
90
+ )
 
 
91
 
92
  demo.queue()
93
  demo.launch()