convert_points_and_boxes.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. __all__ = ["convert_points_to_boxes"]
  15. import numpy as np
  16. def convert_points_to_boxes(dt_polys: list) -> np.ndarray:
  17. """
  18. Converts a list of polygons to a numpy array of bounding boxes.
  19. Args:
  20. dt_polys (list): A list of polygons, where each polygon is represented
  21. as a list of (x, y) points.
  22. Returns:
  23. np.ndarray: A numpy array of bounding boxes, where each box is represented
  24. as [left, top, right, bottom].
  25. If the input list is empty, returns an empty numpy array.
  26. """
  27. if len(dt_polys) > 0:
  28. dt_polys_tmp = dt_polys.copy()
  29. dt_polys_tmp = np.array(dt_polys_tmp)
  30. boxes_left = np.min(dt_polys_tmp[:, :, 0], axis=1)
  31. boxes_right = np.max(dt_polys_tmp[:, :, 0], axis=1)
  32. boxes_top = np.min(dt_polys_tmp[:, :, 1], axis=1)
  33. boxes_bottom = np.max(dt_polys_tmp[:, :, 1], axis=1)
  34. dt_boxes = np.array([boxes_left, boxes_top, boxes_right, boxes_bottom])
  35. dt_boxes = dt_boxes.T
  36. else:
  37. dt_boxes = np.array([])
  38. return dt_boxes