artus.spatialize package

Submodules

artus.spatialize.GeoCOCOExporter module

A module to convert a coco file into a geojson file.

Annotations in the coco file are pixel values but if they match with a georeferenced raster file (a tif file for example), they are spatialized with an affine transformation.

Typical usage examples:

geojson_exporter = GeoCOCOExporter( coco_path = ‘path/to/coco/file.json’, sample_dir = ‘path/to/tif/directory/’, epsg_code = ‘EPSG:4326’, dest_path = ‘path/to/export/directory/’, dest_name = ‘predictions.geojson’

)

geojson_exporter.export()

class artus.spatialize.GeoCOCOExporter.GeoCOCOExporter(coco_path, epsg_code, dest_path, dest_name, sample_dir=None)

Bases: object

Class to convert a coco.json file into a geojson file.

Annotations in the coco file are pixel values. When converted to geojson format with an affine transformation, they become georeferenced.

coco_path

the relative or absolute path top a coco.json file

Type:

str

epsg_code

the epsg_code (like EPSG:4326’) to set the CRS of the geojson file

Type:

str

dest_path

the destination path where the results will be exported. If the directory does not exist yet, it will be created.

Type:

str

dest_name

the name of the file to store

Type:

str

sample_dir

the path to the directory where the tif files are located. If sample_name is already the full path of the sample, no need to provide sample_dir.

Type:

str, optional

affine_transform(gdf)

Apply an affine transformation to every polygons (annotations) in the geometry column of a geopandas.Dataframe

Parameters:

gdf – a g:py:class:geopandas.Dataframe containing a geometry column with shapely polygons in pixel value.

Returns:

A geopandas.Dataframe with polygons spatialized to the desire CRS

coco2gdf()

Convert a coco file into a geopandas dataframe.

Returns:

py:class: geopandas.GeoDataFrame): a dataframe with annotation masks converted into shapely polygons but still with pixel values.

The polygons are indeed, not yet converted to spatial information.

Return type:

gdf (

export()

Export the geopandas dataframe to geojson.

Returns:

A geojson file exported at the dest_path + dest_name location.

get_transform(sample)

Get affine transformation for a tif sample.

Parameters:

sample (str) – the path or name of the sample with the extension that should be .tif

Returns:

the affine.Affine shapely object with the 6 elements required for an affine transformation

artus.spatialize.GeoFiftyoneExporter module

A module to convert a fiftyone.core.dataset into a geojson file.

Annotations in the fiftyone dataset are masks or bounding box, mapped as pixel values but if they match with a georeferenced raster file (a tif file for example), they are spatialized with an affine transformation.

Typical usage examples:

dataset = fo.load_dataset(‘dataset_name’)

geojson_exporter = GeoFiftyoneExporter( export_dir = ‘/path/to/directory/’, epsg_code = ‘EPSG:4326’, label_type = ‘detections’, dest_name = ‘spatial_predictions.geojson’ )

dataset.export( dataset_exporter=geojson_exporter, label_field=’predictions’, export_media=False )

class artus.spatialize.GeoFiftyoneExporter.GeoFiftyoneExporter(export_dir, label_type, epsg_code, dest_name)

Bases: LabeledImageDatasetExporter, GeoCOCOExporter

Export a fiftyone dataset to a geospatial format (geojson).

This is only possible if samples are raster format (i.e. tif).

Datasets of this type are exported in the following format:

<export_dir>/

dest_name.geojson

where dest_name.geojson is a GeoJson file containing labels.

export_dir

the directory to write the export

label_type

the label_type of the concerned fiftyone field (‘polylines’ for segmentation annotations or ‘detections’ for bbox annotations)

epsg_code

the epsg code (for example : ‘4326’ for world coordinates) in which the results will be exported

dest_name

the file name of the geojson file with the extension (for example : ‘spatial_predictions.geojson’)

close(*args)

Performs any necessary actions after the last sample has been exported. This method is called when the exporter’s context manager interface is exited, DatasetExporter.__exit__(). Polygons are converted to a world coordinates with an affine transformation.

Parameters:

*args – the arguments to DatasetExporter.__exit__()

export_sample(image_or_path, label, metadata=None)

Exports the given sample to the dataset.

If images in the dataset are tif then it will convert use the world coordinates for the masks or bbox… Otherwise, if images are simply georeferenced it will assigned every label on the image to a single GPS point GPS point must have been added to the dataset using artus.spatialize.LocationImporter.import_csv_locations()

Parameters:
  • image_or_path – an image or the path to the image on disk

  • label – an instance of label_cls(), or a dictionary mapping field names to fiftyone.core.labels.Label instances, or None if the sample is unlabeled

  • metadata (None) – a fiftyone.core.metadata.ImageMetadata instance for the sample. Only required when requires_image_metadata() is True

property label_cls

The fiftyone.core.labels.Label class(es) exported by this exporter.

This can be any of the following:

  • a fiftyone.core.labels.Label class. In this case, the exporter directly exports labels of this type

  • a list or tuple of fiftyone.core.labels.Label classes. In this case, the exporter can export a single label field of any of these types

  • a dict mapping keys to fiftyone.core.labels.Label classes. In this case, the exporter can handle label dictionaries with value-types specified by this dictionary. Not all keys need be present in the exported label dicts

  • None. In this case, the exporter makes no guarantees about the labels that it can export

log_collection(sample_collection)

Logs any relevant information about the fiftyone.core.collections.SampleCollection whose samples will be exported.

Subclasses can optionally implement this method if their export format can record information such as the fiftyone.core.collections.SampleCollection.info() of the collection being exported.

By convention, this method must be optional; i.e., if it is not called before the first call to export_sample(), then the exporter must make do without any information about the fiftyone.core.collections.SampleCollection (which may not be available, for example, if the samples being exported are not stored in a collection).

Parameters:

sample_collection – the fiftyone.core.collections.SampleCollection whose samples will be exported

property requires_image_metadata

Whether this exporter requires fiftyone.core.metadata.ImageMetadata instances for each sample being exported.

setup()

Performs any necessary setup before exporting the first sample in the dataset.

This method is called when the exporter’s context manager interface is entered, DatasetExporter.__enter__().

shapely_polygons(label, metadata)

Transform a fiftyone.core.label.Label into a shapely polygon.

Parameters:
  • label (fiftyone.core.label.Label) – a detection or polylines field.

  • metadata (fiftyone.core.metadata.ImageMetadata) – metadata of the sample

Returns:

a shapely polygon that is a square if label was bouding boxes or a polygon if label was a polyline. Shapely polygons are still pixel values.

Return type:

shapely.Polygon

artus.spatialize.LocationImporter module

A module to import sample location from a CSV.

This module is dedicated to user that only have images georeferenced by a GPS point. It will add the matching GPS point to every sample in the fiftyone dataset.

artus.spatialize.LocationImporter.import_csv_locations(location_path, fiftyone_dataset)

A function to import GPS locations from a CSV file and add it to the matching sample in a fiftyone dataset.

Parameters:
  • location_path (str) – a path to csv containing 3 columns called ‘filename’, ‘latitude’ and ‘longitude’

  • fiftyone_dataset (fiftyone.core.dataset) – a fiftyone dataset containing images

Returns:

the same dataset with a geolocation point added to the samples listed in the csv file.

Return type:

fiftyone.core.dataset

Module contents