赞
踩
使用腾讯 gfpgan方案进行人脸识别并高清修复
1.首先在github下载源文件https://github.com/TencentARC/GFPGAN
左边是从视频中截取的人脸,右边是修复后的结果,只能说这效果杠杠的
2.代码修改后,运行时会在’xxx\gfpgan\weights’文件夹中自动下载GFPGANv1.3.pth
模型文件,同时在程序的文件夹’xxx\notepad\gfpgan\weights’下会下载’detection_Resnet50_Final.pth’, ‘parsing_parsenet.pth’两个模型,
可以自动运行时自动下载相应模型到对应位置,
也可以在如下链接’ ’下载这三个模型,然后放到对应的文件夹下
1)定义关键参数(未做修改) upscale = 2 arch ='clean' channel_multiplier=2 bg_upsampler = None aligned = False only_center_face = False weight=0.5 2)调用GFPGANer(...) restorer = GFPGANer( model_path=model_path, upscale=upscale, arch=arch, channel_multiplier=channel_multiplier, bg_upsampler=bg_upsampler) 3) cropped_faces, restored_faces, restored_img = restorer.enhance( input_img, has_aligned=aligned, only_center_face=only_center_face, paste_back=True, weight=weight) ---restored_faces即是我们需要的图片数据
from gfpgan import GFPGANer import torch,os,glob,cv2,sys import numpy as np #from basicsr.utils import imwrite baseDirPath = sys.path[0] print(baseDirPath) ################################################################### #读含中文/韩文/日文等特殊字符路径的图片 def cv_imread(in_path): im = cv2.imdecode(np.fromfile(in_path, dtype=np.uint8),-1) return im #路径中有中文名 cv2写 def cv_imwrite(out_path, imp_np): imp_type = '.' + out_path.split('.')[-1] cv2.imencode(imp_type, imp_np)[1].tofile(out_path) ################################################################### #模型位置 model_path = baseDirPath + '\\gfpgan\\weights\\GFPGANv1.3.pth' #如下参数固定不变 upscale = 2 arch ='clean' channel_multiplier=2 bg_upsampler = None aligned = False only_center_face = False weight=0.5 if not torch.cuda.is_available(): # CPU bg_upsampler = None restorer = GFPGANer( model_path=model_path, upscale=upscale, arch=arch, channel_multiplier=channel_multiplier, bg_upsampler=bg_upsampler) input_path = baseDirPath + '\\pic\\src' output_path = baseDirPath + '\\pic\\dst' if os.path.isfile(input_path): img_list = [input_path] else: img_list = sorted(glob.glob(os.path.join(input_path, '*'))) print(img_list) for img_path in img_list: img_name = os.path.basename(img_path) print(f'processing {img_name} ...') basename, ext = os.path.splitext(img_name) input_img = cv_imread(img_path) cropped_faces, restored_faces, restored_img = restorer.enhance( input_img, has_aligned=aligned, only_center_face=only_center_face, paste_back=True, weight=weight) # save faces for idx, restored_face in enumerate(restored_faces): # save restored face save_restore_path = os.path.join(output_path, f'{basename}_{idx:02d}.png') cv_imwrite(save_restore_path, restored_face) print(f'Results are in the [{output_path}] folder.')
备注:如果输入图片尺寸较小或质量较差,会出现很多奇葩结果,如这样:
源码:https://download.csdn.net/download/mjc1321/89070575
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。