|
import sys |
|
|
|
sys.path.append('./post_process/inswapper/CodeFormer/CodeFormer') |
|
|
|
import os |
|
import cv2 |
|
import torch |
|
import torch.nn.functional as F |
|
from torchvision.transforms.functional import normalize |
|
|
|
from basicsr.utils import imwrite, img2tensor, tensor2img |
|
from basicsr.utils.download_util import load_file_from_url |
|
from facelib.utils.face_restoration_helper import FaceRestoreHelper |
|
from facelib.utils.misc import is_gray |
|
from basicsr.archs.rrdbnet_arch import RRDBNet |
|
from basicsr.utils.realesrgan_utils import RealESRGANer |
|
|
|
|
|
|
|
def check_ckpts(): |
|
pretrain_model_url = { |
|
'codeformer': 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/codeformer.pth', |
|
'detection': 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/detection_Resnet50_Final.pth', |
|
'parsing': 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/parsing_parsenet.pth', |
|
'realesrgan': 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/RealESRGAN_x2plus.pth' |
|
} |
|
|
|
if not os.path.exists('CodeFormer/CodeFormer/weights/CodeFormer/codeformer.pth'): |
|
load_file_from_url(url=pretrain_model_url['codeformer'], model_dir='CodeFormer/CodeFormer/weights/CodeFormer', progress=True, file_name=None) |
|
if not os.path.exists('CodeFormer/CodeFormer/weights/facelib/detection_Resnet50_Final.pth'): |
|
load_file_from_url(url=pretrain_model_url['detection'], model_dir='CodeFormer/CodeFormer/weights/facelib', progress=True, file_name=None) |
|
if not os.path.exists('CodeFormer/CodeFormer/weights/facelib/parsing_parsenet.pth'): |
|
load_file_from_url(url=pretrain_model_url['parsing'], model_dir='CodeFormer/CodeFormer/weights/facelib', progress=True, file_name=None) |
|
if not os.path.exists('CodeFormer/CodeFormer/weights/realesrgan/RealESRGAN_x2plus.pth'): |
|
load_file_from_url(url=pretrain_model_url['realesrgan'], model_dir='CodeFormer/CodeFormer/weights/realesrgan', progress=True, file_name=None) |
|
|
|
|
|
|
|
def set_realesrgan(): |
|
half = True if torch.cuda.is_available() else False |
|
model = 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="CodeFormer/CodeFormer/weights/realesrgan/RealESRGAN_x2plus.pth", |
|
model=model, |
|
tile=400, |
|
tile_pad=40, |
|
pre_pad=0, |
|
half=half, |
|
) |
|
return upsampler |
|
|
|
|
|
def face_restoration(img, background_enhance, face_upsample, upscale, codeformer_fidelity, upsampler, codeformer_net, device): |
|
"""Run a single prediction on the model""" |
|
try: |
|
|
|
has_aligned = False |
|
only_center_face = False |
|
draw_box = False |
|
detection_model = "retinaface_resnet50" |
|
|
|
|
|
|
|
|
|
upscale = upscale if (upscale is not None and upscale > 0) else 2 |
|
|
|
upscale = int(upscale) |
|
if upscale > 4: |
|
upscale = 4 |
|
if upscale > 2 and max(img.shape[:2])>1000: |
|
upscale = 2 |
|
if max(img.shape[:2]) > 1500: |
|
upscale = 1 |
|
background_enhance = False |
|
face_upsample = False |
|
|
|
face_helper = FaceRestoreHelper( |
|
upscale, |
|
face_size=512, |
|
crop_ratio=(1, 1), |
|
det_model=detection_model, |
|
save_ext="png", |
|
use_parse=True, |
|
) |
|
bg_upsampler = upsampler if background_enhance else None |
|
face_upsampler = upsampler if face_upsample else None |
|
|
|
if has_aligned: |
|
|
|
img = cv2.resize(img, (512, 512), interpolation=cv2.INTER_LINEAR) |
|
face_helper.is_gray = is_gray(img, threshold=5) |
|
face_helper.cropped_faces = [img] |
|
else: |
|
face_helper.read_image(img) |
|
|
|
num_det_faces = face_helper.get_face_landmarks_5( |
|
only_center_face=only_center_face, resize=640, eye_dist_threshold=5 |
|
) |
|
|
|
face_helper.align_warp_face() |
|
|
|
|
|
for idx, cropped_face in enumerate(face_helper.cropped_faces): |
|
|
|
cropped_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) |
|
|
|
try: |
|
with torch.no_grad(): |
|
output = codeformer_net( |
|
cropped_face_t, w=codeformer_fidelity, adain=True |
|
)[0] |
|
restored_face = tensor2img(output, rgb2bgr=True, min_max=(-1, 1)) |
|
del output |
|
torch.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) |
|
|
|
|
|
if not has_aligned: |
|
|
|
if bg_upsampler is not None: |
|
|
|
bg_img = bg_upsampler.enhance(img, outscale=upscale)[0] |
|
else: |
|
bg_img = None |
|
face_helper.get_inverse_affine(None) |
|
|
|
if face_upsample and face_upsampler is not None: |
|
restored_img = face_helper.paste_faces_to_input_image( |
|
upsample_img=bg_img, |
|
draw_box=draw_box, |
|
face_upsampler=face_upsampler, |
|
) |
|
else: |
|
restored_img = face_helper.paste_faces_to_input_image( |
|
upsample_img=bg_img, draw_box=draw_box |
|
) |
|
|
|
restored_img = cv2.cvtColor(restored_img, cv2.COLOR_BGR2RGB) |
|
return restored_img |
|
except Exception as error: |
|
print('Global exception', error) |
|
return None, None |
|
|