# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. __all__ = ["convert_points_to_boxes"] import numpy as np def convert_points_to_boxes(dt_polys: list) -> np.ndarray: """ Converts a list of polygons to a numpy array of bounding boxes. Args: dt_polys (list): A list of polygons, where each polygon is represented as a list of (x, y) points. Returns: np.ndarray: A numpy array of bounding boxes, where each box is represented as [left, top, right, bottom]. If the input list is empty, returns an empty numpy array. """ if len(dt_polys) > 0: dt_polys_tmp = dt_polys.copy() dt_polys_tmp = np.array(dt_polys_tmp) boxes_left = np.min(dt_polys_tmp[:, :, 0], axis=1) boxes_right = np.max(dt_polys_tmp[:, :, 0], axis=1) boxes_top = np.min(dt_polys_tmp[:, :, 1], axis=1) boxes_bottom = np.max(dt_polys_tmp[:, :, 1], axis=1) dt_boxes = np.array([boxes_left, boxes_top, boxes_right, boxes_bottom]) dt_boxes = dt_boxes.T else: dt_boxes = np.array([]) return dt_boxes