def xyxy_to_xywh(xyxy): """" Calculates the relative bounding box from absolute pixel values. """ bbox_left = min([xyxy[0], xyxy[2]]) bbox_top = min([xyxy[1], xyxy[3]]) bbox_w = abs(xyxy[0] - xyxy[2]) bbox_h = abs(xyxy[1] - xyxy[3]) x_c = (bbox_left + bbox_w / 2) y_c = (bbox_top + bbox_h / 2) w = bbox_w h = bbox_h return [x_c, y_c, w, h] def tlwh_to_xyxy(tlwh): """" Convert tlwh to xyxy """ x1 = tlwh[0] y1 = tlwh[1] x2 = tlwh[2] + x1 y2 = tlwh[3] + y1 return [x1, y1, x2, y2] def xyxy_to_tlwh(bbox_xyxy): tlwh_bboxs = [] for i, box in enumerate(bbox_xyxy): x1, y1, x2, y2 = [int(i) for i in box] top = x1 left = y1 w = int(x2 - x1) h = int(y2 - y1) tlwh_obj = [top, left, w, h] tlwh_bboxs.append(tlwh_obj) return tlwh_bboxs