2023-12-25 21:01:02 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import logging
|
2022-09-03 09:08:45 +00:00
|
|
|
import os
|
|
|
|
|
2023-12-25 21:01:02 +00:00
|
|
|
from modules import (
|
|
|
|
devices,
|
|
|
|
errors,
|
|
|
|
face_restoration,
|
|
|
|
face_restoration_utils,
|
|
|
|
modelloader,
|
|
|
|
shared,
|
|
|
|
)
|
2022-09-03 09:08:45 +00:00
|
|
|
|
2023-12-25 21:01:02 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2022-09-26 14:29:50 +00:00
|
|
|
model_url = "https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth"
|
2023-12-25 21:01:02 +00:00
|
|
|
model_download_name = "GFPGANv1.4.pth"
|
|
|
|
gfpgan_face_restorer: face_restoration.FaceRestoration | None = None
|
|
|
|
|
|
|
|
|
|
|
|
class FaceRestorerGFPGAN(face_restoration_utils.CommonFaceRestoration):
|
|
|
|
def name(self):
|
|
|
|
return "GFPGAN"
|
|
|
|
|
|
|
|
def get_device(self):
|
|
|
|
return devices.device_gfpgan
|
|
|
|
|
|
|
|
def load_net(self) -> None:
|
|
|
|
for model_path in modelloader.load_models(
|
|
|
|
model_path=self.model_path,
|
|
|
|
model_url=model_url,
|
|
|
|
command_path=self.model_path,
|
|
|
|
download_name=model_download_name,
|
|
|
|
ext_filter=['.pth'],
|
|
|
|
):
|
|
|
|
if 'GFPGAN' in os.path.basename(model_path):
|
|
|
|
net = modelloader.load_spandrel_model(
|
|
|
|
model_path,
|
|
|
|
device=self.get_device(),
|
2023-12-30 14:37:03 +00:00
|
|
|
expected_architecture='GFPGAN',
|
2023-12-25 21:01:02 +00:00
|
|
|
).model
|
|
|
|
net.different_w = True # see https://github.com/chaiNNer-org/spandrel/pull/81
|
|
|
|
return net
|
|
|
|
raise ValueError("No GFPGAN model found")
|
|
|
|
|
|
|
|
def restore(self, np_image):
|
|
|
|
def restore_face(cropped_face_t):
|
|
|
|
assert self.net is not None
|
|
|
|
return self.net(cropped_face_t, return_rgb=False)[0]
|
|
|
|
|
|
|
|
return self.restore_with_helper(np_image, restore_face)
|
2022-10-04 09:32:22 +00:00
|
|
|
|
|
|
|
|
2022-09-03 09:08:45 +00:00
|
|
|
def gfpgan_fix_faces(np_image):
|
2023-12-25 21:01:02 +00:00
|
|
|
if gfpgan_face_restorer:
|
|
|
|
return gfpgan_face_restorer.restore(np_image)
|
|
|
|
logger.warning("GFPGAN face restorer not set up")
|
2022-09-03 09:08:45 +00:00
|
|
|
return np_image
|
|
|
|
|
|
|
|
|
2023-12-25 21:01:02 +00:00
|
|
|
def setup_model(dirname: str) -> None:
|
|
|
|
global gfpgan_face_restorer
|
2022-09-03 09:08:45 +00:00
|
|
|
|
2022-09-26 14:29:50 +00:00
|
|
|
try:
|
2023-12-25 21:01:02 +00:00
|
|
|
face_restoration_utils.patch_facexlib(dirname)
|
|
|
|
gfpgan_face_restorer = FaceRestorerGFPGAN(model_path=dirname)
|
|
|
|
shared.face_restorers.append(gfpgan_face_restorer)
|
2022-09-03 09:08:45 +00:00
|
|
|
except Exception:
|
2023-05-31 16:56:37 +00:00
|
|
|
errors.report("Error setting up GFPGAN", exc_info=True)
|