def create_augimg(self, img, img_info): """ Create an augmented image and key points. Parameters img: as numpy array img_info: tuple with label and keypoints Return augmented image and keypoints """ label, points = img_info # print(points) kps_merge = [point for key_points in points for point in key_points] kps = KeypointsOnImage(kps_merge, shape=img.shape) # print(kps) if img.shape != self.dim: img = ia.imresize_single_image(img, self.dim[0:2]) kps = kps.on(img) # Augment keypoints and images. img_aug, kps_aug = seq(image=img, keypoints=kps) aug_points = [[kp.x, kp.y] for kp in kps_aug.keypoints] aug_points_dic = {'label': label, 'points': aug_points} return img_aug, aug_points_dic
def create_multi_augimg(self, img, img_info): """ This method create image augmentation of multiple classes per image. Parameters img: as numpy array img_info: tuple with label and keypoints Return augmented image and keypoints """ labels, points_list = img_info kps_merge = list() shapes_dict = list() index = 0 for label, key_points in zip(labels, points_list): kps_merge += key_points dict_aux = {} dict_aux['label'] = label dict_aux['index'] = (index, index + len(key_points)) shapes_dict.append(dict_aux) index += len(key_points) kps_merge = [ point for key_points in points_list for point in key_points ] kps_oi = KeypointsOnImage(kps_merge, shape=img.shape) # Resize image if shapes are not equals if img.shape != self.dim: img = ia.imresize_single_image(img, self.dim[0:2]) kps_oi = kps_oi.on(img) # Augment keypoints and images. if self.augmentation: seq_det = seq.to_deterministic() img_aug = seq_det.augment_images([img])[0] kps_aug = seq_det.augment_keypoints([kps_oi])[0] else: img_aug = img kps_aug = kps_oi # Add points to each dictionary for shape in shapes_dict: first, last = shape['index'] shape['points'] = [[kp.x, kp.y] for kp in kps_aug.keypoints[first:last]] return img_aug, shapes_dict