def transform(self, in_data): augmenter = iaa.AffineCv2( translate_percent=(-0.25, 0.25), rotate=(-180, 180), shear=(-15, 15), ) augmenter = augmenter.to_deterministic() shape = None for index in self._rgb_indices: if shape is None: shape = in_data[index].shape[:2] assert shape == in_data[index].shape[:2] in_data[index] = augmenter.augment_image(in_data[index]) for index in self._mask_indices: masks = in_data[index].astype(np.uint8) masks = np.array([augmenter.augment_image(m) for m in masks]) in_data[index] = masks.astype(bool) for index in self._bbox_indices: bbox_on_img = imgaug.BoundingBoxesOnImage( [ imgaug.BoundingBox(x1, y1, x2, y2) for y1, x1, y2, x2 in in_data[index] ], shape, ) bbox_on_img = augmenter.augment_bounding_boxes(bbox_on_img) bboxes = [] for bbox in bbox_on_img.bounding_boxes: bboxes.append([bbox.y1, bbox.x1, bbox.y2, bbox.x2]) bboxes = np.array(bboxes, dtype=in_data[index].dtype) in_data[index] = bboxes
def main(): image = data.astronaut() image = ia.imresize_single_image(image, (HEIGHT, WIDTH)) kps = [] for y in range(NB_ROWS): ycoord = BB_Y1 + int(y * (BB_Y2 - BB_Y1) / (NB_COLS - 1)) for x in range(NB_COLS): xcoord = BB_X1 + int(x * (BB_X2 - BB_X1) / (NB_ROWS - 1)) kp = (xcoord, ycoord) kps.append(kp) kps = set(kps) kps = [ia.Keypoint(x=xcoord, y=ycoord) for (xcoord, ycoord) in kps] kps = ia.KeypointsOnImage(kps, shape=image.shape) bb = ia.BoundingBox(x1=BB_X1, x2=BB_X2, y1=BB_Y1, y2=BB_Y2) bbs = ia.BoundingBoxesOnImage([bb], shape=image.shape) pairs = [] seqs = [ iaa.AffineCv2(rotate=45), iaa.AffineCv2(translate_px=20), iaa.AffineCv2(translate_percent=0.1), iaa.AffineCv2(scale=1.2), iaa.AffineCv2(scale=0.8), iaa.AffineCv2(shear=45), iaa.AffineCv2(rotate=45, cval=256), iaa.AffineCv2(translate_px=20, mode=cv2.BORDER_CONSTANT), iaa.AffineCv2(translate_px=20, mode=cv2.BORDER_REPLICATE), iaa.AffineCv2(translate_px=20, mode=cv2.BORDER_REFLECT), iaa.AffineCv2(translate_px=20, mode=cv2.BORDER_REFLECT_101), iaa.AffineCv2(translate_px=20, mode=cv2.BORDER_WRAP), iaa.AffineCv2(translate_px=20, mode="constant"), iaa.AffineCv2(translate_px=20, mode="replicate"), iaa.AffineCv2(translate_px=20, mode="reflect"), iaa.AffineCv2(translate_px=20, mode="reflect_101"), iaa.AffineCv2(translate_px=20, mode="wrap"), iaa.AffineCv2(scale=0.5, order=cv2.INTER_NEAREST), iaa.AffineCv2(scale=0.5, order=cv2.INTER_LINEAR), iaa.AffineCv2(scale=0.5, order=cv2.INTER_CUBIC), iaa.AffineCv2(scale=0.5, order=cv2.INTER_LANCZOS4), iaa.AffineCv2(scale=0.5, order="nearest"), iaa.AffineCv2(scale=0.5, order="linear"), iaa.AffineCv2(scale=0.5, order="cubic"), iaa.AffineCv2(scale=0.5, order="lanczos4"), iaa.AffineCv2(rotate=45, translate_px=20, scale=1.2), iaa.AffineCv2(rotate=45, translate_px=20, scale=0.8), iaa.AffineCv2(rotate=(-45, 45), translate_px=(-20, 20), scale=(0.8, 1.2), order=ia.ALL, mode=ia.ALL, cval=ia.ALL), iaa.AffineCv2(rotate=(-45, 45), translate_px=(-20, 20), scale=(0.8, 1.2), order=ia.ALL, mode=ia.ALL, cval=ia.ALL), iaa.AffineCv2(rotate=(-45, 45), translate_px=(-20, 20), scale=(0.8, 1.2), order=ia.ALL, mode=ia.ALL, cval=ia.ALL), iaa.AffineCv2(rotate=(-45, 45), translate_px=(-20, 20), scale=(0.8, 1.2), order=ia.ALL, mode=ia.ALL, cval=ia.ALL) ] for seq in seqs: seq_det = seq.to_deterministic() image_aug = seq_det.augment_image(image) #print(image_aug.dtype, np.min(image_aug), np.max(image_aug)) kps_aug = seq_det.augment_keypoints([kps])[0] bbs_aug = seq_det.augment_bounding_boxes([bbs])[0] image_before = np.copy(image) image_before = kps.draw_on_image(image_before) image_before = bbs.draw_on_image(image_before) image_after = np.copy(image_aug) image_after = kps_aug.draw_on_image(image_after) image_after = bbs_aug.draw_on_image(image_after) pairs.append(np.hstack((image_before, image_after))) misc.imshow(np.vstack(pairs)) misc.imsave("affinecv2.jpg", np.vstack(pairs))
import os import glob import shutil from csv import reader, writer import cv2 folders = glob.glob('D:\\tsr\\train\\*') images = [] counter = 0 counter2 = 0 counter3 = 0 aug = iaa.SomeOf(2, [ iaa.Affine(rotate=(-5, 5)), iaa.AffineCv2(translate_px=8, mode=["replicate"]), iaa.Affine(shear=(-8, 8), cval=(0, 255)), iaa.AffineCv2(scale=(0.5, 1.5), order=[0, 1]), iaa.Multiply((0.5, 1.5)), iaa.AverageBlur(k=(2, 5)), iaa.MedianBlur(k=(3, 7)), iaa.GaussianBlur(sigma=(1.0, 3.0)), iaa.Sharpen(alpha=(0, 0.6), lightness=(0.75, 1.5)), iaa.Emboss(alpha=(0, 0.85), strength=(0, 1.5)), iaa.LinearContrast((0.5, 2.0), per_channel=0.5), iaa.AdditiveGaussianNoise(scale=(0.03 * 255, 0.05 * 255)) ]) def searchImagesFolder(list, str): for folder in folders: