init mirror
Browse files- README.md +5 -5
- app.py +145 -0
- packages.txt +3 -0
- requirements.txt +13 -0
README.md
CHANGED
@@ -1,13 +1,13 @@
|
|
1 |
---
|
2 |
title: GFPGAN
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 3.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
-
license:
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
title: GFPGAN
|
3 |
+
emoji: π
|
4 |
+
colorFrom: yellow
|
5 |
+
colorTo: green
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 3.26.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
+
license: apache-2.0
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
11 |
+
REALESRGAN_REPO_ID = 'leonelhs/realesrgan'
|
12 |
+
GFPGAN_REPO_ID = 'leonelhs/gfpgan'
|
13 |
+
|
14 |
+
os.system("pip freeze")
|
15 |
+
|
16 |
+
torch.hub.download_url_to_file(
|
17 |
+
'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',
|
18 |
+
'lincoln.jpg')
|
19 |
+
torch.hub.download_url_to_file(
|
20 |
+
'https://user-images.githubusercontent.com/17445847/187400315-87a90ac9-d231-45d6-b377-38702bd1838f.jpg',
|
21 |
+
'AI-generate.jpg')
|
22 |
+
torch.hub.download_url_to_file(
|
23 |
+
'https://user-images.githubusercontent.com/17445847/187400981-8a58f7a4-ef61-42d9-af80-bc6234cef860.jpg',
|
24 |
+
'Blake_Lively.jpg')
|
25 |
+
torch.hub.download_url_to_file(
|
26 |
+
'https://user-images.githubusercontent.com/17445847/187401133-8a3bf269-5b4d-4432-b2f0-6d26ee1d3307.png',
|
27 |
+
'10045.png')
|
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 |
+
model_path = hf_hub_download(repo_id=REALESRGAN_REPO_ID, filename='realesr-general-x4v3.pth')
|
32 |
+
half = True if torch.cuda.is_available() else False
|
33 |
+
upsampler = RealESRGANer(scale=4, model_path=model_path, model=model, tile=0, tile_pad=10, pre_pad=0, half=half)
|
34 |
+
|
35 |
+
os.makedirs('output', exist_ok=True)
|
36 |
+
|
37 |
+
|
38 |
+
# def inference(img, version, scale, weight):
|
39 |
+
def inference(img, version, scale):
|
40 |
+
# weight /= 100
|
41 |
+
print(img, version, scale)
|
42 |
+
if scale > 4:
|
43 |
+
scale = 4 # avoid too large scale value
|
44 |
+
try:
|
45 |
+
extension = os.path.splitext(os.path.basename(str(img)))[1]
|
46 |
+
img = cv2.imread(img, cv2.IMREAD_UNCHANGED)
|
47 |
+
if len(img.shape) == 3 and img.shape[2] == 4:
|
48 |
+
img_mode = 'RGBA'
|
49 |
+
elif len(img.shape) == 2: # for gray inputs
|
50 |
+
img_mode = None
|
51 |
+
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
52 |
+
else:
|
53 |
+
img_mode = None
|
54 |
+
|
55 |
+
h, w = img.shape[0:2]
|
56 |
+
if h > 3500 or w > 3500:
|
57 |
+
print('too large size')
|
58 |
+
return None, None
|
59 |
+
|
60 |
+
if h < 300:
|
61 |
+
img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
|
62 |
+
|
63 |
+
face_enhancer = None
|
64 |
+
snapshot_folder = snapshot_download(repo_id=GFPGAN_REPO_ID)
|
65 |
+
|
66 |
+
if version == 'v1.2':
|
67 |
+
path = os.path.join(snapshot_folder, 'GFPGANv1.2.pth')
|
68 |
+
face_enhancer = GFPGANer(
|
69 |
+
model_path=path, upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
|
70 |
+
elif version == 'v1.3':
|
71 |
+
path = os.path.join(snapshot_folder, 'GFPGANv1.3.pth')
|
72 |
+
face_enhancer = GFPGANer(
|
73 |
+
model_path=path, upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
|
74 |
+
elif version == 'v1.4':
|
75 |
+
path = os.path.join(snapshot_folder, 'GFPGANv1.4.pth')
|
76 |
+
face_enhancer = GFPGANer(
|
77 |
+
model_path=path, upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
|
78 |
+
elif version == 'RestoreFormer':
|
79 |
+
path = os.path.join(snapshot_folder, 'RestoreFormer.pth')
|
80 |
+
face_enhancer = GFPGANer(
|
81 |
+
model_path=path, upscale=2, arch='RestoreFormer', channel_multiplier=2,
|
82 |
+
bg_upsampler=upsampler)
|
83 |
+
|
84 |
+
try:
|
85 |
+
_, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
|
86 |
+
except RuntimeError as error:
|
87 |
+
print('Error', error)
|
88 |
+
|
89 |
+
try:
|
90 |
+
if scale != 2:
|
91 |
+
interpolation = cv2.INTER_AREA if scale < 2 else cv2.INTER_LANCZOS4
|
92 |
+
h, w = img.shape[0:2]
|
93 |
+
output = cv2.resize(output, (int(w * scale / 2), int(h * scale / 2)), interpolation=interpolation)
|
94 |
+
except Exception as error:
|
95 |
+
print('wrong scale input.', error)
|
96 |
+
if img_mode == 'RGBA': # RGBA images should be saved in png format
|
97 |
+
extension = 'png'
|
98 |
+
else:
|
99 |
+
extension = 'jpg'
|
100 |
+
save_path = f'output/out.{extension}'
|
101 |
+
cv2.imwrite(save_path, output)
|
102 |
+
|
103 |
+
output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
|
104 |
+
return output, save_path
|
105 |
+
except Exception as error:
|
106 |
+
print('global exception', error)
|
107 |
+
return None, None
|
108 |
+
|
109 |
+
|
110 |
+
title = "GFPGAN: Practical Face Restoration Algorithm"
|
111 |
+
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>
|
112 |
+
It can be used to restore your **old photos** or improve **AI-generated faces**.<br>
|
113 |
+
To use it, simply upload your image.<br>
|
114 |
+
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 π
|
115 |
+
"""
|
116 |
+
article = r"""
|
117 |
+
|
118 |
+
[![download](https://img.shields.io/github/downloads/TencentARC/GFPGAN/total.svg)](https://github.com/TencentARC/GFPGAN/releases)
|
119 |
+
[![GitHub Stars](https://img.shields.io/github/stars/TencentARC/GFPGAN?style=social)](https://github.com/TencentARC/GFPGAN)
|
120 |
+
[![arXiv](https://img.shields.io/badge/arXiv-Paper-<COLOR>.svg)](https://arxiv.org/abs/2101.04061)
|
121 |
+
|
122 |
+
If you have any question, please email π§ `xintao.wang@outlook.com` or `xintaowang@tencent.com`.
|
123 |
+
|
124 |
+
<center><img src='https://visitor-badge.glitch.me/badge?page_id=akhaliq_GFPGAN' alt='visitor badge'></center>
|
125 |
+
<center><img src='https://visitor-badge.glitch.me/badge?page_id=Gradio_Xintao_GFPGAN' alt='visitor badge'></center>
|
126 |
+
"""
|
127 |
+
demo = gr.Interface(
|
128 |
+
inference, [
|
129 |
+
gr.Image(type="filepath", label="Input"),
|
130 |
+
# gr.Radio(['v1.2', 'v1.3', 'v1.4', 'RestoreFormer', 'CodeFormer'], type="value", value='v1.4', label='version'),
|
131 |
+
gr.Radio(['v1.2', 'v1.3', 'v1.4', 'RestoreFormer'], type="value", value='v1.4', label='version'),
|
132 |
+
gr.Number(label="Rescaling factor", value=2),
|
133 |
+
# gr.Slider(0, 100, label='Weight, only for CodeFormer. 0 for better quality, 100 for better identity', value=50)
|
134 |
+
], [
|
135 |
+
gr.Image(type="numpy", label="Output (The whole image)"),
|
136 |
+
gr.File(label="Download the output image")
|
137 |
+
],
|
138 |
+
title=title,
|
139 |
+
description=description,
|
140 |
+
article=article,
|
141 |
+
examples=[['AI-generate.jpg', 'v1.4', 2],
|
142 |
+
['lincoln.jpg', 'v1.4', 2],
|
143 |
+
['Blake_Lively.jpg', 'v1.4', 2],
|
144 |
+
['10045.png', 'v1.4', 2]])
|
145 |
+
demo.queue().launch()
|
packages.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
ffmpeg
|
2 |
+
libsm6
|
3 |
+
libxext6
|
requirements.txt
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch>=2.0.1
|
2 |
+
basicsr>=1.4.2
|
3 |
+
facexlib>=0.3.0
|
4 |
+
gfpgan>=1.3.8
|
5 |
+
realesrgan>=0.3.0
|
6 |
+
numpy
|
7 |
+
opencv-python
|
8 |
+
torchvision
|
9 |
+
scipy
|
10 |
+
tqdm
|
11 |
+
lmdb
|
12 |
+
pyyaml
|
13 |
+
yapf
|