Esempio n. 1
0
def test_vectorize_mask(tanzania_raw_image_size):
    """Test the mask vectorization operation, that transform raster mask into a
    MultiPolygon.

    Test a simple case with two expected polygons. The considered function uses
    OpenCV library to find polygon contours, and some approximated methods are
    implied. Hence exact polygon vertice coordinates are not tested.
    """
    label_dicts = [{
        "id": 1,
        "color": [50, 200, 50]
    }, {
        "id": 2,
        "color": [200, 50, 50]
    }]
    mask = np.zeros([tanzania_raw_image_size, tanzania_raw_image_size],
                    dtype=np.uint8)
    data = np.zeros([tanzania_raw_image_size, tanzania_raw_image_size, 3],
                    dtype=np.uint8)
    empty_labels, empty_multipolygon = vectorize_mask(mask, data, label_dicts)
    assert len(empty_labels) == 0
    assert len(empty_multipolygon) == 0
    x1, y1 = 100, 200
    x2, y2 = 300, 400
    x3, y3 = 300, 600
    x4, y4 = 700, 900
    mask[y1:y2, x1:x2] = label_dicts[0]["id"]
    mask[y3:y4, x3:x4] = label_dicts[1]["id"]
    data[y1:y2, x1:x2] = label_dicts[0]["color"]
    data[y3:y4, x3:x4] = label_dicts[1]["color"]
    labels, multipolygon = vectorize_mask(mask, data, label_dicts)
    assert len(labels) == 2
    assert np.sum(labels == 1) == 1
    assert np.sum(labels == 2) == 1
    assert len(multipolygon) == 2
Esempio n. 2
0
def test_vectorize_mask(tanzania_raw_image_size):
    """Test the mask vectorization operation, that transform raster mask into a
    MultiPolygon.

    Test a simple case with two expected polygons. The considered function uses
    OpenCV library to find polygon contours, and some approximated methods are
    implied. Hence exact polygon vertice coordinates are not tested.
    """
    mask = np.zeros([tanzania_raw_image_size, tanzania_raw_image_size],
                    dtype=np.uint8)
    empty_multipolygon = vectorize_mask(mask)
    assert len(empty_multipolygon) == 0
    x1, y1 = 100, 200
    x2, y2 = 300, 400
    x3, y3 = 300, 600
    x4, y4 = 700, 900
    mask[y1:y2, x1:x2] = 1
    mask[y3:y4, x3:x4] = 1
    multipolygon = vectorize_mask(mask)
    assert len(multipolygon) == 2
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)