Beispiel #1
0
def chapter_augmenters_multiplybrightness():
    fn_start = "color/multiplybrightness"

    aug = iaa.MultiplyBrightness((0.5, 1.5))

    run_and_save_augseq(fn_start + ".jpg",
                        aug, [ia.quokka(size=(128, 128)) for _ in range(8)],
                        cols=4,
                        rows=2)
    def __call__(self, *args, **kwargs) -> typing.Tuple[np.ndarray, typing.List[Polygon]]:

        if self.is_training:
            resize = iaa.Resize(size=dict(longer_side=self.long_sizes,
                                          width='keep-aspect-ratio'))
            rotate = iaa.Rotate(rotate=self.angles, fit_output=True)
            resize_height = iaa.Resize(size=dict(height=self.height_ratios,
                                                 width='keep'))
            crop = iaa.CropToFixedSize(width=self.cropped_size[0], height=self.cropped_size[1])
            fix_resize = iaa.Resize(size=self.output_size)


            # blur = iaa.GaussianBlur()
            # blur = iaa.Sometimes(p=self.blur_prob,
            #                      then_list=blur)

            brightness = iaa.MultiplyBrightness((0.5, 1.5))
            brightness = iaa.Sometimes(self.color_jitter_prob, then_list=brightness)

            saturation = iaa.MultiplySaturation((0.5, 1.5))
            saturation = iaa.Sometimes(self.color_jitter_prob, then_list=saturation)

            contrast = iaa.LinearContrast(0.5)
            contrast = iaa.Sometimes(self.color_jitter_prob, then_list=contrast)

            hue = iaa.MultiplyHue()
            hue = iaa.Sometimes(self.color_jitter_prob, then_list=hue)

            augs = [resize,
                    rotate,
                    resize_height,
                    crop,
                    fix_resize,
                    brightness,
                    saturation,
                    contrast,
                    hue]
            ia = iaa.Sequential(augs)
        else:
            fix_resize = iaa.Resize(size=self.output_size)
            ia = iaa.Sequential([fix_resize])

        image = args[0]
        polygons = args[1]

        polygon_list = []
        for i in range(polygons.shape[0]):
            polygon_list.append(Polygon(polygons[i].tolist()))

        polygons_on_image = PolygonsOnImage(polygon_list, shape=image.shape)

        image_aug, polygons_aug = ia(image=image, polygons=polygons_on_image)

        return image_aug, polygons_aug.polygons
def ol_aug(image, mask):
    # ia.seed(seed)

    # Example batch of images.
    # The array has shape (32, 64, 64, 3) and dtype uint8.
    images = image  # B,H,W,C
    masks = mask  # B,H,W,C

    # print('In Aug',images.shape,masks.shape)
    combo = np.concatenate((images, masks), axis=3)
    # print('COMBO: ',combo.shape)

    seq_all = iaa.Sequential([
        iaa.Fliplr(0.5),  # horizontal flips
        # iaa.PadToFixedSize(width=crop_size[0], height=crop_size[1]),
        # iaa.CropToFixedSize(width=crop_size[0], height=crop_size[1]),
        iaa.Affine(
            scale={"x": (0.9, 1.1), "y": (0.9, 1.1)},
            # scale images to 90-110% of their size, individually per axis
            translate_percent={"x": (-0.1, 0.1), "y": (-0.1, 0.1)},
            # translate by -10 to +10 percent (per axis)
            rotate=(-5, 5),  # rotate by -5 to +5 degrees
            shear=(-3, 3),  # shear by -3 to +3 degrees
        ),
        # iaa.Cutout(nb_iterations=(1, 5), size=0.2, cval=0, squared=False),
    ], random_order=False)  # apply augmenters in random order

    seq_f = iaa.Sequential([
        iaa.Sometimes(0.5,
                      iaa.OneOf([
                          iaa.GaussianBlur((0.0, 3.0)),
                          iaa.MotionBlur(k=(3, 20)),
                      ]),
                      ),
        iaa.Sometimes(0.5,
                      iaa.OneOf([
                          iaa.Multiply((0.8, 1.2), per_channel=0.2),
                          iaa.MultiplyBrightness((0.5, 1.5)),
                          iaa.LinearContrast((0.5, 2.0), per_channel=0.2),
                          iaa.BlendAlpha((0., 1.), iaa.HistogramEqualization()),
                          iaa.MultiplyHueAndSaturation((0.5, 1.5), per_channel=0.2),
                      ]),
                      ),
    ], random_order=False)

    combo_aug = np.array(seq_all.augment_images(images=combo))
    # print('combo_au: ', combo_aug.shape)
    images_aug = combo_aug[:, :, :, :3]
    masks_aug = combo_aug[:, :, :, 3:]
    images_aug = seq_f.augment_images(images=images_aug)

    return images_aug, masks_aug
Beispiel #4
0
def new_gen_train_trans(image, mask):

    image = np.array(image)
    mask = np.array(mask)

    h, w = mask.shape
    th, tw = args.train_size

    crop_scales = [1.0, 0.875, 0.75, 0.625, 0.5]
    hue_factor = 0.6
    brightness_factor = 0.6  # was 0.5
    p_flip = 0.5
    jpeg_scale = 0, 80  # was 70
    p_erase_class = 0.5

    crop_scale = np.random.choice(crop_scales)
    ch, cw = [int(x * crop_scale) for x in (h, w)]
    i = np.random.randint(0, h - ch + 1)
    j = np.random.randint(0, w - cw + 1)
    image = image[i:i + ch, j:j + cw, :]
    mask = mask[i:i + ch, j:j + cw]

    brightness = iaa.MultiplyBrightness(
        (1 - brightness_factor, 1 + brightness_factor))
    hue = iaa.MultiplyHue((1 - hue_factor, 1 + hue_factor))
    jpeg = iaa.JpegCompression(compression=jpeg_scale)

    img_transforms = iaa.Sequential([brightness, hue, jpeg])
    image = img_transforms(image=image)

    if np.random.rand() < p_flip:
        image = np.flip(image, axis=1)
        mask = np.flip(mask, axis=1)

    image = Image.fromarray(image)
    mask = Image.fromarray(mask)

    # Resize, 1 for Image.LANCZOS
    image = TF.resize(image, (th, tw), interpolation=1)
    # Resize, 0 for Image.NEAREST
    mask = TF.resize(mask, (th, tw), interpolation=0)

    # From PIL to Tensor
    image = TF.to_tensor(image)
    # Normalize
    image = TF.normalize(image, args.dataset_mean, args.dataset_std)

    # Convert ids to train_ids
    mask = np.array(mask, np.uint8)
    mask = torch.from_numpy(mask)  # Numpy array to tensor

    return image, mask
Beispiel #5
0
 def __init__(self):
     self.aug = iaa.Sequential([
         iaa.Sometimes(0.25, iaa.GaussianBlur(sigma=(0, 3.0))),
         # iaa.Fliplr(0.15),
         # iaa.Crop(px=(0, 10)),
         iaa.Sometimes(0.25, iaa.PerspectiveTransform(0.08)),
         iaa.MultiplyBrightness((0.5, 1.5)),
         iaa.Affine(rotate=(-10, 10), mode='symmetric'),
         iaa.Sometimes(0.25,
                     iaa.OneOf([iaa.Dropout(p=(0, 0.1)),
                                 iaa.CoarseDropout(0.1, size_percent=0.5)])),
         iaa.AddToHueAndSaturation(value=(-10, 10), per_channel=True)
     ])
Beispiel #6
0
    def __init__(self, key_source='image', key_target=None):
        super(RandomColorJitter, self).__init__(key_source=key_source,
                                                key_target=key_target)

        self.sequence = iaa.Sequential([
            iaa.Sometimes(
                0.8,
                iaa.Sequential([
                    iaa.MultiplyBrightness((0.8, 1.25)),
                    iaa.MultiplyHueAndSaturation(mul_hue=(0.8, 1.25),
                                                 mul_saturation=(0.8, 1.25))
                ])),
            iaa.Sometimes(0.2, iaa.Grayscale())
        ])
Beispiel #7
0
    def __init__(self, dataset: Dataset, cfg):
        self._dataset = dataset
        self.input_shape = cfg.AUGMENT.INPUT_SHAPE
        self.zoom_in = cfg.AUGMENT.ZOOM_IN
        self.min_scale = cfg.AUGMENT.MIN_SCALE
        self.max_scale = cfg.AUGMENT.MAX_SCALE
        self.max_try_times = cfg.AUGMENT.MAX_TRY_TIMES
        self.flip = cfg.AUGMENT.FLIP
        self.aspect_ratio = cfg.AUGMENT.ASPECT_RATIO
        self.translate_percent = cfg.AUGMENT.TRANSLATE_PRESENT
        self.rotate = cfg.AUGMENT.ROTATE
        self.shear = cfg.AUGMENT.SHEAR
        self.perspective_transform = cfg.AUGMENT.PERSPECTIVE_TRANSFORM
        self.brightness = cfg.AUGMENT.BRIGHTNESS
        self.hue = cfg.AUGMENT.HUE
        self.saturation = cfg.AUGMENT.SATURATION
        self.augment_background = cfg.AUGMENT.BACKGROUND
        if self.augment_background:
            self.backgrounds = [
                os.path.join('../../data/background', item)
                for item in os.listdir('../../data/background')
            ]

        self.seq = iaa.Sequential([
            iaa.Fliplr(self.flip),
            iaa.Affine(scale={
                "x": self.aspect_ratio,
                "y": self.aspect_ratio
            },
                       translate_percent={
                           "x": self.translate_percent,
                           "y": self.translate_percent
                       },
                       rotate=self.rotate,
                       shear=self.shear,
                       order=[0, 1],
                       cval=(0, 255)),
            iaa.PerspectiveTransform(scale=self.perspective_transform),
            iaa.MultiplyBrightness(self.brightness),
            iaa.MultiplySaturation(self.saturation),
            iaa.MultiplyHue(self.hue)
        ])
Beispiel #8
0
def data_augmentation(path):
    ia.seed(2)

    seq = iaa.Sequential([
        iaa.Sometimes(0.5, iaa.Grayscale(alpha=(0.1, 0.5))),
        iaa.Sometimes(0.5, iaa.Multiply((0.5, 1.5), per_channel=0.5)),
        iaa.Sometimes(0.5,
                      iaa.MultiplyHueAndSaturation(mul_saturation=(0.5, 1.5))),
        iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0, 2.0))),
        iaa.Sometimes(0.8, iaa.MultiplyBrightness((0.5, 1.5))),
        iaa.AddToBrightness((-30, 30)),
        iaa.Sometimes(0.6,
                      iaa.MultiplyHueAndSaturation(mul_saturation=(0.5, 1.5)))
    ],
                         random_order=True)

    i = 0
    for fname in os.listdir(path):

        try:
            img = imageio.imread(os.path.join(path, fname), pilmode="RGB")
            print(i)
            if i % 5 == 0:
                img_aug = seq.augment_image(img)
                imageio.imwrite(
                    os.path.join(path, fname.replace(".jpg", "_imgaug.jpg")),
                    img_aug)
                fname_txt = fname.replace('.jpg', '.txt')
                print(
                    os.path.join(path,
                                 fname_txt.replace(".txt", "_imgaug.txt")))
                shutil.copyfile(
                    os.path.join(path, fname_txt),
                    os.path.join(path,
                                 fname_txt.replace(".txt", "_imgaug.txt")))

        except:
            print('Error reading img')
        i += 1
    def __init__(self):

        self.aug = iaa.Sequential([
            #         iaa.HorizontalFlip(p = 0.5),
            #         iaa.VerticalFlip(p = 0.5),
            #         iaa.Affine(scale=(0.5, 1.5)),
            iaa.Dropout(p=(0, 0.2), per_channel=0.5),
            iaa.SomeOf(
                (1, 2),
                [
                    #                     iaa.Cutout(fill_mode="gaussian", fill_per_channel=True),
                    iaa.SaltAndPepper(0.1),
                    #                     iaa.Affine(rotate=(-45, 45), shear=(-16, 16)),
                    #                     iaa.imgcorruptlike.GaussianNoise(severity=1),
                    #                     iaa.AveragePooling(2),
                    iaa.AddToHueAndSaturation((-60, 60)),
                    iaa.MultiplyBrightness(mul=(0.65, 1.35)),
                    iaa.LinearContrast((0.5, 2.0)),
                    iaa.GaussianBlur(sigma=(0.5, 2.0)),
                    #                     iaa.CoarseDropout((0.01,0.1), size_percent = 0.01)
                ])
        ])
Beispiel #10
0
    def __getitem__(self, index):
        co_ords = self.coords[index * BATCH_SIZE:(index + 1) * BATCH_SIZE]

        batch_images = np.zeros((len(co_ords), INPUT_HEIGHT, INPUT_WIDTH, 3),
                                dtype=np.float32)
        batch_heatmaps = np.zeros(
            (len(co_ords), OUTPUT_HEIGHT, OUTPUT_WIDTH, 2), dtype=np.float32)

        for i, row in enumerate(co_ords):
            images_path, x, y = row

            proc_image = image.load_img(self.image_path + images_path,
                                        target_size=(INPUT_HEIGHT,
                                                     INPUT_WIDTH))
            proc_image = image.img_to_array(proc_image, dtype='uint8')

            heatmap = heatmap_splat(y, x)  # y is height and x is width!!
            heatmap = np.expand_dims(heatmap, axis=0)

            aug_list = iaa.OneOf([
                iaa.Dropout([0.05, 0.1]),
                iaa.Sharpen((0.0, 1.0)),
                iaa.MultiplyHue((0.7, 1.4)),
                iaa.MultiplyBrightness((0.7, 1.4)),
            ])

            aug = iaa.Sequential([aug_list, iaa.Fliplr(0.5)],
                                 random_order=True)

            proc_image, heatmap = aug.augment(image=proc_image,
                                              heatmaps=heatmap)

            proc_image = np.expand_dims(proc_image, axis=0)
            proc_image = proc_image / 255.  #just for now try without normalising
            batch_images[i] = proc_image
            batch_heatmaps[i] = heatmap

        return batch_images, batch_heatmaps
def init_augmenter(img_mode="color"):
    """Initializes the augmenters used in the training dataset
    :param config: the config object that contains all the 
    """
    ia.seed(10)

    if img_mode == 'color':
        return iaa.Sequential([
            sometimes(iaa.Fliplr()),
            iaa.MultiplyBrightness((0.6, 1.4)),
            # TODO: try no ChangeColor or Brightness
            sometimes(iaa.ChangeColorTemperature((5000, 7000))),
            iaa.Crop(percent=(
                (0, 0.50),
                (0, 0.50),
                (0, 0.50),
                (0, 0.50)
            ))
            # sometimes(iaa.OneOf([
            #     iaa.Cutout(nb_iterations=(1, 4), size=0.2,
            #                squared=False, cval=(0, 255), fill_mode="constant"),
            #     iaa.Cutout(nb_iterations=(1, 4), size=0.2, squared=False, cval=(
            #         0, 255), fill_mode="gaussian", fill_per_channel=True),
            #     iaa.AdditiveGaussianNoise(scale=(0, 0.1*255))
            # ]))
        ])
    else:
        return iaa.Sequential([
            sometimes(iaa.Fliplr()),
            iaa.Crop(percent=(
                (0, 0.40),
                (0, 0.40),
                (0, 0.40),
                (0, 0.40)
            ))
        ])
# instantiate imgaug augmentation object
sometimes = lambda aug: iaa.Sometimes(0.5, aug)

AUGMENTATIONS = iaa.Sequential([
    iaa.Fliplr(0.5),
    iaa.Flipud(0.5),
    sometimes(iaa.Affine(
        scale=(0.8, 1.2),
        rotate=(90),
        mode=ia.ALL)),
    sometimes(iaa.ElasticTransformation(alpha=(0.8, 1.2),\
                                        sigma=(9.0, 11.0))),
    sometimes(iaa.AdditiveGaussianNoise(scale=(0, 0.1))),
    sometimes(iaa.GaussianBlur((0, 0.1))),
    sometimes(iaa.MultiplyBrightness((0.65, 1.35))),
    sometimes(iaa.LinearContrast((0.5, 1.5))),
    sometimes(iaa.MultiplyHueAndSaturation((-1, 1)))
    ], random_order=True)

# instantiate datagen objects
train_datagen = ImageDataAugmentor(
    #     featurewise_center=True,
    #     featurewise_std_normalization=True,
    augment=AUGMENTATIONS,
    rescale=1. / 255,
    preprocess_input=None)

val_datagen = ImageDataAugmentor(rescale=1. / 255)

# define the ImageNet mean subtraction (in RGB order)
# dataset = NewDataset()
# loader = DataLoader(dataset, batch_size=2)

# for x in loader:
#     print(x)
import numpy as np
from imgaug import augmenters as iaa
from torch.utils.data import DataLoader, Dataset
from torchvision import transforms

tfs = transforms.Compose([
    iaa.Sequential([
        iaa.flip.Fliplr(p=0.5),
        iaa.flip.Flipud(p=0.5),
        iaa.GaussianBlur(sigma=(0.0, 0.1)),
        iaa.MultiplyBrightness(mul=(0.65, 1.35)),
    ]).augment_image,
    transforms.ToTensor()
])


class CustomDataset(Dataset):
    def __init__(self, n_images, n_classes, transform=None):
        self.images = np.random.randint(0,
                                        255, (n_images, 224, 224, 3),
                                        dtype=np.uint8)
        self.targets = np.random.randn(n_images, n_classes)
        self.transform = transform

    def __getitem__(self, item):
        image = self.images[item]
Beispiel #14
0
         iaa.BlendAlphaElementwise((0.0, 1.0),
                                   foreground=iaa.Add((-15, 15)),
                                   background=iaa.Multiply((0.8, 1.2))),
         iaa.ReplaceElementwise(0.05,
                                iap.Normal(128, 0.4 * 128),
                                per_channel=0.5),
         iaa.Dropout(p=(0, 0.05), per_channel=0.5),
     ])),
 # Brightness + Color + Contrast
 iaa.Sometimes(
     0.5,
     iaa.OneOf([
         iaa.Add(iap.Normal(iap.Choice([-30, 30]), 10)),
         iaa.Multiply((0.75, 1.25)),
         iaa.AddToBrightness((-35, 35)),
         iaa.MultiplyBrightness((0.85, 1.15)),
         iaa.MultiplyAndAddToBrightness(mul=(0.85, 1.15), add=(-10, 10)),
         iaa.BlendAlphaHorizontalLinearGradient(iaa.Add(
             iap.Normal(iap.Choice([-40, 40]), 10)),
                                                start_at=(0, 0.2),
                                                end_at=(0.8, 1)),
         iaa.BlendAlphaHorizontalLinearGradient(iaa.Add(
             iap.Normal(iap.Choice([-40, 40]), 10)),
                                                start_at=(0.8, 1),
                                                end_at=(0, 0.2)),
         iaa.BlendAlphaVerticalLinearGradient(iaa.Add(
             iap.Normal(iap.Choice([-40, 40]), 10)),
                                              start_at=(0.8, 1),
                                              end_at=(0, 0.2)),
         iaa.BlendAlphaVerticalLinearGradient(iaa.Add(
             iap.Normal(iap.Choice([-40, 40]), 10)),
    def __init__(self):
        self.seq = iaa.Sequential(
            [
                iaa.Fliplr(0.5),
                iaa.Sometimes(0.5, iaa.Crop(percent=(0, 0.1))),

                iaa.Sometimes(0.5, iaa.Affine(
                    rotate=(-20, 20),  # 旋转±20度
                    # shear=(-16, 16),   # 剪切变换±16度,矩形变平行四边形
                    # order=[0, 1],  # 使用最近邻插值 或 双线性插值
                    cval=0,  # 填充值
                    mode=ia.ALL  # 定义填充图像外区域的方法
                )),

                # 使用0~3个方法进行图像增强
                iaa.SomeOf((0, 3),
                           [
                               iaa.Sometimes(0.8, iaa.OneOf([
                                   iaa.GaussianBlur((0, 2.0)),  # 高斯模糊
                                   iaa.AverageBlur(k=(1, 5)),  # 平均模糊,磨砂
                               ])),

                               # 要么运动,要么美颜
                               iaa.Sometimes(0.8, iaa.OneOf([
                                   iaa.MotionBlur(k=(3, 11)),  # 运动模糊
                                   iaa.BilateralBlur(d=(1, 5),
                                                     sigma_color=(10, 250),
                                                     sigma_space=(10, 250)),  # 双边滤波,美颜
                               ])),

                               # 模仿雪花
                               iaa.Sometimes(0.8, iaa.OneOf([
                                   iaa.SaltAndPepper(p=(0., 0.03)),
                                   iaa.AdditiveGaussianNoise(loc=0, scale=(0., 0.05 * 255), per_channel=False)
                               ])),

                               # 对比度
                               iaa.Sometimes(0.8, iaa.LinearContrast((0.6, 1.4), per_channel=0.5)),

                               # 锐化
                               iaa.Sometimes(0.8, iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5))),

                               # 整体亮度
                               iaa.Sometimes(0.8, iaa.OneOf([
                                   # 加性调整
                                   iaa.AddToBrightness((-30, 30)),
                                   # 线性调整
                                   iaa.MultiplyBrightness((0.5, 1.5)),
                                   # 加性 & 线性
                                   iaa.MultiplyAndAddToBrightness(mul=(0.5, 1.5), add=(-30, 30)),
                                ])),

                               # 饱和度
                               iaa.Sometimes(0.8, iaa.OneOf([
                                   iaa.AddToSaturation((-75, 75)),
                                   iaa.MultiplySaturation((0., 3.)),
                               ])),

                               # 色相
                               iaa.Sometimes(0.8, iaa.OneOf([
                                   iaa.AddToHue((-255, 255)),
                                   iaa.MultiplyHue((-3.0, 3.0)),
                               ])),

                               # 云雾
                               # iaa.Sometimes(0.3, iaa.Clouds()),

                               # 卡通化
                               # iaa.Sometimes(0.01, iaa.Cartoon()),
                           ],
                           random_order=True
                           )
            ],
            random_order=True
        )
save_model_dir = '/tmp2/wide_angel'
train_type = 'wide_angle'
data_version = 'v4'
csv_train = Path(__file__).parent.parent.absolute().joinpath(
    'datafiles', data_version, 'train.csv')
csv_valid = Path(__file__).parent.parent.absolute().joinpath(
    'datafiles', data_version, 'valid.csv')
csv_test = Path(__file__).parent.parent.absolute().joinpath(
    'datafiles', data_version, 'test.csv')

iaa = iaa.Sequential([
    # iaa.CropAndPad(percent=(-0.04, 0.04)),
    iaa.Fliplr(0.5),
    iaa.Flipud(0.25),
    iaa.GaussianBlur(sigma=(0.0, 0.3)),
    iaa.MultiplyBrightness(mul=(0.7, 1.3)),
    iaa.contrast.LinearContrast((0.7, 1.3)),
    iaa.Sometimes(0.9, iaa.Add((-8, 8))),
    iaa.Sometimes(
        0.9,
        iaa.Affine(
            scale=(0.98, 1.02),
            translate_percent={
                "x": (-0.06, 0.06),
                "y": (-0.06, 0.06)
            },
            rotate=(-15, 15),
        )),
])

batch_size_train, batch_size_valid = 32, 64
Beispiel #17
0
import cv2
import os
import uuid

filename_csv = 'fovea.csv'
image_shape = (299, 299)

dir_tmp = '/tmp2/dataset_test/'

iaa = iaa.Sequential([
    # iaa.CropAndPad(percent=(-0.04, 0.04)),
    iaa.Fliplr(0.5),  # horizontally flip 50% of the images
    iaa.Flipud(0.2),  # horizontally flip 50% of the images

    iaa.GaussianBlur(sigma=(0.0, 0.5)),
    iaa.MultiplyBrightness(mul=(0.8, 1.2)),
    iaa.contrast.LinearContrast((0.8, 1.2)),
    iaa.Sometimes(0.9, iaa.Add((-8, 8))),
    iaa.Sometimes(0.9, iaa.Affine(
        scale=(0.98, 1.02),
        translate_percent={"x": (-0.06, 0.06), "y": (-0.06, 0.06)},
        rotate=(-15, 15),
    )),
])

iaa = None

batch_size = 32

dataset= Dataset_CSV(csv_file=filename_csv, imgaug_iaa=iaa, image_shape=image_shape)
loader = DataLoader(dataset, batch_size=batch_size,
Beispiel #18
0
import numpy as np
from imgaug import augmenters as iaa
from torch.utils.data import DataLoader, Dataset
from torchvision import transforms

from skimage import io
import os

tfs = transforms.Compose([
    iaa.Sequential([
                    iaa.Sometimes(0.5, iaa.Fliplr(1.0)),
                    iaa.Sometimes(0.5, iaa.MultiplyBrightness((0.3, 1.3))),
                    iaa.Sometimes(0.2, iaa.ChangeColorTemperature((4300, 6000))),
                    #iaa.Sometimes(0.9, iaa.Affine(rotate=(-180, 180), shear=(-6, 6))),
    ]).augment_image,
    transforms.ToTensor()
])


class CustomDataset(Dataset):
    def __init__(self, n_images, n_classes=15, transform=None):
        self.images = []
        
        self.transform = transform

        test_path = "/content/gdrive/My Drive/Arirang/data/test/images"
        file_list = os.listdir(test_path)
        file_list_png = [file for file in file_list if file.endswith(".png")]
        
        for idx, filename in enumerate(file_list_png):
            self.images.append( os.path.join(test_path, filename)) 
# save to local


# read from local
# f = open("dict.txt", 'r')
# dict_ = eval(f.read())
# f.close()
# print("read from local : ", dict_)



def getImageVar(img):
    img=cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
    imagevar=cv2.Laplacian(img,cv2.CV_64F).var()
    return imagevar
aug_brightness = iaa.MultiplyBrightness((0.7, 1.1))
aug_gaussian =iaa.GaussianBlur((0, 2.0))
kernel_sharpen_1 = np.array([
    [-1, -1, -1],
    [-1, 9, -1],
    [-1, -1, -1]])
path='/home/ubuntu/hks/ocr/idcard_generator_project/idcard_pix2pix/data_train_with_aug/'
dirs=os.listdir(path)
d_dict={}
for d in dirs:
    d_dict[d+'_0']=0
    d_dict[d+'_1']=0
l=[]
for i in range(1,20):
    for index,d in enumerate(dirs):
        # os.mkdir('/home/ubuntu/hks/ocr/idcard_generator_project/idcard_pix2pix/data_val_with_aug/'+d)
Beispiel #20
0
        transformed_image = transform(image=image)['image']

    elif augmentation == 'brightness':
        transform = iaa.imgcorruptlike.Brightness(severity=2)
        transformed_image = transform(image=image)

    elif augmentation == 'addto_hue_and_saturation':
        transform = iaa.AddToHueAndSaturation((-50, 50), per_channel=True)
        transformed_image = transform(image=image)

    elif augmentation == 'hue_saturation':
        transform = HueSaturationValue(always_apply=True)
        transformed_image = transform(image=image)['image']

    elif augmentation == 'multiply_brightness':
        transform = iaa.MultiplyBrightness((0.1, 1.9))
        transformed_image = transform(image=image)

    elif augmentation == 'addto_brightness':
        transform = iaa.AddToBrightness((-50, 50))
        transformed_image = transform(image=image)

    elif augmentation == 'multiply_and_addtobrightness':
        transform = iaa.MultiplyAndAddToBrightness(mul=(0.5, 1.5), 
                                                   add=(-30, 30))
        transformed_image = transform(image=image)

    elif augmentation == 'to_gray':
        transform = ToGray(always_apply=True)
        transformed_image = transform(image=image)['image']
Beispiel #21
0
    image = tf.image.random_crop(image,
                                 [tf.shape(image)[0], *INPUT_SHAPE[:2], 3])
    return image, target


# resize image to 224x224
def resize_image(image, target):
    image = tf.image.resize(image, INPUT_SHAPE[:2])
    return image, target


# augmentation options
AUG = iaa.Sequential([
    iaa.SomeOf((0, 1), [
        iaa.AddToBrightness((-30, 30)),
        iaa.MultiplyBrightness((0.5, 1.5)),
        iaa.MultiplySaturation((0.5, 1.5)),
        iaa.AddToSaturation((-50, 50))
    ]),
    iaa.OneOf([
        iaa.ScaleX((1.0, 1.5)),
        iaa.ScaleY((1.0, 1.5)),
        iaa.Affine(scale={
            "x": (1.0, 1.2),
            "y": (1.0, 1.2)
        }),
        iaa.Affine(rotate=(-20, 20)),
        iaa.PiecewiseAffine(scale=(0.01, 0.05)),
        iaa.Affine(shear=(-16, 16))
    ]),
    iaa.Fliplr(0.5),
def main():

    try:
        config_dirs_file = sys.argv[1] # directories file
        config_file = sys.argv[2]      # main params file
    except:
        print("Config file names not specified, setting them to default namess")
        config_dirs_file = "config_dirs.json"
        config_file = "config760.json"
    print(f'USING CONFIG FILES: config dirs:{config_dirs_file}  main config:{config_file}')    
    
    #print(type(feature_directory))
    C = cs760.loadas_json('config760.json')
    print("Running with parameters:", C)
    
    Cdirs = cs760.loadas_json(config_dirs_file)
    print("Directories:", Cdirs)
    
    C['dirs'] = Cdirs
    video_directory = C['dirs']['indir']
    feature_directory = C['dirs']['outdir']
    
    print(f'Creating feature file Dir: {feature_directory}')
    os.makedirs(feature_directory, exist_ok=True)        #if dir already exists will continue and WILL NOT delete existing files in that directory


    sometimes = lambda aug: iaa.Sometimes(C["augmentation_chance"][0], aug)
    sequential_list = [iaa.Sequential([sometimes(iaa.Fliplr(1.0))]), # horizontal flip
    iaa.Sequential([sometimes(iaa.Rotate(-5, 5))]), # rotate 5 degrees +/-
    iaa.Sequential([sometimes(iaa.CenterCropToAspectRatio(1.15))]),
    iaa.Sequential([sometimes(iaa.MultiplyBrightness((2.0, 2.0)))]), # increase brightness
    iaa.Sequential([sometimes(iaa.MultiplyHue((0.5, 1.5)))]), # change hue random
    iaa.Sequential([sometimes(iaa.RemoveSaturation(1.0))]), # effectively greyscale
    iaa.Sequential([sometimes(iaa.pillike.FilterContour())]), # edge detection
    iaa.Sequential([sometimes(iaa.AdditiveLaplaceNoise(scale=0.05*255, per_channel=True))]), # add colourful noise
    iaa.Sequential([sometimes(iaa.Invert(1))]) # invert colours
    ]


    print("Reading videos from " + video_directory)
    print("Outputting features to " + feature_directory)

    print("Loading pretrained CNN...")
    model = hub.KerasLayer(C["module_url"])  # can be used like any other kera layer including in other layers...
    print("Pretrained CNN Loaded OK")

    vids = cs760.list_files_pattern(video_directory, C["vid_type"])
    print(f'Processing {len(vids)} videos...')

    for i, vid in enumerate(vids):
        print(f'{i} Processing: {vid}')    
        vid_np = cs760.get_vid_frames(vid, 
                        video_directory, 
                        writejpgs=False,
                        writenpy=False,
                        returnnp=True)
        (framecount, frameheight, framewidth, channels) = vid_np.shape
        res_key = str(frameheight) + "-" + str(framewidth)
        #print(vid, vid_np.shape)
        outfile = os.path.splitext(vid)[0]
        
        print(f"Vid frames, h, w, c = {(framecount, frameheight, framewidth, channels)}")
        
        if C["crop_by_res"].get(res_key) is not None:
            vid_np_top = cs760.crop_image(vid_np, C["crop_by_res"][res_key])
            print(f"Cropped by resolution to {C['crop_by_res'][res_key]}")
        else:    
            vid_np_top = cs760.crop_image(vid_np, C["crop_top"])
            print(f"Cropped by default to {C['crop_top']}")

        outfile_top = outfile + "__TOP.pkl"

        for n in range((len(sequential_list) + 1)):
            if n != 0:
                vid_aug = sequential_list[n - 1](images=vid_np_top) # augments frames
                if type(vid_aug) is list:
                    vid_aug = np.asarray(vid_aug)
                batch = cs760.resize_batch(vid_aug, width=C["expect_img_size"], height=C["expect_img_size"], pad_type='L',
                            inter=cv2.INTER_CUBIC, BGRtoRGB=False, 
                            simplenormalize=True,
                            imagenetmeansubtract=False)
                temp_outfile = outfile_top[:-4] + C["augmentation_type"][n - 1] + ".pkl"
                features = extract(C, model, batch)
                cs760.saveas_pickle(features, os.path.join(feature_directory, temp_outfile))
            else:
                batch = cs760.resize_batch(vid_np_top, width=C["expect_img_size"], height=C["expect_img_size"], pad_type='L',
                                inter=cv2.INTER_CUBIC, BGRtoRGB=False, 
                                simplenormalize=True,
                                imagenetmeansubtract=False)
                features = extract(C, model, batch)
                cs760.saveas_pickle(features, os.path.join(feature_directory, outfile_top))
                print(f'Features output shape: {features.shape}')
                
        if C["crop_type"] == 'B':  # only for boston vids
            vid_np_bot = cs760.crop_image(vid_np, C["crop_bottom"])
            outfile_bot = outfile + "__BOT.pkl"  
            batch = cs760.resize_batch(vid_np_bot, width=C["expect_img_size"], height=C["expect_img_size"], pad_type='L',
                        inter=cv2.INTER_CUBIC, BGRtoRGB=False, 
                        simplenormalize=True,
                        imagenetmeansubtract=False)
            features = extract(C, model, batch)
            cs760.saveas_pickle(features, os.path.join(feature_directory, outfile_bot))

    print('Finished outputting features!!')
def augumentation(train_set):

    img_final_height = int(375 * 0.7)
    img_final_width = int(500 * 0.7)

    #The transform function for train data
    # transform_train = transforms.Compose([
    #     transforms.Resize((int(img_final_height*1.1),int(img_final_width*1.1))),
    #     transforms.RandomCrop((img_final_height,img_final_width), padding=4),
    #     # transforms.Resize(224),
    #     transforms.ToTensor(),
    #     #transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
    #     transforms.Normalize(mean=[0.485, 0.456, 0.406],
    #                          std=[0.229, 0.224, 0.225])
    # ])
    # transform_train = transforms.Compose([
    #         transforms.Resize((256,128), interpolation=transforms.InterpolationMode.BICUBIC),
    #         transforms.RandomHorizontalFlip(0.5),
    #         transforms.Pad(10),
    #         transforms.RandomCrop((256,128)),
    #         transforms.ToTensor(),
    #         transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
    #         RandomErasing(probability=0.5, mean=([0.485, 0.456, 0.406]))
    #     ])

    transform_train = transforms.Compose([
        transforms.Resize(
            (int(img_final_height * 1.1), int(img_final_width * 1.1))),
        transforms.RandomCrop((img_final_height, img_final_width), padding=4),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225]),
        RandomErasing(probability=0.5, mean=([0.485, 0.456, 0.406]))
    ])
    # transform_train= transforms.Compose([
    #     iaa.Sequential([
    #         iaa.flip.Fliplr(p=0.5),
    #         iaa.flip.Flipud(p=0.5),
    #         iaa.GaussianBlur(sigma=(0.0, 0.1)),
    #         iaa.MultiplyBrightness(mul=(0.65, 1.35)),
    #     ]).augment_image,
    #     transforms.ToTensor()
    # ])

    seq = iaa.Sequential([
        iaa.flip.Flipud(p=0.5),
        iaa.MultiplyBrightness(mul=(0.65, 1.35)),
        #iaa.GammaContrast(1.5),
        iaa.AdditiveGaussianNoise(loc=0,
                                  scale=(0.0, 0.05 * 255),
                                  per_channel=0.5),
        iaa.Fliplr(p=0.5),  # 水平翻轉影象
        iaa.GaussianBlur(sigma=(0, 3.0)),  # 使用0到3.0的sigma模糊影象
        # Small gaussian blur with random sigma between 0 and 0.5.
        # But we only blur about 50% of all images.
        iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0, 0.5))),
        # Add gaussian noise.
        # For 50% of all images, we sample the noise once per pixel.
        # For the other 50% of all images, we sample the noise per pixel AND
        # channel. This can change the color (not only brightness) of the
        # pixels.
        iaa.AdditiveGaussianNoise(loc=0,
                                  scale=(0.0, 0.05 * 255),
                                  per_channel=0.5),
        # Make some images brighter and some darker.
        # In 20% of all cases, we sample the multiplier once per channel,
        # which can end up changing the color of the images.
        iaa.Multiply((0.8, 1.2), per_channel=0.8)
        # Apply affine transformations to each image.
        # Scale/zoom them, translate/move them, rotate them and shear them.
    ])

    #The transform function for test data

    # trainset.setImgaug(seq)
    train_set.setTransform(transform_train)
    return train_set
def gen_faker_card_run():
    aug_brightness = iaa.MultiplyBrightness((0.5, 1.))
    aug_gaussian =iaa.GaussianBlur((0, 2.0))

    # blur images with a sigma between 0 and 3.0
    csv_file = open(ori_csv_file, 'r', encoding='UTF-8')
    csv_reader_lines = list(csv.reader(csv_file))
    csv_reader_lines_train,csv_reader_lines_val=train_test_split(csv_reader_lines,test_size=0.000001, random_state=0)# 逐行读取csv文件
    date = []  # 创建列表准备接收csv各行数据
    cnt = 0  # 记录csv文件行数
    path='/home/ubuntu/hks/ocr/idcard_generator_project/idcard_generator/template/'
    files = os.listdir(os.path.join(path, 'fuzhiwuxiao_mask'))
    for one_line in csv_reader_lines_train:
            date.append(one_line)
            image_name=date[cnt][0]

            result_front = []
            result_back = []

            result_front.append(date[cnt][1])  # 姓名
            result_front.append(date[cnt][3])  # 性别
            result_front.append(date[cnt][2])  # 名族
            result_front.append(date[cnt][4])  # 年
            result_front.append(date[cnt][5])  # 月
            result_front.append(date[cnt][6])  # 日
            result_front.append(date[cnt][7])  # 地址
            result_front.append(date[cnt][8])  # 身份号

            result_back.append(date[cnt][9])  # 签发机关
            result_back.append(date[cnt][10])  # 有效日期

            image1 = cv2.imread(front_img)  # 读取正面模板
            image2 = cv2.imread(back_img)  # 读取背面模板

            #img_new_white1 = img_to_white(image1)
            img_new_white1 = image1
            # cv2.imshow('hjs',img_new_white1)
            # cv2.waitKey(0)# 生成画布
            img_res_f = gen_card_front(img_new_white1, result_front)
            img_res_f = cv2.cvtColor(img_res_f,cv2.COLOR_BGR2GRAY)
            # 写入文字
            #cv2.imwrite(result_card_path + '/{}_1.jpg'.format(image_name), img_res_f)

            #img_new_white2 = img_to_white(image2)
            img_new_white2 = image2
            img_res_b = gen_card_back(img_new_white2, result_back)
            img_res_b = cv2.cvtColor(img_res_b, cv2.COLOR_BGR2GRAY)
            #cv2.imwrite(result_card_path + '/{}_0.jpg'.format(image_name), img_res_b)
            cnt = cnt + 1
            print(cnt)

            for i in range(4):
                l = len(files)
                index = np.random.randint(0, l)

                image_gen_copy = img_res_f.copy()
                mask = cv2.imread(os.path.join(path + 'fuzhiwuxiao_mask', files[index]), 0)
                mask[mask > 150] = 255
                mask[mask <= 150] = 0
                image = cv2.imread(os.path.join(path + 'fuzhiwuxiao_template', files[index]), 0)
                image = cv2.blur(image, ksize=(5, 5))
                image = image + 20

                template = cv2.bitwise_and(image, image, mask=mask)

                h_image_gen, w_image_gen= img_res_f.shape
                point_x, point_y = h_image_gen, w_image_gen
                h, w = template.shape
                while point_x + h >= h_image_gen or point_y + w >= w_image_gen:
                    point_x, point_y = np.random.randint(0, h_image_gen), np.random.randint(0, w_image_gen)

                rect = image_gen_copy[point_x:(h + point_x), point_y:(w + point_y)]
                rect1 = cv2.bitwise_and(rect, rect, mask=mask)
                mask = cv2.bitwise_not(mask)
                rect2 = cv2.bitwise_and(rect, rect, mask=mask)
                image_with_logo = cv2.addWeighted(rect1, 0.3, template, 0.3, 0)
                add_logo = image_with_logo + rect2

                image_gen_copy_logo = image_gen_copy.copy()
                image_gen_copy_logo[point_x:(h + point_x), point_y:(w + point_y)] = add_logo

                image_gen_copy_logo=np.stack([image_gen_copy_logo,image_gen_copy_logo,image_gen_copy_logo],axis=2)
                aug_brightness_deterministic= aug_brightness.to_deterministic()
                aug_gaussian_deterministic=aug_gaussian.to_deterministic()
                image_gen_copy_logo=aug_brightness_deterministic.augment_images(images=[image_gen_copy_logo])[0]
                image_gen_copy_logo=aug_gaussian_deterministic.augment_images(images=[image_gen_copy_logo])[0]

                image_gen_copy = np.stack([image_gen_copy, image_gen_copy, image_gen_copy], axis=2)
                image_gen_copy = aug_brightness_deterministic.augment_images(images=[image_gen_copy])[0]
                image_gen_copy = aug_gaussian_deterministic.augment_images(images=[image_gen_copy])[0]
                #image_gen_copy=seq_nologo(image=np.stack([image_gen_copy,image_gen_copy,image_gen_copy],axis=2))

                train_data = np.hstack((image_gen_copy_logo,image_gen_copy))
                # cv2.imshow('image', train_data)
                # cv2.waitKey(300)
                #image_gen_copy[point_x:(h + point_x), point_y:(w + point_y)] = add_logo[:,:]
                if os.path.exists('/home/ubuntu/hks/ocr/idcard_generator_project/remove_logo_and_aug_image/train/' +
                            image_name)==False:
                    os.mkdir('/home/ubuntu/hks/ocr/idcard_generator_project/remove_logo_and_aug_image/train/' +
                                image_name)
                cv2.imwrite('/home/ubuntu/hks/ocr/idcard_generator_project/remove_logo_and_aug_image/train/' +image_name+'/'+
                            image_name +'_'+'1'+ '-' + str(i) + '.jpg', train_data)

            for i in range(4):
                l = len(files)
                index = np.random.randint(0, l)
                image_gen_copy = img_res_b.copy()
                mask = cv2.imread(os.path.join(path + 'fuzhiwuxiao_mask', files[index]), 0)
                mask[mask > 150] = 255
                mask[mask <= 150] = 0
                image = cv2.imread(os.path.join(path + 'fuzhiwuxiao_template', files[index]), 0)
                image = cv2.blur(image, ksize=(5, 5))
                image = image + 50
                template = cv2.bitwise_and(image, image, mask=mask)

                h_image_gen, w_image_gen = img_res_b.shape
                point_x, point_y = h_image_gen, w_image_gen
                h, w = template.shape
                while point_x + h >= h_image_gen or point_y + w >= w_image_gen:
                    l_point = len(point_select)
                    l_index = np.random.randint(0, l_point)
                    point_x, point_y = point_select[l_index]

                rect = image_gen_copy[point_x:(h + point_x), point_y:(w + point_y)]
                rect1 = cv2.bitwise_and(rect, rect, mask=mask)
                mask = cv2.bitwise_not(mask)
                rect2 = cv2.bitwise_and(rect, rect, mask=mask)
                image_with_logo = cv2.addWeighted(rect1, 0.3, template, 0.3, 0)
                add_logo = image_with_logo + rect2
                image_gen_copy_logo = image_gen_copy.copy()
                image_gen_copy_logo[point_x:(h + point_x), point_y:(w + point_y)] = add_logo

                image_gen_copy_logo=np.stack([image_gen_copy_logo,image_gen_copy_logo,image_gen_copy_logo],axis=2)
                aug_brightness_deterministic= aug_brightness.to_deterministic()
                aug_gaussian_deterministic=aug_gaussian.to_deterministic()
                image_gen_copy_logo=aug_brightness_deterministic.augment_images(images=[image_gen_copy_logo])[0]
                image_gen_copy_logo=aug_gaussian_deterministic.augment_images(images=[image_gen_copy_logo])[0]

                image_gen_copy = np.stack([image_gen_copy, image_gen_copy, image_gen_copy], axis=2)
                image_gen_copy = aug_brightness_deterministic.augment_images(images=[image_gen_copy])[0]
                image_gen_copy = aug_gaussian_deterministic.augment_images(images=[image_gen_copy])[0]

                train_data = np.hstack((image_gen_copy_logo,image_gen_copy))

                # cv2.imshow('template', template)
                # cv2.imshow('logo',image_with_logo)
                # cv2.imshow('image_gen',image_gen_copy)
                # #cv2.imshow('rect',rect2)
                # # cv2.imshow('image', train_data)
                # cv2.waitKey(300000)
                if os.path.exists('/home/ubuntu/hks/ocr/idcard_generator_project/remove_logo_and_aug_image/train/' +
                                  image_name) == False:
                        os.mkdir('/home/ubuntu/hks/ocr/idcard_generator_project/remove_logo_and_aug_image/train/' +
                                 image_name)
                cv2.imwrite('/home/ubuntu/hks/ocr/idcard_generator_project/remove_logo_and_aug_image/train/'+image_name+'/' +
                            image_name+'_'+'0'+ '-' + str(i) + '.jpg', train_data)
                del image_gen_copy_logo
    for one_line in csv_reader_lines_val:
        date.append(one_line)
        image_name=date[cnt][0]

        result_front = []
        result_back = []

        result_front.append(date[cnt][1])  # 姓名
        result_front.append(date[cnt][3])  # 性别
        result_front.append(date[cnt][2])  # 名族
        result_front.append(date[cnt][4])  # 年
        result_front.append(date[cnt][5])  # 月
        result_front.append(date[cnt][6])  # 日
        result_front.append(date[cnt][7])  # 地址
        result_front.append(date[cnt][8])  # 身份号

        result_back.append(date[cnt][9])  # 签发机关
        result_back.append(date[cnt][10])  # 有效日期

        image1 = cv2.imread(front_img)  # 读取正面模板
        image2 = cv2.imread(back_img)  # 读取背面模板

        #img_new_white1 = img_to_white(image1)
        img_new_white1 = image1
        # cv2.imshow('hjs',img_new_white1)
        # cv2.waitKey(0)# 生成画布
        img_res_f = gen_card_front(img_new_white1, result_front)
        img_res_f = cv2.cvtColor(img_res_f,cv2.COLOR_BGR2GRAY)
        # 写入文字
        #cv2.imwrite(result_card_path + '/{}_1.jpg'.format(image_name), img_res_f)

        #img_new_white2 = img_to_white(image2)
        img_new_white2 = image2
        img_res_b = gen_card_back(img_new_white2, result_back)
        img_res_b = cv2.cvtColor(img_res_b, cv2.COLOR_BGR2GRAY)
        #cv2.imwrite(result_card_path + '/{}_0.jpg'.format(image_name), img_res_b)
        cnt = cnt + 1
        print(cnt)

        for i in range(4):
            l = len(files)
            index = np.random.randint(0, l)

            image_gen_copy = img_res_f.copy()
            mask = cv2.imread(os.path.join(path + 'fuzhiwuxiao_mask', files[index]), 0)
            mask[mask > 150] = 255
            mask[mask <= 150] = 0
            image = cv2.imread(os.path.join(path + 'fuzhiwuxiao_template', files[index]), 0)
            template = cv2.bitwise_and(image, image, mask=mask)

            h_image_gen, w_image_gen= img_res_f.shape
            point_x, point_y = h_image_gen, w_image_gen
            h, w = template.shape
            while point_x + h >= h_image_gen or point_y + w >= w_image_gen:
                point_x, point_y = np.random.randint(0, h_image_gen), np.random.randint(0, w_image_gen)

            rect = image_gen_copy[point_x:(h + point_x), point_y:(w + point_y)]
            rect1 = cv2.bitwise_and(rect, rect, mask=mask)
            mask = cv2.bitwise_not(mask)
            rect2 = cv2.bitwise_and(rect, rect, mask=mask)
            image_with_logo = cv2.addWeighted(rect1, 0.3, template, 0.3, 0)
            add_logo = image_with_logo + rect2

            image_gen_copy_logo = image_gen_copy.copy()
            image_gen_copy_logo[point_x:(h + point_x), point_y:(w + point_y)] = add_logo

            image_gen_copy_logo = np.stack([image_gen_copy_logo, image_gen_copy_logo, image_gen_copy_logo], axis=2)
            aug_brightness_deterministic = aug_brightness.to_deterministic()
            aug_gaussian_deterministic = aug_gaussian.to_deterministic()
            image_gen_copy_logo = aug_brightness_deterministic.augment_images(images=[image_gen_copy_logo])[0]
            image_gen_copy_logo = aug_gaussian_deterministic.augment_images(images=[image_gen_copy_logo])[0]

            image_gen_copy = np.stack([image_gen_copy, image_gen_copy, image_gen_copy], axis=2)
            image_gen_copy = aug_brightness_deterministic.augment_images(images=[image_gen_copy])[0]
            #image_gen_copy = aug_gaussian_deterministic.augment_images(images=[image_gen_copy])[0]
            #image_gen_copy=seq_nologo(image=np.stack([image_gen_copy,image_gen_copy,image_gen_copy],axis=2))

            train_data = np.hstack((image_gen_copy_logo,image_gen_copy))
            #image_gen_copy[point_x:(h + point_x), point_y:(w + point_y)] = add_logo[:,:]
            if os.path.exists('/home/ubuntu/hks/ocr/idcard_generator_project/remove_logo_and_aug_image/val/' +
                     image_name) == False:
                os.mkdir('/home/ubuntu/hks/ocr/idcard_generator_project/remove_logo_and_aug_image/val/' +
                         image_name)
            cv2.imwrite('/home/ubuntu/hks/ocr/idcard_generator_project/remove_logo_and_aug_image/val/' +image_name+'/'+
                        image_name +'_'+'0'+ '-' + str(i) + '.jpg', train_data)

        for i in range(4):
            l = len(files)
            index = np.random.randint(0, l)
            image_gen_copy = img_res_b.copy()
            mask = cv2.imread(os.path.join(path + 'fuzhiwuxiao_mask', files[index]), 0)
            mask[mask > 150] = 255
            mask[mask <= 150] = 0
            image = cv2.imread(os.path.join(path + 'fuzhiwuxiao_template', files[index]), 0)
            template = cv2.bitwise_and(image, image, mask=mask)

            h_image_gen, w_image_gen = img_res_b.shape
            point_x, point_y = h_image_gen, w_image_gen
            h, w = template.shape
            while point_x + h >= h_image_gen or point_y + w >= w_image_gen:
                l_point = len(point_select)
                l_index = np.random.randint(0, l_point)
                point_x, point_y = point_select[l_index]

            rect = image_gen_copy[point_x:(h + point_x), point_y:(w + point_y)]
            rect1 = cv2.bitwise_and(rect, rect, mask=mask)
            mask = cv2.bitwise_not(mask)
            rect2 = cv2.bitwise_and(rect, rect, mask=mask)
            image_with_logo = cv2.addWeighted(rect1, 0.3, template, 0.3, 0)
            add_logo = image_with_logo + rect2
            image_gen_copy_logo = image_gen_copy.copy()
            image_gen_copy_logo[point_x:(h + point_x), point_y:(w + point_y)] = add_logo
            image_gen_copy_logo = np.stack([image_gen_copy_logo, image_gen_copy_logo, image_gen_copy_logo], axis=2)

            aug_brightness_deterministic = aug_brightness.to_deterministic()
            aug_gaussian_deterministic = aug_gaussian.to_deterministic()
            image_gen_copy_logo = aug_brightness_deterministic.augment_images(images=[image_gen_copy_logo])[0]
            image_gen_copy_logo = aug_gaussian_deterministic.augment_images(images=[image_gen_copy_logo])[0]

            image_gen_copy = np.stack([image_gen_copy, image_gen_copy, image_gen_copy], axis=2)
            image_gen_copy = aug_brightness_deterministic.augment_images(images=[image_gen_copy])[0]
            #image_gen_copy = aug_gaussian_deterministic.augment_images(images=[image_gen_copy])[0]
            train_data = np.hstack((image_gen_copy_logo,image_gen_copy))

            # cv2.imshow('template', template)
            # cv2.imshow('logo',image_with_logo)
            # cv2.imshow('image_gen',image_gen_copy)
            # #cv2.imshow('rect',rect2)
            # # cv2.imshow('image', train_data)
            # cv2.waitKey(300000)
            if os.path.exists('/home/ubuntu/hks/ocr/idcard_generator_project/remove_logo_and_aug_image/val/' +
                     image_name) == False:
                    os.mkdir('/home/ubuntu/hks/ocr/idcard_generator_project/remove_logo_and_aug_image/val/' +
                             image_name)
            cv2.imwrite('/home/ubuntu/hks/ocr/idcard_generator_project/remove_logo_and_aug_image/val/' +image_name+'/'+
                        image_name+'_'+'1'+ '-' + str(i) + '.jpg', train_data)
            del image_gen_copy_logo
Beispiel #25
0
save_dir_img = 'vitamin_aug3'
save_dir_seg = 'vitamin_aug3_seg'
save_dir_out = 'vitamin_aug3_out'

os.makedirs(save_dir_img, exist_ok=True)
os.makedirs(save_dir_seg, exist_ok=True)
os.makedirs(save_dir_out, exist_ok=True)

file_list_img = glob('%s/*.png' % load_dir_img)
file_list_seg = glob('%s/*.png' % load_dir_img)

aug1 = iaa.PerspectiveTransform(scale=(0, 0.15))
aug2 = iaa.Affine(scale={"x": (0.5, 1), "y": (0.5, 1)})
aug3 = iaa.Affine(translate_percent={"x": (-0.25, 0.25), "y": (-0.25, 0.25)})
aug4 = iaa.MultiplyBrightness((0.8, 1.2))
#aug4 = iaa.Affine(rotate=(-90, 90))
seq = iaa.Sequential([aug1, aug2, aug3])
aug_num = 20

img_ind = 1
for file_path in file_list_img:
    print('%d/%d' % (img_ind, len(file_list_img)))
    file_name = os.path.basename(file_path)
    img = cv2.imread('%s/%s' % (load_dir_img, file_name))
    seg = cv2.imread('%s/%s_0.png' % (load_dir_seg, file_name[:-4]))
    out = cv2.Canny(img, 30, 50)
    cv2.imwrite('%s/%s_aug00.png' % (save_dir_img, file_name[:-4]), img)
    cv2.imwrite('%s/%s_aug00.png' % (save_dir_seg, file_name[:-4]), seg)
    cv2.imwrite('%s/%s_aug00.png' % (save_dir_out, file_name[:-4]), out)
Beispiel #26
0
def create_augmenters(height, width, height_augmentable, width_augmentable, only_augmenters):
    def lambda_func_images(images, random_state, parents, hooks):
        return images

    def lambda_func_heatmaps(heatmaps, random_state, parents, hooks):
        return heatmaps

    def lambda_func_keypoints(keypoints, random_state, parents, hooks):
        return keypoints

    def assertlambda_func_images(images, random_state, parents, hooks):
        return True

    def assertlambda_func_heatmaps(heatmaps, random_state, parents, hooks):
        return True

    def assertlambda_func_keypoints(keypoints, random_state, parents, hooks):
        return True

    augmenters_meta = [
        iaa.Sequential([iaa.Noop(), iaa.Noop()], random_order=False, name="Sequential_2xNoop"),
        iaa.Sequential([iaa.Noop(), iaa.Noop()], random_order=True, name="Sequential_2xNoop_random_order"),
        iaa.SomeOf((1, 3), [iaa.Noop(), iaa.Noop(), iaa.Noop()], random_order=False, name="SomeOf_3xNoop"),
        iaa.SomeOf((1, 3), [iaa.Noop(), iaa.Noop(), iaa.Noop()], random_order=True, name="SomeOf_3xNoop_random_order"),
        iaa.OneOf([iaa.Noop(), iaa.Noop(), iaa.Noop()], name="OneOf_3xNoop"),
        iaa.Sometimes(0.5, iaa.Noop(), name="Sometimes_Noop"),
        iaa.WithChannels([1, 2], iaa.Noop(), name="WithChannels_1_and_2_Noop"),
        iaa.Identity(name="Identity"),
        iaa.Noop(name="Noop"),
        iaa.Lambda(func_images=lambda_func_images, func_heatmaps=lambda_func_heatmaps, func_keypoints=lambda_func_keypoints,
                   name="Lambda"),
        iaa.AssertLambda(func_images=assertlambda_func_images, func_heatmaps=assertlambda_func_heatmaps,
                         func_keypoints=assertlambda_func_keypoints, name="AssertLambda"),
        iaa.AssertShape((None, height_augmentable, width_augmentable, None), name="AssertShape"),
        iaa.ChannelShuffle(0.5, name="ChannelShuffle")
    ]
    augmenters_arithmetic = [
        iaa.Add((-10, 10), name="Add"),
        iaa.AddElementwise((-10, 10), name="AddElementwise"),
        #iaa.AddElementwise((-500, 500), name="AddElementwise"),
        iaa.AdditiveGaussianNoise(scale=(5, 10), name="AdditiveGaussianNoise"),
        iaa.AdditiveLaplaceNoise(scale=(5, 10), name="AdditiveLaplaceNoise"),
        iaa.AdditivePoissonNoise(lam=(1, 5), name="AdditivePoissonNoise"),
        iaa.Multiply((0.5, 1.5), name="Multiply"),
        iaa.MultiplyElementwise((0.5, 1.5), name="MultiplyElementwise"),
        iaa.Cutout(nb_iterations=1, name="Cutout-fill_constant"),
        iaa.Dropout((0.01, 0.05), name="Dropout"),
        iaa.CoarseDropout((0.01, 0.05), size_percent=(0.01, 0.1), name="CoarseDropout"),
        iaa.Dropout2d(0.1, name="Dropout2d"),
        iaa.TotalDropout(0.1, name="TotalDropout"),
        iaa.ReplaceElementwise((0.01, 0.05), (0, 255), name="ReplaceElementwise"),
        #iaa.ReplaceElementwise((0.95, 0.99), (0, 255), name="ReplaceElementwise"),
        iaa.SaltAndPepper((0.01, 0.05), name="SaltAndPepper"),
        iaa.ImpulseNoise((0.01, 0.05), name="ImpulseNoise"),
        iaa.CoarseSaltAndPepper((0.01, 0.05), size_percent=(0.01, 0.1), name="CoarseSaltAndPepper"),
        iaa.Salt((0.01, 0.05), name="Salt"),
        iaa.CoarseSalt((0.01, 0.05), size_percent=(0.01, 0.1), name="CoarseSalt"),
        iaa.Pepper((0.01, 0.05), name="Pepper"),
        iaa.CoarsePepper((0.01, 0.05), size_percent=(0.01, 0.1), name="CoarsePepper"),
        iaa.Invert(0.1, name="Invert"),
        # ContrastNormalization
        iaa.JpegCompression((50, 99), name="JpegCompression")
    ]
    augmenters_artistic = [
        iaa.Cartoon(name="Cartoon")
    ]
    augmenters_blend = [
        iaa.BlendAlpha((0.01, 0.99), iaa.Identity(), name="Alpha"),
        iaa.BlendAlphaElementwise((0.01, 0.99), iaa.Identity(), name="AlphaElementwise"),
        iaa.BlendAlphaSimplexNoise(iaa.Identity(), name="SimplexNoiseAlpha"),
        iaa.BlendAlphaFrequencyNoise((-2.0, 2.0), iaa.Identity(), name="FrequencyNoiseAlpha"),
        iaa.BlendAlphaSomeColors(iaa.Identity(), name="BlendAlphaSomeColors"),
        iaa.BlendAlphaHorizontalLinearGradient(iaa.Identity(), name="BlendAlphaHorizontalLinearGradient"),
        iaa.BlendAlphaVerticalLinearGradient(iaa.Identity(), name="BlendAlphaVerticalLinearGradient"),
        iaa.BlendAlphaRegularGrid(nb_rows=(2, 8), nb_cols=(2, 8), foreground=iaa.Identity(), name="BlendAlphaRegularGrid"),
        iaa.BlendAlphaCheckerboard(nb_rows=(2, 8), nb_cols=(2, 8), foreground=iaa.Identity(), name="BlendAlphaCheckerboard"),
        # TODO BlendAlphaSegMapClassId
        # TODO BlendAlphaBoundingBoxes
    ]
    augmenters_blur = [
        iaa.GaussianBlur(sigma=(1.0, 5.0), name="GaussianBlur"),
        iaa.AverageBlur(k=(3, 11), name="AverageBlur"),
        iaa.MedianBlur(k=(3, 11), name="MedianBlur"),
        iaa.BilateralBlur(d=(3, 11), name="BilateralBlur"),
        iaa.MotionBlur(k=(3, 11), name="MotionBlur"),
        iaa.MeanShiftBlur(spatial_radius=(5.0, 40.0), color_radius=(5.0, 40.0),
                          name="MeanShiftBlur")
    ]
    augmenters_collections = [
        iaa.RandAugment(n=2, m=(6, 12), name="RandAugment")
    ]
    augmenters_color = [
        # InColorspace (deprecated)
        iaa.WithColorspace(to_colorspace="HSV", children=iaa.Noop(), name="WithColorspace"),
        iaa.WithBrightnessChannels(iaa.Identity(), name="WithBrightnessChannels"),
        iaa.MultiplyAndAddToBrightness(mul=(0.7, 1.3), add=(-30, 30), name="MultiplyAndAddToBrightness"),
        iaa.MultiplyBrightness((0.7, 1.3), name="MultiplyBrightness"),
        iaa.AddToBrightness((-30, 30), name="AddToBrightness"),
        iaa.WithHueAndSaturation(children=iaa.Noop(), name="WithHueAndSaturation"),
        iaa.MultiplyHueAndSaturation((0.8, 1.2), name="MultiplyHueAndSaturation"),
        iaa.MultiplyHue((-1.0, 1.0), name="MultiplyHue"),
        iaa.MultiplySaturation((0.8, 1.2), name="MultiplySaturation"),
        iaa.RemoveSaturation((0.01, 0.99), name="RemoveSaturation"),
        iaa.AddToHueAndSaturation((-10, 10), name="AddToHueAndSaturation"),
        iaa.AddToHue((-10, 10), name="AddToHue"),
        iaa.AddToSaturation((-10, 10), name="AddToSaturation"),
        iaa.ChangeColorspace(to_colorspace="HSV", name="ChangeColorspace"),
        iaa.Grayscale((0.01, 0.99), name="Grayscale"),
        iaa.KMeansColorQuantization((2, 16), name="KMeansColorQuantization"),
        iaa.UniformColorQuantization((2, 16), name="UniformColorQuantization"),
        iaa.UniformColorQuantizationToNBits((1, 7), name="UniformQuantizationToNBits"),
        iaa.Posterize((1, 7), name="Posterize")
    ]
    augmenters_contrast = [
        iaa.GammaContrast(gamma=(0.5, 2.0), name="GammaContrast"),
        iaa.SigmoidContrast(gain=(5, 20), cutoff=(0.25, 0.75), name="SigmoidContrast"),
        iaa.LogContrast(gain=(0.7, 1.0), name="LogContrast"),
        iaa.LinearContrast((0.5, 1.5), name="LinearContrast"),
        iaa.AllChannelsCLAHE(clip_limit=(2, 10), tile_grid_size_px=(3, 11), name="AllChannelsCLAHE"),
        iaa.CLAHE(clip_limit=(2, 10), tile_grid_size_px=(3, 11), to_colorspace="HSV", name="CLAHE"),
        iaa.AllChannelsHistogramEqualization(name="AllChannelsHistogramEqualization"),
        iaa.HistogramEqualization(to_colorspace="HSV", name="HistogramEqualization"),
    ]
    augmenters_convolutional = [
        iaa.Convolve(np.float32([[0, 0, 0], [0, 1, 0], [0, 0, 0]]), name="Convolve_3x3"),
        iaa.Sharpen(alpha=(0.01, 0.99), lightness=(0.5, 2), name="Sharpen"),
        iaa.Emboss(alpha=(0.01, 0.99), strength=(0, 2), name="Emboss"),
        iaa.EdgeDetect(alpha=(0.01, 0.99), name="EdgeDetect"),
        iaa.DirectedEdgeDetect(alpha=(0.01, 0.99), name="DirectedEdgeDetect")
    ]
    augmenters_edges = [
        iaa.Canny(alpha=(0.01, 0.99), name="Canny")
    ]
    augmenters_flip = [
        iaa.Fliplr(1.0, name="Fliplr"),
        iaa.Flipud(1.0, name="Flipud")
    ]
    augmenters_geometric = [
        iaa.Affine(scale=(0.9, 1.1), translate_percent={"x": (-0.05, 0.05), "y": (-0.05, 0.05)}, rotate=(-10, 10),
                   shear=(-10, 10), order=0, mode="constant", cval=(0, 255), name="Affine_order_0_constant"),
        iaa.Affine(scale=(0.9, 1.1), translate_percent={"x": (-0.05, 0.05), "y": (-0.05, 0.05)}, rotate=(-10, 10),
                   shear=(-10, 10), order=1, mode="constant", cval=(0, 255), name="Affine_order_1_constant"),
        iaa.Affine(scale=(0.9, 1.1), translate_percent={"x": (-0.05, 0.05), "y": (-0.05, 0.05)}, rotate=(-10, 10),
                   shear=(-10, 10), order=3, mode="constant", cval=(0, 255), name="Affine_order_3_constant"),
        iaa.Affine(scale=(0.9, 1.1), translate_percent={"x": (-0.05, 0.05), "y": (-0.05, 0.05)}, rotate=(-10, 10),
                   shear=(-10, 10), order=1, mode="edge", cval=(0, 255), name="Affine_order_1_edge"),
        iaa.Affine(scale=(0.9, 1.1), translate_percent={"x": (-0.05, 0.05), "y": (-0.05, 0.05)}, rotate=(-10, 10),
                   shear=(-10, 10), order=1, mode="constant", cval=(0, 255), backend="skimage",
                   name="Affine_order_1_constant_skimage"),
        iaa.PiecewiseAffine(scale=(0.01, 0.05), nb_rows=4, nb_cols=4, order=1, mode="constant",
                            name="PiecewiseAffine_4x4_order_1_constant"),
        iaa.PiecewiseAffine(scale=(0.01, 0.05), nb_rows=4, nb_cols=4, order=0, mode="constant",
                            name="PiecewiseAffine_4x4_order_0_constant"),
        iaa.PiecewiseAffine(scale=(0.01, 0.05), nb_rows=4, nb_cols=4, order=1, mode="edge",
                            name="PiecewiseAffine_4x4_order_1_edge"),
        iaa.PiecewiseAffine(scale=(0.01, 0.05), nb_rows=8, nb_cols=8, order=1, mode="constant",
                            name="PiecewiseAffine_8x8_order_1_constant"),
        iaa.PerspectiveTransform(scale=(0.01, 0.05), keep_size=False, name="PerspectiveTransform"),
        iaa.PerspectiveTransform(scale=(0.01, 0.05), keep_size=True, name="PerspectiveTransform_keep_size"),
        iaa.ElasticTransformation(alpha=(1, 10), sigma=(0.5, 1.5), order=0, mode="constant", cval=0,
                                  name="ElasticTransformation_order_0_constant"),
        iaa.ElasticTransformation(alpha=(1, 10), sigma=(0.5, 1.5), order=1, mode="constant", cval=0,
                                  name="ElasticTransformation_order_1_constant"),
        iaa.ElasticTransformation(alpha=(1, 10), sigma=(0.5, 1.5), order=1, mode="nearest", cval=0,
                                  name="ElasticTransformation_order_1_nearest"),
        iaa.ElasticTransformation(alpha=(1, 10), sigma=(0.5, 1.5), order=1, mode="reflect", cval=0,
                                  name="ElasticTransformation_order_1_reflect"),
        iaa.Rot90((1, 3), keep_size=False, name="Rot90"),
        iaa.Rot90((1, 3), keep_size=True, name="Rot90_keep_size"),
        iaa.WithPolarWarping(iaa.Identity(), name="WithPolarWarping"),
        iaa.Jigsaw(nb_rows=(3, 8), nb_cols=(3, 8), max_steps=1, name="Jigsaw")
    ]
    augmenters_pooling = [
        iaa.AveragePooling(kernel_size=(1, 16), keep_size=False, name="AveragePooling"),
        iaa.AveragePooling(kernel_size=(1, 16), keep_size=True, name="AveragePooling_keep_size"),
        iaa.MaxPooling(kernel_size=(1, 16), keep_size=False, name="MaxPooling"),
        iaa.MaxPooling(kernel_size=(1, 16), keep_size=True, name="MaxPooling_keep_size"),
        iaa.MinPooling(kernel_size=(1, 16), keep_size=False, name="MinPooling"),
        iaa.MinPooling(kernel_size=(1, 16), keep_size=True, name="MinPooling_keep_size"),
        iaa.MedianPooling(kernel_size=(1, 16), keep_size=False, name="MedianPooling"),
        iaa.MedianPooling(kernel_size=(1, 16), keep_size=True, name="MedianPooling_keep_size")
    ]
    augmenters_imgcorruptlike = [
        iaa.imgcorruptlike.GaussianNoise(severity=(1, 5), name="imgcorruptlike.GaussianNoise"),
        iaa.imgcorruptlike.ShotNoise(severity=(1, 5), name="imgcorruptlike.ShotNoise"),
        iaa.imgcorruptlike.ImpulseNoise(severity=(1, 5), name="imgcorruptlike.ImpulseNoise"),
        iaa.imgcorruptlike.SpeckleNoise(severity=(1, 5), name="imgcorruptlike.SpeckleNoise"),
        iaa.imgcorruptlike.GaussianBlur(severity=(1, 5), name="imgcorruptlike.GaussianBlur"),
        iaa.imgcorruptlike.GlassBlur(severity=(1, 5), name="imgcorruptlike.GlassBlur"),
        iaa.imgcorruptlike.DefocusBlur(severity=(1, 5), name="imgcorruptlike.DefocusBlur"),
        iaa.imgcorruptlike.MotionBlur(severity=(1, 5), name="imgcorruptlike.MotionBlur"),
        iaa.imgcorruptlike.ZoomBlur(severity=(1, 5), name="imgcorruptlike.ZoomBlur"),
        iaa.imgcorruptlike.Fog(severity=(1, 5), name="imgcorruptlike.Fog"),
        iaa.imgcorruptlike.Frost(severity=(1, 5), name="imgcorruptlike.Frost"),
        iaa.imgcorruptlike.Snow(severity=(1, 5), name="imgcorruptlike.Snow"),
        iaa.imgcorruptlike.Spatter(severity=(1, 5), name="imgcorruptlike.Spatter"),
        iaa.imgcorruptlike.Contrast(severity=(1, 5), name="imgcorruptlike.Contrast"),
        iaa.imgcorruptlike.Brightness(severity=(1, 5), name="imgcorruptlike.Brightness"),
        iaa.imgcorruptlike.Saturate(severity=(1, 5), name="imgcorruptlike.Saturate"),
        iaa.imgcorruptlike.JpegCompression(severity=(1, 5), name="imgcorruptlike.JpegCompression"),
        iaa.imgcorruptlike.Pixelate(severity=(1, 5), name="imgcorruptlike.Pixelate"),
        iaa.imgcorruptlike.ElasticTransform(severity=(1, 5), name="imgcorruptlike.ElasticTransform")
    ]
    augmenters_pillike = [
        iaa.pillike.Solarize(p=1.0, threshold=(32, 128), name="pillike.Solarize"),
        iaa.pillike.Posterize((1, 7), name="pillike.Posterize"),
        iaa.pillike.Equalize(name="pillike.Equalize"),
        iaa.pillike.Autocontrast(name="pillike.Autocontrast"),
        iaa.pillike.EnhanceColor((0.0, 3.0), name="pillike.EnhanceColor"),
        iaa.pillike.EnhanceContrast((0.0, 3.0), name="pillike.EnhanceContrast"),
        iaa.pillike.EnhanceBrightness((0.0, 3.0), name="pillike.EnhanceBrightness"),
        iaa.pillike.EnhanceSharpness((0.0, 3.0), name="pillike.EnhanceSharpness"),
        iaa.pillike.FilterBlur(name="pillike.FilterBlur"),
        iaa.pillike.FilterSmooth(name="pillike.FilterSmooth"),
        iaa.pillike.FilterSmoothMore(name="pillike.FilterSmoothMore"),
        iaa.pillike.FilterEdgeEnhance(name="pillike.FilterEdgeEnhance"),
        iaa.pillike.FilterEdgeEnhanceMore(name="pillike.FilterEdgeEnhanceMore"),
        iaa.pillike.FilterFindEdges(name="pillike.FilterFindEdges"),
        iaa.pillike.FilterContour(name="pillike.FilterContour"),
        iaa.pillike.FilterEmboss(name="pillike.FilterEmboss"),
        iaa.pillike.FilterSharpen(name="pillike.FilterSharpen"),
        iaa.pillike.FilterDetail(name="pillike.FilterDetail"),
        iaa.pillike.Affine(scale=(0.9, 1.1),
                           translate_percent={"x": (-0.05, 0.05), "y": (-0.05, 0.05)},
                           rotate=(-10, 10),
                           shear=(-10, 10),
                           fillcolor=(0, 255),
                           name="pillike.Affine"),
    ]
    augmenters_segmentation = [
        iaa.Superpixels(p_replace=(0.05, 1.0), n_segments=(10, 100), max_size=64, interpolation="cubic",
                        name="Superpixels_max_size_64_cubic"),
        iaa.Superpixels(p_replace=(0.05, 1.0), n_segments=(10, 100), max_size=64, interpolation="linear",
                        name="Superpixels_max_size_64_linear"),
        iaa.Superpixels(p_replace=(0.05, 1.0), n_segments=(10, 100), max_size=128, interpolation="linear",
                        name="Superpixels_max_size_128_linear"),
        iaa.Superpixels(p_replace=(0.05, 1.0), n_segments=(10, 100), max_size=224, interpolation="linear",
                        name="Superpixels_max_size_224_linear"),
        iaa.UniformVoronoi(n_points=(250, 1000), name="UniformVoronoi"),
        iaa.RegularGridVoronoi(n_rows=(16, 31), n_cols=(16, 31), name="RegularGridVoronoi"),
        iaa.RelativeRegularGridVoronoi(n_rows_frac=(0.07, 0.14), n_cols_frac=(0.07, 0.14), name="RelativeRegularGridVoronoi"),
    ]
    augmenters_size = [
        iaa.Resize((0.8, 1.2), interpolation="nearest", name="Resize_nearest"),
        iaa.Resize((0.8, 1.2), interpolation="linear", name="Resize_linear"),
        iaa.Resize((0.8, 1.2), interpolation="cubic", name="Resize_cubic"),
        iaa.CropAndPad(percent=(-0.2, 0.2), pad_mode="constant", pad_cval=(0, 255), keep_size=False,
                       name="CropAndPad"),
        iaa.CropAndPad(percent=(-0.2, 0.2), pad_mode="edge", pad_cval=(0, 255), keep_size=False,
                       name="CropAndPad_edge"),
        iaa.CropAndPad(percent=(-0.2, 0.2), pad_mode="constant", pad_cval=(0, 255), name="CropAndPad_keep_size"),
        iaa.Pad(percent=(0.05, 0.2), pad_mode="constant", pad_cval=(0, 255), keep_size=False, name="Pad"),
        iaa.Pad(percent=(0.05, 0.2), pad_mode="edge", pad_cval=(0, 255), keep_size=False, name="Pad_edge"),
        iaa.Pad(percent=(0.05, 0.2), pad_mode="constant", pad_cval=(0, 255), name="Pad_keep_size"),
        iaa.Crop(percent=(0.05, 0.2), keep_size=False, name="Crop"),
        iaa.Crop(percent=(0.05, 0.2), name="Crop_keep_size"),
        iaa.PadToFixedSize(width=width+10, height=height+10, pad_mode="constant", pad_cval=(0, 255),
                           name="PadToFixedSize"),
        iaa.CropToFixedSize(width=width-10, height=height-10, name="CropToFixedSize"),
        iaa.KeepSizeByResize(iaa.CropToFixedSize(height=height-10, width=width-10), interpolation="nearest",
                             name="KeepSizeByResize_CropToFixedSize_nearest"),
        iaa.KeepSizeByResize(iaa.CropToFixedSize(height=height-10, width=width-10), interpolation="linear",
                             name="KeepSizeByResize_CropToFixedSize_linear"),
        iaa.KeepSizeByResize(iaa.CropToFixedSize(height=height-10, width=width-10), interpolation="cubic",
                             name="KeepSizeByResize_CropToFixedSize_cubic"),
    ]
    augmenters_weather = [
        iaa.FastSnowyLandscape(lightness_threshold=(100, 255), lightness_multiplier=(1.0, 4.0),
                               name="FastSnowyLandscape"),
        iaa.Clouds(name="Clouds"),
        iaa.Fog(name="Fog"),
        iaa.CloudLayer(intensity_mean=(196, 255), intensity_freq_exponent=(-2.5, -2.0), intensity_coarse_scale=10,
                       alpha_min=0, alpha_multiplier=(0.25, 0.75), alpha_size_px_max=(2, 8),
                       alpha_freq_exponent=(-2.5, -2.0), sparsity=(0.8, 1.0), density_multiplier=(0.5, 1.0),
                       name="CloudLayer"),
        iaa.Snowflakes(name="Snowflakes"),
        iaa.SnowflakesLayer(density=(0.005, 0.075), density_uniformity=(0.3, 0.9),
                            flake_size=(0.2, 0.7), flake_size_uniformity=(0.4, 0.8),
                            angle=(-30, 30), speed=(0.007, 0.03),
                            blur_sigma_fraction=(0.0001, 0.001), name="SnowflakesLayer"),
        iaa.Rain(name="Rain"),
        iaa.RainLayer(density=(0.03, 0.14),
                      density_uniformity=(0.8, 1.0),
                      drop_size=(0.01, 0.02),
                      drop_size_uniformity=(0.2, 0.5),
                      angle=(-15, 15),
                      speed=(0.04, 0.20),
                      blur_sigma_fraction=(0.001, 0.001),
                      name="RainLayer")
    ]

    augmenters = (
        augmenters_meta
        + augmenters_arithmetic
        + augmenters_artistic
        + augmenters_blend
        + augmenters_blur
        + augmenters_collections
        + augmenters_color
        + augmenters_contrast
        + augmenters_convolutional
        + augmenters_edges
        + augmenters_flip
        + augmenters_geometric
        + augmenters_pooling
        + augmenters_imgcorruptlike
        + augmenters_pillike
        + augmenters_segmentation
        + augmenters_size
        + augmenters_weather
    )

    if only_augmenters is not None:
        augmenters_reduced = []
        for augmenter in augmenters:
            if any([re.search(pattern, augmenter.name) for pattern in only_augmenters]):
                augmenters_reduced.append(augmenter)
        augmenters = augmenters_reduced

    return augmenters
Beispiel #27
0
 def __init__(self, Brightness_ratio=None):
     self.Brightness_ratio = Brightness_ratio
     self.seq = iaa.Sequential([iaa.MultiplyBrightness((0.95, 1.05))])
Beispiel #28
0
#heatmap =heatmap[:,:,:,0]
#plt.imshow(heatmap, cmap='gray')
#plt.show()


aug_list = iaa.OneOf([
<<<<<<< Updated upstream
  #iaa.Dropout([0.02, 0.1]),
  #iaa.Sharpen((0.0, 1.0)),
  iaa.MultiplyHue((0.7, 1.4)),
  #iaa.MultiplyBrightness((0.7, 1.4))
=======
  iaa.Dropout([0.02, 0.1]),
  iaa.Sharpen((0.0, 1.0)),
  iaa.MultiplyHue((0.7, 1.4)),
  iaa.MultiplyBrightness((0.7, 1.4))
>>>>>>> Stashed changes
])

aug = iaa.Sequential([aug_list, iaa.Fliplr(0.5)], random_order=True)

proc, hm= aug.augment(image=proc_image, heatmaps=heatmap)

hm = hm[0,:,:,0]
plt.imshow(hm, cmap='gray')
plt.show()

imageio.imwrite("example_segmaps.jpg", proc)


def gen_faker_card_run():
    path_save = '/home/simple/mydemo/ocr_project/idcard_generator_project/split_text_idcard/'

    path = '/home/simple/mydemo/ocr_project/idcard_generator_project/generator_datas1/'
    labels = csv.reader(
        open(
            '/home/simple/mydemo/ocr_project/idcard_generator_project/idcard_generator/src/generate_labels1.csv'
        ))
    # font_template = json.load(open(
    #     '/home/simple/mydemo/ocr_project/idcard_generator_project/idcard_generator/split_text_template/0adyypn1yq_1.json'))
    # xingming = font_template['shapes'][0]['points']
    # xingbie = font_template['shapes'][1]['points']
    # mingzhu = font_template['shapes'][2]['points']
    # chusheng = font_template['shapes'][3]['points']
    # dizhi = font_template['shapes'][4]['points']
    # shengfengzhenghao = font_template['shapes'][5]['points']
    # back_template = json.load(open(
    #     '/home/simple/mydemo/ocr_project/idcard_generator_project/idcard_generator/image_match/back1.json'))
    # qianfajiguang = back_template['shapes'][0]['points']
    # youxiaoqixian = back_template['shapes'][1]['points']

    font_template = json.load(
        open(
            '/home/simple/mydemo/ocr_project/idcard_generator_project/idcard_generator/image_match/front.json'
        ))
    xingming = font_template['shapes'][0]['points']
    xingbie = font_template['shapes'][1]['points']
    mingzhu = font_template['shapes'][2]['points']
    chusheng_year = font_template['shapes'][3]['points']
    chusheng_month = font_template['shapes'][4]['points']
    chusheng_day = font_template['shapes'][5]['points']
    dizhi_line1 = font_template['shapes'][6]['points']
    dizhi_line2 = font_template['shapes'][7]['points']
    dizhi_line3 = font_template['shapes'][8]['points']
    shengfengzhenghao = font_template['shapes'][9]['points']
    back_template = json.load(
        open(
            '/home/simple/mydemo/ocr_project/idcard_generator_project/idcard_generator/image_match/back1.json'
        ))
    qianfajiguang1 = back_template['shapes'][1]['points']
    qianfajiguang2 = back_template['shapes'][2]['points']
    youxiaoqixian = back_template['shapes'][0]['points']

    aug_brightness = iaa.MultiplyBrightness((0.5, 1.))
    aug_gaussian = iaa.GaussianBlur((0, 2.0))

    # blur images with a sigma between 0 and 3.0
    csv_file = open(ori_csv_file, 'r', encoding='UTF-8')
    csv_reader_lines = list(csv.reader(csv_file))
    csv_reader_lines_train, csv_reader_lines_val = train_test_split(
        csv_reader_lines, test_size=0.005, random_state=0)  # 逐行读取csv文件
    date = []  # 创建列表准备接收csv各行数据
    cnt = 0  # 记录csv文件行数
    path = '/home/simple/mydemo/ocr_project/idcard_generator_project/idcard_generator/template/'
    files = os.listdir(os.path.join(path, 'fuzhiwuxiao_mask'))
    for one_line in csv_reader_lines_train:
        date.append(one_line)
        image_name = date[cnt][0]

        result_front = []
        result_back = []

        result_front.append(date[cnt][1])  # 姓名
        result_front.append(date[cnt][3])  # 性别
        result_front.append(date[cnt][2])  # 名族
        result_front.append(date[cnt][4])  # 年
        result_front.append(date[cnt][5])  # 月
        result_front.append(date[cnt][6])  # 日
        result_front.append(date[cnt][7])  # 地址
        result_front.append(date[cnt][8])  # 身份号

        result_back.append(date[cnt][9])  # 签发机关
        result_back.append(date[cnt][10])  # 有效日期

        image1 = cv2.imread(front_img)  # 读取正面模板
        image2 = cv2.imread(back_img)  # 读取背面模板

        #img_new_white1 = img_to_white(image1)
        img_new_white1 = image1
        # cv2.imshow('hjs',img_new_white1)
        # cv2.waitKey(0)# 生成画布
        img_res_f = gen_card_front(img_new_white1, result_front)
        img_res_f = cv2.cvtColor(img_res_f, cv2.COLOR_BGR2GRAY)
        # 写入文字
        #cv2.imwrite(result_card_path + '/{}_1.jpg'.format(image_name), img_res_f)

        #img_new_white2 = img_to_white(image2)
        img_new_white2 = image2
        img_res_b = gen_card_back(img_new_white2, result_back)
        img_res_b = cv2.cvtColor(img_res_b, cv2.COLOR_BGR2GRAY)
        #cv2.imwrite(result_card_path + '/{}_0.jpg'.format(image_name), img_res_b)
        cnt = cnt + 1
        print(cnt)
        # os.mkdir(path_save + image_name)
        for i in range(4):
            l = len(files)
            index = np.random.randint(0, l)
            image_gen_copy = img_res_f.copy()
            aug_brightness_deterministic = aug_brightness.to_deterministic()
            aug_gaussian_deterministic = aug_gaussian.to_deterministic()

            image_gen_copy = np.stack(
                [image_gen_copy, image_gen_copy, image_gen_copy], axis=2)
            image_gen_copy = aug_brightness_deterministic.augment_images(
                images=[image_gen_copy])[0]
            image_gen_copy = aug_gaussian_deterministic.augment_images(
                images=[image_gen_copy])[0][:, :, 0]

            xingming_rect = image_gen_copy[
                int(xingming[0][1]):int(xingming[1][1]),
                int(xingming[0][0]):int(xingming[1][0])]
            xingbie_rect = image_gen_copy[
                int(xingbie[0][1]):int(xingbie[1][1]),
                int(xingbie[0][0]):int(xingbie[1][0])]
            mingzhu_rect = image_gen_copy[
                int(mingzhu[0][1]):int(mingzhu[1][1]),
                int(mingzhu[0][0]):int(mingzhu[1][0])]

            chusheng_rect_year = image_gen_copy[
                int(chusheng_year[0][1]):int(chusheng_year[1][1]),
                int(chusheng_year[0][0]):int(chusheng_year[1][0])]
            chusheng_rect_month = image_gen_copy[
                int(chusheng_month[0][1]):int(chusheng_month[1][1]),
                int(chusheng_month[0][0]):int(chusheng_month[1][0])]
            chusheng_rect_day = image_gen_copy[
                int(chusheng_day[0][1]):int(chusheng_day[1][1]),
                int(chusheng_day[0][0]):int(chusheng_day[1][0])]
            dizhi_rect_line1 = image_gen_copy[
                int(dizhi_line1[0][1]):int(dizhi_line1[1][1]),
                int(dizhi_line1[0][0]):int(dizhi_line1[1][0])]
            dizhi_rect_line2 = image_gen_copy[
                int(dizhi_line2[0][1] + 2):int(dizhi_line2[1][1]),
                int(dizhi_line2[0][0]):int(dizhi_line2[1][0])]
            dizhi_rect_line3 = image_gen_copy[
                int(dizhi_line3[0][1] + 3):int(dizhi_line3[1][1]),
                int(dizhi_line3[0][0]):int(dizhi_line3[1][0])]
            shengfengzhenghao_rect = image_gen_copy[
                int(shengfengzhenghao[0][1]):int(shengfengzhenghao[1][1]),
                int(shengfengzhenghao[0][0] + 5):int(shengfengzhenghao[1][0])]

            cv2.imshow('xingming_roi', xingming_rect)
            cv2.imshow('xingbie_roi', xingbie_rect)
            cv2.imshow('mingzhu_roi', mingzhu_rect)
            cv2.imshow('chusheng_year_roi', chusheng_rect_year)
            cv2.imshow('chusheng_month_roi', chusheng_rect_month)
            cv2.imshow('chusheng_day_roi', chusheng_rect_day)
            cv2.imshow('dizhi_line1_roi', dizhi_rect_line1)
            cv2.imshow('dizhi_line2_roi', dizhi_rect_line2)
            cv2.imshow('dizhi_line3_roi', dizhi_rect_line3)
            cv2.imshow('shengfengzhenghao_roi', shengfengzhenghao_rect)
            cv2.waitKey(1000)

            # cv2.imwrite(path_save+image_name+'/'+'xingming_rect'+'-'+str(i)+'.jpg',xingming_rect)
            # cv2.imwrite(path_save + image_name + '/' + 'dizhi_rect' + '-' + str(i) + '.jpg', dizhi_rect)
            # cv2.imwrite(path_save + image_name + '/' + 'xingbie_rect' + '-' + str(i) + '.jpg', xingbie_rect)
            # cv2.imwrite(path_save + image_name + '/' + 'mingzhu_rect' + '-' + str(i) + '.jpg', mingzhu_rect)
            # cv2.imwrite(path_save + image_name + '/' + 'shengfengzhenghao_rect' + '-' + str(i) + '.jpg', shengfengzhenghao_rect)
            # cv2.imwrite(path_save + image_name + '/' + 'chusheng_rect_year' + '-' + str(i) + '.jpg', chusheng_rect_year)
            # cv2.imwrite(path_save + image_name + '/' + 'chusheng_rect_month' + '-' + str(i) + '.jpg',
            #             chusheng_rect_month)
            # cv2.imwrite(path_save + image_name + '/' + 'chusheng_rect_day' + '-' + str(i) + '.jpg',
            #             chusheng_rect_day)

        for i in range(4):
            l = len(files)
            index = np.random.randint(0, l)
            image_gen_copy = img_res_b.copy()

            aug_brightness_deterministic = aug_brightness.to_deterministic()
            aug_gaussian_deterministic = aug_gaussian.to_deterministic()
            image_gen_copy = np.stack(
                [image_gen_copy, image_gen_copy, image_gen_copy], axis=2)
            image_gen_copy = aug_brightness_deterministic.augment_images(
                images=[image_gen_copy])[0]
            image_gen_copy = aug_gaussian_deterministic.augment_images(
                images=[image_gen_copy])[0][:, :, 0]

            qianfajiguang1_roi = image_gen_copy[
                int(qianfajiguang1[0][1]):int(qianfajiguang1[1][1]),
                int(qianfajiguang1[0][0]):int(qianfajiguang1[1][0])]
            qianfajiguang2_roi = image_gen_copy[
                int(qianfajiguang2[0][1] - 3):int(qianfajiguang2[1][1] - 3),
                int(qianfajiguang2[0][0]):int(qianfajiguang2[1][0])]
            youxiaoqixian_roi = image_gen_copy[
                int(youxiaoqixian[0][1]):int(youxiaoqixian[1][1]),
                int(youxiaoqixian[0][0]):int(youxiaoqixian[1][0])]
            cv2.imshow('qianfajiguang1_roi', qianfajiguang1_roi)
            cv2.imshow('qianfajiguang2_roi', qianfajiguang2_roi)
            cv2.imshow('youxiaoqixian_roi', youxiaoqixian_roi)
            cv2.waitKey(1000)
Beispiel #30
0
    def __getitem__(self, index):
        assert index <= self.nSamples, 'index range error'

        index += 1

        try:
            img_root_i = osp.join(self.data_root, str(index) + '.txt')
            with open(img_root_i, 'r') as f:
                lines = f.readlines()
            js_p = lines[0].strip()
            img_p = lines[1].strip()
            img = Image.open(img_p)
            img = img.convert("RGB")
        except IOError:
            print('Corrupted image for %d' % index)
            return self[index + 1]

        ori_w, ori_h = img.size
        img = img.resize((self.width, self.height))
        try:
            pt = fileutils.red_json(js_p)
            pt[0] = pt[0] / ori_w * self.width
            pt[1] = pt[1] / ori_h * self.height
        except IndexError:
            return self[index + 1]
        pt_x = pt[0]
        pt_y = pt[1]

        # fileutils.test_point(img,pt[0],pt[1],index,tag='ori')

        if self.is_train:
            self.augment = iaa.Sequential([
                iaa.MotionBlur(k=5, angle=45),
                iaa.CropAndPad(percent=(-0.2, 0.2), pad_mode="edge"),
                iaa.AdditiveGaussianNoise(scale=(0, 0.05 * 255)),
                iaa.SaltAndPepper(0.2, per_channel=True),
                iaa.AddToHueAndSaturation((-60, 60)),
                iaa.MultiplyBrightness((0.5, 1.5)),
                iaa.Affine(rotate=(-180, 180)),
                iaa.pillike.EnhanceSharpness(),
                iaa.pillike.EnhanceColor(),
                iaa.Dropout(p=(0, 0.1))
            ],
                                          random_order=True)

            kps = [
                ia.Keypoint(x=pt[0], y=pt[1]),
            ]

            if random.uniform(0, 1) < 0.2:
                pt_x = pt[0]
                pt_y = pt[1]
            else:
                img = np.asarray(img, dtype=np.uint8)

                kpsoi = ia.KeypointsOnImage(kps, shape=img.shape)
                aug_det = self.augment.to_deterministic()
                img = aug_det.augment_image(img)
                pt_aug = aug_det.augment_keypoints(kpsoi)

                # img = self.augment(image = img)
                img = Image.fromarray(img)
                pt_x = pt_aug[0].x_int
                pt_y = pt_aug[0].y_int

                # fileutils.test_point(img,pt_x,pt_y,index)
                # img.save('seeee_'+str(index)+'_.jpg')

        if self.transform is not None:
            img = self.transform(img)

        return img, pt_x, pt_y