GFPGAN:用于真实世界面部修复的实用算法

一、GFPGAN

(1)介绍

官网地址:TencentARC/GFPGAN: GFPGAN aims at developing Practical Algorithms for Real-world Face Restoration. (github.com)

这个GFPGAN是一种图像修复的算法,可以把破旧模糊的图片还原成清晰可见的照片。

一点也不夸张,我试了一下,把一个64x64的照片变成256x256分辨率的清晰照片,非常高清。

这个算法是基于这个叫Real-ESRGAN的项目,所以都是一步步迭代上来的,挺了不起的!感兴趣的朋友可以去复现一下。这里面还有需要注意的两个项目包,前面的real-rsrgangfpgan都是基于下面两个基础上完成的:

BasicSR: An open-source image and video restoration toolbox
facexlib: A collection that provides useful face-relation functions

basicsr负责的是图像和视频修复,这个是最厉害的工具,而facexlib是提供面部关系一个函数库,提高修复效果。
论文:GFP-GAN: Towards Real-World Blind Face Restoration with Generative Facial Prior

(2)安装环境

1)主要是python≥3.7、pytorch≥1.7、一块GPU显卡

2)下载命令:

1
2
git clone https://github.com/TencentARC/GFPGAN.git
cd GFPGAN
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Install basicsr - https://github.com/xinntao/BasicSR
# We use BasicSR for both training and inference
pip install basicsr

# Install facexlib - https://github.com/xinntao/facexlib
# We use face detection and face restoration helper in the facexlib package
pip install facexlib

pip install -r requirements.txt
python setup.py develop

# If you want to enhance the background (non-face) regions with Real-ESRGAN,
# you also need to install the realesrgan package
pip install realesrgan

3)下载模型GFPGANv1.3(可选项,也可以是v1.2和v1):

1
wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth -P experiments/pretrained_models

4)运行代码,run:

1
python inference_gfpgan.py -i inputs/whole_imgs -o results -v 1.3 -s 2

命令的一些参数:

1
2
3
4
5
6
7
8
9
10
11
12
13
Usage: python inference_gfpgan.py -i inputs/whole_imgs -o results -v 1.3 -s 2 [options]...

-h show this help
-i input Input image or folder. Default: inputs/whole_imgs
-o output Output folder. Default: results
-v version GFPGAN model version. Option: 1 | 1.2 | 1.3. Default: 1.3
-s upscale The final upsampling scale of the image. Default: 2
-bg_upsampler background upsampler. Default: realesrgan
-bg_tile Tile size for background sampler, 0 for no tile during testing. Default: 400
-suffix Suffix of the restored faces
-only_center_face Only restore the center face
-aligned Input are aligned faces
-ext Image extension. Options: auto | jpg | png, auto means using the same extension as inputs. Default: auto

二、项目结构和代码介绍

(1)代码地址

https://github.com/Lucki-ly/GFPGAN_1

(2)项目结构

1

如上图所示,项目的结构有这么多,解释如下:
1)experiments:存放pretrained_models,就是我们的模型(GFPGANv1.3.pth等);

2)gfpgan:模型的目录;

3)inputs:输入文件的目录;

4)options:存放一些训练的yaml文件;

5)results:输出文件的目录;

6)tests:存放一些测试的文件,测试功能;

7)inference_gfpgan.py:项目接口的运行主文件,我们靠这个调用所有的功能;

8)requirements.txt:项目要求的安装文件;

9)setup.py:项目的启动文件,运行之前要python一下。

(3)代码调整

我做了一个图像尺寸(分辨率)的控制,这样我们可以根据自己的需求去获得想要的大小。

前面的参数增加:

1
parser.add_argument('-d', '--desired_size', type=int, default=512, help='Desired size for the output images. Default: 256')

restore部分修改:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
 # ------------------------ restore ------------------------
for img_path in img_list:
# read image
img_name = os.path.basename(img_path)
print(f'Processing {img_name} ...')
basename, ext = os.path.splitext(img_name)
input_img = cv2.imread(img_path, cv2.IMREAD_COLOR)


# Print the size of the input image
height, width, _ = input_img.shape
print(f'Input image size: {width}x{height}')


# restore faces and background if necessary
cropped_faces, restored_faces, restored_img = restorer.enhance(
input_img,
has_aligned=args.aligned,
only_center_face=args.only_center_face,
paste_back=True,
weight=args.weight)

# Print the size of the restored image
if restored_img is not None:
height, width, _ = restored_img.shape
print(f'Restored image size: {width}x{height}')

# Resize the restored image to 256x256
restored_img = cv2.resize(restored_img, (args.desired_size, args.desired_size), interpolation=cv2.INTER_LINEAR)

# Print the size of the restored image after resizing
height, width, _ = restored_img.shape
print(f'Restored image size after resizing: {width}x{height}')

# save faces
for idx, (cropped_face, restored_face) in enumerate(zip(cropped_faces, restored_faces)):
cropped_face = cv2.resize(cropped_face, (args.desired_size, args.desired_size), interpolation=cv2.INTER_LINEAR)
restored_face = cv2.resize(restored_face, (args.desired_size, args.desired_size), interpolation=cv2.INTER_LINEAR)

# save cropped face
save_crop_path = os.path.join(args.output, 'cropped_faces', f'{basename}_{idx:02d}.jpg')
imwrite(cropped_face, save_crop_path)
# save restored face
if args.suffix is not None:
save_face_name = f'{basename}_{idx:02d}_{args.suffix}.jpg'
else:
save_face_name = f'{basename}_{idx:02d}.jpg'
save_restore_path = os.path.join(args.output, 'restored_faces', save_face_name)
imwrite(restored_face, save_restore_path)
# save comparison image
cmp_img = np.concatenate((cropped_face, restored_face), axis=1)
imwrite(cmp_img, os.path.join(args.output, 'cmp', f'{basename}_{idx:02d}.jpg'))

三、效果展示

2

3

4

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2024 John Doe
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信