Saltar a contenido

Model Module

ImageBoundingBoxes

Class that represents the detected objects bounding boxes from an object detection model on an image.

Source code in devices\raspberry_pi_5\src\model\__init__.py
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
class ImageBoundingBoxes:
    """
    Class that represents the detected objects bounding boxes from an object detection model on an image.
    """

    def __init__(
        self,
        xywhn: Optional[List] = None,
        xyxy: Optional[List] = None,
        xywh: Optional[List] = None,
        xyxyn: Optional[List] = None,
        cls: Optional[List] = None,
        conf: Optional[List] = None,
        n: Optional[int] = None
    ):
        """
        Initialize the ImageBoundingBoxes instance with bounding box coordinates, classes, and confidences.

        Args:
            xywh (Optional[List]): Bounding box coordinates in the format (x_center, y_center, width, height).
            xyxy (Optional[List]): Bounding box coordinates in the format (x1, y1, x2, y2).
            xywhn (Optional[List]): Normalized bounding box coordinates in the format (x_center, y_center, width, height).
            xyxyn (Optional[List]): Normalized bounding box coordinates in the format (x1, y1, x2, y2).
            cls (Optional[List]): Class indices for each detected object.
            conf (Optional[List]): Confidence scores for each detected object.
            n (Optional[int]): Number of detected objects.
        """
        self.__xywh = xywh
        self.__xywhn = xywhn
        self.__xyxy = xyxy
        self.__xyxyn = xyxyn
        self.__cls = cls
        self.__conf = conf
        self.__n = n

    def __str__(self) -> str:
        """
        String representation of the objects detected in the image.

        Returns:
            str: A formatted string containing the bounding boxes, classes, and confidences.
        """
        bounding_boxes = []
        for i in range(self.__n):
            bounding_box_attributes = [
                f"Class: {int(self.__cls[i])}",
                f"Confidence: {self.__conf[i]}",
                f"(X0, Y0): ({self.__xyxyn[i][0]}, {self.__xyxyn[i][1]})",
                f"(X1, Y1): ({self.__xyxyn[i][2]}, {self.__xyxyn[i][3]})",
            ]
            bounding_boxes.append(
                f"Box {i + 1}:\n\t" + "\n\t".join(bounding_box_attributes)
            )
        return "\n".join(bounding_boxes)

    @staticmethod
    def from_pt_cpu_boxes(boxes) -> 'ImageBoundingBoxes':
        """
        Initialize a new ImageBoundingBoxes instances from a PyTorch CPU tensor.

        Args:
            boxes (torch.Tensor): The bounding boxes' tensor.

        Returns:
            ImageBoundingBoxes: An instance containing the bounding boxes, classes, and confidences.
        """
        return ImageBoundingBoxes(
            xywh=boxes.xywh.cpu().numpy(),
            xywhn=boxes.xywhn.cpu().numpy(),
            xyxy=boxes.xyxy.cpu().numpy(),
            xyxyn=boxes.xyxyn.cpu().numpy(),
            cls=boxes.cls.cpu().numpy(),
            conf=boxes.conf.cpu().numpy(),
            n=len(boxes.cls)
        )

    @staticmethod
    def from_pt_cpu(input_data: List) -> 'ImageBoundingBoxes':
        """
        Extract detections from the input data.

        Args:
            input_data (List): Raw detections from the model.

        Returns:
            ImageBoundingBoxes: An instance containing the bounding boxes, classes, and confidences.
        """
        return ImageBoundingBoxes.from_pt_cpu_boxes(input_data[0].boxes)

    @staticmethod
    def from_hailo(
        input_data: List,
        threshold: float = 0.5
    ) -> 'ImageBoundingBoxes':
        """
        Extract detections from the input data.

        Args:
            input_data (List): Raw detections from the model.
            threshold (float): Score threshold for filtering detections. Defaults to 0.5.

        Returns:
            ImageBoundingBoxes: An instance containing the bounding boxes, classes, and confidences.
        """
        boxes, scores, classes = [], [], []
        num_detections = 0

        for i, detection in enumerate(input_data):
            if len(detection) == 0:
                continue

            for det in detection:
                bbox, score = det[:4], det[4]

                if score >= threshold:
                    boxes.append(bbox)
                    scores.append(score)
                    classes.append(i)
                    num_detections += 1

        return ImageBoundingBoxes(
            n=num_detections, xyxy=boxes, cls=classes,
            conf=scores
        )

    def get_number_of_objects(self) -> int:
        """
        Get the number of detected objects.

        Returns:
            int: The number of detected objects.
        """
        return self.__n

    def get_xyxy(self) -> List:
        """
        Get the bounding box coordinates in the format (x1, y1, x2, y2).

        Returns:
            List: A list of bounding box coordinates in the format (x1, y1, x2, y2).
        """
        return self.__xyxy

    def get_xywh(self) -> List:
        """
        Get the bounding box coordinates in the format (x_center, y_center, width, height).

        Returns:
            List: A list of bounding box coordinates in the format (x_center, y_center, width, height).
        """
        return self.__xywh

    def get_xywhn(self) -> List:
        """
        Get the bounding box coordinates in the format (x_center, y_center, width, height) normalized.

        Returns:
            List: A list of bounding box coordinates in the format (x_center, y_center, width, height) normalized.
        """
        return self.__xywhn

    def get_xyxyn(self) -> List:
        """
        Get the bounding box coordinates in the format (x1, y1, x2, y2) normalized.

        Returns:
            List: A list of bounding box coordinates in the format (x1, y1, x2, y2) normalized.
        """
        return self.__xyxyn

    def get_classes(self) -> List:
        """
        Get the classes of the detected objects.

        Returns:
            List: A list of class indices for each detected object.
        """
        return self.__cls

    def get_confidences(self) -> List:
        """
        Get the confidence of the detected objects.

        Returns:
            List: A list of confidence scores for each detected object.
        """
        return self.__conf

    def get_boxes(self) -> tuple:
        """
        Get the bounding box coordinates and class of the detected objects.

        Returns:
            tuple: A tuple containing the class, confidence, and bounding box coordinates in xyxy format.
        """
        return self.__cls, self.__conf, self.__xyxy

__init__(xywhn=None, xyxy=None, xywh=None, xyxyn=None, cls=None, conf=None, n=None)

Initialize the ImageBoundingBoxes instance with bounding box coordinates, classes, and confidences.

Parameters:

Name Type Description Default
xywh Optional[List]

Bounding box coordinates in the format (x_center, y_center, width, height).

None
xyxy Optional[List]

Bounding box coordinates in the format (x1, y1, x2, y2).

None
xywhn Optional[List]

Normalized bounding box coordinates in the format (x_center, y_center, width, height).

None
xyxyn Optional[List]

Normalized bounding box coordinates in the format (x1, y1, x2, y2).

None
cls Optional[List]

Class indices for each detected object.

None
conf Optional[List]

Confidence scores for each detected object.

None
n Optional[int]

Number of detected objects.

None
Source code in devices\raspberry_pi_5\src\model\__init__.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def __init__(
    self,
    xywhn: Optional[List] = None,
    xyxy: Optional[List] = None,
    xywh: Optional[List] = None,
    xyxyn: Optional[List] = None,
    cls: Optional[List] = None,
    conf: Optional[List] = None,
    n: Optional[int] = None
):
    """
    Initialize the ImageBoundingBoxes instance with bounding box coordinates, classes, and confidences.

    Args:
        xywh (Optional[List]): Bounding box coordinates in the format (x_center, y_center, width, height).
        xyxy (Optional[List]): Bounding box coordinates in the format (x1, y1, x2, y2).
        xywhn (Optional[List]): Normalized bounding box coordinates in the format (x_center, y_center, width, height).
        xyxyn (Optional[List]): Normalized bounding box coordinates in the format (x1, y1, x2, y2).
        cls (Optional[List]): Class indices for each detected object.
        conf (Optional[List]): Confidence scores for each detected object.
        n (Optional[int]): Number of detected objects.
    """
    self.__xywh = xywh
    self.__xywhn = xywhn
    self.__xyxy = xyxy
    self.__xyxyn = xyxyn
    self.__cls = cls
    self.__conf = conf
    self.__n = n

__str__()

String representation of the objects detected in the image.

Returns:

Name Type Description
str str

A formatted string containing the bounding boxes, classes, and confidences.

Source code in devices\raspberry_pi_5\src\model\__init__.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
def __str__(self) -> str:
    """
    String representation of the objects detected in the image.

    Returns:
        str: A formatted string containing the bounding boxes, classes, and confidences.
    """
    bounding_boxes = []
    for i in range(self.__n):
        bounding_box_attributes = [
            f"Class: {int(self.__cls[i])}",
            f"Confidence: {self.__conf[i]}",
            f"(X0, Y0): ({self.__xyxyn[i][0]}, {self.__xyxyn[i][1]})",
            f"(X1, Y1): ({self.__xyxyn[i][2]}, {self.__xyxyn[i][3]})",
        ]
        bounding_boxes.append(
            f"Box {i + 1}:\n\t" + "\n\t".join(bounding_box_attributes)
        )
    return "\n".join(bounding_boxes)

from_hailo(input_data, threshold=0.5) staticmethod

Extract detections from the input data.

Parameters:

Name Type Description Default
input_data List

Raw detections from the model.

required
threshold float

Score threshold for filtering detections. Defaults to 0.5.

0.5

Returns:

Name Type Description
ImageBoundingBoxes ImageBoundingBoxes

An instance containing the bounding boxes, classes, and confidences.

Source code in devices\raspberry_pi_5\src\model\__init__.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
@staticmethod
def from_hailo(
    input_data: List,
    threshold: float = 0.5
) -> 'ImageBoundingBoxes':
    """
    Extract detections from the input data.

    Args:
        input_data (List): Raw detections from the model.
        threshold (float): Score threshold for filtering detections. Defaults to 0.5.

    Returns:
        ImageBoundingBoxes: An instance containing the bounding boxes, classes, and confidences.
    """
    boxes, scores, classes = [], [], []
    num_detections = 0

    for i, detection in enumerate(input_data):
        if len(detection) == 0:
            continue

        for det in detection:
            bbox, score = det[:4], det[4]

            if score >= threshold:
                boxes.append(bbox)
                scores.append(score)
                classes.append(i)
                num_detections += 1

    return ImageBoundingBoxes(
        n=num_detections, xyxy=boxes, cls=classes,
        conf=scores
    )

from_pt_cpu(input_data) staticmethod

Extract detections from the input data.

Parameters:

Name Type Description Default
input_data List

Raw detections from the model.

required

Returns:

Name Type Description
ImageBoundingBoxes ImageBoundingBoxes

An instance containing the bounding boxes, classes, and confidences.

Source code in devices\raspberry_pi_5\src\model\__init__.py
80
81
82
83
84
85
86
87
88
89
90
91
@staticmethod
def from_pt_cpu(input_data: List) -> 'ImageBoundingBoxes':
    """
    Extract detections from the input data.

    Args:
        input_data (List): Raw detections from the model.

    Returns:
        ImageBoundingBoxes: An instance containing the bounding boxes, classes, and confidences.
    """
    return ImageBoundingBoxes.from_pt_cpu_boxes(input_data[0].boxes)

from_pt_cpu_boxes(boxes) staticmethod

Initialize a new ImageBoundingBoxes instances from a PyTorch CPU tensor.

Parameters:

Name Type Description Default
boxes Tensor

The bounding boxes' tensor.

required

Returns:

Name Type Description
ImageBoundingBoxes ImageBoundingBoxes

An instance containing the bounding boxes, classes, and confidences.

Source code in devices\raspberry_pi_5\src\model\__init__.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
@staticmethod
def from_pt_cpu_boxes(boxes) -> 'ImageBoundingBoxes':
    """
    Initialize a new ImageBoundingBoxes instances from a PyTorch CPU tensor.

    Args:
        boxes (torch.Tensor): The bounding boxes' tensor.

    Returns:
        ImageBoundingBoxes: An instance containing the bounding boxes, classes, and confidences.
    """
    return ImageBoundingBoxes(
        xywh=boxes.xywh.cpu().numpy(),
        xywhn=boxes.xywhn.cpu().numpy(),
        xyxy=boxes.xyxy.cpu().numpy(),
        xyxyn=boxes.xyxyn.cpu().numpy(),
        cls=boxes.cls.cpu().numpy(),
        conf=boxes.conf.cpu().numpy(),
        n=len(boxes.cls)
    )

get_boxes()

Get the bounding box coordinates and class of the detected objects.

Returns:

Name Type Description
tuple tuple

A tuple containing the class, confidence, and bounding box coordinates in xyxy format.

Source code in devices\raspberry_pi_5\src\model\__init__.py
192
193
194
195
196
197
198
199
def get_boxes(self) -> tuple:
    """
    Get the bounding box coordinates and class of the detected objects.

    Returns:
        tuple: A tuple containing the class, confidence, and bounding box coordinates in xyxy format.
    """
    return self.__cls, self.__conf, self.__xyxy

get_classes()

Get the classes of the detected objects.

Returns:

Name Type Description
List List

A list of class indices for each detected object.

Source code in devices\raspberry_pi_5\src\model\__init__.py
174
175
176
177
178
179
180
181
def get_classes(self) -> List:
    """
    Get the classes of the detected objects.

    Returns:
        List: A list of class indices for each detected object.
    """
    return self.__cls

get_confidences()

Get the confidence of the detected objects.

Returns:

Name Type Description
List List

A list of confidence scores for each detected object.

Source code in devices\raspberry_pi_5\src\model\__init__.py
183
184
185
186
187
188
189
190
def get_confidences(self) -> List:
    """
    Get the confidence of the detected objects.

    Returns:
        List: A list of confidence scores for each detected object.
    """
    return self.__conf

get_number_of_objects()

Get the number of detected objects.

Returns:

Name Type Description
int int

The number of detected objects.

Source code in devices\raspberry_pi_5\src\model\__init__.py
129
130
131
132
133
134
135
136
def get_number_of_objects(self) -> int:
    """
    Get the number of detected objects.

    Returns:
        int: The number of detected objects.
    """
    return self.__n

get_xywh()

Get the bounding box coordinates in the format (x_center, y_center, width, height).

Returns:

Name Type Description
List List

A list of bounding box coordinates in the format (x_center, y_center, width, height).

Source code in devices\raspberry_pi_5\src\model\__init__.py
147
148
149
150
151
152
153
154
def get_xywh(self) -> List:
    """
    Get the bounding box coordinates in the format (x_center, y_center, width, height).

    Returns:
        List: A list of bounding box coordinates in the format (x_center, y_center, width, height).
    """
    return self.__xywh

get_xywhn()

Get the bounding box coordinates in the format (x_center, y_center, width, height) normalized.

Returns:

Name Type Description
List List

A list of bounding box coordinates in the format (x_center, y_center, width, height) normalized.

Source code in devices\raspberry_pi_5\src\model\__init__.py
156
157
158
159
160
161
162
163
def get_xywhn(self) -> List:
    """
    Get the bounding box coordinates in the format (x_center, y_center, width, height) normalized.

    Returns:
        List: A list of bounding box coordinates in the format (x_center, y_center, width, height) normalized.
    """
    return self.__xywhn

get_xyxy()

Get the bounding box coordinates in the format (x1, y1, x2, y2).

Returns:

Name Type Description
List List

A list of bounding box coordinates in the format (x1, y1, x2, y2).

Source code in devices\raspberry_pi_5\src\model\__init__.py
138
139
140
141
142
143
144
145
def get_xyxy(self) -> List:
    """
    Get the bounding box coordinates in the format (x1, y1, x2, y2).

    Returns:
        List: A list of bounding box coordinates in the format (x1, y1, x2, y2).
    """
    return self.__xyxy

get_xyxyn()

Get the bounding box coordinates in the format (x1, y1, x2, y2) normalized.

Returns:

Name Type Description
List List

A list of bounding box coordinates in the format (x1, y1, x2, y2) normalized.

Source code in devices\raspberry_pi_5\src\model\__init__.py
165
166
167
168
169
170
171
172
def get_xyxyn(self) -> List:
    """
    Get the bounding box coordinates in the format (x1, y1, x2, y2) normalized.

    Returns:
        List: A list of bounding box coordinates in the format (x1, y1, x2, y2) normalized.
    """
    return self.__xyxyn