def do_poly_nms(gdf:gpd.GeoDataFrame, nms_thresh=0.1, crit='score') -> gpd.GeoDataFrame:
"Perform non-max suppression for polygons using `nms_threshold` to `gdf`"
= gdf.copy()
gdf = gdf.score.values
scores = non_max_suppression_poly(gdf.geometry.values, scores, nms_thresh, crit)
idxs = gdf.iloc[idxs]
gdf return gdf
Postprocessing
Smoothing, combining etc.
Non-maximum suppression
First the commonly used NMS with bounding boxes, that prioritizes either confidence score (default) or bounding box area.
non_max_suppression_fast
non_max_suppression_fast (boxes, scores, overlap_thresh:float, sort_criterion:str='score')
Possibility to sort boxes by score (default) or area
Non-max suppression can in theory be applied also on polygons, but it hasn’t been used in any publications as far as I know.
If non_max_suppression_poly
is used to eliminate polygons, threshold might need to be smaller than typical value of 0.7 that is used.
poly_IoU
poly_IoU (poly_1:shapely.geometry.polygon.Polygon, poly_2:shapely.geometry.polygon.Polygon)
Calculate IoU for two shapely Polygons
non_max_suppression_poly
non_max_suppression_poly (geoms, scores, overlap_thresh:float, sort_criterion:str='score')
Do non-max suppression for shapely Polygons in geoms
. Can be sorted according to area
or score
Some utils to run above functions to GeoDataFrames
do_nms
do_nms (gdf:geopandas.geodataframe.GeoDataFrame, nms_thresh=0.7, crit='score')
Perform non-max suppression for bounding boxes using nms_threshold
to gdf
def do_min_rot_rectangle_nms(gdf:gpd.GeoDataFrame, nms_thresh=0.7, crit='score') -> gpd.GeoDataFrame:
"Perform non-max suppression for rotated bounding boxes using `nms_threshold` to `gdf`"
= gdf.copy()
gdf = gdf.score.values
scores = np.array([g.minimum_rotated_rectangle for g in gdf.geometry.values])
boxes = non_max_suppression_poly(boxes, scores, nms_thresh, crit)
idxs = gdf.iloc[idxs]
gdf return gdf