def apply_bbox(self, bbox, s): ''' Resize bbox with random scale and position Args: bbox (numpy.ndarray, shape 1x4): bbox to be resized s (dict): ratio (tuple), scale of width and height recorded in function, apply_image Return: bbox (numpy.ndarray, shape 1x4): resized bbox ''' assert 'c' in s assert 's' in s out_w, out_h = np.array(self.size) trans_output = get_affine_transform(s['c'], s['s'], 0, [out_w, out_h]) bbox[:2] = affine_transform(bbox[:2], trans_output) bbox[2:4] = affine_transform(bbox[2:4], trans_output) bbox[[0, 2]] = np.clip(bbox[[0, 2]], 0, out_w - 1) bbox[[1, 3]] = np.clip(bbox[[1, 3]], 0, out_h - 1) return bbox
def apply_pts(self, cid, pts, s): ''' Resize keypoints with random scale and position Args: cid (int): the class for keypoints pts (numpy.ndarray, shape Nx2): keypoints to be resized s (dict): ratio (tuple), scale of width and height recorded in function, apply_image Return: pts (numpy.ndarray, shape Nx2): resized keypoints ''' assert 'c' in s assert 's' in s out_h, out_w = (np.array(self.size) // self.stride).astype(int) trans_output = get_affine_transform(s['c'], s['s'], 0, [out_w, out_h]) for i in range(pts.shape[0]): pts[i] = affine_transform(pts[i], trans_output) return pts
def apply_pts(self, cid, pts, s): ''' Resize keypoints Args: cid (int): the class for keypoints pts (numpy.ndarray, shape Nx2): keypoints to be resized s (dict): ratio (tuple), scale of width and height recorded in function, apply_image Return: pts (numpy.ndarray, shape Nx2): resized keypoints ''' assert 'c' in s assert 's' in s out_w, out_h = np.array(self.size) trans_output = get_affine_transform(s['c'], s['s'], 0, [out_w, out_h]) for i in range(pts.shape[0]): pts[i,:2] = affine_transform(pts[i,:2], trans_output) if ((pts[i, :2] < 0).sum() + (pts[i, :2] > (out_w, out_h)).sum()) > 0: pts[i, 2] = 0.0 return pts