Spaces:
Running
Running
update.
Browse filesFiles changed (8)
4 unmodified lines5678910111213108 unmodified lines1221231241251261271284 unmodified lines# ignored files with suffix*.html# *.png# *.jpeg# *.jpg*.pt*.gif*.pth108 unmodified lines.mypy_cache/# projectresults/dlib/*.pth*_old*4 unmodified lines5678910111213108 unmodified lines1221231241251261271281294 unmodified lines# ignored files with suffix*.html*.png*.jpeg*.jpg*.pt*.gif*.pth108 unmodified lines.mypy_cache/# projectCodeFormer/results/output/dlib/*.pth*_old*
12345678910111213117 unmodified lines131132133134import numpy as npimport osimport randomimport timeimport torchfrom os import path as ospfrom .dist_util import master_onlyfrom .logger import get_root_loggerdef set_random_seed(seed):"""Set random seeds."""117 unmodified linesif abs(size) < 1024.0:return f'{size:3.1f} {unit}{suffix}'size /= 1024.0return f'{size:3.1f} Y{suffix}'123456789101112131415161718192021222324252627282930313233343536117 unmodified lines154155156157import osimport reimport randomimport timeimport torchimport numpy as npfrom os import path as ospfrom .dist_util import master_onlyfrom .logger import get_root_loggerIS_HIGH_VERSION = [int(m) for m in list(re.findall(r"^([0-9]+)\.([0-9]+)\.([0-9]+)([^0-9][a-zA-Z0-9]*)?(\+git.*)?$",\torch.__version__)[0][:3])] >= [1, 12, 0]def gpu_is_available():if IS_HIGH_VERSION:if torch.backends.mps.is_available():return Truereturn True if torch.cuda.is_available() and torch.backends.cudnn.is_available() else Falsedef get_device(gpu_id=None):if gpu_id is None:gpu_str = ''elif isinstance(gpu_id, int):gpu_str = f':{gpu_id}'else:raise TypeError('Input should be int value.')if IS_HIGH_VERSION:if torch.backends.mps.is_available():return torch.device('mps'+gpu_str)return torch.device('cuda'+gpu_str if torch.cuda.is_available() and torch.backends.cudnn.is_available() else 'cpu')def set_random_seed(seed):"""Set random seeds."""117 unmodified linesif abs(size) < 1024.0:return f'{size:3.1f} {unit}{suffix}'size /= 1024.0return f'{size:3.1f} Y{suffix}'
12345# GENERATED VERSION FILE# TIME: Sun Aug 7 15:14:26 2022__version__ = '1.3.2'__gitsha__ = '6f94023'version_info = (1, 3, 2)12345# GENERATED VERSION FILE# TIME: Sat Sep 21 15:31:46 2024__version__ = '1.3.2'__gitsha__ = '1.3.2'version_info = (1, 3, 2)
5 unmodified lines67891011121350 unmodified lines64656667686970715 unmodified lines7778798081828313 unmodified lines97989910010110210310410510610710816 unmodified lines1251261271281291301311 unmodified line133134135136137138139140141142143144145146147148149150151152153154155156157140 unmodified lines2982993003013023033043053063075 unmodified linesfrom facelib.detection import init_detection_modelfrom facelib.parsing import init_parsing_modelfrom facelib.utils.misc import img2tensor, imwrite, is_gray, bgr2graydef get_largest_face(det_faces, h, w):50 unmodified linesself.crop_ratio = crop_ratio # (h, w)assert (self.crop_ratio[0] >= 1 and self.crop_ratio[1] >= 1), 'crop ration only supports >=1'self.face_size = (int(face_size * self.crop_ratio[1]), int(face_size * self.crop_ratio[0]))if self.template_3points:self.face_template = np.array([[192, 240], [319, 240], [257, 371]])else:# standard 5 landmarks for FFHQ faces with 512 x 5125 unmodified lines# self.face_template = np.array([[193.65928, 242.98541], [318.32558, 243.06108], [255.67984, 328.82894],# [198.22603, 372.82502], [313.91018, 372.75659]])self.face_template = self.face_template * (face_size / 512.0)if self.crop_ratio[0] > 1:self.face_template[:, 1] += face_size * (self.crop_ratio[0] - 1) / 213 unmodified linesself.pad_input_imgs = []if device is None:self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')else:self.device = device# init face detection modelself.face_det = init_detection_model(det_model, half=False, device=self.device)# init face parsing modelself.use_parse = use_parse16 unmodified linesimg = img[:, :, 0:3]self.input_img = imgself.is_gray = is_gray(img, threshold=5)if self.is_gray:print('Grayscale input: True')1 unmodified linef = 512.0/min(self.input_img.shape[:2])self.input_img = cv2.resize(self.input_img, (0,0), fx=f, fy=f, interpolation=cv2.INTER_LINEAR)def get_face_landmarks_5(self,only_keep_largest=False,only_center_face=False,resize=None,blur_ratio=0.01,eye_dist_threshold=None):if resize is None:scale = 1input_img = self.input_imgelse:h, w = self.input_img.shape[0:2]scale = resize / min(h, w)scale = max(1, scale) # always scale uph, w = int(h * scale), int(w * scale)interp = cv2.INTER_AREA if scale < 1 else cv2.INTER_LINEARinput_img = cv2.resize(self.input_img, (w, h), interpolation=interp)with torch.no_grad():bboxes = self.face_det.detect_faces(input_img)if bboxes is None or bboxes.shape[0] == 0:return 0140 unmodified linestorch.save(inverse_affine, save_path)def add_restored_face(self, face):if self.is_gray:face = bgr2gray(face) # convert img into grayscaleself.restored_faces.append(face)def paste_faces_to_input_image(self, save_path=None, upsample_img=None, draw_box=False, face_upsampler=None):5 unmodified lines67891011121314151617181950 unmodified lines7071727374757677787980818283845 unmodified lines90919293949513 unmodified lines10911011111211311411511611711811912012112212312416 unmodified lines1411421431441451461471 unmodified line149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220140 unmodified lines3613623633643653663673683693703713725 unmodified linesfrom facelib.detection import init_detection_modelfrom facelib.parsing import init_parsing_modelfrom facelib.utils.misc import img2tensor, imwrite, is_gray, bgr2gray, adain_npyfrom basicsr.utils.download_util import load_file_from_urlfrom basicsr.utils.misc import get_devicedlib_model_url = {'face_detector': 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/mmod_human_face_detector-4cb19393.dat','shape_predictor_5': 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/shape_predictor_5_face_landmarks-c4b1e980.dat'}def get_largest_face(det_faces, h, w):50 unmodified linesself.crop_ratio = crop_ratio # (h, w)assert (self.crop_ratio[0] >= 1 and self.crop_ratio[1] >= 1), 'crop ration only supports >=1'self.face_size = (int(face_size * self.crop_ratio[1]), int(face_size * self.crop_ratio[0]))self.det_model = det_modelif self.det_model == 'dlib':# standard 5 landmarks for FFHQ faces with 1024 x 1024self.face_template = np.array([[686.77227723, 488.62376238], [586.77227723, 493.59405941],[337.91089109, 488.38613861], [437.95049505, 493.51485149],[513.58415842, 678.5049505]])self.face_template = self.face_template / (1024 // face_size)elif self.template_3points:self.face_template = np.array([[192, 240], [319, 240], [257, 371]])else:# standard 5 landmarks for FFHQ faces with 512 x 5125 unmodified lines# self.face_template = np.array([[193.65928, 242.98541], [318.32558, 243.06108], [255.67984, 328.82894],# [198.22603, 372.82502], [313.91018, 372.75659]])self.face_template = self.face_template * (face_size / 512.0)if self.crop_ratio[0] > 1:self.face_template[:, 1] += face_size * (self.crop_ratio[0] - 1) / 213 unmodified linesself.pad_input_imgs = []if device is None:# self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')self.device = get_device()else:self.device = device# init face detection modelif self.det_model == 'dlib':self.face_detector, self.shape_predictor_5 = self.init_dlib(dlib_model_url['face_detector'], dlib_model_url['shape_predictor_5'])else:self.face_detector = init_detection_model(det_model, half=False, device=self.device)# init face parsing modelself.use_parse = use_parse16 unmodified linesimg = img[:, :, 0:3]self.input_img = imgself.is_gray = is_gray(img, threshold=10)if self.is_gray:print('Grayscale input: True')1 unmodified linef = 512.0/min(self.input_img.shape[:2])self.input_img = cv2.resize(self.input_img, (0,0), fx=f, fy=f, interpolation=cv2.INTER_LINEAR)def init_dlib(self, detection_path, landmark5_path):"""Initialize the dlib detectors and predictors."""try:import dlibexcept ImportError:print('Please install dlib by running:' 'conda install -c conda-forge dlib')detection_path = load_file_from_url(url=detection_path, model_dir='weights/dlib', progress=True, file_name=None)landmark5_path = load_file_from_url(url=landmark5_path, model_dir='weights/dlib', progress=True, file_name=None)face_detector = dlib.cnn_face_detection_model_v1(detection_path)shape_predictor_5 = dlib.shape_predictor(landmark5_path)return face_detector, shape_predictor_5def get_face_landmarks_5_dlib(self,only_keep_largest=False,scale=1):det_faces = self.face_detector(self.input_img, scale)if len(det_faces) == 0:print('No face detected. Try to increase upsample_num_times.')return 0else:if only_keep_largest:print('Detect several faces and only keep the largest.')face_areas = []for i in range(len(det_faces)):face_area = (det_faces[i].rect.right() - det_faces[i].rect.left()) * (det_faces[i].rect.bottom() - det_faces[i].rect.top())face_areas.append(face_area)largest_idx = face_areas.index(max(face_areas))self.det_faces = [det_faces[largest_idx]]else:self.det_faces = det_facesif len(self.det_faces) == 0:return 0for face in self.det_faces:shape = self.shape_predictor_5(self.input_img, face.rect)landmark = np.array([[part.x, part.y] for part in shape.parts()])self.all_landmarks_5.append(landmark)return len(self.all_landmarks_5)def get_face_landmarks_5(self,only_keep_largest=False,only_center_face=False,resize=None,blur_ratio=0.01,eye_dist_threshold=None):if self.det_model == 'dlib':return self.get_face_landmarks_5_dlib(only_keep_largest)if resize is None:scale = 1input_img = self.input_imgelse:h, w = self.input_img.shape[0:2]scale = resize / min(h, w)# scale = max(1, scale) # always scale up; comment this out for HD images, e.g., AIGC faces.h, w = int(h * scale), int(w * scale)interp = cv2.INTER_AREA if scale < 1 else cv2.INTER_LINEARinput_img = cv2.resize(self.input_img, (w, h), interpolation=interp)with torch.no_grad():bboxes = self.face_detector.detect_faces(input_img)if bboxes is None or bboxes.shape[0] == 0:return 0140 unmodified linestorch.save(inverse_affine, save_path)def add_restored_face(self, restored_face, input_face=None):if self.is_gray:restored_face = bgr2gray(restored_face) # convert img into grayscaleif input_face is not None:restored_face = adain_npy(restored_face, input_face) # transfer the colorself.restored_faces.append(restored_face)def paste_faces_to_input_image(self, save_path=None, upsample_img=None, draw_box=False, face_upsampler=None):
6 unmodified lines789101112131415161718193 unmodified lines232425262728291 unmodified line31323334353637134 unmodified lines1721731746 unmodified linesfrom torch.hub import download_url_to_file, get_dirfrom urllib.parse import urlparse# from basicsr.utils.download_util import download_file_from_google_drive# import gdownROOT_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))def download_pretrained_models(file_ids, save_path_root):os.makedirs(save_path_root, exist_ok=True)for file_name, file_id in file_ids.items():3 unmodified linesuser_response = input(f'{file_name} already exist. Do you want to cover it? Y/N\n')if user_response.lower() == 'y':print(f'Covering {file_name} to {save_path}')# gdown.download(file_url, save_path, quiet=False)# download_file_from_google_drive(file_id, save_path)elif user_response.lower() == 'n':print(f'Skipping {file_name}')1 unmodified lineraise ValueError('Wrong input. Only accepts Y/N.')else:print(f'Downloading {file_name} to {save_path}')# gdown.download(file_url, save_path, quiet=False)# download_file_from_google_drive(file_id, save_path)134 unmodified linesif out_channel == 3:gray = gray[:,:,np.newaxis].repeat(3, axis=2)return gray6 unmodified lines789101112131415161718193 unmodified lines232425262728291 unmodified line31323334353637134 unmodified lines1721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012026 unmodified linesfrom torch.hub import download_url_to_file, get_dirfrom urllib.parse import urlparse# from basicsr.utils.download_util import download_file_from_google_driveROOT_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))def download_pretrained_models(file_ids, save_path_root):import gdownos.makedirs(save_path_root, exist_ok=True)for file_name, file_id in file_ids.items():3 unmodified linesuser_response = input(f'{file_name} already exist. Do you want to cover it? Y/N\n')if user_response.lower() == 'y':print(f'Covering {file_name} to {save_path}')gdown.download(file_url, save_path, quiet=False)# download_file_from_google_drive(file_id, save_path)elif user_response.lower() == 'n':print(f'Skipping {file_name}')1 unmodified lineraise ValueError('Wrong input. Only accepts Y/N.')else:print(f'Downloading {file_name} to {save_path}')gdown.download(file_url, save_path, quiet=False)# download_file_from_google_drive(file_id, save_path)134 unmodified linesif out_channel == 3:gray = gray[:,:,np.newaxis].repeat(3, axis=2)return graydef calc_mean_std(feat, eps=1e-5):"""Args:feat (numpy): 3D [w h c]s"""size = feat.shapeassert len(size) == 3, 'The input feature should be 3D tensor.'c = size[2]feat_var = feat.reshape(-1, c).var(axis=0) + epsfeat_std = np.sqrt(feat_var).reshape(1, 1, c)feat_mean = feat.reshape(-1, c).mean(axis=0).reshape(1, 1, c)return feat_mean, feat_stddef adain_npy(content_feat, style_feat):"""Adaptive instance normalization for numpy.Args:content_feat (numpy): The input feature.style_feat (numpy): The reference feature."""size = content_feat.shapestyle_mean, style_std = calc_mean_std(style_feat)content_mean, content_std = calc_mean_std(content_feat)normalized_feat = (content_feat - np.broadcast_to(content_mean, size)) / np.broadcast_to(content_std, size)return normalized_feat * np.broadcast_to(style_std, size) + np.broadcast_to(style_mean, size)
12342 unmodified lines78910111213142 unmodified lines17181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666741 unmodified lines10911011111211311411511611711811912012112212312412512612722 unmodified lines15015115215315415515621 unmodified lines178179180181182183184185186187188189# Modified by Shangchen Zhou from: https://github.com/TencentARC/GFPGAN/blob/master/inference_gfpgan.pyimport osimport cv2import argparse2 unmodified linesfrom torchvision.transforms.functional import normalizefrom basicsr.utils import imwrite, img2tensor, tensor2imgfrom basicsr.utils.download_util import load_file_from_urlfrom facelib.utils.face_restoration_helper import FaceRestoreHelperimport torch.nn.functional as Ffrom basicsr.utils.registry import ARCH_REGISTRY2 unmodified lines}def set_realesrgan():if not torch.cuda.is_available(): # CPUimport warningswarnings.warn('The unoptimized RealESRGAN is slow on CPU. We do not use it. ''If you really want to use it, please modify the corresponding codes.',category=RuntimeWarning)bg_upsampler = Noneelse:from basicsr.archs.rrdbnet_arch import RRDBNetfrom basicsr.utils.realesrgan_utils import RealESRGANermodel = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2)bg_upsampler = RealESRGANer(scale=2,model_path='https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth',model=model,tile=args.bg_tile,tile_pad=40,pre_pad=0,half=True) # need to set False in CPU modereturn bg_upsamplerif __name__ == '__main__':device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')parser = argparse.ArgumentParser()parser.add_argument('--w', type=float, default=0.5, help='Balance the quality and fidelity')parser.add_argument('--upscale', type=int, default=2, help='The final upsampling scale of the image. Default: 2')parser.add_argument('--test_path', type=str, default='./inputs/cropped_faces')parser.add_argument('--has_aligned', action='store_true', help='Input are cropped and aligned faces')parser.add_argument('--only_center_face', action='store_true', help='Only restore the center face')# large det_model: 'YOLOv5l', 'retinaface_resnet50'# small det_model: 'YOLOv5n', 'retinaface_mobile0.25'parser.add_argument('--detection_model', type=str, default='retinaface_resnet50')parser.add_argument('--draw_box', action='store_true')parser.add_argument('--bg_upsampler', type=str, default='None', help='background upsampler. Optional: realesrgan')parser.add_argument('--face_upsample', action='store_true', help='face upsampler after enhancement.')parser.add_argument('--bg_tile', type=int, default=400, help='Tile size for background sampler. Default: 400')args = parser.parse_args()# ------------------------ input & output ------------------------if args.test_path.endswith('/'): # solve when path ends with /args.test_path = args.test_path[:-1]w = args.wresult_root = f'results/{os.path.basename(args.test_path)}_{w}'# ------------------ set up background upsampler ------------------if args.bg_upsampler == 'realesrgan':41 unmodified linesdevice=device)# -------------------- start to processing ---------------------# scan all the jpg and png imagesfor img_path in sorted(glob.glob(os.path.join(args.test_path, '*.[jp][pn]g'))):# clean all the intermediate results to process the next imageface_helper.clean_all()img_name = os.path.basename(img_path)print(f'Processing: {img_name}')basename, ext = os.path.splitext(img_name)img = cv2.imread(img_path, cv2.IMREAD_COLOR)if args.has_aligned:# the input faces are already cropped and alignedimg = cv2.resize(img, (512, 512), interpolation=cv2.INTER_LINEAR)face_helper.cropped_faces = [img]else:face_helper.read_image(img)22 unmodified linesrestored_face = tensor2img(cropped_face_t, rgb2bgr=True, min_max=(-1, 1))restored_face = restored_face.astype('uint8')face_helper.add_restored_face(restored_face)# paste_backif not args.has_aligned:21 unmodified linessave_face_name = f'{basename}.png'else:save_face_name = f'{basename}_{idx:02d}.png'save_restore_path = os.path.join(result_root, 'restored_faces', save_face_name)imwrite(restored_face, save_restore_path)# save restored imgif not args.has_aligned and restored_img is not None:save_restore_path = os.path.join(result_root, 'final_results', f'{basename}.png')imwrite(restored_img, save_restore_path)print(f'\nAll results are saved in {result_root}')1232 unmodified lines678910111213142 unmodified lines171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912041 unmodified lines16216316416516616716816917017117217317417517617717817918018118218318418518618718822 unmodified lines21121221321421521621721 unmodified lines239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274import osimport cv2import argparse2 unmodified linesfrom torchvision.transforms.functional import normalizefrom basicsr.utils import imwrite, img2tensor, tensor2imgfrom basicsr.utils.download_util import load_file_from_urlfrom basicsr.utils.misc import gpu_is_available, get_devicefrom facelib.utils.face_restoration_helper import FaceRestoreHelperfrom facelib.utils.misc import is_grayfrom basicsr.utils.registry import ARCH_REGISTRY2 unmodified lines}def set_realesrgan():from basicsr.archs.rrdbnet_arch import RRDBNetfrom basicsr.utils.realesrgan_utils import RealESRGANeruse_half = Falseif torch.cuda.is_available(): # set False in CPU/MPS modeno_half_gpu_list = ['1650', '1660'] # set False for GPUs that don't support f16if not True in [gpu in torch.cuda.get_device_name(0) for gpu in no_half_gpu_list]:use_half = Truemodel = RRDBNet(num_in_ch=3,num_out_ch=3,num_feat=64,num_block=23,num_grow_ch=32,scale=2,)upsampler = RealESRGANer(scale=2,model_path="https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/RealESRGAN_x2plus.pth",model=model,tile=args.bg_tile,tile_pad=40,pre_pad=0,half=use_half)if not gpu_is_available(): # CPUimport warningswarnings.warn('Running on CPU now! Make sure your PyTorch version matches your CUDA.''The unoptimized RealESRGAN is slow on CPU. ''If you want to disable it, please remove `--bg_upsampler` and `--face_upsample` in command.',category=RuntimeWarning)return upsamplerif __name__ == '__main__':# device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')device = get_device()parser = argparse.ArgumentParser()parser.add_argument('-i', '--input_path', type=str, default='./inputs/whole_imgs',help='Input image, video or folder. Default: inputs/whole_imgs')parser.add_argument('-o', '--output_path', type=str, default=None,help='Output folder. Default: results/<input_name>_<w>')parser.add_argument('-w', '--fidelity_weight', type=float, default=0.5,help='Balance the quality and fidelity. Default: 0.5')parser.add_argument('-s', '--upscale', type=int, default=2,help='The final upsampling scale of the image. Default: 2')parser.add_argument('--has_aligned', action='store_true', help='Input are cropped and aligned faces. Default: False')parser.add_argument('--only_center_face', action='store_true', help='Only restore the center face. Default: False')parser.add_argument('--draw_box', action='store_true', help='Draw the bounding box for the detected faces. Default: False')# large det_model: 'YOLOv5l', 'retinaface_resnet50'# small det_model: 'YOLOv5n', 'retinaface_mobile0.25'parser.add_argument('--detection_model', type=str, default='retinaface_resnet50',help='Face detector. Optional: retinaface_resnet50, retinaface_mobile0.25, YOLOv5l, YOLOv5n, dlib. \Default: retinaface_resnet50')parser.add_argument('--bg_upsampler', type=str, default='None', help='Background upsampler. Optional: realesrgan')parser.add_argument('--face_upsample', action='store_true', help='Face upsampler after enhancement. Default: False')parser.add_argument('--bg_tile', type=int, default=400, help='Tile size for background sampler. Default: 400')parser.add_argument('--suffix', type=str, default=None, help='Suffix of the restored faces. Default: None')parser.add_argument('--save_video_fps', type=float, default=None, help='Frame rate for saving video. Default: None')args = parser.parse_args()# ------------------------ input & output ------------------------w = args.fidelity_weightinput_video = Falseif args.input_path.endswith(('jpg', 'jpeg', 'png', 'JPG', 'JPEG', 'PNG')): # input single img pathinput_img_list = [args.input_path]result_root = f'results/test_img_{w}'elif args.input_path.endswith(('mp4', 'mov', 'avi', 'MP4', 'MOV', 'AVI')): # input video pathfrom basicsr.utils.video_util import VideoReader, VideoWriterinput_img_list = []vidreader = VideoReader(args.input_path)image = vidreader.get_frame()while image is not None:input_img_list.append(image)image = vidreader.get_frame()audio = vidreader.get_audio()fps = vidreader.get_fps() if args.save_video_fps is None else args.save_video_fpsvideo_name = os.path.basename(args.input_path)[:-4]result_root = f'results/{video_name}_{w}'input_video = Truevidreader.close()else: # input img folderif args.input_path.endswith('/'): # solve when path ends with /args.input_path = args.input_path[:-1]# scan all the jpg and png imagesinput_img_list = sorted(glob.glob(os.path.join(args.input_path, '*.[jpJP][pnPN]*[gG]')))result_root = f'results/{os.path.basename(args.input_path)}_{w}'if not args.output_path is None: # set output pathresult_root = args.output_pathtest_img_num = len(input_img_list)if test_img_num == 0:raise FileNotFoundError('No input image/video is found...\n''\tNote that --input_path for video should end with .mp4|.mov|.avi')# ------------------ set up background upsampler ------------------if args.bg_upsampler == 'realesrgan':41 unmodified linesdevice=device)# -------------------- start to processing ---------------------for i, img_path in enumerate(input_img_list):# clean all the intermediate results to process the next imageface_helper.clean_all()if isinstance(img_path, str):img_name = os.path.basename(img_path)basename, ext = os.path.splitext(img_name)print(f'[{i+1}/{test_img_num}] Processing: {img_name}')img = cv2.imread(img_path, cv2.IMREAD_COLOR)else: # for video processingbasename = str(i).zfill(6)img_name = f'{video_name}_{basename}' if input_video else basenameprint(f'[{i+1}/{test_img_num}] Processing: {img_name}')img = img_pathif args.has_aligned:# the input faces are already cropped and alignedimg = cv2.resize(img, (512, 512), interpolation=cv2.INTER_LINEAR)face_helper.is_gray = is_gray(img, threshold=10)if face_helper.is_gray:print('Grayscale input: True')face_helper.cropped_faces = [img]else:face_helper.read_image(img)22 unmodified linesrestored_face = tensor2img(cropped_face_t, rgb2bgr=True, min_max=(-1, 1))restored_face = restored_face.astype('uint8')face_helper.add_restored_face(restored_face, cropped_face)# paste_backif not args.has_aligned:21 unmodified linessave_face_name = f'{basename}.png'else:save_face_name = f'{basename}_{idx:02d}.png'if args.suffix is not None:save_face_name = f'{save_face_name[:-4]}_{args.suffix}.png'save_restore_path = os.path.join(result_root, 'restored_faces', save_face_name)imwrite(restored_face, save_restore_path)# save restored imgif not args.has_aligned and restored_img is not None:if args.suffix is not None:basename = f'{basename}_{args.suffix}'save_restore_path = os.path.join(result_root, 'final_results', f'{basename}.png')imwrite(restored_img, save_restore_path)# save enhanced videoif input_video:print('Video Saving...')# load imagesvideo_frames = []img_list = sorted(glob.glob(os.path.join(result_root, 'final_results', '*.[jp][pn]g')))for img_path in img_list:img = cv2.imread(img_path)video_frames.append(img)# write images to videoheight, width = video_frames[0].shape[:2]if args.suffix is not None:video_name = f'{video_name}_{args.suffix}.png'save_restore_path = os.path.join(result_root, f'{video_name}.mp4')vidwriter = VideoWriter(save_restore_path, height, width, fps, audio)for f in video_frames:vidwriter.write_frame(f)vidwriter.close()print(f'\nAll results are saved in {result_root}')
8 unmodified lines91011128 unmodified linespinned: false---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference8 unmodified lines91011128 unmodified linespinned: false---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
15 unmodified lines161718192021222324141 unmodified lines1661671681691701711721731747 unmodified lines18218318418518618718818919019119219370 unmodified lines26426526626726826932 unmodified lines30230330430530615 unmodified linesfrom basicsr.utils import imwrite, img2tensor, tensor2imgfrom basicsr.utils.download_util import load_file_from_urlfrom facelib.utils.face_restoration_helper import FaceRestoreHelperfrom facelib.utils.misc import is_grayfrom basicsr.archs.rrdbnet_arch import RRDBNetfrom basicsr.utils.realesrgan_utils import RealESRGANerfrom basicsr.utils.registry import ARCH_REGISTRY141 unmodified lines# face restoration for each cropped facefor idx, cropped_face in enumerate(face_helper.cropped_faces):# prepare datacropped_face_t = img2tensor(cropped_face / 255.0, bgr2rgb=True, float32=True)normalize(cropped_face_t, (0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True)cropped_face_t = cropped_face_t.unsqueeze(0).to(device)7 unmodified linestorch.cuda.empty_cache()except RuntimeError as error:print(f"Failed inference for CodeFormer: {error}")restored_face = tensor2img(cropped_face_t, rgb2bgr=True, min_max=(-1, 1))restored_face = restored_face.astype("uint8")face_helper.add_restored_face(restored_face)# paste_backif not has_aligned:70 unmodified linestd {padding-right: 0px !important;}</style><table>32 unmodified lines)DEBUG = os.getenv('DEBUG') == '1'demo.launch(debug=DEBUG)# demo.launch(debug=DEBUG, share=True)15 unmodified lines161718192021222324141 unmodified lines1661671681691701711727 unmodified lines18018118218318418518618718818970 unmodified lines26026126226326426526626726826927027132 unmodified lines30430530630730815 unmodified linesfrom basicsr.utils import imwrite, img2tensor, tensor2imgfrom basicsr.utils.download_util import load_file_from_urlfrom facelib.utils.face_restoration_helper import FaceRestoreHelperfrom basicsr.archs.rrdbnet_arch import RRDBNetfrom basicsr.utils.realesrgan_utils import RealESRGANerfrom facelib.utils.misc import is_grayfrom basicsr.utils.registry import ARCH_REGISTRY141 unmodified lines# face restoration for each cropped facefor idx, cropped_face in enumerate(face_helper.cropped_faces):# prepare datacropped_face_t = img2tensor(cropped_face / 255., bgr2rgb=True, float32=True)normalize(cropped_face_t, (0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True)cropped_face_t = cropped_face_t.unsqueeze(0).to(device)7 unmodified linestorch.cuda.empty_cache()except RuntimeError as error:print(f"Failed inference for CodeFormer: {error}")restored_face = tensor2img(cropped_face_t, rgb2bgr=True, min_max=(-1, 1))restored_face = restored_face.astype("uint8")face_helper.add_restored_face(restored_face, cropped_face)# paste_backif not has_aligned:70 unmodified linestd {padding-right: 0px !important;}.gradio-container-4-37-2 .prose table, .gradio-container-4-37-2 .prose tr, .gradio-container-4-37-2 .prose td, .gradio-container-4-37-2 .prose th {border: 0px solid #ffffff;border-bottom: 0px solid #ffffff;}</style><table>32 unmodified lines)DEBUG = os.getenv('DEBUG') == '1'# demo.launch(debug=DEBUG)demo.launch(debug=DEBUG, share=True)
CodeFormer/.gitignore → .gitignoreRenamed+5-4
CodeFormer/basicsr/utils/misc.py+25-2
CodeFormer/basicsr/version.py+2-2
CodeFormer/facelib/utils/face_restoration_helper.py+77-12
CodeFormer/facelib/utils/misc.py+32-4
CodeFormer/inference_codeformer.py+126-41
README.md+1-1
app.py+12-10