leonelhs commited on
Commit
d0fad33
β€’
1 Parent(s): 3672be6

revert changes

Browse files
Files changed (2) hide show
  1. app.py +95 -36
  2. examples.py +25 -0
app.py CHANGED
@@ -1,74 +1,133 @@
1
  import os
2
 
 
3
  import gradio as gr
4
  import torch
5
  from basicsr.archs.srvgg_arch import SRVGGNetCompact
6
  from gfpgan.utils import GFPGANer
7
- from huggingface_hub import hf_hub_download
8
  from realesrgan.utils import RealESRGANer
 
9
 
10
  REALESRGAN_REPO_ID = 'leonelhs/realesrgan'
11
  GFPGAN_REPO_ID = 'leonelhs/gfpgan'
12
 
13
  os.system("pip freeze")
14
 
 
 
15
  # background enhancer with RealESRGAN
16
  model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
17
  model_path = hf_hub_download(repo_id=REALESRGAN_REPO_ID, filename='realesr-general-x4v3.pth')
18
  half = True if torch.cuda.is_available() else False
19
  upsampler = RealESRGANer(scale=4, model_path=model_path, model=model, tile=0, tile_pad=10, pre_pad=0, half=half)
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
- def download_model(file):
23
- return hf_hub_download(repo_id=GFPGAN_REPO_ID, filename=file)
 
 
24
 
 
 
25
 
26
- def predict(image, version, scale):
27
- scale = int(scale)
28
- face_enhancer = None
29
 
30
- if version == 'v1.2':
31
- path = download_model('GFPGANv1.2.pth')
32
- face_enhancer = GFPGANer(
33
- model_path=path, upscale=scale, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
34
- elif version == 'v1.3':
35
- path = download_model('GFPGANv1.3.pth')
36
- face_enhancer = GFPGANer(
37
- model_path=path, upscale=scale, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
38
- elif version == 'v1.4':
39
- path = download_model('GFPGANv1.4.pth')
40
- face_enhancer = GFPGANer(
41
- model_path=path, upscale=scale, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
42
- elif version == 'RestoreFormer':
43
- path = download_model('RestoreFormer.pth')
44
- face_enhancer = GFPGANer(
45
- model_path=path, upscale=scale, arch='RestoreFormer', channel_multiplier=2, bg_upsampler=upsampler)
 
46
 
47
- _, _, output = face_enhancer.enhance(image, has_aligned=False, only_center_face=False, paste_back=True)
 
 
 
48
 
49
- return output
 
 
 
 
 
 
 
 
 
 
 
 
50
 
 
 
 
 
 
51
 
52
- title = "GFPGAN"
53
- description = r"""
54
- <b>Practical Face Restoration Algorithm</b>
 
 
 
55
  """
56
  article = r"""
57
- <center><span>[email protected] or [email protected]</span></center>
58
- </br>
59
- <center><a href='https://github.com/TencentARC/GFPGAN' target='_blank'>Github Repo ⭐ </a> are welcome</center>
60
- """
61
 
 
 
 
 
 
 
 
 
 
62
  demo = gr.Interface(
63
  predict, [
64
- gr.Image(type="numpy", label="Input"),
65
  gr.Radio(['v1.2', 'v1.3', 'v1.4', 'RestoreFormer'], type="value", value='v1.4', label='version'),
66
- gr.Dropdown(["1", "2", "3", "4"], value="2", label="Rescaling factor")
67
  ], [
68
- gr.Image(type="numpy", label="Output", interactive=False)
 
69
  ],
70
  title=title,
71
  description=description,
72
- article=article)
73
-
 
 
 
74
  demo.queue().launch()
 
1
  import os
2
 
3
+ import cv2
4
  import gradio as gr
5
  import torch
6
  from basicsr.archs.srvgg_arch import SRVGGNetCompact
7
  from gfpgan.utils import GFPGANer
8
+ from huggingface_hub import snapshot_download, hf_hub_download
9
  from realesrgan.utils import RealESRGANer
10
+ import examples
11
 
12
  REALESRGAN_REPO_ID = 'leonelhs/realesrgan'
13
  GFPGAN_REPO_ID = 'leonelhs/gfpgan'
14
 
15
  os.system("pip freeze")
16
 
17
+ examples.download()
18
+
19
  # background enhancer with RealESRGAN
20
  model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
21
  model_path = hf_hub_download(repo_id=REALESRGAN_REPO_ID, filename='realesr-general-x4v3.pth')
22
  half = True if torch.cuda.is_available() else False
23
  upsampler = RealESRGANer(scale=4, model_path=model_path, model=model, tile=0, tile_pad=10, pre_pad=0, half=half)
24
 
25
+ os.makedirs('output', exist_ok=True)
26
+
27
+
28
+ # def inference(img, version, scale, weight):
29
+ def predict(img, version, scale):
30
+ # weight /= 100
31
+ print(img, version, scale)
32
+ if scale > 4:
33
+ scale = 4 # avoid too large scale value
34
+ try:
35
+ extension = os.path.splitext(os.path.basename(str(img)))[1]
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: # for gray inputs
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 > 3500 or w > 3500:
47
+ print('too large size')
48
+ return None, None
49
 
50
+ if h < 300:
51
+ img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
52
 
53
+ face_enhancer = None
54
+ snapshot_folder = snapshot_download(repo_id=GFPGAN_REPO_ID)
 
55
 
56
+ if version == 'v1.2':
57
+ path = os.path.join(snapshot_folder, 'GFPGANv1.2.pth')
58
+ face_enhancer = GFPGANer(
59
+ model_path=path, upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
60
+ elif version == 'v1.3':
61
+ path = os.path.join(snapshot_folder, 'GFPGANv1.3.pth')
62
+ face_enhancer = GFPGANer(
63
+ model_path=path, upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
64
+ elif version == 'v1.4':
65
+ path = os.path.join(snapshot_folder, 'GFPGANv1.4.pth')
66
+ face_enhancer = GFPGANer(
67
+ model_path=path, upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
68
+ elif version == 'RestoreFormer':
69
+ path = os.path.join(snapshot_folder, 'RestoreFormer.pth')
70
+ face_enhancer = GFPGANer(
71
+ model_path=path, upscale=2, arch='RestoreFormer', channel_multiplier=2,
72
+ bg_upsampler=upsampler)
73
 
74
+ try:
75
+ _, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
76
+ except RuntimeError as error:
77
+ print('Error', error)
78
 
79
+ try:
80
+ if scale != 2:
81
+ interpolation = cv2.INTER_AREA if scale < 2 else cv2.INTER_LANCZOS4
82
+ h, w = img.shape[0:2]
83
+ output = cv2.resize(output, (int(w * scale / 2), int(h * scale / 2)), interpolation=interpolation)
84
+ except Exception as error:
85
+ print('wrong scale input.', error)
86
+ if img_mode == 'RGBA': # RGBA images should be saved in png format
87
+ extension = 'png'
88
+ else:
89
+ extension = 'jpg'
90
+ save_path = f'output/out.{extension}'
91
+ cv2.imwrite(save_path, output)
92
 
93
+ output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
94
+ return output, save_path
95
+ except Exception as error:
96
+ print('global exception', error)
97
+ return None, None
98
 
99
+
100
+ title = "GFPGAN: Practical Face Restoration Algorithm"
101
+ 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>
102
+ It can be used to restore your **old photos** or improve **AI-generated faces**.<br>
103
+ To use it, simply upload your image.<br>
104
+ If GFPGAN is helpful, please help to ⭐ the <a href='https://github.com/TencentARC/GFPGAN' target='_blank'>Github Repo</a> and recommend it to your friends 😊
105
  """
106
  article = r"""
 
 
 
 
107
 
108
+ [![download](https://img.shields.io/github/downloads/TencentARC/GFPGAN/total.svg)](https://github.com/TencentARC/GFPGAN/releases)
109
+ [![GitHub Stars](https://img.shields.io/github/stars/TencentARC/GFPGAN?style=social)](https://github.com/TencentARC/GFPGAN)
110
+ [![arXiv](https://img.shields.io/badge/arXiv-Paper-<COLOR>.svg)](https://arxiv.org/abs/2101.04061)
111
+
112
+ If you have any question, please email πŸ“§ `[email protected]` or `[email protected]`.
113
+
114
+ <center><img src='https://visitor-badge.glitch.me/badge?page_id=akhaliq_GFPGAN' alt='visitor badge'></center>
115
+ <center><img src='https://visitor-badge.glitch.me/badge?page_id=Gradio_Xintao_GFPGAN' alt='visitor badge'></center>
116
+ """
117
  demo = gr.Interface(
118
  predict, [
119
+ gr.Image(type="filepath", label="Input"),
120
  gr.Radio(['v1.2', 'v1.3', 'v1.4', 'RestoreFormer'], type="value", value='v1.4', label='version'),
121
+ gr.Number(label="Rescaling factor", value=2),
122
  ], [
123
+ gr.Image(type="numpy", label="Output (The whole image)"),
124
+ gr.File(label="Download the output image")
125
  ],
126
  title=title,
127
  description=description,
128
+ article=article,
129
+ examples=[['AI-generate.jpg', 'v1.4', 2],
130
+ ['lincoln.jpg', 'v1.4', 2],
131
+ ['Blake_Lively.jpg', 'v1.4', 2],
132
+ ['10045.png', 'v1.4', 2]])
133
  demo.queue().launch()
examples.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+ examples = [
4
+ {
5
+ 'name': 'lincoln.jpg',
6
+ 'url': 'https://upload.wikimedia.org/wikipedia/commons/thumb/a/ab/Abraham_Lincoln_O-77_matte_collodion_print.jpg/1024px-Abraham_Lincoln_O-77_matte_collodion_print.jpg'
7
+ },
8
+ {
9
+ 'name': 'AI-generate.jpg',
10
+ 'url': 'https://user-images.githubusercontent.com/17445847/187400315-87a90ac9-d231-45d6-b377-38702bd1838f.jpg'
11
+ },
12
+ {
13
+ 'name': 'Blake_Lively.jpg',
14
+ 'url': 'https://user-images.githubusercontent.com/17445847/187400981-8a58f7a4-ef61-42d9-af80-bc6234cef860.jpg'
15
+ },
16
+ {
17
+ 'name': '10045.png',
18
+ 'url': 'https://user-images.githubusercontent.com/17445847/187401133-8a3bf269-5b4d-4432-b2f0-6d26ee1d3307.png'
19
+ }
20
+ ]
21
+
22
+
23
+ def download():
24
+ for example in examples:
25
+ torch.hub.download_url_to_file(example['url'], example['name'])