sahi
¶
SAHI: Sliced Aided Hyper Inference.
A framework for performing object detection on large images using slicing.
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
AutoDetectionModel
¶
Automatic detection model loader.
Methods:¶
from_pretrained
staticmethod
¶
from_pretrained(
model_type: str,
model_path: str | None = None,
model: object | None = None,
config_path: str | None = None,
device: str | None = None,
mask_threshold: float = 0.5,
confidence_threshold: float = 0.3,
category_mapping: dict | None = None,
category_remapping: dict | None = None,
load_at_init: bool = True,
image_size: int | None = None,
**kwargs: object,
) -> DetectionModel
Load a DetectionModel from given path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_type
¶ |
str
|
str Name of the detection framework (example: "ultralytics", "huggingface", "torchvision") |
required |
model_path
¶ |
str | None
|
str Path of the detection model (ex. 'model.pt') |
None
|
model
¶ |
object | None
|
Any A pre-initialized model instance, if available |
None
|
config_path
¶ |
str | None
|
str Path of the config file (ex. 'mmdet/configs/cascade_rcnn_r50_fpn_1x.py') |
None
|
device
¶ |
str | None
|
str Device, "cpu" or "cuda:0" |
None
|
mask_threshold
¶ |
float
|
float Value to threshold mask pixels, should be between 0 and 1 |
0.5
|
confidence_threshold
¶ |
float
|
float All predictions with score < confidence_threshold will be discarded |
0.3
|
category_mapping
¶ |
dict | None
|
dict: str to str Mapping from category id (str) to category name (str) e.g. {"1": "pedestrian"} |
None
|
category_remapping
¶ |
dict | None
|
dict: str to int Remap category ids based on category names, after performing inference e.g. {"car": 3} |
None
|
load_at_init
¶ |
bool
|
bool If True, automatically loads the model at initialization |
True
|
image_size
¶ |
int | None
|
int Inference input size. |
None
|
**kwargs
¶ |
object
|
object Additional keyword arguments to pass to the model. |
{}
|
Returns:
| Type | Description |
|---|---|
DetectionModel
|
Returns an instance of a DetectionModel |
Raises:
| Type | Description |
|---|---|
ImportError
|
If given {model_type} framework is not installed |
Source code in sahi/auto_model.py
DetectionModel
¶
DetectionModel(
model_path: str | None = None,
model: Any | None = None,
config_path: str | None = None,
device: str | None = None,
mask_threshold: float = 0.5,
confidence_threshold: float = 0.3,
category_mapping: dict | None = None,
category_remapping: dict | None = None,
load_at_init: bool = True,
image_size: int | None = None,
)
Base class for all detection models in SAHI.
Subclasses must implement load_model, perform_inference, and
_create_object_prediction_list_from_original_predictions to integrate
a new detection framework. The base class handles device management,
dependency checking, category remapping, and the public prediction API.
Init object detection/instance segmentation model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str | None
|
str Path for the instance segmentation model weight |
None
|
|
Any | None
|
Any A pre-loaded detection model instance. |
None
|
|
str | None
|
str Path for the mmdetection instance segmentation model config file |
None
|
|
str | None
|
Torch device, "cpu", "mps", "cuda", "cuda:0", "cuda:1", etc. |
None
|
|
float
|
float Value to threshold mask pixels, should be between 0 and 1 |
0.5
|
|
float
|
float All predictions with score < confidence_threshold will be discarded |
0.3
|
|
dict | None
|
dict: str to str Mapping from category id (str) to category name (str) e.g. {"1": "pedestrian"} |
None
|
|
dict | None
|
dict: str to int Remap category ids based on category names, after performing inference e.g. {"car": 3} |
None
|
|
bool
|
bool If True, automatically loads the model at initialization |
True
|
|
int | None
|
int Inference input size. |
None
|
Source code in sahi/models/base.py
Attributes¶
object_prediction_list
property
¶
object_prediction_list: list[ObjectPrediction]
Returns the object predictions for the first image.
This is a convenience accessor for single-image inference. For batch
inference results, use object_prediction_list_per_image instead.
object_prediction_list_per_image
property
¶
object_prediction_list_per_image: list[
list[ObjectPrediction]
]
Returns object predictions grouped by image.
Each element is a list of ObjectPrediction instances for the
corresponding image in the batch.
original_predictions
property
¶
Returns the raw predictions from the underlying model.
The format is model-specific and is set by perform_inference or
perform_batch_inference.
Methods:¶
check_dependencies
¶
Ensures required dependencies are installed.
If 'packages' is None, uses self.required_packages. Subclasses may still call with a custom list for dynamic needs.
Source code in sahi/models/base.py
load_model
¶
Load the detection model from disk and assign it to self.model.
Subclasses must override this method. The implementation should use
self.model_path, self.config_path, and self.device to
construct the underlying model object and store it in self.model.
Source code in sahi/models/base.py
set_model
¶
Set an already-instantiated model as the underlying detection model.
Subclasses must override this method to assign model to
self.model and perform any additional setup (e.g. category mapping).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
¶ |
Any
|
Any A pre-loaded detection model instance. |
required |
**kwargs
¶ |
Any
|
Any Additional keyword arguments for subclass-specific setup. |
{}
|
Source code in sahi/models/base.py
set_device
¶
set_device(device: str | None = None) -> None
Sets the device pytorch should use for the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
¶ |
str | None
|
Torch device, "cpu", "mps", "cuda", "cuda:0", "cuda:1", etc. |
None
|
unload_model
¶
perform_inference
¶
perform_inference(image: ndarray) -> None
Run inference on a single image and store raw predictions.
Subclasses must override this method. The implementation should run
the model on image and assign the raw results to
self._original_predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
¶ |
ndarray
|
np.ndarray A numpy array (H, W, C) containing the image to run inference on. |
required |
Source code in sahi/models/base.py
perform_batch_inference
¶
perform_batch_inference(images: list[ndarray]) -> None
Performs inference on a batch of images.
Subclasses can override this for native batch support (e.g.
UltralyticsDetectionModel passes the full list to YOLO for
true GPU batching, HuggingfaceDetectionModel feeds all images
to the processor in one call).
The default does not run inference here. It stores images so
that convert_original_predictions can call perform_inference
per image, preserving each model's _original_predictions format.
Subclasses with native batch support override this to run inference
immediately.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
¶ |
list[ndarray]
|
list[np.ndarray] List of numpy arrays (H, W, C) to run inference on. |
required |
Source code in sahi/models/base.py
convert_original_predictions
¶
convert_original_predictions(
shift_amount: list[list[int | float]] | None = [[0, 0]],
full_shape: list[list[int | float]] | None = None,
) -> None
Convert raw predictions to ObjectPrediction lists.
Should be called after perform_inference or perform_batch_inference.
When the default (sequential) perform_batch_inference was used,
this method runs inference + conversion one image at a time so that
each model's internal _original_predictions format is preserved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shift_amount
¶ |
list[list[int | float]] | None
|
Per-image shift amounts |
[[0, 0]]
|
full_shape
¶ |
list[list[int | float]] | None
|
Per-image full image sizes |
None
|
Source code in sahi/models/base.py
ObjectPrediction
¶
ObjectPrediction(
bbox: list[float] | None = None,
category_id: int | None = None,
category_name: str | None = None,
segmentation: list[list[float]] | None = None,
score: float = 0.0,
shift_amount: list[int]
| list[int | float]
| None = None,
full_shape: list[int] | list[int | float] | None = None,
)
Bases: ObjectAnnotation
Class for handling detection model predictions.
Initialize ObjectPrediction from bbox, score, category_id, category_name, segmentation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
list[float] | None
|
list [minx, miny, maxx, maxy] |
None
|
|
float
|
float Prediction score between 0 and 1 |
0.0
|
|
int | None
|
int ID of the object category |
None
|
|
str | None
|
str Name of the object category |
None
|
|
list[list[float]] | None
|
List[List] [ [x1, y1, x2, y2, x3, y3, ...], [x1, y1, x2, y2, x3, y3, ...], ... ] |
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/prediction.py
Methods:¶
get_shifted_object_prediction
¶
get_shifted_object_prediction() -> ObjectPrediction
Get shifted version of ObjectPrediction.
Shifts bbox and mask coords. Used for mapping sliced predictions over full image.
Source code in sahi/prediction.py
to_coco_prediction
¶
to_coco_prediction(
image_id: int | None = None,
) -> CocoPrediction
Convert to sahi.utils.coco.CocoPrediction representation.
Source code in sahi/prediction.py
to_fiftyone_detection
¶
Convert to fiftyone.Detection representation.
Source code in sahi/prediction.py
Modules¶
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
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 |
|---|---|---|---|
segmentation
¶ |
list[list[float]] | ndarray
|
List[List] [ [x1, y1, x2, y2, x3, y3, ...], [x1, y1, x2, y2, x3, y3, ...], ... ] |
required |
full_shape
¶ |
list[int] | list[int | float] | None
|
List[int] Size of the full image, should be in the form of [height, width] |
required |
shift_amount
¶ |
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
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].
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 |
|---|---|---|---|
bbox
¶ |
list[float] | None
|
List [minx, miny, maxx, maxy] |
None
|
segmentation
¶ |
ndarray | list[list[float]] | None
|
List[List] [ [x1, y1, x2, y2, x3, y3, ...], [x1, y1, x2, y2, x3, y3, ...], ... ] |
None
|
category_id
¶ |
int | None
|
int ID of the object category |
None
|
category_name
¶ |
str | None
|
str Name of the object category |
None
|
shift_amount
¶ |
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
|
full_shape
¶ |
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 | |
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.
Source code in sahi/annotation.py
Functions:¶
auto_model
¶
Automatic model loading utilities.
Classes¶
AutoDetectionModel
¶
Automatic detection model loader.
from_pretrained
staticmethod
¶from_pretrained(
model_type: str,
model_path: str | None = None,
model: object | None = None,
config_path: str | None = None,
device: str | None = None,
mask_threshold: float = 0.5,
confidence_threshold: float = 0.3,
category_mapping: dict | None = None,
category_remapping: dict | None = None,
load_at_init: bool = True,
image_size: int | None = None,
**kwargs: object,
) -> DetectionModel
Load a DetectionModel from given path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_type
¶ |
str
|
str Name of the detection framework (example: "ultralytics", "huggingface", "torchvision") |
required |
model_path
¶ |
str | None
|
str Path of the detection model (ex. 'model.pt') |
None
|
model
¶ |
object | None
|
Any A pre-initialized model instance, if available |
None
|
config_path
¶ |
str | None
|
str Path of the config file (ex. 'mmdet/configs/cascade_rcnn_r50_fpn_1x.py') |
None
|
device
¶ |
str | None
|
str Device, "cpu" or "cuda:0" |
None
|
mask_threshold
¶ |
float
|
float Value to threshold mask pixels, should be between 0 and 1 |
0.5
|
confidence_threshold
¶ |
float
|
float All predictions with score < confidence_threshold will be discarded |
0.3
|
category_mapping
¶ |
dict | None
|
dict: str to str Mapping from category id (str) to category name (str) e.g. {"1": "pedestrian"} |
None
|
category_remapping
¶ |
dict | None
|
dict: str to int Remap category ids based on category names, after performing inference e.g. {"car": 3} |
None
|
load_at_init
¶ |
bool
|
bool If True, automatically loads the model at initialization |
True
|
image_size
¶ |
int | None
|
int Inference input size. |
None
|
**kwargs
¶ |
object
|
object Additional keyword arguments to pass to the model. |
{}
|
Returns:
| Type | Description |
|---|---|
DetectionModel
|
Returns an instance of a DetectionModel |
Raises:
| Type | Description |
|---|---|
ImportError
|
If given {model_type} framework is not installed |
Source code in sahi/auto_model.py
Functions:¶
cli
¶
constants
¶
Constants for COCO dataset.
logger
¶
Logger configuration for SAHI.
Classes¶
SupportsPkgInfo
¶
Bases: Protocol
Protocol for loggers supporting pkg_info method.
SahiLogger
¶
Bases: BaseSahiLogger
SAHI logger implementation.
SahiLoggerFormatter
¶
Bases: Formatter
Custom formatter for SAHI logs.
models
¶
SAHI model classes for various detection frameworks.
This package provides a unified interface for object detection and instance segmentation models from multiple frameworks including Detectron2, MMDetection, HuggingFace Transformers, Roboflow, Ultralytics YOLO variants, and TorchVision.
Modules¶
base
¶
Base class for all detection models in SAHI.
Provides a unified interface for loading, inference, and prediction conversion across different detection frameworks.
DetectionModel
¶DetectionModel(
model_path: str | None = None,
model: Any | None = None,
config_path: str | None = None,
device: str | None = None,
mask_threshold: float = 0.5,
confidence_threshold: float = 0.3,
category_mapping: dict | None = None,
category_remapping: dict | None = None,
load_at_init: bool = True,
image_size: int | None = None,
)
Base class for all detection models in SAHI.
Subclasses must implement load_model, perform_inference, and
_create_object_prediction_list_from_original_predictions to integrate
a new detection framework. The base class handles device management,
dependency checking, category remapping, and the public prediction API.
Init object detection/instance segmentation model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_path
¶ |
str | None
|
str Path for the instance segmentation model weight |
None
|
model
¶ |
Any | None
|
Any A pre-loaded detection model instance. |
None
|
config_path
¶ |
str | None
|
str Path for the mmdetection instance segmentation model config file |
None
|
device
¶ |
str | None
|
Torch device, "cpu", "mps", "cuda", "cuda:0", "cuda:1", etc. |
None
|
mask_threshold
¶ |
float
|
float Value to threshold mask pixels, should be between 0 and 1 |
0.5
|
confidence_threshold
¶ |
float
|
float All predictions with score < confidence_threshold will be discarded |
0.3
|
category_mapping
¶ |
dict | None
|
dict: str to str Mapping from category id (str) to category name (str) e.g. {"1": "pedestrian"} |
None
|
category_remapping
¶ |
dict | None
|
dict: str to int Remap category ids based on category names, after performing inference e.g. {"car": 3} |
None
|
load_at_init
¶ |
bool
|
bool If True, automatically loads the model at initialization |
True
|
image_size
¶ |
int | None
|
int Inference input size. |
None
|
Source code in sahi/models/base.py
object_prediction_list
property
¶object_prediction_list: list[ObjectPrediction]
Returns the object predictions for the first image.
This is a convenience accessor for single-image inference. For batch
inference results, use object_prediction_list_per_image instead.
object_prediction_list_per_image
property
¶object_prediction_list_per_image: list[
list[ObjectPrediction]
]
Returns object predictions grouped by image.
Each element is a list of ObjectPrediction instances for the
corresponding image in the batch.
original_predictions
property
¶Returns the raw predictions from the underlying model.
The format is model-specific and is set by perform_inference or
perform_batch_inference.
check_dependencies
¶Ensures required dependencies are installed.
If 'packages' is None, uses self.required_packages. Subclasses may still call with a custom list for dynamic needs.
Source code in sahi/models/base.py
load_model
¶Load the detection model from disk and assign it to self.model.
Subclasses must override this method. The implementation should use
self.model_path, self.config_path, and self.device to
construct the underlying model object and store it in self.model.
Source code in sahi/models/base.py
set_model
¶Set an already-instantiated model as the underlying detection model.
Subclasses must override this method to assign model to
self.model and perform any additional setup (e.g. category mapping).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
¶ |
Any
|
Any A pre-loaded detection model instance. |
required |
**kwargs
¶ |
Any
|
Any Additional keyword arguments for subclass-specific setup. |
{}
|
Source code in sahi/models/base.py
set_device
¶set_device(device: str | None = None) -> None
Sets the device pytorch should use for the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
¶ |
str | None
|
Torch device, "cpu", "mps", "cuda", "cuda:0", "cuda:1", etc. |
None
|
unload_model
¶ perform_inference
¶perform_inference(image: ndarray) -> None
Run inference on a single image and store raw predictions.
Subclasses must override this method. The implementation should run
the model on image and assign the raw results to
self._original_predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
¶ |
ndarray
|
np.ndarray A numpy array (H, W, C) containing the image to run inference on. |
required |
Source code in sahi/models/base.py
perform_batch_inference
¶perform_batch_inference(images: list[ndarray]) -> None
Performs inference on a batch of images.
Subclasses can override this for native batch support (e.g.
UltralyticsDetectionModel passes the full list to YOLO for
true GPU batching, HuggingfaceDetectionModel feeds all images
to the processor in one call).
The default does not run inference here. It stores images so
that convert_original_predictions can call perform_inference
per image, preserving each model's _original_predictions format.
Subclasses with native batch support override this to run inference
immediately.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
¶ |
list[ndarray]
|
list[np.ndarray] List of numpy arrays (H, W, C) to run inference on. |
required |
Source code in sahi/models/base.py
convert_original_predictions
¶convert_original_predictions(
shift_amount: list[list[int | float]] | None = [[0, 0]],
full_shape: list[list[int | float]] | None = None,
) -> None
Convert raw predictions to ObjectPrediction lists.
Should be called after perform_inference or perform_batch_inference.
When the default (sequential) perform_batch_inference was used,
this method runs inference + conversion one image at a time so that
each model's internal _original_predictions format is preserved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shift_amount
¶ |
list[list[int | float]] | None
|
Per-image shift amounts |
[[0, 0]]
|
full_shape
¶ |
list[list[int | float]] | None
|
Per-image full image sizes |
None
|
Source code in sahi/models/base.py
detectron2
¶
Detectron2 detection model wrapper for SAHI.
Provides integration with Facebook's Detectron2 framework for object detection and instance segmentation.
Detectron2DetectionModel
¶
Bases: DetectionModel
Detectron2 object detection model.
Wraps Detectron2's DefaultPredictor for detection and instance segmentation.
Initialize Detectron2 detection model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
¶ |
object
|
Variable length argument list passed to DetectionModel. |
()
|
**kwargs
¶ |
object
|
Arbitrary keyword arguments passed to DetectionModel. |
{}
|
Source code in sahi/models/detectron2.py
load_model
¶Load Detectron2 model from configuration.
Source code in sahi/models/detectron2.py
perform_inference
¶perform_inference(image: ndarray) -> None
Prediction is performed using self.model and the prediction result is set to self._original_predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
¶ |
ndarray
|
np.ndarray A numpy array that contains the image to be predicted. 3 channel image should be in RGB order. |
required |
Source code in sahi/models/detectron2.py
huggingface
¶
HuggingFace Transformers detection model wrapper for SAHI.
Provides integration with Hugging Face Transformers library for object detection and instance segmentation models like DETR variants.
HuggingfaceDetectionModel
¶HuggingfaceDetectionModel(
model_path: str | None = None,
model: object | None = None,
processor: object | None = None,
config_path: str | None = None,
device: str | None = None,
mask_threshold: float = 0.5,
confidence_threshold: float = 0.3,
category_mapping: dict | None = None,
category_remapping: dict | None = None,
load_at_init: bool = True,
image_size: int | None = None,
token: str | None = None,
text_prompt: str | None = None,
text_labels: list[str] | None = None,
text_threshold: float = 0.25,
)
Bases: DetectionModel
HuggingFace Transformers object detection model.
Supports DETR-style object detection models and GroundingDINO-style zero-shot detection models.
Initialize HuggingFace detection model.
Source code in sahi/models/huggingface.py
load_model
¶Load model from HuggingFace.
Source code in sahi/models/huggingface.py
set_model
¶Set the detection model and processor.
Source code in sahi/models/huggingface.py
perform_inference
¶perform_inference(image: list | ndarray) -> None
Prediction is performed using self.model and the prediction result is set to self._original_predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
¶ |
list | ndarray
|
np.ndarray A numpy array that contains the image to be predicted. 3 channel image should be in RGB order. |
required |
Source code in sahi/models/huggingface.py
perform_batch_inference
¶perform_batch_inference(images: list[ndarray]) -> None
Native batch inference: process all images in a single processor + model call.
Unlike the base-class default (which runs images sequentially), this feeds the entire list to the HuggingFace processor at once and executes one batched forward pass. The processor pads images to a uniform size internally, so images of different resolutions are handled correctly.
This avoids setting _batch_images so
convert_original_predictions uses the standard multi-image path
rather than the sequential fallback.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
¶ |
list[ndarray]
|
List of numpy arrays (H, W, C) in RGB order. |
required |
Source code in sahi/models/huggingface.py
get_valid_predictions
¶get_valid_predictions(
logits: Any, pred_boxes: Any
) -> tuple
Get predictions above confidence threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logits
¶ |
Any
|
torch.Tensor |
required |
pred_boxes
¶ |
Any
|
torch.Tensor |
required |
Returns:
| Name | Type | Description |
|---|---|---|
scores |
tuple
|
torch.Tensor |
cat_ids |
tuple
|
torch.Tensor |
boxes |
tuple
|
torch.Tensor |
Source code in sahi/models/huggingface.py
huggingface_segmentation
¶
HuggingFace segmentation model wrapper for SAHI.
Supports MaskFormer, Mask2Former, and OneFormer for instance, semantic, and panoptic segmentation via Hugging Face Transformers.
HuggingfaceSegmentationModel
¶HuggingfaceSegmentationModel(
*args: Any,
overlap_mask_area_threshold: float = 0.8,
label_ids_to_fuse: list[int] | None = None,
min_segment_area: int = 100,
segmentation_type: SegmentationType = INSTANCE_SEGMENTATION,
**kwargs: Any,
)
Bases: HuggingfaceDetectionModel
HuggingFace segmentation model.
Subclasses :class:HuggingfaceDetectionModel, reusing its processor,
num_categories, token handling, and dependency checks. Supports
MaskFormer, Mask2Former, and OneFormer for instance, semantic, and
panoptic segmentation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
overlap_mask_area_threshold
¶ |
float
|
Overlap mask area threshold to merge or discard small disconnected parts within each binary instance mask. |
0.8
|
label_ids_to_fuse
¶ |
list[int] | None
|
Label ids whose instances will be fused together (panoptic only). E.g. sky can be a single segment per image. |
None
|
min_segment_area
¶ |
int
|
Segments below this contour area are dropped. |
100
|
segmentation_type
¶ |
SegmentationType
|
Which segmentation head to use. Params that do not apply to the chosen type are ignored. |
INSTANCE_SEGMENTATION
|
Source code in sahi/models/huggingface_segmentation.py
load_model
¶Load model and processor from HuggingFace.
Source code in sahi/models/huggingface_segmentation.py
mmdet
¶
MMDetection detection model wrapper for SAHI.
Provides integration with OpenMMLab's MMDetection framework for object detection and instance segmentation.
DetInferencerWrapper
¶DetInferencerWrapper(
model: ModelType | str | None = None,
weights: str | None = None,
device: str | None = None,
scope: str | None = "mmdet",
palette: str = "none",
image_size: int | None = None,
)
Bases: DetInferencer
Wrapper around MMDetection DetInferencer for custom inference pipeline.
Initialize the DetInferencer wrapper.
Source code in sahi/models/mmdet.py
MmdetDetectionModel
¶MmdetDetectionModel(
model_path: str | None = None,
model: object | None = None,
config_path: str | None = None,
device: str | None = None,
mask_threshold: float = 0.5,
confidence_threshold: float = 0.3,
category_mapping: dict | None = None,
category_remapping: dict | None = None,
load_at_init: bool = True,
image_size: int | None = None,
scope: str = "mmdet",
)
Bases: DetectionModel
MMDetection object detection model.
Wraps MMDetection's DetInferencer for detection and instance segmentation.
Initialize MMDetection detection model.
Source code in sahi/models/mmdet.py
has_mask
property
¶Returns if model output contains segmentation mask.
Considers both single dataset and ConcatDataset scenarios.
load_model
¶Detection model is initialized and set to self.model.
Source code in sahi/models/mmdet.py
set_model
¶Sets the underlying MMDetection model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
¶ |
Any
|
Any A MMDetection model |
required |
**kwargs
¶ |
Any
|
Any Additional keyword arguments for model setup. |
{}
|
Source code in sahi/models/mmdet.py
perform_inference
¶perform_inference(image: ndarray) -> None
Prediction is performed using self.model and the prediction result is set to self._original_predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
¶ |
ndarray
|
np.ndarray A numpy array that contains the image to be predicted. 3 channel image should be in RGB order. |
required |
Source code in sahi/models/mmdet.py
roboflow
¶
Roboflow detection model wrapper for SAHI.
Provides integration with Roboflow's inference SDK for object detection and instance segmentation models.
RoboflowDetectionModel
¶RoboflowDetectionModel(
model: object | None = None,
model_path: str | None = None,
config_path: str | None = None,
device: str | None = None,
mask_threshold: float = 0.5,
confidence_threshold: float = 0.3,
category_mapping: dict | None = None,
category_remapping: dict | None = None,
load_at_init: bool = True,
image_size: int | None = None,
api_key: str | None = None,
)
Bases: DetectionModel
Roboflow object detection model.
Supports both Roboflow Universe models (API-based) and local RF-DETR models.
Initialize the RoboflowDetectionModel with the given parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
¶ |
object | None
|
object Either a Roboflow model string identifier or an RF-DETR model class. |
None
|
api_key
¶ |
str | None
|
str Roboflow API key for authentication. |
None
|
model_path
¶ |
str | None
|
str Path for the instance segmentation model weight |
None
|
config_path
¶ |
str | None
|
str Path for the mmdetection instance segmentation model config file |
None
|
device
¶ |
str | None
|
Torch device, "cpu", "mps", "cuda", "cuda:0", "cuda:1", etc. |
None
|
mask_threshold
¶ |
float
|
float Value to threshold mask pixels, should be between 0 and 1 |
0.5
|
confidence_threshold
¶ |
float
|
float All predictions with score < confidence_threshold will be discarded |
0.3
|
category_mapping
¶ |
dict | None
|
dict: str to str Mapping from category id (str) to category name (str) e.g. {"1": "pedestrian"} |
None
|
category_remapping
¶ |
dict | None
|
dict: str to int Remap category ids based on category names, after performing inference e.g. {"car": 3} |
None
|
load_at_init
¶ |
bool
|
bool If True, automatically loads the model at initialization |
True
|
image_size
¶ |
int | None
|
int Inference input size. |
None
|
Source code in sahi/models/roboflow.py
set_model
¶ load_model
¶Load detection model from Roboflow.
This function initializes detection model and sets to self.model. Uses self.model_path, self.config_path, and self.device.
Source code in sahi/models/roboflow.py
perform_inference
¶perform_inference(image: ndarray) -> None
Run inference on image and store predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
¶ |
ndarray
|
np.ndarray A numpy array that contains the image to be predicted. |
required |
Source code in sahi/models/roboflow.py
rtdetr
¶
RT-DETR detection model wrapper for SAHI.
Provides integration with Ultralytics RT-DETR real-time detection transformer models.
RTDetrDetectionModel
¶RTDetrDetectionModel(
*args: object,
fuse: bool = False,
task: str | None = None,
**kwargs: object,
)
Bases: UltralyticsDetectionModel
RT-DETR object detection model.
Wraps Ultralytics RT-DETR for real-time detection inference.
Source code in sahi/models/ultralytics.py
load_model
¶Detection model is initialized and set to self.model.
Source code in sahi/models/rtdetr.py
torchvision
¶
TorchVision detection model wrapper for SAHI.
Provides integration with PyTorch's TorchVision library for object detection and instance segmentation models.
TorchVisionDetectionModel
¶
Bases: DetectionModel
TorchVision object detection model.
Supports various TorchVision detection models like Faster R-CNN, Mask R-CNN, etc.
Initialize TorchVision detection model.
Source code in sahi/models/torchvision.py
load_model
¶Load TorchVision model from config and weights.
Source code in sahi/models/torchvision.py
set_model
¶Sets the underlying TorchVision model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
¶ |
Any
|
Any A TorchVision model |
required |
**kwargs
¶ |
Any
|
Any Additional keyword arguments for model setup. |
{}
|
Source code in sahi/models/torchvision.py
perform_inference
¶perform_inference(
image: ndarray, image_size: int | None = None
) -> None
Prediction is performed using self.model and the prediction result is set to self._original_predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
¶ |
ndarray
|
np.ndarray A numpy array that contains the image to be predicted. 3 channel image should be in RGB order. |
required |
image_size
¶ |
int | None
|
int Inference input size. |
None
|
Source code in sahi/models/torchvision.py
ultralytics
¶
Ultralytics detection model wrapper for SAHI.
Provides integration with Ultralytics YOLO models for object detection, instance segmentation, and oriented bounding box detection.
UltralyticsDetectionModel
¶UltralyticsDetectionModel(
*args: object,
fuse: bool = False,
task: str | None = None,
**kwargs: object,
)
Bases: DetectionModel
Detection model for Ultralytics YOLO models.
Supports PyTorch (.pt), ONNX (.onnx), OpenVINO (.xml or _openvino_model/), NCNN (.param or _ncnn_model/), and TorchScript (.torchscript) models.
Initialize the Ultralytics detection model.
Accepts all arguments from DetectionModel.__init__ plus the
following keyword arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
¶ |
object
|
Variable length argument list passed to DetectionModel. |
()
|
fuse
¶ |
bool
|
If True, fuse Conv2d and BatchNorm2d layers for faster inference. Default: False. |
False
|
task
¶ |
str | None
|
Ultralytics task type (e.g. |
None
|
**kwargs
¶ |
object
|
Arbitrary keyword arguments passed to DetectionModel. |
{}
|
Source code in sahi/models/ultralytics.py
category_names
property
¶Returns the list of category names from the model.
Falls back to category_mapping values when model metadata is
unavailable (e.g. ONNX models without embedded names).
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither model names nor category_mapping are available. |
load_model
¶Detection model is initialized and set to self.model.
Source code in sahi/models/ultralytics.py
set_model
¶Sets the underlying Ultralytics model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
¶ |
Any
|
Any A Ultralytics model |
required |
**kwargs
¶ |
Any
|
Any Additional keyword arguments for model setup. |
{}
|
Source code in sahi/models/ultralytics.py
perform_inference
¶perform_inference(image: ndarray) -> None
Prediction is performed using self.model and the prediction result is set to self._original_predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
¶ |
ndarray
|
np.ndarray A numpy array that contains the image to be predicted. 3 channel image should be in RGB order. |
required |
Source code in sahi/models/ultralytics.py
perform_batch_inference
¶perform_batch_inference(images: list[ndarray]) -> None
Performs inference on a batch of images using native YOLO batch support.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
¶ |
list[ndarray]
|
list[np.ndarray] List of numpy arrays (H, W, C) in RGB order. |
required |
Source code in sahi/models/ultralytics.py
yolo-world
¶
YOLO-World detection model wrapper for SAHI.
Provides integration with Ultralytics YOLO-World open-vocabulary detection models.
YOLOWorldDetectionModel
¶YOLOWorldDetectionModel(
*args: object,
fuse: bool = False,
task: str | None = None,
**kwargs: object,
)
Bases: UltralyticsDetectionModel
YOLO-World object detection model.
An open-vocabulary object detector that can detect custom classes at test-time.
Source code in sahi/models/ultralytics.py
load_model
¶Detection model is initialized and set to self.model.
Source code in sahi/models/yolo-world.py
yoloe
¶
YOLOE detection model wrapper for SAHI.
Provides integration with YOLOE (Real-Time Seeing Anything) open-vocabulary detection and segmentation models.
YOLOEDetectionModel
¶YOLOEDetectionModel(
*args: object,
fuse: bool = False,
task: str | None = None,
**kwargs: object,
)
Bases: UltralyticsDetectionModel
YOLOE Detection Model for open-vocabulary detection and segmentation.
YOLOE (Real-Time Seeing Anything) is a zero-shot, promptable YOLO model designed for open-vocabulary detection and segmentation. It supports text prompts, visual prompts, and prompt-free detection with internal vocabulary (1200+ categories).
Key Features
- Open-vocabulary detection: Detect any object class via text prompts
- Visual prompting: One-shot detection using reference images
- Instance segmentation: Built-in segmentation for detected objects
- Real-time performance: Maintains YOLO speed with no inference overhead
- Prompt-free mode: Uses internal vocabulary for open-set recognition
Available Models
Text/Visual Prompt models: - yoloe-11s-seg.pt, yoloe-11m-seg.pt, yoloe-11l-seg.pt - yoloe-v8s-seg.pt, yoloe-v8m-seg.pt, yoloe-v8l-seg.pt
Prompt-free models: - yoloe-11s-seg-pf.pt, yoloe-11m-seg-pf.pt, yoloe-11l-seg-pf.pt - yoloe-v8s-seg-pf.pt, yoloe-v8m-seg-pf.pt, yoloe-v8l-seg-pf.pt
Usage Text Prompts
from sahi import AutoDetectionModel
# Load YOLOE model
detection_model = AutoDetectionModel.from_pretrained(
model_type="yoloe",
model_path="yoloe-11l-seg.pt",
confidence_threshold=0.3,
device="cuda:0"
)
# Set text prompts for specific classes
detection_model.model.set_classes(
["person", "car", "traffic light"],
detection_model.model.get_text_pe(["person", "car", "traffic light"])
)
# Perform prediction
from sahi.predict import get_prediction
result = get_prediction("image.jpg", detection_model)
Usage for standard detection (no prompts)
from sahi import AutoDetectionModel
# Load YOLOE model (works like standard YOLO)
detection_model = AutoDetectionModel.from_pretrained(
model_type="yoloe",
model_path="yoloe-11l-seg.pt",
confidence_threshold=0.3,
device="cuda:0"
)
# Perform prediction without prompts (uses internal vocabulary)
from sahi.predict import get_sliced_prediction
result = get_sliced_prediction(
"image.jpg",
detection_model,
slice_height=512,
slice_width=512,
overlap_height_ratio=0.2,
overlap_width_ratio=0.2
)
Note
- YOLOE models perform instance segmentation by default
- When used without prompts, YOLOE performs like standard YOLO11 with identical speed
- For visual prompting, see Ultralytics YOLOE documentation
- YOLOE achieves +3.5 AP over YOLO-Worldv2 on LVIS with 1.4x faster inference
References
- Paper: https://arxiv.org/abs/2503.07465
- Docs: https://docs.ultralytics.com/models/yoloe/
- GitHub: https://github.com/THU-MIG/yoloe
Source code in sahi/models/ultralytics.py
load_model
¶Loads the YOLOE detection model from the specified path.
Initializes the YOLOE model with the given model path or uses the default 'yoloe-11s-seg.pt' if no path is provided. The model is then moved to the specified device (CPU/GPU).
By default, YOLOE works in prompt-free mode using its internal vocabulary of 1200+ categories. To use text prompts for specific classes, call model.set_classes() after loading:
model.set_classes(["person", "car"], model.get_text_pe(["person", "car"]))
Raises:
| Type | Description |
|---|---|
TypeError
|
If the model_path is not a valid YOLOE model path or if the ultralytics package with YOLOE support is not installed. |
Source code in sahi/models/yoloe.py
yolov5
¶
YOLOv5 detection model wrapper for SAHI.
Provides integration with Ultralytics YOLOv5 for object detection.
Yolov5DetectionModel
¶
Bases: DetectionModel
YOLOv5 object detection model.
Wraps Ultralytics YOLOv5 for fast object detection.
Initialize YOLOv5 detection model.
Source code in sahi/models/yolov5.py
load_model
¶Detection model is initialized and set to self.model.
Source code in sahi/models/yolov5.py
set_model
¶Sets the underlying YOLOv5 model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
¶ |
Any
|
Any A YOLOv5 model |
required |
**kwargs
¶ |
Any
|
Any Additional keyword arguments for model setup. |
{}
|
Source code in sahi/models/yolov5.py
perform_inference
¶perform_inference(image: ndarray) -> None
Prediction is performed using self.model and the prediction result is set to self._original_predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
¶ |
ndarray
|
np.ndarray A numpy array that contains the image to be predicted. 3 channel image should be in RGB order. |
required |
Source code in sahi/models/yolov5.py
postprocess
¶
Postprocessing backends and utilities for object prediction refinement.
Functions:¶
get_postprocess_backend
¶
set_postprocess_backend
¶
set_postprocess_backend(name: str) -> None
Set the postprocessing backend.
Call once at startup before running any inference. This function is not thread-safe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
¶ |
str
|
One of "auto", "numpy", "numba", "torchvision". |
required |
Source code in sahi/postprocess/backends.py
Modules¶
backends
¶
Postprocessing backend selection and auto-detection.
Usage
from sahi.postprocess.backends import set_postprocess_backend, get_postprocess_backend
set_postprocess_backend("numba") # force numba set_postprocess_backend("auto") # auto-detect best available
set_postprocess_backend
¶set_postprocess_backend(name: str) -> None
Set the postprocessing backend.
Call once at startup before running any inference. This function is not thread-safe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
¶ |
str
|
One of "auto", "numpy", "numba", "torchvision". |
required |
Source code in sahi/postprocess/backends.py
get_postprocess_backend
¶ resolve_backend
¶Resolve "auto" to a concrete backend, caching the result.
When the backend is set to "auto", detection follows this priority:
- torchvision -- selected if torchvision is installed and a GPU is available, either CUDA or Apple MPS (GPU-accelerated NMS).
- numba -- selected if the numba package is installed (JIT-compiled loops, faster than pure numpy for large prediction counts).
- numpy -- always available as the fallback (pure numpy, no extra dependencies).
If the backend was explicitly set via set_postprocess_backend, that
value is returned directly without auto-detection.
Returns:
| Type | Description |
|---|---|
str
|
One of "numpy", "numba", or "torchvision". |
Source code in sahi/postprocess/backends.py
combine
¶
Postprocessing strategies for combining predictions from sliced inference.
PostprocessPredictions
¶PostprocessPredictions(
match_threshold: float = 0.5,
match_metric: str = "IOU",
class_agnostic: bool = True,
)
Bases: ABC
Abstract base class for postprocessing object prediction lists.
Subclasses implement a specific strategy (NMS, NMM, greedy NMM, etc.) to reduce overlapping detections produced by sliced inference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
match_threshold
¶ |
float
|
Minimum overlap value (IoU or IoS) to consider two predictions as matching. |
0.5
|
match_metric
¶ |
str
|
Overlap metric, "IOU" or "IOS". |
'IOU'
|
class_agnostic
¶ |
bool
|
If True, apply postprocessing across all categories. If False, apply per category independently. |
True
|
Initialize the postprocessor with configuration parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
match_threshold
¶ |
float
|
Minimum overlap value (IoU or IoS) to consider two predictions as matching. |
0.5
|
match_metric
¶ |
str
|
Overlap metric, "IOU" or "IOS". |
'IOU'
|
class_agnostic
¶ |
bool
|
If True, apply postprocessing across all categories. If False, apply per category independently. |
True
|
Source code in sahi/postprocess/combine.py
NMSPostprocess
¶NMSPostprocess(
match_threshold: float = 0.5,
match_metric: str = "IOU",
class_agnostic: bool = True,
)
Bases: PostprocessPredictions
Postprocessor using Non-Maximum Suppression (NMS).
Keeps the highest-scored prediction among overlapping boxes and discards the rest. Does not merge bounding boxes or masks.
Source code in sahi/postprocess/combine.py
NMMPostprocess
¶NMMPostprocess(
match_threshold: float = 0.5,
match_metric: str = "IOU",
class_agnostic: bool = True,
)
Bases: PostprocessPredictions
Postprocessor using Non-Maximum Merging (NMM) with transitive merging.
Instead of discarding overlapping detections, merges their bounding boxes, masks, and scores. Uses non-greedy transitive merging: if A overlaps B and B overlaps C, all three are merged even if A does not directly overlap C.
Source code in sahi/postprocess/combine.py
GreedyNMMPostprocess
¶GreedyNMMPostprocess(
match_threshold: float = 0.5,
match_metric: str = "IOU",
class_agnostic: bool = True,
)
Bases: NMMPostprocess
Postprocessor using Greedy Non-Maximum Merging (NMM).
Similar to NMM but uses a greedy strategy: each kept prediction only merges boxes that directly overlap with it (no transitive merging). This is faster than full NMM and produces tighter merged boxes.
Source code in sahi/postprocess/combine.py
LSNMSPostprocess
¶LSNMSPostprocess(
match_threshold: float = 0.5,
match_metric: str = "IOU",
class_agnostic: bool = True,
)
Bases: PostprocessPredictions
Postprocessor using Locality-Sensitive NMS from the lsnms package.
Uses a spatial index for fast neighbor lookup, making it efficient for
large numbers of predictions. Only supports IoU metric (not IoS).
Requires the lsnms package (pip install lsnms>0.3.1).
Note
This postprocessor is experimental and not recommended for production use.
Source code in sahi/postprocess/combine.py
nms
¶nms(
predictions: ndarray,
match_metric: str = "IOU",
match_threshold: float = 0.5,
) -> list[int]
Non-maximum suppression for axis-aligned bounding boxes.
Dispatches to the resolved backend (numpy, numba, or torchvision).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predictions
¶ |
ndarray
|
Array of shape (N, 6) with columns [x1, y1, x2, y2, score, category_id]. |
required |
match_metric
¶ |
str
|
Overlap metric, "IOU" or "IOS". |
'IOU'
|
match_threshold
¶ |
float
|
Minimum overlap to suppress a lower-scored box. |
0.5
|
Returns:
| Type | Description |
|---|---|
list[int]
|
List of indices of the kept predictions, sorted by score descending. |
Source code in sahi/postprocess/combine.py
batched_nms
¶batched_nms(
predictions: ndarray,
match_metric: str = "IOU",
match_threshold: float = 0.5,
) -> list[int]
Apply non-maximum suppression independently per category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predictions
¶ |
ndarray
|
Array of shape (N, 6) with columns [x1, y1, x2, y2, score, category_id]. |
required |
match_metric
¶ |
str
|
Overlap metric, "IOU" or "IOS". |
'IOU'
|
match_threshold
¶ |
float
|
Minimum overlap to suppress a lower-scored box. |
0.5
|
Returns:
| Type | Description |
|---|---|
list[int]
|
List of indices of the kept predictions, sorted by score descending. |
Source code in sahi/postprocess/combine.py
greedy_nmm
¶greedy_nmm(
predictions: ndarray,
match_metric: str = "IOU",
match_threshold: float = 0.5,
) -> dict[int, list[int]]
Greedy non-maximum merging for axis-aligned bounding boxes.
Instead of discarding overlapping boxes, merges them into the highest-scored box. Dispatches to the resolved backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predictions
¶ |
ndarray
|
Array of shape (N, 6) with columns [x1, y1, x2, y2, score, category_id]. |
required |
match_metric
¶ |
str
|
Overlap metric, "IOU" or "IOS". |
'IOU'
|
match_threshold
¶ |
float
|
Minimum overlap to merge a lower-scored box. |
0.5
|
Returns:
| Type | Description |
|---|---|
dict[int, list[int]]
|
Dict mapping each kept index to a list of indices merged into it. |
Source code in sahi/postprocess/combine.py
batched_greedy_nmm
¶batched_greedy_nmm(
predictions: ndarray,
match_metric: str = "IOU",
match_threshold: float = 0.5,
) -> dict[int, list[int]]
Apply greedy non-maximum merging independently per category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predictions
¶ |
ndarray
|
Array of shape (N, 6) with columns [x1, y1, x2, y2, score, category_id]. |
required |
match_metric
¶ |
str
|
Overlap metric, "IOU" or "IOS". |
'IOU'
|
match_threshold
¶ |
float
|
Minimum overlap to merge a lower-scored box. |
0.5
|
Returns:
| Type | Description |
|---|---|
dict[int, list[int]]
|
Dict mapping each kept index to a list of indices merged into it. |
Source code in sahi/postprocess/combine.py
nmm
¶nmm(
predictions: ndarray,
match_metric: str = "IOU",
match_threshold: float = 0.5,
) -> dict[int, list[int]]
Non-maximum merging (non-greedy, transitive) for axis-aligned bounding boxes.
Unlike greedy NMM, this variant allows transitive merging: if box A merges with B and B merges with C, all three are merged together. Dispatches to the resolved backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predictions
¶ |
ndarray
|
Array of shape (N, 6) with columns [x1, y1, x2, y2, score, category_id]. |
required |
match_metric
¶ |
str
|
Overlap metric, "IOU" or "IOS". |
'IOU'
|
match_threshold
¶ |
float
|
Minimum overlap to merge a lower-scored box. |
0.5
|
Returns:
| Type | Description |
|---|---|
dict[int, list[int]]
|
Dict mapping each kept index to a list of indices merged into it. |
Source code in sahi/postprocess/combine.py
batched_nmm
¶batched_nmm(
predictions: ndarray,
match_metric: str = "IOU",
match_threshold: float = 0.5,
) -> dict[int, list[int]]
Apply non-maximum merging (non-greedy, transitive) independently per category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predictions
¶ |
ndarray
|
Array of shape (N, 6) with columns [x1, y1, x2, y2, score, category_id]. |
required |
match_metric
¶ |
str
|
Overlap metric, "IOU" or "IOS". |
'IOU'
|
match_threshold
¶ |
float
|
Minimum overlap to merge a lower-scored box. |
0.5
|
Returns:
| Type | Description |
|---|---|
dict[int, list[int]]
|
Dict mapping each kept index to a list of indices merged into it. |
Source code in sahi/postprocess/combine.py
legacy
¶
Legacy postprocessing implementations.
combine
¶Legacy postprocessing implementations for object prediction merging.
PostprocessPredictions
¶PostprocessPredictions(
match_threshold: float = 0.5,
match_metric: str = "IOU",
class_agnostic: bool = True,
)
Utilities for calculating IOU/IOS based match for given ObjectPredictions.
Initialize the postprocessor with matching configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
match_threshold
¶ |
float
|
Minimum overlap value to consider predictions matching. |
0.5
|
match_metric
¶ |
str
|
Metric for overlap computation, "IOU" or "IOS". |
'IOU'
|
class_agnostic
¶ |
bool
|
If True, apply postprocessing across all categories. |
True
|
Source code in sahi/postprocess/legacy/combine.py
get_score_func
staticmethod
¶get_score_func(
object_prediction: ObjectPrediction,
) -> float
has_same_category_id
staticmethod
¶has_same_category_id(
pred1: ObjectPrediction, pred2: ObjectPrediction
) -> bool
Check if two predictions belong to the same category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pred1
¶ |
ObjectPrediction
|
First ObjectPrediction instance. |
required |
pred2
¶ |
ObjectPrediction
|
Second ObjectPrediction instance. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if both predictions have the same category ID. |
Source code in sahi/postprocess/legacy/combine.py
calculate_bbox_iou
staticmethod
¶calculate_bbox_iou(
pred1: ObjectPrediction, pred2: ObjectPrediction
) -> float
Returns the ratio of intersection area to the union.
Source code in sahi/postprocess/legacy/combine.py
calculate_bbox_ios
staticmethod
¶calculate_bbox_ios(
pred1: ObjectPrediction, pred2: ObjectPrediction
) -> float
Returns the ratio of intersection area to the smaller box's area.
Source code in sahi/postprocess/legacy/combine.py
NMSPostprocess
¶NMSPostprocess(
match_threshold: float = 0.5,
match_metric: str = "IOU",
class_agnostic: bool = True,
)
Bases: PostprocessPredictions
Non-Maximum Suppression postprocessor for legacy usage.
Source code in sahi/postprocess/legacy/combine.py
UnionMergePostprocess
¶UnionMergePostprocess(
match_threshold: float = 0.5,
match_metric: str = "IOU",
class_agnostic: bool = True,
)
Bases: PostprocessPredictions
Union merging postprocessor for overlapping predictions.
Source code in sahi/postprocess/legacy/combine.py
utils
¶
Utilities for postprocessing object predictions.
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 |
|---|---|---|---|
prediction_list
¶ |
list
|
List of ObjectPrediction instances to wrap. |
required |
Initialize with a list of object predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prediction_list
¶ |
list
|
List of ObjectPrediction instances. |
required |
Source code in sahi/postprocess/utils.py
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
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 |
|---|---|---|---|
shapely_polygon
¶ |
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 |
|---|---|---|---|
shapely_multipolygon
¶ |
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 |
|---|---|---|---|
object_prediction_list
¶ |
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 |
|---|---|---|---|
box1
¶ |
list[int] | list[float] | ndarray
|
First box as [x1, y1, x2, y2]. |
required |
box2
¶ |
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 |
|---|---|---|---|
box
¶ |
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 |
|---|---|---|---|
box1
¶ |
ndarray
|
First box as np.array([x1, y1, x2, y2]). |
required |
box2
¶ |
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 |
|---|---|---|---|
pred1
¶ |
ObjectPrediction
|
First object prediction. |
required |
pred2
¶ |
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 |
|---|---|---|---|
pred1
¶ |
ObjectPrediction
|
First object prediction. |
required |
pred2
¶ |
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 |
|---|---|---|---|
pred1
¶ |
ObjectPrediction
|
First object prediction. |
required |
pred2
¶ |
ObjectPrediction
|
Second object prediction. |
required |
match_type
¶ |
str
|
Overlap metric, "IOU" or "IOS". |
'IOU'
|
match_threshold
¶ |
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 |
|---|---|---|---|
pred1
¶ |
ObjectPrediction
|
First object prediction with a valid mask. |
required |
pred2
¶ |
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 |
|---|---|---|---|
pred1
¶ |
ObjectPrediction
|
First object prediction. |
required |
pred2
¶ |
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 |
|---|---|---|---|
pred1
¶ |
ObjectPrediction
|
First object prediction. |
required |
pred2
¶ |
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 |
|---|---|---|---|
pred1
¶ |
ObjectPrediction
|
First object prediction. |
required |
pred2
¶ |
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 |
|---|---|---|---|
pred1
¶ |
ObjectPrediction
|
First object prediction. |
required |
pred2
¶ |
ObjectPrediction
|
Second object prediction. |
required |
Returns:
| Type | Description |
|---|---|
ObjectPrediction
|
A new ObjectPrediction with merged attributes. |
Source code in sahi/postprocess/utils.py
predict
¶
High-level prediction API for object detection.
Classes¶
Functions:¶
filter_predictions
¶
filter_predictions(
object_prediction_list: list[ObjectPrediction],
exclude_classes_by_name: list[str] | None,
exclude_classes_by_id: list[int] | None,
) -> list[ObjectPrediction]
Filter out predictions whose category matches an exclusion list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
object_prediction_list
¶ |
list[ObjectPrediction]
|
list[ObjectPrediction] Predictions to filter. |
required |
exclude_classes_by_name
¶ |
list[str] | None
|
list[str] or None Category names to exclude. |
required |
exclude_classes_by_id
¶ |
list[int] | None
|
list[int] or None Category IDs to exclude. |
required |
Returns:
| Type | Description |
|---|---|
list[ObjectPrediction]
|
list[ObjectPrediction]: Predictions not matching any exclusion criterion. |
Source code in sahi/predict.py
get_prediction
¶
get_prediction(
image: str | ndarray | Image,
detection_model: DetectionModel,
shift_amount: list[int] | None = None,
full_shape: list[int] | None = None,
postprocess: PostprocessPredictions | None = None,
verbose: int = 0,
exclude_classes_by_name: list[str] | None = None,
exclude_classes_by_id: list[int] | None = None,
confidence_threshold: float | None = None,
) -> PredictionResult
Function for performing prediction for given image using given detection_model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
¶ |
str | ndarray | Image
|
str or np.ndarray Location of image or numpy image matrix to slice |
required |
detection_model
¶ |
DetectionModel
|
model.DetectionMode |
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
|
postprocess
¶ |
PostprocessPredictions | None
|
sahi.postprocess.combine.PostprocessPredictions |
None
|
verbose
¶ |
int
|
int 0: no print (default) 1: print prediction duration |
0
|
exclude_classes_by_name
¶ |
list[str] | None
|
Optional[List[str]] None: if no classes are excluded List[str]: set of classes to exclude using its/their class label name/s |
None
|
exclude_classes_by_id
¶ |
list[int] | None
|
Optional[List[int]] None: if no classes are excluded List[int]: set of classes to exclude using one or more IDs |
None
|
confidence_threshold
¶ |
float | None
|
float, optional Override the model's confidence threshold for this call only. The model's original threshold is restored after the call. |
None
|
Example
from sahi import AutoDetectionModel from sahi.predict import get_prediction model = AutoDetectionModel.from_pretrained( ... model_type="ultralytics", ... model_path="yolo11n.pt", ... confidence_threshold=0.3, ... )
run with a different threshold without changing the model¶
result = get_prediction("image.jpg", model, confidence_threshold=0.7) print(model.confidence_threshold) # still 0.3
Returns:
| Type | Description |
|---|---|
PredictionResult
|
A dict with fields: object_prediction_list: a list of ObjectPrediction durations_in_seconds: a dict containing elapsed times for profiling |
Source code in sahi/predict.py
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
get_sliced_prediction
¶
get_sliced_prediction(
image: str | ndarray | Image,
detection_model: DetectionModel | None = None,
slice_height: int | None = None,
slice_width: int | None = None,
overlap_height_ratio: float = 0.2,
overlap_width_ratio: float = 0.2,
perform_standard_pred: bool = True,
postprocess_type: str = "GREEDYNMM",
postprocess_match_metric: str = "IOS",
postprocess_match_threshold: float = 0.5,
postprocess_class_agnostic: bool = False,
verbose: int = 1,
merge_buffer_length: int | None = None,
auto_slice_resolution: bool = True,
slice_export_prefix: str | None = None,
slice_dir: str | None = None,
exclude_classes_by_name: list[str] | None = None,
exclude_classes_by_id: list[int] | None = None,
progress_bar: bool = False,
progress_callback: Callable | None = None,
batch_size: int = 1,
force_postprocess_type: bool = False,
confidence_threshold: float | None = None,
) -> PredictionResult
Function for slice image + get predicion for each slice + combine predictions in full image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
¶ |
str | ndarray | Image
|
str or np.ndarray Location of image or numpy image matrix to slice |
required |
detection_model
¶ |
DetectionModel | None
|
model.DetectionModel |
None
|
slice_height
¶ |
int | None
|
int
Height of each slice. Defaults to |
None
|
slice_width
¶ |
int | None
|
int
Width of each slice. Defaults to |
None
|
overlap_height_ratio
¶ |
float
|
float
Fractional overlap in height of each window (e.g. an overlap of 0.2 for a window
of size 512 yields an overlap of 102 pixels).
Default to |
0.2
|
overlap_width_ratio
¶ |
float
|
float
Fractional overlap in width of each window (e.g. an overlap of 0.2 for a window
of size 512 yields an overlap of 102 pixels).
Default to |
0.2
|
perform_standard_pred
¶ |
bool
|
bool Perform a standard prediction on top of sliced predictions to increase large object detection accuracy. Default: True. |
True
|
postprocess_type
¶ |
str
|
str Type of the postprocess to be used after sliced inference while merging/eliminating predictions. Options are 'NMM', 'GREEDYNMM' or 'NMS'. Default is 'GREEDYNMM'. |
'GREEDYNMM'
|
postprocess_match_metric
¶ |
str
|
str Metric to be used during object prediction matching after sliced prediction. 'IOU' for intersection over union, 'IOS' for intersection over smaller area. |
'IOS'
|
postprocess_match_threshold
¶ |
float
|
float Sliced predictions having higher iou than postprocess_match_threshold will be postprocessed after sliced prediction. |
0.5
|
postprocess_class_agnostic
¶ |
bool
|
bool If True, postprocess will ignore category ids. |
False
|
verbose
¶ |
int
|
int 0: no print 1: print number of slices (default) 2: print number of slices and slice/prediction durations |
1
|
merge_buffer_length
¶ |
int | None
|
int The length of buffer for slices to be used during sliced prediction, which is suitable for low memory. It may affect the AP if it is specified. The higher the amount, the closer results to the non-buffered. scenario. See the discussion. |
None
|
auto_slice_resolution
¶ |
bool
|
bool if slice parameters (slice_height, slice_width) are not given, it enables automatically calculate these params from image resolution and orientation. |
True
|
slice_export_prefix
¶ |
str | None
|
str Prefix for the exported slices. Defaults to None. |
None
|
slice_dir
¶ |
str | None
|
str Directory to save the slices. Defaults to None. |
None
|
exclude_classes_by_name
¶ |
list[str] | None
|
Optional[List[str]] None: if no classes are excluded List[str]: set of classes to exclude using its/their class label name/s |
None
|
exclude_classes_by_id
¶ |
list[int] | None
|
Optional[List[int]] None: if no classes are excluded List[int]: set of classes to exclude using one or more IDs |
None
|
progress_bar
¶ |
bool
|
bool Whether to show progress bar for slice processing. Default: False. |
False
|
progress_callback
¶ |
Callable | None
|
callable A callback function that will be called after each slice is processed. The function should accept two arguments: (current_slice, total_slices) |
None
|
batch_size
¶ |
int
|
int Number of slices to process in a single batch inference call. Increasing this value can improve GPU utilization and throughput. Default: 1 (sequential, same as previous behavior). |
1
|
force_postprocess_type
¶ |
bool
|
bool If True, the auto postprocess type switch will be disabled. When False (default) and the detection model's confidence threshold is below LOW_MODEL_CONFIDENCE (0.1), the postprocess type will be automatically switched to NMS/IOU to avoid bounding box enlargement from merge operations. Default: False. |
False
|
confidence_threshold
¶ |
float | None
|
float, optional Override the model's confidence threshold for this call only. The model's original threshold is restored after the call. |
None
|
Example
from sahi import AutoDetectionModel from sahi.predict import get_sliced_prediction model = AutoDetectionModel.from_pretrained( ... model_type="ultralytics", ... model_path="yolo11n.pt", ... confidence_threshold=0.3, ... )
sweep thresholds without recreating the model¶
for thresh in [0.3, 0.5, 0.7]: ... result = get_sliced_prediction("image.jpg", model, confidence_threshold=thresh) ... print(thresh, len(result.object_prediction_list)) print(model.confidence_threshold) # still 0.3
Returns:
| Type | Description |
|---|---|
PredictionResult
|
A Dict with fields: object_prediction_list: a list of sahi.prediction.ObjectPrediction durations_in_seconds: a dict containing elapsed times for profiling |
Source code in sahi/predict.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 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 424 425 426 427 428 429 430 431 432 433 | |
bbox_sort
¶
Compare two bounding boxes for reading-order sorting.
Boxes whose Y-coordinates differ by no more than thresh are
considered to be on the same row and are sorted by X-coordinate.
Otherwise they are sorted by Y-coordinate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
¶ |
tuple
|
tuple
First bounding box as |
required |
b
¶ |
tuple
|
tuple
Second bounding box as |
required |
thresh
¶ |
int | float
|
int or float Maximum Y-coordinate difference to treat two boxes as being on the same row. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Negative if |
int
|
should come first, zero if equal. |
Source code in sahi/predict.py
agg_prediction
¶
agg_prediction(
result: PredictionResult, thresh: float
) -> list
Aggregate predictions by merging overlapping bounding boxes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
¶ |
PredictionResult
|
Prediction result object containing detections. |
required |
thresh
¶ |
float
|
Threshold for bounding box overlap merging. |
required |
Returns:
| Type | Description |
|---|---|
list
|
List of aggregated bounding boxes and associated data. |
Source code in sahi/predict.py
predict
¶
predict(
detection_model: DetectionModel | None = None,
model_type: str = "ultralytics",
model_path: str | None = None,
model_config_path: str | None = None,
model_confidence_threshold: float = 0.25,
model_device: str | None = None,
model_category_mapping: dict | None = None,
model_category_remapping: dict | None = None,
source: str | None = None,
no_standard_prediction: bool = False,
no_sliced_prediction: bool = False,
image_size: int | None = None,
slice_height: int = 512,
slice_width: int = 512,
overlap_height_ratio: float = 0.2,
overlap_width_ratio: float = 0.2,
postprocess_type: str = "GREEDYNMM",
postprocess_match_metric: str = "IOS",
postprocess_match_threshold: float = 0.5,
postprocess_class_agnostic: bool = False,
novisual: bool = False,
view_video: bool = False,
frame_skip_interval: int = 0,
export_pickle: bool = False,
export_crop: bool = False,
dataset_json_path: str | None = None,
project: str = "runs/predict",
name: str = "exp",
visual_bbox_thickness: int | None = None,
visual_text_size: float | None = None,
visual_text_thickness: int | None = None,
visual_hide_labels: bool = False,
visual_hide_conf: bool = False,
visual_export_format: str = "png",
verbose: int = 1,
return_dict: bool = False,
force_postprocess_type: bool = False,
exclude_classes_by_name: list[str] | None = None,
exclude_classes_by_id: list[int] | None = None,
progress_bar: bool = False,
batch_size: int = 1,
**kwargs: Any,
) -> dict | None
Performs prediction for all present images in given folder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
detection_model
¶ |
DetectionModel | None
|
sahi.model.DetectionModel Optionally provide custom DetectionModel to be used for inference. When provided, model_type, model_path, config_path, model_device, model_category_mapping, image_size params will be ignored |
None
|
model_type
¶ |
str
|
str mmdet for 'MmdetDetectionModel', 'yolov5' for 'Yolov5DetectionModel'. |
'ultralytics'
|
model_path
¶ |
str | None
|
str Path for the model weight |
None
|
model_config_path
¶ |
str | None
|
str Path for the detection model config file |
None
|
model_confidence_threshold
¶ |
float
|
float All predictions with score < model_confidence_threshold will be discarded. |
0.25
|
model_device
¶ |
str | None
|
str Torch device, "cpu" or "cuda" |
None
|
model_category_mapping
¶ |
dict | None
|
dict Mapping from category id (str) to category name (str) e.g. {"1": "pedestrian"} |
None
|
model_category_remapping
¶ |
dict | None
|
dict: str to int Remap category ids after performing inference |
None
|
source
¶ |
str | None
|
str Folder directory that contains images or path of the image to be predicted. Also video to be predicted. |
None
|
no_standard_prediction
¶ |
bool
|
bool Dont perform standard prediction. Default: False. |
False
|
no_sliced_prediction
¶ |
bool
|
bool Dont perform sliced prediction. Default: False. |
False
|
image_size
¶ |
int | None
|
int Input image size for each inference (image is scaled by preserving asp. rat.). |
None
|
slice_height
¶ |
int
|
int
Height of each slice. Defaults to |
512
|
slice_width
¶ |
int
|
int
Width of each slice. Defaults to |
512
|
overlap_height_ratio
¶ |
float
|
float
Fractional overlap in height of each window (e.g. an overlap of 0.2 for a window
of size 512 yields an overlap of 102 pixels).
Default to |
0.2
|
overlap_width_ratio
¶ |
float
|
float
Fractional overlap in width of each window (e.g. an overlap of 0.2 for a window
of size 512 yields an overlap of 102 pixels).
Default to |
0.2
|
postprocess_type
¶ |
str
|
str Type of the postprocess to be used after sliced inference while merging/eliminating predictions. Options are 'NMM', 'GREEDYNMM', 'LSNMS' or 'NMS'. Default is 'GREEDYNMM'. |
'GREEDYNMM'
|
postprocess_match_metric
¶ |
str
|
str Metric to be used during object prediction matching after sliced prediction. 'IOU' for intersection over union, 'IOS' for intersection over smaller area. |
'IOS'
|
postprocess_match_threshold
¶ |
float
|
float Sliced predictions having higher iou than postprocess_match_threshold will be postprocessed after sliced prediction. |
0.5
|
postprocess_class_agnostic
¶ |
bool
|
bool If True, postprocess will ignore category ids. |
False
|
novisual
¶ |
bool
|
bool Dont export predicted video/image visuals. |
False
|
view_video
¶ |
bool
|
bool View result of prediction during video inference. |
False
|
frame_skip_interval
¶ |
int
|
int If view_video or export_visual is slow, you can process one frames of 3(for exp: --frame_skip_interval=3). |
0
|
export_pickle
¶ |
bool
|
bool Export predictions as .pickle |
False
|
export_crop
¶ |
bool
|
bool Export predictions as cropped images. |
False
|
dataset_json_path
¶ |
str | None
|
str If coco file path is provided, detection results will be exported in coco json format. |
None
|
project
¶ |
str
|
str Save results to project/name. |
'runs/predict'
|
name
¶ |
str
|
str Save results to project/name. |
'exp'
|
visual_bbox_thickness
¶ |
int | None
|
int, optional Line thickness (in pixels) for bounding boxes in exported visualizations. If None, a default thickness is chosen based on image size. |
None
|
visual_text_size
¶ |
float | None
|
float, optional Font scale/size for label text in exported visualizations. If None, a sensible default is used. |
None
|
visual_text_thickness
¶ |
int | None
|
int, optional Thickness of text labels. If None, a sensible default is used. |
None
|
visual_hide_labels
¶ |
bool
|
bool, optional If True, class label names won't be shown on the exported visuals. |
False
|
visual_hide_conf
¶ |
bool
|
bool, optional If True, confidence scores won't be shown on the exported visuals. |
False
|
visual_export_format
¶ |
str
|
str, optional
Output image format to use when exporting visuals. Supported values are
'png' (default) and 'jpg'. Note that 'jpg' uses lossy compression and may
produce smaller files. This parameter is ignored when |
'png'
|
verbose
¶ |
int
|
int 0: no print 1: print slice/prediction durations, number of slices 2: print model loading/file exporting durations |
1
|
return_dict
¶ |
bool
|
bool If True, returns a dict with 'export_dir' field. |
False
|
force_postprocess_type
¶ |
bool
|
bool If True, auto postprocess check will e disabled |
False
|
exclude_classes_by_name
¶ |
list[str] | None
|
Optional[List[str]] None: if no classes are excluded List[str]: set of classes to exclude using its/their class label name/s |
None
|
exclude_classes_by_id
¶ |
list[int] | None
|
Optional[List[int]] None: if no classes are excluded List[int]: set of classes to exclude using one or more IDs |
None
|
progress_bar
¶ |
bool
|
bool Whether to show a progress bar. Default is False. |
False
|
batch_size
¶ |
int
|
int Batch size for processing images. Default is 1. |
1
|
**kwargs
¶ |
Any
|
Additional keyword arguments passed to the prediction pipeline. |
{}
|
Source code in sahi/predict.py
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 | |
predict_fiftyone
¶
predict_fiftyone(
model_type: str = "mmdet",
model_path: str | None = None,
model_config_path: str | None = None,
model_confidence_threshold: float = 0.25,
model_device: str | None = None,
model_category_mapping: dict | None = None,
model_category_remapping: dict | None = None,
dataset_json_path: str = "",
image_dir: str = "",
no_standard_prediction: bool = False,
no_sliced_prediction: bool = False,
image_size: int | None = None,
slice_height: int = 256,
slice_width: int = 256,
overlap_height_ratio: float = 0.2,
overlap_width_ratio: float = 0.2,
postprocess_type: str = "GREEDYNMM",
postprocess_match_metric: str = "IOS",
postprocess_match_threshold: float = 0.5,
postprocess_class_agnostic: bool = False,
verbose: int = 1,
exclude_classes_by_name: list[str] | None = None,
exclude_classes_by_id: list[int] | None = None,
progress_bar: bool = False,
batch_size: int = 1,
) -> None
Performs prediction for all present images in given folder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_type
¶ |
str
|
str mmdet for 'MmdetDetectionModel', 'yolov5' for 'Yolov5DetectionModel'. |
'mmdet'
|
model_path
¶ |
str | None
|
str Path for the model weight |
None
|
model_config_path
¶ |
str | None
|
str Path for the detection model config file |
None
|
model_confidence_threshold
¶ |
float
|
float All predictions with score < model_confidence_threshold will be discarded. |
0.25
|
model_device
¶ |
str | None
|
str Torch device, "cpu" or "cuda" |
None
|
model_category_mapping
¶ |
dict | None
|
dict Mapping from category id (str) to category name (str) e.g. {"1": "pedestrian"} |
None
|
model_category_remapping
¶ |
dict | None
|
dict: str to int Remap category ids after performing inference |
None
|
dataset_json_path
¶ |
str
|
str If coco file path is provided, detection results will be exported in coco json format. |
''
|
image_dir
¶ |
str
|
str Folder directory that contains images or path of the image to be predicted. |
''
|
no_standard_prediction
¶ |
bool
|
bool Dont perform standard prediction. Default: False. |
False
|
no_sliced_prediction
¶ |
bool
|
bool Dont perform sliced prediction. Default: False. |
False
|
image_size
¶ |
int | None
|
int Input image size for each inference (image is scaled by preserving asp. rat.). |
None
|
slice_height
¶ |
int
|
int
Height of each slice. Defaults to |
256
|
slice_width
¶ |
int
|
int
Width of each slice. Defaults to |
256
|
overlap_height_ratio
¶ |
float
|
float
Fractional overlap in height of each window (e.g. an overlap of 0.2 for a window
of size 256 yields an overlap of 51 pixels).
Default to |
0.2
|
overlap_width_ratio
¶ |
float
|
float
Fractional overlap in width of each window (e.g. an overlap of 0.2 for a window
of size 256 yields an overlap of 51 pixels).
Default to |
0.2
|
postprocess_type
¶ |
str
|
str Type of the postprocess to be used after sliced inference while merging/eliminating predictions. Options are 'NMM', 'GREEDYNMM' or 'NMS'. Default is 'GREEDYNMM'. |
'GREEDYNMM'
|
postprocess_match_metric
¶ |
str
|
str Metric to be used during object prediction matching after sliced prediction. 'IOU' for intersection over union, 'IOS' for intersection over smaller area. |
'IOS'
|
postprocess_match_metric
¶ |
str
|
str Metric to be used during object prediction matching after sliced prediction. 'IOU' for intersection over union, 'IOS' for intersection over smaller area. |
'IOS'
|
postprocess_match_threshold
¶ |
float
|
float Sliced predictions having higher iou than postprocess_match_threshold will be postprocessed after sliced prediction. |
0.5
|
postprocess_class_agnostic
¶ |
bool
|
bool If True, postprocess will ignore category ids. |
False
|
verbose
¶ |
int
|
int 0: no print 1: print slice/prediction durations, number of slices, model loading/file exporting durations |
1
|
exclude_classes_by_name
¶ |
list[str] | None
|
Optional[List[str]] None: if no classes are excluded List[str]: set of classes to exclude using its/their class label name/s |
None
|
exclude_classes_by_id
¶ |
list[int] | None
|
Optional[List[int]] None: if no classes are excluded List[int]: set of classes to exclude using one or more IDs |
None
|
progress_bar
¶ |
bool
|
bool Whether to show progress bar for slice processing. Default: False. |
False
|
batch_size
¶ |
int
|
int Batch size for processing images. Default is 1. |
1
|
Source code in sahi/predict.py
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 | |
prediction
¶
Prediction classes for object detection results.
Classes¶
PredictionScore
¶
PredictionScore(value: float | ndarray)
Wrapper around a numeric prediction confidence score.
Provides comparison operators and conversion from numpy scalars to native Python floats for serialization safety.
Initialize PredictionScore.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
¶ |
float | ndarray
|
prediction score between 0 and 1. |
required |
Source code in sahi/prediction.py
ObjectPrediction
¶
ObjectPrediction(
bbox: list[float] | None = None,
category_id: int | None = None,
category_name: str | None = None,
segmentation: list[list[float]] | None = None,
score: float = 0.0,
shift_amount: list[int]
| list[int | float]
| None = None,
full_shape: list[int] | list[int | float] | None = None,
)
Bases: ObjectAnnotation
Class for handling detection model predictions.
Initialize ObjectPrediction from bbox, score, category_id, category_name, segmentation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
¶ |
list[float] | None
|
list [minx, miny, maxx, maxy] |
None
|
score
¶ |
float
|
float Prediction score between 0 and 1 |
0.0
|
category_id
¶ |
int | None
|
int ID of the object category |
None
|
category_name
¶ |
str | None
|
str Name of the object category |
None
|
segmentation
¶ |
list[list[float]] | None
|
List[List] [ [x1, y1, x2, y2, x3, y3, ...], [x1, y1, x2, y2, x3, y3, ...], ... ] |
None
|
shift_amount
¶ |
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
|
full_shape
¶ |
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/prediction.py
get_shifted_object_prediction
¶get_shifted_object_prediction() -> ObjectPrediction
Get shifted version of ObjectPrediction.
Shifts bbox and mask coords. Used for mapping sliced predictions over full image.
Source code in sahi/prediction.py
to_coco_prediction
¶to_coco_prediction(
image_id: int | None = None,
) -> CocoPrediction
Convert to sahi.utils.coco.CocoPrediction representation.
Source code in sahi/prediction.py
to_fiftyone_detection
¶Convert to fiftyone.Detection representation.
Source code in sahi/prediction.py
PredictionResult
¶
PredictionResult(
object_prediction_list: list[ObjectPrediction],
image: Image | str | ndarray,
durations_in_seconds: dict[str, Any] = dict(),
)
Container for detection results on a single image.
Holds the list of ObjectPrediction instances together with the
source image and optional profiling durations. Provides helpers for
exporting results to COCO, FiftyOne, and visual formats.
Initialize a PredictionResult.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
object_prediction_list
¶ |
list[ObjectPrediction]
|
list[ObjectPrediction] Detected objects for this image. |
required |
image
¶ |
Image | str | ndarray
|
Image.Image or str or np.ndarray The source image as a PIL Image, file path, or numpy array. |
required |
durations_in_seconds
¶ |
dict[str, Any]
|
dict[str, Any] Elapsed times for profiling (e.g. inference, postprocess). |
dict()
|
Source code in sahi/prediction.py
export_visuals
¶export_visuals(
export_dir: str,
text_size: float | None = None,
rect_th: int | None = None,
hide_labels: bool = False,
hide_conf: bool = False,
file_name: str = "prediction_visual",
) -> None
Export prediction visualizations to directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
export_dir
¶ |
str
|
directory for resulting visualization to be exported. |
required |
text_size
¶ |
float | None
|
size of the category name over box. |
None
|
rect_th
¶ |
int | None
|
rectangle thickness. |
None
|
hide_labels
¶ |
bool
|
hide labels. |
False
|
hide_conf
¶ |
bool
|
hide confidence. |
False
|
file_name
¶ |
str
|
saving name. |
'prediction_visual'
|
Source code in sahi/prediction.py
to_coco_annotations
¶Convert predictions to COCO annotation format.
Source code in sahi/prediction.py
to_coco_predictions
¶Convert predictions to COCO prediction format.
Source code in sahi/prediction.py
to_imantics_annotations
¶Convert predictions to imantics annotation format.
Source code in sahi/prediction.py
to_fiftyone_detections
¶Convert predictions to FiftyOne detection format.
Source code in sahi/prediction.py
Functions:¶
scripts
¶
Command-line scripts for SAHI utilities.
Modules¶
coco2fiftyone
¶
Convert COCO dataset annotations to FiftyOne format.
main
¶main(
image_dir: str,
dataset_json_path: str,
*result_json_paths: str,
iou_thresh: float = 0.5,
) -> None
Convert COCO dataset to FiftyOne and optionally evaluate detection results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_dir
¶ |
str
|
Directory containing COCO images. |
required |
dataset_json_path
¶ |
str
|
Path to the COCO dataset JSON file. |
required |
result_json_paths
¶ |
str
|
Paths to COCO result JSON files. |
()
|
iou_thresh
¶ |
float
|
IoU threshold for COCO evaluation. |
0.5
|
Source code in sahi/scripts/coco2fiftyone.py
coco2yolo
¶
Convert COCO dataset annotations to YOLO format.
main
¶main(
image_dir: str,
dataset_json_path: str,
train_split: int | float = 0.9,
project: str = "runs/coco2yolo",
name: str = "exp",
seed: int = 1,
disable_symlink: bool = False,
) -> None
Convert COCO dataset annotations to YOLO format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_dir
¶ |
str
|
Directory containing COCO images. |
required |
dataset_json_path
¶ |
str
|
Path to the COCO JSON file to be converted. |
required |
train_split
¶ |
int | float
|
Training/validation split ratio. |
0.9
|
project
¶ |
str
|
Project directory for results. |
'runs/coco2yolo'
|
name
¶ |
str
|
Experiment name within project. |
'exp'
|
seed
¶ |
int
|
Random seed for reproducibility. |
1
|
disable_symlink
¶ |
bool
|
Disable symlinks (needed for Google Colab). |
False
|
Source code in sahi/scripts/coco2yolo.py
coco_error_analysis
¶
Error analysis utilities for COCO detection results.
analyse
¶analyse(
dataset_json_path: str,
result_json_path: str,
out_dir: str | None = None,
type: str = "bbox",
no_extraplots: bool = False,
areas: list[int] = [1024, 9216, 10000000000],
max_detections: int = 500,
return_dict: bool = False,
) -> dict | None
Analyze COCO detection results and generate error analysis plots.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_json_path
¶ |
str
|
File path for the COCO dataset JSON file. |
required |
result_json_path
¶ |
str
|
File path for the COCO result JSON file. |
required |
out_dir
¶ |
str | None
|
Directory to save analysis result images. |
None
|
no_extraplots
¶ |
bool
|
If True, do not export extra bar/stat plots. |
False
|
type
¶ |
str
|
Detection type, either 'bbox' or 'mask'. |
'bbox'
|
areas
¶ |
list[int]
|
Area regions for COCO evaluation calculations. |
[1024, 9216, 10000000000]
|
max_detections
¶ |
int
|
Maximum number of detections to consider for AP calculation. Default is 500. |
500
|
return_dict
¶ |
bool
|
If True, returns a dict of export paths. |
False
|
Returns:
| Type | Description |
|---|---|
dict | None
|
Dict of export paths if return_dict is True, otherwise None. |
Source code in sahi/scripts/coco_error_analysis.py
coco_evaluation
¶
COCO dataset evaluation and analysis utilities.
evaluate_core
¶evaluate_core(
dataset_path: str,
result_path: str,
COCO: type,
COCOeval: type,
metric: str = "bbox",
classwise: bool = False,
max_detections: int = 500,
iou_thrs: list[float] | float | None = None,
metric_items: list[str] | None = None,
out_dir: str | Path | None = None,
areas: list[int] = [1024, 9216, 10000000000],
) -> dict
Evaluate detection results using COCO protocol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_path
¶ |
str
|
COCO dataset JSON path. |
required |
result_path
¶ |
str
|
COCO result JSON path. |
required |
COCO
¶ |
type
|
COCO class after safely imported. |
required |
COCOeval
¶ |
type
|
COCOeval class after safely imported. |
required |
metric
¶ |
str | list[str]
|
Metrics to be evaluated. Options are 'bbox', 'segm', 'proposal'. |
'bbox'
|
classwise
¶ |
bool
|
Whether to evaluating the AP for each class. |
False
|
max_detections
¶ |
int
|
Maximum number of detections to consider for AP calculation. Default: 500 |
500
|
iou_thrs
¶ |
List[float]
|
IoU threshold used for evaluating recalls/mAPs. If set to a list, the average of all IoUs will also be computed. If not specified, [0.50, 0.55, 0.60, 0.65, 0.70, 0.75, 0.80, 0.85, 0.90, 0.95] will be used. Default: None. |
None
|
metric_items
¶ |
list[str] | str
|
Metric items that will
be returned. If not specified, |
None
|
out_dir
¶ |
str
|
Directory to save evaluation result json. |
None
|
areas
¶ |
List[int]
|
area regions for coco evaluation calculations |
[1024, 9216, 10000000000]
|
Returns: dict: eval_results (dict[str, float]): COCO style evaluation metric. export_path (str): Path for the exported eval result json.
Source code in sahi/scripts/coco_evaluation.py
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | |
evaluate
¶evaluate(
dataset_json_path: str,
result_json_path: str,
out_dir: str | None = None,
type: Literal["bbox", "segm"] = "bbox",
classwise: bool = False,
max_detections: int = 500,
iou_thrs: list[float] | float | None = None,
areas: list[int] = [1024, 9216, 10000000000],
return_dict: bool = False,
) -> dict
Evaluate COCO object detection results and compute metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_json_path
¶ |
str
|
File path for the COCO dataset JSON file. |
required |
result_json_path
¶ |
str
|
File path for the COCO result JSON file. |
required |
out_dir
¶ |
str | None
|
Directory to save evaluation results. |
None
|
type
¶ |
Literal['bbox', 'segm']
|
Detection type, either 'bbox' or 'segm'. |
'bbox'
|
classwise
¶ |
bool
|
If True, evaluate AP for each class separately. |
False
|
max_detections
¶ |
int
|
Maximum number of detections to consider for AP calculation. Default is 500. |
500
|
iou_thrs
¶ |
list[float] | float | None
|
IoU threshold(s) used for evaluating recalls and mAPs. |
None
|
areas
¶ |
list[int]
|
Area regions for COCO evaluation calculations. |
[1024, 9216, 10000000000]
|
return_dict
¶ |
bool
|
If True, returns a dict with 'eval_results' and 'export_path' fields. |
False
|
Returns:
| Type | Description |
|---|---|
dict
|
Dict containing evaluation results and export path if return_dict is True, |
dict
|
otherwise None. |
Source code in sahi/scripts/coco_evaluation.py
predict
¶
predict_fiftyone
¶
Command-line interface for SAHI predictions with FiftyOne integration.
slice_coco
¶
Slice COCO dataset images and annotations.
slicer
¶slicer(
image_dir: str,
dataset_json_path: str,
slice_size: int = 512,
overlap_ratio: float = 0.2,
ignore_negative_samples: bool = False,
output_dir: str = "runs/slice_coco",
min_area_ratio: float = 0.1,
) -> None
Slice COCO dataset into smaller images.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_dir
¶ |
str
|
Directory containing COCO images. |
required |
dataset_json_path
¶ |
str
|
Path to COCO dataset JSON file. |
required |
slice_size
¶ |
int
|
Size of each slice in pixels. |
512
|
overlap_ratio
¶ |
float
|
Overlap ratio between slices. |
0.2
|
ignore_negative_samples
¶ |
bool
|
Skip images without annotations. |
False
|
output_dir
¶ |
str
|
Output directory for sliced results. |
'runs/slice_coco'
|
min_area_ratio
¶ |
float
|
Minimum area ratio for cropped annotations. If the annotation ratio is smaller than this value, the annotation is filtered out. Default 0.1. |
0.1
|
Source code in sahi/scripts/slice_coco.py
slicing
¶
Image slicing utilities for splitting large images into tiles.
Classes¶
SlicedImage
¶
SlicedImage(
image: ndarray,
coco_image: CocoImage,
starting_pixel: list[int],
)
Container for a sliced image and its metadata.
Initialize SlicedImage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
¶ |
ndarray
|
np.array Sliced image. |
required |
coco_image
¶ |
CocoImage
|
CocoImage Coco styled image object that belong to sliced image. |
required |
starting_pixel
¶ |
list[int]
|
list of list of int Starting pixel coordinates of the sliced image. |
required |
Source code in sahi/slicing.py
SliceImageResult
¶
SliceImageResult(
original_image_size: list[int],
image_dir: str | None = None,
original_image: ndarray | None = None,
)
Container for sliced image results.
Initialize SliceImageResult.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_dir
¶ |
str | None
|
str Directory of the sliced image exports. |
None
|
original_image_size
¶ |
list[int]
|
list of int Size of the unsliced original image in [height, width]. |
required |
original_image
¶ |
ndarray | None
|
np.ndarray, optional The decoded source image. Every slice is a view into it, so it is alive for as long as this result is; holding it lets callers reuse the decode instead of reading the file again. |
None
|
Source code in sahi/slicing.py
images
property
¶Returns sliced images.
Returns:
| Name | Type | Description |
|---|---|---|
images |
list[ndarray]
|
a list of np.array |
coco_images
property
¶coco_images: list[CocoImage]
Returns CocoImage representation of SliceImageResult.
Returns:
| Name | Type | Description |
|---|---|---|
coco_images |
list[CocoImage]
|
a list of CocoImage |
starting_pixels
property
¶Returns a list of starting pixels for each slice.
Returns:
| Name | Type | Description |
|---|---|---|
starting_pixels |
list[list[int]]
|
a list of starting pixel coords [x,y] |
filenames
property
¶Returns a list of filenames for each slice.
Returns:
| Name | Type | Description |
|---|---|---|
filenames |
list[str]
|
a list of filenames as str |
add_sliced_image
¶add_sliced_image(sliced_image: SlicedImage) -> None
Add a sliced image to the result.
Source code in sahi/slicing.py
Functions:¶
get_slice_bboxes
¶
get_slice_bboxes(
image_height: int,
image_width: int,
slice_height: int | None = None,
slice_width: int | None = None,
auto_slice_resolution: bool | None = True,
overlap_height_ratio: float | None = 0.2,
overlap_width_ratio: float | None = 0.2,
) -> list[list[int]]
Generate bounding boxes for slicing an image into crops.
The function calculates the coordinates for each slice based on the provided image dimensions, slice size, and overlap ratios. If slice size is not provided and auto_slice_resolution is True, the function will automatically determine appropriate slice parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_height
¶ |
int
|
Height of the original image. |
required |
image_width
¶ |
int
|
Width of the original image. |
required |
slice_height
¶ |
int
|
Height of each slice. Default None. |
None
|
slice_width
¶ |
int
|
Width of each slice. Default None. |
None
|
overlap_height_ratio
¶ |
float
|
Fractional overlap in height of each slice (e.g. an overlap of 0.2 for a slice of size 100 yields an overlap of 20 pixels). Default 0.2. |
0.2
|
overlap_width_ratio
¶ |
float
|
Fractional overlap in width of each slice (e.g. an overlap of 0.2 for a slice of size 100 yields an overlap of 20 pixels). Default 0.2. |
0.2
|
auto_slice_resolution
¶ |
bool
|
if not set slice parameters such as slice_height and slice_width, it enables automatically calculate these parameters from image resolution and orientation. |
True
|
Returns:
| Type | Description |
|---|---|
list[list[int]]
|
List[List[int]]: List of 4 corner coordinates for each N slices. [ [slice_0_left, slice_0_top, slice_0_right, slice_0_bottom], ... [slice_N_left, slice_N_top, slice_N_right, slice_N_bottom] ] |
Source code in sahi/slicing.py
annotation_inside_slice
¶
annotation_inside_slice(
annotation: dict, slice_bbox: list[int]
) -> bool
Check whether annotation coordinates lie inside slice coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
annotation
¶ |
dict
|
Single annotation entry in COCO format. |
required |
slice_bbox
¶ |
List[int]
|
Generated from |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if any annotation coordinate lies inside slice. |
Source code in sahi/slicing.py
process_coco_annotations
¶
process_coco_annotations(
coco_annotation_list: list[CocoAnnotation],
slice_bbox: list[int],
min_area_ratio: float,
) -> list[CocoAnnotation]
Slices and filters given list of CocoAnnotation objects with given 'slice_bbox' and 'min_area_ratio'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coco_annotation_list
¶ |
list[CocoAnnotation]
|
List[CocoAnnotation] Annotations to slice and filter. |
required |
slice_bbox
¶ |
List[int]
|
Generated from |
required |
min_area_ratio
¶ |
float
|
If the cropped annotation area to original annotation ratio is smaller than this value, the annotation is filtered out. Default 0.1. |
required |
Returns:
| Type | Description |
|---|---|
List[CocoAnnotation]
|
Sliced annotations. |
Source code in sahi/slicing.py
slice_image
¶
slice_image(
image: str | Image | ndarray,
coco_annotation_list: list[CocoAnnotation]
| None = None,
output_file_name: str | None = None,
output_dir: str | None = None,
slice_height: int | None = None,
slice_width: int | None = None,
overlap_height_ratio: float | None = 0.2,
overlap_width_ratio: float | None = 0.2,
auto_slice_resolution: bool | None = True,
min_area_ratio: float | None = 0.1,
out_ext: str | None = None,
verbose: bool | None = False,
exif_fix: bool = True,
) -> SliceImageResult
Slice a large image into smaller windows. If output_file_name and output_dir is given, export sliced images.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
¶ |
str or Image
|
File path of image or Pillow Image to be sliced. |
required |
coco_annotation_list
¶ |
List[CocoAnnotation]
|
List of CocoAnnotation objects. |
None
|
output_file_name
¶ |
str
|
Root name of output files (coordinates will be appended to this) |
None
|
output_dir
¶ |
str
|
Output directory |
None
|
slice_height
¶ |
int
|
Height of each slice. Default None. |
None
|
slice_width
¶ |
int
|
Width of each slice. Default None. |
None
|
overlap_height_ratio
¶ |
float
|
Fractional overlap in height of each slice (e.g. an overlap of 0.2 for a slice of size 100 yields an overlap of 20 pixels). Default 0.2. |
0.2
|
overlap_width_ratio
¶ |
float
|
Fractional overlap in width of each slice (e.g. an overlap of 0.2 for a slice of size 100 yields an overlap of 20 pixels). Default 0.2. |
0.2
|
auto_slice_resolution
¶ |
bool
|
if not set slice parameters such as slice_height and slice_width, it enables automatically calculate these params from image resolution and orientation. |
True
|
min_area_ratio
¶ |
float
|
If the cropped annotation area to original annotation ratio is smaller than this value, the annotation is filtered out. Default 0.1. |
0.1
|
out_ext
¶ |
str
|
Extension of saved images. Default is the original suffix for lossless image formats and png for lossy formats ('.jpg','.jpeg'). |
None
|
verbose
¶ |
bool
|
Switch to print relevant values to screen. Default 'False'. |
False
|
exif_fix
¶ |
bool
|
Whether to apply an EXIF fix to the image. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
sliced_image_result |
SliceImageResult
|
SliceImageResult: sliced_image_list: list of SlicedImage image_dir: str Directory of the sliced image exports. original_image_size: list of int Size of the unsliced original image in [height, width] |
Source code in sahi/slicing.py
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 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 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | |
slice_coco
¶
slice_coco(
coco_annotation_file_path: str,
image_dir: str,
output_coco_annotation_file_name: str,
output_dir: str | None = None,
ignore_negative_samples: bool | None = False,
slice_height: int | None = 512,
slice_width: int | None = 512,
overlap_height_ratio: float | None = 0.2,
overlap_width_ratio: float | None = 0.2,
min_area_ratio: float | None = 0.1,
out_ext: str | None = None,
verbose: bool | None = False,
exif_fix: bool = True,
) -> tuple[dict, str]
Slice large images given in a directory into smaller windows.
If output_dir is given, export sliced images and coco file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coco_annotation_file_path
¶ |
str
|
Location of the coco annotation file |
required |
image_dir
¶ |
str
|
Base directory for the images |
required |
output_coco_annotation_file_name
¶ |
str
|
File name of the exported coco dataset json. |
required |
output_dir
¶ |
str
|
Output directory |
None
|
ignore_negative_samples
¶ |
bool
|
If True, images without annotations are ignored. Defaults to False. |
False
|
slice_height
¶ |
int
|
Height of each slice. Default 512. |
512
|
slice_width
¶ |
int
|
Width of each slice. Default 512. |
512
|
overlap_height_ratio
¶ |
float
|
Fractional overlap in height of each slice (e.g. an overlap of 0.2 for a slice of size 100 yields an overlap of 20 pixels). Default 0.2. |
0.2
|
overlap_width_ratio
¶ |
float
|
Fractional overlap in width of each slice (e.g. an overlap of 0.2 for a slice of size 100 yields an overlap of 20 pixels). Default 0.2. |
0.2
|
min_area_ratio
¶ |
float
|
If the cropped annotation area to original annotation ratio is smaller than this value, the annotation is filtered out. Default 0.1. |
0.1
|
out_ext
¶ |
str
|
Extension of saved images. Default is the original suffix. |
None
|
verbose
¶ |
bool
|
Switch to print relevant values to screen. |
False
|
exif_fix
¶ |
bool
|
Whether to apply an EXIF fix to the image. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
coco_dict |
dict
|
dict COCO dict for sliced images and annotations |
save_path |
str
|
str Path to the saved coco file |
Source code in sahi/slicing.py
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 | |
calc_ratio_and_slice
¶
calc_ratio_and_slice(
orientation: Literal[
"vertical", "horizontal", "square"
],
slide: int = 1,
ratio: float = 0.1,
) -> tuple[int, int, float, float]
Calculate overlap params according to image resolution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
orientation
¶ |
Literal['vertical', 'horizontal', 'square']
|
image capture angle. |
required |
slide
¶ |
int
|
sliding window. |
1
|
ratio
¶ |
float
|
buffer value. |
0.1
|
Returns:
| Type | Description |
|---|---|
tuple[int, int, float, float]
|
overlap params. |
Source code in sahi/slicing.py
calc_resolution_factor
¶
calc_resolution_factor(resolution: int) -> int
Calculate power(2,n) and return the closest smaller n for resolution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resolution
¶ |
int
|
the width and height of the image multiplied. such as 1024x720 = 737280. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Power value of 2 closest to the resolution. |
Source code in sahi/slicing.py
calc_aspect_ratio_orientation
¶
calc_aspect_ratio_orientation(
width: int, height: int
) -> Literal["vertical", "horizontal", "square"]
Calculate image capture orientation from aspect ratio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
width
¶ |
int
|
image width. |
required |
height
¶ |
int
|
image height. |
required |
Returns:
| Type | Description |
|---|---|
Literal['vertical', 'horizontal', 'square']
|
image capture orientation. |
Source code in sahi/slicing.py
calc_slice_and_overlap_params
¶
calc_slice_and_overlap_params(
resolution: str,
height: int,
width: int,
orientation: Literal[
"vertical", "horizontal", "square"
],
) -> tuple[int, int, int, int]
Calculate slice and overlap params according to image resolution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resolution
¶ |
str
|
str |
required |
height
¶ |
int
|
int |
required |
width
¶ |
int
|
int |
required |
orientation
¶ |
Literal['vertical', 'horizontal', 'square']
|
str. |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int, int, int]
|
x_overlap, y_overlap, slice_width, slice_height |
Source code in sahi/slicing.py
get_resolution_selector
¶
Get slicing parameters based on resolution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
res
¶ |
str
|
resolution of image such as low, medium. |
required |
height
¶ |
int
|
image height. |
required |
width
¶ |
int
|
image width. |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int, int, int]
|
overlap params from slicing params function. |
Source code in sahi/slicing.py
get_auto_slice_params
¶
Calculate overlap sliding window and buffer params from image dimensions.
Factor is the power value of 2 closest to the image resolution
- factor <= 18: low resolution image such as 300x300, 640x640
- 18 < factor <= 21: medium resolution image such as 1024x1024, 1336x960
- 21 < factor <= 24: high resolution image such as 2048x2048, 2048x4096, 4096x4096
- factor > 24: ultra-high resolution image such as 6380x6380, 4096x8192.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
height
¶ |
int
|
image height. |
required |
width
¶ |
int
|
image width. |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int, int, int]
|
slicing overlap params x_overlap, y_overlap, slice_width, slice_height. |
Source code in sahi/slicing.py
shift_bboxes
¶
Shift bboxes w.r.t offset.
Supports Tensor, np.ndarray, and list inputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bboxes
¶ |
(Tensor, ndarray, list)
|
The bboxes need to be translated. Its shape can be (n, 4), which means (x, y, x, y). |
required |
offset
¶ |
Sequence[int]
|
The translation offsets with shape of (2, ). |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Tensor, np.ndarray, list: Shifted bboxes. |
Source code in sahi/slicing.py
shift_masks
¶
shift_masks(
masks: ndarray,
offset: Sequence[int],
full_shape: Sequence[int],
) -> ndarray
Shift masks to the original image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
masks
¶ |
ndarray
|
masks that need to be shifted. |
required |
offset
¶ |
Sequence[int]
|
The offset to translate with shape of (2, ). |
required |
full_shape
¶ |
Sequence[int]
|
A (height, width) tuple of the huge image's shape. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Shifted masks. |
Source code in sahi/slicing.py
utils
¶
Utilities for SAHI object detection and image processing.
Modules¶
coco
¶
COCO dataset format utilities and classes for handling annotations and predictions.
CocoCategory
¶CocoCategory(
id: int = 0,
name: str | None = None,
supercategory: str | None = None,
)
COCO formatted category.
Initialize a COCO category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
¶ |
int
|
Category ID. |
0
|
name
¶ |
str | None
|
Category name. |
None
|
supercategory
¶ |
str | None
|
Supercategory name. |
None
|
Source code in sahi/utils/coco.py
from_coco_category
classmethod
¶from_coco_category(category: dict) -> _TCocoCategory
Create CocoCategory object using coco category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category
¶ |
dict
|
Dict {"supercategory": "person", "id": 1, "name": "person"}, |
required |
Source code in sahi/utils/coco.py
CocoAnnotation
¶CocoAnnotation(
category_id: int,
category_name: str | None = None,
segmentation: list[list[float]]
| list[list[int]]
| None = None,
bbox: list[int] | None = None,
image_id: int | None = None,
iscrowd: int = 0,
)
COCO formatted annotation.
Create coco annotation object using bbox or segmentation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
segmentation
¶ |
list[list[float]] | list[list[int]] | None
|
List[List]
|
None
|
bbox
¶ |
list[int] | None
|
List [xmin, ymin, width, height] |
None
|
category_id
¶ |
int
|
int Category id of the annotation |
required |
category_name
¶ |
str | None
|
str Category name of the annotation |
None
|
image_id
¶ |
int | None
|
int Image ID of the annotation |
None
|
iscrowd
¶ |
int
|
int 0 or 1 |
0
|
Source code in sahi/utils/coco.py
bbox
property
¶Returns coco formatted bbox of the annotation as [xmin, ymin, width, height].
segmentation
property
¶Returns coco formatted segmentation of the annotation as [[1, 1, 325, 125, 250, 200, 5, 200]].
category_name
property
writable
¶Returns category name of the annotation as str.
from_coco_segmentation
classmethod
¶from_coco_segmentation(
segmentation: list[list[float]] | list[list[int]],
category_id: int,
category_name: str,
iscrowd: int = 0,
) -> _TCocoAnnotation
Create CocoAnnotation object using coco segmentation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
segmentation
¶ |
list[list[float]] | list[list[int]]
|
List[List]
|
required |
category_id
¶ |
int
|
int Category id of the annotation |
required |
category_name
¶ |
str
|
str Category name of the annotation |
required |
iscrowd
¶ |
int
|
int 0 or 1 |
0
|
Source code in sahi/utils/coco.py
from_coco_bbox
classmethod
¶from_coco_bbox(
bbox: list[int],
category_id: int,
category_name: str,
iscrowd: int = 0,
) -> _TCocoAnnotation
Create CocoAnnotation object using coco bbox.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
¶ |
list[int]
|
List [xmin, ymin, width, height] |
required |
category_id
¶ |
int
|
int Category id of the annotation |
required |
category_name
¶ |
str
|
str Category name of the annotation |
required |
iscrowd
¶ |
int
|
int 0 or 1 |
0
|
Source code in sahi/utils/coco.py
from_coco_annotation_dict
classmethod
¶from_coco_annotation_dict(
annotation_dict: dict, category_name: str | None = None
) -> _TCocoAnnotation
Create CocoAnnotation object from category name and COCO formatted annotation dict.
Creates object from COCO formatted annotation dict with fields "bbox", "segmentation", "category_id".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category_name
¶ |
str | None
|
str Category name of the annotation |
None
|
annotation_dict
¶ |
dict
|
dict COCO formatted annotation dict (with fields "bbox", "segmentation", "category_id") |
required |
Source code in sahi/utils/coco.py
from_shapely_annotation
classmethod
¶from_shapely_annotation(
shapely_annotation: ShapelyAnnotation,
category_id: int,
category_name: str,
iscrowd: int,
) -> _TCocoAnnotation
Create CocoAnnotation object from ShapelyAnnotation object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shapely_annotation
¶ |
ShapelyAnnotation
|
ShapelyAnnotation object to convert. |
required |
category_id
¶ |
int
|
Category id of the annotation. |
required |
category_name
¶ |
str
|
Category name of the annotation. |
required |
iscrowd
¶ |
int
|
0 or 1. |
required |
Source code in sahi/utils/coco.py
get_sliced_coco_annotation
¶get_sliced_coco_annotation(
slice_bbox: list[int],
) -> CocoAnnotation
Get the annotation sliced by a bounding box.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slice_bbox
¶ |
list[int]
|
Bounding box to slice with as [xmin, ymin, xmax, ymax]. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
CocoAnnotation |
CocoAnnotation
|
The sliced annotation. |
Source code in sahi/utils/coco.py
CocoPrediction
¶CocoPrediction(
segmentation: list[list[float]]
| list[list[int]]
| None = None,
bbox: list[int] | None = None,
category_id: int = 0,
category_name: str = "",
image_id: int | None = None,
score: float | None = None,
iscrowd: int = 0,
)
Bases: CocoAnnotation
Class for handling predictions in coco format.
Initialize a COCO prediction object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
segmentation
¶ |
list[list[float]] | list[list[int]] | None
|
List[List]
|
None
|
bbox
¶ |
list[int] | None
|
List [xmin, ymin, width, height] |
None
|
category_id
¶ |
int
|
int Category id of the annotation |
0
|
category_name
¶ |
str
|
str Category name of the annotation |
''
|
image_id
¶ |
int | None
|
int Image ID of the annotation |
None
|
score
¶ |
float | None
|
float Prediction score between 0 and 1 |
None
|
iscrowd
¶ |
int
|
int 0 or 1. |
0
|
Source code in sahi/utils/coco.py
from_coco_segmentation
classmethod
¶from_coco_segmentation(
segmentation: list[list[float]] | list[list[int]],
category_id: int,
category_name: str,
score: float,
iscrowd: int = 0,
image_id: int | None = None,
) -> _TCocoPrediction
Create CocoAnnotation object using coco segmentation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
segmentation
¶ |
list[list[float]] | list[list[int]]
|
List[List]
|
required |
category_id
¶ |
int
|
int Category id of the annotation |
required |
category_name
¶ |
str
|
str Category name of the annotation |
required |
score
¶ |
float
|
float Prediction score between 0 and 1 |
required |
iscrowd
¶ |
int
|
int 0 or 1 |
0
|
image_id
¶ |
int | None
|
Image ID of the prediction. |
None
|
Source code in sahi/utils/coco.py
from_coco_bbox
classmethod
¶from_coco_bbox(
bbox: list[int],
category_id: int,
category_name: str,
score: float,
iscrowd: int = 0,
image_id: int | None = None,
) -> _TCocoPrediction
Create CocoAnnotation object using coco bbox.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
¶ |
list[int]
|
List [xmin, ymin, width, height] |
required |
category_id
¶ |
int
|
int Category id of the annotation |
required |
category_name
¶ |
str
|
str Category name of the annotation |
required |
score
¶ |
float
|
float Prediction score between 0 and 1 |
required |
iscrowd
¶ |
int
|
int 0 or 1 |
0
|
image_id
¶ |
int | None
|
Image ID of the prediction. |
None
|
Source code in sahi/utils/coco.py
from_coco_annotation_dict
classmethod
¶from_coco_annotation_dict(
category_name: str,
annotation_dict: dict,
score: float,
image_id: int | None = None,
) -> _TCocoPrediction
Create CocoAnnotation object from category name and COCO formatted annotation dict.
Creates object from COCO formatted annotation dict with fields "bbox", "segmentation", "category_id".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category_name
¶ |
str
|
str Category name of the annotation |
required |
annotation_dict
¶ |
dict
|
dict COCO formatted annotation dict (with fields "bbox", "segmentation", "category_id") |
required |
score
¶ |
float
|
float Prediction score between 0 and 1 |
required |
image_id
¶ |
int | None
|
Image ID of the prediction. |
None
|
Source code in sahi/utils/coco.py
CocoVidAnnotation
¶CocoVidAnnotation(
category_id: int,
category_name: str,
bbox: list[int],
image_id: int | None = None,
instance_id: int | None = None,
iscrowd: int = 0,
id: int | None = None,
)
Bases: CocoAnnotation
COCOVid formatted annotation.
https://github.com/open-mmlab/mmtracking/blob/master/docs/tutorials/customize_dataset.md#the-cocovid-annotation-file
Initialize a COCOVid annotation object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category_id
¶ |
int
|
Category id of the annotation. |
required |
category_name
¶ |
str
|
Category name of the annotation. |
required |
bbox
¶ |
list[int]
|
List [xmin, ymin, width, height]. |
required |
image_id
¶ |
int | None
|
Image ID of the annotation. |
None
|
instance_id
¶ |
int | None
|
Instance id used for tracking. |
None
|
iscrowd
¶ |
int
|
0 or 1. |
0
|
id
¶ |
int | None
|
Annotation id. |
None
|
Source code in sahi/utils/coco.py
CocoImage
¶COCO formatted image.
Create CocoImage object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
¶ |
int | None
|
int Image id |
None
|
file_name
¶ |
str
|
str Image path |
required |
height
¶ |
int
|
int Image height in pixels |
required |
width
¶ |
int
|
int Image width in pixels |
required |
Source code in sahi/utils/coco.py
from_coco_image_dict
classmethod
¶from_coco_image_dict(image_dict: dict) -> _TCocoImage
Create CocoImage object from COCO formatted image dict.
Creates object from COCO formatted image dict with fields "id", "file_name", "height" and "width".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_dict
¶ |
dict
|
dict COCO formatted image dict (with fields "id", "file_name", "height" and "weight") |
required |
Source code in sahi/utils/coco.py
add_annotation
¶add_annotation(annotation: CocoAnnotation) -> None
Add annotation to this CocoImage instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
annotation
¶ |
CocoAnnotation
|
CocoAnnotation object to add. |
required |
Source code in sahi/utils/coco.py
add_prediction
¶add_prediction(prediction: CocoPrediction) -> None
Add prediction to this CocoImage instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prediction
¶ |
CocoPrediction
|
CocoPrediction object to add. |
required |
Source code in sahi/utils/coco.py
CocoVidImage
¶CocoVidImage(
file_name: str,
height: int,
width: int,
video_id: int | None = None,
frame_id: int | None = None,
id: int | None = None,
)
Bases: CocoImage
COCOVid formatted image.
https://github.com/open-mmlab/mmtracking/blob/master/docs/tutorials/customize_dataset.md#the-cocovid-annotation-file
Create CocoVidImage object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
¶ |
int | None
|
int Image id |
None
|
file_name
¶ |
str
|
str Image path |
required |
height
¶ |
int
|
int Image height in pixels |
required |
width
¶ |
int
|
int Image width in pixels |
required |
frame_id
¶ |
int | None
|
int 0-indexed frame id |
None
|
video_id
¶ |
int | None
|
int Video id |
None
|
Source code in sahi/utils/coco.py
from_coco_image
classmethod
¶from_coco_image(
coco_image: CocoImage,
video_id: int | None = None,
frame_id: int | None = None,
) -> _TCocoVidImage
Create CocoVidImage object using CocoImage object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coco_image
¶ |
CocoImage
|
CocoImage |
required |
frame_id
¶ |
int | None
|
int 0-indexed frame id |
None
|
video_id
¶ |
int | None
|
int Video id |
None
|
Source code in sahi/utils/coco.py
add_annotation
¶add_annotation(annotation: CocoVidAnnotation) -> None
Add annotation to this CocoImage instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
annotation
¶ |
CocoVidAnnotation
|
CocoVidAnnotation object to add. |
required |
Source code in sahi/utils/coco.py
CocoVideo
¶CocoVideo(
name: str,
id: int | None = None,
fps: float | None = None,
height: int | None = None,
width: int | None = None,
)
COCO formatted video.
https://github.com/open-mmlab/mmtracking/blob/master/docs/tutorials/customize_dataset.md#the-cocovid-annotation-file
Create CocoVideo object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
¶ |
str
|
str Video name |
required |
id
¶ |
int | None
|
int Video id |
None
|
fps
¶ |
float | None
|
float Video fps |
None
|
height
¶ |
int | None
|
int Video height in pixels |
None
|
width
¶ |
int | None
|
int Video width in pixels |
None
|
Source code in sahi/utils/coco.py
add_image
¶ add_cocovidimage
¶add_cocovidimage(cocovidimage: CocoVidImage) -> None
Add CocoVidImage to this CocoVideo instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cocovidimage
¶ |
CocoVidImage
|
CocoVidImage. |
required |
Source code in sahi/utils/coco.py
Coco
¶Coco(
name: str | None = None,
image_dir: str | None = None,
remapping_dict: dict[int, int] | None = None,
ignore_negative_samples: bool = False,
clip_bboxes_to_img_dims: bool = False,
image_id_setting: Literal["auto", "manual"] = "auto",
)
COCO dataset object for managing images, annotations, and predictions.
Create Coco object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
¶ |
str | None
|
Name of the Coco dataset, determines exported json name. |
None
|
image_dir
¶ |
str | None
|
Base file directory that contains dataset images. Required for dataset merging. |
None
|
remapping_dict
¶ |
dict[int, int] | None
|
Maps category ids, e.g., {1:0, 2:1} maps category id 1 to 0. |
None
|
ignore_negative_samples
¶ |
bool
|
If True, ignores images without annotations. |
False
|
clip_bboxes_to_img_dims
¶ |
bool
|
If True, clips bounding boxes to image dimensions. |
False
|
image_id_setting
¶ |
Literal['auto', 'manual']
|
How to assign image ids while exporting ("auto" or "manual"). |
'auto'
|
Source code in sahi/utils/coco.py
category_mapping
property
¶Get mapping of category IDs to names.
add_categories_from_coco_category_list
¶add_categories_from_coco_category_list(
coco_category_list: list[dict],
) -> None
Create CocoCategory object using coco category list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coco_category_list
¶ |
list[dict]
|
List[Dict] [ {"supercategory": "person", "id": 1, "name": "person"}, {"supercategory": "vehicle", "id": 2, "name": "bicycle"} ] |
required |
Source code in sahi/utils/coco.py
add_category
¶add_category(category: CocoCategory) -> None
Add category to this Coco instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category
¶ |
CocoCategory
|
CocoCategory |
required |
Source code in sahi/utils/coco.py
add_image
¶ update_categories
¶update_categories(
desired_name2id: dict[str, int],
update_image_filenames: bool = False,
) -> None
Rearrange category mapping of given COCO object based on given desired_name2id.
Can also be used to filter some of the categories.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desired_name2id
¶ |
dict[str, int]
|
dict |
required |
update_image_filenames
¶ |
bool
|
bool If True, updates coco image file_names with absolute file paths. |
False
|
Source code in sahi/utils/coco.py
merge
¶merge(
coco: Coco,
desired_name2id: dict | None = None,
verbose: int = 1,
) -> None
Combine the images/annotations/categories of given coco object with current one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coco
¶ |
Coco
|
sahi.utils.coco.Coco instance A COCO dataset object |
required |
desired_name2id
¶ |
dict | None
|
dict |
None
|
verbose
¶ |
int
|
bool If True, merging info is printed |
1
|
Source code in sahi/utils/coco.py
from_coco_dict_or_path
classmethod
¶from_coco_dict_or_path(
coco_dict_or_path: dict | str,
image_dir: str | None = None,
remapping_dict: dict | None = None,
ignore_negative_samples: bool = False,
clip_bboxes_to_img_dims: bool = False,
use_threads: bool = False,
num_threads: int = 10,
) -> _TCoco
Create coco object from COCO formatted dict or COCO dataset file path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coco_dict_or_path
¶ |
dict | str
|
dict/str or List[dict/str] COCO formatted dict or COCO dataset file path List of COCO formatted dict or COCO dataset file path |
required |
image_dir
¶ |
str | None
|
str Base file directory that contains dataset images. Required for merging and yolov5 conversion. |
None
|
remapping_dict
¶ |
dict | None
|
dict {1:0, 2:1} maps category id 1 to 0 and category id 2 to 1 |
None
|
ignore_negative_samples
¶ |
bool
|
bool If True ignores images without annotations in all operations. |
False
|
clip_bboxes_to_img_dims
¶ |
bool
|
bool = False Limits bounding boxes to image dimensions. |
False
|
use_threads
¶ |
bool
|
bool = False Use threads when processing the json image list, defaults to False |
False
|
num_threads
¶ |
int
|
int = 10 Slice the image list to given number of chunks, defaults to 10 |
10
|
Properties
images: list of CocoImage category_mapping: dict
Source code in sahi/utils/coco.py
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 | |
calculate_stats
¶Iterate over all annotations and calculate total number of.
Source code in sahi/utils/coco.py
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 | |
split_coco_as_train_val
¶split_coco_as_train_val(
train_split_rate: float = 0.9, numpy_seed: int = 0
) -> dict
Split images into train-val and return as Coco objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
train_split_rate
¶ |
float
|
float |
0.9
|
numpy_seed
¶ |
int
|
int random seed. Actually, this doesn't use numpy, but the random package from the standard library, but it is called numpy for compatibility. |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
result |
dict
|
Dict with keys "train_coco" and "val_coco". |
Source code in sahi/utils/coco.py
export_as_yolo
¶export_as_yolo(
output_dir: str | Path,
train_split_rate: float = 1.0,
numpy_seed: int = 0,
mp: bool = False,
disable_symlink: bool = False,
) -> None
Export current COCO dataset in YOLO format.
Creates train/val folders with image symlinks and txt files and a data yaml file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_dir
¶ |
str | Path
|
str Export directory. |
required |
train_split_rate
¶ |
float
|
If given 1, exports as train split. If 0, as val split. If between 0-1, exports both. |
1.0
|
numpy_seed
¶ |
int
|
Random seed for splitting. |
0
|
mp
¶ |
bool
|
If True, multiprocess mode is on (should be in 'if name == "main":' block). |
False
|
disable_symlink
¶ |
bool
|
If True, images will be copied instead of creating symlinks. |
False
|
Source code in sahi/utils/coco.py
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 | |
get_subsampled_coco
¶get_subsampled_coco(
subsample_ratio: int = 2, category_id: int | None = None
) -> Coco
Subsample images and return as Coco object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
subsample_ratio
¶ |
int
|
int 10 means take every 10th image with its annotations |
2
|
category_id
¶ |
int | None
|
int subsample only images containing given category_id, if -1 then subsamples negative samples |
None
|
Returns: subsampled_coco: sahi.utils.coco.Coco
Source code in sahi/utils/coco.py
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 | |
get_upsampled_coco
¶get_upsampled_coco(
upsample_ratio: int = 2, category_id: int | None = None
) -> Coco
Upsample images and return as Coco object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
upsample_ratio
¶ |
int
|
int 10 means copy each sample 10 times |
2
|
category_id
¶ |
int | None
|
int upsample only images containing given category_id, if -1 then upsamples negative samples |
None
|
Returns: upsampled_coco: sahi.utils.coco.Coco
Source code in sahi/utils/coco.py
get_area_filtered_coco
¶get_area_filtered_coco(
min: int = 0,
max_val: float = float("inf"),
intervals_per_category: dict | None = None,
) -> Coco
Filter annotations by area and return remaining images as Coco object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min
¶ |
int
|
int minimum allowed area |
0
|
max_val
¶ |
float
|
int maximum allowed area |
float('inf')
|
intervals_per_category
¶ |
dict | None
|
dict of dicts { "human": {"min": 20, "max": 10000}, "vehicle": {"min": 50, "max": 15000}, } |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
area_filtered_coco |
Coco
|
sahi.utils.coco.Coco |
Source code in sahi/utils/coco.py
get_coco_with_clipped_bboxes
¶get_coco_with_clipped_bboxes() -> Coco
Limits overflowing bounding boxes to image dimensions.
Source code in sahi/utils/coco.py
DatasetClassCounts
dataclass
¶ CocoVid
¶CocoVid(
name: str | None = None,
remapping_dict: dict | None = None,
)
COCOVid dataset object for managing videos, images, and annotations.
Initialize a COCOVid dataset object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
¶ |
str | None
|
Name of the CocoVid dataset, determines exported json name. |
None
|
remapping_dict
¶ |
dict | None
|
Category id mapping, e.g., {1:0, 2:1} maps id 1 to 0. |
None
|
Source code in sahi/utils/coco.py
category_mapping
property
¶Get mapping of category IDs to names.
add_categories_from_coco_category_list
¶add_categories_from_coco_category_list(
coco_category_list: list[dict],
) -> None
Create CocoCategory object using coco category list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coco_category_list
¶ |
list[dict]
|
List[Dict] [ {"supercategory": "person", "id": 1, "name": "person"}, {"supercategory": "vehicle", "id": 2, "name": "bicycle"} ] |
required |
Source code in sahi/utils/coco.py
add_category
¶add_category(category: CocoCategory) -> None
Add category to this CocoVid instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category
¶ |
CocoCategory
|
CocoCategory |
required |
Source code in sahi/utils/coco.py
export_yolo_images_and_txts_from_coco_object
¶export_yolo_images_and_txts_from_coco_object(
output_dir: str,
coco: Coco,
ignore_negative_samples: bool = False,
mp: bool = False,
disable_symlink: bool = False,
) -> None
Create image symlinks and annotation txts in yolo format from coco dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_dir
¶ |
str
|
str Export directory. |
required |
coco
¶ |
Coco
|
sahi.utils.coco.Coco Initialized Coco object that contains images and categories. |
required |
ignore_negative_samples
¶ |
bool
|
bool If True ignores images without annotations in all operations. |
False
|
mp
¶ |
bool
|
bool If True, multiprocess mode is on. Should be called in 'if name == main:' block. |
False
|
disable_symlink
¶ |
bool
|
bool If True, symlinks are not created. Instead images are copied. |
False
|
Source code in sahi/utils/coco.py
export_single_yolo_image_and_corresponding_txt
¶export_single_yolo_image_and_corresponding_txt(
coco_image: CocoImage,
coco_image_dir: str,
output_dir: str,
ignore_negative_samples: bool = False,
disable_symlink: bool = False,
) -> None
Generate YOLO formatted image symlink and annotation txt file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coco_image
¶ |
CocoImage
|
CocoImage object. |
required |
coco_image_dir
¶ |
str
|
Image directory path. |
required |
output_dir
¶ |
str
|
Export directory. |
required |
ignore_negative_samples
¶ |
bool
|
If True, ignores images without annotations. |
False
|
disable_symlink
¶ |
bool
|
If True, copies images instead of creating symlinks. |
False
|
Source code in sahi/utils/coco.py
1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 | |
update_categories
¶update_categories(
desired_name2id: dict, coco_dict: dict
) -> dict
Rearrange category mapping of COCO dictionary.
Can also be used to filter some of the categories.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desired_name2id
¶ |
dict
|
Desired category name to id mapping, e.g. {"big_vehicle": 1, "car": 2, "human": 3}. |
required |
coco_dict
¶ |
dict
|
COCO formatted dictionary. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
coco_target |
dict
|
COCO dict with updated/filtered categories. |
Source code in sahi/utils/coco.py
update_categories_from_file
¶update_categories_from_file(
desired_name2id: dict, coco_path: str, save_path: str
) -> None
Rearrange category mapping from COCO file.
Can also be used to filter some of the categories.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desired_name2id
¶ |
dict
|
Category name to id mapping, e.g., {"human": 1, "car": 2}. |
required |
coco_path
¶ |
str
|
Path to COCO JSON file. |
required |
save_path
¶ |
str
|
Path where the updated COCO JSON will be saved. |
required |
Source code in sahi/utils/coco.py
merge
¶merge(
coco_dict1: dict,
coco_dict2: dict,
desired_name2id: dict | None = None,
) -> dict
Combine 2 coco formatted annotations dicts, and returns the combined coco dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coco_dict1
¶ |
dict
|
dict First coco dictionary. |
required |
coco_dict2
¶ |
dict
|
dict Second coco dictionary. |
required |
desired_name2id
¶ |
dict | None
|
dict |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
merged_coco_dict |
dict
|
Merged COCO dict. |
Source code in sahi/utils/coco.py
merge_from_list
¶merge_from_list(
coco_dict_list: list[dict],
desired_name2id: dict | None = None,
verbose: int = 1,
) -> dict
Combine a list of coco formatted annotations dicts, and returns the combined coco dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coco_dict_list
¶ |
list[dict]
|
list of dict A list of coco dicts |
required |
desired_name2id
¶ |
dict | None
|
dict |
None
|
verbose
¶ |
int
|
bool If True, merging info is printed |
1
|
Returns:
merged_coco_dict: dict
Merged COCO dict.
Source code in sahi/utils/coco.py
merge_from_file
¶merge_from_file(
coco_path1: str, coco_path2: str, save_path: str
) -> None
Combine 2 coco formatted annotations files given their paths, and saves the combined file to save_path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coco_path1
¶ |
str
|
Path for the first coco file. |
required |
coco_path2
¶ |
str
|
Path for the second coco file. |
required |
save_path
¶ |
str
|
Path to save the merged file, e.g. "dirname/coco.json". |
required |
Source code in sahi/utils/coco.py
get_imageid2annotationlist_mapping
¶get_imageid2annotationlist_mapping(
coco_dict: dict,
) -> dict[int, list[dict]]
Get image_id to annotationlist mapping for faster indexing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coco_dict
¶ |
dict
|
COCO dict with fields "images", "annotations", "categories". |
required |
Returns:
| Name | Type | Description |
|---|---|---|
image_id_to_annotation_list |
dict[int, list[dict]]
|
Mapping from image id to list of annotation dicts. |
Source code in sahi/utils/coco.py
create_coco_dict
¶create_coco_dict(
images: list[CocoImage],
categories: list[dict],
ignore_negative_samples: bool = False,
image_id_setting: str = "auto",
) -> dict
Create COCO dict with fields "images", "annotations", "categories".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
¶ |
list[CocoImage]
|
List of CocoImage containing a list of CocoAnnotation. |
required |
categories
¶ |
list[dict]
|
List of Dict COCO categories. |
required |
ignore_negative_samples
¶ |
bool
|
If True, images without annotations are ignored. |
False
|
image_id_setting
¶ |
str
|
How to assign image ids while exporting can be
auto --> will assign id from scratch ( |
'auto'
|
Returns:
| Name | Type | Description |
|---|---|---|
coco_dict |
dict
|
COCO dict with fields "images", "annotations", "categories". |
Source code in sahi/utils/coco.py
1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 | |
create_coco_prediction_array
¶create_coco_prediction_array(
images: list[CocoImage],
ignore_negative_samples: bool = False,
image_id_setting: str = "auto",
) -> list[dict]
Create COCO prediction array which is list of predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
¶ |
list[CocoImage]
|
List of CocoImage containing a list of CocoAnnotation. |
required |
ignore_negative_samples
¶ |
bool
|
If True, images without predictions are ignored. |
False
|
image_id_setting
¶ |
str
|
How to assign image ids while exporting can be
auto --> will assign id from scratch ( |
'auto'
|
Returns:
| Name | Type | Description |
|---|---|---|
coco_prediction_array |
list[dict]
|
COCO predictions array. |
Source code in sahi/utils/coco.py
add_bbox_and_area_to_coco
¶add_bbox_and_area_to_coco(
source_coco_path: str = "",
target_coco_path: str = "",
add_bbox: bool = True,
add_area: bool = True,
) -> dict
Calculate and fill bbox and area fields in COCO annotations.
Takes a COCO dataset file, calculates bbox and area fields, and exports updated dict.
Returns:
| Name | Type | Description |
|---|---|---|
coco_dict |
dict
|
Updated COCO dict. |
Source code in sahi/utils/coco.py
count_images_with_category
¶count_images_with_category(
coco_file_path: str,
) -> DatasetClassCounts
Count images with each category in COCO dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coco_file_path
¶ |
str
|
Path to COCO dataset file. |
required |
Returns:
| Type | Description |
|---|---|
DatasetClassCounts
|
DatasetClassCounts object storing counts. |
Source code in sahi/utils/coco.py
remove_invalid_coco_results
¶remove_invalid_coco_results(
result_list_or_path: list | str,
dataset_dict_or_path: dict | str | None = None,
) -> list[dict]
Remove invalid predictions from coco result.
Removes predictions with negative bbox values or extreme bbox values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result_list_or_path
¶ |
list | str
|
path or list for coco result json |
required |
dataset_dict_or_path
¶ |
optional
|
path or dict for coco dataset json |
None
|
Source code in sahi/utils/coco.py
export_coco_as_yolo
¶export_coco_as_yolo(
output_dir: str,
train_coco: Coco | None = None,
val_coco: Coco | None = None,
train_split_rate: float = 0.9,
numpy_seed: int = 0,
disable_symlink: bool = False,
) -> str
Export current COCO dataset in ultralytics/YOLO format.
Creates train val folders with image symlinks and txt files and a data yaml file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_dir
¶ |
str
|
str Export directory. |
required |
train_coco
¶ |
Coco | None
|
Coco coco object for training |
None
|
val_coco
¶ |
Coco | None
|
Coco coco object for val |
None
|
train_split_rate
¶ |
float
|
float train split rate between 0 and 1. will be used when val_coco is None. |
0.9
|
numpy_seed
¶ |
int
|
int To fix the numpy seed. |
0
|
disable_symlink
¶ |
bool
|
bool If True, copy images instead of creating symlinks. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
yaml_path |
str
|
str Path for the exported YOLO data.yml |
Source code in sahi/utils/coco.py
2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 | |
export_coco_as_yolo_via_yml
¶export_coco_as_yolo_via_yml(
yml_path: str,
output_dir: str,
train_split_rate: float = 0.9,
numpy_seed: int = 0,
disable_symlink: bool = False,
) -> str
Export current COCO dataset in ultralytics/YOLO format using a YML file.
Creates train val folders with image symlinks and txt files and a data yaml file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
yml_path
¶ |
str
|
str file should contain these fields: train_json_path: str train_image_dir: str val_json_path: str val_image_dir: str |
required |
output_dir
¶ |
str
|
str Export directory. |
required |
train_split_rate
¶ |
float
|
float train split rate between 0 and 1. will be used when val_json_path is None. |
0.9
|
numpy_seed
¶ |
int
|
int To fix the numpy seed. |
0
|
disable_symlink
¶ |
bool
|
bool If True, copy images instead of creating symlinks. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
yaml_path |
str
|
str Path for the exported YOLO data.yml |
Source code in sahi/utils/coco.py
2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 | |
compatibility
¶
Compatibility utilities for version handling.
fix_shift_amount_list
¶Ensure shift_amount_list is in the expected nested list format.
Compatibility for sahi v0.8.15 and earlier versions.
Source code in sahi/utils/compatibility.py
fix_full_shape_list
¶Ensure full_shape_list is in the expected nested list format.
Compatibility for sahi v0.8.15 and earlier versions.
Source code in sahi/utils/compatibility.py
cv
¶
Computer vision utilities for image processing and visualization.
Colors
¶Color palette for visualization.
Initialize the color palette from hex color codes.
Source code in sahi/utils/cv.py
hex_to_rgb
staticmethod
¶hex_to_rgb(hex_code: str) -> tuple[int, int, int]
Converts a hexadecimal color code to RGB format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hex_code
¶ |
str
|
The hexadecimal color code to convert. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple[int, int, int]
|
A tuple representing the RGB values in the order (R, G, B). |
Source code in sahi/utils/cv.py
crop_object_predictions
¶crop_object_predictions(
image: ndarray,
object_prediction_list: list,
output_dir: str = "",
file_name: str = "prediction_visual",
export_format: str = "png",
) -> None
Crops bounding boxes over the source image and exports it to the output folder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
¶ |
ndarray
|
The source image to crop bounding boxes from. |
required |
object_prediction_list
¶ |
list
|
A list of object predictions. |
required |
output_dir
¶ |
str
|
The directory where the resulting visualizations will be exported. Defaults to an empty string. |
''
|
file_name
¶ |
str
|
The name of the exported file. The exported file will be saved as |
'prediction_visual'
|
export_format
¶ |
str
|
The format of the exported file. Can be specified as 'jpg' or 'png'. Defaults to "png". |
'png'
|
Source code in sahi/utils/cv.py
convert_image_to
¶Reads an image from the given path and saves it with the specified extension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
read_path
¶ |
str
|
The path to the image file. |
required |
extension
¶ |
str
|
The desired file extension for the saved image. Defaults to "jpg". |
'jpg'
|
grayscale
¶ |
bool
|
Whether to convert the image to grayscale. Defaults to False. |
False
|
Source code in sahi/utils/cv.py
read_large_image
¶read_large_image(image_path: str) -> tuple[ndarray, bool]
Reads a large image from the specified image path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_path
¶ |
str
|
The path to the image file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple[ndarray, bool]
|
A tuple containing the image data and a flag indicating whether cv2 was used to read the image. The image data is a numpy array representing the image in RGB format. The flag is True if cv2 was used, False otherwise. |
Source code in sahi/utils/cv.py
read_image
¶read_image(image_path: str) -> ndarray
Loads image as a numpy array from the given path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_path
¶ |
str
|
The path to the image file. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
numpy.ndarray: The loaded image as a numpy array. |
Source code in sahi/utils/cv.py
read_image_size
¶read_image_size(
image: Image | str | PathLike | ndarray,
exif_fix: bool = True,
) -> tuple[int, int]
Return the (width, height) read_image_as_pil would produce, without decoding it.
For a local path only the header is read. Decoding a gigapixel scan just to ask for its dimensions costs gigabytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
¶ |
Image | str | PathLike | ndarray
|
The image to size. An image path or URL (str), a numpy image (np.ndarray), or a PIL.Image object. |
required |
exif_fix
¶ |
bool
|
Whether the caller will apply the EXIF orientation, as |
True
|
Returns:
| Type | Description |
|---|---|
tuple[int, int]
|
The image size as (width, height). |
Example
from sahi.utils.cv import read_image_size read_image_size("tests/data/small-vehicles1.jpeg") (1068, 580)
Source code in sahi/utils/cv.py
read_image_as_pil
¶read_image_as_pil(
image: Image | str | PathLike | ndarray,
exif_fix: bool = True,
return_arr: bool = False,
) -> Image | ndarray
Loads an image as PIL.Image.Image (or np.ndarray when return_arr=True).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
¶ |
Union[Image, str, PathLike, ndarray]
|
The image to be loaded. It can be an image path (str or path-like) or URL (str), a numpy image (np.ndarray), or a PIL.Image object. |
required |
exif_fix
¶ |
bool
|
Whether to apply the EXIF orientation to the image. Defaults to True. |
True
|
return_arr
¶ |
bool
|
When True, return an HWC RGB ndarray. Local paths decode straight to one; other inputs convert from PIL before returning. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
Image | ndarray
|
PIL.Image.Image | np.ndarray: The loaded image. |
Source code in sahi/utils/cv.py
select_random_color
¶Selects a random color from a predefined list of colors.
Returns:
| Name | Type | Description |
|---|---|---|
list |
list[int]
|
A list representing the RGB values of the selected color. |
Source code in sahi/utils/cv.py
apply_color_mask
¶Applies color mask to given input image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
¶ |
ndarray
|
The input image to apply the color mask to. |
required |
color
¶ |
tuple
|
The RGB color tuple to use for the mask. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: The resulting image with the applied color mask. |
Source code in sahi/utils/cv.py
get_video_reader
¶get_video_reader(
source: str,
save_dir: str,
frame_skip_interval: int,
export_visual: bool = False,
view_visual: bool = False,
) -> tuple[Generator[Image], VideoWriter | None, str, int]
Creates OpenCV video capture object from given video file path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
¶ |
str
|
Video file path |
required |
save_dir
¶ |
str
|
Video export directory |
required |
frame_skip_interval
¶ |
int
|
Frame skip interval |
required |
export_visual
¶ |
bool
|
Set True if you want to export visuals |
False
|
view_visual
¶ |
bool
|
Set True if you want to render visual |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
iterator |
Generator[Image]
|
Pillow Image |
video_writer |
VideoWriter | None
|
cv2.VideoWriter |
video_file_name |
str
|
video name with extension |
Source code in sahi/utils/cv.py
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 | |
visualize_prediction
¶visualize_prediction(
image: ndarray,
boxes: list[list],
classes: list[str],
masks: list[ndarray] | None = None,
rect_th: int | None = None,
text_size: float | None = None,
text_th: int | None = None,
color: tuple | None = None,
hide_labels: bool = False,
output_dir: str | None = None,
file_name: str | None = "prediction_visual",
) -> dict
Visualizes prediction classes, bounding boxes over the source image and exports it to output folder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
¶ |
ndarray
|
The source image. |
required |
boxes
¶ |
List[List]
|
List of bounding boxes coordinates. |
required |
classes
¶ |
List[str]
|
List of class labels corresponding to each bounding box. |
required |
masks
¶ |
Optional[List[ndarray]]
|
List of masks corresponding to each bounding box. Defaults to None. |
None
|
rect_th
¶ |
int
|
Thickness of the bounding box rectangle. Defaults to None. |
None
|
text_size
¶ |
float
|
Size of the text for class labels. Defaults to None. |
None
|
text_th
¶ |
int
|
Thickness of the text for class labels. Defaults to None. |
None
|
color
¶ |
tuple
|
Color of the bounding box and text. Defaults to None. |
None
|
hide_labels
¶ |
bool
|
Whether to hide the class labels. Defaults to False. |
False
|
output_dir
¶ |
Optional[str]
|
Output directory to save the visualization. Defaults to None. |
None
|
file_name
¶ |
Optional[str]
|
File name for the saved visualization. Defaults to "prediction_visual". |
'prediction_visual'
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
A dictionary containing the visualized image and the elapsed time for the visualization process. |
Source code in sahi/utils/cv.py
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 | |
visualize_object_predictions
¶visualize_object_predictions(
image: ndarray,
object_prediction_list: list,
rect_th: int | None = None,
text_size: float | None = None,
text_th: int | None = None,
color: tuple | None = None,
hide_labels: bool = False,
hide_conf: bool = False,
output_dir: str | None = None,
file_name: str | None = "prediction_visual",
export_format: str | None = "png",
) -> dict
Visualize object predictions with bounding boxes and category names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
¶ |
ndarray
|
Input image as numpy array. |
required |
object_prediction_list
¶ |
list
|
List of prediction.ObjectPrediction instances. |
required |
rect_th
¶ |
int | None
|
rectangle thickness |
None
|
text_size
¶ |
float | None
|
size of the category name over box |
None
|
text_th
¶ |
int | None
|
text thickness |
None
|
color
¶ |
tuple | None
|
annotation color in the form: (0, 255, 0) |
None
|
hide_labels
¶ |
bool
|
hide labels |
False
|
hide_conf
¶ |
bool
|
hide confidence |
False
|
output_dir
¶ |
str | None
|
directory for resulting visualization to be exported |
None
|
file_name
¶ |
str | None
|
exported file will be saved as: output_dir+file_name+".png" |
'prediction_visual'
|
export_format
¶ |
str | None
|
can be specified as 'jpg' or 'png' |
'png'
|
Source code in sahi/utils/cv.py
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 | |
get_coco_segmentation_from_bool_mask
¶Convert boolean mask to COCO segmentation format.
Converts a 2D boolean mask to COCO polygon format: [ [x1, y1, x2, y2, x3, y3, ...], [x1, y1, x2, y2, x3, y3, ...], ... ].
Source code in sahi/utils/cv.py
get_bool_mask_from_coco_segmentation
¶get_bool_mask_from_coco_segmentation(
coco_segmentation: list[list[float]],
width: int,
height: int,
) -> ndarray
Convert COCO segmentation to a 2D boolean mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coco_segmentation
¶ |
list[list[float]]
|
List of polygons representing the COCO segmentation. |
required |
width
¶ |
int
|
Width of the boolean mask. |
required |
height
¶ |
int
|
Height of the boolean mask. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
2D boolean mask of size (height, width). |
Source code in sahi/utils/cv.py
get_bbox_from_bool_mask
¶get_bbox_from_bool_mask(
bool_mask: ndarray,
) -> list[int] | None
Generate VOC bounding box [xmin, ymin, xmax, ymax] from given boolean mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bool_mask
¶ |
ndarray
|
2D boolean mask. |
required |
Returns:
| Type | Description |
|---|---|
list[int] | None
|
Optional[List[int]]: VOC bounding box [xmin, ymin, xmax, ymax] or None if no bounding box is found. |
Source code in sahi/utils/cv.py
get_bbox_from_coco_segmentation
¶Generate voc box ([xmin, ymin, xmax, ymax]) from given coco segmentation.
Source code in sahi/utils/cv.py
yolo_bbox_to_voc_bbox
¶yolo_bbox_to_voc_bbox(
yolo_bbox: list[float],
image_width: int,
image_height: int,
) -> list[float]
Convert YOLO format bounding box to VOC format.
Converts normalized YOLO format [x_center, y_center, width, height] to absolute VOC format [xmin, ymin, xmax, ymax] pixel coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
yolo_bbox
¶ |
list[float]
|
list of [x_center, y_center, width, height] |
required |
image_width
¶ |
int
|
width of the image |
required |
image_height
¶ |
int
|
height of the image |
required |
Returns:
| Type | Description |
|---|---|
list[float]
|
list of [xmin, ymin, xmax, ymax] |
Source code in sahi/utils/cv.py
get_coco_segmentation_from_obb_points
¶get_coco_segmentation_from_obb_points(
obb_points: ndarray,
) -> list[list[float]]
Convert OBB (Oriented Bounding Box) points to COCO polygon format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obb_points
¶ |
ndarray
|
np.ndarray OBB points tensor from ultralytics.engine.results.OBB Shape: (4, 2) containing 4 points with (x,y) coordinates each |
required |
Returns:
| Type | Description |
|---|---|
list[list[float]]
|
List[List[float]]: Polygon points in COCO format [[x1, y1, x2, y2, x3, y3, x4, y4], [...], ...] |
Source code in sahi/utils/cv.py
normalize_numpy_image
¶ ipython_display
¶Displays numpy image in notebook.
If input image is in range 0..1, please first multiply img by 255 Assumes image is ndarray of shape [height, width, channels] where channels can be 1, 3 or 4
Source code in sahi/utils/cv.py
detectron2
¶
Detectron2 model utilities and constants.
Detectron2TestConstants
¶Detectron2 test model configurations.
export_cfg_as_yaml
¶export_cfg_as_yaml(
cfg: object, export_path: str = "config.yaml"
) -> None
Export Detectron2 config object to YAML format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg
¶ |
CfgNode
|
Detectron2 config object. |
required |
export_path
¶ |
str
|
Path to export the Detectron2 config. |
'config.yaml'
|
Related Detectron2 doc: https://detectron2.readthedocs.io/en/stable/modules/config.html#detectron2.config.CfgNode.dump
Source code in sahi/utils/detectron2.py
fiftyone
¶
FiftyOne dataset and visualization utilities.
COCODetectionDatasetImporter
¶
Bases: COCODetectionDatasetImporter
Custom COCO detection dataset importer for FiftyOne.
setup
¶Set up the importer with COCO dataset information.
Source code in sahi/utils/fiftyone.py
create_fiftyone_dataset_from_coco_file
¶Create a FiftyOne dataset from COCO format files.
Source code in sahi/utils/fiftyone.py
launch_fiftyone_app
¶Launch FiftyOne app with COCO dataset.
Source code in sahi/utils/fiftyone.py
file
¶
File I/O utilities for SAHI.
NumpyEncoder
¶ unzip
¶Unzips compressed .zip file.
Example inputs
file_path: 'data/01_alb_id.zip' dest_dir: 'data/'
save_json
¶Saves json formatted data (given as "data") as save_path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
¶ |
object
|
dict Data to be saved as json |
required |
save_path
¶ |
str | Path
|
str "dirname/coco.json" |
required |
indent
¶ |
int | None
|
int or None Indentation level for pretty-printing the JSON data. If None, the most compact representation will be used. If an integer is provided, it specifies the number of spaces to use for indentation. Example: indent=4 will format the JSON data with an indentation of 4 spaces per level. |
None
|
Example inputs
data: {"image_id": 5} save_path: "dirname/coco.json" indent: Train json files with indent=None, val json files with indent=4
Source code in sahi/utils/file.py
load_json
¶Load JSON formatted data from file.
Encoding type can be specified with 'encoding' argument.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
load_path
¶ |
str
|
str "dirname/coco.json" |
required |
encoding
¶ |
str
|
str Encoding type, default is 'utf-8' |
'utf-8'
|
Example inputs
load_path: "dirname/coco.json"
Source code in sahi/utils/file.py
list_files
¶Walk given directory and return a list of file path with desired extension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory
¶ |
str
|
str "data/coco/" |
required |
contains
¶ |
list
|
list A list of strings to check if the target file contains them, example: ["coco.png", ".jpg", "jpeg"] |
['.json']
|
verbose
¶ |
int
|
int 0: no print 1: print number of files |
1
|
Returns:
| Name | Type | Description |
|---|---|---|
filepath_list |
list[str]
|
List of file paths. |
Source code in sahi/utils/file.py
list_files_recursively
¶list_files_recursively(
directory: str,
contains: list[str] = [".json"],
verbose: bool = True,
) -> tuple[list[str], list[str]]
Walk given directory recursively and return a list of file path with desired extension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory
¶ |
str
|
Directory path to walk, e.g. "data/coco/". |
required |
contains
¶ |
list[str]
|
A list of strings to check if the target file contains them, example: ["coco.png", ".jpg", "jpeg"]. |
['.json']
|
verbose
¶ |
bool
|
If true, prints some results. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
relative_filepath_list |
list[str]
|
List of file paths relative to given directory. |
abs_filepath_list |
list[str]
|
List of absolute file paths. |
Source code in sahi/utils/file.py
get_base_filename
¶Takes a file path, returns (base_filename_with_extension, base_filename_without_extension).
Source code in sahi/utils/file.py
get_file_extension
¶get_file_extension(path: str) -> str
Get the file extension from a given file path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
¶ |
str
|
The file path. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The file extension. |
Source code in sahi/utils/file.py
load_pickle
¶load_pickle(load_path: str | Path) -> object
Loads pickle formatted data (given as "data") from load_path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
load_path
¶ |
str | Path
|
str "dirname/coco.pickle" |
required |
Example inputs
load_path: "dirname/coco.pickle"
Source code in sahi/utils/file.py
save_pickle
¶Saves pickle formatted data (given as "data") as save_path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
¶ |
object
|
dict Data to be saved as pickle |
required |
save_path
¶ |
str | Path
|
str "dirname/coco.pickle" |
required |
Example inputs
data: {"image_id": 5} save_path: "dirname/coco.pickle"
Source code in sahi/utils/file.py
import_model_class
¶import_model_class(
model_type: str, class_name: str
) -> type
Import a predefined detection model class by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_type
¶ |
str
|
Framework type ("yolov5", "detectron2", "mmdet", etc). |
required |
class_name
¶ |
str
|
Name of the detection model class (e.g., "MmdetDetectionModel"). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
class_ |
type
|
class with given path |
Source code in sahi/utils/file.py
increment_path
¶Increment path, i.e. runs/exp --> runs/exp{sep}0, runs/exp{sep}1 etc.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
¶ |
str | Path
|
str The base path to increment. |
required |
exist_ok
¶ |
bool
|
bool If True, return the path as is if it already exists. If False, increment the path. |
True
|
sep
¶ |
str
|
str The separator to use between the base path and the increment number. |
''
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The incremented path. |
Example
increment_path("runs/exp", sep="") 'runs/exp_0' increment_path("runs/exp_0", sep="") 'runs/exp_1'
Source code in sahi/utils/file.py
download_from_url
¶Downloads a file from the given URL and saves it to the specified path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
from_url
¶ |
str
|
The URL of the file to download. |
required |
to_path
¶ |
str
|
The path where the downloaded file should be saved. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in sahi/utils/file.py
is_colab
¶Check if the current environment is a Google Colab instance.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the environment is a Google Colab instance, False otherwise. |
import_utils
¶
Import utilities for checking package availability.
get_package_info
¶get_package_info(
package_name: str, verbose: bool = True
) -> tuple[bool, str]
Check whether a package is installed and retrieve its version.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
package_name
¶ |
str
|
The name of the package to look up. |
required |
verbose
¶ |
bool
|
If True, log the package version when available. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
is_available |
bool
|
Whether the package is installed. |
version_string |
str
|
Version string, or "N/A" if not installed. |
Source code in sahi/utils/import_utils.py
print_environment_info
¶Log version info for all commonly used SAHI dependency packages.
Source code in sahi/utils/import_utils.py
get_opencv_distribution_versions
¶Collect the installed versions of every OpenCV distribution.
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Mapping of distribution name to version, for those that are installed. |
Source code in sahi/utils/import_utils.py
get_opencv_conflict_message
¶Describe an OpenCV installation that mixes distribution versions.
All OpenCV distributions install into the same cv2 directory, so
installing more than one of them at different versions leaves a mixture of
Python and native files behind, and import cv2 fails with a confusing
error such as partially initialized module 'cv2' has no attribute
'gapi_wip_gst_GStreamerPipeline'.
Returns:
| Type | Description |
|---|---|
str | None
|
A message explaining how to fix the installation, or None when the installed OpenCV distributions agree on a single version. |
Source code in sahi/utils/import_utils.py
is_available
¶is_available(module_name: str) -> bool
Check whether a Python module is importable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module_name
¶ |
str
|
Dotted module name (e.g. "torch", "torchvision"). |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the module can be found by the import system. |
Source code in sahi/utils/import_utils.py
check_requirements
¶check_requirements(package_names: Iterable[str]) -> None
Verify that all required packages are importable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
package_names
¶ |
Iterable[str]
|
Iterable of package names to check. |
required |
Raises:
| Type | Description |
|---|---|
ImportError
|
If any of the listed packages cannot be found. |
Source code in sahi/utils/import_utils.py
check_package_minimum_version
¶check_package_minimum_version(
package_name: str,
minimum_version: str,
verbose: bool = False,
) -> bool
Check whether an installed package meets a minimum version requirement.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
package_name
¶ |
str
|
The name of the package to check. |
required |
minimum_version
¶ |
str
|
The minimum acceptable version string (e.g. "1.0.0"). |
required |
verbose
¶ |
bool
|
If True, log the detected package version. |
False
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the package is missing (assumed compatible), its version is unknown, or its version meets the minimum. False if the installed version is below the minimum. |
Source code in sahi/utils/import_utils.py
ensure_package_minimum_version
¶ensure_package_minimum_version(
package_name: str,
minimum_version: str,
verbose: bool = False,
) -> None
Ensure a package meets a minimum version, raising on failure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
package_name
¶ |
str
|
The name of the package to check. |
required |
minimum_version
¶ |
str
|
The minimum acceptable version string (e.g. "1.0.0"). |
required |
verbose
¶ |
bool
|
If True, log the detected package version. |
False
|
Raises:
| Type | Description |
|---|---|
ImportError
|
If the installed version is below minimum_version. |
Source code in sahi/utils/import_utils.py
mmdet
¶
MMDetection model utilities and helpers.
MmdetTestConstants
¶MMDetection test model configurations.
mmdet_version_as_integer
¶ download_mmdet_cascade_mask_rcnn_model
¶Download the Cascade Mask R-CNN model for testing.
Source code in sahi/utils/mmdet.py
download_mmdet_retinanet_model
¶Download the RetinaNet model for testing.
Source code in sahi/utils/mmdet.py
download_mmdet_yolox_tiny_model
¶Download the YOLOX-Tiny model for testing.
Source code in sahi/utils/mmdet.py
download_mmdet_config
¶download_mmdet_config(
model_name: str = "cascade_rcnn",
config_file_name: str = "cascade_mask_rcnn_r50_fpn_1x_coco.py",
verbose: bool = True,
) -> str
Merges config files starting from given main config file name. Saves as single file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
¶ |
str
|
mmdet model name. check https://github.com/open-mmlab/mmdetection/tree/master/configs. |
'cascade_rcnn'
|
config_file_name
¶ |
str
|
mdmet config file name. |
'cascade_mask_rcnn_r50_fpn_1x_coco.py'
|
verbose
¶ |
bool
|
if True, print save path. |
True
|
Returns:
| Type | Description |
|---|---|
str
|
(str) abs path of the downloaded config file. |
Source code in sahi/utils/mmdet.py
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | |
rtdetr
¶
RT-DETR model utilities and constants.
RTDETRTestConstants
¶RT-DETR test model configurations.
download_rtdetrl_model
¶Download the RT-DETR-L model for testing.
Source code in sahi/utils/rtdetr.py
download_rtdetrx_model
¶Download the RT-DETR-X model for testing.
Source code in sahi/utils/rtdetr.py
shapely
¶
Shapely-based geometry utilities for polygon and segmentation handling.
ShapelyAnnotation
¶ShapelyAnnotation(
multipolygon: MultiPolygon,
slice_bbox: list[float] | None = None,
)
Creates ShapelyAnnotation (as shapely MultiPolygon).
Can convert this instance annotation to various formats.
Initialize ShapelyAnnotation with a multipolygon.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
multipolygon
¶ |
MultiPolygon
|
A Shapely MultiPolygon object. |
required |
slice_bbox
¶ |
list[float] | None
|
Optional slice bounding box for coordinate adjustment. |
None
|
Source code in sahi/utils/shapely.py
multipolygon
property
writable
¶Get the underlying Shapely MultiPolygon object.
from_coco_segmentation
classmethod
¶from_coco_segmentation(
segmentation: list[list[float]] | list[list[int]],
slice_bbox: list[float] | None = None,
) -> ShapelyAnnotation
Init ShapelyAnnotation from coco segmentation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
segmentation
¶ |
list[list[float]] | list[list[int]]
|
COCO segmentation format,
e.g. |
required |
slice_bbox
¶ |
list[float] | None
|
Bounding box as [xmin, ymin, width, height]. Should have the same format as the output of the get_bbox_from_shapely function. Is used to calculate sliced coco coordinates. |
None
|
Source code in sahi/utils/shapely.py
from_coco_bbox
classmethod
¶from_coco_bbox(
bbox: list[int] | list[float],
slice_bbox: list[float] | None = None,
) -> ShapelyAnnotation
Init ShapelyAnnotation from coco bbox.
bbox (List[int]): [xmin, ymin, width, height] slice_bbox (List[int]): [x_min, y_min, x_max, y_max] Is used to calculate sliced coco coordinates.
Source code in sahi/utils/shapely.py
to_list
¶Convert to nested list of coordinate tuples.
Returns:
| Type | Description |
|---|---|
list[list[tuple[float, float]]]
|
List format: [ [(x1, y1), (x2, y2), (x3, y3), ...], [(x1, y1), (x2, y2), (x3, y3), ...], ... |
list[list[tuple[float, float]]]
|
]. |
Source code in sahi/utils/shapely.py
to_coco_segmentation
¶Convert to COCO segmentation format.
Returns:
| Type | Description |
|---|---|
list[list[int]]
|
List format: [ [x1, y1, x2, y2, x3, y3, ...], [x1, y1, x2, y2, x3, y3, ...], ... |
list[list[int]]
|
]. |
Source code in sahi/utils/shapely.py
to_opencv_contours
¶Convert to OpenCV contours format.
Source code in sahi/utils/shapely.py
to_xywh
¶[xmin, ymin, width, height].
Source code in sahi/utils/shapely.py
to_coco_bbox
¶ to_xyxy
¶[xmin, ymin, xmax, ymax].
Source code in sahi/utils/shapely.py
to_voc_bbox
¶ get_convex_hull_shapely_annotation
¶get_convex_hull_shapely_annotation() -> ShapelyAnnotation
Return convex hull of this annotation as a new ShapelyAnnotation.
Source code in sahi/utils/shapely.py
get_simplified_shapely_annotation
¶get_simplified_shapely_annotation(
tolerance: float = 1,
) -> ShapelyAnnotation
Return simplified version of this annotation as a new ShapelyAnnotation.
Source code in sahi/utils/shapely.py
get_buffered_shapely_annotation
¶get_buffered_shapely_annotation(
distance: float = 3,
resolution: int = 16,
quadsegs: int | None = None,
cap_style: int = round,
join_style: int = round,
mitre_limit: float = 5.0,
single_sided: bool = False,
) -> ShapelyAnnotation
Approximates the present polygon to have a valid polygon shape.
For more, check: https://shapely.readthedocs.io/en/stable/manual.html#object.buffer
Source code in sahi/utils/shapely.py
get_intersection
¶get_intersection(polygon: Polygon) -> ShapelyAnnotation
Accepts shapely polygon object and returns the intersection in ShapelyAnnotation format.
Source code in sahi/utils/shapely.py
get_shapely_box
¶get_shapely_box(
x: int | float,
y: int | float,
width: int | float,
height: int | float,
) -> Polygon
Accepts coco style bbox coords and converts it to shapely box object.
Source code in sahi/utils/shapely.py
get_shapely_multipolygon
¶Accepts coco style polygon coords and converts it to valid shapely multipolygon object.
Source code in sahi/utils/shapely.py
get_bbox_from_shapely
¶Accepts shapely box/poly object and returns its bounding box in coco and voc formats.
Source code in sahi/utils/shapely.py
table
¶
Table formatting utilities.
create_ascii_table
¶create_ascii_table(data: list[list[Any]]) -> str
Creates a clean, properly padded ASCII string grid from a list of lists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
¶ |
List[List[Any]]
|
A list of lists representing headers and rows. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The formatted ASCII table as a string. |
Source code in sahi/utils/table.py
torch_utils
¶
Torch-related utilities for tensor operations.
empty_cuda_cache
¶Release all unused cached memory from the CUDA allocator.
Acts as a no-op when torch is not installed, so it is safe to call unconditionally regardless of the runtime environment.
Source code in sahi/utils/torch_utils.py
to_float_tensor
¶to_float_tensor(img: ndarray | Image) -> Tensor
Convert PIL.Image or numpy array to torch.FloatTensor.
Converts a PIL.Image (RGB) or numpy.ndarray (H x W x C) in the range [0, 255] to a torch.FloatTensor of shape (C x H x W).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
¶ |
ndarray | Image
|
PIL.Image or numpy array |
required |
Returns: torch.tensor
Source code in sahi/utils/torch_utils.py
torch_to_numpy
¶torch_to_numpy(img: Tensor) -> ndarray
Convert a torch image tensor to a numpy array in HWC format.
Pixel values greater than 1 are rescaled to [0, 1] by dividing by 255.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
¶ |
Tensor
|
A torch.Tensor of shape (C, H, W). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
A numpy array of shape (H, W, C) with values in [0, 1]. |
Source code in sahi/utils/torch_utils.py
select_device
¶select_device(device: str | None = None) -> str | device
Selects compute device.
When torch is not installed, returns the string "cpu" (raising an
error if a GPU device was explicitly requested). When torch is
installed, returns a torch.device with the usual auto-detection
logic (cuda > mps > cpu).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
¶ |
str | None
|
"cpu", "mps", "cuda", "cuda:0", "cuda:1", etc. When no device string is given, the order of preference to try is: cuda:0 > mps > cpu |
None
|
Returns:
| Type | Description |
|---|---|
str | device
|
torch.device (when torch is available) or str "cpu" |
Inspired by https://github.com/ultralytics/yolov5/blob/6371de8879e7ad7ec5283e8b95cc6dd85d6a5e72/utils/torch_utils.py#L107
Source code in sahi/utils/torch_utils.py
torchvision
¶
Torchvision model utilities.
yolov5
¶
YOLOv5 model utilities and constants.
Yolov5TestConstants
¶YOLOv5 test model configurations.
download_yolov5n_model
¶Download the YOLOv5-Nano model for testing.
Source code in sahi/utils/yolov5.py
download_yolov5s6_model
¶Download the YOLOv5s6 model for testing.