img.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 ppmatting.core import predict
  17. from ppmatting.utils import get_image_list
  18. from tools.model import get_model
  19. def seg(img_path: str, save_dir: str):
  20. image_list, image_dir = get_image_list(img_path)
  21. logger.info('Number of predict images = {}'.format(len(image_list)))
  22. model = get_model()
  23. return predict(
  24. model=model.model,
  25. model_path=model.path,
  26. transforms=model.transforms,
  27. image_list=image_list,
  28. image_dir=image_dir,
  29. trimap_list=None,
  30. save_dir=save_dir,
  31. fg_estimate=True)
  32. def has_seg(img_path: str, save_dir: str):
  33. # 是否已经抠过图了
  34. paths = os.path.splitext(img_path)
  35. rgba = "{}_rgba{}".format(paths[0], paths[1])
  36. alpha = "{}_alpha{}".format(paths[0], paths[1])
  37. rgba_path = os.path.join(save_dir, rgba)
  38. alpha_path = os.path.join(save_dir, alpha)
  39. return os.path.exists(rgba_path) and os.path.exists(alpha_path), alpha_path, rgba_path