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,51 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
import cv2
|
|
4
|
+
import numpy as np
|
|
5
|
+
from sinapsis_core.data_containers.data_packet import ImagePacket
|
|
6
|
+
from sklearn.cluster import KMeans
|
|
7
|
+
from sklearn.decomposition import PCA
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def perform_k_means_analysis(feature_vec: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
|
|
11
|
+
"""
|
|
12
|
+
Instantiates a class of KMeans clustering, predicts the cluster that each sample
|
|
13
|
+
from the feature_vector belongs to, and then performs a PCA to
|
|
14
|
+
return the transformed values with shape (n_samples, n_components)
|
|
15
|
+
|
|
16
|
+
Args:
|
|
17
|
+
feature_vec (np.ndarray): array of shape (n_samples, n_features)
|
|
18
|
+
|
|
19
|
+
Returns:
|
|
20
|
+
(np.ndarray): the array of labels corresponding to the indices of the
|
|
21
|
+
cluster each sample belongs to the labels and transformed data
|
|
22
|
+
(np.ndarray): the reduced vectors to be plotted
|
|
23
|
+
"""
|
|
24
|
+
k_means = KMeans(n_clusters=3, random_state=0)
|
|
25
|
+
pca = PCA(n_components=2, random_state=0)
|
|
26
|
+
labels: np.ndarray = k_means.fit_predict(feature_vec)
|
|
27
|
+
reduced_vectors = pca.fit_transform(feature_vec)
|
|
28
|
+
|
|
29
|
+
return labels, reduced_vectors
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def pre_process_images(images: list[ImagePacket], size: int = 28) -> np.ndarray | None:
|
|
33
|
+
"""Resizes and flatten images to homogenize them
|
|
34
|
+
|
|
35
|
+
Args:
|
|
36
|
+
images (list[ImagePacket]): list of ImagePackets to resize
|
|
37
|
+
size (int): new size for the images.
|
|
38
|
+
|
|
39
|
+
Returns:
|
|
40
|
+
np.ndarray : The array with flattened and resized image"""
|
|
41
|
+
|
|
42
|
+
feature_vec: list[np.ndarray] = []
|
|
43
|
+
for image in images:
|
|
44
|
+
resized_img = cv2.resize(image.content, (size, size))
|
|
45
|
+
flattened_img = resized_img.flatten()
|
|
46
|
+
|
|
47
|
+
feature_vec.append(flattened_img)
|
|
48
|
+
|
|
49
|
+
feature_arr = np.array(feature_vec)
|
|
50
|
+
|
|
51
|
+
return feature_arr
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
import importlib
|
|
3
|
+
from typing import Callable
|
|
4
|
+
|
|
5
|
+
_root_lib_path = "sinapsis_data_visualization.templates"
|
|
6
|
+
|
|
7
|
+
_template_lookup = {
|
|
8
|
+
"DataDistributionVisualization": f"{_root_lib_path}.data_distribution_visualization_template",
|
|
9
|
+
"LabelDrawer": f"{_root_lib_path}.label_drawer",
|
|
10
|
+
"BBoxDrawer": f"{_root_lib_path}.bbox_drawer",
|
|
11
|
+
"KeyPointsDrawer": f"{_root_lib_path}.key_points_drawer",
|
|
12
|
+
"OrientedBBoxDrawer": f"{_root_lib_path}.oriented_bbox_drawer",
|
|
13
|
+
"SegmentationMaskDrawer": f"{_root_lib_path}.segmentation_mask_drawer",
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def __getattr__(name: str) -> Callable: # type:ignore
|
|
18
|
+
if name in _template_lookup:
|
|
19
|
+
module = importlib.import_module(_template_lookup[name])
|
|
20
|
+
return getattr(module, name)
|
|
21
|
+
|
|
22
|
+
raise AttributeError(f"template `{name}` not found in {_root_lib_path}")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
__all__ = list(_template_lookup.keys())
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
import abc
|
|
3
|
+
import random
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
from sinapsis_core.data_containers.data_packet import DataContainer, ImagePacket
|
|
7
|
+
from sinapsis_core.template_base import (
|
|
8
|
+
Template,
|
|
9
|
+
TemplateAttributes,
|
|
10
|
+
TemplateAttributeType,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
from sinapsis_data_visualization.helpers.color_utils import (
|
|
14
|
+
RGB_TYPE,
|
|
15
|
+
build_color_map,
|
|
16
|
+
get_color_rgb_tuple,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
random.seed(0)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class BaseAnnotationDrawer(Template, abc.ABC):
|
|
23
|
+
"""Base template to handle and draw annotations in images.
|
|
24
|
+
|
|
25
|
+
This class incorporates methods to determine if an ImagePacket
|
|
26
|
+
has annotations and to draw annotations on images.
|
|
27
|
+
|
|
28
|
+
The execute method calls the draw_annotation method for
|
|
29
|
+
the list of ImagePackets
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
COLOR_MAP = build_color_map()
|
|
33
|
+
|
|
34
|
+
class AttributesBaseModel(TemplateAttributes):
|
|
35
|
+
"""
|
|
36
|
+
Attributes for Image Annotation Drawer
|
|
37
|
+
|
|
38
|
+
Args:
|
|
39
|
+
overwrite (bool): Enable overwriting original image content.
|
|
40
|
+
If False Image drawing is performed on a copy of the original one.
|
|
41
|
+
randomized_color (bool): Flag to use random colors in the annotations.
|
|
42
|
+
Defaults to True
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
overwrite: bool = False
|
|
46
|
+
randomized_color: bool = True
|
|
47
|
+
|
|
48
|
+
def __init__(self, attributes: TemplateAttributeType) -> None:
|
|
49
|
+
"""Initializes the Image Annotation Drawer with the given attributes."""
|
|
50
|
+
super().__init__(attributes)
|
|
51
|
+
self.drawing_strategies: list = []
|
|
52
|
+
self.set_drawing_strategy()
|
|
53
|
+
|
|
54
|
+
def set_drawing_strategy(self) -> None:
|
|
55
|
+
"""Abstract method to determine which annotations to draw
|
|
56
|
+
(e.g., labels, bbox, kpts, etc.)"""
|
|
57
|
+
|
|
58
|
+
@staticmethod
|
|
59
|
+
def image_has_annotations(image: ImagePacket) -> bool:
|
|
60
|
+
"""
|
|
61
|
+
Checks if the image packet contains annotations.
|
|
62
|
+
|
|
63
|
+
Args:
|
|
64
|
+
image (ImagePacket): The image packet to check.
|
|
65
|
+
|
|
66
|
+
Returns:
|
|
67
|
+
bool: True if the image has annotations, False otherwise.
|
|
68
|
+
"""
|
|
69
|
+
return image.annotations is not None
|
|
70
|
+
|
|
71
|
+
def draw_annotation(self, image_packet: ImagePacket) -> None:
|
|
72
|
+
"""
|
|
73
|
+
Abstract method for drawing annotations on an image packet.
|
|
74
|
+
Args:
|
|
75
|
+
image_packet (ImagePacket): The image packet containing annotations to draw.
|
|
76
|
+
"""
|
|
77
|
+
image_packet.content = np.ascontiguousarray(image_packet.content)
|
|
78
|
+
|
|
79
|
+
if self.image_has_annotations(image_packet):
|
|
80
|
+
for ann in image_packet.annotations:
|
|
81
|
+
ann_color: RGB_TYPE = get_color_rgb_tuple(
|
|
82
|
+
color_map=self.COLOR_MAP,
|
|
83
|
+
class_id=ann.label,
|
|
84
|
+
randomized=self.attributes.randomized_color,
|
|
85
|
+
)
|
|
86
|
+
for strategy in self.drawing_strategies:
|
|
87
|
+
image_packet.content = strategy(image_packet.content, ann, ann_color)
|
|
88
|
+
|
|
89
|
+
def execute(self, container: DataContainer) -> DataContainer:
|
|
90
|
+
"""
|
|
91
|
+
Executes image drawer for images in the container.
|
|
92
|
+
|
|
93
|
+
Args:
|
|
94
|
+
container (DataContainer): The data container containing image packets.
|
|
95
|
+
|
|
96
|
+
Returns:
|
|
97
|
+
DataContainer: The updated data container with images annotated.
|
|
98
|
+
"""
|
|
99
|
+
|
|
100
|
+
for image_packet in container.images:
|
|
101
|
+
self.draw_annotation(image_packet)
|
|
102
|
+
|
|
103
|
+
return container
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
import cv2
|
|
4
|
+
import numpy as np
|
|
5
|
+
from sinapsis_core.data_containers.annotations import BoundingBox, ImageAnnotations
|
|
6
|
+
|
|
7
|
+
from sinapsis_data_visualization.helpers.color_utils import RGB_TYPE
|
|
8
|
+
from sinapsis_data_visualization.templates.label_drawer import LabelDrawer
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class BBoxDrawer(LabelDrawer):
|
|
12
|
+
"""
|
|
13
|
+
A class for drawing bounding boxes in images, extending from the LabelDrawer template.
|
|
14
|
+
|
|
15
|
+
This class allows the addition of bounding boxes along with the label annotations
|
|
16
|
+
by implementing a drawing strategy that draws both bounding boxes and associated labels.
|
|
17
|
+
It inherits from the `LabelDrawer` class, leveraging its functionality for label drawing
|
|
18
|
+
and customization.
|
|
19
|
+
|
|
20
|
+
Usage example:
|
|
21
|
+
agent:
|
|
22
|
+
name: my_test_agent
|
|
23
|
+
templates:
|
|
24
|
+
- template_name: InputTemplate
|
|
25
|
+
class_name: InputTemplate
|
|
26
|
+
attributes: {}
|
|
27
|
+
- template_name: BBoxDrawer
|
|
28
|
+
class_name: BBoxDrawer
|
|
29
|
+
template_input: InputTemplate
|
|
30
|
+
attributes:
|
|
31
|
+
overwrite: false
|
|
32
|
+
randomized_color: true
|
|
33
|
+
draw_confidence: true
|
|
34
|
+
draw_extra_labels: true
|
|
35
|
+
text_style:
|
|
36
|
+
font: 0
|
|
37
|
+
font_scale: 0.5
|
|
38
|
+
thickness: 2
|
|
39
|
+
draw_classification_label: false
|
|
40
|
+
classification_label_position: top_right
|
|
41
|
+
text_box_to_border_offset: 0.01
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
def set_drawing_strategy(self) -> None:
|
|
47
|
+
"""
|
|
48
|
+
Appends the method to draw the bbox on the image, to
|
|
49
|
+
the drawing_strategies list.
|
|
50
|
+
"""
|
|
51
|
+
super().set_drawing_strategy()
|
|
52
|
+
self.drawing_strategies.insert(0, self.draw_bbox_strategy)
|
|
53
|
+
|
|
54
|
+
@staticmethod
|
|
55
|
+
def draw_bbox(
|
|
56
|
+
image: np.ndarray,
|
|
57
|
+
bbox: BoundingBox,
|
|
58
|
+
color: RGB_TYPE,
|
|
59
|
+
thickness: int = 2,
|
|
60
|
+
) -> np.ndarray:
|
|
61
|
+
"""
|
|
62
|
+
Draws a bounding box on the given image.
|
|
63
|
+
|
|
64
|
+
Args:
|
|
65
|
+
image (np.ndarray): The image to draw on.
|
|
66
|
+
bbox (BoundingBox): The bounding box to be drawn on the image.
|
|
67
|
+
color (RGB_TYPE): The color of the bounding box (in RGB format).
|
|
68
|
+
thickness (int, optional): The thickness of the bounding box lines. Defaults to 2.
|
|
69
|
+
|
|
70
|
+
Returns:
|
|
71
|
+
np.ndarray: The image with the bounding box drawn.
|
|
72
|
+
"""
|
|
73
|
+
image = np.array(image, dtype="uint8")
|
|
74
|
+
cv2.rectangle(
|
|
75
|
+
img=image,
|
|
76
|
+
pt1=(int(bbox.x), int(bbox.y)),
|
|
77
|
+
pt2=(int(bbox.x) + int(bbox.w), int(bbox.y) + int(bbox.h)),
|
|
78
|
+
color=color,
|
|
79
|
+
thickness=thickness,
|
|
80
|
+
)
|
|
81
|
+
return image
|
|
82
|
+
|
|
83
|
+
def draw_bbox_strategy(self, image: np.ndarray, annotation: ImageAnnotations, ann_color: RGB_TYPE) -> np.ndarray:
|
|
84
|
+
"""
|
|
85
|
+
Strategy method to draw both bounding boxes and labels on the image.
|
|
86
|
+
|
|
87
|
+
This method is used as part of the drawing strategy to draw a bounding box and
|
|
88
|
+
associated labels (if applicable) for the given annotation.
|
|
89
|
+
|
|
90
|
+
Args:
|
|
91
|
+
image (np.ndarray): The image to draw on.
|
|
92
|
+
annotation (ImageAnnotations): The annotation containing the bounding box and label data.
|
|
93
|
+
ann_color (RGB_TYPE): The color to use for the bounding box and text.
|
|
94
|
+
|
|
95
|
+
Returns:
|
|
96
|
+
np.ndarray: The image with the bounding box and label drawn.
|
|
97
|
+
"""
|
|
98
|
+
if annotation.bbox:
|
|
99
|
+
image = self.draw_bbox(
|
|
100
|
+
image=image,
|
|
101
|
+
bbox=annotation.bbox,
|
|
102
|
+
color=ann_color,
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
return image
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""Data distribution visualization template"""
|
|
3
|
+
|
|
4
|
+
import os
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
import matplotlib
|
|
8
|
+
import matplotlib.pyplot as plt
|
|
9
|
+
import numpy as np
|
|
10
|
+
from pydantic import Field
|
|
11
|
+
from pydantic.dataclasses import dataclass
|
|
12
|
+
from sinapsis_core.data_containers.data_packet import DataContainer
|
|
13
|
+
from sinapsis_core.template_base import (
|
|
14
|
+
Template,
|
|
15
|
+
TemplateAttributes,
|
|
16
|
+
TemplateAttributeType,
|
|
17
|
+
)
|
|
18
|
+
from sinapsis_core.utils.env_var_keys import SINAPSIS_CACHE_DIR
|
|
19
|
+
|
|
20
|
+
from sinapsis_data_visualization.helpers.plot_distributions import (
|
|
21
|
+
plot_distribution,
|
|
22
|
+
plot_map,
|
|
23
|
+
)
|
|
24
|
+
from sinapsis_data_visualization.helpers.scikit_pca_analysis import (
|
|
25
|
+
perform_k_means_analysis,
|
|
26
|
+
pre_process_images,
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class PlotAttributes(TemplateAttributes):
|
|
31
|
+
"""Base attributes for all plot types.
|
|
32
|
+
|
|
33
|
+
Attributes:
|
|
34
|
+
fig_title (str|None): Title for the figure.
|
|
35
|
+
y_label (str|None): Label of the y-axis.
|
|
36
|
+
x_label (str|None): Label of the x-axis.
|
|
37
|
+
fig_width (int): Width of the figure.
|
|
38
|
+
fig_height (int): Height of the figure.
|
|
39
|
+
x_position (float): X-position for the legend and/or texts.
|
|
40
|
+
y_position (float): Y-position for the legend and/or texts.
|
|
41
|
+
save_image_dir (str): Directory to save the image.
|
|
42
|
+
fig_name (str): Name of the figure.
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
fig_title: str | None = None
|
|
46
|
+
y_label: str | None = None
|
|
47
|
+
x_label: str | None = None
|
|
48
|
+
fig_width: int = 10
|
|
49
|
+
fig_height: int = 10
|
|
50
|
+
x_position: float = 0.5
|
|
51
|
+
y_position: float = 0.5
|
|
52
|
+
text: str | None = None
|
|
53
|
+
save_image_dir: str | None = SINAPSIS_CACHE_DIR
|
|
54
|
+
fig_name: str
|
|
55
|
+
kwargs: dict[str, Any] = Field(default_factory=dict)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
@dataclass
|
|
59
|
+
class PlotTypes:
|
|
60
|
+
"""Dataclass with different types of plots.
|
|
61
|
+
HISTOGRAM: Key for the histogram plot
|
|
62
|
+
BOX_PLOT: Key for the box plot
|
|
63
|
+
PIE_CHART: Key for the pie chart plot
|
|
64
|
+
CLUSTERING: Key for the k-means custering plot
|
|
65
|
+
"""
|
|
66
|
+
|
|
67
|
+
HISTOGRAM: str = "histogram"
|
|
68
|
+
BOX_PLOT: str = "box_plot"
|
|
69
|
+
PIE_CHART: str = "pie_chart"
|
|
70
|
+
CLUSTERING: str = "k_means_clustering"
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class DataDistributionVisualization(Template):
|
|
74
|
+
"""
|
|
75
|
+
This template plots the distribution for image data, and can be
|
|
76
|
+
extended to plot the distribution of different Packets, provided
|
|
77
|
+
they have labels.
|
|
78
|
+
The template allows for the drawing of histograms, box plots, pie charts
|
|
79
|
+
and PCA from k-means cluster
|
|
80
|
+
The template returns a figure in the container as an ImagePacket.
|
|
81
|
+
|
|
82
|
+
Usage example:
|
|
83
|
+
|
|
84
|
+
agent:
|
|
85
|
+
name: my_test_agent
|
|
86
|
+
templates:
|
|
87
|
+
- template_name: InputTemplate
|
|
88
|
+
class_name: InputTemplate
|
|
89
|
+
attributes: {}
|
|
90
|
+
- template_name: DataDistributionVisualization
|
|
91
|
+
class_name: DataDistributionVisualization
|
|
92
|
+
template_input: InputTemplate
|
|
93
|
+
attributes:
|
|
94
|
+
fig_title: null
|
|
95
|
+
y_label: null
|
|
96
|
+
x_label: null
|
|
97
|
+
fig_width: 10
|
|
98
|
+
fig_height: 10
|
|
99
|
+
x_position: 0.5
|
|
100
|
+
y_position: 0.5
|
|
101
|
+
text: null
|
|
102
|
+
save_image_dir: $SINAPSIS_CACHE_DIR
|
|
103
|
+
fig_name: 'desired_name_for_the_figure'
|
|
104
|
+
kwargs: '{}'
|
|
105
|
+
histogram: false
|
|
106
|
+
box_plot: false
|
|
107
|
+
pie_chart: false
|
|
108
|
+
k_means_clustering: false
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
"""
|
|
112
|
+
|
|
113
|
+
class AttributesBaseModel(PlotAttributes):
|
|
114
|
+
"""
|
|
115
|
+
Attributes for data analysis using matplotlib.
|
|
116
|
+
|
|
117
|
+
Attributes:
|
|
118
|
+
histogram (bool): Whether to plot a histogram. Defaults to False.
|
|
119
|
+
box_plot (bool): Whether to plot a box plot. Defaults to False.
|
|
120
|
+
pie_chart (bool): Whether to plot a pie chart. Defaults to False.
|
|
121
|
+
k_means_clustering (bool): Whether to plot k-means clustering. Defaults to False.
|
|
122
|
+
"""
|
|
123
|
+
|
|
124
|
+
histogram: bool = False
|
|
125
|
+
box_plot: bool = False
|
|
126
|
+
pie_chart: bool = False
|
|
127
|
+
k_means_clustering: bool = False
|
|
128
|
+
|
|
129
|
+
def __init__(self, attributes: TemplateAttributeType) -> None:
|
|
130
|
+
"""Initializes the DataDistributionVisualization
|
|
131
|
+
template with given attributes.
|
|
132
|
+
|
|
133
|
+
Args:
|
|
134
|
+
attributes (TemplateAttributeType): The attributes for
|
|
135
|
+
configuring the data analysis.
|
|
136
|
+
"""
|
|
137
|
+
super().__init__(attributes)
|
|
138
|
+
|
|
139
|
+
if not os.path.exists(self.attributes.save_image_dir):
|
|
140
|
+
os.makedirs(self.attributes.save_image_dir)
|
|
141
|
+
|
|
142
|
+
def create_figure(self, text: str | None = None, rotate_x_ticks: bool = False) -> matplotlib.figure.Figure:
|
|
143
|
+
"""
|
|
144
|
+
Creates the matplotlib.figure.Figure object and adds relevant
|
|
145
|
+
information, such as the x-label, y-label and title.
|
|
146
|
+
It also adds available text to the desired x and y positions and rotated
|
|
147
|
+
the x-ticks is flag is True
|
|
148
|
+
|
|
149
|
+
Args:
|
|
150
|
+
text (str | None): Optional text to add to the figure. Defaults to None.
|
|
151
|
+
rotate_x_ticks (bool): Flag to rotate x-ticks 90 degrees. Defaults to False
|
|
152
|
+
Returns:
|
|
153
|
+
The initialized figure with corresponding parameters
|
|
154
|
+
|
|
155
|
+
"""
|
|
156
|
+
figure = plt.figure(figsize=(self.attributes.fig_width, self.attributes.fig_height))
|
|
157
|
+
plt.xlabel(self.attributes.x_label or "X-Axis")
|
|
158
|
+
plt.ylabel(self.attributes.y_label or "Y-Axis")
|
|
159
|
+
plt.title(self.attributes.fig_title or "Plot Title")
|
|
160
|
+
|
|
161
|
+
# Prepare the text for annotation
|
|
162
|
+
if text:
|
|
163
|
+
plt.text(self.attributes.x_position, self.attributes.y_position, text)
|
|
164
|
+
|
|
165
|
+
if rotate_x_ticks:
|
|
166
|
+
plt.xticks(rotation=90)
|
|
167
|
+
return figure
|
|
168
|
+
|
|
169
|
+
@staticmethod
|
|
170
|
+
def retrieve_labels_from_images(
|
|
171
|
+
container: DataContainer,
|
|
172
|
+
) -> tuple[list[str | int], list[int]]:
|
|
173
|
+
"""
|
|
174
|
+
Iterates through the ImagePackets, extracting each of the
|
|
175
|
+
annotations for all of them.
|
|
176
|
+
Generates a dictionary with the label and number of points associated
|
|
177
|
+
with that label.
|
|
178
|
+
Returns:
|
|
179
|
+
tuple[list[str | int], list[int]]: list of labels and number of coincidences for each label
|
|
180
|
+
"""
|
|
181
|
+
labels: list[str | int] = [ann.label_str for image in container.images for ann in image.annotations]
|
|
182
|
+
|
|
183
|
+
label_count: dict[str | int, int] = {}
|
|
184
|
+
for label in labels:
|
|
185
|
+
label_count[label] = label_count.get(label, 0) + 1
|
|
186
|
+
labels, counts = list(label_count.keys()), list(label_count.values())
|
|
187
|
+
return labels, counts
|
|
188
|
+
|
|
189
|
+
@staticmethod
|
|
190
|
+
def process_cluster(container: DataContainer) -> tuple[list[int | str], np.ndarray]:
|
|
191
|
+
"""
|
|
192
|
+
For the k-means clustering, the images need to be flattened first
|
|
193
|
+
and then returned as a reduced vector. This method calls the pre_process_images
|
|
194
|
+
method from scikit-pca analysis to perform such preprocessing.
|
|
195
|
+
Then, perform_k_means_analysis instantiates a KMeans class from scikit-learn
|
|
196
|
+
and perform Principal Component Analysis (PCA) on the reduced vector.
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
Args:
|
|
200
|
+
container (DataContainer): Container to extract the images from
|
|
201
|
+
|
|
202
|
+
Returns:
|
|
203
|
+
list[str | int]: list of labels
|
|
204
|
+
np.ndarray : transformed values from the pca analysis
|
|
205
|
+
"""
|
|
206
|
+
images = container.images
|
|
207
|
+
feature_arr = pre_process_images(images)
|
|
208
|
+
label, counts = perform_k_means_analysis(feature_arr)
|
|
209
|
+
return label, counts
|
|
210
|
+
|
|
211
|
+
def execute(self, container: DataContainer) -> DataContainer:
|
|
212
|
+
figure = self.create_figure()
|
|
213
|
+
labels_to_plot, counts_to_plot = self.retrieve_labels_from_images(container)
|
|
214
|
+
fig_name = self.attributes.fig_name
|
|
215
|
+
|
|
216
|
+
if self.attributes.k_means_clustering:
|
|
217
|
+
plot_type = PlotTypes.CLUSTERING
|
|
218
|
+
fig_name = f"{PlotTypes.CLUSTERING}_{self.attributes.fig_name}"
|
|
219
|
+
labels, feature_vec = self.process_cluster(container)
|
|
220
|
+
figure = plot_distribution(
|
|
221
|
+
figure=figure,
|
|
222
|
+
labels=labels,
|
|
223
|
+
counts=feature_vec,
|
|
224
|
+
plot_type=plot_type,
|
|
225
|
+
kwargs=self.attributes.kwargs,
|
|
226
|
+
)
|
|
227
|
+
else:
|
|
228
|
+
for plot_type in plot_map:
|
|
229
|
+
if not getattr(self.attributes, plot_type):
|
|
230
|
+
continue
|
|
231
|
+
figure = plot_distribution(
|
|
232
|
+
figure=figure,
|
|
233
|
+
labels=labels_to_plot,
|
|
234
|
+
counts=counts_to_plot,
|
|
235
|
+
plot_type=plot_type,
|
|
236
|
+
kwargs=self.attributes.kwargs,
|
|
237
|
+
)
|
|
238
|
+
|
|
239
|
+
fig_name = f"{plot_type}_{self.attributes.fig_name}"
|
|
240
|
+
fig_path = os.path.join(self.attributes.save_image_dir, fig_name)
|
|
241
|
+
if figure:
|
|
242
|
+
plt.savefig(f"{fig_path}_{container.container_id}")
|
|
243
|
+
plt.close()
|
|
244
|
+
container.generic_data[plot_type] = figure
|
|
245
|
+
return container
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
import cv2
|
|
4
|
+
import numpy as np
|
|
5
|
+
from sinapsis_core.data_containers.annotations import ImageAnnotations, KeyPoint
|
|
6
|
+
|
|
7
|
+
from sinapsis_data_visualization.helpers.annotation_drawer_types import (
|
|
8
|
+
KeyPointAppearance,
|
|
9
|
+
)
|
|
10
|
+
from sinapsis_data_visualization.helpers.color_utils import RGB_TYPE
|
|
11
|
+
from sinapsis_data_visualization.templates.bbox_drawer import BBoxDrawer
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class KeyPointsDrawer(BBoxDrawer):
|
|
15
|
+
"""
|
|
16
|
+
A class for drawing keypoints and oriented bounding boxes on images,
|
|
17
|
+
extending from BBoxDrawer.
|
|
18
|
+
|
|
19
|
+
This class allows the addition of keypoints as annotations on images,
|
|
20
|
+
along with oriented bounding boxes.
|
|
21
|
+
It inherits from the 'BBoxDrawer' class to reuse the functionality
|
|
22
|
+
for drawing oriented bounding boxes and extends it to handle the
|
|
23
|
+
drawing of keypoints, which can be visualized with customizable
|
|
24
|
+
appearance properties.
|
|
25
|
+
|
|
26
|
+
Usage example:
|
|
27
|
+
|
|
28
|
+
agent:
|
|
29
|
+
name: my_test_agent
|
|
30
|
+
templates:
|
|
31
|
+
- template_name: InputTemplate
|
|
32
|
+
class_name: InputTemplate
|
|
33
|
+
attributes: {}
|
|
34
|
+
- template_name: KeyPointsDrawer
|
|
35
|
+
class_name: KeyPointsDrawer
|
|
36
|
+
template_input: InputTemplate
|
|
37
|
+
attributes:
|
|
38
|
+
overwrite: false
|
|
39
|
+
randomized_color: true
|
|
40
|
+
draw_confidence: true
|
|
41
|
+
draw_extra_labels: true
|
|
42
|
+
text_style:
|
|
43
|
+
font: 0
|
|
44
|
+
font_scale: 0.5
|
|
45
|
+
thickness: 2
|
|
46
|
+
draw_classification_label: false
|
|
47
|
+
classification_label_position: top_right
|
|
48
|
+
text_box_to_border_offset: 0.01
|
|
49
|
+
|
|
50
|
+
"""
|
|
51
|
+
|
|
52
|
+
def set_drawing_strategy(self) -> None:
|
|
53
|
+
"""
|
|
54
|
+
Appends the method to draw the keypoints on the image, to
|
|
55
|
+
the drawing_strategies list.
|
|
56
|
+
"""
|
|
57
|
+
self.drawing_strategies.append(self.draw_key_points_strategy)
|
|
58
|
+
|
|
59
|
+
@staticmethod
|
|
60
|
+
def draw_key_points(
|
|
61
|
+
image: np.ndarray,
|
|
62
|
+
key_points: list[KeyPoint],
|
|
63
|
+
appearance: KeyPointAppearance,
|
|
64
|
+
) -> np.ndarray:
|
|
65
|
+
"""
|
|
66
|
+
Draws keypoints from annotations on the given image.
|
|
67
|
+
|
|
68
|
+
Args:
|
|
69
|
+
image (np.ndarray): The image in which to draw the keypoints.
|
|
70
|
+
key_points (List[KeyPoint]): A list of keypoints to be drawn
|
|
71
|
+
on the image.
|
|
72
|
+
appearance (KeyPointAppearance): Defines the visual properties
|
|
73
|
+
(e.g., radius, color, thickness) of the keypoints.
|
|
74
|
+
|
|
75
|
+
Returns:
|
|
76
|
+
np.ndarray: The edited image with the keypoints drawn.
|
|
77
|
+
"""
|
|
78
|
+
image = np.array(image, dtype="uint8")
|
|
79
|
+
|
|
80
|
+
for kpt in key_points:
|
|
81
|
+
cv2.circle(
|
|
82
|
+
image,
|
|
83
|
+
center=(int(kpt.x), int(kpt.y)),
|
|
84
|
+
radius=appearance.radius,
|
|
85
|
+
color=appearance.color,
|
|
86
|
+
thickness=appearance.thickness,
|
|
87
|
+
lineType=8,
|
|
88
|
+
shift=0,
|
|
89
|
+
)
|
|
90
|
+
return image
|
|
91
|
+
|
|
92
|
+
def draw_key_points_strategy(
|
|
93
|
+
self,
|
|
94
|
+
image: np.ndarray,
|
|
95
|
+
annotation: ImageAnnotations,
|
|
96
|
+
ann_color: RGB_TYPE,
|
|
97
|
+
) -> np.ndarray:
|
|
98
|
+
"""
|
|
99
|
+
Strategy method to draw keypoints as part of the annotation process.
|
|
100
|
+
|
|
101
|
+
This method is used in the drawing strategy pipeline to draw keypoints
|
|
102
|
+
for a given annotation on the image.
|
|
103
|
+
If keypoints are present in the annotation, they will be drawn on the
|
|
104
|
+
image with the specified appearance properties.
|
|
105
|
+
|
|
106
|
+
Args:
|
|
107
|
+
image (np.ndarray): The image on which to draw the keypoints.
|
|
108
|
+
annotation (ImageAnnotations): The annotation containing the keypoints.
|
|
109
|
+
ann_color (RGB_TYPE): The color to use for drawing the keypoints.
|
|
110
|
+
|
|
111
|
+
Returns:
|
|
112
|
+
np.ndarray: The image with the keypoints drawn.
|
|
113
|
+
"""
|
|
114
|
+
|
|
115
|
+
if annotation.keypoints:
|
|
116
|
+
image = self.draw_key_points(
|
|
117
|
+
image=image,
|
|
118
|
+
key_points=annotation.keypoints,
|
|
119
|
+
appearance=KeyPointAppearance(color=ann_color),
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
return image
|