Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close #511, replaced convert_to_boxes with geometry_type #514

Merged
merged 4 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions deepforest/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,24 +245,33 @@ def xml_to_annotations(xml_path):
def shapefile_to_annotations(shapefile,
rgb,
buffer_size=0.5,
convert_to_boxes=False,
geometry_type="boundingbox",
savedir="."):
"""
Convert a shapefile of annotations into annotations csv file for DeepForest training and evaluation

Geometry Handling:
The geometry_type is the form of the objects in the given shapefile. It can be "boundingbox" or "point".
If geometry_type is set to "boundingbox" (default) then the bounding boxes in the shapefile will be used as is and transferred over
to the annotations file. However, if the geometry_type is "point" then a bounding box will be created in its place that's centered on
the original point with an apothem equal to buffer_size.
JSpencerPittman marked this conversation as resolved.
Show resolved Hide resolved

Args:
shapefile: Path to a shapefile on disk. If a label column is present, it will be used, else all labels are assumed to be "Tree"
rgb: Path to the RGB image on disk
savedir: Directory to save csv files
buffer_size: size of point to box expansion in map units of the target object, meters for projected data, pixels for unprojected data. The buffer_size is added to each side of the x,y point to create the box.
convert_to_boxes (False): If True, convert the point objects in the shapefile into bounding boxes with size 'buffer_size'.
geometry_type (boundingbox): Specifies the spatial representation used in the shapefile; can be 'boundingbox' or 'point'
Returns:
results: a pandas dataframe


"""
# Read shapefile
gdf = gpd.read_file(shapefile)

# define in image coordinates and buffer to create a box
if convert_to_boxes:
if geometry_type == 'point':
gdf["geometry"] = [
shapely.geometry.Point(x, y)
for x, y in zip(gdf.geometry.x.astype(float), gdf.geometry.y.astype(float))
Expand Down
4 changes: 2 additions & 2 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_shapefile_to_annotations_convert_to_boxes(tmpdir):
gdf = gpd.GeoDataFrame(df, geometry="geometry")
gdf.to_file("{}/annotations.shp".format(tmpdir))
image_path = get_data("OSBS_029.tif")
shp = utilities.shapefile_to_annotations(shapefile="{}/annotations.shp".format(tmpdir), rgb=image_path, savedir=tmpdir, convert_to_boxes=True)
shp = utilities.shapefile_to_annotations(shapefile="{}/annotations.shp".format(tmpdir), rgb=image_path, savedir=tmpdir, geometry_type="point")
assert shp.shape[0] == 2

def test_shapefile_to_annotations(tmpdir):
Expand All @@ -70,7 +70,7 @@ def test_shapefile_to_annotations(tmpdir):

gdf.to_file("{}/annotations.shp".format(tmpdir))
image_path = get_data("OSBS_029.tif")
shp = utilities.shapefile_to_annotations(shapefile="{}/annotations.shp".format(tmpdir), rgb=image_path, savedir=tmpdir, convert_to_boxes=False)
shp = utilities.shapefile_to_annotations(shapefile="{}/annotations.shp".format(tmpdir), rgb=image_path, savedir=tmpdir, geometry_type="boundingbox")
assert shp.shape[0] == 2

def test_boxes_to_shapefile_projected(m):
Expand Down
Loading