Utils¶
sahi.postprocess.utils
¶
Utilities for postprocessing object predictions.
Classes¶
ObjectPredictionList
¶
ObjectPredictionList(prediction_list: list)
Bases: Sequence
Sequence wrapper around a list of ObjectPrediction instances.
Provides indexing by int, list, or tensor-like objects, and conversion to numpy arrays or torch tensors for batch postprocessing operations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
list
|
List of ObjectPrediction instances to wrap. |
required |
Initialize with a list of object predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
list
|
List of ObjectPrediction instances. |
required |
Source code in sahi/postprocess/utils.py
Methods:¶
extend
¶
extend(
object_prediction_list: ObjectPredictionList,
) -> None
Extend this list with predictions from another ObjectPredictionList.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
object_prediction_list
¶ |
ObjectPredictionList
|
The list whose predictions to append. |
required |
Source code in sahi/postprocess/utils.py
totensor
¶
tonumpy
¶
Convert to a numpy array of shape (N, 6).
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray with columns [x1, y1, x2, y2, score, category_id]. |
tolist
¶
tolist() -> ObjectPrediction | list[ObjectPrediction]
Unwrap to a single ObjectPrediction or a list.
Returns:
| Type | Description |
|---|---|
ObjectPrediction | list[ObjectPrediction]
|
A single ObjectPrediction if the list has one element, |
ObjectPrediction | list[ObjectPrediction]
|
otherwise the full list of ObjectPrediction instances. |
Source code in sahi/postprocess/utils.py
Functions:¶
repair_polygon
¶
repair_polygon(shapely_polygon: Polygon) -> Polygon
Attempt to fix an invalid Shapely polygon using a zero-width buffer.
If the repaired result is a MultiPolygon or GeometryCollection, the polygon with the largest area is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Polygon
|
A Shapely Polygon that may be invalid. |
required |
Returns:
| Type | Description |
|---|---|
Polygon
|
A valid Polygon, or the original if it was already valid or |
Polygon
|
could not be repaired. |
Source code in sahi/postprocess/utils.py
repair_multipolygon
¶
repair_multipolygon(
shapely_multipolygon: MultiPolygon,
) -> MultiPolygon
Attempt to fix an invalid Shapely MultiPolygon using a zero-width buffer.
If the repaired result is a single Polygon, it is wrapped in a MultiPolygon. GeometryCollection results are filtered to polygons only.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
MultiPolygon
|
A Shapely MultiPolygon that may be invalid. |
required |
Returns:
| Type | Description |
|---|---|
MultiPolygon
|
A valid MultiPolygon, or the original if it was already valid or |
MultiPolygon
|
could not be repaired. |
Source code in sahi/postprocess/utils.py
coco_segmentation_to_shapely
¶
Convert COCO segmentation format to a Shapely MultiPolygon.
Source code in sahi/postprocess/utils.py
object_prediction_list_to_torch
¶
object_prediction_list_to_torch(
object_prediction_list: ObjectPredictionList,
) -> object
Convert to torch.Tensor. Requires torch to be installed.
Returns:
| Type | Description |
|---|---|
object
|
torch.Tensor of size N x [x1, y1, x2, y2, score, category_id] |
Source code in sahi/postprocess/utils.py
object_prediction_list_to_numpy
¶
object_prediction_list_to_numpy(
object_prediction_list: ObjectPredictionList,
) -> ndarray
Convert an ObjectPredictionList to a numpy array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ObjectPredictionList
|
The predictions to convert. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray of shape (N, 6) with columns |
ndarray
|
[x1, y1, x2, y2, score, category_id]. |
Source code in sahi/postprocess/utils.py
calculate_box_union
¶
calculate_box_union(
box1: list[int] | list[float] | ndarray,
box2: list[int] | list[float] | ndarray,
) -> list[int]
Compute the smallest bounding box enclosing both input boxes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
list[int] | list[float] | ndarray
|
First box as [x1, y1, x2, y2]. |
required |
|
list[int] | list[float] | ndarray
|
Second box as [x1, y1, x2, y2]. |
required |
Returns:
| Type | Description |
|---|---|
list[int]
|
The union bounding box as [x1, y1, x2, y2]. |
Source code in sahi/postprocess/utils.py
calculate_area
¶
calculate_area(
box: list[int] | list[float] | ndarray,
) -> float
Compute the area of an axis-aligned bounding box.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
list[int] | list[float] | ndarray
|
Bounding box as [x1, y1, x2, y2]. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The area of the box (width * height). |
Source code in sahi/postprocess/utils.py
calculate_intersection_area
¶
Compute the intersection area of two axis-aligned bounding boxes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ndarray
|
First box as np.array([x1, y1, x2, y2]). |
required |
|
ndarray
|
Second box as np.array([x1, y1, x2, y2]). |
required |
Returns:
| Type | Description |
|---|---|
float
|
The area of the intersection region, or 0 if the boxes do not |
float
|
overlap. |
Source code in sahi/postprocess/utils.py
calculate_bbox_iou
¶
calculate_bbox_iou(
pred1: ObjectPrediction, pred2: ObjectPrediction
) -> float
Compute Intersection over Union (IoU) between two predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ObjectPrediction
|
First object prediction. |
required |
|
ObjectPrediction
|
Second object prediction. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The IoU value in [0, 1]. |
Source code in sahi/postprocess/utils.py
calculate_bbox_ios
¶
calculate_bbox_ios(
pred1: ObjectPrediction, pred2: ObjectPrediction
) -> float
Compute Intersection over Smaller (IoS) between two predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ObjectPrediction
|
First object prediction. |
required |
|
ObjectPrediction
|
Second object prediction. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The IoS value in [0, 1], where the denominator is the area of |
float
|
the smaller bounding box. |
Source code in sahi/postprocess/utils.py
has_match
¶
has_match(
pred1: ObjectPrediction,
pred2: ObjectPrediction,
match_type: str = "IOU",
match_threshold: float = 0.5,
) -> bool
Check whether two predictions overlap above the given threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ObjectPrediction
|
First object prediction. |
required |
|
ObjectPrediction
|
Second object prediction. |
required |
|
str
|
Overlap metric, "IOU" or "IOS". |
'IOU'
|
|
float
|
Minimum overlap to count as a match. |
0.5
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the overlap exceeds match_threshold. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If match_type is not "IOU" or "IOS". |
Source code in sahi/postprocess/utils.py
get_merged_mask
¶
get_merged_mask(
pred1: ObjectPrediction, pred2: ObjectPrediction
) -> Mask
Compute the union of two prediction masks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ObjectPrediction
|
First object prediction with a valid mask. |
required |
|
ObjectPrediction
|
Second object prediction with a valid mask. |
required |
Returns:
| Type | Description |
|---|---|
Mask
|
A new Mask representing the geometric union of both masks. |
Source code in sahi/postprocess/utils.py
get_merged_score
¶
get_merged_score(
pred1: ObjectPrediction, pred2: ObjectPrediction
) -> float
Return the higher confidence score from two predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ObjectPrediction
|
First object prediction. |
required |
|
ObjectPrediction
|
Second object prediction. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The maximum score value. |
Source code in sahi/postprocess/utils.py
get_merged_bbox
¶
get_merged_bbox(
pred1: ObjectPrediction, pred2: ObjectPrediction
) -> BoundingBox
Compute the union bounding box of two predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ObjectPrediction
|
First object prediction. |
required |
|
ObjectPrediction
|
Second object prediction. |
required |
Returns:
| Type | Description |
|---|---|
BoundingBox
|
A BoundingBox enclosing both input bounding boxes. |
Source code in sahi/postprocess/utils.py
get_merged_category
¶
get_merged_category(
pred1: ObjectPrediction, pred2: ObjectPrediction
) -> Category
Return the category of the higher-scored prediction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ObjectPrediction
|
First object prediction. |
required |
|
ObjectPrediction
|
Second object prediction. |
required |
Returns:
| Type | Description |
|---|---|
Category
|
The Category from whichever prediction has the higher score. |
Source code in sahi/postprocess/utils.py
merge_object_prediction_pair
¶
merge_object_prediction_pair(
pred1: ObjectPrediction, pred2: ObjectPrediction
) -> ObjectPrediction
Merge two overlapping predictions into a single prediction.
Combines bounding boxes (union), masks (geometric union), scores (maximum), and categories (from the higher-scored prediction).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ObjectPrediction
|
First object prediction. |
required |
|
ObjectPrediction
|
Second object prediction. |
required |
Returns:
| Type | Description |
|---|---|
ObjectPrediction
|
A new ObjectPrediction with merged attributes. |