class documentation

class Detection(BaseModelExtraForbid):

View In Hierarchy

Detection record containing annotations and metadata for one object.

It describes a single detected object in an image and can contain various types of annotations and metadata as well as nested sub-detections for hierarchical annotations.

When scale_to_boxes is enabled, keypoints and segmentation data are interpreted relative to the bounding box and rescaled to image-normalized coordinates.

Example

>>> detection = Detection(
...     class_name="person",
...     instance_id=1,
...     metadata={
...         "category": Category("adult"),
...     },
...     boundingbox={
...         "x": 0.1,
...         "y": 0.2,
...         "w": 0.3,
...         "h": 0.4,
...     },
...     instance_segmentation={
...         "mask": np.array([[0, 1], [1, 0]]),
...     },
...     sub_detections={
...         "face": {
...             "class_name": "face",
...             "boundingbox": {
...                 "x": 0.2,
...                 "y": 0.3,
...                 "w": 0.1,
...                 "h": 0.1,
...             },
...             "keypoints": {
...                 "keypoints": [
...                     (0.25, 0.35, 2),  # left eye
...                     (0.3, 0.35, 2),  # right eye
...                 ],
...             },
...             "metadata": {
...                 "expression": Category("happy"),
...                 "eye_color": Category("blue"),
...             },
...         },
...     },
... )
Method get_task_types Get all the task type associated with this detection.
Instance Variable array Optional array annotation.
Instance Variable boundingbox Optional bounding box annotation.
Instance Variable class_name optional class name for the detection. Input data may use the "class" alias.
Instance Variable instance_id Instance identifier. If not provided, the instance IDs will correspond to the order in which the detections were added to the dataset. Note that this might lead to incorrect pairing of instance annotations if individual detection types are added separately and in an inconsistent order across records:...
Instance Variable instance_segmentation Optional instance segmentation annotation.
Instance Variable keypoints Optional keypoint annotation.
Instance Variable metadata Metadata values keyed by metadata name.
Instance Variable scale_to_boxes Whether annotation coordinates should be rescaled from bounding-box-relative coordinates.
Instance Variable segmentation Optional semantic segmentation annotation.
Instance Variable sub_detections Nested detections keyed by sub-detection name.
Static Method _check_valid_identifier Undocumented
Method _rescale_values Undocumented
Method _validate_names Undocumented
def get_task_types(self) -> set[str]:

Get all the task type associated with this detection.

Example

>>> detection = Detection(
...     class_name="cat",
...     boundingbox=BBoxAnnotation(x=0.1, y=0.2, w=0.3, h=0.4),
...     metadata={"color": "black"},
... )
>>> sorted(detection.get_task_types())
['boundingbox', 'classification', 'metadata/color']
Returns
set[str]Annotation task types and metadata keys.
array: ArrayAnnotation | None =

Optional array annotation.

boundingbox: BBoxAnnotation | None =

Optional bounding box annotation.

class_name: str | None =

optional class name for the detection. Input data may use the "class" alias.

instance_id: int =

Instance identifier. If not provided, the instance IDs will correspond to the order in which the detections were added to the dataset. Note that this might lead to incorrect pairing of instance annotations if individual detection types are added separately and in an inconsistent order across records:

# Without specifying `instance_id`, the
# bounding box and keypoint annotation will
# not be correctly paired as they are added in separate
# detections and in a different order.
def generator():
    yield {
        "file": ...,
        "annotation": {"boundingbox": bbox1},
    }
    yield {
        "file": ...,
        "annotation": {"keypoints": kpts2},
    }
    yield {
        "file": ...,
        "annotation": {"boundingbox": bbox2},
    }
    yield {
        "file": ...,
        "annotation": {"keypoints": kpts1},
    }

It is recommended to provide instance IDs if possible and to avoid generating annotations individually in separate detections:

# This is the correct way
def generator():
    yield {
        "file": ...,
        "annotation": {
            "instance_id": 1,
            "boundingbox": bbox1
            "keypoints": kpts1,
        },
    }
    yield {
        "file": ...,
        "annotation": {
            "instance_id": 2,
            "boundingbox": bbox2
            "keypoints": kpts2,
        },
    }
instance_segmentation: InstanceSegmentationAnnotation | None =

Optional instance segmentation annotation.

keypoints: KeypointAnnotation | None =

Optional keypoint annotation.

metadata: dict[str, int | float | str | Category] =

Metadata values keyed by metadata name.

scale_to_boxes: bool =

Whether annotation coordinates should be rescaled from bounding-box-relative coordinates.

segmentation: SegmentationAnnotation | None =

Optional semantic segmentation annotation.

sub_detections: dict[str, Detection] =

Nested detections keyed by sub-detection name.

@staticmethod
def _check_valid_identifier(name: str, *, label: str):

Undocumented

@model_validator(mode='after')
def _rescale_values(self) -> Self:

Undocumented

@model_validator(mode='after')
def _validate_names(self) -> Self:

Undocumented