def test_multi_threading_override():

    tmpdir = tempfile.mkdtemp()

    n = 100
    tmpfiles = []
    for i in range(n):
        tmpfiles.append(create_temp_file(tmpdir, '.JPEG'))
        im = Image.new('RGB', original_dimensions)
        im.save(tmpfiles[i].name, 'JPEG')

    p = Augmentor.Pipeline(tmpdir)
    assert len(p.augmentor_images) == n

    p.resize(probability=1, width=larger_dimensions[0], height=larger_dimensions[1])
    p.sample(n, multi_threaded=False)

    generated_images = glob.glob(os.path.join(tmpdir, "output", "*.JPEG"))
    number_of_gen_images = len(generated_images)

    assert number_of_gen_images == n

    for im_path in generated_images:
        im_g = Image.open(im_path)
        assert im_g.size == larger_dimensions
        im_g.close()

    # Clean up
    shutil.rmtree(tmpdir)
def test_all_operations_multi_thread():
    tmpdir = tempfile.mkdtemp()

    n = 100
    tmpfiles = []
    for i in range(n):
        tmpfiles.append(create_temp_file(tmpdir, '.JPEG'))
        im = Image.new('RGB', (480, 800))
        im.save(tmpfiles[i].name, 'JPEG')

    p = Augmentor.Pipeline(tmpdir)
    assert len(p.augmentor_images) == n

    p.resize(probability=1, width=300, height=300)
    p.rotate(probability=1, max_left_rotation=5, max_right_rotation=5)
    p.flip_left_right(probability=0.5)
    p.flip_top_bottom(probability=0.5)
    p.flip_random(probability=0.5)

    p.sample(n, multi_threaded=True)

    generated_images = glob.glob(os.path.join(tmpdir, "output", "*.JPEG"))
    number_of_gen_images = len(generated_images)

    assert number_of_gen_images == n

    # Clean up
    shutil.rmtree(tmpdir)
Beispiel #3
0
def test_resize_in_memory():

    tmpdir = tempfile.mkdtemp()
    tmp = create_temp_file(tmpdir, '.JPEG')
    im = Image.new('RGB', original_dimensions)
    im.save(tmp.name, 'JPEG')

    resize = Operations.Resize(probability=1,
                               width=larger_dimensions[0],
                               height=larger_dimensions[1],
                               resample_filter="BICUBIC")

    im = [im]

    im_resized = resize.perform_operation(im)
    assert im_resized[0].size == larger_dimensions

    resize_smaller = Operations.Resize(probability=1,
                                       width=smaller_dimensions[0],
                                       height=smaller_dimensions[1],
                                       resample_filter="BICUBIC")
    im_resized_smaller = resize_smaller.perform_operation(im)

    assert im_resized_smaller[0].size == smaller_dimensions

    shutil.rmtree(tmpdir)
Beispiel #4
0
def test_initialise_with_subfolders():

    num_of_folders = 10
    num_of_images = 10

    temp_directories, temp_files, parent_temp_directory = \
        create_sub_folders(number_of_sub_folders=num_of_folders, number_of_images=num_of_images)

    assert len(temp_directories) == num_of_folders
    assert len(temp_files) == num_of_images * num_of_folders

    # Add some images in the root directory, and some folders in the sub directories,
    # they should not be found when doing the scan
    tmp_not_to_be_found = create_temp_file(parent_temp_directory, '.JPEG')
    im_not_to_be_found = Image.fromarray(
        np.uint8(np.random.rand(800, 800) * 255))
    im_not_to_be_found.save(tmp_not_to_be_found.name, "JPEG")

    sub_temp_directory_not_to_be_found = tempfile.mkdtemp(
        dir=temp_directories[random.randint(0,
                                            len(temp_directories) - 1)])

    # TODO: fix
    files_found = ImageUtilities.scan_directory_with_classes(
        parent_temp_directory)

    assert len(files_found.keys()) == num_of_folders

    image_count = 0
    for val in files_found.values():
        image_count += len(val)
        for image_path in val:
            assert os.path.isfile(image_path)

    assert image_count == num_of_folders * num_of_images

    scanned_directories = []
    glob_scanned_files = glob.glob(os.path.join(parent_temp_directory, '*'))

    for glob_scanned_file in glob_scanned_files:
        if os.path.isdir(glob_scanned_file):
            scanned_directories.append(os.path.split(glob_scanned_file)[1])

    for key in files_found.keys():
        assert key in scanned_directories
        assert os.path.exists(os.path.join(parent_temp_directory, key))

    # Tidy up and delete temporary dirs.
    shutil.rmtree(sub_temp_directory_not_to_be_found)

    for temp_directory in temp_directories:
        shutil.rmtree(temp_directory)

    shutil.rmtree(parent_temp_directory)
Beispiel #5
0
def test_rotate_images_custom_temp_files():

    original_dimensions = (800, 800)

    tmpdir = tempfile.mkdtemp()
    tmp = create_temp_file(tmpdir, '.JPEG')
    im = Image.new('RGB', original_dimensions)
    im.save(tmp.name, 'JPEG')

    r = Operations.Rotate(probability=1, rotation=90)
    im = [im]
    im_r = r.perform_operation(im)

    assert im_r is not None
    assert im_r[0].size == original_dimensions

    shutil.rmtree(tmpdir)
Beispiel #6
0
def test_class_image_scan():
    # Some constants
    num_of_sub_dirs = random.randint(1, 10)
    num_of_im_files = random.randint(1, 10)

    output_directory = "some_folder"

    # Make an empty temporary directory
    initial_temp_directory = tempfile.mkdtemp()

    sub_dirs = []

    # Make num_of_sub_dirs subdirectories of this initial directory
    for _ in range(num_of_sub_dirs):
        sub_dirs.append(tempfile.mkdtemp(dir=initial_temp_directory))

    tmp_files = []
    image_counter = 0

    # Just to mix things up, we can create different file types
    suffix_filetypes = [('.PNG', 'PNG'), ('.JPEG', 'JPEG'), ('.GIF', 'GIF'),
                        ('.JPG', 'JPEG'), ('.png', 'PNG'), ('.jpeg', 'JPEG'),
                        ('.gif', 'GIF'), ('.jpg', 'JPEG')]

    # Make num_of_im_files images in each sub directory.
    for sub_dir in sub_dirs:
        for _ in range(num_of_im_files):
            suffix_filetype = random.choice(suffix_filetypes)
            tmp_files.append(
                create_temp_file(os.path.abspath(sub_dir), suffix_filetype[0]))
            im = Image.fromarray(np.uint8(np.random.rand(80, 80, 3) * 255))
            im.save(tmp_files[image_counter].name, suffix_filetype[1])
            image_counter += 1

    # Make a folder within the root directory with the same name as the output directory,
    # it should be ignored and all the tests should still run fine
    os.mkdir(os.path.join(initial_temp_directory, output_directory))

    def run():
        p = Augmentor.Pipeline(initial_temp_directory,
                               output_directory=output_directory)

        assert len(p.augmentor_images) == (num_of_sub_dirs * num_of_im_files)
        assert len(p.class_labels) == num_of_sub_dirs

        class_label_strings = [x[0] for x in p.class_labels]
        for sub_dir in sub_dirs:
            assert os.path.basename(sub_dir) in class_label_strings

        unique_class_labels = [x.class_label for x in p.augmentor_images]
        unique_class_labels = set(unique_class_labels)
        unique_class_labels = list(unique_class_labels)
        assert len(unique_class_labels) == num_of_sub_dirs

        for unique_class_label in unique_class_labels:
            assert unique_class_label in class_label_strings

        for class_label_string in class_label_strings:
            assert class_label_string in unique_class_labels

        assert set(class_label_strings) == set(unique_class_labels)

        # Count
        labels_int = [x.class_label_int for x in p.augmentor_images]
        bins = np.bincount(labels_int)
        for bin in bins:
            assert bin == num_of_im_files

        labels = [x.class_label for x in p.augmentor_images]
        for sub_dir in sub_dirs:
            assert labels.count(os.path.basename(sub_dir)) == num_of_im_files

    # Run the tests now, we will repeat later so it's been made into a func.
    run()

    # Add some extra images in places where they should not be and re-run the tests.
    create_temp_file(initial_temp_directory, ".PNG")
    create_temp_file(initial_temp_directory, ".PNG")

    # All tests should run exactly as before, those two files above should be ignored.
    run()

    # Sub directories in the sub directories should be ignored, so all tests should pass after
    # randomly placing a folder in any of these sud directories
    r1 = random.randint(0, len(sub_dirs) - 1)
    r2 = random.randint(0, len(sub_dirs) - 1)
    os.mkdir(
        os.path.join(initial_temp_directory, sub_dirs[r1], output_directory))
    os.mkdir(os.path.join(initial_temp_directory, sub_dirs[r2], "ignore_me"))
    temp_to_ignore = create_temp_file(
        os.path.join(initial_temp_directory, sub_dirs[r1], output_directory),
        ".JPEG")
    im = Image.fromarray(np.uint8(np.random.rand(80, 80, 3) * 255))
    im.save(temp_to_ignore.name, "JPEG")
    run()

    # Clean up
    shutil.rmtree(
        os.path.join(initial_temp_directory, sub_dirs[r1], output_directory))
    shutil.rmtree(
        os.path.join(initial_temp_directory, sub_dirs[r2], "ignore_me"))

    for sub_dir in sub_dirs:
        shutil.rmtree(sub_dir)

    shutil.rmtree(os.path.join(initial_temp_directory, output_directory))
    shutil.rmtree(initial_temp_directory)