2023-12-25 14:01:02 -07:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import logging
|
2022-09-03 03:08:45 -06:00
|
|
|
import os
|
|
|
|
|
2023-12-30 15:09:51 -07:00
|
|
|
import torch
|
|
|
|
|
2023-12-25 14:01:02 -07:00
|
|
|
from modules import (
|
|
|
|
devices,
|
|
|
|
errors,
|
|
|
|
face_restoration,
|
|
|
|
face_restoration_utils,
|
|
|
|
modelloader,
|
|
|
|
shared,
|
|
|
|
)
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2023-12-25 14:01:02 -07:00
|
|
|
logger = logging.getLogger(__name__)
|
2022-09-26 08:29:50 -06:00
|
|
|
model_url = "https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth"
|
2023-12-25 14:01:02 -07: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
|
|
|
|
|
2023-12-30 15:09:51 -07:00
|
|
|
def load_net(self) -> torch.Module:
|
2023-12-25 14:01:02 -07:00
|
|
|
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):
|
2023-12-30 15:09:51 -07:00
|
|
|
model = modelloader.load_spandrel_model(
|
2023-12-25 14:01:02 -07:00
|
|
|
model_path,
|
|
|
|
device=self.get_device(),
|
2023-12-30 07:37:03 -07:00
|
|
|
expected_architecture='GFPGAN',
|
2023-12-25 14:01:02 -07:00
|
|
|
).model
|
2023-12-30 15:09:51 -07:00
|
|
|
model.different_w = True # see https://github.com/chaiNNer-org/spandrel/pull/81
|
|
|
|
return model
|
2023-12-25 14:01:02 -07:00
|
|
|
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 03:32:22 -06:00
|
|
|
|
|
|
|
|
2022-09-03 03:08:45 -06:00
|
|
|
def gfpgan_fix_faces(np_image):
|
2023-12-25 14:01:02 -07:00
|
|
|
if gfpgan_face_restorer:
|
|
|
|
return gfpgan_face_restorer.restore(np_image)
|
|
|
|
logger.warning("GFPGAN face restorer not set up")
|
2022-09-03 03:08:45 -06:00
|
|
|
return np_image
|
|
|
|
|
|
|
|
|
2023-12-25 14:01:02 -07:00
|
|
|
def setup_model(dirname: str) -> None:
|
|
|
|
global gfpgan_face_restorer
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2022-09-26 08:29:50 -06:00
|
|
|
try:
|
2023-12-25 14:01:02 -07:00
|
|
|
face_restoration_utils.patch_facexlib(dirname)
|
|
|
|
gfpgan_face_restorer = FaceRestorerGFPGAN(model_path=dirname)
|
|
|
|
shared.face_restorers.append(gfpgan_face_restorer)
|
2022-09-03 03:08:45 -06:00
|
|
|
except Exception:
|
2023-05-31 10:56:37 -06:00
|
|
|
errors.report("Error setting up GFPGAN", exc_info=True)
|