gfpgan_model.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import os
  2. import facexlib
  3. import gfpgan
  4. import modules.face_restoration
  5. from modules import paths, shared, devices, modelloader, errors
  6. model_dir = "GFPGAN"
  7. user_path = None
  8. model_path = os.path.join(paths.models_path, model_dir)
  9. model_url = "https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth"
  10. have_gfpgan = False
  11. loaded_gfpgan_model = None
  12. def gfpgann():
  13. global loaded_gfpgan_model
  14. global model_path
  15. if loaded_gfpgan_model is not None:
  16. loaded_gfpgan_model.gfpgan.to(devices.device_gfpgan)
  17. return loaded_gfpgan_model
  18. if gfpgan_constructor is None:
  19. return None
  20. models = modelloader.load_models(model_path, model_url, user_path, ext_filter="GFPGAN")
  21. if len(models) == 1 and models[0].startswith("http"):
  22. model_file = models[0]
  23. elif len(models) != 0:
  24. latest_file = max(models, key=os.path.getctime)
  25. model_file = latest_file
  26. else:
  27. print("Unable to load gfpgan model!")
  28. return None
  29. if hasattr(facexlib.detection.retinaface, 'device'):
  30. facexlib.detection.retinaface.device = devices.device_gfpgan
  31. model = gfpgan_constructor(model_path=model_file, upscale=1, arch='clean', channel_multiplier=2, bg_upsampler=None, device=devices.device_gfpgan)
  32. loaded_gfpgan_model = model
  33. return model
  34. def send_model_to(model, device):
  35. model.gfpgan.to(device)
  36. model.face_helper.face_det.to(device)
  37. model.face_helper.face_parse.to(device)
  38. def gfpgan_fix_faces(np_image):
  39. model = gfpgann()
  40. if model is None:
  41. return np_image
  42. send_model_to(model, devices.device_gfpgan)
  43. np_image_bgr = np_image[:, :, ::-1]
  44. cropped_faces, restored_faces, gfpgan_output_bgr = model.enhance(np_image_bgr, has_aligned=False, only_center_face=False, paste_back=True)
  45. np_image = gfpgan_output_bgr[:, :, ::-1]
  46. model.face_helper.clean_all()
  47. if shared.opts.face_restoration_unload:
  48. send_model_to(model, devices.cpu)
  49. return np_image
  50. gfpgan_constructor = None
  51. def setup_model(dirname):
  52. try:
  53. os.makedirs(model_path, exist_ok=True)
  54. from gfpgan import GFPGANer
  55. from facexlib import detection, parsing # noqa: F401
  56. global user_path
  57. global have_gfpgan
  58. global gfpgan_constructor
  59. load_file_from_url_orig = gfpgan.utils.load_file_from_url
  60. facex_load_file_from_url_orig = facexlib.detection.load_file_from_url
  61. facex_load_file_from_url_orig2 = facexlib.parsing.load_file_from_url
  62. def my_load_file_from_url(**kwargs):
  63. return load_file_from_url_orig(**dict(kwargs, model_dir=model_path))
  64. def facex_load_file_from_url(**kwargs):
  65. return facex_load_file_from_url_orig(**dict(kwargs, save_dir=model_path, model_dir=None))
  66. def facex_load_file_from_url2(**kwargs):
  67. return facex_load_file_from_url_orig2(**dict(kwargs, save_dir=model_path, model_dir=None))
  68. gfpgan.utils.load_file_from_url = my_load_file_from_url
  69. facexlib.detection.load_file_from_url = facex_load_file_from_url
  70. facexlib.parsing.load_file_from_url = facex_load_file_from_url2
  71. user_path = dirname
  72. have_gfpgan = True
  73. gfpgan_constructor = GFPGANer
  74. class FaceRestorerGFPGAN(modules.face_restoration.FaceRestoration):
  75. def name(self):
  76. return "GFPGAN"
  77. def restore(self, np_image):
  78. return gfpgan_fix_faces(np_image)
  79. shared.face_restorers.append(FaceRestorerGFPGAN())
  80. except Exception:
  81. errors.report("Error setting up GFPGAN", exc_info=True)