sinapsis-generic-data-tools 0.1.6__tar.gz → 0.1.7__tar.gz

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.
Files changed (24) hide show
  1. {sinapsis_generic_data_tools-0.1.6/src/sinapsis_generic_data_tools.egg-info → sinapsis_generic_data_tools-0.1.7}/PKG-INFO +1 -1
  2. {sinapsis_generic_data_tools-0.1.6 → sinapsis_generic_data_tools-0.1.7}/pyproject.toml +1 -1
  3. sinapsis_generic_data_tools-0.1.7/src/sinapsis_generic_data_tools/helpers/image_color_space_converter_cv.py +56 -0
  4. sinapsis_generic_data_tools-0.1.7/src/sinapsis_generic_data_tools/helpers/image_color_space_converter_torch.py +63 -0
  5. sinapsis_generic_data_tools-0.1.7/src/sinapsis_generic_data_tools/helpers/tags.py +16 -0
  6. sinapsis_generic_data_tools-0.1.7/src/sinapsis_generic_data_tools/templates/image_color_conversion_np.py +61 -0
  7. {sinapsis_generic_data_tools-0.1.6 → sinapsis_generic_data_tools-0.1.7}/src/sinapsis_generic_data_tools/templates/mask_non_roi.py +13 -2
  8. {sinapsis_generic_data_tools-0.1.6 → sinapsis_generic_data_tools-0.1.7}/src/sinapsis_generic_data_tools/templates/packet_buffer_queue.py +5 -0
  9. {sinapsis_generic_data_tools-0.1.6 → sinapsis_generic_data_tools-0.1.7}/src/sinapsis_generic_data_tools/templates/source_history_aggregator.py +12 -1
  10. {sinapsis_generic_data_tools-0.1.6 → sinapsis_generic_data_tools-0.1.7/src/sinapsis_generic_data_tools.egg-info}/PKG-INFO +1 -1
  11. {sinapsis_generic_data_tools-0.1.6 → sinapsis_generic_data_tools-0.1.7}/src/sinapsis_generic_data_tools.egg-info/SOURCES.txt +4 -0
  12. {sinapsis_generic_data_tools-0.1.6 → sinapsis_generic_data_tools-0.1.7}/LICENSE +0 -0
  13. {sinapsis_generic_data_tools-0.1.6 → sinapsis_generic_data_tools-0.1.7}/README.md +0 -0
  14. {sinapsis_generic_data_tools-0.1.6 → sinapsis_generic_data_tools-0.1.7}/setup.cfg +0 -0
  15. {sinapsis_generic_data_tools-0.1.6 → sinapsis_generic_data_tools-0.1.7}/src/sinapsis_generic_data_tools/__init__.py +0 -0
  16. {sinapsis_generic_data_tools-0.1.6 → sinapsis_generic_data_tools-0.1.7}/src/sinapsis_generic_data_tools/helpers/__init__.py +0 -0
  17. {sinapsis_generic_data_tools-0.1.6 → sinapsis_generic_data_tools-0.1.7}/src/sinapsis_generic_data_tools/helpers/encode_img_base64.py +0 -0
  18. {sinapsis_generic_data_tools-0.1.6 → sinapsis_generic_data_tools-0.1.7}/src/sinapsis_generic_data_tools/helpers/file_downloader.py +0 -0
  19. {sinapsis_generic_data_tools-0.1.6 → sinapsis_generic_data_tools-0.1.7}/src/sinapsis_generic_data_tools/helpers/image_color_space_converter.py +0 -0
  20. {sinapsis_generic_data_tools-0.1.6 → sinapsis_generic_data_tools-0.1.7}/src/sinapsis_generic_data_tools/templates/__init__.py +0 -0
  21. {sinapsis_generic_data_tools-0.1.6 → sinapsis_generic_data_tools-0.1.7}/src/sinapsis_generic_data_tools/templates/image_color_conversion.py +0 -0
  22. {sinapsis_generic_data_tools-0.1.6 → sinapsis_generic_data_tools-0.1.7}/src/sinapsis_generic_data_tools.egg-info/dependency_links.txt +0 -0
  23. {sinapsis_generic_data_tools-0.1.6 → sinapsis_generic_data_tools-0.1.7}/src/sinapsis_generic_data_tools.egg-info/requires.txt +0 -0
  24. {sinapsis_generic_data_tools-0.1.6 → sinapsis_generic_data_tools-0.1.7}/src/sinapsis_generic_data_tools.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sinapsis-generic-data-tools
3
- Version: 0.1.6
3
+ Version: 0.1.7
4
4
  Summary: Collection of generic data tools
5
5
  Author-email: SinapsisAI <dev@sinapsis.tech>
6
6
  Project-URL: Homepage, https://sinapsis.tech
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "sinapsis-generic-data-tools"
3
- version = "0.1.6"
3
+ version = "0.1.7"
4
4
  description = "Collection of generic data tools"
5
5
  authors = [{ name = "SinapsisAI", email = "dev@sinapsis.tech" }]
6
6
 
@@ -0,0 +1,56 @@
1
+ # -*- coding: utf-8 -*-
2
+ import cv2
3
+ from sinapsis_core.data_containers.data_packet import ImageColor, ImagePacket
4
+ from sinapsis_core.utils.logging_utils import sinapsis_logger
5
+
6
+ color_mapping_cv = {
7
+ (ImageColor.RGB, ImageColor.BGR): cv2.COLOR_RGB2BGR,
8
+ (ImageColor.RGB, ImageColor.GRAY): cv2.COLOR_RGB2GRAY,
9
+ (ImageColor.RGB, ImageColor.RGBA): cv2.COLOR_RGB2RGBA,
10
+ (ImageColor.BGR, ImageColor.RGB): cv2.COLOR_BGR2RGB,
11
+ (ImageColor.BGR, ImageColor.GRAY): cv2.COLOR_BGR2GRAY,
12
+ (ImageColor.BGR, ImageColor.RGBA): cv2.COLOR_BGR2RGBA,
13
+ (ImageColor.GRAY, ImageColor.RGB): cv2.COLOR_GRAY2RGB,
14
+ (ImageColor.GRAY, ImageColor.BGR): cv2.COLOR_GRAY2BGR,
15
+ (ImageColor.GRAY, ImageColor.RGBA): cv2.COLOR_GRAY2RGBA,
16
+ (ImageColor.RGBA, ImageColor.RGB): cv2.COLOR_RGBA2RGB,
17
+ (ImageColor.RGBA, ImageColor.BGR): cv2.COLOR_RGBA2BGR,
18
+ (ImageColor.RGBA, ImageColor.GRAY): cv2.COLOR_RGBA2GRAY,
19
+ }
20
+
21
+
22
+ def convert_color_space_cv(image_packet: ImagePacket, desired_color_space: ImageColor) -> ImagePacket:
23
+ """Converts the color space of the image contained in the ImagePacket using OpenCV.
24
+
25
+ Args:
26
+ image_packet (ImagePacket): The ImagePacket containing the image and its current color space.
27
+ desired_color_space (ImageColor): The target color space to which the image should be converted.
28
+
29
+ Raises:
30
+ ValueError: If the conversion between the current and desired color spaces is not supported.
31
+
32
+ Returns:
33
+ ImagePacket: The ImagePacket with the image converted to the desired color space.
34
+ """
35
+ current_color_space = image_packet.color_space
36
+
37
+ if current_color_space is None:
38
+ sinapsis_logger.debug("No color conversion was performed due to current color space being None.")
39
+ return image_packet
40
+
41
+ if current_color_space == desired_color_space:
42
+ return image_packet
43
+
44
+ if (current_color_space, desired_color_space) in color_mapping_cv:
45
+ conversion_code = color_mapping_cv[(current_color_space, desired_color_space)]
46
+ try:
47
+ image_packet.content = cv2.cvtColor(image_packet.content, conversion_code)
48
+ image_packet.color_space = desired_color_space
49
+
50
+ except cv2.error:
51
+ sinapsis_logger.error(f"Invalid conversion between {current_color_space} and {desired_color_space}")
52
+
53
+ else:
54
+ raise ValueError(f"Conversion from {current_color_space} to {desired_color_space} is not supported.")
55
+
56
+ return image_packet
@@ -0,0 +1,63 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ import torch
4
+ from sinapsis_core.data_containers.data_packet import ImageColor, ImagePacket
5
+ from sinapsis_core.utils.logging_utils import sinapsis_logger
6
+
7
+
8
+ def convert_color_space_torch(image_packet: ImagePacket, desired_color_space: ImageColor) -> ImagePacket:
9
+ """Converts the color space of the image contained in the ImagePacket using PyTorch.
10
+
11
+ Args:
12
+ image_packet (ImagePacket): The ImagePacket containing the image and its current color space.
13
+ desired_color_space (ImageColor): The target color space to which the image should be converted.
14
+
15
+ Raises:
16
+ ValueError: If the conversion between the current and desired color spaces is not supported.
17
+
18
+ Returns:
19
+ ImagePacket: The ImagePacket with the image converted to the desired color space.
20
+ """
21
+ tensor = image_packet.content
22
+ current_color_space = image_packet.color_space
23
+ if current_color_space is None:
24
+ sinapsis_logger.info("Image has no color space associated")
25
+ return image_packet
26
+ if (current_color_space, desired_color_space) in {
27
+ (ImageColor.RGB, ImageColor.BGR),
28
+ (ImageColor.BGR, ImageColor.RGB),
29
+ }:
30
+ if tensor.shape[-1] == 3:
31
+ image_packet.content = tensor[..., [2, 1, 0]]
32
+ else:
33
+ image_packet.content = tensor.permute(2, 1, 0)
34
+ image_packet.color_space = desired_color_space
35
+
36
+ elif (current_color_space, desired_color_space) in {
37
+ (ImageColor.RGB, ImageColor.GRAY),
38
+ (ImageColor.BGR, ImageColor.GRAY),
39
+ }:
40
+ if current_color_space == ImageColor.BGR:
41
+ tensor = tensor[..., [2, 1, 0]] if tensor.shape[-1] == 3 else tensor.permute(2, 1, 0)
42
+
43
+ weights = torch.tensor([0.299, 0.587, 0.114], dtype=tensor.dtype, device=tensor.device)
44
+ if tensor.shape[-1] == 3:
45
+ image_packet.content = (tensor * weights).sum(dim=-1, keepdim=True)
46
+ else:
47
+ image_packet.content = (tensor.permute(1, 2, 0) * weights).sum(dim=-1, keepdim=True).permute(2, 0, 1)
48
+ image_packet.color_space = desired_color_space
49
+
50
+ elif (current_color_space, desired_color_space) in {
51
+ (ImageColor.GRAY, ImageColor.RGB),
52
+ (ImageColor.GRAY, ImageColor.BGR),
53
+ }:
54
+ if tensor.shape[-1] == 1:
55
+ image_packet.content = tensor.repeat(1, 1, 3)
56
+ else:
57
+ image_packet.content = tensor.repeat(3, 1, 1)
58
+ image_packet.color_space = desired_color_space
59
+
60
+ else:
61
+ raise ValueError(f"Unsupported Torch conversion: {current_color_space} -> {desired_color_space}")
62
+
63
+ return image_packet
@@ -0,0 +1,16 @@
1
+ # -*- coding: utf-8 -*-
2
+ from enum import Enum
3
+
4
+
5
+ class Tags(Enum):
6
+ AGGREGATOR = "aggregator"
7
+ BUFFER = "buffer"
8
+ COLOR = "color"
9
+ CONVERSION = "color_conversion"
10
+ HISTORY = "history"
11
+ IMAGE = "image"
12
+ SEGMENTATION = "segmentation"
13
+ ROI = "region_of_interest"
14
+ MASK = "mask"
15
+ SPACE = "color_space"
16
+ QUEUE = "queue"
@@ -0,0 +1,61 @@
1
+ # -*- coding: utf-8 -*-
2
+ from sinapsis_core.data_containers.data_packet import (
3
+ DataContainer,
4
+ ImageColor,
5
+ )
6
+ from sinapsis_core.template_base import Template
7
+ from sinapsis_core.template_base.base_models import TemplateAttributes
8
+
9
+ from sinapsis_generic_data_tools.helpers.image_color_space_converter_cv import convert_color_space_cv
10
+
11
+
12
+ class ImageColorConversionNumpy(Template):
13
+ """A template for converting the color space of images within a DataContainer.
14
+
15
+ This template applies color space conversion to all image packets contained within a DataContainer.
16
+ The target color space is specified in the template's attributes.
17
+
18
+ Usage example:
19
+
20
+ agent:
21
+ name: my_test_agent
22
+ templates:
23
+ - template_name: InputTemplate
24
+ class_name: InputTemplate
25
+ attributes: {}
26
+ - template_name: ColorConversion
27
+ class_name: ColorConversion
28
+ template_input: InputTemplate
29
+ attributes:
30
+ target_color_space: 2
31
+ """
32
+
33
+ class AttributesBaseModel(TemplateAttributes):
34
+ """Defines the attributes required for the ColorConversion template.
35
+
36
+ Attributes:
37
+ target_color_space (ImageColor): The target color space to which all images will be converted.
38
+ - RGB = 1
39
+ - BGR = 2
40
+ - GRAY = 3
41
+ - RGBA = 4
42
+ """
43
+
44
+ target_color_space: ImageColor
45
+
46
+ def execute(self, container: DataContainer) -> DataContainer:
47
+ """Executes the color space conversion on all image packets within the provided DataContainer.
48
+
49
+ Args:
50
+ container (DataContainer): The DataContainer containing the image packets to be processed.
51
+
52
+ Returns:
53
+ DataContainer: The DataContainer with all image packets converted to the target color space.
54
+ """
55
+ new_image_packets = [
56
+ convert_color_space_cv(image_packet, self.attributes.target_color_space)
57
+ for image_packet in container.images
58
+ ]
59
+
60
+ container.images = new_image_packets
61
+ return container
@@ -7,8 +7,14 @@ import numpy as np
7
7
  from pydantic import BaseModel, ConfigDict, computed_field
8
8
  from sinapsis_core.data_containers.annotations import KeyPoint
9
9
  from sinapsis_core.data_containers.data_packet import DataContainer
10
- from sinapsis_core.template_base.base_models import TemplateAttributes
11
- from sinapsis_core.template_base.template import Template
10
+ from sinapsis_core.template_base import Template
11
+ from sinapsis_core.template_base.base_models import (
12
+ OutputTypes,
13
+ TemplateAttributes,
14
+ UIPropertiesMetadata,
15
+ )
16
+
17
+ from sinapsis_generic_data_tools.helpers.tags import Tags
12
18
 
13
19
 
14
20
  class RegionOfInterest(BaseModel):
@@ -48,6 +54,11 @@ class MaskNonROIs(Template):
48
54
 
49
55
  """
50
56
 
57
+ UIProperties = UIPropertiesMetadata(
58
+ output_type=OutputTypes.IMAGE,
59
+ tags=[Tags.SEGMENTATION, Tags.MASK, Tags.IMAGE, Tags.ROI],
60
+ )
61
+
51
62
  class AttributesBaseModel(TemplateAttributes):
52
63
  """
53
64
  Example usage:
@@ -11,8 +11,11 @@ from sinapsis_core.template_base import Template
11
11
  from sinapsis_core.template_base.base_models import (
12
12
  TemplateAttributes,
13
13
  TemplateAttributeType,
14
+ UIPropertiesMetadata,
14
15
  )
15
16
 
17
+ from sinapsis_generic_data_tools.helpers.tags import Tags
18
+
16
19
 
17
20
  @dataclass
18
21
  class PacketBuffer:
@@ -49,6 +52,8 @@ class PacketBufferQueue(Template):
49
52
 
50
53
  """
51
54
 
55
+ UIProperties = UIPropertiesMetadata(tags=[Tags.BUFFER, Tags.QUEUE])
56
+
52
57
  class AttributesBaseModel(TemplateAttributes):
53
58
  """Attributes of the template
54
59
 
@@ -3,7 +3,14 @@ from collections import deque
3
3
  from typing import Any, Literal
4
4
 
5
5
  from sinapsis_core.data_containers.data_packet import DataContainer
6
- from sinapsis_core.template_base.template import Template, TemplateAttributes, TemplateAttributeType
6
+ from sinapsis_core.template_base import Template
7
+ from sinapsis_core.template_base.base_models import (
8
+ TemplateAttributes,
9
+ TemplateAttributeType,
10
+ UIPropertiesMetadata,
11
+ )
12
+
13
+ from sinapsis_generic_data_tools.helpers.tags import Tags
7
14
 
8
15
 
9
16
  class SourceHistoryAggregator(Template):
@@ -26,6 +33,10 @@ class SourceHistoryAggregator(Template):
26
33
  context_max_length: 5
27
34
  """
28
35
 
36
+ UIProperties = UIPropertiesMetadata(
37
+ tags=[Tags.AGGREGATOR, Tags.HISTORY, Tags.QUEUE],
38
+ )
39
+
29
40
  class AttributesBaseModel(TemplateAttributes):
30
41
  packet_to_extract_from: Literal["audios", "images", "texts", "generic_data", "time_series", "binary_data"] = (
31
42
  "texts"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sinapsis-generic-data-tools
3
- Version: 0.1.6
3
+ Version: 0.1.7
4
4
  Summary: Collection of generic data tools
5
5
  Author-email: SinapsisAI <dev@sinapsis.tech>
6
6
  Project-URL: Homepage, https://sinapsis.tech
@@ -11,8 +11,12 @@ src/sinapsis_generic_data_tools/helpers/__init__.py
11
11
  src/sinapsis_generic_data_tools/helpers/encode_img_base64.py
12
12
  src/sinapsis_generic_data_tools/helpers/file_downloader.py
13
13
  src/sinapsis_generic_data_tools/helpers/image_color_space_converter.py
14
+ src/sinapsis_generic_data_tools/helpers/image_color_space_converter_cv.py
15
+ src/sinapsis_generic_data_tools/helpers/image_color_space_converter_torch.py
16
+ src/sinapsis_generic_data_tools/helpers/tags.py
14
17
  src/sinapsis_generic_data_tools/templates/__init__.py
15
18
  src/sinapsis_generic_data_tools/templates/image_color_conversion.py
19
+ src/sinapsis_generic_data_tools/templates/image_color_conversion_np.py
16
20
  src/sinapsis_generic_data_tools/templates/mask_non_roi.py
17
21
  src/sinapsis_generic_data_tools/templates/packet_buffer_queue.py
18
22
  src/sinapsis_generic_data_tools/templates/source_history_aggregator.py