Spaces:
Sleeping
Sleeping
File size: 893 Bytes
f996a9b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 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
|