import math import numpy as np def calculate_angle(box): """ Calculate the rotation angle of a bounding box. `box` is a list of 4 points: [[x1, y1], [x2, y2], [x3, y3], [x4, y4]] typically representing [top-left, top-right, bottom-right, bottom-left]. """ pt1, pt2 = box[0], box[1] dx = pt2[0] - pt1[0] dy = pt2[1] - pt1[1] angle_rad = math.atan2(dy, dx) angle_deg = math.degrees(angle_rad) return angle_deg def get_bounding_box_dimensions(box): """ Calculate width and height of the rotated bounding box. """ pt1, pt2, pt3, pt4 = box width = math.dist(pt1, pt2) height = math.dist(pt2, pt3) return width, height def get_center(box): """ Calculate the center of the bounding box. """ pts = np.array(box) center_x = np.mean(pts[:, 0]) center_y = np.mean(pts[:, 1]) return center_x, center_y