def augment(self, batch_input):
     batch = []
     for sample in batch_input:
         aug_func = random.choice([0, 1, 1, 2, 3, 3])
         if aug_func == 0:
             aug_sample = sample.numpy()
         elif aug_func == 1:
             aug_sample = distort(sample.numpy(), 4)
         elif aug_func == 2:
             aug_sample = stretch(sample.numpy(), 4)
         elif aug_func == 3:
             aug_sample = perspective(sample.numpy())
         batch.append(aug_sample)
     return np.array(batch)
def augmentation(data_input, output):
    """
        Run augmentor for text augmentation
    """
    lines = open('{}/labels.txt'.format(data_input), 'r').readlines()
    image_dirs = os.path.join(output, 'images')
    imgs = os.path.join(data_input, 'images')
    check_exits(image_dirs)
    augment = [2, 3, 4, 5]
    with open('{}/labels.txt'.format(output), 'w') as f:
        for line in tqdm.tqdm(lines):
            distort_aug = random.choice(augment)
            stretch_aug = random.choice(augment)
            try:
                filename, label = line.strip().split('\t', 1)
                name = os.path.splitext(filename)[0]
                img_path = os.path.join(imgs, filename)
                im = cv2.imread(img_path)
                im = cv2.resize(im, (32 * 3, 32))

                for i in range(20):
                    distort_img = distort(im, distort_aug)
                    cv2.imwrite(
                        '{}/distort_{}_{}.jpg'.format(image_dirs, name, i),
                        distort_img)
                    f.write('distort_{}_{}.jpg'.format(name, i))
                    f.write('\t')
                    f.write(label)
                    f.write('\n')

                    stretch_img = stretch(im, stretch_aug)
                    cv2.imwrite(
                        '{}/stretch_{}_{}.jpg'.format(image_dirs, name, i),
                        stretch_img)
                    f.write('stretch_{}_{}.jpg'.format(name, i))
                    f.write('\t')
                    f.write(label)
                    f.write('\n')

                    perspective_img = perspective(im)
                    cv2.imwrite(
                        '{}/perspec_{}_{}.jpg'.format(image_dirs, name, i),
                        perspective_img)
                    f.write('perspec_{}_{}.jpg'.format(name, i))
                    f.write('\t')
                    f.write(label)
                    f.write('\n')
            except Exception as e:
                continue
Example #3
0
    def __getitem__(self, index):
        assert index <= len(self), 'index range error'
        index = self.filtered_index_list[index]

        with self.env.begin(write=False) as txn:
            label_key = 'label-%09d'.encode() % index
            label = txn.get(label_key).decode('utf-8')
            img_key = 'image-%09d'.encode() % index
            imgbuf = txn.get(img_key)

            buf = six.BytesIO()
            buf.write(imgbuf)
            buf.seek(0)
            try:
                if self.opt.rgb:
                    img = Image.open(buf).convert('RGB')  # for color image
                else:
                    img = Image.open(buf).convert('L')

            except IOError:
                print(f'Corrupted image for {index}')
                # make dummy image and dummy label for corrupted image.
                if self.opt.rgb:
                    img = Image.new('RGB', (self.opt.imgW, self.opt.imgH))
                else:
                    img = Image.new('L', (self.opt.imgW, self.opt.imgH))
                label = '[dummy_label]'

            if not self.opt.sensitive:
                label = label.lower()

            # We only train and evaluate on alphanumerics (or pre-defined character set in train.py)
            out_of_char = f'[^{self.opt.character}]'
            label = re.sub(out_of_char, '', label)
        
        if self.opt.data_augment:
            imgArray = np.asarray(img)
            if torch.rand(1) < 0.3:
                imgArray = augment.distort(imgArray, random.randint(3, 8))
            if torch.rand(1) < 0.3:
                imgArray = augment.stretch(imgArray, random.randint(3, 8))
            img = Image.fromarray(np.uint8(imgArray))

        return (img, label)
Example #4
0
def generate_variations():
    file_paths = glob.glob(input_images_dir +
                           '/*.jpg')  # search for all jpg images in the folder
    counter = 1
    # percorre todos os arquivos de imagem da base original
    for filepath in file_paths:
        filename = os.path.basename(filepath)

        # verifica quais fazem parte da base de treinamento
        if filename in filenames_for_training:
            print(counter)  # apenas para controle do tempo de execucao
            index_filename = filenames_for_training.index(filename)
            filename_without_extension = filename.split('.')[0]
            im = cv2.imread(filepath)
            # para cinco níveis (2 a 11, números primos), gera novas imagens
            parameters = (2, 3, 5, 7, 11)
            for i in parameters:
                # gera e salva imagem distorcida
                distort_img = distort(im, i)
                new_filename = filename_without_extension + '-distort-' + str(
                    counter) + '.jpg'
                cv2.imwrite(images_dir + '/' + new_filename, distort_img)
                adiciona_rotulo(classes_month[index_filename], new_filename)
                counter = counter + 1

                # gera e salva imagem esticada
                stretch_img = stretch(im, i)
                new_filename = filename_without_extension + '-distort-' + str(
                    counter) + '.jpg'
                cv2.imwrite(images_dir + '/' + new_filename, stretch_img)
                adiciona_rotulo(classes_month[index_filename], new_filename)
                counter = counter + 1

            # gera e salva imagem em perspectiva
            perspective_img = perspective(im)
            new_filename = filename_without_extension + '-perspective-' + str(
                counter) + '.jpg'
            cv2.imwrite(images_dir + '/' + new_filename, perspective_img)
            adiciona_rotulo(classes_month[index_filename], new_filename)
            counter = counter + 1
    for image in image_list:
        frames.append(image)
    imageio.mimsave(gif_name, frames, 'GIF', duration=duration)
    return


if __name__ == '__main__':
    im = cv2.imread("imgs/demo.png")
    im = cv2.resize(im, (200, 64))
    cv2.imshow("im_CV", im)
    distort_img_list = list()
    stretch_img_list = list()
    perspective_img_list = list()
    for i in range(12):
        distort_img = distort(im, 4)
        distort_img_list.append(distort_img)
        cv2.imshow("distort_img", distort_img)

        stretch_img = stretch(im, 4)
        cv2.imshow("stretch_img", stretch_img)
        stretch_img_list.append(stretch_img)

        perspective_img = perspective(im)
        cv2.imshow("perspective_img", perspective_img)
        perspective_img_list.append(perspective_img)
        cv2.waitKey(100)

    create_gif(distort_img_list, r'imgs/distort.gif')
    create_gif(stretch_img_list, r'imgs/stretch.gif')
    create_gif(perspective_img_list, r'imgs/perspective.gif')