CoolFace
Apppublic

cymic/Waifu_Diffusion_Webui

sourceHugging Faceupdated 4y agoView on Hugging Face
1likes
gfpgan_model.py116 linesDownload Raw Back to modules
1import os2import sys3import traceback4 5import facexlib6import gfpgan7 8import modules.face_restoration9from modules import shared, devices, modelloader10from modules.paths import models_path11 12model_dir = "GFPGAN"13user_path = None14model_path = os.path.join(models_path, model_dir)15model_url = "https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth"16have_gfpgan = False17loaded_gfpgan_model = None18 19 20def gfpgann():21    global loaded_gfpgan_model22    global model_path23    if loaded_gfpgan_model is not None:24        loaded_gfpgan_model.gfpgan.to(devices.device_gfpgan)25        return loaded_gfpgan_model26 27    if gfpgan_constructor is None:28        return None29 30    models = modelloader.load_models(model_path, model_url, user_path, ext_filter="GFPGAN")31    if len(models) == 1 and "http" in models[0]:32        model_file = models[0]33    elif len(models) != 0:34        latest_file = max(models, key=os.path.getctime)35        model_file = latest_file36    else:37        print("Unable to load gfpgan model!")38        return None39    model = gfpgan_constructor(model_path=model_file, upscale=1, arch='clean', channel_multiplier=2, bg_upsampler=None)40    loaded_gfpgan_model = model41 42    return model43 44 45def send_model_to(model, device):46    model.gfpgan.to(device)47    model.face_helper.face_det.to(device)48    model.face_helper.face_parse.to(device)49 50 51def gfpgan_fix_faces(np_image):52    model = gfpgann()53    if model is None:54        return np_image55 56    send_model_to(model, devices.device_gfpgan)57 58    np_image_bgr = np_image[:, :, ::-1]59    cropped_faces, restored_faces, gfpgan_output_bgr = model.enhance(np_image_bgr, has_aligned=False, only_center_face=False, paste_back=True)60    np_image = gfpgan_output_bgr[:, :, ::-1]61 62    model.face_helper.clean_all()63 64    if shared.opts.face_restoration_unload:65        send_model_to(model, devices.cpu)66 67    return np_image68 69 70gfpgan_constructor = None71 72 73def setup_model(dirname):74    global model_path75    if not os.path.exists(model_path):76        os.makedirs(model_path)77 78    try:79        from gfpgan import GFPGANer80        from facexlib import detection, parsing81        global user_path82        global have_gfpgan83        global gfpgan_constructor84 85        load_file_from_url_orig = gfpgan.utils.load_file_from_url86        facex_load_file_from_url_orig = facexlib.detection.load_file_from_url87        facex_load_file_from_url_orig2 = facexlib.parsing.load_file_from_url88 89        def my_load_file_from_url(**kwargs):90            return load_file_from_url_orig(**dict(kwargs, model_dir=model_path))91 92        def facex_load_file_from_url(**kwargs):93            return facex_load_file_from_url_orig(**dict(kwargs, save_dir=model_path, model_dir=None))94 95        def facex_load_file_from_url2(**kwargs):96            return facex_load_file_from_url_orig2(**dict(kwargs, save_dir=model_path, model_dir=None))97 98        gfpgan.utils.load_file_from_url = my_load_file_from_url99        facexlib.detection.load_file_from_url = facex_load_file_from_url100        facexlib.parsing.load_file_from_url = facex_load_file_from_url2101        user_path = dirname102        have_gfpgan = True103        gfpgan_constructor = GFPGANer104 105        class FaceRestorerGFPGAN(modules.face_restoration.FaceRestoration):106            def name(self):107                return "GFPGAN"108 109            def restore(self, np_image):110                return gfpgan_fix_faces(np_image)111 112        shared.face_restorers.append(FaceRestorerGFPGAN())113    except Exception:114        print("Error setting up GFPGAN:", file=sys.stderr)115        print(traceback.format_exc(), file=sys.stderr)116