def augment_batch_img(batch_img, batch_pts, plot=False): """ Image augmentation, used when training :param batch_img: [B,H,W,C] :param batch_pts: [B,number,xy] :return: aug_b_img, aug_b_pts """ sometimes = lambda aug: iaa.Sometimes(0.5, aug) seq = iaa.Sequential([ iaa.CropAndPad(percent=((0., 0.), (-0.1, 0.1), (0., 0.), (-0.1, 0.1))), iaa.Affine(rotate=(-10, 10)), iaa.Add((-25, 25)) # change brightness ]) aug_b_imgs, aug_b_pts = seq(images=batch_img, keypoints=batch_pts) if plot: import cv2 batch_img = [ cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) for img in batch_img ] aug_b_imgs = [ cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) for img in aug_b_imgs ] for i in range(len(batch_img)): print("[Image #%d]" % (i, )) keypoints_before = KeypointsOnImage.from_xy_array( batch_pts[i], shape=batch_img[i].shape) keypoints_after = KeypointsOnImage.from_xy_array( aug_b_pts[i], shape=aug_b_imgs[i].shape) image_before = keypoints_before.draw_on_image(batch_img[i]) image_after = keypoints_after.draw_on_image(aug_b_imgs[i]) ia.imshow(np.hstack([image_before, image_after])) return aug_b_imgs, aug_b_pts
def test_augment_keypoints__kernel_size_is_two__no_keep_size(self): from imgaug.augmentables.kps import Keypoint, KeypointsOnImage kps = [Keypoint(x=1.5, y=5.5), Keypoint(x=5.5, y=1.5)] kpsoi = KeypointsOnImage(kps, shape=(6, 6, 3)) expected = KeypointsOnImage.from_xy_array(np.float32( [[1.5 / 2, 5.5 / 2], [5.5 / 2, 1.5 / 2]]), shape=(3, 3, 3)) self._test_augment_cbaoi__kernel_size_is_two__no_keep_size( kpsoi, expected, "augment_keypoints")
def test_empty_keypoints(self): from imgaug.augmentables.kps import KeypointsOnImage kpsoi = KeypointsOnImage([], shape=(5, 6, 3)) aug = self.augmenter(3, keep_size=False) kpsoi_aug = aug.augment_keypoints(kpsoi) expected = kpsoi.deepcopy(shape=(2, 2, 3)) assert_cbaois_equal(kpsoi_aug, expected)
def test_keypoint_alignment(self): from imgaug.augmentables.kps import Keypoint, KeypointsOnImage kps = [Keypoint(x=10, y=10), Keypoint(x=30, y=30)] kpsoi = KeypointsOnImage(kps, shape=(40, 40, 1)) kpsoi_empty = KeypointsOnImage([], shape=(40, 40, 1)) self._test_cbaoi_alignment(kpsoi, kpsoi_empty, [[(5, 5)], [(15, 15)]], [[(10, 10)], [(30, 30)]], "augment_keypoints")
def show_pts_on_img(image, pts, img_pth = '/home/workspace2/dataset/3dhand/dataset/pts_on_img.png'): if pts.shape[1] == 3: kps = [Keypoint(x, y) for x,y,_ in pts] else : kps = [Keypoint(x, y) for x,y in pts] kpsoi = KeypointsOnImage(kps, shape=image.shape) misc.imsave('/home/workspace2/dataset/3dhand/dataset/img.png', image) misc.imsave(img_pth, kpsoi.draw_on_image(image, size = 7))
def test_augment_keypoints__kernel_size_is_two__no_keep_size(self): from imgaug.augmentables.kps import Keypoint, KeypointsOnImage kps = [Keypoint(x=1.5, y=5.5), Keypoint(x=5.5, y=1.5)] kpsoi = KeypointsOnImage(kps, shape=(6, 6, 3)) aug = self.augmenter(2, keep_size=False) kpsoi_aug = aug.augment_keypoints(kpsoi) expected = KeypointsOnImage.from_xy_array(np.float32( [[1.5 / 2, 5.5 / 2], [5.5 / 2, 1.5 / 2]]), shape=(3, 3, 3)) assert_cbaois_equal(kpsoi_aug, expected)
def img2xy(self, img_path, gal, gpr, augment=0): ''' Single img file to xys for all blocks and channels returns: xs: cropped window, grayscale[0~255]=>[0~1] shape ( #blocks, 1, win_shape[0], win_shape[1] ) ys: gaussian heatmaps of same w, h as xs, one channel for one kpt idx: (img_path, block) for one x sample ''' idxs, imgs, df = super().img2x(img_path, gal, gpr) # imgs: (N, 1, h, w) h, w = imgs.shape[2], imgs.shape[3] gpr = Gpr(gpr) if type(gpr) == str else gpr xs, ys, coords = [], [], [] for (img_path, b), img, block_df in zip(idxs, imgs, df): nrows = gal.header[f'Block{b}'][Gal.N_ROWS] ncols = gal.header[f'Block{b}'][Gal.N_COLS] kpts = KeypointsOnImage([ Keypoint(x=block_df.loc[1, 1]['X'], y=block_df.loc[1, 1]['Y']), Keypoint(x=block_df.loc[1, ncols]['X'], y=block_df.loc[1, ncols]['Y']), Keypoint(x=block_df.loc[nrows, ncols]['X'], y=block_df.loc[nrows, ncols]['Y']), Keypoint(x=block_df.loc[nrows, 1]['X'], y=block_df.loc[nrows, 1]['Y']) ], shape=(h, w)) if augment <= 0: xs.append(img) coord = self.to_Lcoord(kpts.to_xy_array()) ys.append( self.coord2heatmap(coord, (img.shape[-2], img.shape[-1]))) coords.append(coord.flatten()) else: for i in range(augment): img_aug, kpts_aug = self.aug_seq( image=(img[0] * 255).astype('uint8'), keypoints=kpts) # img: (1, w, h) -> (w, h) coord = self.to_Lcoord(kpts_aug.to_xy_array()) # (3, 2) if self.check_out_of_bounds(img_aug.shape, coord): continue # skip if coord out of bounds xs.append(np.array([img_aug / 255])) ys.append( self.coord2heatmap( coord, (img_aug.shape[-2], img_aug.shape[-1]))) coords.append(coord.flatten()) return np.stack(xs), np.stack(ys), np.stack(coords)
def _test_augment_keypoints__kernel_size_differs(self, shape, shape_exp): from imgaug.augmentables.kps import Keypoint, KeypointsOnImage kps = [Keypoint(x=1.5, y=5.5), Keypoint(x=5.5, y=1.5)] kpsoi = KeypointsOnImage(kps, shape=shape) aug = self.augmenter((iap.Deterministic(3), iap.Deterministic(2)), keep_size=False) kpsoi_aug = aug.augment_keypoints(kpsoi) expected = KeypointsOnImage.from_xy_array(np.float32([[ (1.5 / shape[1]) * shape_exp[1], (5.5 / shape[0]) * shape_exp[0] ], [(5.5 / shape[1]) * shape_exp[1], (1.5 / shape[0]) * shape_exp[0]]]), shape=shape_exp) assert_cbaois_equal(kpsoi_aug, expected)
def augment_card(self, card, hull): cardH, cardW, _ = card.shape decalX = int((self.imgW - cardW) / 2) decalY = int((self.imgH - cardH) / 2) img_card = np.zeros((self.imgH, self.imgW, 4), dtype=np.uint8) img_card[decalY:decalY + cardH, decalX:decalX + cardW, :] = card card = img_card[:, :, :3] hull = hull[:, 0, :] card_kps_xy = np.float32([[decalX, decalY], [decalX + cardW, decalY], [decalX + cardW, decalY + cardH], [decalX, decalY + cardH]]) kpsoi_card = KeypointsOnImage.from_xy_array(card_kps_xy, shape=card.shape) # hull is a cv2.Contour, shape : Nx1x2 kpsoi_hull = [ ia.Keypoint(x=p[0] + decalX, y=p[1] + decalY) for p in hull.reshape(-1, 2) ] kpsoi_hull = KeypointsOnImage(kpsoi_hull, shape=(self.imgH, self.imgW, 3)) # create polygon poly_hull = Polygon(kpsoi_hull.keypoints) # create empty segmentation map for classes: background and card segmap = np.zeros((card.shape[0], card.shape[1], 3), dtype=np.uint8) # draw the tree polygon into the second channel segmap = poly_hull.draw_on_image(segmap, color=(0, 255, 0), alpha=1.0, alpha_lines=0.0, alpha_points=0.0) # merge the two channels to a single one segmap = np.argmax(segmap, axis=2) segmap = segmap.astype(np.uint8) segmap = SegmentationMapOnImage(segmap, nb_classes=2, shape=card.shape) myseq = self.seq.to_deterministic() card_aug, segmap_aug = myseq(image=card, segmentation_maps=segmap) card_aug, kpsoi_aug = myseq(image=card, keypoints=kpsoi_card) return card_aug, kpsoi_aug, segmap_aug
def __getitem__(self, index): """ Generates one sample of data :param index: The index of the image in the list of paths :return: Returns image as tensor, keypoints as tensor and image path as string """ # Select sample sample_path = self.list_paths[index] if not os.path.isfile(sample_path): print("{} is not a file/does not exist!".format(sample_path)) # Load data and get label image = cv.imread(sample_path) if image is None: print("{} is not a valid image".format(sample_path)) image_size = image.shape kps = self.np_to_keypoints(self.labels[index].flatten(), image_size) kpsoi = KeypointsOnImage(kps, shape=image.shape) # image_aug, kpsoi_aug = self.seq(image=image, keypoints=kpsoi) if not self.augment: image_aug, kpsoi_aug = self.seq_basic(image=image, keypoints=kpsoi) image_aug = cv.cvtColor(image_aug, cv.COLOR_BGR2RGB) if self.augment: image_aug, kpsoi_aug = self.seq2(image=image, keypoints=kpsoi) out_of_range = self.point_out_of_range(kpsoi_aug, image_size) if out_of_range: image_aug, kpsoi_aug = self.seq1(image=image, keypoints=kpsoi) # H: 0-179, S: 0-255, V: 0-255 image_aug = cv.cvtColor(image_aug, cv.COLOR_BGR2HSV) add_hue = np.random.normal(0, 8) add_sat = np.random.normal(0, 10) add_val = np.random.normal(0, 0) # print("h={}, s={}, v={}".format(add_hue, add_sat, add_val)) image_aug[:, :, 0] = np.mod((image_aug[:, :, 0] + int(add_hue)), 180) image_aug[:, :, 1] = np.clip((image_aug[:, :, 1] + int(add_sat)), 0, 254) image_aug[:, :, 2] = np.clip((image_aug[:, :, 2] + int(add_val)), 0, 254) image_aug = cv.cvtColor(image_aug, cv.COLOR_HSV2RGB) variance = random.uniform(0, 80) image_aug = self.add_noise(image_aug, 0, variance) image_aug = cv.normalize(image_aug, None, alpha=0, beta=1, norm_type=cv.NORM_MINMAX, dtype=cv.CV_32FC3) input_tensor = ((torch.tensor(image_aug)).permute(2, 0, 1)) # self.normalize output_tensor = torch.tensor(kpsoi_aug.to_xy_array().flatten() / self.keypoint_divisor) return input_tensor, output_tensor, sample_path
def pts_2_imgaug_keypts(points, img): kps = [] for point in points: keypoint = Keypoint(x=point[0], y=point[1]) kps.append(keypoint) kpsoi = KeypointsOnImage(kps, shape=img.shape) return kpsoi
def _augment_batch(self, images, corners): aug = iaa.SomeOf( (0, None), [ iaa.KeepSizeByResize( iaa.Affine( translate_px={"x": (10, 30)}, rotate=(-5, 5), mode="edge", fit_output=True, )), iaa.KeepSizeByResize( iaa.Affine(shear=(-10, 10), mode="edge", fit_output=True)), iaa.AddToHueAndSaturation((-50, 50)), iaa.AverageBlur(k=(2, 5)), ], random_order=True, ) # Convert array of corners to list of KeypointsOnImage instances for use with the augmenter. keypoints_from_corners = [ KeypointsOnImage( [Keypoint(x=point[0], y=point[1]) for point in img_corners], shape=self.dim, ) for img_corners in corners ] images_augm, keypoints_augm = aug.augment( images=images, keypoints=keypoints_from_corners) # Convert augmented keypoints back to array of size (batch_size, 4, 2). corners_augm = np.array( [keypoints.to_xy_array() for keypoints in keypoints_augm]) return images_augm, corners_augm
def code_anno(self, anno): coded_anno = np.zeros((6, 6, 5), dtype='float32') origin_w = int(anno['size']['width']) origin_h = int(anno['size']['height']) xmin = int(anno['object'][0]['bndbox']['xmin']) - 1 ymin = int(anno['object'][0]['bndbox']['ymin']) - 1 xmax = int(anno['object'][0]['bndbox']['xmax']) - 1 ymax = int(anno['object'][0]['bndbox']['ymax']) - 1 c_x = ((xmax + xmin) / 2) / origin_w c_y = ((ymax + ymin) / 2) / origin_h w = (xmax - xmin) / origin_w h = (ymax - ymin) / origin_h if self.aug: d = KeypointsOnImage([Keypoint(c_x * 400, c_y * 400)], (400, 400)) f**k = self.seq.augment_keypoints([d])[0] c_x = f**k.keypoints[0].x / 400 c_y = f**k.keypoints[0].y / 400 grid_x = int(np.floor(c_x * 6)) grid_y = int(np.floor(c_y * 6)) relative_x = c_x * 6 - grid_x relative_y = c_y * 6 - grid_y coded_anno[grid_y, grid_x, :] = np.array([1, relative_x, relative_y, w, h]) return coded_anno
def quokka_keypoints(size=None, extract=None): """Return example keypoints on the standard example quokke image. The keypoints cover the eyes, ears, nose and paws. Added in 0.5.0. (Moved from ``imgaug.imgaug``.) Parameters ---------- size : None or float or tuple of int or tuple of float, optional Size of the output image on which the keypoints are placed. If ``None``, then the keypoints are not projected to any new size (positions on the original image are used). ``float`` s lead to relative size changes, ``int`` s to absolute sizes in pixels. extract : None or 'square' or tuple of number or imgaug.augmentables.bbs.BoundingBox or imgaug.augmentables.bbs.BoundingBoxesOnImage Subarea to extract from the image. See :func:`~imgaug.imgaug.quokka`. Returns ------- imgaug.augmentables.kps.KeypointsOnImage Example keypoints on the quokka image. """ # TODO get rid of this deferred import from imgaug.augmentables.kps import Keypoint, KeypointsOnImage left, top = 0, 0 if extract is not None: bb_extract = _quokka_normalize_extract(extract) left = bb_extract.x1 top = bb_extract.y1 with open(_QUOKKA_ANNOTATIONS_FP, "r") as f: json_dict = json.load(f) keypoints = [] for kp_dict in json_dict["keypoints"]: keypoints.append(Keypoint(x=kp_dict["x"] - left, y=kp_dict["y"] - top)) if extract is not None: shape = (bb_extract.height, bb_extract.width, 3) else: shape = (643, 960, 3) kpsoi = KeypointsOnImage(keypoints, shape=shape) if size is not None: shape_resized = _compute_resized_shape(shape, size) kpsoi = kpsoi.on(shape_resized) return kpsoi
def __getitem__(self, item): dp = self.dataset[item] corners = dp['corners'] neighs = dp['neighs'] center = corners.mean(axis=0) ng = (neighs - center) * 3 + center mins = ng.min(axis=0).round().astype(int) maxs = ng.max(axis=0).round().astype(int) patch = dp['img'][mins[1]:maxs[1], mins[0]:maxs[0], :] corners = corners - mins neighs = neighs - mins kps_c = KeypointsOnImage.from_xy_array(corners, patch.shape) kps_n = KeypointsOnImage.from_xy_array(neighs, patch.shape) kps = KeypointsOnImage(kps_c.keypoints + kps_n.keypoints, patch.shape) if self.augment: augs = [ iaa.Affine(rotate=(-10, 10)), iaa.CropAndPad(percent=0.1), iaa.Resize(64), iaa.AdditiveGaussianNoise(scale=(0, 0.05 * 255)) ] else: augs = [iaa.Resize(64)] seq = iaa.Sequential(augs) norm_patch, norm_kps = seq(image=patch, keypoints=kps) norm_coords = norm_kps.to_xy_array().reshape([2, 4, 2]).transpose(0, 2, 1) for dim in range(2): norm_coords[:, dim, :] /= norm_patch.shape[dim] #keypoints = np.concatenate([np.array([1, dp['color'], 1,1,1,1]), corners[:,0], corners[:,1], neighs[:,0], neighs[:,1]]) norm_keypoints = np.concatenate( [np.array([1, dp['color'], 1, 1, 1, 1]), norm_coords.reshape(-1)]) return torch.tensor(norm_patch).permute( [2, 0, 1]).float() / 255, torch.tensor(norm_keypoints).float()
def test_empty_keypoints(self): from imgaug.augmentables.kps import KeypointsOnImage kpsoi = KeypointsOnImage([], shape=(5, 6, 3)) aug = self.augmenter(3, keep_size=False) kpsoi_aug = aug.augment_keypoints(kpsoi) assert kpsoi_aug.shape == (2, 2, 3) assert len(kpsoi_aug.keypoints) == 0
def _test_augment_keypoints__kernel_size_is_noop(self, kernel_size): from imgaug.augmentables.kps import Keypoint, KeypointsOnImage kps = [Keypoint(x=1.5, y=5.5), Keypoint(x=5.5, y=1.5)] kpsoi = KeypointsOnImage(kps, shape=(6, 6, 3)) aug = self.augmenter(kernel_size) kpsoi_aug = aug.augment_keypoints(kpsoi) assert_cbaois_equal(kpsoi_aug, kpsoi)
def aug_data(seq, img, pts=None): if pts is None: return seq(image=img) pts_shape = pts.shape kps = [Keypoint(x=x[0], y=x[1]) for x in pts.reshape(-1, 2)] kpsoi = KeypointsOnImage(kps, shape=img.shape) img_auged, kpsoi_auged = seq(image=img, keypoints=kpsoi) res_pts = kpsoi_auged.to_xy_array().reshape(pts_shape) return img_auged, res_pts
def _test_augment_keypoints__kernel_size_is_noop(self, kernel_size): from imgaug.augmentables.kps import Keypoint, KeypointsOnImage kps = [Keypoint(x=1.5, y=5.5), Keypoint(x=5.5, y=1.5)] kpsoi = KeypointsOnImage(kps, shape=(6, 6, 3)) aug = self.augmenter(kernel_size) kpsoi_aug = aug.augment_keypoints(kpsoi) assert kpsoi_aug.shape == (6, 6, 3) assert np.allclose(kpsoi_aug.to_xy_array(), [[1.5, 5.5], [5.5, 1.5]])
def test_augment_keypoints__kernel_size_is_two__no_keep_size(self): from imgaug.augmentables.kps import Keypoint, KeypointsOnImage kps = [Keypoint(x=1.5, y=5.5), Keypoint(x=5.5, y=1.5)] kpsoi = KeypointsOnImage(kps, shape=(6, 6, 3)) aug = self.augmenter(2, keep_size=False) kpsoi_aug = aug.augment_keypoints(kpsoi) assert kpsoi_aug.shape == (3, 3, 3) assert np.allclose(kpsoi_aug.to_xy_array(), [[1.5/2, 5.5/2], [5.5/2, 1.5/2]])
def test_keypoint_alignment(self): from imgaug.augmentables.kps import Keypoint, KeypointsOnImage aug = self.augmenter((1, 2), keep_size=False) image = np.zeros((40, 40, 1), dtype=np.uint8) kps = [Keypoint(x=10, y=10), Keypoint(x=30, y=30)] kpsoi = KeypointsOnImage(kps, shape=image.shape) kpsoi_empty = KeypointsOnImage([], shape=image.shape) images_batch = [image, image, image, image] kpsoi_batch = [kpsoi, kpsoi, kpsoi_empty, kpsoi] nb_iterations = 10 for _ in sm.xrange(nb_iterations): images_aug, kpsois_aug = aug(images=images_batch, keypoints=kpsoi_batch) for index in [0, 1, 3]: image_aug = images_aug[index] kpsoi_aug = kpsois_aug[index] assert image_aug.shape == kpsoi_aug.shape if image_aug.shape == (20, 20, 1): assert np.allclose( kpsoi_aug.to_xy_array(), [[5, 5], [15, 15]] ) else: assert np.allclose( kpsoi_aug.to_xy_array(), [[10, 10], [30, 30]] ) for index in [2]: image_aug = images_aug[index] kpsoi_aug = kpsois_aug[index] assert kpsoi_aug.shape == image_aug.shape assert len(kpsoi_aug.keypoints) == 0
def tranformKeypoints(self, keypoints, augmentation, imageshape): imgaug_keypoints = [] for i in range(len(keypoints)): imgaug_keypoints.append( Keypoint(x=keypoints[i, 0], y=keypoints[i, 1])) kpsoi = KeypointsOnImage(imgaug_keypoints, shape=imageshape) keypoitns_aug = augmentation(keypoints=kpsoi) if (isinstance(keypoints, np.ndarray)): keypoints[:, :2] = keypoitns_aug.to_xy_array() else: keypoints[:, :2] = torch.from_numpy( keypoitns_aug.to_xy_array()) return keypoints
def generate_augment_dataset(dataset_dir, name_list, num_aug=500): for name in name_list: image_name = os.path.join(dataset_dir, name + '.jpg') json_name = os.path.join(dataset_dir, name + '.json') image = Image.open(image_name).convert('RGB') image = np.asarray(image) points, labels = parse_annotations(json_name) kps = [ Keypoint(x=int(points[i][0]), y=int(points[i][1])) for i in range(len(points)) ] kpsoi = KeypointsOnImage(kps, shape=image.shape) for j in range(num_aug): image_aug, kpsoi_aug = seq(image=image, keypoints=kpsoi) points_aug = kpsoi_aug.keypoints height, width, _ = image_aug.shape image_aug_name = os.path.join(dataset_dir, name + '_' + str(j) + '.jpg') json_aug_name = os.path.join(dataset_dir, name + '_' + str(j) + '.json') data = {} data['version'] = '4.2.10' data['flags'] = {} data['shapes'] = [] for k in range(len(points_aug)): point_name = labels[k] p_x = int(points_aug[k].x) p_y = int(points_aug[k].y) data['shapes'].append({ 'label': point_name, 'points': [[p_x, p_y]], 'group_id': None, 'shape_type': 'point', 'flags': {} }) data['imagePath'] = name + str(j) + '.jpg' data['imageData'] = image_to_base64(image_aug) data['imageHeight'] = height data['imageWidth'] = width with open(json_aug_name, 'w') as outfile: json.dump(data, outfile, indent=4) image_aug = Image.fromarray(image_aug) image_aug.save(image_aug_name) print('success')
def test_augment_keypoints__kernel_size_differs__requires_padding(self): from imgaug.augmentables.kps import Keypoint, KeypointsOnImage kps = [Keypoint(x=1.5, y=5.5), Keypoint(x=5.5, y=1.5)] kpsoi = KeypointsOnImage(kps, shape=(5, 6, 3)) aug = self.augmenter( (iap.Deterministic(3), iap.Deterministic(2)), keep_size=False) kpsoi_aug = aug.augment_keypoints(kpsoi) assert kpsoi_aug.shape == (2, 3, 3) assert np.allclose(kpsoi_aug.to_xy_array(), [[(1.5/6)*3, (5.5/5)*2], [(5.5/6)*3, (1.5/5)*2]])
def getimage_FAN(self, imagefile, augmentations=None, keypoints=None, is_it_test_sample=False): image = cv2.cvtColor( cv2.imread( self.GetFullImagePath(self, imagefile, is_it_test_sample)), cv2.COLOR_BGR2RGB) if (augmentations is not None): keypoints_originalres = self.keypointsToOriginalResolution( self, imagefile, keypoints) imgaug_keypoints = [] for i in range(len(keypoints)): imgaug_keypoints.append( Keypoint(x=keypoints_originalres[i, 0], y=keypoints_originalres[i, 1])) kpsoi = KeypointsOnImage(imgaug_keypoints, shape=image.shape) image, keypoitns_aug = self.augmentations(image=image, keypoints=kpsoi) keypoints_originalres = np.column_stack( (keypoitns_aug.to_xy_array(), keypoints_originalres[:, 2:])) minx, miny, maxx, maxy = self.getFANBox( self, imagefile, image.shape[1], image.shape[0], is_it_test_sample) image = image[miny:maxy, minx:maxx, :] image = cv2.resize(image, dsize=(256, 256)) if (keypoints is not None): augmentedkeypoints = self.keypointsToFANResolution( self, imagefile, keypoints_originalres, self.W, self.H) return image, augmentedkeypoints return image
def test_augment_keypoints(self): from imgaug.augmentables.kps import Keypoint, KeypointsOnImage def do_return_augmentables(keypoints_on_images, parents, hooks): return keypoints_on_images aug_mock = mock.MagicMock(spec=meta.Augmenter) aug_mock.augment_keypoints.side_effect = do_return_augmentables kpsoi = KeypointsOnImage.from_xy_array(np.float32([[0, 0], [5, 1]]), shape=(16, 24, 3)) aug = iaa.WithHueAndSaturation(aug_mock) kpsoi_aug = aug.augment_keypoints(kpsoi) assert kpsoi_aug.shape == (16, 24, 3) assert kpsoi.keypoints[0].x == 0 assert kpsoi.keypoints[0].y == 0 assert kpsoi.keypoints[1].x == 5 assert kpsoi.keypoints[1].y == 1 assert aug_mock.augment_keypoints.call_count == 1
def augment(self, img, corners, row_inc=0, col_inc=0): ''' args: corners: [top left, bottom left, bottom right, top right] (4, 2) row_inc (int): changes n_row returns: aug_img coords: corner coords (4, 2) ''' img_aug, corners_aug, row_cut_coords = self.augment_row( img, corners, row_inc) img_aug, corners_aug, col_cut_coords = self.augment_col( img_aug, corners_aug, col_inc) kpts = KeypointsOnImage([ Keypoint(x=corners_aug[0, 0], y=corners_aug[0, 1]), Keypoint(x=corners_aug[1, 0], y=corners_aug[1, 1]), Keypoint(x=corners_aug[2, 0], y=corners_aug[2, 1]), Keypoint(x=corners_aug[3, 0], y=corners_aug[3, 1]), ], shape=img_aug.shape) return img_aug, kpts, (row_cut_coords, col_cut_coords)
def __data_generation(self, image_keys_temp): batch_images = np.empty((self.batch_size, IMG_SIZE, IMG_SIZE, 3), dtype="int") batch_keypoints = np.empty((self.batch_size, 1, 1, NUM_KEYPOINTS), dtype="float32") for i, key in enumerate(image_keys_temp): data = get_dog(key) current_keypoint = np.array(data["joints"])[:, :2] kps = [] # To apply our data augmentation pipeline, we first need to # form Keypoint objects with the original coordinates. for j in range(0, len(current_keypoint)): kps.append( Keypoint(x=current_keypoint[j][0], y=current_keypoint[j][1])) # We then project the original image and its keypoint coordinates. current_image = data["img_data"] kps_obj = KeypointsOnImage(kps, shape=current_image.shape) # Apply the augmentation pipeline. (new_image, new_kps_obj) = self.aug(image=current_image, keypoints=kps_obj) batch_images[i, ] = new_image # Parse the coordinates from the new keypoint object. kp_temp = [] for keypoint in new_kps_obj: kp_temp.append(np.nan_to_num(keypoint.x)) kp_temp.append(np.nan_to_num(keypoint.y)) # More on why this reshaping later. batch_keypoints[i, ] = np.array(kp_temp).reshape(1, 1, 24 * 2) # Scale the coordinates to [0, 1] range. batch_keypoints = batch_keypoints / IMG_SIZE return (batch_images, batch_keypoints)
def getimage_FAN(self, imagefile, augmentations=None, keypoints=None, is_it_test_sample=False): image = cv2.cvtColor( cv2.imread( self.GetFullImagePath(self, imagefile, is_it_test_sample)), cv2.COLOR_BGR2RGB) if (augmentations is not None): keypoints_originalres = self.keypointsToOriginalResolution( self, imagefile, keypoints) imgaug_keypoints = [] for i in range(len(keypoints)): imgaug_keypoints.append( Keypoint(x=keypoints_originalres[i, 0], y=keypoints_originalres[i, 1])) kpsoi = KeypointsOnImage(imgaug_keypoints, shape=image.shape) image, keypoitns_aug = self.augmentations(image=image, keypoints=kpsoi) keypoints_originalres = np.column_stack( (keypoitns_aug.to_xy_array(), keypoints_originalres[:, 2:])) scaledImage = self.scaleToFANRes(image=image) if (keypoints is not None): augmentedkeypoints = self.keypointsToFANResolution( self, imagefile, keypoints_originalres) return scaledImage, augmentedkeypoints return scaledImage
def augment_image(self, image, landmarks): """ Generates a batch of augmented images with landmarks from image and its landmarks :param image: image which will be augmented :param landmarks: numpy coordinates ([x], [x, y] or [x, y, z]) of the landmark point of the image. :return: numpy array of the landmark image. """ keypoints = [] for k in list(range(0, 2 * self.landmarks_num - 1, 2)): keypoint = Keypoint(x=(float(landmarks[k])), y=float(landmarks[k + 1])) keypoints.append(keypoint) keypoints_on_image = KeypointsOnImage(keypoints, shape=image.shape) images = [image for _ in range(self.batch_size)] keypoints_on_images = [ keypoints_on_image for _ in range(self.batch_size) ] images_aug, kpsois_aug = self.seq(images=images, keypoints=keypoints_on_images) return images_aug, kpsois_aug
def normalize_keypoints(inputs, shapes=None): # TODO get rid of this deferred import from imgaug.augmentables.kps import Keypoint, KeypointsOnImage shapes = _preprocess_shapes(shapes) ntype = estimate_keypoints_norm_type(inputs) _assert_exactly_n_shapes_partial = functools.partial( _assert_exactly_n_shapes, from_ntype=ntype, to_ntype="List[KeypointsOnImage]", shapes=shapes) if ntype == "None": return inputs elif ntype in ["array[float]", "array[int]", "array[uint]"]: _assert_single_array_ndim(inputs, 3, "(N,K,2)", "KeypointsOnImage") _assert_single_array_last_dim_exactly(inputs, 2, "KeypointsOnImage") _assert_exactly_n_shapes_partial(n=len(inputs)) return [ KeypointsOnImage.from_xy_array(attr_i, shape=shape) for attr_i, shape in zip(inputs, shapes) ] elif ntype == "tuple[number,size=2]": _assert_exactly_n_shapes_partial(n=1) return [KeypointsOnImage([Keypoint(x=inputs[0], y=inputs[1])], shape=shapes[0])] elif ntype == "Keypoint": _assert_exactly_n_shapes_partial(n=1) return [KeypointsOnImage([inputs], shape=shapes[0])] elif ntype == "KeypointsOnImage": return [inputs] elif ntype == "iterable[empty]": return None elif ntype in ["iterable-array[float]", "iterable-array[int]", "iterable-array[uint]"]: _assert_many_arrays_ndim(inputs, 2, "(K,2)", "KeypointsOnImage") _assert_many_arrays_last_dim_exactly(inputs, 2, "KeypointsOnImage") _assert_exactly_n_shapes_partial(n=len(inputs)) return [ KeypointsOnImage.from_xy_array(attr_i, shape=shape) for attr_i, shape in zip(inputs, shapes) ] elif ntype == "iterable-tuple[number,size=2]": _assert_exactly_n_shapes_partial(n=1) return [KeypointsOnImage([Keypoint(x=x, y=y) for x, y in inputs], shape=shapes[0])] elif ntype == "iterable-Keypoint": _assert_exactly_n_shapes_partial(n=1) return [KeypointsOnImage(inputs, shape=shapes[0])] elif ntype == "iterable-KeypointsOnImage": return inputs elif ntype == "iterable-iterable[empty]": return None elif ntype == "iterable-iterable-tuple[number,size=2]": _assert_exactly_n_shapes_partial(n=len(inputs)) return [ KeypointsOnImage.from_xy_array( np.array(attr_i, dtype=np.float32), shape=shape) for attr_i, shape in zip(inputs, shapes) ] else: assert ntype == "iterable-iterable-Keypoint", ( "Got unknown normalization type '%s'." % (ntype,)) _assert_exactly_n_shapes_partial(n=len(inputs)) return [KeypointsOnImage(attr_i, shape=shape) for attr_i, shape in zip(inputs, shapes)]