์ฝ˜ํ…์ธ ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

์ฐธ์กฐ 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."""
        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

์ธ๋ฑ์‹ฑ๋œ ๋ฐ”์šด๋”ฉ ๋ฐ•์Šค๊ฐ€ 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."""
    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]

์—ฐ๊ฒฐํ•  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, ๋ณด์ด๋Š”). ๊ธฐ๋ณธ๊ฐ’์€ ์—†์Œ์ž…๋‹ˆ๋‹ค.

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."""
        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, ๋ณด์ด๋Š”).

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

์—ฐ๊ฒฐ๋œ ๊ฒฝ๊ณ„ ์ƒ์ž๋ฅผ ํฌํ•จํ•˜๋Š” ์ƒˆ ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค, ์„ธ๊ทธ๋จผํŠธ, ํ‚คํฌ์ธํŠธ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ํ‚คํฌ์ธํŠธ๊ฐ€ ํฌํ•จ๋œ ์ƒˆ ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.

์ฐธ๊ณ 

๊ทธ๋ฆฌ๊ณ  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."""
    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, ์—…๋ฐ์ดํŠธ 2024-05-08
์ž‘์„ฑ์ž: Burhan-Q (1), glenn-jocher (3), Laughing-q (1)