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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import cv2
|
| 3 |
+
import glob
|
| 4 |
+
import numpy as np
|
| 5 |
+
import os
|
| 6 |
+
import torch
|
| 7 |
+
from basicsr.utils import imwrite
|
| 8 |
+
|
| 9 |
+
from gfpgan import GFPGANer
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def main():
|
| 13 |
+
"""Inference demo for GFPGAN (for users).
|
| 14 |
+
"""
|
| 15 |
+
parser = argparse.ArgumentParser()
|
| 16 |
+
parser.add_argument(
|
| 17 |
+
'-i',
|
| 18 |
+
'--input',
|
| 19 |
+
type=str,
|
| 20 |
+
default='inputs/whole_imgs',
|
| 21 |
+
help='Input image or folder. Default: inputs/whole_imgs')
|
| 22 |
+
parser.add_argument('-o', '--output', type=str, default='results', help='Output folder. Default: results')
|
| 23 |
+
# we use version to select models, which is more user-friendly
|
| 24 |
+
parser.add_argument(
|
| 25 |
+
'-v', '--version', type=str, default='1.3', help='GFPGAN model version. Option: 1 | 1.2 | 1.3. Default: 1.3')
|
| 26 |
+
parser.add_argument(
|
| 27 |
+
'-s', '--upscale', type=int, default=2, help='The final upsampling scale of the image. Default: 2')
|
| 28 |
+
|
| 29 |
+
parser.add_argument(
|
| 30 |
+
'--bg_upsampler', type=str, default='realesrgan', help='background upsampler. Default: realesrgan')
|
| 31 |
+
parser.add_argument(
|
| 32 |
+
'--bg_tile',
|
| 33 |
+
type=int,
|
| 34 |
+
default=400,
|
| 35 |
+
help='Tile size for background sampler, 0 for no tile during testing. Default: 400')
|
| 36 |
+
parser.add_argument('--suffix', type=str, default=None, help='Suffix of the restored faces')
|
| 37 |
+
parser.add_argument('--only_center_face', action='store_true', help='Only restore the center face')
|
| 38 |
+
parser.add_argument('--aligned', action='store_true', help='Input are aligned faces')
|
| 39 |
+
parser.add_argument(
|
| 40 |
+
'--ext',
|
| 41 |
+
type=str,
|
| 42 |
+
default='auto',
|
| 43 |
+
help='Image extension. Options: auto | jpg | png, auto means using the same extension as inputs. Default: auto')
|
| 44 |
+
parser.add_argument('-w', '--weight', type=float, default=0.5, help='Adjustable weights.')
|
| 45 |
+
args = parser.parse_args()
|
| 46 |
+
|
| 47 |
+
args = parser.parse_args()
|
| 48 |
+
|
| 49 |
+
# ------------------------ input & output ------------------------
|
| 50 |
+
if args.input.endswith('/'):
|
| 51 |
+
args.input = args.input[:-1]
|
| 52 |
+
if os.path.isfile(args.input):
|
| 53 |
+
img_list = [args.input]
|
| 54 |
+
else:
|
| 55 |
+
img_list = sorted(glob.glob(os.path.join(args.input, '*')))
|
| 56 |
+
|
| 57 |
+
os.makedirs(args.output, exist_ok=True)
|
| 58 |
+
|
| 59 |
+
# ------------------------ set up background upsampler ------------------------
|
| 60 |
+
if args.bg_upsampler == 'realesrgan':
|
| 61 |
+
if not torch.cuda.is_available(): # CPU
|
| 62 |
+
import warnings
|
| 63 |
+
warnings.warn('The unoptimized RealESRGAN is slow on CPU. We do not use it. '
|
| 64 |
+
'If you really want to use it, please modify the corresponding codes.')
|
| 65 |
+
bg_upsampler = None
|
| 66 |
+
else:
|
| 67 |
+
from basicsr.archs.rrdbnet_arch import RRDBNet
|
| 68 |
+
from realesrgan import RealESRGANer
|
| 69 |
+
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2)
|
| 70 |
+
bg_upsampler = RealESRGANer(
|
| 71 |
+
scale=2,
|
| 72 |
+
model_path='https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth',
|
| 73 |
+
model=model,
|
| 74 |
+
tile=args.bg_tile,
|
| 75 |
+
tile_pad=10,
|
| 76 |
+
pre_pad=0,
|
| 77 |
+
half=True) # need to set False in CPU mode
|
| 78 |
+
else:
|
| 79 |
+
bg_upsampler = None
|
| 80 |
+
|
| 81 |
+
# ------------------------ set up GFPGAN restorer ------------------------
|
| 82 |
+
if args.version == '1':
|
| 83 |
+
arch = 'original'
|
| 84 |
+
channel_multiplier = 1
|
| 85 |
+
model_name = 'GFPGANv1'
|
| 86 |
+
url = 'https://github.com/TencentARC/GFPGAN/releases/download/v0.1.0/GFPGANv1.pth'
|
| 87 |
+
elif args.version == '1.2':
|
| 88 |
+
arch = 'clean'
|
| 89 |
+
channel_multiplier = 2
|
| 90 |
+
model_name = 'GFPGANCleanv1-NoCE-C2'
|
| 91 |
+
url = 'https://github.com/TencentARC/GFPGAN/releases/download/v0.2.0/GFPGANCleanv1-NoCE-C2.pth'
|
| 92 |
+
elif args.version == '1.3':
|
| 93 |
+
arch = 'clean'
|
| 94 |
+
channel_multiplier = 2
|
| 95 |
+
model_name = 'GFPGANv1.3'
|
| 96 |
+
url = 'https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth'
|
| 97 |
+
elif args.version == '1.4':
|
| 98 |
+
arch = 'clean'
|
| 99 |
+
channel_multiplier = 2
|
| 100 |
+
model_name = 'GFPGANv1.4'
|
| 101 |
+
url = 'https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth'
|
| 102 |
+
elif args.version == 'RestoreFormer':
|
| 103 |
+
arch = 'RestoreFormer'
|
| 104 |
+
channel_multiplier = 2
|
| 105 |
+
model_name = 'RestoreFormer'
|
| 106 |
+
url = 'https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/RestoreFormer.pth'
|
| 107 |
+
else:
|
| 108 |
+
raise ValueError(f'Wrong model version {args.version}.')
|
| 109 |
+
|
| 110 |
+
# determine model paths
|
| 111 |
+
model_path = os.path.join('experiments/pretrained_models', model_name + '.pth')
|
| 112 |
+
if not os.path.isfile(model_path):
|
| 113 |
+
model_path = os.path.join('gfpgan/weights', model_name + '.pth')
|
| 114 |
+
if not os.path.isfile(model_path):
|
| 115 |
+
# download pre-trained models from url
|
| 116 |
+
model_path = url
|
| 117 |
+
|
| 118 |
+
restorer = GFPGANer(
|
| 119 |
+
model_path=model_path,
|
| 120 |
+
upscale=args.upscale,
|
| 121 |
+
arch=arch,
|
| 122 |
+
channel_multiplier=channel_multiplier,
|
| 123 |
+
bg_upsampler=bg_upsampler)
|
| 124 |
+
|
| 125 |
+
# ------------------------ restore ------------------------
|
| 126 |
+
for img_path in img_list:
|
| 127 |
+
# read image
|
| 128 |
+
img_name = os.path.basename(img_path)
|
| 129 |
+
print(f'Processing {img_name} ...')
|
| 130 |
+
basename, ext = os.path.splitext(img_name)
|
| 131 |
+
input_img = cv2.imread(img_path, cv2.IMREAD_COLOR)
|
| 132 |
+
|
| 133 |
+
# restore faces and background if necessary
|
| 134 |
+
cropped_faces, restored_faces, restored_img = restorer.enhance(
|
| 135 |
+
input_img,
|
| 136 |
+
has_aligned=args.aligned,
|
| 137 |
+
only_center_face=args.only_center_face,
|
| 138 |
+
paste_back=True,
|
| 139 |
+
weight=args.weight)
|
| 140 |
+
|
| 141 |
+
# save faces
|
| 142 |
+
for idx, (cropped_face, restored_face) in enumerate(zip(cropped_faces, restored_faces)):
|
| 143 |
+
# save cropped face
|
| 144 |
+
save_crop_path = os.path.join(args.output, 'cropped_faces', f'{basename}_{idx:02d}.png')
|
| 145 |
+
imwrite(cropped_face, save_crop_path)
|
| 146 |
+
# save restored face
|
| 147 |
+
if args.suffix is not None:
|
| 148 |
+
save_face_name = f'{basename}_{idx:02d}_{args.suffix}.png'
|
| 149 |
+
else:
|
| 150 |
+
save_face_name = f'{basename}_{idx:02d}.png'
|
| 151 |
+
save_restore_path = os.path.join(args.output, 'restored_faces', save_face_name)
|
| 152 |
+
imwrite(restored_face, save_restore_path)
|
| 153 |
+
# save comparison image
|
| 154 |
+
cmp_img = np.concatenate((cropped_face, restored_face), axis=1)
|
| 155 |
+
imwrite(cmp_img, os.path.join(args.output, 'cmp', f'{basename}_{idx:02d}.png'))
|
| 156 |
+
|
| 157 |
+
# save restored img
|
| 158 |
+
if restored_img is not None:
|
| 159 |
+
if args.ext == 'auto':
|
| 160 |
+
extension = ext[1:]
|
| 161 |
+
else:
|
| 162 |
+
extension = args.ext
|
| 163 |
+
|
| 164 |
+
if args.suffix is not None:
|
| 165 |
+
save_restore_path = os.path.join(args.output, 'restored_imgs', f'{basename}_{args.suffix}.{extension}')
|
| 166 |
+
else:
|
| 167 |
+
save_restore_path = os.path.join(args.output, 'restored_imgs', f'{basename}.{extension}')
|
| 168 |
+
imwrite(restored_img, save_restore_path)
|
| 169 |
+
|
| 170 |
+
print(f'Results are in the [{args.output}] folder.')
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
if __name__ == '__main__':
|
| 174 |
+
main()
|