convert_points_and_boxes.py 1.7 KB

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