μ½˜ν…μΈ λ‘œ κ±΄λ„ˆλ›°κΈ°

μ°Έμ‘° ultralytics/utils/instance.py

μ°Έκ³ 

이 νŒŒμΌμ€ https://github.com/ultralytics/ ultralytics/blob/main/ ultralytics/utils/instance .pyμ—μ„œ 확인할 수 μžˆμŠ΅λ‹ˆλ‹€. 문제λ₯Ό λ°œκ²¬ν•˜λ©΄ ν’€ λ¦¬ν€˜μŠ€νŠΈ (πŸ› οΈ)λ₯Ό μ œμΆœν•˜μ—¬ 문제λ₯Ό ν•΄κ²°ν•˜λ„λ‘ λ„μ™€μ£Όμ„Έμš”. κ°μ‚¬ν•©λ‹ˆλ‹€ πŸ™!



ultralytics.utils.instance.Bboxes

λ°”μš΄λ”© λ°•μŠ€λ₯Ό μ²˜λ¦¬ν•˜λŠ” ν΄λž˜μŠ€μž…λ‹ˆλ‹€.

이 ν΄λž˜μŠ€λŠ” 'xyxy', 'xywh', 'ltwh' λ“± λ‹€μ–‘ν•œ λ°”μš΄λ”© λ°•μŠ€ ν˜•μ‹μ„ μ§€μ›ν•©λ‹ˆλ‹€. λ°”μš΄λ”© λ°•μŠ€ λ°μ΄ν„°λŠ” 널 λ°°μ—΄λ‘œ μ œκ³΅ν•΄μ•Ό ν•©λ‹ˆλ‹€.

속성:

이름 μœ ν˜• μ„€λͺ…
bboxes ndarray

2D 널 배열에 μ €μž₯된 λ°”μš΄λ”© λ°•μŠ€μž…λ‹ˆλ‹€.

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."""
        self.convert("xyxy")
        return (self.bboxes[:, 2] - self.bboxes[:, 0]) * (self.bboxes[:, 3] - self.bboxes[:, 1])

    # 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

μΈλ±μ‹±λœ 경계 μƒμžκ°€ 2차원 행렬을 ν˜•μ„±ν•˜μ§€ μ•ŠλŠ” 경우.

μ°Έκ³ 

λΆ€μšΈ 인덱싱을 μ‚¬μš©ν•  λ•ŒλŠ” λ°”μš΄λ”© λ°•μŠ€ μˆ˜μ™€ λ™μΌν•œ 길이의 길이의 λΆ€μšΈ 배열을 μ œκ³΅ν•΄μ•Ό ν•©λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ 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."""
    self.convert("xyxy")
    return (self.bboxes[:, 2] - self.bboxes[:, 0]) * (self.bboxes[:, 3] - self.bboxes[:, 1])

concatenate(boxes_list, axis=0) classmethod

Bboxes 개체 λͺ©λ‘μ„ ν•˜λ‚˜μ˜ Bboxes 개체둜 μ—°κ²°ν•©λ‹ˆλ‹€.

λ§€κ°œλ³€μˆ˜:

이름 μœ ν˜• μ„€λͺ… κΈ°λ³Έκ°’
boxes_list List[Bboxes]

μ—°κ²°ν•  Bbox 객체의 λͺ©λ‘μž…λ‹ˆλ‹€.

ν•„μˆ˜
axis int

경계 μƒμžλ₯Ό μ—°κ²°ν•  μΆ•μž…λ‹ˆλ‹€. 기본값은 0μž…λ‹ˆλ‹€.

0

λ°˜ν™˜ν•©λ‹ˆλ‹€:

이름 μœ ν˜• μ„€λͺ…
Bboxes Bboxes

μ—°κ²°λœ 경계 μƒμžλ₯Ό ν¬ν•¨ν•˜λŠ” μƒˆλ‘œμš΄ Bboxes κ°μ²΄μž…λ‹ˆλ‹€.

μ°Έκ³ 

μž…λ ₯은 Bbox 개체의 λͺ©λ‘ λ˜λŠ” νŠœν”Œμ΄μ–΄μ•Ό ν•©λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ 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

λͺ¨μ–‘ [N, 17, 3]을 가진 ν‚€ν¬μΈνŠΈ(x, y, visible). 기본값은 μ—†μŒμž…λ‹ˆλ‹€.

normalized bool

경계 μƒμž μ’Œν‘œμ˜ μ •κ·œν™” μ—¬λΆ€λ₯Ό λ‚˜νƒ€λ‚΄λŠ” ν”Œλž˜κ·Έμž…λ‹ˆλ‹€.

segments ndarray

λ¦¬μƒ˜ν”Œλ§ ν›„ λͺ¨μ–‘ [N, 1000, 2]의 μ„Έκ·Έλ¨ΌνŠΈ λ°°μ—΄μž…λ‹ˆλ‹€.

λ§€κ°œλ³€μˆ˜:

이름 μœ ν˜• μ„€λͺ… κΈ°λ³Έκ°’
bboxes ndarray

λͺ¨μ–‘이 [N, 4]인 λ°”μš΄λ”© λ°•μŠ€μ˜ λ°°μ—΄μž…λ‹ˆλ‹€.

ν•„μˆ˜
segments list | ndarray

객체 μ„Έκ·Έλ¨ΌνŠΈμ˜ λͺ©λ‘ λ˜λŠ” λ°°μ—΄μž…λ‹ˆλ‹€. 기본값은 μ—†μŒμž…λ‹ˆλ‹€.

None
keypoints ndarray

λͺ¨μ–‘이 [N, 17, 3]인 ν‚€ν¬μΈνŠΈ λ°°μ—΄μž…λ‹ˆλ‹€. 기본값은 μ—†μŒμž…λ‹ˆλ‹€.

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.

        This removes them.
        """
        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

μ„ νƒν•œ 경계 μƒμžλ₯Ό ν¬ν•¨ν•˜λŠ” μƒˆ μΈμŠ€ν„΄μŠ€ κ°μ²΄μž…λ‹ˆλ‹€, μ„Έκ·Έλ¨ΌνŠΈ, ν‚€ν¬μΈνŠΈκ°€ μžˆλŠ” 경우.

μ°Έκ³ 

λΆ€μšΈ 인덱싱을 μ‚¬μš©ν•  λ•ŒλŠ” μΈμŠ€ν„΄μŠ€ μˆ˜μ™€ λ™μΌν•œ 길이의 λΆ€μšΈ 배열을 길이의 λΆ€μšΈ 배열을 μ œκ³΅ν•΄μ•Ό ν•©λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ 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

λͺ¨μ–‘이 [N, 4]인 bμƒμžμž…λ‹ˆλ‹€.

ν•„μˆ˜
segments list | ndarray

μ„Έκ·Έλ¨ΌνŠΈ.

None
keypoints ndarray

λͺ¨μ–‘ [N, 17, 3]을 가진 ν‚€ν¬μΈνŠΈ(x, y, visible).

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_list List[Instances]

μ—°κ²°ν•  μΈμŠ€ν„΄μŠ€ 객체의 λͺ©λ‘μž…λ‹ˆλ‹€.

ν•„μˆ˜
axis int

배열이 연결될 μΆ•μž…λ‹ˆλ‹€. 기본값은 0μž…λ‹ˆλ‹€.

0

λ°˜ν™˜ν•©λ‹ˆλ‹€:

이름 μœ ν˜• μ„€λͺ…
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()

μ˜μ—­μ΄ 0인 μƒμžλ₯Ό μ œκ±°ν•©λ‹ˆλ‹€. 즉, 클리핑 ν›„ 일뢀 μƒμžμ˜ λ„ˆλΉ„λ‚˜ 높이가 0이 될 수 μžˆμŠ΅λ‹ˆλ‹€.

μ΄λ ‡κ²Œ ν•˜λ©΄ μ œκ±°λ©λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ 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.

    This removes them.
    """
    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)

μ΄λŠ” μ •κ·œν™” ν•¨μˆ˜μ™€ λΉ„μŠ·ν•˜μ§€λ§Œ μ •κ·œν™” λΆ€ν˜Έκ°€ μ—†λŠ” κ²½μš°μž…λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ 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 λ‚΄λΆ€μ—μ„œ κ°€μ Έμ˜΄.

의 μ†ŒμŠ€ μ½”λ“œ 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 생성, 2023-11-25 μ—…λ°μ΄νŠΈλ¨
μž‘μ„±μž: glenn-jocher (3), Laughing-q (1)