ΠŸΠ΅Ρ€Π΅ΠΉΡ‚ΠΈ ΠΊ содСрТимому

Бсылка для ultralytics/utils/instance.py

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅

Π­Ρ‚ΠΎΡ‚ Ρ„Π°ΠΉΠ» доступСн ΠΏΠΎ адрСсу https://github.com/ultralytics/ ultralytics/blob/main/ ultralytics/utils/instance .py. Если Ρ‚Ρ‹ Π·Π°ΠΌΠ΅Ρ‚ΠΈΠ» ΠΏΡ€ΠΎΠ±Π»Π΅ΠΌΡƒ, поТалуйста, ΠΏΠΎΠΌΠΎΠ³ΠΈ ΠΈΡΠΏΡ€Π°Π²ΠΈΡ‚ΡŒ Π΅Π΅, ΠΎΡ‚ΠΏΡ€Π°Π²ΠΈΠ² Pull Request πŸ› οΈ. Бпасибо πŸ™!



ultralytics.utils.instance.Bboxes

Класс для Ρ€Π°Π±ΠΎΡ‚Ρ‹ с ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΠ²Π°ΡŽΡ‰ΠΈΠΌΠΈ Ρ€Π°ΠΌΠΊΠ°ΠΌΠΈ.

Класс ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°Π΅Ρ‚ Ρ€Π°Π·Π»ΠΈΡ‡Π½Ρ‹Π΅ Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Ρ‹ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Ρ… Ρ€Π°ΠΌΠΎΠΊ, Ρ‚Π°ΠΊΠΈΠ΅ ΠΊΠ°ΠΊ 'xyxy', 'xywh' ΠΈ 'ltwh'. Π”Π°Π½Π½Ρ‹Π΅ ΠΎ Π³Ρ€Π°Π½ΠΈΡ†Π°Ρ… Π΄ΠΎΠ»ΠΆΠ½Ρ‹ Π±Ρ‹Ρ‚ΡŒ прСдоставлСны Π² массивах numpy.

Атрибуты:

Имя Вип ОписаниС
bboxes ndarray

ΠžΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Π΅ Ρ€Π°ΠΌΠΊΠΈ хранятся Π² Π΄Π²ΡƒΠΌΠ΅Ρ€Π½ΠΎΠΌ массивС numpy.

format str

Π€ΠΎΡ€ΠΌΠ°Ρ‚ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Ρ… Ρ€Π°ΠΌΠΎΠΊ ('xyxy', 'xywh' ΠΈΠ»ΠΈ 'ltwh').

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅

Π­Ρ‚ΠΎΡ‚ класс Π½Π΅ ΠΎΠ±Ρ€Π°Π±Π°Ρ‚Ρ‹Π²Π°Π΅Ρ‚ Π½ΠΎΡ€ΠΌΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡŽ ΠΈΠ»ΠΈ Π΄Π΅Π½ΠΎΡ€ΠΌΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡŽ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΠ²Π°ΡŽΡ‰ΠΈΡ… боксов.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/instance.py
class Bboxes:
    """
    A class for handling bounding boxes.

    The class supports various bounding box formats like 'xyxy', 'xywh', and 'ltwh'.
    Bounding box data should be provided in numpy arrays.

    Attributes:
        bboxes (numpy.ndarray): The bounding boxes stored in a 2D numpy array.
        format (str): The format of the bounding boxes ('xyxy', 'xywh', or 'ltwh').

    Note:
        This class does not handle normalization or denormalization of bounding boxes.
    """

    def __init__(self, bboxes, format="xyxy") -> None:
        """Initializes the Bboxes class with bounding box data in a specified format."""
        assert format in _formats, f"Invalid bounding box format: {format}, format must be one of {_formats}"
        bboxes = bboxes[None, :] if bboxes.ndim == 1 else bboxes
        assert bboxes.ndim == 2
        assert bboxes.shape[1] == 4
        self.bboxes = bboxes
        self.format = format
        # self.normalized = normalized

    def convert(self, format):
        """Converts bounding box format from one type to another."""
        assert format in _formats, f"Invalid bounding box format: {format}, format must be one of {_formats}"
        if self.format == format:
            return
        elif self.format == "xyxy":
            func = xyxy2xywh if format == "xywh" else xyxy2ltwh
        elif self.format == "xywh":
            func = xywh2xyxy if format == "xyxy" else xywh2ltwh
        else:
            func = ltwh2xyxy if format == "xyxy" else ltwh2xywh
        self.bboxes = func(self.bboxes)
        self.format = format

    def areas(self):
        """Return box areas."""
        return (
            (self.bboxes[:, 2] - self.bboxes[:, 0]) * (self.bboxes[:, 3] - self.bboxes[:, 1])  # format xyxy
            if self.format == "xyxy"
            else self.bboxes[:, 3] * self.bboxes[:, 2]  # format xywh or ltwh
        )

    # def denormalize(self, w, h):
    #    if not self.normalized:
    #         return
    #     assert (self.bboxes <= 1.0).all()
    #     self.bboxes[:, 0::2] *= w
    #     self.bboxes[:, 1::2] *= h
    #     self.normalized = False
    #
    # def normalize(self, w, h):
    #     if self.normalized:
    #         return
    #     assert (self.bboxes > 1.0).any()
    #     self.bboxes[:, 0::2] /= w
    #     self.bboxes[:, 1::2] /= h
    #     self.normalized = True

    def mul(self, scale):
        """
        Args:
            scale (tuple | list | int): the scale for four coords.
        """
        if isinstance(scale, Number):
            scale = to_4tuple(scale)
        assert isinstance(scale, (tuple, list))
        assert len(scale) == 4
        self.bboxes[:, 0] *= scale[0]
        self.bboxes[:, 1] *= scale[1]
        self.bboxes[:, 2] *= scale[2]
        self.bboxes[:, 3] *= scale[3]

    def add(self, offset):
        """
        Args:
            offset (tuple | list | int): the offset for four coords.
        """
        if isinstance(offset, Number):
            offset = to_4tuple(offset)
        assert isinstance(offset, (tuple, list))
        assert len(offset) == 4
        self.bboxes[:, 0] += offset[0]
        self.bboxes[:, 1] += offset[1]
        self.bboxes[:, 2] += offset[2]
        self.bboxes[:, 3] += offset[3]

    def __len__(self):
        """Return the number of boxes."""
        return len(self.bboxes)

    @classmethod
    def concatenate(cls, boxes_list: List["Bboxes"], axis=0) -> "Bboxes":
        """
        Concatenate a list of Bboxes objects into a single Bboxes object.

        Args:
            boxes_list (List[Bboxes]): A list of Bboxes objects to concatenate.
            axis (int, optional): The axis along which to concatenate the bounding boxes.
                                   Defaults to 0.

        Returns:
            Bboxes: A new Bboxes object containing the concatenated bounding boxes.

        Note:
            The input should be a list or tuple of Bboxes objects.
        """
        assert isinstance(boxes_list, (list, tuple))
        if not boxes_list:
            return cls(np.empty(0))
        assert all(isinstance(box, Bboxes) for box in boxes_list)

        if len(boxes_list) == 1:
            return boxes_list[0]
        return cls(np.concatenate([b.bboxes for b in boxes_list], axis=axis))

    def __getitem__(self, index) -> "Bboxes":
        """
        Retrieve a specific bounding box or a set of bounding boxes using indexing.

        Args:
            index (int, slice, or np.ndarray): The index, slice, or boolean array to select
                                               the desired bounding boxes.

        Returns:
            Bboxes: A new Bboxes object containing the selected bounding boxes.

        Raises:
            AssertionError: If the indexed bounding boxes do not form a 2-dimensional matrix.

        Note:
            When using boolean indexing, make sure to provide a boolean array with the same
            length as the number of bounding boxes.
        """
        if isinstance(index, int):
            return Bboxes(self.bboxes[index].view(1, -1))
        b = self.bboxes[index]
        assert b.ndim == 2, f"Indexing on Bboxes with {index} failed to return a matrix!"
        return Bboxes(b)

__getitem__(index)

ΠŸΠΎΠ»ΡƒΡ‡ΠΈ ΠΊΠΎΠ½ΠΊΡ€Π΅Ρ‚Π½ΡƒΡŽ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½ΡƒΡŽ Ρ€Π°ΠΌΠΊΡƒ ΠΈΠ»ΠΈ Π½Π°Π±ΠΎΡ€ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Ρ… Ρ€Π°ΠΌΠΎΠΊ, ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΡ ΠΈΠ½Π΄Π΅ΠΊΡΠ°Ρ†ΠΈΡŽ.

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹:

Имя Π’ΠΈΠΏ ОписаниС По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ
index int, slice, or np.ndarray

ИндСкс, Ρ„Ρ€Π°Π³ΠΌΠ΅Π½Ρ‚ ΠΈΠ»ΠΈ логичСский массив для Π²Ρ‹Π±ΠΎΡ€Π° Π½ΡƒΠΆΠ½Ρ‹Π΅ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Π΅ Ρ€Π°ΠΌΠΊΠΈ.

трСбуСтся

ВозвращаСтся:

Имя Вип ОписаниС
Bboxes Bboxes

Новый ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ Bboxes, содСрТащий Π²Ρ‹Π±Ρ€Π°Π½Π½Ρ‹Π΅ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Π΅ Ρ€Π°ΠΌΠΊΠΈ.

ΠŸΠΎΠ΄Π½ΠΈΠΌΠ°Π΅Ρ‚:

Вип ОписаниС
AssertionError

Если индСксированныС ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Π΅ Ρ€Π°ΠΌΠΊΠΈ Π½Π΅ ΠΎΠ±Ρ€Π°Π·ΡƒΡŽΡ‚ Π΄Π²ΡƒΠΌΠ΅Ρ€Π½ΡƒΡŽ ΠΌΠ°Ρ‚Ρ€ΠΈΡ†Ρƒ.

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅

ΠŸΡ€ΠΈ использовании Π±ΡƒΠ»Π΅Π²ΠΎΠΉ индСксации ΠΎΠ±ΡΠ·Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎ ΠΏΡ€Π΅Π΄ΠΎΡΡ‚Π°Π²ΡŒ Π±ΡƒΠ»Π΅Π²Ρ‹ΠΉ массив Ρ‚ΠΎΠΉ ΠΆΠ΅ Π΄Π»ΠΈΠ½Ρ‹. Π΄Π»ΠΈΠ½ΠΎΠΉ, Ρ€Π°Π²Π½ΠΎΠΉ количСству ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΠ²Π°ΡŽΡ‰ΠΈΡ… боксов.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/instance.py
def __getitem__(self, index) -> "Bboxes":
    """
    Retrieve a specific bounding box or a set of bounding boxes using indexing.

    Args:
        index (int, slice, or np.ndarray): The index, slice, or boolean array to select
                                           the desired bounding boxes.

    Returns:
        Bboxes: A new Bboxes object containing the selected bounding boxes.

    Raises:
        AssertionError: If the indexed bounding boxes do not form a 2-dimensional matrix.

    Note:
        When using boolean indexing, make sure to provide a boolean array with the same
        length as the number of bounding boxes.
    """
    if isinstance(index, int):
        return Bboxes(self.bboxes[index].view(1, -1))
    b = self.bboxes[index]
    assert b.ndim == 2, f"Indexing on Bboxes with {index} failed to return a matrix!"
    return Bboxes(b)

__init__(bboxes, format='xyxy')

Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·ΠΈΡ€ΡƒΠ΅Ρ‚ класс Bboxes с Π΄Π°Π½Π½Ρ‹ΠΌΠΈ ΠΎ Π³Ρ€Π°Π½ΠΈΡ‡Π½Ρ‹Ρ… боксах Π² ΡƒΠΊΠ°Π·Π°Π½Π½ΠΎΠΌ Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/instance.py
def __init__(self, bboxes, format="xyxy") -> None:
    """Initializes the Bboxes class with bounding box data in a specified format."""
    assert format in _formats, f"Invalid bounding box format: {format}, format must be one of {_formats}"
    bboxes = bboxes[None, :] if bboxes.ndim == 1 else bboxes
    assert bboxes.ndim == 2
    assert bboxes.shape[1] == 4
    self.bboxes = bboxes
    self.format = format

__len__()

Π’Π΅Ρ€Π½ΠΈ количСство ΠΊΠΎΡ€ΠΎΠ±ΠΎΠΊ.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/instance.py
def __len__(self):
    """Return the number of boxes."""
    return len(self.bboxes)

add(offset)

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹:

Имя Π’ΠΈΠΏ ОписаниС По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ
offset tuple | list | int

смСщСниС для Ρ‡Π΅Ρ‚Ρ‹Ρ€Π΅Ρ… ΠΊΠΎΠΎΡ€Π΄.

трСбуСтся
Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/instance.py
def add(self, offset):
    """
    Args:
        offset (tuple | list | int): the offset for four coords.
    """
    if isinstance(offset, Number):
        offset = to_4tuple(offset)
    assert isinstance(offset, (tuple, list))
    assert len(offset) == 4
    self.bboxes[:, 0] += offset[0]
    self.bboxes[:, 1] += offset[1]
    self.bboxes[:, 2] += offset[2]
    self.bboxes[:, 3] += offset[3]

areas()

ΠžΠ±Π»Π°ΡΡ‚ΠΈ Π²ΠΎΠ·Π²Ρ€Π°Ρ‚Π° ΠΊΠΎΡ€ΠΎΠ±ΠΊΠΈ.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/instance.py
def areas(self):
    """Return box areas."""
    return (
        (self.bboxes[:, 2] - self.bboxes[:, 0]) * (self.bboxes[:, 3] - self.bboxes[:, 1])  # format xyxy
        if self.format == "xyxy"
        else self.bboxes[:, 3] * self.bboxes[:, 2]  # format xywh or ltwh
    )

concatenate(boxes_list, axis=0) classmethod

ΠšΠΎΠ½ΠΊΠ°Ρ‚Π΅Π½ΠΈΡ€ΡƒΠΉ список ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ΠΎΠ² Bboxes Π² ΠΎΠ΄ΠΈΠ½ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ Bboxes.

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹:

Имя Π’ΠΈΠΏ ОписаниС По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ
boxes_list List[Bboxes]

Бписок ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ΠΎΠ² Bboxes для ΠΊΠΎΠ½ΠΊΠ°Ρ‚Π΅Π½Π°Ρ†ΠΈΠΈ.

трСбуСтся
axis int

Ось, вдоль ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΉ Π½ΡƒΠΆΠ½ΠΎ ΠΎΠ±ΡŠΠ΅Π΄ΠΈΠ½ΠΈΡ‚ΡŒ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Π΅ Ρ€Π°ΠΌΠΊΠΈ. По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ Ρ€Π°Π²Π½ΠΎ 0.

0

ВозвращаСтся:

Имя Вип ОписаниС
Bboxes Bboxes

Новый ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ Bboxes, содСрТащий ΠΊΠΎΠ½ΠΊΠ°Ρ‚Π΅Π½ΠΈΡ€ΠΎΠ²Π°Π½Π½Ρ‹Π΅ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Π΅ Ρ€Π°ΠΌΠΊΠΈ.

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅

На Π²Ρ…ΠΎΠ΄Π΅ Π΄ΠΎΠ»ΠΆΠ΅Π½ Π±Ρ‹Ρ‚ΡŒ список ΠΈΠ»ΠΈ ΠΊΠΎΡ€Ρ‚Π΅ΠΆ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ΠΎΠ² Bboxes.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/instance.py
@classmethod
def concatenate(cls, boxes_list: List["Bboxes"], axis=0) -> "Bboxes":
    """
    Concatenate a list of Bboxes objects into a single Bboxes object.

    Args:
        boxes_list (List[Bboxes]): A list of Bboxes objects to concatenate.
        axis (int, optional): The axis along which to concatenate the bounding boxes.
                               Defaults to 0.

    Returns:
        Bboxes: A new Bboxes object containing the concatenated bounding boxes.

    Note:
        The input should be a list or tuple of Bboxes objects.
    """
    assert isinstance(boxes_list, (list, tuple))
    if not boxes_list:
        return cls(np.empty(0))
    assert all(isinstance(box, Bboxes) for box in boxes_list)

    if len(boxes_list) == 1:
        return boxes_list[0]
    return cls(np.concatenate([b.bboxes for b in boxes_list], axis=axis))

convert(format)

ΠŸΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΡƒΠ΅Ρ‚ Ρ„ΠΎΡ€ΠΌΠ°Ρ‚ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎΠΉ Ρ€Π°ΠΌΠΊΠΈ ΠΈΠ· ΠΎΠ΄Π½ΠΎΠ³ΠΎ Ρ‚ΠΈΠΏΠ° Π² Π΄Ρ€ΡƒΠ³ΠΎΠΉ.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/instance.py
def convert(self, format):
    """Converts bounding box format from one type to another."""
    assert format in _formats, f"Invalid bounding box format: {format}, format must be one of {_formats}"
    if self.format == format:
        return
    elif self.format == "xyxy":
        func = xyxy2xywh if format == "xywh" else xyxy2ltwh
    elif self.format == "xywh":
        func = xywh2xyxy if format == "xyxy" else xywh2ltwh
    else:
        func = ltwh2xyxy if format == "xyxy" else ltwh2xywh
    self.bboxes = func(self.bboxes)
    self.format = format

mul(scale)

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹:

Имя Π’ΠΈΠΏ ОписаниС По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ
scale tuple | list | int

шкала для Ρ‡Π΅Ρ‚Ρ‹Ρ€Π΅Ρ… ΠΊΠΎΠΎΡ€Π΄.

трСбуСтся
Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/instance.py
def mul(self, scale):
    """
    Args:
        scale (tuple | list | int): the scale for four coords.
    """
    if isinstance(scale, Number):
        scale = to_4tuple(scale)
    assert isinstance(scale, (tuple, list))
    assert len(scale) == 4
    self.bboxes[:, 0] *= scale[0]
    self.bboxes[:, 1] *= scale[1]
    self.bboxes[:, 2] *= scale[2]
    self.bboxes[:, 3] *= scale[3]



ultralytics.utils.instance.Instances

ΠšΠΎΠ½Ρ‚Π΅ΠΉΠ½Π΅Ρ€ для ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Ρ… Ρ€Π°ΠΌΠΎΠΊ, сСгмСнтов ΠΈ ΠΊΠ»ΡŽΡ‡Π΅Π²Ρ‹Ρ… Ρ‚ΠΎΡ‡Π΅ΠΊ ΠΎΠ±Π½Π°Ρ€ΡƒΠΆΠ΅Π½Π½Ρ‹Ρ… ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ΠΎΠ² Π½Π° ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΈ.

Атрибуты:

Имя Вип ОписаниС
_bboxes Bboxes

Π’Π½ΡƒΡ‚Ρ€Π΅Π½Π½ΠΈΠΉ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ для ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠΈ ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΉ с ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹ΠΌΠΈ Ρ€Π°ΠΌΠΊΠ°ΠΌΠΈ.

keypoints ndarray

ΠšΠ»ΡŽΡ‡Π΅Π²Ρ‹Π΅ Ρ‚ΠΎΡ‡ΠΊΠΈ(x, y, visible) с Ρ„ΠΎΡ€ΠΌΠΎΠΉ [N, 17, 3]. По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ это None.

normalized bool

Π€Π»Π°Π³, ΡƒΠΊΠ°Π·Ρ‹Π²Π°ΡŽΡ‰ΠΈΠΉ, Π½ΠΎΡ€ΠΌΠ°Π»ΠΈΠ·ΠΎΠ²Π°Π½Ρ‹ Π»ΠΈ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎΠΉ Ρ€Π°ΠΌΠΊΠΈ.

segments ndarray

Π‘Π΅Π³ΠΌΠ΅Π½Ρ‚Ρ‹ массива с Ρ„ΠΎΡ€ΠΌΠΎΠΉ [N, 1000, 2] послС ΠΏΠΎΠ²Ρ‚ΠΎΡ€Π½ΠΎΠΉ Π²Ρ‹Π±ΠΎΡ€ΠΊΠΈ.

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹:

Имя Π’ΠΈΠΏ ОписаниС По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ
bboxes ndarray

Массив ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΠ²Π°ΡŽΡ‰ΠΈΡ… боксов с Ρ„ΠΎΡ€ΠΌΠΎΠΉ [N, 4].

трСбуСтся
segments list | ndarray

Бписок ΠΈΠ»ΠΈ массив сСгмСнтов ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ΠΎΠ². По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ - None.

None
keypoints ndarray

Массив ΠΊΠ»ΡŽΡ‡Π΅Π²Ρ‹Ρ… Ρ‚ΠΎΡ‡Π΅ΠΊ с Ρ„ΠΎΡ€ΠΌΠΎΠΉ [N, 17, 3]. По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ это None.

None
bbox_format str

Π€ΠΎΡ€ΠΌΠ°Ρ‚ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Ρ… Ρ€Π°ΠΌΠΎΠΊ ('xywh' ΠΈΠ»ΠΈ 'xyxy'). По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ - 'xywh'.

'xywh'
normalized bool

ΠΠΎΡ€ΠΌΠ°Π»ΠΈΠ·ΡƒΡŽΡ‚ΡΡ Π»ΠΈ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎΠΉ Ρ€Π°ΠΌΠΊΠΈ. По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ это Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Ρ€Π°Π²Π½ΠΎ True.

True

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹:

# Create an Instances object
instances = Instances(
    bboxes=np.array([[10, 10, 30, 30], [20, 20, 40, 40]]),
    segments=[np.array([[5, 5], [10, 10]]), np.array([[15, 15], [20, 20]])],
    keypoints=np.array([[[5, 5, 1], [10, 10, 1]], [[15, 15, 1], [20, 20, 1]]])
)
ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅

Π€ΠΎΡ€ΠΌΠ°Ρ‚ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎΠΉ Ρ€Π°ΠΌΠΊΠΈ - Π»ΠΈΠ±ΠΎ 'xywh', Π»ΠΈΠ±ΠΎ 'xyxy', ΠΈ ΠΎΠ½ опрСдСляСтся bbox_format Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚. Π­Ρ‚ΠΎΡ‚ класс Π½Π΅ выполняСт ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΡƒ Π²Π²ΠΎΠ΄Π° ΠΈ ΠΏΡ€Π΅Π΄ΠΏΠΎΠ»Π°Π³Π°Π΅Ρ‚, Ρ‡Ρ‚ΠΎ Π²Π²ΠΎΠ΄ΠΈΠΌΡ‹Π΅ Π΄Π°Π½Π½Ρ‹Π΅ Ρ…ΠΎΡ€ΠΎΡˆΠΎ сформированы.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/instance.py
class Instances:
    """
    Container for bounding boxes, segments, and keypoints of detected objects in an image.

    Attributes:
        _bboxes (Bboxes): Internal object for handling bounding box operations.
        keypoints (ndarray): keypoints(x, y, visible) with shape [N, 17, 3]. Default is None.
        normalized (bool): Flag indicating whether the bounding box coordinates are normalized.
        segments (ndarray): Segments array with shape [N, 1000, 2] after resampling.

    Args:
        bboxes (ndarray): An array of bounding boxes with shape [N, 4].
        segments (list | ndarray, optional): A list or array of object segments. Default is None.
        keypoints (ndarray, optional): An array of keypoints with shape [N, 17, 3]. Default is None.
        bbox_format (str, optional): The format of bounding boxes ('xywh' or 'xyxy'). Default is 'xywh'.
        normalized (bool, optional): Whether the bounding box coordinates are normalized. Default is True.

    Examples:
        ```python
        # Create an Instances object
        instances = Instances(
            bboxes=np.array([[10, 10, 30, 30], [20, 20, 40, 40]]),
            segments=[np.array([[5, 5], [10, 10]]), np.array([[15, 15], [20, 20]])],
            keypoints=np.array([[[5, 5, 1], [10, 10, 1]], [[15, 15, 1], [20, 20, 1]]])
        )
        ```

    Note:
        The bounding box format is either 'xywh' or 'xyxy', and is determined by the `bbox_format` argument.
        This class does not perform input validation, and it assumes the inputs are well-formed.
    """

    def __init__(self, bboxes, segments=None, keypoints=None, bbox_format="xywh", normalized=True) -> None:
        """
        Args:
            bboxes (ndarray): bboxes with shape [N, 4].
            segments (list | ndarray): segments.
            keypoints (ndarray): keypoints(x, y, visible) with shape [N, 17, 3].
        """
        self._bboxes = Bboxes(bboxes=bboxes, format=bbox_format)
        self.keypoints = keypoints
        self.normalized = normalized
        self.segments = segments

    def convert_bbox(self, format):
        """Convert bounding box format."""
        self._bboxes.convert(format=format)

    @property
    def bbox_areas(self):
        """Calculate the area of bounding boxes."""
        return self._bboxes.areas()

    def scale(self, scale_w, scale_h, bbox_only=False):
        """This might be similar with denormalize func but without normalized sign."""
        self._bboxes.mul(scale=(scale_w, scale_h, scale_w, scale_h))
        if bbox_only:
            return
        self.segments[..., 0] *= scale_w
        self.segments[..., 1] *= scale_h
        if self.keypoints is not None:
            self.keypoints[..., 0] *= scale_w
            self.keypoints[..., 1] *= scale_h

    def denormalize(self, w, h):
        """Denormalizes boxes, segments, and keypoints from normalized coordinates."""
        if not self.normalized:
            return
        self._bboxes.mul(scale=(w, h, w, h))
        self.segments[..., 0] *= w
        self.segments[..., 1] *= h
        if self.keypoints is not None:
            self.keypoints[..., 0] *= w
            self.keypoints[..., 1] *= h
        self.normalized = False

    def normalize(self, w, h):
        """Normalize bounding boxes, segments, and keypoints to image dimensions."""
        if self.normalized:
            return
        self._bboxes.mul(scale=(1 / w, 1 / h, 1 / w, 1 / h))
        self.segments[..., 0] /= w
        self.segments[..., 1] /= h
        if self.keypoints is not None:
            self.keypoints[..., 0] /= w
            self.keypoints[..., 1] /= h
        self.normalized = True

    def add_padding(self, padw, padh):
        """Handle rect and mosaic situation."""
        assert not self.normalized, "you should add padding with absolute coordinates."
        self._bboxes.add(offset=(padw, padh, padw, padh))
        self.segments[..., 0] += padw
        self.segments[..., 1] += padh
        if self.keypoints is not None:
            self.keypoints[..., 0] += padw
            self.keypoints[..., 1] += padh

    def __getitem__(self, index) -> "Instances":
        """
        Retrieve a specific instance or a set of instances using indexing.

        Args:
            index (int, slice, or np.ndarray): The index, slice, or boolean array to select
                                               the desired instances.

        Returns:
            Instances: A new Instances object containing the selected bounding boxes,
                       segments, and keypoints if present.

        Note:
            When using boolean indexing, make sure to provide a boolean array with the same
            length as the number of instances.
        """
        segments = self.segments[index] if len(self.segments) else self.segments
        keypoints = self.keypoints[index] if self.keypoints is not None else None
        bboxes = self.bboxes[index]
        bbox_format = self._bboxes.format
        return Instances(
            bboxes=bboxes,
            segments=segments,
            keypoints=keypoints,
            bbox_format=bbox_format,
            normalized=self.normalized,
        )

    def flipud(self, h):
        """Flips the coordinates of bounding boxes, segments, and keypoints vertically."""
        if self._bboxes.format == "xyxy":
            y1 = self.bboxes[:, 1].copy()
            y2 = self.bboxes[:, 3].copy()
            self.bboxes[:, 1] = h - y2
            self.bboxes[:, 3] = h - y1
        else:
            self.bboxes[:, 1] = h - self.bboxes[:, 1]
        self.segments[..., 1] = h - self.segments[..., 1]
        if self.keypoints is not None:
            self.keypoints[..., 1] = h - self.keypoints[..., 1]

    def fliplr(self, w):
        """Reverses the order of the bounding boxes and segments horizontally."""
        if self._bboxes.format == "xyxy":
            x1 = self.bboxes[:, 0].copy()
            x2 = self.bboxes[:, 2].copy()
            self.bboxes[:, 0] = w - x2
            self.bboxes[:, 2] = w - x1
        else:
            self.bboxes[:, 0] = w - self.bboxes[:, 0]
        self.segments[..., 0] = w - self.segments[..., 0]
        if self.keypoints is not None:
            self.keypoints[..., 0] = w - self.keypoints[..., 0]

    def clip(self, w, h):
        """Clips bounding boxes, segments, and keypoints values to stay within image boundaries."""
        ori_format = self._bboxes.format
        self.convert_bbox(format="xyxy")
        self.bboxes[:, [0, 2]] = self.bboxes[:, [0, 2]].clip(0, w)
        self.bboxes[:, [1, 3]] = self.bboxes[:, [1, 3]].clip(0, h)
        if ori_format != "xyxy":
            self.convert_bbox(format=ori_format)
        self.segments[..., 0] = self.segments[..., 0].clip(0, w)
        self.segments[..., 1] = self.segments[..., 1].clip(0, h)
        if self.keypoints is not None:
            self.keypoints[..., 0] = self.keypoints[..., 0].clip(0, w)
            self.keypoints[..., 1] = self.keypoints[..., 1].clip(0, h)

    def remove_zero_area_boxes(self):
        """Remove zero-area boxes, i.e. after clipping some boxes may have zero width or height."""
        good = self.bbox_areas > 0
        if not all(good):
            self._bboxes = self._bboxes[good]
            if len(self.segments):
                self.segments = self.segments[good]
            if self.keypoints is not None:
                self.keypoints = self.keypoints[good]
        return good

    def update(self, bboxes, segments=None, keypoints=None):
        """Updates instance variables."""
        self._bboxes = Bboxes(bboxes, format=self._bboxes.format)
        if segments is not None:
            self.segments = segments
        if keypoints is not None:
            self.keypoints = keypoints

    def __len__(self):
        """Return the length of the instance list."""
        return len(self.bboxes)

    @classmethod
    def concatenate(cls, instances_list: List["Instances"], axis=0) -> "Instances":
        """
        Concatenates a list of Instances objects into a single Instances object.

        Args:
            instances_list (List[Instances]): A list of Instances objects to concatenate.
            axis (int, optional): The axis along which the arrays will be concatenated. Defaults to 0.

        Returns:
            Instances: A new Instances object containing the concatenated bounding boxes,
                       segments, and keypoints if present.

        Note:
            The `Instances` objects in the list should have the same properties, such as
            the format of the bounding boxes, whether keypoints are present, and if the
            coordinates are normalized.
        """
        assert isinstance(instances_list, (list, tuple))
        if not instances_list:
            return cls(np.empty(0))
        assert all(isinstance(instance, Instances) for instance in instances_list)

        if len(instances_list) == 1:
            return instances_list[0]

        use_keypoint = instances_list[0].keypoints is not None
        bbox_format = instances_list[0]._bboxes.format
        normalized = instances_list[0].normalized

        cat_boxes = np.concatenate([ins.bboxes for ins in instances_list], axis=axis)
        cat_segments = np.concatenate([b.segments for b in instances_list], axis=axis)
        cat_keypoints = np.concatenate([b.keypoints for b in instances_list], axis=axis) if use_keypoint else None
        return cls(cat_boxes, cat_segments, cat_keypoints, bbox_format, normalized)

    @property
    def bboxes(self):
        """Return bounding boxes."""
        return self._bboxes.bboxes

bbox_areas property

Рассчитай ΠΏΠ»ΠΎΡ‰Π°Π΄ΡŒ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Ρ… ΠΊΠΎΡ€ΠΎΠ±ΠΎΠΊ.

bboxes property

Π’Π΅Ρ€Π½ΠΈ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Π΅ Ρ€Π°ΠΌΠΊΠΈ.

__getitem__(index)

ΠŸΠΎΠ»ΡƒΡ‡ΠΈ ΠΊΠΎΠ½ΠΊΡ€Π΅Ρ‚Π½Ρ‹ΠΉ экзСмпляр ΠΈΠ»ΠΈ Π½Π°Π±ΠΎΡ€ экзСмпляров, ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΡ ΠΈΠ½Π΄Π΅ΠΊΡΠ°Ρ†ΠΈΡŽ.

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹:

Имя Π’ΠΈΠΏ ОписаниС По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ
index int, slice, or np.ndarray

ИндСкс, Ρ„Ρ€Π°Π³ΠΌΠ΅Π½Ρ‚ ΠΈΠ»ΠΈ логичСский массив для Π²Ρ‹Π±ΠΎΡ€Π° Π½ΡƒΠΆΠ½Ρ‹Π΅ экзСмпляры.

трСбуСтся

ВозвращаСтся:

Имя Вип ОписаниС
Instances Instances

Новый ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ Instances, содСрТащий Π²Ρ‹Π±Ρ€Π°Π½Π½Ρ‹Π΅ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Π΅ Ρ€Π°ΠΌΠΊΠΈ, сСгмСнты ΠΈ ΠΊΠ»ΡŽΡ‡Π΅Π²Ρ‹Π΅ Ρ‚ΠΎΡ‡ΠΊΠΈ, Ссли ΠΎΠ½ΠΈ Π΅ΡΡ‚ΡŒ.

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅

ΠŸΡ€ΠΈ использовании Π±ΡƒΠ»Π΅Π²ΠΎΠΉ индСксации ΠΎΠ±ΡΠ·Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎ ΠΏΡ€Π΅Π΄ΠΎΡΡ‚Π°Π²ΡŒ Π±ΡƒΠ»Π΅Π²Ρ‹ΠΉ массив с Ρ‚ΠΎΠΉ ΠΆΠ΅ Π΄Π»ΠΈΠ½ΠΎΠΉ, Ρ€Π°Π²Π½ΠΎΠΉ количСству экзСмпляров.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/instance.py
def __getitem__(self, index) -> "Instances":
    """
    Retrieve a specific instance or a set of instances using indexing.

    Args:
        index (int, slice, or np.ndarray): The index, slice, or boolean array to select
                                           the desired instances.

    Returns:
        Instances: A new Instances object containing the selected bounding boxes,
                   segments, and keypoints if present.

    Note:
        When using boolean indexing, make sure to provide a boolean array with the same
        length as the number of instances.
    """
    segments = self.segments[index] if len(self.segments) else self.segments
    keypoints = self.keypoints[index] if self.keypoints is not None else None
    bboxes = self.bboxes[index]
    bbox_format = self._bboxes.format
    return Instances(
        bboxes=bboxes,
        segments=segments,
        keypoints=keypoints,
        bbox_format=bbox_format,
        normalized=self.normalized,
    )

__init__(bboxes, segments=None, keypoints=None, bbox_format='xywh', normalized=True)

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹:

Имя Π’ΠΈΠΏ ОписаниС По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ
bboxes ndarray

bboxes с Ρ„ΠΎΡ€ΠΌΠΎΠΉ [N, 4].

трСбуСтся
segments list | ndarray

сСгмСнты.

None
keypoints ndarray

ΠšΠ»ΡŽΡ‡Π΅Π²Ρ‹Π΅ Ρ‚ΠΎΡ‡ΠΊΠΈ(x, y, visible) с Ρ„ΠΎΡ€ΠΌΠΎΠΉ [N, 17, 3].

None
Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/instance.py
def __init__(self, bboxes, segments=None, keypoints=None, bbox_format="xywh", normalized=True) -> None:
    """
    Args:
        bboxes (ndarray): bboxes with shape [N, 4].
        segments (list | ndarray): segments.
        keypoints (ndarray): keypoints(x, y, visible) with shape [N, 17, 3].
    """
    self._bboxes = Bboxes(bboxes=bboxes, format=bbox_format)
    self.keypoints = keypoints
    self.normalized = normalized
    self.segments = segments

__len__()

Π’Π΅Ρ€Π½ΠΈ Π΄Π»ΠΈΠ½Ρƒ списка экзСмпляров.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/instance.py
def __len__(self):
    """Return the length of the instance list."""
    return len(self.bboxes)

add_padding(padw, padh)

Π‘ΠΏΡ€Π°Π²ΠΈΡΡŒ с рСксом ΠΈ ΠΌΠΎΠ·Π°ΠΈΡ‡Π½ΠΎΠΉ ситуациСй.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/instance.py
def add_padding(self, padw, padh):
    """Handle rect and mosaic situation."""
    assert not self.normalized, "you should add padding with absolute coordinates."
    self._bboxes.add(offset=(padw, padh, padw, padh))
    self.segments[..., 0] += padw
    self.segments[..., 1] += padh
    if self.keypoints is not None:
        self.keypoints[..., 0] += padw
        self.keypoints[..., 1] += padh

clip(w, h)

ΠžΠ±Ρ€Π΅ΠΆΡŒ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Π΅ Ρ€Π°ΠΌΠΊΠΈ, сСгмСнты ΠΈ значСния ΠΊΠ»ΡŽΡ‡Π΅Π²Ρ‹Ρ… Ρ‚ΠΎΡ‡Π΅ΠΊ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΎΠ½ΠΈ Π½Π΅ Π²Ρ‹Ρ…ΠΎΠ΄ΠΈΠ»ΠΈ Π·Π° Π³Ρ€Π°Π½ΠΈΡ†Ρ‹ изобраТСния.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/instance.py
def clip(self, w, h):
    """Clips bounding boxes, segments, and keypoints values to stay within image boundaries."""
    ori_format = self._bboxes.format
    self.convert_bbox(format="xyxy")
    self.bboxes[:, [0, 2]] = self.bboxes[:, [0, 2]].clip(0, w)
    self.bboxes[:, [1, 3]] = self.bboxes[:, [1, 3]].clip(0, h)
    if ori_format != "xyxy":
        self.convert_bbox(format=ori_format)
    self.segments[..., 0] = self.segments[..., 0].clip(0, w)
    self.segments[..., 1] = self.segments[..., 1].clip(0, h)
    if self.keypoints is not None:
        self.keypoints[..., 0] = self.keypoints[..., 0].clip(0, w)
        self.keypoints[..., 1] = self.keypoints[..., 1].clip(0, h)

concatenate(instances_list, axis=0) classmethod

ΠšΠΎΠ½ΠΊΠ°Ρ‚Π΅Π½ΠΈΡ€ΡƒΠ΅Ρ‚ список ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ΠΎΠ² Instances Π² ΠΎΠ΄ΠΈΠ½ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ Instances.

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹:

Имя Π’ΠΈΠΏ ОписаниС По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ
instances_list List[Instances]

Бписок ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ΠΎΠ² Instances для ΠΊΠΎΠ½ΠΊΠ°Ρ‚Π΅Π½Π°Ρ†ΠΈΠΈ.

трСбуСтся
axis int

Ось, вдоль ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΉ Π±ΡƒΠ΄ΡƒΡ‚ ΠΊΠΎΠ½ΠΊΠ°Ρ‚Π΅Π½ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒΡΡ массивы. По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ Ρ€Π°Π²Π½Π° 0.

0

ВозвращаСтся:

Имя Вип ОписаниС
Instances Instances

Новый ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ Instances, содСрТащий скомпонованныС ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Π΅ Ρ€Π°ΠΌΠΊΠΈ, сСгмСнты ΠΈ ΠΊΠ»ΡŽΡ‡Π΅Π²Ρ‹Π΅ Ρ‚ΠΎΡ‡ΠΊΠΈ, Ссли ΠΎΠ½ΠΈ Π΅ΡΡ‚ΡŒ.

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅

The Instances ΠžΠ±ΡŠΠ΅ΠΊΡ‚Ρ‹ Π² спискС Π΄ΠΎΠ»ΠΆΠ½Ρ‹ ΠΎΠ±Π»Π°Π΄Π°Ρ‚ΡŒ ΠΎΠ΄ΠΈΠ½Π°ΠΊΠΎΠ²Ρ‹ΠΌΠΈ свойствами, Ρ‚Π°ΠΊΠΈΠΌΠΈ ΠΊΠ°ΠΊ Ρ„ΠΎΡ€ΠΌΠ°Ρ‚ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Ρ… Ρ€Π°ΠΌΠΎΠΊ, Π½Π°Π»ΠΈΡ‡ΠΈΠ΅ ΠΊΠ»ΡŽΡ‡Π΅Π²Ρ‹Ρ… Ρ‚ΠΎΡ‡Π΅ΠΊ ΠΈ нормализация ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚. ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ Π½ΠΎΡ€ΠΌΠ°Π»ΠΈΠ·ΠΎΠ²Π°Π½Ρ‹.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/instance.py
@classmethod
def concatenate(cls, instances_list: List["Instances"], axis=0) -> "Instances":
    """
    Concatenates a list of Instances objects into a single Instances object.

    Args:
        instances_list (List[Instances]): A list of Instances objects to concatenate.
        axis (int, optional): The axis along which the arrays will be concatenated. Defaults to 0.

    Returns:
        Instances: A new Instances object containing the concatenated bounding boxes,
                   segments, and keypoints if present.

    Note:
        The `Instances` objects in the list should have the same properties, such as
        the format of the bounding boxes, whether keypoints are present, and if the
        coordinates are normalized.
    """
    assert isinstance(instances_list, (list, tuple))
    if not instances_list:
        return cls(np.empty(0))
    assert all(isinstance(instance, Instances) for instance in instances_list)

    if len(instances_list) == 1:
        return instances_list[0]

    use_keypoint = instances_list[0].keypoints is not None
    bbox_format = instances_list[0]._bboxes.format
    normalized = instances_list[0].normalized

    cat_boxes = np.concatenate([ins.bboxes for ins in instances_list], axis=axis)
    cat_segments = np.concatenate([b.segments for b in instances_list], axis=axis)
    cat_keypoints = np.concatenate([b.keypoints for b in instances_list], axis=axis) if use_keypoint else None
    return cls(cat_boxes, cat_segments, cat_keypoints, bbox_format, normalized)

convert_bbox(format)

ΠŸΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΡƒΠΉ Ρ„ΠΎΡ€ΠΌΠ°Ρ‚ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎΠΉ Ρ€Π°ΠΌΠΊΠΈ.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/instance.py
def convert_bbox(self, format):
    """Convert bounding box format."""
    self._bboxes.convert(format=format)

denormalize(w, h)

Π”Π΅Π½ΠΎΡ€ΠΌΠ°Π»ΠΈΠ·ΡƒΠΉ боксы, сСгмСнты ΠΈ ΠΊΠ»ΡŽΡ‡Π΅Π²Ρ‹Π΅ Ρ‚ΠΎΡ‡ΠΊΠΈ ΠΈΠ· Π½ΠΎΡ€ΠΌΠ°Π»ΠΈΠ·ΠΎΠ²Π°Π½Π½Ρ‹Ρ… ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/instance.py
def denormalize(self, w, h):
    """Denormalizes boxes, segments, and keypoints from normalized coordinates."""
    if not self.normalized:
        return
    self._bboxes.mul(scale=(w, h, w, h))
    self.segments[..., 0] *= w
    self.segments[..., 1] *= h
    if self.keypoints is not None:
        self.keypoints[..., 0] *= w
        self.keypoints[..., 1] *= h
    self.normalized = False

fliplr(w)

Π˜Π·ΠΌΠ΅Π½ΡΠ΅Ρ‚ порядок располоТСния ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Ρ… ΠΊΠΎΡ€ΠΎΠ±ΠΎΠΊ ΠΈ сСгмСнтов ΠΏΠΎ Π³ΠΎΡ€ΠΈΠ·ΠΎΠ½Ρ‚Π°Π»ΠΈ.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/instance.py
def fliplr(self, w):
    """Reverses the order of the bounding boxes and segments horizontally."""
    if self._bboxes.format == "xyxy":
        x1 = self.bboxes[:, 0].copy()
        x2 = self.bboxes[:, 2].copy()
        self.bboxes[:, 0] = w - x2
        self.bboxes[:, 2] = w - x1
    else:
        self.bboxes[:, 0] = w - self.bboxes[:, 0]
    self.segments[..., 0] = w - self.segments[..., 0]
    if self.keypoints is not None:
        self.keypoints[..., 0] = w - self.keypoints[..., 0]

flipud(h)

ΠŸΠ΅Ρ€Π΅Π²ΠΎΡ€Π°Ρ‡ΠΈΠ²Π°ΠΉ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΠ²Π°ΡŽΡ‰ΠΈΡ… Π±Π»ΠΎΠΊΠΎΠ², сСгмСнтов ΠΈ ΠΊΠ»ΡŽΡ‡Π΅Π²Ρ‹Ρ… Ρ‚ΠΎΡ‡Π΅ΠΊ ΠΏΠΎ Π²Π΅Ρ€Ρ‚ΠΈΠΊΠ°Π»ΠΈ.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/instance.py
def flipud(self, h):
    """Flips the coordinates of bounding boxes, segments, and keypoints vertically."""
    if self._bboxes.format == "xyxy":
        y1 = self.bboxes[:, 1].copy()
        y2 = self.bboxes[:, 3].copy()
        self.bboxes[:, 1] = h - y2
        self.bboxes[:, 3] = h - y1
    else:
        self.bboxes[:, 1] = h - self.bboxes[:, 1]
    self.segments[..., 1] = h - self.segments[..., 1]
    if self.keypoints is not None:
        self.keypoints[..., 1] = h - self.keypoints[..., 1]

normalize(w, h)

Нормализуй ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Π΅ Ρ€Π°ΠΌΠΊΠΈ, сСгмСнты ΠΈ ΠΊΠ»ΡŽΡ‡Π΅Π²Ρ‹Π΅ Ρ‚ΠΎΡ‡ΠΊΠΈ ΠΊ Ρ€Π°Π·ΠΌΠ΅Ρ€Π°ΠΌ изобраТСния.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/instance.py
def normalize(self, w, h):
    """Normalize bounding boxes, segments, and keypoints to image dimensions."""
    if self.normalized:
        return
    self._bboxes.mul(scale=(1 / w, 1 / h, 1 / w, 1 / h))
    self.segments[..., 0] /= w
    self.segments[..., 1] /= h
    if self.keypoints is not None:
        self.keypoints[..., 0] /= w
        self.keypoints[..., 1] /= h
    self.normalized = True

remove_zero_area_boxes()

Π£Π΄Π°Π»ΠΈ боксы с Π½ΡƒΠ»Π΅Π²ΠΎΠΉ ΠΏΠ»ΠΎΡ‰Π°Π΄ΡŒΡŽ, Ρ‚ΠΎ Π΅ΡΡ‚ΡŒ послС ΠΎΠ±Ρ€Π΅Π·ΠΊΠΈ Π½Π΅ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ боксы ΠΌΠΎΠ³ΡƒΡ‚ ΠΈΠΌΠ΅Ρ‚ΡŒ Π½ΡƒΠ»Π΅Π²ΡƒΡŽ ΡˆΠΈΡ€ΠΈΠ½Ρƒ ΠΈΠ»ΠΈ высоту.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/instance.py
def remove_zero_area_boxes(self):
    """Remove zero-area boxes, i.e. after clipping some boxes may have zero width or height."""
    good = self.bbox_areas > 0
    if not all(good):
        self._bboxes = self._bboxes[good]
        if len(self.segments):
            self.segments = self.segments[good]
        if self.keypoints is not None:
            self.keypoints = self.keypoints[good]
    return good

scale(scale_w, scale_h, bbox_only=False)

Π­Ρ‚ΠΎ ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ ΠΏΠΎΡ…ΠΎΠΆΠ΅ Π½Π° denormalize func, Π½ΠΎ Π±Π΅Π· Π½ΠΎΡ€ΠΌΠ°Π»ΠΈΠ·ΠΎΠ²Π°Π½Π½ΠΎΠ³ΠΎ Π·Π½Π°ΠΊΠ°.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/instance.py
def scale(self, scale_w, scale_h, bbox_only=False):
    """This might be similar with denormalize func but without normalized sign."""
    self._bboxes.mul(scale=(scale_w, scale_h, scale_w, scale_h))
    if bbox_only:
        return
    self.segments[..., 0] *= scale_w
    self.segments[..., 1] *= scale_h
    if self.keypoints is not None:
        self.keypoints[..., 0] *= scale_w
        self.keypoints[..., 1] *= scale_h

update(bboxes, segments=None, keypoints=None)

ΠžΠ±Π½ΠΎΠ²Π»ΡΠ΅Ρ‚ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Π΅ экзСмпляра.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/instance.py
def update(self, bboxes, segments=None, keypoints=None):
    """Updates instance variables."""
    self._bboxes = Bboxes(bboxes, format=self._bboxes.format)
    if segments is not None:
        self.segments = segments
    if keypoints is not None:
        self.keypoints = keypoints



ultralytics.utils.instance._ntuple(n)

Π‘ сайта PyTorch internals.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/instance.py
def _ntuple(n):
    """From PyTorch internals."""

    def parse(x):
        """Parse bounding boxes format between XYWH and LTWH."""
        return x if isinstance(x, abc.Iterable) else tuple(repeat(x, n))

    return parse





Боздано 2023-11-12, ОбновлСно 2024-05-08
Авторы: Burhan-Q (1), Glenn-jocher (3), Laughing-q (1)