Esempio n. 1
0
def test_rasterize_polygons(tanzania_raw_image_size):
    """Test the rasterization process

    Considering the "no-polygon" case, the function must return an empty mask.

    Considering a polygon that fill the left part of the original image. The
    rasterized mask must be filled with "1" on this part, and with "0" on the
    right part.
    """
    polygons = []
    mask = rasterize_polygons(polygons, tanzania_raw_image_size,
                              tanzania_raw_image_size)
    assert mask.shape == (tanzania_raw_image_size, tanzania_raw_image_size)
    assert np.unique(mask) == np.array([0])
    x1 = int(tanzania_raw_image_size / 2)
    polygon = Polygon(shell=(
        (0, 0),
        (x1, 0),
        (x1, tanzania_raw_image_size),
        (0, tanzania_raw_image_size),
        (0, 0),
    ))
    mask = rasterize_polygons(
        MultiPolygon([polygon]),
        tanzania_raw_image_size,
        tanzania_raw_image_size,
    )
    mask_polygon = mask[:tanzania_raw_image_size, :x1]
    assert np.all(np.unique(mask_polygon) == np.array([1]))
    mask_no_polygon = mask[:tanzania_raw_image_size, (1 + x1):]
    assert np.all(np.unique(mask_no_polygon) == np.array([0]))
Esempio n. 2
0
def test_rasterize_polygons(tanzania_raw_image_size):
    """Test the rasterization process

    Considering the "no-polygon" case, the function must return an empty mask.

    Considering a polygon that fill the left part of the original image. The
    rasterized mask must be filled with "1" on this part, and with "0" on the
    right part.
    """
    mask = rasterize_polygons([], np.array([]), tanzania_raw_image_size,
                              tanzania_raw_image_size)
    assert mask.shape == (tanzania_raw_image_size, tanzania_raw_image_size)
    assert np.unique(mask) == np.array([0])
    x1 = int(tanzania_raw_image_size / 3)
    x2 = int(2 * tanzania_raw_image_size / 3)
    polygon1 = Polygon(shell=(
        (0, 0),
        (x1, 0),
        (x1, tanzania_raw_image_size),
        (0, tanzania_raw_image_size),
        (0, 0),
    ))
    polygon2 = Polygon(shell=(
        (x1, 0),
        (x2, 0),
        (x2, tanzania_raw_image_size),
        (x1, tanzania_raw_image_size),
        (x1, 0),
    ))
    labels = [1, 2]
    mask = rasterize_polygons(
        MultiPolygon([polygon1, polygon2]),
        np.array(labels),
        tanzania_raw_image_size,
        tanzania_raw_image_size,
    )
    mask_polygon_1 = mask[:tanzania_raw_image_size, :x1]
    assert np.unique(mask_polygon_1) == labels[0]
    mask_polygon_2 = mask[:tanzania_raw_image_size, (1 + x1):x2]
    assert np.unique(mask_polygon_2) == labels[1]
    mask_no_polygon = mask[:tanzania_raw_image_size, (1 + x2):]
    assert np.unique(mask_no_polygon) == 0
Esempio n. 3
0
def main(args):
    features = get_image_features(args.datapath, args.dataset,
                                  args.image_basename)

    img_width, img_height = features["width"], features["height"]
    logger.info("Raw image size: %s, %s" % (img_width, img_height))

    image_paths = get_image_paths(args.datapath, args.dataset, args.image_size,
                                  args.image_basename)
    logger.info("The image will be splitted into %s tiles" % len(image_paths))
    images = extract_images(image_paths)
    coordinates = extract_coordinates_from_filenames(image_paths)
    labels = get_labels(args.datapath, args.dataset, args.image_size)

    model = get_trained_model(args.datapath, args.dataset, args.image_size,
                              len(labels))

    data = build_full_labelled_image(
        images,
        coordinates,
        model,
        args.image_size,
        img_width,
        img_height,
        args.batch_size,
    )
    logger.info("Labelled image dimension: %s, %s" %
                (data.shape[0], data.shape[1]))
    colored_data = assign_label_colors(data, labels)
    colored_data = draw_grid(colored_data, img_width, img_height,
                             args.image_size)
    predicted_label_folder = os.path.join(args.datapath, args.dataset,
                                          "output", "semseg",
                                          "predicted_labels")
    os.makedirs(predicted_label_folder, exist_ok=True)
    predicted_label_file = os.path.join(
        predicted_label_folder,
        args.image_basename + "_" + str(args.image_size) + ".png",
    )
    Image.fromarray(colored_data).save(predicted_label_file)

    vectorized_data = geometries.vectorize_mask(data)
    gdf = gpd.GeoDataFrame({"geometry": vectorized_data})
    predicted_geom_folder = os.path.join(
        args.datapath,
        args.dataset,
        "output",
        "semseg",
        "predicted_geometries",
    )
    os.makedirs(predicted_geom_folder, exist_ok=True)
    predicted_geom_file = os.path.join(
        predicted_geom_folder,
        args.image_basename + "_" + str(args.image_size) + ".geojson",
    )
    if not os.path.isfile(predicted_geom_file):
        gdf.to_file(predicted_geom_file, driver="GeoJSON")

    rasterized_data = geometries.rasterize_polygons(vectorized_data,
                                                    img_height, img_width)
    colored_raster_data = assign_label_colors(rasterized_data, labels)
    colored_raster_data = draw_grid(colored_raster_data, img_width, img_height,
                                    args.image_size)
    predicted_raster_folder = os.path.join(
        args.datapath,
        args.dataset,
        "output",
        "semseg",
        "predicted_rasters",
    )
    os.makedirs(predicted_raster_folder, exist_ok=True)
    predicted_raster_file = os.path.join(
        predicted_raster_folder,
        args.image_basename + "_" + str(args.image_size) + ".png",
    )
    Image.fromarray(colored_raster_data).save(predicted_raster_file)
Esempio n. 4
0
def main(args):

    logger.info("Postprocess %s...", args.image_basename)
    features = get_image_features(
        args.datapath, args.dataset, args.image_basename
    )

    img_width, img_height = features["width"], features["height"]
    logger.info("Raw image size: %s, %s" % (img_width, img_height))

    prepro_folder = utils.prepare_preprocessed_folder(args.datapath, args.dataset, args.image_size)
    image_paths = get_image_paths(prepro_folder["testing"], args.image_basename)
    logger.info("The image will be splitted into %s tiles" % len(image_paths))
    images = extract_images(image_paths)
    coordinates = extract_coordinates_from_filenames(image_paths)
    labels = get_labels(args.datapath, args.dataset, args.image_size)

    output_folder = utils.prepare_output_folder(
        args.datapath, args.dataset, args.image_size, "semseg"
    )
    model = get_trained_model(
        output_folder["best-model"], args.image_size, len(labels)
    )

    logger.info("Predict labels for input images...")
    data = build_full_labelled_image(
        images,
        coordinates,
        model,
        args.image_size,
        img_width,
        img_height,
        args.batch_size,
    )
    logger.info(
        "Labelled image dimension: %s, %s" % (data.shape[0], data.shape[1])
    )
    colored_data = assign_label_colors(data, labels)
    if args.draw_grid:
        colored_data = draw_grid(
            colored_data, img_width, img_height, args.image_size
        )
    predicted_label_file = os.path.join(
        output_folder["labels"],
        args.image_basename + "_" + str(args.image_size) + ".png",
    )
    Image.fromarray(colored_data).save(predicted_label_file)

    vectorized_labels, vectorized_data = geometries.vectorize_mask(
        data, colored_data, labels
    )
    gdf = gpd.GeoDataFrame(
        {"labels": vectorized_labels, "geometry": vectorized_data}
    )
    predicted_geom_file = os.path.join(
        output_folder["geometries"],
        args.image_basename + "_" + str(args.image_size) + ".geojson",
    )
    if os.path.isfile(predicted_geom_file):
        os.remove(predicted_geom_file)
    gdf.to_file(predicted_geom_file, driver="GeoJSON")

    rasterized_data = geometries.rasterize_polygons(
        vectorized_data, vectorized_labels, img_height, img_width
    )
    colored_raster_data = assign_label_colors(rasterized_data, labels)
    if args.draw_grid:
        colored_raster_data = draw_grid(
            colored_raster_data, img_width, img_height, args.image_size
        )
    predicted_raster_file = os.path.join(
        output_folder["rasters"],
        args.image_basename + "_" + str(args.image_size) + ".png",
    )
    Image.fromarray(colored_raster_data).save(predicted_raster_file)