Xintao commited on
Commit
76cb1c1
1 Parent(s): dc21641
Files changed (1) hide show
  1. app.py +60 -52
app.py CHANGED
@@ -8,8 +8,7 @@ from gfpgan.utils import GFPGANer
8
  from realesrgan.utils import RealESRGANer
9
 
10
  os.system("pip freeze")
11
- os.system(
12
- "wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth -P .")
13
  os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.2.pth -P .")
14
  os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth -P .")
15
 
@@ -28,10 +27,9 @@ torch.hub.download_url_to_file(
28
 
29
  # background enhancer with RealESRGAN
30
  model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
31
- netscale = 4
32
  model_path = 'realesr-general-x4v3.pth'
33
  half = True if torch.cuda.is_available() else False
34
- upsampler = RealESRGANer(scale=netscale, model_path=model_path, model=model, tile=0, tile_pad=10, pre_pad=0, half=half)
35
 
36
  # Use GFPGAN for face enhancement
37
  face_enhancer_v3 = GFPGANer(
@@ -40,70 +38,80 @@ face_enhancer_v2 = GFPGANer(
40
  model_path='GFPGANv1.2.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
41
  os.makedirs('output', exist_ok=True)
42
 
 
43
  def inference(img, version, scale):
44
  print(img, version, scale)
45
- img = cv2.imread(img, cv2.IMREAD_UNCHANGED)
46
- if len(img.shape) == 3 and img.shape[2] == 4:
47
- img_mode = 'RGBA'
48
- else:
49
- img_mode = None
50
-
51
- h, w = img.shape[0:2]
52
- if h < 400:
53
- img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
54
-
55
- if version == 'v1.2':
56
- face_enhancer = face_enhancer_v2
57
- else:
58
- face_enhancer = face_enhancer_v3
59
  try:
60
- _, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
61
- except RuntimeError as error:
62
- print('Error', error)
63
- print('If you encounter CUDA out of memory, try to set --tile with a smaller number.')
64
- else:
65
 
66
- extension = 'png'
67
- try:
68
- if scale != 2:
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('wrong scale input.', error)
74
- if img_mode == 'RGBA': # RGBA images should be saved in png format
75
- extension = 'png'
76
- else:
77
- extension = 'jpg'
78
- save_path = f'output/out.{extension}'
79
- cv2.imwrite(save_path, output)
80
 
81
- output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
82
- return output, save_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
 
85
  title = "GFPGAN: Practical Face Restoration Algorithm"
86
- description = r"""
87
- <a href="https://github.com/TencentARC/GFPGAN" target='_blank'> <img src="https://img.shields.io/github/stars/TencentARC/GFPGAN?style=social" alt="GFPGAN stars" /></a> Gradio demo for <a href='https://github.com/TencentARC/GFPGAN' target='_blank'><b>GFPGAN: Towards Real-World Blind Face Restoration with Generative Facial Prior</b></a>.<br>
88
  It can be used to restore your **old photos** or improve **AI-generated faces**.<br>
89
  To use it, simply upload your image.
 
90
  """
91
- article = r"""<p style='text-align: center'><a href='https://arxiv.org/abs/2101.04061' target='_blank'>GFPGAN: Towards Real-World Blind Face Restoration with Generative Facial Prior</a> | <a href='https://github.com/TencentARC/GFPGAN' target='_blank'>Github Repo</a></p><center><img src='https://visitor-badge.glitch.me/badge?page_id=akhaliq_GFPGAN' alt='visitor badge'></center>
92
- <center><img src='https://visitor-badge.glitch.me/badge?page_id=Gradio_Xintao_GFPGAN' alt='visitor badge'></center>
93
 
94
- [![arXiv](https://img.shields.io/badge/arXiv-Paper-<COLOR>.svg)](https://arxiv.org/abs/2101.04061)
95
- [![GitHub Stars](https://img.shields.io/github/stars/TencentARC/GFPGAN?style=social)](https://github.com/TencentARC/GFPGAN)
96
  [![download](https://img.shields.io/github/downloads/TencentARC/GFPGAN/total.svg)](https://github.com/TencentARC/GFPGAN/releases)
 
 
 
 
97
 
 
 
98
  """
99
  gr.Interface(
100
- inference,
101
- [gr.inputs.Image(type="filepath", label="Input"),
102
- gr.inputs.Radio(['v1.2','v1.3'], type="value", default='v1.3', label='GFPGAN version'),
103
- gr.inputs.Number(label="Rescaling factor", default=2)],
104
- [gr.outputs.Image(type="numpy", label="Output (The whole image)"),
105
- gr.outputs.File(label="Download the output image")],
 
 
106
  title=title,
107
  description=description,
108
  article=article,
109
- examples=[['AI-generate.jpg', 'v1.3', 2], ['lincoln.jpg', 'v1.3',2], ['Blake_Lively.jpg', 'v1.3',2], ['10045.png', 'v1.3',2]]).launch()
 
 
8
  from realesrgan.utils import RealESRGANer
9
 
10
  os.system("pip freeze")
11
+ os.system("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth -P .")
 
12
  os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.2.pth -P .")
13
  os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth -P .")
14
 
 
27
 
28
  # background enhancer with RealESRGAN
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
  # Use GFPGAN for face enhancement
35
  face_enhancer_v3 = GFPGANer(
 
38
  model_path='GFPGANv1.2.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
39
  os.makedirs('output', exist_ok=True)
40
 
41
+
42
  def inference(img, version, scale):
43
  print(img, version, scale)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  try:
45
+ img = cv2.imread(img, cv2.IMREAD_UNCHANGED)
46
+ if len(img.shape) == 3 and img.shape[2] == 4:
47
+ img_mode = 'RGBA'
48
+ else:
49
+ img_mode = None
50
 
51
+ h, w = img.shape[0:2]
52
+ if h < 300:
53
+ img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
 
 
 
 
 
 
 
 
 
 
 
54
 
55
+ if version == 'v1.2':
56
+ face_enhancer = face_enhancer_v2
57
+ else:
58
+ face_enhancer = face_enhancer_v3
59
+ try:
60
+ _, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
61
+ except RuntimeError as error:
62
+ print('Error', error)
63
+ else:
64
+ extension = 'png'
65
+
66
+ try:
67
+ if scale != 2:
68
+ interpolation = cv2.INTER_AREA if scale < 2 else cv2.INTER_LANCZOS4
69
+ h, w = img.shape[0:2]
70
+ output = cv2.resize(output, (int(w * scale / 2), int(h * scale / 2)), interpolation=interpolation)
71
+ except Exception as error:
72
+ print('wrong scale input.', error)
73
+ if img_mode == 'RGBA': # RGBA images should be saved in png format
74
+ extension = 'png'
75
+ else:
76
+ extension = 'jpg'
77
+ save_path = f'output/out.{extension}'
78
+ cv2.imwrite(save_path, output)
79
+
80
+ output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
81
+ return output, save_path
82
+ except Exception as error:
83
+ print('global exception', error)
84
+ return None, None
85
 
86
 
87
  title = "GFPGAN: Practical Face Restoration Algorithm"
88
+ description = r"""Gradio demo for <a href='https://github.com/TencentARC/GFPGAN' target='_blank'><b>GFPGAN: Towards Real-World Blind Face Restoration with Generative Facial Prior</b></a>.<br>
 
89
  It can be used to restore your **old photos** or improve **AI-generated faces**.<br>
90
  To use it, simply upload your image.
91
+ More details are in the <a href='https://github.com/TencentARC/GFPGAN' target='_blank'><b>Github Repo</b></a>.
92
  """
93
+ article = r"""
 
94
 
 
 
95
  [![download](https://img.shields.io/github/downloads/TencentARC/GFPGAN/total.svg)](https://github.com/TencentARC/GFPGAN/releases)
96
+ [![GitHub Stars](https://img.shields.io/github/stars/TencentARC/GFPGAN?style=social)](https://github.com/TencentARC/GFPGAN)
97
+ [![arXiv](https://img.shields.io/badge/arXiv-Paper-<COLOR>.svg)](https://arxiv.org/abs/2101.04061)
98
+
99
+ If you have any question, please email `[email protected]` or `[email protected]`.
100
 
101
+ <center><img src='https://visitor-badge.glitch.me/badge?page_id=akhaliq_GFPGAN' alt='visitor badge'></center>
102
+ <center><img src='https://visitor-badge.glitch.me/badge?page_id=Gradio_Xintao_GFPGAN' alt='visitor badge'></center>
103
  """
104
  gr.Interface(
105
+ inference, [
106
+ gr.inputs.Image(type="filepath", label="Input"),
107
+ gr.inputs.Radio(['v1.2', 'v1.3'], type="value", default='v1.3', label='GFPGAN version'),
108
+ gr.inputs.Number(label="Rescaling factor", default=2)
109
+ ], [
110
+ gr.outputs.Image(type="numpy", label="Output (The whole image)"),
111
+ gr.outputs.File(label="Download the output image")
112
+ ],
113
  title=title,
114
  description=description,
115
  article=article,
116
+ examples=[['AI-generate.jpg', 'v1.3', 2], ['lincoln.jpg', 'v1.3', 2], ['Blake_Lively.jpg', 'v1.3', 2],
117
+ ['10045.png', 'v1.3', 2]]).launch()