2022-09-03 09:08:45 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import traceback
|
2022-09-23 07:26:00 +00:00
|
|
|
from glob import glob
|
2022-09-03 09:08:45 +00:00
|
|
|
|
2022-09-12 15:40:06 +00:00
|
|
|
from modules import shared, devices
|
2022-09-03 09:08:45 +00:00
|
|
|
from modules.shared import cmd_opts
|
2022-09-07 09:32:28 +00:00
|
|
|
from modules.paths import script_path
|
|
|
|
import modules.face_restoration
|
2022-09-26 14:29:50 +00:00
|
|
|
from modules import shared, devices, modelloader
|
|
|
|
from modules.paths import models_path
|
2022-09-03 09:08:45 +00:00
|
|
|
|
2022-09-26 14:29:50 +00:00
|
|
|
model_dir = "GFPGAN"
|
|
|
|
cmd_dir = None
|
|
|
|
model_path = os.path.join(models_path, model_dir)
|
|
|
|
model_url = "https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth"
|
2022-09-03 09:08:45 +00:00
|
|
|
|
|
|
|
loaded_gfpgan_model = None
|
|
|
|
|
|
|
|
|
|
|
|
def gfpgan():
|
|
|
|
global loaded_gfpgan_model
|
2022-09-26 14:29:50 +00:00
|
|
|
global model_path
|
2022-09-03 14:28:30 +00:00
|
|
|
if loaded_gfpgan_model is not None:
|
2022-09-12 15:40:06 +00:00
|
|
|
loaded_gfpgan_model.gfpgan.to(shared.device)
|
2022-09-03 14:28:30 +00:00
|
|
|
return loaded_gfpgan_model
|
2022-09-03 09:08:45 +00:00
|
|
|
|
2022-09-03 14:28:30 +00:00
|
|
|
if gfpgan_constructor is None:
|
|
|
|
return None
|
|
|
|
|
2022-09-26 14:29:50 +00:00
|
|
|
models = modelloader.load_models(model_path, model_url, cmd_dir)
|
|
|
|
if len(models) != 0:
|
|
|
|
latest_file = max(models, key=os.path.getctime)
|
|
|
|
model_file = latest_file
|
|
|
|
else:
|
|
|
|
print("Unable to load gfpgan model!")
|
|
|
|
return None
|
|
|
|
model = gfpgan_constructor(model_path=model_file, model_dir=model_path, upscale=1, arch='clean', channel_multiplier=2,
|
|
|
|
bg_upsampler=None)
|
2022-09-12 15:40:06 +00:00
|
|
|
model.gfpgan.to(shared.device)
|
|
|
|
loaded_gfpgan_model = model
|
2022-09-03 14:28:30 +00:00
|
|
|
|
|
|
|
return model
|
2022-09-03 09:08:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
def gfpgan_fix_faces(np_image):
|
2022-09-12 15:40:06 +00:00
|
|
|
model = gfpgan()
|
2022-09-26 14:29:50 +00:00
|
|
|
if model is None:
|
|
|
|
return np_image
|
2022-09-03 09:08:45 +00:00
|
|
|
np_image_bgr = np_image[:, :, ::-1]
|
2022-09-12 15:40:06 +00:00
|
|
|
cropped_faces, restored_faces, gfpgan_output_bgr = model.enhance(np_image_bgr, has_aligned=False, only_center_face=False, paste_back=True)
|
2022-09-03 09:08:45 +00:00
|
|
|
np_image = gfpgan_output_bgr[:, :, ::-1]
|
|
|
|
|
2022-09-12 15:40:06 +00:00
|
|
|
if shared.opts.face_restoration_unload:
|
|
|
|
model.gfpgan.to(devices.cpu)
|
|
|
|
|
2022-09-03 09:08:45 +00:00
|
|
|
return np_image
|
|
|
|
|
|
|
|
|
|
|
|
have_gfpgan = False
|
|
|
|
gfpgan_constructor = None
|
|
|
|
|
|
|
|
|
2022-09-26 14:29:50 +00:00
|
|
|
def setup_model(dirname):
|
|
|
|
global model_path
|
|
|
|
if not os.path.exists(model_path):
|
|
|
|
os.makedirs(model_path)
|
2022-09-03 09:08:45 +00:00
|
|
|
|
2022-09-26 14:29:50 +00:00
|
|
|
try:
|
|
|
|
from modules.gfpgan_model_arch import GFPGANerr
|
|
|
|
global cmd_dir
|
2022-09-03 09:08:45 +00:00
|
|
|
global have_gfpgan
|
|
|
|
global gfpgan_constructor
|
2022-09-26 14:29:50 +00:00
|
|
|
|
|
|
|
cmd_dir = dirname
|
|
|
|
have_gfpgan = True
|
|
|
|
gfpgan_constructor = GFPGANerr
|
2022-09-07 09:32:28 +00:00
|
|
|
|
|
|
|
class FaceRestorerGFPGAN(modules.face_restoration.FaceRestoration):
|
|
|
|
def name(self):
|
|
|
|
return "GFPGAN"
|
|
|
|
|
|
|
|
def restore(self, np_image):
|
|
|
|
np_image_bgr = np_image[:, :, ::-1]
|
|
|
|
cropped_faces, restored_faces, gfpgan_output_bgr = gfpgan().enhance(np_image_bgr, has_aligned=False, only_center_face=False, paste_back=True)
|
|
|
|
np_image = gfpgan_output_bgr[:, :, ::-1]
|
|
|
|
|
|
|
|
return np_image
|
|
|
|
|
|
|
|
shared.face_restorers.append(FaceRestorerGFPGAN())
|
2022-09-03 09:08:45 +00:00
|
|
|
except Exception:
|
|
|
|
print("Error setting up GFPGAN:", file=sys.stderr)
|
|
|
|
print(traceback.format_exc(), file=sys.stderr)
|