sinapsis-generic-data-tools 0.1.0__tar.gz → 0.1.2__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 (23) hide show
  1. sinapsis_generic_data_tools-0.1.2/PKG-INFO +152 -0
  2. sinapsis_generic_data_tools-0.1.2/README.md +136 -0
  3. sinapsis_generic_data_tools-0.1.2/pyproject.toml +29 -0
  4. sinapsis_generic_data_tools-0.1.2/src/sinapsis_generic_data_tools/helpers/file_downloader.py +27 -0
  5. sinapsis_generic_data_tools-0.1.2/src/sinapsis_generic_data_tools/helpers/image_color_space_converter.py +136 -0
  6. {sinapsis_generic_data_tools-0.1.0 → sinapsis_generic_data_tools-0.1.2}/src/sinapsis_generic_data_tools/templates/__init__.py +1 -0
  7. sinapsis_generic_data_tools-0.1.2/src/sinapsis_generic_data_tools/templates/image_color_conversion.py +60 -0
  8. {sinapsis_generic_data_tools-0.1.0 → sinapsis_generic_data_tools-0.1.2}/src/sinapsis_generic_data_tools/templates/mask_non_roi.py +3 -2
  9. {sinapsis_generic_data_tools-0.1.0 → sinapsis_generic_data_tools-0.1.2}/src/sinapsis_generic_data_tools/templates/packet_buffer_queue.py +2 -2
  10. sinapsis_generic_data_tools-0.1.2/src/sinapsis_generic_data_tools.egg-info/PKG-INFO +152 -0
  11. {sinapsis_generic_data_tools-0.1.0 → sinapsis_generic_data_tools-0.1.2}/src/sinapsis_generic_data_tools.egg-info/SOURCES.txt +4 -1
  12. sinapsis_generic_data_tools-0.1.2/src/sinapsis_generic_data_tools.egg-info/requires.txt +2 -0
  13. sinapsis_generic_data_tools-0.1.0/PKG-INFO +0 -670
  14. sinapsis_generic_data_tools-0.1.0/pyproject.toml +0 -19
  15. sinapsis_generic_data_tools-0.1.0/src/sinapsis_generic_data_tools/helpers/postprocess.py +0 -33
  16. sinapsis_generic_data_tools-0.1.0/src/sinapsis_generic_data_tools.egg-info/PKG-INFO +0 -670
  17. sinapsis_generic_data_tools-0.1.0/src/sinapsis_generic_data_tools.egg-info/requires.txt +0 -1
  18. {sinapsis_generic_data_tools-0.1.0 → sinapsis_generic_data_tools-0.1.2}/LICENSE +0 -0
  19. {sinapsis_generic_data_tools-0.1.0 → sinapsis_generic_data_tools-0.1.2}/setup.cfg +0 -0
  20. {sinapsis_generic_data_tools-0.1.0 → sinapsis_generic_data_tools-0.1.2}/src/sinapsis_generic_data_tools/__init__.py +0 -0
  21. {sinapsis_generic_data_tools-0.1.0 → sinapsis_generic_data_tools-0.1.2}/src/sinapsis_generic_data_tools/helpers/__init__.py +0 -0
  22. {sinapsis_generic_data_tools-0.1.0 → sinapsis_generic_data_tools-0.1.2}/src/sinapsis_generic_data_tools.egg-info/dependency_links.txt +0 -0
  23. {sinapsis_generic_data_tools-0.1.0 → sinapsis_generic_data_tools-0.1.2}/src/sinapsis_generic_data_tools.egg-info/top_level.txt +0 -0
@@ -0,0 +1,152 @@
1
+ Metadata-Version: 2.4
2
+ Name: sinapsis-generic-data-tools
3
+ Version: 0.1.2
4
+ Summary: Collection of generic data tools
5
+ Author-email: SinapsisAI <dev@sinapsis.tech>
6
+ Project-URL: Homepage, https://sinapsis.tech
7
+ Project-URL: Documentation, https://docs.sinapsis.tech/docs
8
+ Project-URL: Tutorials, https://docs.sinapsis.tech/tutorials
9
+ Project-URL: Repository, https://github.com/Sinapsis-AI/sinapsis-data-tools.git
10
+ Requires-Python: >=3.10
11
+ Description-Content-Type: text/markdown
12
+ License-File: LICENSE
13
+ Requires-Dist: requests>=2.32.3
14
+ Requires-Dist: sinapsis>=0.1.1
15
+ Dynamic: license-file
16
+
17
+ <h1 align="center">
18
+ <br>
19
+ <a href="https://sinapsis.tech/">
20
+ <img
21
+ src="https://github.com/Sinapsis-AI/brand-resources/blob/main/sinapsis_logo/4x/logo.png?raw=true"
22
+ alt="" width="300">
23
+ </a>
24
+
25
+ <br>
26
+ Sinapsis Generic Data Tools
27
+ <br>
28
+ </h1>
29
+
30
+ <h4 align="center"> Package with generic data tools for image color conversion, buffering of data packets and other useful tools for handling data</h4>
31
+
32
+ <p align="center">
33
+ <a href="#installation">🐍 Installation</a> •
34
+ <a href="#documentation">📙 Documentation</a> •
35
+ <a href="#license">🔍 License</a>
36
+ </p>
37
+
38
+ <h2 id="installation">🐍 Installation</h2>
39
+
40
+
41
+ Install using your package manager of choice. We encourage the use of <code>uv</code>
42
+
43
+ Example with <code>uv</code>:
44
+
45
+ ```bash
46
+ uv pip install sinapsis-generic-data-tools --extra-index-url https://pypi.sinapsis.tech
47
+ ```
48
+ or with raw <code>pip</code>:
49
+ ```bash
50
+ pip install sinapsis-generic-data-tools --extra-index-url https://pypi.sinapsis.tech
51
+ ```
52
+
53
+ > [!IMPORTANT]
54
+ > Templates in each package may require extra dependencies. For development, we recommend installing the package with all the optional dependencies:
55
+ >
56
+
57
+ with <code>uv</code>:
58
+
59
+ ```bash
60
+ uv pip install sinapsis-generic-data-tools[all] --extra-index-url https://pypi.sinapsis.tech
61
+ ```
62
+ or with raw <code>pip</code>:
63
+ ```bash
64
+ pip install sinapsis-generic-data-tools[all] --extra-index-url https://pypi.sinapsis.tech
65
+ ```
66
+
67
+
68
+
69
+ > [!TIP]
70
+ > Use CLI command ``` sinapsis info --all-template-names``` to show a list with all the available Template names installed with Sinapsis Data Tools.
71
+
72
+ > [!TIP]
73
+ > Use CLI command ```sinapsis info --example-template-config TEMPLATE_NAME``` to produce an example Agent config for the Template specified in ***TEMPLATE_NAME***.
74
+
75
+ For example, for ***ColorConversion*** use ```sinapsis info --example-template-config ImageColorConversion``` to produce the following example config:
76
+
77
+
78
+ ```yaml
79
+ agent:
80
+ name: my_test_agent
81
+ templates:
82
+ - template_name: InputTemplate
83
+ class_name: InputTemplate
84
+ attributes: {}
85
+ - template_name: ImageColorConversion
86
+ class_name: ImageColorConversion
87
+ template_input: InputTemplate
88
+ attributes:
89
+ target_color_space: 2
90
+ ```
91
+
92
+
93
+ <h2 id="usage">📚 Usage example</h2>
94
+ <details id='usage'><summary><strong><span style="font-size: 1.0em;"> Example agent config</span></strong></summary>
95
+ You can copy and paste the following config and run it using the sinapsis cli, changing the <code>data_dir</code> attribute in the <code>FolderImageDatasetCV2</code> and the <code>root_dir</code> attribute in the <code>ImageSaver</code> template
96
+
97
+ ```yaml
98
+ agent:
99
+ name: my_test_agent
100
+ description: agent to save image locally
101
+ templates:
102
+ - template_name: InputTemplate
103
+ class_name: InputTemplate
104
+ attributes: {}
105
+ - template_name: FolderImageDatasetCV2
106
+ class_name: FolderImageDatasetCV2
107
+ attributes:
108
+ data_dir: /path/to/image
109
+ pattern: '**/*'
110
+ batch_size: 1
111
+ load_on_init: true
112
+ label_path_index: 0
113
+ is_ground_truth: false
114
+ - template_name: ImageColorConversion
115
+ class_name: ImageColorConversion
116
+ template_input: FolderImageDatasetCV2
117
+ attributes:
118
+ target_color_space: 2
119
+ - template_name: ImageSaver
120
+ class_name: ImageSaver
121
+ template_input: ColorConversion
122
+ attributes:
123
+ save_dir: /path/to/save/dir
124
+ extension: jpg
125
+ root_dir: '/path/to/sinapsis/cache'
126
+ save_full_image: true
127
+ save_bbox_crops: false
128
+ save_mask_crops: false
129
+ min_bbox_dim: 5
130
+ ```
131
+ </details>
132
+ To run, simply use:
133
+
134
+ ```bash
135
+ sinapsis run name_of_the_config.yml
136
+ ```
137
+ **NOTE**: Make sure to update the `data_dir` attribute in the `FolderImageDatasetCV2`, and the `save_dir` and `root_dir` attributes in the `ImageSaver` templates to actual directories
138
+
139
+ <h2 id="documentation">📙 Documentation</h2>
140
+
141
+ Documentation for this and other sinapsis packages is available on the [sinapsis website](https://docs.sinapsis.tech/docs)
142
+
143
+ Tutorials for different projects within sinapsis are available at [sinapsis tutorials page](https://docs.sinapsis.tech/tutorials)
144
+
145
+ <h2 id="license">🔍 License</h2>
146
+
147
+ This project is licensed under the AGPLv3 license, which encourages open collaboration and sharing. For more details, please refer to the [LICENSE](LICENSE) file.
148
+
149
+ For commercial use, please refer to our [official Sinapsis website](https://sinapsis.tech) for information on obtaining a commercial license.
150
+
151
+
152
+
@@ -0,0 +1,136 @@
1
+ <h1 align="center">
2
+ <br>
3
+ <a href="https://sinapsis.tech/">
4
+ <img
5
+ src="https://github.com/Sinapsis-AI/brand-resources/blob/main/sinapsis_logo/4x/logo.png?raw=true"
6
+ alt="" width="300">
7
+ </a>
8
+
9
+ <br>
10
+ Sinapsis Generic Data Tools
11
+ <br>
12
+ </h1>
13
+
14
+ <h4 align="center"> Package with generic data tools for image color conversion, buffering of data packets and other useful tools for handling data</h4>
15
+
16
+ <p align="center">
17
+ <a href="#installation">🐍 Installation</a> •
18
+ <a href="#documentation">📙 Documentation</a> •
19
+ <a href="#license">🔍 License</a>
20
+ </p>
21
+
22
+ <h2 id="installation">🐍 Installation</h2>
23
+
24
+
25
+ Install using your package manager of choice. We encourage the use of <code>uv</code>
26
+
27
+ Example with <code>uv</code>:
28
+
29
+ ```bash
30
+ uv pip install sinapsis-generic-data-tools --extra-index-url https://pypi.sinapsis.tech
31
+ ```
32
+ or with raw <code>pip</code>:
33
+ ```bash
34
+ pip install sinapsis-generic-data-tools --extra-index-url https://pypi.sinapsis.tech
35
+ ```
36
+
37
+ > [!IMPORTANT]
38
+ > Templates in each package may require extra dependencies. For development, we recommend installing the package with all the optional dependencies:
39
+ >
40
+
41
+ with <code>uv</code>:
42
+
43
+ ```bash
44
+ uv pip install sinapsis-generic-data-tools[all] --extra-index-url https://pypi.sinapsis.tech
45
+ ```
46
+ or with raw <code>pip</code>:
47
+ ```bash
48
+ pip install sinapsis-generic-data-tools[all] --extra-index-url https://pypi.sinapsis.tech
49
+ ```
50
+
51
+
52
+
53
+ > [!TIP]
54
+ > Use CLI command ``` sinapsis info --all-template-names``` to show a list with all the available Template names installed with Sinapsis Data Tools.
55
+
56
+ > [!TIP]
57
+ > Use CLI command ```sinapsis info --example-template-config TEMPLATE_NAME``` to produce an example Agent config for the Template specified in ***TEMPLATE_NAME***.
58
+
59
+ For example, for ***ColorConversion*** use ```sinapsis info --example-template-config ImageColorConversion``` to produce the following example config:
60
+
61
+
62
+ ```yaml
63
+ agent:
64
+ name: my_test_agent
65
+ templates:
66
+ - template_name: InputTemplate
67
+ class_name: InputTemplate
68
+ attributes: {}
69
+ - template_name: ImageColorConversion
70
+ class_name: ImageColorConversion
71
+ template_input: InputTemplate
72
+ attributes:
73
+ target_color_space: 2
74
+ ```
75
+
76
+
77
+ <h2 id="usage">📚 Usage example</h2>
78
+ <details id='usage'><summary><strong><span style="font-size: 1.0em;"> Example agent config</span></strong></summary>
79
+ You can copy and paste the following config and run it using the sinapsis cli, changing the <code>data_dir</code> attribute in the <code>FolderImageDatasetCV2</code> and the <code>root_dir</code> attribute in the <code>ImageSaver</code> template
80
+
81
+ ```yaml
82
+ agent:
83
+ name: my_test_agent
84
+ description: agent to save image locally
85
+ templates:
86
+ - template_name: InputTemplate
87
+ class_name: InputTemplate
88
+ attributes: {}
89
+ - template_name: FolderImageDatasetCV2
90
+ class_name: FolderImageDatasetCV2
91
+ attributes:
92
+ data_dir: /path/to/image
93
+ pattern: '**/*'
94
+ batch_size: 1
95
+ load_on_init: true
96
+ label_path_index: 0
97
+ is_ground_truth: false
98
+ - template_name: ImageColorConversion
99
+ class_name: ImageColorConversion
100
+ template_input: FolderImageDatasetCV2
101
+ attributes:
102
+ target_color_space: 2
103
+ - template_name: ImageSaver
104
+ class_name: ImageSaver
105
+ template_input: ColorConversion
106
+ attributes:
107
+ save_dir: /path/to/save/dir
108
+ extension: jpg
109
+ root_dir: '/path/to/sinapsis/cache'
110
+ save_full_image: true
111
+ save_bbox_crops: false
112
+ save_mask_crops: false
113
+ min_bbox_dim: 5
114
+ ```
115
+ </details>
116
+ To run, simply use:
117
+
118
+ ```bash
119
+ sinapsis run name_of_the_config.yml
120
+ ```
121
+ **NOTE**: Make sure to update the `data_dir` attribute in the `FolderImageDatasetCV2`, and the `save_dir` and `root_dir` attributes in the `ImageSaver` templates to actual directories
122
+
123
+ <h2 id="documentation">📙 Documentation</h2>
124
+
125
+ Documentation for this and other sinapsis packages is available on the [sinapsis website](https://docs.sinapsis.tech/docs)
126
+
127
+ Tutorials for different projects within sinapsis are available at [sinapsis tutorials page](https://docs.sinapsis.tech/tutorials)
128
+
129
+ <h2 id="license">🔍 License</h2>
130
+
131
+ This project is licensed under the AGPLv3 license, which encourages open collaboration and sharing. For more details, please refer to the [LICENSE](LICENSE) file.
132
+
133
+ For commercial use, please refer to our [official Sinapsis website](https://sinapsis.tech) for information on obtaining a commercial license.
134
+
135
+
136
+
@@ -0,0 +1,29 @@
1
+ [project]
2
+ name = "sinapsis-generic-data-tools"
3
+ version = "0.1.2"
4
+ description = "Collection of generic data tools"
5
+ authors = [{ name = "SinapsisAI", email = "dev@sinapsis.tech" }]
6
+
7
+ readme = "README.md"
8
+ license-files = ["LICENSE"]
9
+
10
+ requires-python = ">=3.10"
11
+ dependencies = [
12
+ "requests>=2.32.3",
13
+ "sinapsis>=0.1.1",
14
+ ]
15
+ [build-system]
16
+ requires = ["setuptools"]
17
+ build-backend = "setuptools.build_meta"
18
+
19
+
20
+ [[tool.uv.index]]
21
+ name = "sinapsis"
22
+ url = "https://pypi.sinapsis.tech/"
23
+
24
+
25
+ [project.urls]
26
+ Homepage = "https://sinapsis.tech"
27
+ Documentation = "https://docs.sinapsis.tech/docs"
28
+ Tutorials = "https://docs.sinapsis.tech/tutorials"
29
+ Repository = "https://github.com/Sinapsis-AI/sinapsis-data-tools.git"
@@ -0,0 +1,27 @@
1
+ # -*- coding: utf-8 -*-
2
+ import os
3
+
4
+ import requests
5
+ from sinapsis_core.utils.logging_utils import sinapsis_logger
6
+
7
+
8
+ def download_file(url: str, output_path: str, description: str | None = None) -> None:
9
+ """General-purpose function to download a file from a URL.
10
+
11
+ Args:
12
+ url (str): The URL of the file to download.
13
+ output_path (str): The path to save the downloaded file.
14
+ description (Optional[str]): Description for logging purposes.
15
+ """
16
+ if os.path.exists(output_path):
17
+ return
18
+
19
+ sinapsis_logger.info(f"Downloading {description or 'file'} from {url} to {output_path}...")
20
+ response = requests.get(url, stream=True)
21
+ if response.status_code == 200:
22
+ with open(output_path, "wb") as f:
23
+ for chunk in response.iter_content(chunk_size=8192):
24
+ f.write(chunk)
25
+ sinapsis_logger.info(f"{description or 'File'} download completed successfully.")
26
+ else:
27
+ raise RuntimeError(f"Failed to download {description or 'file'}. HTTP status code: {response.status_code}")
@@ -0,0 +1,136 @@
1
+ # -*- coding: utf-8 -*-
2
+ import cv2
3
+ import torch
4
+ from numpy import ndarray
5
+ from sinapsis_core.data_containers.data_packet import ImageColor, ImagePacket
6
+ from sinapsis_core.utils.logging_utils import sinapsis_logger
7
+
8
+ color_mapping_cv = {
9
+ (ImageColor.RGB, ImageColor.BGR): cv2.COLOR_RGB2BGR,
10
+ (ImageColor.RGB, ImageColor.GRAY): cv2.COLOR_RGB2GRAY,
11
+ (ImageColor.RGB, ImageColor.RGBA): cv2.COLOR_RGB2RGBA,
12
+ (ImageColor.BGR, ImageColor.RGB): cv2.COLOR_BGR2RGB,
13
+ (ImageColor.BGR, ImageColor.GRAY): cv2.COLOR_BGR2GRAY,
14
+ (ImageColor.BGR, ImageColor.RGBA): cv2.COLOR_BGR2RGBA,
15
+ (ImageColor.GRAY, ImageColor.RGB): cv2.COLOR_GRAY2RGB,
16
+ (ImageColor.GRAY, ImageColor.BGR): cv2.COLOR_GRAY2BGR,
17
+ (ImageColor.GRAY, ImageColor.RGBA): cv2.COLOR_GRAY2RGBA,
18
+ (ImageColor.RGBA, ImageColor.RGB): cv2.COLOR_RGBA2RGB,
19
+ (ImageColor.RGBA, ImageColor.BGR): cv2.COLOR_RGBA2BGR,
20
+ (ImageColor.RGBA, ImageColor.GRAY): cv2.COLOR_RGBA2GRAY,
21
+ }
22
+
23
+
24
+ def convert_color_space(image_packet: ImagePacket, desired_color_space: ImageColor) -> ImagePacket:
25
+ """Converts the color space of the image contained in the ImagePacket to the desired color space.
26
+
27
+ Args:
28
+ image_packet (ImagePacket): The ImagePacket containing the image and its current color space.
29
+ desired_color_space (ImageColor): The target color space to which the image should be converted.
30
+
31
+ Raises:
32
+ TypeError: If the content of the ImagePacket is neither a numpy.ndarray nor a torch.Tensor.
33
+
34
+ Returns:
35
+ ImagePacket: The ImagePacket with the image converted to the desired color space.
36
+ """
37
+ current_color_space = image_packet.color_space
38
+
39
+ if current_color_space == desired_color_space:
40
+ return image_packet
41
+
42
+ if isinstance(image_packet.content, ndarray):
43
+ return convert_color_space_cv(image_packet, current_color_space, desired_color_space)
44
+ elif isinstance(image_packet.content, torch.Tensor):
45
+ return convert_color_space_torch(image_packet, current_color_space, desired_color_space)
46
+ else:
47
+ raise TypeError(f"Unsupported content type: {type(image_packet.content)}")
48
+
49
+
50
+ def convert_color_space_cv(
51
+ image_packet: ImagePacket, current_color_space: ImageColor, desired_color_space: ImageColor
52
+ ) -> ImagePacket:
53
+ """Converts the color space of the image contained in the ImagePacket using OpenCV.
54
+
55
+ Args:
56
+ image_packet (ImagePacket): The ImagePacket containing the image and its current color space.
57
+ current_color_space (ImageColor): The current color space of the image.
58
+ desired_color_space (ImageColor): The target color space to which the image should be converted.
59
+
60
+ Raises:
61
+ ValueError: If the conversion between the current and desired color spaces is not supported.
62
+
63
+ Returns:
64
+ ImagePacket: The ImagePacket with the image converted to the desired color space.
65
+ """
66
+ if (current_color_space, desired_color_space) in color_mapping_cv:
67
+ conversion_code = color_mapping_cv[(current_color_space, desired_color_space)]
68
+ try:
69
+ image_packet.content = cv2.cvtColor(image_packet.content, conversion_code)
70
+ image_packet.color_space = desired_color_space
71
+
72
+ except cv2.error:
73
+ sinapsis_logger.error(f"Invalid conversion between {current_color_space} and {desired_color_space}")
74
+
75
+ else:
76
+ raise ValueError(f"Conversion from {current_color_space} to {desired_color_space} is not supported.")
77
+
78
+ return image_packet
79
+
80
+
81
+ def convert_color_space_torch(
82
+ image_packet: ImagePacket, current_color_space: ImageColor, desired_color_space: ImageColor
83
+ ) -> ImagePacket:
84
+ """Converts the color space of the image contained in the ImagePacket using PyTorch.
85
+
86
+ Args:
87
+ image_packet (ImagePacket): The ImagePacket containing the image and its current color space.
88
+ current_color_space (ImageColor): The current color space of the image.
89
+ desired_color_space (ImageColor): The target color space to which the image should be converted.
90
+
91
+ Raises:
92
+ ValueError: If the conversion between the current and desired color spaces is not supported.
93
+
94
+ Returns:
95
+ ImagePacket: The ImagePacket with the image converted to the desired color space.
96
+ """
97
+ tensor = image_packet.content
98
+
99
+ if (current_color_space, desired_color_space) in {
100
+ (ImageColor.RGB, ImageColor.BGR),
101
+ (ImageColor.BGR, ImageColor.RGB),
102
+ }:
103
+ if tensor.shape[-1] == 3:
104
+ image_packet.content = tensor[..., [2, 1, 0]]
105
+ else:
106
+ image_packet.content = tensor.permute(2, 1, 0)
107
+ image_packet.color_space = desired_color_space
108
+
109
+ elif (current_color_space, desired_color_space) in {
110
+ (ImageColor.RGB, ImageColor.GRAY),
111
+ (ImageColor.BGR, ImageColor.GRAY),
112
+ }:
113
+ if current_color_space == ImageColor.BGR:
114
+ tensor = tensor[..., [2, 1, 0]] if tensor.shape[-1] == 3 else tensor.permute(2, 1, 0)
115
+
116
+ weights = torch.tensor([0.299, 0.587, 0.114], dtype=tensor.dtype, device=tensor.device)
117
+ if tensor.shape[-1] == 3:
118
+ image_packet.content = (tensor * weights).sum(dim=-1, keepdim=True)
119
+ else:
120
+ image_packet.content = (tensor.permute(1, 2, 0) * weights).sum(dim=-1, keepdim=True).permute(2, 0, 1)
121
+ image_packet.color_space = desired_color_space
122
+
123
+ elif (current_color_space, desired_color_space) in {
124
+ (ImageColor.GRAY, ImageColor.RGB),
125
+ (ImageColor.GRAY, ImageColor.BGR),
126
+ }:
127
+ if tensor.shape[-1] == 1:
128
+ image_packet.content = tensor.repeat(1, 1, 3)
129
+ else:
130
+ image_packet.content = tensor.repeat(3, 1, 1)
131
+ image_packet.color_space = desired_color_space
132
+
133
+ else:
134
+ raise ValueError(f"Unsupported Torch conversion: {current_color_space} -> {desired_color_space}")
135
+
136
+ return image_packet
@@ -5,6 +5,7 @@ from typing import Callable
5
5
  _root_lib_path = "sinapsis_generic_data_tools.templates"
6
6
 
7
7
  _template_lookup = {
8
+ "ImageColorConversion": f"{_root_lib_path}.image_color_conversion",
8
9
  "MaskNonROIs": f"{_root_lib_path}.mask_non_roi",
9
10
  "PacketBufferQueue": f"{_root_lib_path}.packet_buffer_queue",
10
11
  }
@@ -0,0 +1,60 @@
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 import convert_color_space
10
+
11
+
12
+ class ImageColorConversion(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(image_packet, self.attributes.target_color_space) for image_packet in container.images
57
+ ]
58
+
59
+ container.images = new_image_packets
60
+ return container
@@ -7,7 +7,8 @@ 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.template import Template, TemplateAttributes
10
+ from sinapsis_core.template_base.base_models import TemplateAttributes
11
+ from sinapsis_core.template_base.template import Template
11
12
 
12
13
 
13
14
  class RegionOfInterest(BaseModel):
@@ -71,7 +72,7 @@ class MaskNonROIs(Template):
71
72
  """
72
73
  Removed non-region of interest areas in an image, keeping only
73
74
  the regions specified in the attributes.
74
-
75
+
75
76
  Args:
76
77
  image (np.ndarray): image to be masked
77
78
  use_bitwise (bool): Flag to indicate if using cv2.bitwise_and
@@ -7,8 +7,8 @@ from typing import Literal, Type
7
7
 
8
8
  from pydantic.dataclasses import dataclass
9
9
  from sinapsis_core.data_containers.data_packet import DataContainer, Packet
10
- from sinapsis_core.template_base import (
11
- Template,
10
+ from sinapsis_core.template_base import Template
11
+ from sinapsis_core.template_base.base_models import (
12
12
  TemplateAttributes,
13
13
  TemplateAttributeType,
14
14
  )