def augment_image(image_num_pair, args):
    filepath, n = image_num_pair
    img = cv2.imread(filepath)
    filename = filepath.split(os.sep)[-1]
    dot_pos = filename.rfind('.')
    imgname = filename[:dot_pos]
    ext = filename[dot_pos:]

    print('Augmenting {} ...'.format(filename))
    for i in range(n):
        img_varied = img.copy()
        varied_imgname = '{}_{:0>3d}_'.format(imgname, i)
        if random.random() < args.p_mirror:
            img_varied = cv2.flip(img_varied, 1)
            varied_imgname += 'm'
        if random.random() < args.p_crop:
            img_varied = ia.random_crop(img_varied, args.crop_size,
                                        args.crop_hw_vari)
            varied_imgname += 'c'
        if random.random() < args.p_rotate:
            img_varied = ia.random_rotate(img_varied, args.rotate_angle_vari,
                                          args.p_rotate_crop)
            varied_imgname += 'r'
        if random.random() < args.p_hsv:
            img_varied = ia.random_hsv_transform(img_varied, args.hue_vari,
                                                 args.sat_vari, args.val_vari)
            varied_imgname += 'h'
        if random.random() < args.p_gamma:
            img_varied = ia.random_gamma_transform(img_varied, args.gamma_vari)
            varied_imgname += 'g'
        output_filepath = os.sep.join(
            [args.output_dir, '{}{}'.format(varied_imgname, ext)])
        cv2.imwrite(output_filepath, img_varied)
def augment_images(filelist, args):
    # 遍历所有列表内的文件
    for filepath, n in filelist:
        img = cv2.imread(filepath)
        filename = filepath.split(os.sep)[-1]
        dot_pos = filename.rfind('.')

        # 获取文件名和后缀名
        imgname = filename[:dot_pos]
        ext = filename[dot_pos:]

        print('Augmenting {} ...'.format(filename))
        for i in range(n):
            img_varied = img.copy()

            # 扰动后文件名的前缀
            varied_imgname = '{}_{:0>3d}_'.format(imgname, i)

            # 按照比例随机对图像进行镜像
            if random.random() < args.p_mirror:
                # 利用numpy.fliplr(img_varied)也能实现
                img_varied = cv2.flip(img_varied, 1)
                varied_imgname += 'm'

            # 按照比例随机对图像进行裁剪
            if random.random() < args.p_crop:
                img_varied = ia.random_crop(img_varied, args.crop_size,
                                            args.crop_hw_vari)
                varied_imgname += 'c'

            # 按照比例随机对图像进行旋转
            if random.random() < args.p_rotate:
                img_varied = ia.random_rotate(img_varied,
                                              args.rotate_angle_vari,
                                              args.p_rotate_crop)
                varied_imgname += 'r'

            # 按照比例随机对图像进行HSV扰动
            if random.random() < args.p_hsv:
                img_varied = ia.random_hsv_transform(img_varied, args.hue_vari,
                                                     args.sat_vari,
                                                     args.val_vari)
                varied_imgname += 'h'

            # 按照比例随机对图像进行Gamma扰动
            if random.random() < args.p_gamma:
                img_varied = ia.random_gamma_transform(img_varied,
                                                       args.gamma_vari)
                varied_imgname += 'g'

            # 生成扰动后的文件名并保存在指定的路径
            output_filepath = os.sep.join(
                [args.output_dir, '{}{}'.format(varied_imgname, ext)])
            cv2.imwrite(output_filepath, img_varied)
Пример #3
0
def random_augmentation(image):
    if random.random() < 0.0:
        return image

    # 按照比例随机对图像进行镜像
    img_varied = random_flip(image)

    # 随机亮度
    if random.random() < 0.5:
        t = random.randint(-100, 100)
        blank = np.zeros(image.shape, image.dtype)
        img_varied = cv2.addWeighted(img_varied, 1, blank, 1, t)

    # 随机对比度
    if random.random() < 0.5:
        t = random.randint(-100, 100)
        blank = np.zeros(image.shape, image.dtype)
        img_varied = cv2.addWeighted(img_varied, 1 + t / 100.0, blank,
                                     1 - t / 100.0, 0)

    # 按照比例随机对图像进行HSV扰动
    if random.random() < 1:
        img_varied = ia.random_hsv_transform(img_varied, 10, 0.1, 0.1)

    # 按照比例随机对图像进行Gamma扰动
    if random.random() < 0.5:
        img_varied = ia.random_gamma_transform(img_varied, 2.0)
    return img_varied


# files = glob.glob("./data/image/mouth/pos/*.png")
# random.shuffle(files)
# c_image = cv2.imread(files[0])
# cv2.imshow("o",c_image)
# imgae = random_augmentation(c_image)
# cv2.imshow("a",imgae)
# cv2.waitKey(0)

# # 按照比例随机对图像进行裁剪
# if random.random() < 1:
#     img_varied = ia.random_crop(
#         img_varied,
#         0.1,
#         0.8)

# # 按照比例随机对图像进行旋转
# if random.random() < 1.0:
#     img_varied = ia.random_rotate(
#         img_varied,
#         1.0,
#         10.0)
def augment_image(image_num_pair, args):
    filepath, n = image_num_pair
    img = cv2.imread(filepath)
    filename = filepath.split(os.sep)[-1]
    dot_pos = filename.rfind('.')
    imgname = filename[:dot_pos]
    ext = filename[dot_pos:]

    print('Augmenting {} ...'.format(filename))
    for i in range(n):
        img_varied = img.copy()
        varied_imgname = '{}_{:0>3d}_'.format(imgname, i)
        if random.random() < args.p_mirror:
            img_varied = cv2.flip(img_varied, 1)
            varied_imgname += 'm'
        if random.random() < args.p_crop:
            img_varied = ia.random_crop(
                img_varied,
                args.crop_size,
                args.crop_hw_vari)
            varied_imgname += 'c'
        if random.random() < args.p_rotate:
            img_varied = ia.random_rotate(
                img_varied,
                args.rotate_angle_vari,
                args.p_rotate_crop)
            varied_imgname += 'r'
        if random.random() < args.p_hsv:
            img_varied = ia.random_hsv_transform(
                img_varied,
                args.hue_vari,
                args.sat_vari,
                args.val_vari)
            varied_imgname += 'h'
        if random.random() < args.p_gamma:
            img_varied = ia.random_gamma_transform(
                img_varied,
                args.gamma_vari)
            varied_imgname += 'g'
        output_filepath = os.sep.join([
            args.output_dir,
            '{}{}'.format(varied_imgname, ext)])
        cv2.imwrite(output_filepath, img_varied)
Пример #5
0
def augment_images(filelist, args):
    for filepath, n in filelist:
        filepath = filepath.replace('\\', '/')
        img = cv_imread(filepath)
        #print(filepath)
        #print(img)
        filename = filepath.split('/')[-1]
        dot_pos = filename.rfind('.')
        imgname = filename[:dot_pos]
        ext = filename[dot_pos:]
        #print(ext, imgname,filename)

        print('Augmenting {} ...'.format(filename))
        for i in range(n):
            img_varied = img.copy()
            varied_imgname = '{}_{:0>3d}_'.format(imgname, i)
            if random.random() < args.p_mirror:
                img_varied = cv.flip(img_varied, 1)
                varied_imgname += 'm'
            if random.random() < args.p_crop:
                img_varied = ia.random_crop(img_varied, args.crop_size,
                                            args.crop_hw_vari)
                varied_imgname += 'c'
            if random.random() < args.p_rotate:
                img_varied = ia.random_rotate(img_varied,
                                              args.rotate_angle_vari,
                                              args.p_rotate_crop)
                varied_imgname += 'r'
            if random.random() < args.p_hsv:
                img_varied = ia.random_hsv_transform(img_varied, args.hue_vari,
                                                     args.sat_vari,
                                                     args.val_vari)
                varied_imgname += 'h'
            if random.random() < args.p_gamma:
                img_varied = ia.random_gamma_transform(img_varied,
                                                       args.gamma_vari)
                varied_imgname += 'g'
            output_filepath = os.sep.join(
                [args.output_dir, '{}{}'.format(varied_imgname,
                                                ext)]).replace('\\', '/')
            #cv.imwrite(output_filepath, img_varied)

            cv.imencode('.png', img_varied)[1].tofile(output_filepath)
Пример #6
0
def generator(input_size=512,
              batch_size=32,
              background_ratio=3. / 8,
              random_scale=np.array([0.5, 1, 2.0, 3.0]),
              vis=False):
    image_list = np.array(get_images())
    print('{} training images in {}'.format(image_list.shape[0],
                                            FLAGS.training_data_path))
    index = np.arange(0, image_list.shape[0])
    while True:
        np.random.shuffle(index)
        images = []
        image_fns = []
        score_maps = []
        geo_maps = []
        training_masks = []
        for i in index:
            try:
                im_fn = image_list[i]
                im = cv2.imread(im_fn)

                #augmentation  1
                im = random_hsv_transform(im, 10, 0.5, 0.5)

                # print im_fn
                h, w, _ = im.shape
                txt_fn = im_fn.replace(
                    os.path.basename(im_fn).split('.')[1], 'txt')
                if not os.path.exists(txt_fn):
                    print('text file {} does not exists'.format(txt_fn))
                    continue

                text_polys, text_tags = load_annoataion(txt_fn)
                #augmentation  2
                im, text_polys = random_flip_left_right(im, text_polys)

                text_polys, text_tags = check_and_validate_polys(
                    text_polys, text_tags, (h, w))
                # if text_polys.shape[0] == 0:
                #     continue
                # random scale this image
                rd_scale = np.random.choice(random_scale)
                im = cv2.resize(im, dsize=None, fx=rd_scale, fy=rd_scale)
                text_polys *= rd_scale
                # print rd_scale
                # random crop a area from image
                if np.random.rand() < background_ratio:
                    # crop background
                    im, text_polys, text_tags = crop_area(im,
                                                          text_polys,
                                                          text_tags,
                                                          crop_background=True)
                    if text_polys.shape[0] > 0:
                        # cannot find background
                        continue
                    # pad and resize image
                    new_h, new_w, _ = im.shape
                    max_h_w_i = np.max([new_h, new_w, input_size])
                    im_padded = np.zeros((max_h_w_i, max_h_w_i, 3),
                                         dtype=np.uint8)
                    im_padded[:new_h, :new_w, :] = im.copy()
                    im = cv2.resize(im_padded, dsize=(input_size, input_size))
                    score_map = np.zeros((input_size, input_size),
                                         dtype=np.uint8)
                    geo_map_channels = 5 if FLAGS.geometry == 'RBOX' else 8
                    geo_map = np.zeros(
                        (input_size, input_size, geo_map_channels),
                        dtype=np.float32)
                    training_mask = np.ones((input_size, input_size),
                                            dtype=np.uint8)
                else:
                    im, text_polys, text_tags = crop_area(
                        im, text_polys, text_tags, crop_background=False)
                    if text_polys.shape[0] == 0:
                        continue
                    h, w, _ = im.shape

                    # pad the image to the training input size or the longer side of image
                    new_h, new_w, _ = im.shape
                    max_h_w_i = np.max([new_h, new_w, input_size])
                    im_padded = np.zeros((max_h_w_i, max_h_w_i, 3),
                                         dtype=np.uint8)
                    im_padded[:new_h, :new_w, :] = im.copy()
                    im = im_padded
                    # resize the image to input size
                    new_h, new_w, _ = im.shape
                    resize_h = input_size
                    resize_w = input_size
                    im = cv2.resize(im, dsize=(resize_w, resize_h))
                    resize_ratio_3_x = resize_w / float(new_w)
                    resize_ratio_3_y = resize_h / float(new_h)
                    text_polys[:, :, 0] *= resize_ratio_3_x
                    text_polys[:, :, 1] *= resize_ratio_3_y
                    new_h, new_w, _ = im.shape
                    score_map, geo_map, training_mask = generate_rbox(
                        (new_h, new_w), text_polys, text_tags)

                if vis:
                    fig, axs = plt.subplots(3, 2, figsize=(20, 30))
                    # axs[0].imshow(im[:, :, ::-1])
                    # axs[0].set_xticks([])
                    # axs[0].set_yticks([])
                    # for poly in text_polys:
                    #     poly_h = min(abs(poly[3, 1] - poly[0, 1]), abs(poly[2, 1] - poly[1, 1]))
                    #     poly_w = min(abs(poly[1, 0] - poly[0, 0]), abs(poly[2, 0] - poly[3, 0]))
                    #     axs[0].add_artist(Patches.Polygon(
                    #         poly * 4, facecolor='none', edgecolor='green', linewidth=2, linestyle='-', fill=True))
                    #     axs[0].text(poly[0, 0] * 4, poly[0, 1] * 4, '{:.0f}-{:.0f}'.format(poly_h * 4, poly_w * 4),
                    #                    color='purple')
                    # axs[1].imshow(score_map)
                    # axs[1].set_xticks([])
                    # axs[1].set_yticks([])
                    axs[0, 0].imshow(im[:, :, ::-1])
                    axs[0, 0].set_xticks([])
                    axs[0, 0].set_yticks([])
                    for poly in text_polys:
                        poly_h = min(abs(poly[3, 1] - poly[0, 1]),
                                     abs(poly[2, 1] - poly[1, 1]))
                        poly_w = min(abs(poly[1, 0] - poly[0, 0]),
                                     abs(poly[2, 0] - poly[3, 0]))
                        axs[0, 0].add_artist(
                            Patches.Polygon(poly,
                                            facecolor='none',
                                            edgecolor='green',
                                            linewidth=2,
                                            linestyle='-',
                                            fill=True))
                        axs[0, 0].text(poly[0, 0],
                                       poly[0, 1],
                                       '{:.0f}-{:.0f}'.format(poly_h, poly_w),
                                       color='purple')
                    axs[0, 1].imshow(score_map[::, ::])
                    axs[0, 1].set_xticks([])
                    axs[0, 1].set_yticks([])
                    axs[1, 0].imshow(geo_map[::, ::, 0])
                    axs[1, 0].set_xticks([])
                    axs[1, 0].set_yticks([])
                    axs[1, 1].imshow(geo_map[::, ::, 1])
                    axs[1, 1].set_xticks([])
                    axs[1, 1].set_yticks([])
                    axs[2, 0].imshow(geo_map[::, ::, 2])
                    axs[2, 0].set_xticks([])
                    axs[2, 0].set_yticks([])
                    axs[2, 1].imshow(training_mask[::, ::])
                    axs[2, 1].set_xticks([])
                    axs[2, 1].set_yticks([])
                    plt.tight_layout()
                    plt.show()
                    plt.close()

                images.append(im[:, :, ::-1].astype(np.float32))
                image_fns.append(im_fn)
                score_maps.append(score_map[::4, ::4,
                                            np.newaxis].astype(np.float32))
                geo_maps.append(geo_map[::4, ::4, :].astype(np.float32))
                training_masks.append(
                    training_mask[::4, ::4, np.newaxis].astype(np.float32))

                if len(images) == batch_size:
                    yield images, image_fns, score_maps, geo_maps, training_masks
                    images = []
                    image_fns = []
                    score_maps = []
                    geo_maps = []
                    training_masks = []
            except Exception as e:
                import traceback
                traceback.print_exc()
                continue