File size: 1,136 Bytes
a090915 |
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import numpy as np
def get_box_center(box):
"""
Calculate the center of a bounding box.
box: [x1, y1, x2, y2]
returns: [center_x, center_y]
"""
return [(box[0] + box[2]) / 2, (box[1] + box[3]) / 2]
def euclidean_distance(point1, point2):
"""
Compute Euclidean distance between two points.
point1, point2: [x, y]
returns: float
"""
return float(np.linalg.norm(np.array(point1) - np.array(point2)))
def relative_distance(box1, box2):
"""
Compute relative distance between two boxes.
Returns distance normalized by sqrt(average box area)
"""
center1 = get_box_center(box1)
center2 = get_box_center(box2)
distance = euclidean_distance(center1, center2)
area1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
area2 = (box2[2] - box2[0]) * (box2[3] - box2[1])
avg_area = (area1 + area2) / 2
return distance / (avg_area**0.5)
def relative_keypoints(pose1, pose2):
"""
Compute difference between keypoints of two people.
Returns a list of [dx, dy] for each keypoint.
"""
return (np.array(pose2) - np.array(pose1)).tolist()
|