img.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  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 os
  15. from paddleseg.utils import logger
  16. from tools.model import get_model
  17. import cv2
  18. import numpy as np
  19. from ppmatting.core import predict
  20. from ppmatting.utils import get_image_list
  21. current_path = os.path.abspath(os.path.dirname(__file__))
  22. def get_rel_path(path: str):
  23. return "{}/../{}".format(current_path, path)
  24. def seg(img_path: str, save_dir: str):
  25. image_list, image_dir = get_image_list(img_path)
  26. logger.info('Number of predict images = {}'.format(len(image_list)))
  27. model = get_model()
  28. return predict(
  29. model=model.model,
  30. model_path=model.path,
  31. transforms=model.transforms,
  32. image_list=image_list,
  33. image_dir=image_dir,
  34. trimap_list=None,
  35. save_dir=save_dir,
  36. fg_estimate=True)
  37. def replace(img_path: str, save_dir: str, background: str = None, width: int = None, height: int = None):
  38. logger.info("replace: {}, {},{},{},{}".format(img_path, save_dir, background, width, height))
  39. image_list, image_dir = get_image_list(img_path)
  40. model = get_model()
  41. alpha, fg, _, p = predict(
  42. model=model.model,
  43. model_path=model.path,
  44. transforms=model.transforms,
  45. image_list=image_list,
  46. trimap_list=None,
  47. save_dir=save_dir,
  48. fg_estimate=False)
  49. if background is None:
  50. return p
  51. img_ori = cv2.imread(img_path)
  52. bg = get_bg(background, img_ori.shape)
  53. if bg is None:
  54. return p
  55. alpha = alpha / 255.0
  56. alpha = alpha[:, :, np.newaxis]
  57. com = alpha * fg + (1 - alpha) * bg
  58. com = com.astype('uint8')
  59. com_save_path = os.path.join(save_dir, os.path.basename(img_path))
  60. cv2.imwrite(com_save_path, com)
  61. return com_save_path
  62. def get_bg(background, img_shape):
  63. # 1、纯色
  64. # 2、通道颜色
  65. # 3、图片
  66. bg = np.zeros(img_shape)
  67. if background is None:
  68. return None
  69. if os.path.exists(background):
  70. bg = cv2.imread(background)
  71. bg = cv2.resize(bg, (img_shape[1], img_shape[0]))
  72. elif background == 'r':
  73. bg[:, :, 2] = 255
  74. elif background == 'g':
  75. bg[:, :, 1] = 255
  76. elif background == 'b':
  77. bg[:, :, 0] = 255
  78. elif background == 'w':
  79. bg[:, :, :] = 255
  80. elif is_color_hex(background):
  81. r, g, b, _ = hex_to_rgb(background)
  82. bg[:, :, 2] = r
  83. bg[:, :, 1] = g
  84. bg[:, :, 0] = b
  85. else:
  86. return None
  87. return bg
  88. def is_color_hex(color: str):
  89. size = len(color)
  90. if color.startswith("#"):
  91. return size == 7 or size == 9
  92. return False
  93. def hex_to_rgb(color: str):
  94. if color.startswith("#"):
  95. color = color[1:len(color)]
  96. r = int(color[0:2], 16)
  97. g = int(color[2:4], 16)
  98. b = int(color[4:6], 16)
  99. a = 100
  100. if len(color) == 8:
  101. a = int(color[6:8], 16)
  102. return r, g, b, a