processors.py 1.3 KB

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