face_enhance.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import paddle
  15. import paddle.nn as nn
  16. import math
  17. import cv2
  18. import numpy as np
  19. from ppgan.utils.download import get_path_from_url
  20. from ppgan.models.generators import GPEN
  21. from ppgan.faceutils.face_detection.detection.blazeface.utils import *
  22. GPEN_weights = 'https://paddlegan.bj.bcebos.com/models/GPEN-512.pdparams'
  23. class FaceEnhancement(object):
  24. def __init__(self, path_to_enhance=None, size=512, batch_size=1):
  25. super(FaceEnhancement, self).__init__()
  26. # Initialise the face detector
  27. if path_to_enhance is None:
  28. model_weights_path = get_path_from_url(GPEN_weights)
  29. model_weights = paddle.load(model_weights_path)
  30. else:
  31. model_weights = paddle.load(path_to_enhance)
  32. self.face_enhance = GPEN(size=512, style_dim=512, n_mlp=8)
  33. self.face_enhance.load_dict(model_weights)
  34. self.face_enhance.eval()
  35. self.size = size
  36. self.mask = np.zeros((512, 512), np.float32)
  37. cv2.rectangle(self.mask, (26, 26), (486, 486), (1, 1, 1), -1,
  38. cv2.LINE_AA)
  39. self.mask = cv2.GaussianBlur(self.mask, (101, 101), 11)
  40. self.mask = cv2.GaussianBlur(self.mask, (101, 101), 11)
  41. self.mask = paddle.tile(
  42. paddle.to_tensor(self.mask).unsqueeze(0).unsqueeze(-1),
  43. repeat_times=[batch_size, 1, 1, 3]).numpy()
  44. def enhance_from_image(self, img):
  45. if isinstance(img, np.ndarray):
  46. img, _ = resize_and_crop_image(img, 512)
  47. img = paddle.to_tensor(img).transpose([2, 0, 1])
  48. else:
  49. assert img.shape == [3, 512, 512]
  50. return self.enhance_from_batch(img.unsqueeze(0))[0]
  51. def enhance_from_batch(self, img):
  52. if isinstance(img, np.ndarray):
  53. img_ori, _ = resize_and_crop_batch(img, 512)
  54. img = paddle.to_tensor(img_ori).transpose([0, 3, 1, 2])
  55. else:
  56. assert img.shape[1:] == [3, 512, 512]
  57. img_ori = img.transpose([0, 2, 3, 1]).numpy()
  58. img_t = (img / 255. - 0.5) / 0.5
  59. with paddle.no_grad():
  60. out, __ = self.face_enhance(img_t)
  61. image_tensor = out * 0.5 + 0.5
  62. image_tensor = image_tensor.transpose([0, 2, 3, 1]) # RGB
  63. image_numpy = paddle.clip(image_tensor, 0, 1) * 255.0
  64. out = image_numpy.astype(np.uint8).cpu().numpy()
  65. return out * self.mask + (1 - self.mask) * img_ori