Skip to content

LibreYOLO Model

sahi.models.libreyolo

LibreYOLO detection model wrapper for SAHI.

LibreYOLO (https://github.com/LibreYOLO/libreyolo) is an MIT-licensed library with an Ultralytics compatible API.

Classes

LibreYoloDetectionModel

LibreYoloDetectionModel(
    *args: object,
    fuse: bool = False,
    task: str | None = None,
    **kwargs: object,
)

Bases: UltralyticsDetectionModel

LibreYOLO object detection model, reusing the Ultralytics wrapper.

Source code in sahi/models/ultralytics.py
def __init__(self, *args: object, fuse: bool = False, task: str | None = None, **kwargs: object) -> None:
    """Initialize the Ultralytics detection model.

    Accepts all arguments from ``DetectionModel.__init__`` plus the
    following keyword arguments.

    Args:
        *args: Variable length argument list passed to DetectionModel.
        fuse: If True, fuse Conv2d and BatchNorm2d layers for faster
            inference. Default: False.
        task: Ultralytics task type (e.g. ``"detect"``, ``"segment"``,
            ``"obb"``). When None, the task is inferred from the model.
            Default: None.
        **kwargs: Arbitrary keyword arguments passed to DetectionModel.
    """
    self.fuse: bool = fuse
    self.task: str | None = task
    existing_packages = getattr(self, "required_packages", None) or []
    self.required_packages = [*list(existing_packages), "ultralytics"]
    super().__init__(*args, **kwargs)  # type: ignore[misc, arg-type]
Methods:
check_dependencies
check_dependencies(
    packages: list[str] | None = None,
) -> None

Check for libreyolo instead of ultralytics.

Source code in sahi/models/libreyolo.py
def check_dependencies(self, packages: list[str] | None = None) -> None:
    """Check for libreyolo instead of ultralytics."""
    super().check_dependencies(packages=["libreyolo"])
load_model
load_model() -> None

Detection model is initialized and set to self.model.

Source code in sahi/models/libreyolo.py
def load_model(self) -> None:
    """Detection model is initialized and set to self.model."""
    from libreyolo import LibreYOLO

    try:
        model = LibreYOLO(self.model_path or "LibreYOLO9t.pt", device=self.device, task=self.task)
        self.set_model(model)
    except Exception as e:
        raise TypeError("model_path is not a valid LibreYOLO model path: ", e)