sinapsis-data-visualization 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- __init__.py +0 -0
- sinapsis_data_visualization/__init__.py +0 -0
- sinapsis_data_visualization/helpers/__init__.py +0 -0
- sinapsis_data_visualization/helpers/annotation_drawer_tools.py +252 -0
- sinapsis_data_visualization/helpers/annotation_drawer_types.py +56 -0
- sinapsis_data_visualization/helpers/color_utils.py +108 -0
- sinapsis_data_visualization/helpers/detection_utils.py +124 -0
- sinapsis_data_visualization/helpers/plot_distributions.py +130 -0
- sinapsis_data_visualization/helpers/scikit_pca_analysis.py +51 -0
- sinapsis_data_visualization/templates/__init__.py +25 -0
- sinapsis_data_visualization/templates/base_annotation_drawer.py +103 -0
- sinapsis_data_visualization/templates/bbox_drawer.py +105 -0
- sinapsis_data_visualization/templates/data_distribution_visualization_template.py +245 -0
- sinapsis_data_visualization/templates/key_points_drawer.py +122 -0
- sinapsis_data_visualization/templates/label_drawer.py +292 -0
- sinapsis_data_visualization/templates/oriented_bbox_drawer.py +121 -0
- sinapsis_data_visualization/templates/segmentation_mask_drawer.py +137 -0
- sinapsis_data_visualization-0.1.0.dist-info/METADATA +674 -0
- sinapsis_data_visualization-0.1.0.dist-info/RECORD +21 -0
- sinapsis_data_visualization-0.1.0.dist-info/WHEEL +5 -0
- sinapsis_data_visualization-0.1.0.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from typing import Literal
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
from sinapsis_core.data_containers.annotations import BoundingBox, ImageAnnotations
|
|
7
|
+
|
|
8
|
+
from sinapsis_data_visualization.helpers.annotation_drawer_tools import (
|
|
9
|
+
draw_annotation_rectangle,
|
|
10
|
+
draw_extra_labels,
|
|
11
|
+
draw_text,
|
|
12
|
+
get_dynamic_text_properties,
|
|
13
|
+
get_extra_ann_labels,
|
|
14
|
+
get_numeric_str,
|
|
15
|
+
get_text_coordinates,
|
|
16
|
+
get_text_size,
|
|
17
|
+
)
|
|
18
|
+
from sinapsis_data_visualization.helpers.annotation_drawer_types import (
|
|
19
|
+
TextInstanceProperties,
|
|
20
|
+
TextStyle,
|
|
21
|
+
)
|
|
22
|
+
from sinapsis_data_visualization.helpers.color_utils import (
|
|
23
|
+
RGB_TYPE,
|
|
24
|
+
darken_or_lighten_color,
|
|
25
|
+
)
|
|
26
|
+
from sinapsis_data_visualization.templates.base_annotation_drawer import (
|
|
27
|
+
BaseAnnotationDrawer,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class LabelDrawer(BaseAnnotationDrawer):
|
|
32
|
+
"""
|
|
33
|
+
Template to insert label annotations in images.
|
|
34
|
+
|
|
35
|
+
The template inherits the functionality from the BaseAnnotationDrawer
|
|
36
|
+
and allows to draw classification labels and labels associated with a
|
|
37
|
+
given annotation.
|
|
38
|
+
|
|
39
|
+
Usage example:
|
|
40
|
+
|
|
41
|
+
agent:
|
|
42
|
+
name: my_test_agent
|
|
43
|
+
templates:
|
|
44
|
+
- template_name: InputTemplate
|
|
45
|
+
class_name: InputTemplate
|
|
46
|
+
attributes: {}
|
|
47
|
+
- template_name: LabelDrawer
|
|
48
|
+
class_name: LabelDrawer
|
|
49
|
+
template_input: InputTemplate
|
|
50
|
+
attributes:
|
|
51
|
+
overwrite: false
|
|
52
|
+
randomized_color: true
|
|
53
|
+
draw_confidence: true
|
|
54
|
+
draw_extra_labels: true
|
|
55
|
+
text_style:
|
|
56
|
+
font: 0
|
|
57
|
+
font_scale: 0.5
|
|
58
|
+
thickness: 2
|
|
59
|
+
draw_classification_label: false
|
|
60
|
+
classification_label_position: top_right
|
|
61
|
+
text_box_to_border_offset: 0.01
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
"""
|
|
65
|
+
|
|
66
|
+
class AttributesBaseModel(BaseAnnotationDrawer.AttributesBaseModel):
|
|
67
|
+
"""
|
|
68
|
+
Attributes for Label Annotation Drawer
|
|
69
|
+
|
|
70
|
+
Args:
|
|
71
|
+
draw_confidence (bool): Flag to determine confidence value is
|
|
72
|
+
drawn. Defaults to True
|
|
73
|
+
draw_extra_labels (bool): Flag to determine if extra labels are drawn.
|
|
74
|
+
Defaults to True
|
|
75
|
+
text_style (TextStyle) = {font=0, font_scale=0.5, thickness=2}:
|
|
76
|
+
Dictionary of font properties used to draw label text boxes.
|
|
77
|
+
draw_classification_label (bool): Flag to determine if a classification label
|
|
78
|
+
is drawn on the image. Defaults to False.
|
|
79
|
+
classification_label_position (str): Position of the text box for the
|
|
80
|
+
classification label. Defaults to "top_right".
|
|
81
|
+
text_box_to_border_offset (float): Weight to control offset of text box to image border.
|
|
82
|
+
"""
|
|
83
|
+
|
|
84
|
+
draw_confidence: bool = True
|
|
85
|
+
draw_extra_labels: bool = True
|
|
86
|
+
text_style: TextStyle = TextStyle()
|
|
87
|
+
draw_classification_label: bool = False
|
|
88
|
+
classification_label_position: Literal["top_left", "top_right"] = "top_right"
|
|
89
|
+
text_box_to_border_offset: float = 0.01
|
|
90
|
+
|
|
91
|
+
def set_drawing_strategy(self) -> None:
|
|
92
|
+
"""Appends the annotation to be drawn (i.e., label)
|
|
93
|
+
to the list of tasks in the execute method"""
|
|
94
|
+
self.drawing_strategies.append(self.draw_label_strategy)
|
|
95
|
+
|
|
96
|
+
def _parse_extra_labels(
|
|
97
|
+
self,
|
|
98
|
+
annotation: ImageAnnotations,
|
|
99
|
+
annotation_str_repr: str,
|
|
100
|
+
spacing: int = 5,
|
|
101
|
+
) -> tuple[list[int], list[str], int, int]:
|
|
102
|
+
"""
|
|
103
|
+
Parses extra annotations (if any) to be drawn below the primary label.
|
|
104
|
+
|
|
105
|
+
Args:
|
|
106
|
+
annotation (ImageAnnotations): The annotation containing extra labels.
|
|
107
|
+
annotation_str_repr (str): The string representation of the main annotation label.
|
|
108
|
+
spacing (int, optional): Spacing between extra labels.
|
|
109
|
+
|
|
110
|
+
Returns:
|
|
111
|
+
tuple:
|
|
112
|
+
- text_y_offsets (list[int]): Y offsets for each extra label.
|
|
113
|
+
- extra_str_repr (list[str]): The string representations of extra labels.
|
|
114
|
+
- text_w (int): The maximum width of the labels.
|
|
115
|
+
- text_h (int): The height of all labels combined.
|
|
116
|
+
"""
|
|
117
|
+
|
|
118
|
+
text_w, text_h = get_text_size(annotation_str_repr, self.attributes.text_style)
|
|
119
|
+
text_y_offsets, extra_str_repr = [], []
|
|
120
|
+
extra_text_h = text_h
|
|
121
|
+
if self.attributes.draw_extra_labels and annotation.extra_labels is not None:
|
|
122
|
+
text_y_offsets, extra_str_repr, extra_text_h, text_w = get_extra_ann_labels(
|
|
123
|
+
annotation, text_w, text_h, self.attributes.text_style, spacing
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
return text_y_offsets, extra_str_repr, text_w, extra_text_h
|
|
127
|
+
|
|
128
|
+
def get_label_ann_str(self, annotation: ImageAnnotations) -> str:
|
|
129
|
+
"""
|
|
130
|
+
Returns the annotation as string, appending confidence score if needed.
|
|
131
|
+
|
|
132
|
+
Args:
|
|
133
|
+
annotation (ImageAnnotations): Annotation object in the ImagePacket to
|
|
134
|
+
extract the label from
|
|
135
|
+
|
|
136
|
+
"""
|
|
137
|
+
if not annotation.label_str:
|
|
138
|
+
return ""
|
|
139
|
+
annotation_str_repr = f"{annotation.label_str}"
|
|
140
|
+
if self.attributes.draw_confidence and annotation.confidence_score:
|
|
141
|
+
annotation_str_repr = get_numeric_str(annotation_str_repr, annotation.confidence_score)
|
|
142
|
+
return annotation_str_repr
|
|
143
|
+
|
|
144
|
+
def get_label_properties(
|
|
145
|
+
self,
|
|
146
|
+
shape: tuple,
|
|
147
|
+
annotation: ImageAnnotations,
|
|
148
|
+
class_label: bool = False,
|
|
149
|
+
spacing: int = 5,
|
|
150
|
+
) -> tuple:
|
|
151
|
+
"""
|
|
152
|
+
Calculates the label properties needed for to draw annotations on an image.
|
|
153
|
+
|
|
154
|
+
Args:
|
|
155
|
+
shape (tuple): The dimensions of the image as (height, width).
|
|
156
|
+
annotation (ImageAnnotations): The annotation data containing
|
|
157
|
+
the label and additional information.
|
|
158
|
+
class_label (bool, optional): Specifies whether the label is
|
|
159
|
+
for a classification. If 'True', the label is positioned
|
|
160
|
+
globally within the image; otherwise, it is placed near the
|
|
161
|
+
bounding box.
|
|
162
|
+
|
|
163
|
+
spacing (int, optional): The spacing between the primary label
|
|
164
|
+
and additional labels.
|
|
165
|
+
|
|
166
|
+
Returns:
|
|
167
|
+
tuple: A tuple containing:
|
|
168
|
+
- 'BoundingBox': The bounding box for the label text.
|
|
169
|
+
- 'str': The main annotation string representation.
|
|
170
|
+
- 'list[str]': Additional label strings, if any.
|
|
171
|
+
- 'list[int]': Vertical offsets for additional labels.
|
|
172
|
+
- 'int': Width of the text bounding box.
|
|
173
|
+
- 'int': Height of the text bounding box.
|
|
174
|
+
"""
|
|
175
|
+
annotation_str_repr = self.get_label_ann_str(annotation)
|
|
176
|
+
text_y_offsets, extra_str_repr, text_w, text_h = self._parse_extra_labels(
|
|
177
|
+
annotation, annotation_str_repr, spacing
|
|
178
|
+
)
|
|
179
|
+
_, top_text_h = get_text_size(annotation_str_repr, self.attributes.text_style)
|
|
180
|
+
text_coords = top_text_h, text_w
|
|
181
|
+
if class_label:
|
|
182
|
+
x, y = get_text_coordinates(
|
|
183
|
+
shape,
|
|
184
|
+
text_coords,
|
|
185
|
+
self.attributes.text_box_to_border_offset,
|
|
186
|
+
self.attributes.classification_label_position,
|
|
187
|
+
)
|
|
188
|
+
bbox = BoundingBox(x, y + text_h - top_text_h, text_w, text_h)
|
|
189
|
+
else:
|
|
190
|
+
bbox = annotation.bbox
|
|
191
|
+
return bbox, annotation_str_repr, extra_str_repr, text_y_offsets, text_w, text_h
|
|
192
|
+
|
|
193
|
+
def add_label(
|
|
194
|
+
self,
|
|
195
|
+
image_info: tuple[np.ndarray, ImageAnnotations, RGB_TYPE],
|
|
196
|
+
spacing: int = 5,
|
|
197
|
+
class_label: bool = False,
|
|
198
|
+
) -> np.ndarray:
|
|
199
|
+
"""
|
|
200
|
+
Adds label to the image
|
|
201
|
+
|
|
202
|
+
Args:
|
|
203
|
+
image_info (tuple): Information of the image where
|
|
204
|
+
annotation is to be Drawn: the actual image as an array,
|
|
205
|
+
the annotation where label is stored and the color of the annotation.
|
|
206
|
+
spacing (int): Space between different annotations if more than one
|
|
207
|
+
class_label (bool): Flag to determine if the label is from classification.
|
|
208
|
+
|
|
209
|
+
"""
|
|
210
|
+
image, annotation, ann_color = image_info
|
|
211
|
+
h, w = image.shape[:2]
|
|
212
|
+
|
|
213
|
+
bbox, annotation_str_repr, extra_str_labels, text_y_offset, text_w, text_h = self.get_label_properties(
|
|
214
|
+
(h, w), annotation, class_label, spacing
|
|
215
|
+
)
|
|
216
|
+
text_color = darken_or_lighten_color(ann_color)
|
|
217
|
+
image = draw_annotation_rectangle(image, bbox, ann_color, text_h, text_w)
|
|
218
|
+
if extra_str_labels and text_y_offset:
|
|
219
|
+
image = draw_extra_labels(
|
|
220
|
+
image,
|
|
221
|
+
annotation.bbox,
|
|
222
|
+
extra_str_labels,
|
|
223
|
+
text_y_offset,
|
|
224
|
+
text_color,
|
|
225
|
+
self.attributes.text_style,
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
image = draw_text(
|
|
229
|
+
image,
|
|
230
|
+
annotation_str_repr,
|
|
231
|
+
TextInstanceProperties(
|
|
232
|
+
bbox.x,
|
|
233
|
+
bbox.y - self.attributes.text_style.font_scale,
|
|
234
|
+
text_color,
|
|
235
|
+
),
|
|
236
|
+
self.attributes.text_style,
|
|
237
|
+
)
|
|
238
|
+
return image
|
|
239
|
+
|
|
240
|
+
def add_classification_label(
|
|
241
|
+
self,
|
|
242
|
+
image_info: tuple[np.ndarray, ImageAnnotations, RGB_TYPE],
|
|
243
|
+
class_label: bool = False,
|
|
244
|
+
) -> np.ndarray:
|
|
245
|
+
"""
|
|
246
|
+
Adds the classification label to the image
|
|
247
|
+
|
|
248
|
+
Args:
|
|
249
|
+
- image_info (tuple): information of the image containing
|
|
250
|
+
the actual image to draw the annotation on, the annotation
|
|
251
|
+
object and the annotation color.
|
|
252
|
+
- class_label (bool): flag to determine if the label is a
|
|
253
|
+
classification label, in which case, there is no bounding
|
|
254
|
+
box associated with it. Adds a classification label annotation
|
|
255
|
+
to the image
|
|
256
|
+
|
|
257
|
+
Returns:
|
|
258
|
+
np.ndarray: The image with the added classification label and associated bounding box.
|
|
259
|
+
"""
|
|
260
|
+
|
|
261
|
+
image = image_info[0]
|
|
262
|
+
font_scale, font_thickness = get_dynamic_text_properties(image)
|
|
263
|
+
self.attributes.text_style.font_scale = font_scale
|
|
264
|
+
self.attributes.text_style.thickness = font_thickness
|
|
265
|
+
|
|
266
|
+
image = self.add_label(image_info=image_info, class_label=class_label, spacing=int(5 * font_scale))
|
|
267
|
+
|
|
268
|
+
return image
|
|
269
|
+
|
|
270
|
+
def draw_label_strategy(self, image: np.ndarray, annotation: ImageAnnotations, ann_color: RGB_TYPE) -> np.ndarray:
|
|
271
|
+
"""
|
|
272
|
+
Strategy for drawing labels on the image, either classification
|
|
273
|
+
or regular labels based on attributes.
|
|
274
|
+
|
|
275
|
+
Args:
|
|
276
|
+
image (np.ndarray): The image to draw on.
|
|
277
|
+
annotation (ImageAnnotations): The annotation containing
|
|
278
|
+
the label to be drawn.
|
|
279
|
+
ann_color (RGB_TYPE): The color for the label.
|
|
280
|
+
|
|
281
|
+
Returns:
|
|
282
|
+
np.ndarray: The image with the label drawn, either classification or regular.
|
|
283
|
+
"""
|
|
284
|
+
if not annotation.label_str and not annotation.confidence_score:
|
|
285
|
+
return image
|
|
286
|
+
|
|
287
|
+
if annotation.label_str and self.attributes.draw_classification_label:
|
|
288
|
+
image = self.add_classification_label(image_info=(image, annotation, ann_color), class_label=True)
|
|
289
|
+
if annotation.bbox:
|
|
290
|
+
image = self.add_label(image_info=(image, annotation, ann_color))
|
|
291
|
+
|
|
292
|
+
return image
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
import cv2
|
|
5
|
+
import numpy as np
|
|
6
|
+
from sinapsis_core.data_containers.annotations import (
|
|
7
|
+
ImageAnnotations,
|
|
8
|
+
OrientedBoundingBox,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
from sinapsis_data_visualization.helpers.color_utils import RGB_TYPE
|
|
12
|
+
from sinapsis_data_visualization.templates.bbox_drawer import BBoxDrawer
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class OrientedBBoxDrawer(BBoxDrawer):
|
|
16
|
+
"""
|
|
17
|
+
A class for drawing oriented bounding boxes on images,
|
|
18
|
+
|
|
19
|
+
This class allows the addition of oriented bounding boxes
|
|
20
|
+
along with the label annotations by implementing adding the method
|
|
21
|
+
'draw_oriented_bbox' to the drawing_strategy
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
Usage example:
|
|
25
|
+
|
|
26
|
+
agent:
|
|
27
|
+
name: my_test_agent
|
|
28
|
+
templates:
|
|
29
|
+
- template_name: InputTemplate
|
|
30
|
+
class_name: InputTemplate
|
|
31
|
+
attributes: {}
|
|
32
|
+
- template_name: OrientedBBoxDrawer
|
|
33
|
+
class_name: OrientedBBoxDrawer
|
|
34
|
+
template_input: InputTemplate
|
|
35
|
+
attributes:
|
|
36
|
+
overwrite: false
|
|
37
|
+
randomized_color: true
|
|
38
|
+
draw_confidence: true
|
|
39
|
+
draw_extra_labels: true
|
|
40
|
+
text_style:
|
|
41
|
+
font: 0
|
|
42
|
+
font_scale: 0.5
|
|
43
|
+
thickness: 2
|
|
44
|
+
draw_classification_label: false
|
|
45
|
+
classification_label_position: top_right
|
|
46
|
+
text_box_to_border_offset: 0.01
|
|
47
|
+
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
def set_drawing_strategy(self) -> None:
|
|
51
|
+
"""
|
|
52
|
+
Appends the method to draw the oriented bbox on the image, to
|
|
53
|
+
the drawing_strategies list.
|
|
54
|
+
"""
|
|
55
|
+
self.drawing_strategies.append(self.draw_bbox_strategy)
|
|
56
|
+
|
|
57
|
+
@staticmethod
|
|
58
|
+
def draw_oriented_bbox(
|
|
59
|
+
image: np.ndarray,
|
|
60
|
+
oriented_bbox: OrientedBoundingBox,
|
|
61
|
+
color: RGB_TYPE,
|
|
62
|
+
thickness: int = 2,
|
|
63
|
+
) -> np.ndarray:
|
|
64
|
+
"""
|
|
65
|
+
Draws an oriented bounding box (polygon) on the image.
|
|
66
|
+
|
|
67
|
+
Args:
|
|
68
|
+
image (np.ndarray): The image to draw the oriented bounding box on.
|
|
69
|
+
oriented_bbox (OrientedBoundingBox): The oriented bounding box
|
|
70
|
+
to be drawn (containing 4 points).
|
|
71
|
+
color (RGB_TYPE): The color of the bounding box in RGB format.
|
|
72
|
+
thickness (int, optional): The thickness of the bounding box lines.
|
|
73
|
+
|
|
74
|
+
Returns:
|
|
75
|
+
np.ndarray: The image with the oriented bounding box drawn.
|
|
76
|
+
"""
|
|
77
|
+
image = np.array(image, dtype="uint8")
|
|
78
|
+
|
|
79
|
+
pts = np.array(
|
|
80
|
+
[
|
|
81
|
+
[int(oriented_bbox.x1), int(oriented_bbox.y1)],
|
|
82
|
+
[int(oriented_bbox.x2), int(oriented_bbox.y2)],
|
|
83
|
+
[int(oriented_bbox.x3), int(oriented_bbox.y3)],
|
|
84
|
+
[int(oriented_bbox.x4), int(oriented_bbox.y4)],
|
|
85
|
+
]
|
|
86
|
+
)
|
|
87
|
+
cv2.polylines(img=image, pts=[pts], isClosed=True, color=color, thickness=thickness)
|
|
88
|
+
|
|
89
|
+
return image
|
|
90
|
+
|
|
91
|
+
def draw_oriented_bbox_strategy(
|
|
92
|
+
self,
|
|
93
|
+
image: np.ndarray,
|
|
94
|
+
annotation: ImageAnnotations,
|
|
95
|
+
ann_color: RGB_TYPE,
|
|
96
|
+
) -> np.ndarray:
|
|
97
|
+
"""
|
|
98
|
+
Strategy method to draw both the oriented bounding box and any
|
|
99
|
+
associated label annotations on the image.
|
|
100
|
+
|
|
101
|
+
This method is used as part of the drawing strategy to draw an
|
|
102
|
+
oriented bounding box and associated labels for the given annotation.
|
|
103
|
+
|
|
104
|
+
Args:
|
|
105
|
+
image (np.ndarray): The image to draw on.
|
|
106
|
+
annotation (ImageAnnotations): The annotation containing the
|
|
107
|
+
oriented bounding box and label data.
|
|
108
|
+
ann_color (RGB_TYPE): The color to use for the bounding box and text.
|
|
109
|
+
|
|
110
|
+
Returns:
|
|
111
|
+
np.ndarray: The image with the oriented bounding box and labels drawn.
|
|
112
|
+
"""
|
|
113
|
+
|
|
114
|
+
if annotation.oriented_bbox:
|
|
115
|
+
image = self.draw_oriented_bbox(
|
|
116
|
+
image=image,
|
|
117
|
+
oriented_bbox=annotation.oriented_bbox,
|
|
118
|
+
color=ann_color,
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
return image
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
from sinapsis_core.data_containers.annotations import ImageAnnotations, Segmentation
|
|
5
|
+
|
|
6
|
+
from sinapsis_data_visualization.helpers.color_utils import RGB_TYPE
|
|
7
|
+
from sinapsis_data_visualization.templates.bbox_drawer import BBoxDrawer
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class SegmentationMaskDrawer(BBoxDrawer):
|
|
11
|
+
"""
|
|
12
|
+
A class for drawing segmentation masks and keypoints on images,
|
|
13
|
+
extending from BBoxDrawer.
|
|
14
|
+
|
|
15
|
+
This class allows drawing segmentation masks (binary or RGB masks)
|
|
16
|
+
along with keypoints on images.
|
|
17
|
+
It inherits from the `BBoxDrawer` class to reuse its functionality
|
|
18
|
+
for drawing bounding boxes and adds the ability to handle segmentation
|
|
19
|
+
masks, which can be overlaid with specific transparency levels (alpha).
|
|
20
|
+
|
|
21
|
+
Usage example:
|
|
22
|
+
|
|
23
|
+
agent:
|
|
24
|
+
name: my_test_agent
|
|
25
|
+
templates:
|
|
26
|
+
- template_name: InputTemplate
|
|
27
|
+
class_name: InputTemplate
|
|
28
|
+
attributes: {}
|
|
29
|
+
- template_name: SegmentationMaskDrawer
|
|
30
|
+
class_name: SegmentationMaskDrawer
|
|
31
|
+
template_input: InputTemplate
|
|
32
|
+
attributes:
|
|
33
|
+
overwrite: false
|
|
34
|
+
randomized_color: true
|
|
35
|
+
draw_confidence: true
|
|
36
|
+
draw_extra_labels: true
|
|
37
|
+
text_style:
|
|
38
|
+
font: 0
|
|
39
|
+
font_scale: 0.5
|
|
40
|
+
thickness: 2
|
|
41
|
+
draw_classification_label: false
|
|
42
|
+
classification_label_position: top_right
|
|
43
|
+
text_box_to_border_offset: 0.01
|
|
44
|
+
alpha: 0.5
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
class AttributesBaseModel(BBoxDrawer.AttributesBaseModel):
|
|
48
|
+
"""
|
|
49
|
+
Nested class to define the specific attributes for segmentation mask drawing.
|
|
50
|
+
|
|
51
|
+
The `alpha` attribute controls the transparency of the segmentation mask,
|
|
52
|
+
where 0 is fully transparent and 1 is fully opaque.
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
alpha: float = 0.5
|
|
56
|
+
|
|
57
|
+
def set_drawing_strategy(self) -> None:
|
|
58
|
+
"""
|
|
59
|
+
Appends the method to draw the segmentation masks on the image, to
|
|
60
|
+
the drawing_strategies list.
|
|
61
|
+
"""
|
|
62
|
+
self.drawing_strategies.append(self.draw_segmentation_strategy)
|
|
63
|
+
|
|
64
|
+
@staticmethod
|
|
65
|
+
def draw_segmentation(
|
|
66
|
+
image: np.ndarray,
|
|
67
|
+
segmentation: Segmentation,
|
|
68
|
+
color: RGB_TYPE,
|
|
69
|
+
alpha: float,
|
|
70
|
+
) -> np.ndarray:
|
|
71
|
+
"""
|
|
72
|
+
Draws a binary segmentation mask on the image with
|
|
73
|
+
specified transparency (alpha).
|
|
74
|
+
|
|
75
|
+
Args:
|
|
76
|
+
image (np.ndarray): The image in which to draw the segmentation mask.
|
|
77
|
+
segmentation (Segmentation): The binary segmentation mask
|
|
78
|
+
(1 for object, 0 for background).
|
|
79
|
+
color (RGB_TYPE): The color to use for the segmented object.
|
|
80
|
+
alpha (float): Transparency of the mask. 0 is completely transparent,
|
|
81
|
+
1 is completely opaque.
|
|
82
|
+
|
|
83
|
+
Returns:
|
|
84
|
+
np.ndarray: The edited image with the segmentation mask applied.
|
|
85
|
+
"""
|
|
86
|
+
# If the image is grayscale, convert it to RGB:
|
|
87
|
+
if image.ndim == 2:
|
|
88
|
+
image = np.stack((image,) * 3, axis=-1)
|
|
89
|
+
# Create an empty RGB mask:
|
|
90
|
+
rgb_mask = np.zeros_like(image, dtype=np.float32)
|
|
91
|
+
mask_condition = segmentation.mask == 1
|
|
92
|
+
# Assign the color to the mask where the condition is True:
|
|
93
|
+
rgb_mask[mask_condition] = color
|
|
94
|
+
# Blend the image and the mask using alpha transparency:
|
|
95
|
+
blended_img_np = np.zeros_like(image, dtype=np.float32)
|
|
96
|
+
blended_img_np[mask_condition] = image[mask_condition] * (1.0 - alpha) + rgb_mask[mask_condition] * alpha
|
|
97
|
+
# Keep the original image where the mask condition is False:
|
|
98
|
+
blended_img_np[~mask_condition] = image[~mask_condition]
|
|
99
|
+
# Convert the image back to uint8 for displaying:
|
|
100
|
+
blended_img_np = blended_img_np.astype(np.uint8)
|
|
101
|
+
return blended_img_np
|
|
102
|
+
|
|
103
|
+
def draw_segmentation_strategy(
|
|
104
|
+
self,
|
|
105
|
+
image: np.ndarray,
|
|
106
|
+
annotation: ImageAnnotations,
|
|
107
|
+
ann_color: RGB_TYPE,
|
|
108
|
+
) -> np.ndarray:
|
|
109
|
+
"""
|
|
110
|
+
Strategy method to draw the segmentation mask as part of the
|
|
111
|
+
annotation process.
|
|
112
|
+
|
|
113
|
+
This method is part of the drawing strategy pipeline, responsible
|
|
114
|
+
for drawing a segmentation mask
|
|
115
|
+
for a given annotation. If a segmentation mask is present in the
|
|
116
|
+
annotation, it will be drawn
|
|
117
|
+
with the specified color and transparency level.
|
|
118
|
+
|
|
119
|
+
Args:
|
|
120
|
+
image (np.ndarray): The image on which to draw the segmentation mask.
|
|
121
|
+
annotation (ImageAnnotations): The annotation containing the
|
|
122
|
+
segmentation mask.
|
|
123
|
+
ann_color (RGB_TYPE): The color to use for drawing the segmentation mask.
|
|
124
|
+
|
|
125
|
+
Returns:
|
|
126
|
+
np.ndarray: The image with the segmentation mask drawn.
|
|
127
|
+
"""
|
|
128
|
+
|
|
129
|
+
if annotation.segmentation is not None:
|
|
130
|
+
image = self.draw_segmentation(
|
|
131
|
+
image=image,
|
|
132
|
+
segmentation=annotation.segmentation,
|
|
133
|
+
color=ann_color,
|
|
134
|
+
alpha=self.attributes.alpha,
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
return image
|