Spaces:
Runtime error
Runtime error
Xintao
commited on
Commit
•
112d8be
1
Parent(s):
82acb0f
add v1.4, restorformer, codeformer
Browse files- app.py +38 -17
- requirements.txt +2 -2
app.py
CHANGED
@@ -8,9 +8,19 @@ from gfpgan.utils import GFPGANer
|
|
8 |
from realesrgan.utils import RealESRGANer
|
9 |
|
10 |
os.system("pip freeze")
|
11 |
-
|
12 |
-
os.
|
13 |
-
os.system("wget https://github.com/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
torch.hub.download_url_to_file(
|
16 |
'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',
|
@@ -31,20 +41,20 @@ 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(
|
36 |
-
model_path='GFPGANv1.3.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
|
37 |
-
face_enhancer_v2 = 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 |
-
|
|
|
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 |
|
@@ -53,15 +63,25 @@ def inference(img, version, scale):
|
|
53 |
img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
|
54 |
|
55 |
if version == 'v1.2':
|
56 |
-
face_enhancer =
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
@@ -104,8 +124,9 @@ If you have any question, please email 📧 `[email protected]` or `xintao
|
|
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.
|
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")
|
|
|
8 |
from realesrgan.utils import RealESRGANer
|
9 |
|
10 |
os.system("pip freeze")
|
11 |
+
# download weights
|
12 |
+
if not os.path.exists('realesr-general-x4v3.pth'):
|
13 |
+
os.system("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth -P .")
|
14 |
+
if not os.path.exists('GFPGANv1.2.pth'):
|
15 |
+
os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.2.pth -P .")
|
16 |
+
if not os.path.exists('GFPGANv1.3.pth'):
|
17 |
+
os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth -P .")
|
18 |
+
if not os.path.exists('GFPGANv1.4.pth'):
|
19 |
+
os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth -P .")
|
20 |
+
if not os.path.exists('RestoreFormer.pth'):
|
21 |
+
os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/RestoreFormer.pth -P .")
|
22 |
+
if not os.path.exists('CodeFormer.pth'):
|
23 |
+
os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/CodeFormer.pth -P .")
|
24 |
|
25 |
torch.hub.download_url_to_file(
|
26 |
'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',
|
|
|
41 |
half = True if torch.cuda.is_available() else False
|
42 |
upsampler = RealESRGANer(scale=4, model_path=model_path, model=model, tile=0, tile_pad=10, pre_pad=0, half=half)
|
43 |
|
|
|
|
|
|
|
|
|
|
|
44 |
os.makedirs('output', exist_ok=True)
|
45 |
|
46 |
|
47 |
+
def inference(img, version, scale, weight):
|
48 |
+
weight /= 100
|
49 |
+
print(img, version, scale, weight)
|
50 |
try:
|
51 |
+
extension = os.path.splitext(os.path.basename(str(img)))[1]
|
52 |
img = cv2.imread(img, cv2.IMREAD_UNCHANGED)
|
53 |
if len(img.shape) == 3 and img.shape[2] == 4:
|
54 |
img_mode = 'RGBA'
|
55 |
+
elif len(img.shape) == 2: # for gray inputs
|
56 |
+
img_mode = None
|
57 |
+
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
58 |
else:
|
59 |
img_mode = None
|
60 |
|
|
|
63 |
img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
|
64 |
|
65 |
if version == 'v1.2':
|
66 |
+
face_enhancer = GFPGANer(
|
67 |
+
model_path='GFPGANv1.2.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
|
68 |
+
elif version == 'v1.3':
|
69 |
+
face_enhancer = GFPGANer(
|
70 |
+
model_path='GFPGANv1.3.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
|
71 |
+
elif version == 'v1.4':
|
72 |
+
face_enhancer = GFPGANer(
|
73 |
+
model_path='GFPGANv1.4.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
|
74 |
+
elif version == 'RestoreFormer':
|
75 |
+
face_enhancer = GFPGANer(
|
76 |
+
model_path='RestoreFormer.pth', upscale=2, arch='RestoreFormer', channel_multiplier=2, bg_upsampler=upsampler)
|
77 |
+
elif version == 'CodeFormer':
|
78 |
+
face_enhancer = GFPGANer(
|
79 |
+
model_path='CodeFormer.pth', upscale=2, arch='CodeFormer', channel_multiplier=2, bg_upsampler=upsampler)
|
80 |
+
|
81 |
try:
|
82 |
_, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
|
83 |
except RuntimeError as error:
|
84 |
print('Error', error)
|
|
|
|
|
85 |
|
86 |
try:
|
87 |
if scale != 2:
|
|
|
124 |
gr.Interface(
|
125 |
inference, [
|
126 |
gr.inputs.Image(type="filepath", label="Input"),
|
127 |
+
gr.inputs.Radio(['v1.2', 'v1.3', 'v1.4', 'RestoeFormer', 'CodeFormer'], type="value", default='v1.4', label='version'),
|
128 |
+
gr.inputs.Number(label="Rescaling factor", default=2),
|
129 |
+
gr.Slider(0, 100, label='weight, only for CodeFormer. 0 for better quality, 100 for better identity', default=50)
|
130 |
], [
|
131 |
gr.outputs.Image(type="numpy", label="Output (The whole image)"),
|
132 |
gr.outputs.File(label="Download the output image")
|
requirements.txt
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
torch>=1.7
|
2 |
basicsr>=1.4.2
|
3 |
-
facexlib>=0.2.
|
4 |
-
gfpgan>=1.3.
|
5 |
realesrgan>=0.2.5
|
6 |
numpy
|
7 |
opencv-python
|
|
|
1 |
torch>=1.7
|
2 |
basicsr>=1.4.2
|
3 |
+
facexlib>=0.2.5
|
4 |
+
gfpgan>=1.3.6
|
5 |
realesrgan>=0.2.5
|
6 |
numpy
|
7 |
opencv-python
|