Utils¶
sahi.postprocess.utils
¶
Utilities for postprocessing object predictions.
Classes¶
ObjectPredictionList
¶
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 |
Source code in sahi/postprocess/utils.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
Functions¶
__getitem__(i)
¶
Retrieve predictions by index, list of indices, or tensor-like.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
i
¶ |
int | list[int] | tuple[int, ...] | object
|
An integer index, list/tuple of indices, or tensor-like
object convertible via |
required |
Returns:
| Type | Description |
|---|---|
ObjectPredictionList
|
A new ObjectPredictionList containing the selected predictions. |
Source code in sahi/postprocess/utils.py
__init__(prediction_list)
¶
Initialize with a list of object predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prediction_list
¶ |
list
|
List of ObjectPrediction instances. |
required |
__len__()
¶
__setitem__(i, elem)
¶
Set predictions at the given index or indices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
i
¶ |
int | list[int] | tuple[int, ...] | object
|
An integer index, list/tuple of indices, or tensor-like. |
required |
elem
¶ |
ObjectPrediction | ObjectPredictionList | list[ObjectPrediction]
|
An ObjectPrediction, ObjectPredictionList, or list of ObjectPrediction instances to assign. |
required |
Source code in sahi/postprocess/utils.py
__str__()
¶
extend(object_prediction_list)
¶
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
tolist()
¶
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
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]. |
Functions¶
calculate_area(box)
¶
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_bbox_ios(pred1, pred2)
¶
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
calculate_bbox_iou(pred1, pred2)
¶
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_box_union(box1, box2)
¶
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_intersection_area(box1, box2)
¶
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
coco_segmentation_to_shapely(segmentation)
¶
Convert COCO segmentation format to a Shapely MultiPolygon.
Source code in sahi/postprocess/utils.py
get_merged_bbox(pred1, pred2)
¶
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(pred1, pred2)
¶
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
get_merged_mask(pred1, pred2)
¶
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(pred1, pred2)
¶
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
has_match(pred1, pred2, match_type='IOU', match_threshold=0.5)
¶
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
merge_object_prediction_pair(pred1, pred2)
¶
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. |
Source code in sahi/postprocess/utils.py
object_prediction_list_to_numpy(object_prediction_list)
¶
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
object_prediction_list_to_torch(object_prediction_list)
¶
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
repair_multipolygon(shapely_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
repair_polygon(shapely_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. |