Annotation¶
sahi.annotation
¶
Annotation classes for object detection.
Contains classes for handling bounding boxes, categories, masks, and annotations.
Classes¶
BoundingBox
dataclass
¶
BoundingBox(
box: tuple[float, float, float, float]
| list[float]
| list[int],
shift_amount: tuple[int, int] = (0, 0),
)
BoundingBox represents a rectangular region in 2D space, typically used for object detection annotations.
Attributes:
| Name | Type | Description |
|---|---|---|
box |
Tuple[float, float, float, float]
|
The bounding box coordinates in the format (minx, miny, maxx, maxy). - minx (float): Minimum x-coordinate (left). - miny (float): Minimum y-coordinate (top). - maxx (float): Maximum x-coordinate (right). - maxy (float): Maximum y-coordinate (bottom). |
shift_amount |
Tuple[int, int]
|
The amount to shift the bounding box in the x and y directions. Defaults to (0, 0). |
BoundingBox Usage Example
Attributes¶
Methods:¶
get_expanded_box
¶
get_expanded_box(
ratio: float = 0.1,
max_x: int | None = None,
max_y: int | None = None,
) -> BoundingBox
Get an expanded bounding box by increasing its size by a given ratio.
The expansion is applied equally in all directions. Optionally, the expanded box can be clipped to maximum x and y boundaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ratio
¶ |
float
|
The proportion by which to expand the box size. Default is 0.1 (10%). |
0.1
|
max_x
¶ |
int
|
The maximum allowed x-coordinate for the expanded box. If None, no maximum is applied. |
None
|
max_y
¶ |
int
|
The maximum allowed y-coordinate for the expanded box. If None, no maximum is applied. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
BoundingBox |
BoundingBox
|
A new BoundingBox instance representing the expanded box. |
Source code in sahi/annotation.py
to_xywh
¶
Convert to [xmin, ymin, width, height] format.
Returns:
| Type | Description |
|---|---|
list[float]
|
list[float]: A list containing the bounding box in the format [xmin, ymin, width, height]. |
Source code in sahi/annotation.py
to_coco_bbox
¶
Convert to COCO format: [xmin, ymin, width, height].
Returns:
| Type | Description |
|---|---|
list[float]
|
list[float]: A list containing the bounding box in COCO format. |
to_xyxy
¶
Convert to [xmin, ymin, xmax, ymax] format.
Returns:
| Type | Description |
|---|---|
list[float]
|
list[float]: A list containing the bounding box in the format [xmin, ymin, xmax, ymax]. |
to_voc_bbox
¶
Convert to VOC format: [xmin, ymin, xmax, ymax].
Returns:
| Type | Description |
|---|---|
list[float]
|
list[float]: A list containing the bounding box in VOC format. |
get_shifted_box
¶
get_shifted_box() -> BoundingBox
Get shifted BoundingBox.
Returns:
| Name | Type | Description |
|---|---|---|
BoundingBox |
BoundingBox
|
A new BoundingBox instance representing the shifted box. |
Source code in sahi/annotation.py
Category
dataclass
¶
Category of the annotation.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
int
|
Unique identifier for the category. |
name |
str
|
Name of the category. |
Mask
¶
Mask(
segmentation: list[list[float]] | ndarray,
full_shape: list[int] | list[int | float] | None,
shift_amount: list[int] | list[int | float] = [0, 0],
)
Init Mask from coco segmentation representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
list[list[float]] | ndarray
|
List[List] [ [x1, y1, x2, y2, x3, y3, ...], [x1, y1, x2, y2, x3, y3, ...], ... ] |
required |
|
list[int] | list[int | float] | None
|
List[int] Size of the full image, should be in the form of [height, width] |
required |
|
list[int] | list[int | float]
|
List[int] To shift the box and mask predictions from sliced image to full sized image, should be in the form of [shift_x, shift_y] |
[0, 0]
|
Initialize Mask object.
Source code in sahi/annotation.py
Attributes¶
full_shape
property
¶
Returns full mask shape after shifting as [height, width].
shift_amount
property
¶
Returns the shift amount of the mask slice as [shift_x, shift_y].
Methods:¶
from_float_mask
classmethod
¶
from_float_mask(
mask: ndarray,
full_shape: list[int],
mask_threshold: float = 0.5,
shift_amount: list[int] | None = None,
) -> Mask
Create Mask from float mask array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
¶ |
ndarray
|
np.ndarray of np.float elements Mask values between 0 and 1 (should have a shape of height*width) |
required |
mask_threshold
¶ |
float
|
float Value to threshold mask pixels between 0 and 1 |
0.5
|
shift_amount
¶ |
list[int] | None
|
List To shift the box and mask predictions from sliced image to full sized image, should be in the form of [shift_x, shift_y] |
None
|
full_shape
¶ |
list[int]
|
List[int] Size of the full image after shifting, should be in the form of [height, width]. |
required |
Source code in sahi/annotation.py
from_bool_mask
classmethod
¶
from_bool_mask(
bool_mask: ndarray,
full_shape: list[int],
shift_amount: list[int] | None = None,
) -> Mask
Create Mask from boolean mask array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bool_mask
¶ |
ndarray
|
np.ndarray with bool elements 2D mask of object, should have a shape of height*width |
required |
full_shape
¶ |
list[int]
|
List[int] Size of the full image, should be in the form of [height, width] |
required |
shift_amount
¶ |
list[int] | None
|
List[int] To shift the box and mask predictions from sliced image to full sized image, should be in the form of [shift_x, shift_y]. |
None
|
Source code in sahi/annotation.py
get_shifted_mask
¶
get_shifted_mask() -> Mask
Return shifted mask.
Source code in sahi/annotation.py
ObjectAnnotation
¶
ObjectAnnotation(
bbox: list[float] | None = None,
segmentation: ndarray | list[list[float]] | None = None,
category_id: int | None = None,
category_name: str | None = None,
shift_amount: list[int]
| list[int | float]
| None = None,
full_shape: list[int] | list[int | float] | None = None,
)
All about an annotation such as Mask, Category, BoundingBox.
Initialize ObjectAnnotation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
list[float] | None
|
List [minx, miny, maxx, maxy] |
None
|
|
ndarray | list[list[float]] | None
|
List[List] [ [x1, y1, x2, y2, x3, y3, ...], [x1, y1, x2, y2, x3, y3, ...], ... ] |
None
|
|
int | None
|
int ID of the object category |
None
|
|
str | None
|
str Name of the object category |
None
|
|
list[int] | list[int | float] | None
|
List To shift the box and mask predictions from sliced image to full sized image, should be in the form of [shift_x, shift_y] |
None
|
|
list[int] | list[int | float] | None
|
List Size of the full image after shifting, should be in the form of [height, width]. |
None
|
Source code in sahi/annotation.py
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 | |
Methods:¶
from_bool_mask
classmethod
¶
from_bool_mask(
bool_mask: ndarray,
category_id: int | None = None,
category_name: str | None = None,
shift_amount: list[int] | None = None,
full_shape: list[int] | None = None,
) -> ObjectAnnotation
Create ObjectAnnotation from bool_mask (2D np.ndarray).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bool_mask
¶ |
ndarray
|
np.ndarray with bool elements 2D mask of object, should have a shape of height*width |
required |
category_id
¶ |
int | None
|
int ID of the object category |
None
|
category_name
¶ |
str | None
|
str Name of the object category |
None
|
full_shape
¶ |
list[int] | None
|
List Size of the full image, should be in the form of [height, width] |
None
|
shift_amount
¶ |
list[int] | None
|
List To shift the box and mask predictions from sliced image to full sized image, should be in the form of [shift_x, shift_y] |
None
|
Source code in sahi/annotation.py
from_coco_segmentation
classmethod
¶
from_coco_segmentation(
segmentation: list[list[float]] | list[list[int]],
full_shape: list[int],
category_id: int | None = None,
category_name: str | None = None,
shift_amount: list[int] | None = None,
) -> ObjectAnnotation
Create ObjectAnnotation from coco segmentation format.
The segmentation format is: [ [x1, y1, x2, y2, x3, y3, ...], [x1, y1, x2, y2, x3, y3, ...], ... ]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
segmentation
¶ |
list[list[float]] | list[list[int]]
|
List[List] [ [x1, y1, x2, y2, x3, y3, ...], [x1, y1, x2, y2, x3, y3, ...], ... ] |
required |
category_id
¶ |
int | None
|
int ID of the object category |
None
|
category_name
¶ |
str | None
|
str Name of the object category |
None
|
full_shape
¶ |
list[int]
|
List Size of the full image, should be in the form of [height, width] |
required |
shift_amount
¶ |
list[int] | None
|
List To shift the box and mask predictions from sliced image to full sized image, should be in the form of [shift_x, shift_y] |
None
|
Source code in sahi/annotation.py
from_coco_bbox
classmethod
¶
from_coco_bbox(
bbox: list[float] | list[int],
category_id: int | None = None,
category_name: str | None = None,
shift_amount: list[int] | None = None,
full_shape: list[int] | None = None,
) -> ObjectAnnotation
Create ObjectAnnotation from coco bbox [minx, miny, width, height].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
¶ |
list[float] | list[int]
|
List [minx, miny, width, height] |
required |
category_id
¶ |
int | None
|
int ID of the object category |
None
|
category_name
¶ |
str | None
|
str Name of the object category |
None
|
full_shape
¶ |
list[int] | None
|
List Size of the full image, should be in the form of [height, width] |
None
|
shift_amount
¶ |
list[int] | None
|
List To shift the box and mask predictions from sliced image to full sized image, should be in the form of [shift_x, shift_y] |
None
|
Source code in sahi/annotation.py
from_coco_annotation_dict
classmethod
¶
from_coco_annotation_dict(
annotation_dict: dict,
full_shape: list[int],
category_name: str | None = None,
shift_amount: list[int] | None = None,
) -> ObjectAnnotation
Create ObjectAnnotation from COCO annotation dict.
Converts a COCO formatted annotation dict (with fields "bbox", "segmentation", "category_id") to ObjectAnnotation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
annotation_dict
¶ |
dict
|
dict COCO formatted annotation dict (with fields "bbox", "segmentation", "category_id") |
required |
category_name
¶ |
str | None
|
str Category name of the annotation |
None
|
full_shape
¶ |
list[int]
|
List Size of the full image, should be in the form of [height, width] |
required |
shift_amount
¶ |
list[int] | None
|
List To shift the box and mask predictions from sliced image to full sized image, should be in the form of [shift_x, shift_y] |
None
|
Source code in sahi/annotation.py
from_shapely_annotation
classmethod
¶
from_shapely_annotation(
annotation: ShapelyAnnotation,
full_shape: list[int],
category_id: int | None = None,
category_name: str | None = None,
shift_amount: list[int] | None = None,
) -> ObjectAnnotation
Create ObjectAnnotation from shapely_utils.ShapelyAnnotation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
annotation
¶ |
ShapelyAnnotation
|
shapely_utils.ShapelyAnnotation |
required |
category_id
¶ |
int | None
|
int ID of the object category |
None
|
category_name
¶ |
str | None
|
str Name of the object category |
None
|
full_shape
¶ |
list[int]
|
List Size of the full image, should be in the form of [height, width] |
required |
shift_amount
¶ |
list[int] | None
|
List To shift the box and mask predictions from sliced image to full sized image, should be in the form of [shift_x, shift_y] |
None
|
Source code in sahi/annotation.py
from_imantics_annotation
classmethod
¶
from_imantics_annotation(
annotation: Any,
shift_amount: list[int] | None = None,
full_shape: list[int] | None = None,
) -> ObjectAnnotation
Create ObjectAnnotation from imantics.annotation.Annotation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
annotation
¶ |
Any
|
imantics.annotation.Annotation |
required |
shift_amount
¶ |
list[int] | None
|
List To shift the box and mask predictions from sliced image to full sized image, should be in the form of [shift_x, shift_y] |
None
|
full_shape
¶ |
list[int] | None
|
List Size of the full image, should be in the form of [height, width] |
None
|
Source code in sahi/annotation.py
to_coco_annotation
¶
to_coco_annotation() -> CocoAnnotation
Convert to sahi.utils.coco.CocoAnnotation representation.
Source code in sahi/annotation.py
to_coco_prediction
¶
to_coco_prediction() -> CocoPrediction
Convert to sahi.utils.coco.CocoPrediction representation.
Source code in sahi/annotation.py
to_shapely_annotation
¶
to_shapely_annotation() -> ShapelyAnnotation
Convert to sahi.utils.shapely.ShapelyAnnotation representation.
Source code in sahi/annotation.py
to_imantics_annotation
¶
Convert to imantics.annotation.Annotation representation.
Source code in sahi/annotation.py
deepcopy
¶
deepcopy() -> ObjectAnnotation
Get deepcopy of current ObjectAnnotation instance.
Returns:
| Name | Type | Description |
|---|---|---|
ObjectAnnotation |
ObjectAnnotation
|
A deep copy of this ObjectAnnotation. |
get_shifted_object_annotation
¶
get_shifted_object_annotation() -> ObjectAnnotation
Return shifted object annotation.