def apply_image(self, img): ''' Resize image with random scale and position Args: img (PIL image): image to be resized Return: img (PIL image): resized image s (dict): ratio (tuple), scale of width and height ''' np_img = np.array(img) mean = np_img.mean(axis=0).mean(axis=0) h, w = np_img.shape[0], np_img.shape[1] in_w, in_h = self.size c = np.array([w / 2., h / 2.], dtype=np.float32) s = max(h, w) * 1.0 # image is randomly dragged to an center area with width and height of half of original w_border = get_border(int(w * 0.5), w) h_border = get_border(int(h * 0.5), h) s = s * np.random.uniform(self.scale[0], self.scale[1]) #np.random.choice(np.arange(0.8, 1.5, 0.1)) c[0] = np.random.randint(low=w_border, high=w - w_border) c[1] = np.random.randint(low=h_border, high=h - h_border) trans_input = get_affine_transform(c, s, 0, [in_w, in_h]) np_img = cv2.warpAffine(np_img, trans_input, (in_w, in_h), flags=cv2.INTER_LINEAR, borderValue=(int(mean[0]), int(mean[1]), int(mean[2]))) img = Image.fromarray(np_img) s = {'c': c, 's': s} return img, s
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_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 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
def apply_image(self, img): ''' Resize image Args: img (PIL image): image to be resized Return: img (PIL image): resized image s (dict): ratio (tuple), scale of width and height ''' np_img = np.array(img) mean = np_img.mean(axis=0).mean(axis=0) h, w = np_img.shape[0], np_img.shape[1] in_w, in_h = self.size c = np.array([w / 2., h / 2.], dtype=np.float32) s = max(h, w) * 1.0 trans_input = get_affine_transform(c, s, 0, [in_w, in_h]) np_img = cv2.warpAffine(np_img, trans_input, (in_w, in_h), flags=cv2.INTER_LINEAR, borderValue=(int(mean[0]), int(mean[1]), int(mean[2]))) img = Image.fromarray(np_img) s = {'c': c, 's': s} return img, s