processors.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # Copyright (c) 2024 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 numpy as np
  15. from ....utils.deps import class_requires_deps, is_dep_available
  16. from ...utils.benchmark import benchmark
  17. if is_dep_available("scikit-image"):
  18. from skimage import morphology
  19. @benchmark.timeit
  20. @class_requires_deps("scikit-image")
  21. class MapToMask:
  22. """Map_to_mask"""
  23. def __init__(self):
  24. """
  25. Initialize the instance.
  26. """
  27. super().__init__()
  28. def __call__(self, preds, *args):
  29. """apply"""
  30. return [self.apply(pred) for pred in preds]
  31. def apply(
  32. self,
  33. pred,
  34. ):
  35. """apply"""
  36. score_map = pred[0]
  37. thred = 0.01
  38. mask = score_map[0]
  39. mask[mask > thred] = 255
  40. mask[mask <= thred] = 0
  41. kernel = morphology.disk(4)
  42. mask = morphology.opening(mask, kernel)
  43. mask = mask.astype(np.uint8)
  44. return mask[None, :, :]