Esempio n. 1
0
def main():
    file_list = get_file_list()
    config = CoreConfig()
    config.num_colors = 3
    data_saver = DataSaver(args.out_dir, config)
    for file_name in file_list:
        y = get_mask_from_page_image(file_name[0], file_name[1],
                                     args.max_image_size)
        data_saver.write_image(file_name[2], y)
    data_saver.write_index()
Esempio n. 2
0
def visualize_object(x, transparency):
    """Given a dictionary object as follows
    x['img']: numpy array of shape (num_class,width,height)
    x['mask']: numpy array of same dimensions as image, but with every element categorizing it 
    into one of the object ids
    The method generates an image overlaying a translucent mask on the image and displays it.
    """
    c = CoreConfig()
    c.num_colors = x['img'].shape[0]
    visualize_mask(x,c,transparency)
    return
def get_mask_from_page_image(madcat_file_path, image_file_name, max_size):
    """ Given a page image, extracts the page image mask from it.
        Input
        -----
        image_file_name (string): complete path and name of the page image.
        madcat_file_path (string): complete path and name of the madcat xml file
                                      corresponding to the page image.
        """

    objects = _get_bounding_box(madcat_file_path)
    img = Image.open(image_file_name).convert("RGB")
    im_arr = np.array(img)

    config = CoreConfig()
    config.num_colors = 3
    image_with_objects = {
        'img': im_arr,
        'objects': objects
    }

    im_height = im_arr.shape[0]
    im_width = im_arr.shape[1]

    validated_objects = []
    for original_object in image_with_objects['objects']:
        ordered_polygon_points = original_object['polygon']
        object = {}
        resized_pp = []
        for point in ordered_polygon_points:
            new_point = _validate_and_update_point(point, im_width, im_height)
            resized_pp.append(new_point)
        object['polygon'] = resized_pp
        validated_objects.append(object)

    validated_image_with_objects = {
        'img': im_arr,
        'objects': validated_objects
    }

    scaled_image_with_objects = scale_down_image_with_objects(validated_image_with_objects, config,
                                                              max_size)

    img_padded = make_square_image_with_padding(scaled_image_with_objects['img'], config)

    padded_image_with_objects = {
        'img': img_padded,
        'objects': scaled_image_with_objects['objects']
    }

    y = convert_to_mask(padded_image_with_objects, config)

    return y