yolo, voc, coco bbox格式互转函数
yolo: [xmid, ymid, w, h],归一化到0-1
voc: [x1, y1, x2, y2]
coco: [xmin, ymin, w, h]
def voc2yolo(bboxes, image_height=720, image_width=1280):
"""
voc => [x1, y1, x2, y2]
yolo => [xmid, ymid, w, h] (normalized)
"""
bboxes = bboxes.copy().astype(float) # otherwise all value will be 0 as voc_pascal dtype is np.int
bboxes[..., [0, 2]] = bboxes[..., [0, 2]]/ image_width
bboxes[..., [1, 3]] = bboxes[..., [1, 3]]/ image_height
w = bboxes[..., 2] - bboxes[..., 0]
h = bboxes[..., 3] - bboxes[..., 1]
bboxes[..., 0] = bboxes[..., 0] + w/2
bboxes[..., 1] = bboxes[..., 1] + h/2
bboxes[..., 2] = w
bboxes[...,