def hw_to_center(a): """Convert (top left, width, height) bounding box to (center, width, height) bounding box. Args: param1 (arr): [xmin, ymin, width, height] where (xmin, ymin) represents the top left of the bounding box Returns: arr: [center_x, center_y, width, height] """ bbs = util.partition(a, 4) bbs = [[a[0] + a[2] / 2, a[1] + a[3] / 2, a[2], a[3]] for a in bbs] return np.concatenate(bbs)
def corners_to_center(a): """Convert (top left, bottom right) bounding box to (center, width, height) bounding box. Args: param1 (arr): [xmin, ymin, xmax, ymax] where (xmin, ymin) and (xmax, ymax) represent the top left and bottom right corners of the bounding box Returns: arr: [center_x, center_y, width, height] """ bbs = util.partition(a, 4) bbs = [[(a[0] + a[2]) / 2, (a[1] + a[3]) / 2, int(a[2] - a[0]), int(a[3] - a[1])] for a in bbs] return np.concatenate(bbs)
def center_to_hw(a): """Convert (center, width, height) bounding box to (top left, width, height) bounding box. Args: param1 (arr): [center_x, center_y, width, height] where (center_x, center_y) represents the center of the bounding box Returns: arr: [xmin, ymin, width, height] """ bbs = util.partition(a, 4) bbs = [[int(a[0] - a[2] / 2), int(a[1] - a[3] / 2), int(a[2]), int(a[3])] for a in bbs] return np.concatenate(bbs)
def transform_y(self, y, x): if not isinstance(y, md.StructuredLabel): return y for i, label in enumerate(y): data, data_type, name = label if (data_type == md.LabelType.CATEGORY): y[i] = (np.array(data), data_type, name) if (data_type == md.LabelType.BOUNDING_BOX): r, c, *_ = x.shape boxes = util.partition( data, 4) # Partition into array of bounding boxes masks = [center_to_mask(bb, x) for bb in boxes] # Create masks from bounding boxes trfms = [self.transform_x(mask) for mask in masks] # Transform masks y[i] = ( np.concatenate([mask_to_center(t) for t in trfms]), data_type, name ) # Convert masks back into bounding boxes, save result return y