sinapsis-data-writers 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.
File without changes
@@ -0,0 +1,26 @@
1
+ # -*- coding: utf-8 -*-
2
+ import importlib
3
+ from typing import Callable
4
+
5
+ _root_lib_path = "sinapsis_data_writers.templates"
6
+
7
+ _template_lookup = {
8
+ "BaseAnnotationWriter": f"{_root_lib_path}.annotation_writers.image_annotation_writer",
9
+ "COCOAnnotationWriter": f"{_root_lib_path}.annotation_writers.coco_annotation_writer",
10
+ "ImageSaver": f"{_root_lib_path}.image_writers.image_saver",
11
+ "PDFToImage": f"{_root_lib_path}.image_writers.pdf_to_image_converter",
12
+ "VideoWriterCV2": f"{_root_lib_path}.video_writers.video_writer_cv2",
13
+ "VideoWriterFFMPEG": f"{_root_lib_path}.video_writers.video_writer_ffmpeg",
14
+ "AudioWriterSoundfile": f"{_root_lib_path}.audio_writers.audio_writer_soundfile",
15
+ }
16
+
17
+
18
+ def __getattr__(name: str) -> Callable:
19
+ if name in _template_lookup:
20
+ module = importlib.import_module(_template_lookup[name])
21
+ return getattr(module, name)
22
+
23
+ raise AttributeError(f"template `{name}` not found in {_root_lib_path}")
24
+
25
+
26
+ __all__ = list(_template_lookup.keys())
@@ -0,0 +1,192 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+
4
+ import json
5
+ from abc import abstractmethod
6
+ from pathlib import Path
7
+ from typing import Literal, cast
8
+
9
+ from sinapsis_core.data_containers.data_packet import DataContainer, ImagePacket
10
+ from sinapsis_core.template_base import Template, TemplateAttributes, TemplateAttributeType
11
+ from sinapsis_core.utils.env_var_keys import SINAPSIS_CACHE_DIR
12
+ from sinapsis_data_readers.helpers.coco_dataclasses import CocoJsonKeys
13
+
14
+ FORMATTED_ANNOTATIONS = list[dict]
15
+
16
+
17
+ class BaseAnnotationWriter(Template): # type:ignore
18
+ """
19
+ Base Image Annotation Writer that saves annotations to a specified format.
20
+ This template defines the base classes for storing data annotations in a
21
+ structured format. It provides functionalities for accumulating and storing
22
+ annotations in JSON format, with methods for processing the data packets and
23
+ folders.
24
+ """
25
+
26
+ class AttributesBaseModel(TemplateAttributes): # type:ignore
27
+ """Attributes for the Base Annotation Writer.
28
+
29
+ Attributes:
30
+ root_dir (str): Local path for cache storage.
31
+ save_dir (str): Local path to save the JSON file.
32
+ output_file (str): Name of the annotations file.
33
+
34
+ extension (Literal['json', 'txt']): extension of the file.
35
+ """
36
+
37
+ root_dir: str = SINAPSIS_CACHE_DIR
38
+ save_dir: str
39
+ output_file: str = "annotations"
40
+ extension: Literal["json", "txt"] = "json"
41
+
42
+ def __init__(self, attributes: TemplateAttributeType) -> None:
43
+ """Initialize the annotation writer and prepared to accumulate annotations."""
44
+ super().__init__(attributes)
45
+ self.folder_annotations: dict[str, dict[str, FORMATTED_ANNOTATIONS]] = {}
46
+
47
+ @staticmethod
48
+ def image_has_annotations(image: ImagePacket) -> bool:
49
+ """
50
+ Checks whether the image packet has annotations.
51
+
52
+ Args:
53
+ image (ImagePacket): The image packet to check.
54
+
55
+ Returns:
56
+ bool: True if the image has annotations, False otherwise.
57
+ """
58
+ return image.annotations is not None
59
+
60
+ @staticmethod
61
+ def get_folder_name_from_source(image_packet: ImagePacket) -> str:
62
+ """
63
+ Extracts the folder name from the image packet's source path.
64
+
65
+ Args:
66
+ image_packet (ImagePacket): The image packet containing the source file path.
67
+
68
+ Returns:
69
+ str: The name of the folder containing the image.
70
+ """
71
+ if image_packet.source:
72
+ return Path(image_packet.source).parent.name
73
+ return "unknown_folder"
74
+
75
+ @staticmethod
76
+ def load_existing_annotations(
77
+ output_path: Path,
78
+ ) -> dict[str, FORMATTED_ANNOTATIONS]:
79
+ """
80
+ Loads existing annotations from a JSON file if it exists.
81
+
82
+ Args:
83
+ output_path (Path): Path to the existing JSON file.
84
+
85
+ Returns:
86
+ dict[str, FORMATTED_ANNOTATIONS]: The existing annotations
87
+ or an empty dictionary
88
+ if no file exists.
89
+ """
90
+ if output_path.exists():
91
+ with open(output_path, encoding="utf-8") as f:
92
+ data = json.load(f)
93
+ return cast(dict[str, FORMATTED_ANNOTATIONS], data)
94
+ return {"images": [], "annotations": []}
95
+
96
+ def save_annotations(self, all_annotations: dict[str, FORMATTED_ANNOTATIONS], folder_name: str) -> None:
97
+ """Saves annotations to a JSON file for a specified folder.
98
+
99
+ Args:
100
+ all_annotations (dict[str, FORMATTED_ANNOTATIONS]):
101
+ the dictionary with annotations from the data packets
102
+ folder_name (str): The name of the folder where the annotations file
103
+ is saved.
104
+ """
105
+ save_path = Path(self.attributes.root_dir) / self.attributes.save_dir
106
+ save_path.mkdir(parents=True, exist_ok=True)
107
+ output_file = save_path / f"{Path(self.attributes.output_file).stem}_{folder_name}.{self.attributes.extension}"
108
+ with open(output_file, "w", encoding="utf-8") as f:
109
+ json.dump(all_annotations, f, separators=(",", ":"))
110
+
111
+ @abstractmethod
112
+ def _annotations_to_format(self, image_packet: ImagePacket) -> FORMATTED_ANNOTATIONS:
113
+ """Converts image annotations to the target format.
114
+
115
+ This method should be implemented by subclasses to provide specific logic
116
+ for converting annotations in the image packet to the desired format.
117
+
118
+ Args:
119
+ image_packet (ImagePacket): The image packet containing annotations.
120
+
121
+ Returns:
122
+ FORMATTED_ANNOTATIONS: A list of formatted annotations for the image.
123
+ """
124
+
125
+ def _process_image(self, image_packet: ImagePacket, folder_name: str) -> None:
126
+ """Processes a single image packet, extracts annotations, and adds them to folder
127
+ annotations.
128
+
129
+ Args:
130
+ image_packet (ImagePacket): The image packet to process.
131
+ folder_name (str): The folder name to store annotations.
132
+ """
133
+ if not self.image_has_annotations(image_packet):
134
+ return
135
+
136
+ formatted_annotations = self._annotations_to_format(image_packet)
137
+
138
+ if not any(
139
+ img[CocoJsonKeys.IMAGE_ID] == image_packet.id for img in self.folder_annotations[folder_name]["images"]
140
+ ):
141
+ file_name = Path(image_packet.source).name
142
+ self.folder_annotations[folder_name]["images"].append(
143
+ {
144
+ CocoJsonKeys.IMAGE_ID: str(image_packet.id),
145
+ CocoJsonKeys.WIDTH: image_packet.shape[0],
146
+ CocoJsonKeys.HEIGHT: image_packet.shape[1],
147
+ CocoJsonKeys.FILE_NAME: file_name,
148
+ CocoJsonKeys.COCO_URL: "",
149
+ }
150
+ )
151
+
152
+ self.folder_annotations[folder_name]["annotations"].extend(formatted_annotations)
153
+
154
+ def _process_folder(self, container: DataContainer) -> None:
155
+ """
156
+ Processes all images within the folder and saves annotations.
157
+
158
+ Args:
159
+ container (DataContainer): The container holding image packets.
160
+ """
161
+ for image_packet in container.images:
162
+ folder_name = self.get_folder_name_from_source(image_packet)
163
+
164
+ if folder_name not in self.folder_annotations:
165
+ output_file = (
166
+ Path(self.attributes.root_dir)
167
+ / self.attributes.save_dir
168
+ / f"{Path(self.attributes.output_file).stem}_{folder_name}.{self.attributes.extension}"
169
+ )
170
+ self.folder_annotations[folder_name] = self.load_existing_annotations(output_file)
171
+
172
+ self._process_image(image_packet, folder_name)
173
+
174
+ def execute(self, container: DataContainer) -> DataContainer:
175
+ """Executes the annotation process by processing all images and saving annotations.
176
+
177
+ Args:
178
+ container (DataContainer): The container holding image packets.
179
+
180
+ Returns:
181
+ DataContainer: The processed data container.
182
+ """
183
+ if not container.images:
184
+ return container
185
+
186
+ self._process_folder(container)
187
+
188
+ current_folder_name = self.get_folder_name_from_source(container.images[-1])
189
+ if current_folder_name in self.folder_annotations:
190
+ self.save_annotations(self.folder_annotations[current_folder_name], current_folder_name)
191
+
192
+ return container
@@ -0,0 +1,290 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ from enum import Enum
4
+ from typing import Any
5
+
6
+ import cv2
7
+ import numpy as np
8
+ from pycocotools import mask as mask_util
9
+ from sinapsis_core.data_containers.annotations import ImageAnnotations, Segmentation
10
+ from sinapsis_core.data_containers.data_packet import ImagePacket
11
+ from sinapsis_core.utils.logging_utils import sinapsis_logger
12
+ from sinapsis_data_readers.helpers.coco_dataclasses import (
13
+ CocoAnnotationsKeys,
14
+ CocoJsonKeys,
15
+ )
16
+
17
+ from sinapsis_data_writers.templates.annotation_writers.base_annotation_writer import (
18
+ FORMATTED_ANNOTATIONS,
19
+ BaseAnnotationWriter,
20
+ )
21
+
22
+
23
+ class SegmentationFormat(str, Enum):
24
+ """
25
+ Enum for segmentation formats.
26
+ RLE: Key for rle annotations
27
+ POLYGON: Key for Polygon annotations
28
+ """
29
+
30
+ RLE = "rle"
31
+ POLYGON = "polygon"
32
+
33
+
34
+ class COCOAnnotationWriter(BaseAnnotationWriter):
35
+ """
36
+ Template to store annotations in COCO format in a file in the local environment
37
+ The template checks for the ImageAnnotations field in ImagePacket and extracts
38
+ those corresponding to BBoxes, keypoints, and Segmentation masks. If needed,
39
+ these annotations are converted to COCO format before being saved in the file
40
+
41
+ Usage example:
42
+
43
+ agent:
44
+ name: my_test_agent
45
+ templates:
46
+ - template_name: InputTemplate
47
+ class_name: InputTemplate
48
+ attributes: {}
49
+ - template_name: COCOAnnotationWriter
50
+ class_name: COCOAnnotationWriter
51
+ template_input: InputTemplate
52
+ attributes:
53
+ root_dir: $SINAPSIS_CACHE_DIR
54
+ save_dir: '/path/to/desired/destination'
55
+ output_file: annotations
56
+ extension: json
57
+ category_mapping: {}
58
+ segmentation_format:
59
+ - rle
60
+ visibility_threshold: 0
61
+ contour_threshold: 4
62
+
63
+ """
64
+
65
+ class AttributesBaseModel(BaseAnnotationWriter.AttributesBaseModel):
66
+ """Attributes for the COCO Annotation Writer:
67
+
68
+ Args:
69
+ category_mapping (dict[str, int]):
70
+ Mapping between category labels and their corresponding category IDs. This is required.
71
+ segmentation_format (str):
72
+ Format to save the segmentation ('polygon' or 'rle'). Defaults to 'rle'.
73
+ visibility_threshold (int):
74
+ Threshold value to determine the visibility of keypoints.
75
+ contour_threshold (int):
76
+ Minimum number of points in a contour to be considered valid.Defaults to 4.
77
+ """
78
+
79
+ category_mapping: dict[str, int]
80
+ segmentation_format: SegmentationFormat = SegmentationFormat.RLE
81
+ visibility_threshold: int = 0
82
+ contour_threshold: int = 4
83
+
84
+ @staticmethod
85
+ def _get_bbox(annotation: ImageAnnotations) -> list[float]:
86
+ """Extracts the bounding box from the annotation.
87
+
88
+ Args:
89
+ annotation (ImageAnnotations): The annotation object
90
+ containing the bounding box.
91
+
92
+ Returns:
93
+ list[float]: A list representing the bounding box in
94
+ [x, y, width, height] format.
95
+
96
+ Raises:
97
+ ValueError: If the bounding box is missing in the annotation.
98
+ """
99
+ if not annotation.bbox:
100
+ sinapsis_logger.warning("No bounding boxes in ImageAnnotations")
101
+ return []
102
+
103
+ return [
104
+ annotation.bbox.x,
105
+ annotation.bbox.y,
106
+ annotation.bbox.w,
107
+ annotation.bbox.h,
108
+ ]
109
+
110
+ @staticmethod
111
+ def _get_oriented_bbox(annotation: ImageAnnotations) -> list[float]:
112
+ """Extracts the oriented bounding box from the annotation.
113
+
114
+ Args:
115
+ annotation (ImageAnnotations): The annotation object containing
116
+ the oriented bounding box.
117
+
118
+ Returns:
119
+ list[float]|None: Optionally, a list representing the oriented
120
+ bounding box or None if not available.
121
+ """
122
+ if annotation.oriented_bbox:
123
+ return [
124
+ annotation.oriented_bbox.x1,
125
+ annotation.oriented_bbox.y1,
126
+ annotation.oriented_bbox.x2,
127
+ annotation.oriented_bbox.y2,
128
+ annotation.oriented_bbox.x3,
129
+ annotation.oriented_bbox.y3,
130
+ annotation.oriented_bbox.x4,
131
+ annotation.oriented_bbox.y4,
132
+ ]
133
+ return []
134
+
135
+ def _get_keypoints(self, annotation: ImageAnnotations) -> tuple[list[float], int]:
136
+ """Extracts keypoints from the annotation and counts valid keypoints.
137
+
138
+ Args:
139
+ annotation (Annotations): The annotation object containing keypoints.
140
+
141
+ Returns:
142
+ tuple[List[float], int]: A tuple containing a list of keypoints
143
+ in [x, y, visibility] format and the number of visible keypoints.
144
+ """
145
+ if not annotation.keypoints:
146
+ return [], 0
147
+
148
+ keypoints = []
149
+ num_keypoints = 0
150
+ for kp in annotation.keypoints:
151
+ x = max(round(kp.x, 3), 0)
152
+ y = max(round(kp.y, 3), 0)
153
+ visibility = 2 if (kp.score and kp.score > self.attributes.visibility_threshold) else 0
154
+ keypoints.extend([x, y, visibility])
155
+ if visibility > 0:
156
+ num_keypoints += 1
157
+
158
+ return keypoints, num_keypoints
159
+
160
+ def _convert_segmentation_to_desired_format(self, segmentation: Segmentation) -> list | dict:
161
+ """Converts segmentation input to the desired format (polygon or RLE).
162
+
163
+ Args:
164
+ segmentation (Segmentation): The segmentation object containing polygon or mask data.
165
+ """
166
+ if self.attributes.segmentation_format == SegmentationFormat.POLYGON:
167
+ if segmentation.polygon is not None:
168
+ return segmentation.polygon
169
+ if segmentation.mask is not None:
170
+ contours, _ = cv2.findContours(
171
+ segmentation.mask.astype(np.uint8),
172
+ cv2.RETR_EXTERNAL,
173
+ cv2.CHAIN_APPROX_SIMPLE,
174
+ )
175
+ return [
176
+ contour.flatten().tolist()
177
+ for contour in contours
178
+ if len(contour) >= self.attributes.contour_threshold
179
+ ]
180
+ elif self.attributes.segmentation_format == SegmentationFormat.RLE:
181
+ if segmentation.mask is not None:
182
+ encoded_rle = mask_util.encode(np.asfortranarray(segmentation.mask.astype(np.uint8)))
183
+ if isinstance(encoded_rle["counts"], bytes):
184
+ encoded_rle["counts"] = encoded_rle["counts"].decode("utf-8")
185
+ return encoded_rle
186
+ return []
187
+
188
+ def _get_segmentation(self, annotation: ImageAnnotations) -> list | dict:
189
+ """Returns the segmentation based on the defined format.
190
+
191
+ Args:
192
+ annotation (Annotations): The annotation object containing segmentation data.
193
+ """
194
+ if annotation.segmentation:
195
+ return self._convert_segmentation_to_desired_format(annotation.segmentation)
196
+ return []
197
+
198
+ def _get_category_id(self, label_str: str) -> int | None:
199
+ """Converts label_str to category_id, assigning -1 for unknowns.
200
+
201
+ Args:
202
+ label_str (str): The label string to be converted to category ID.
203
+ """
204
+ if label_str.isdigit():
205
+ self.logger.debug(
206
+ "Label string '%s' is numeric. Defaulting to generic category.",
207
+ label_str,
208
+ )
209
+ return -1
210
+ category_id = self.attributes.category_mapping.get(label_str, -1)
211
+ if category_id == -1:
212
+ self.logger.debug("Label string '%s' not found in category mapping. Defaulting to generic category.")
213
+ return category_id if category_id != -1 else None
214
+
215
+ def _convert_annotation(self, idx: int, annotation: ImageAnnotations, image_packet: ImagePacket) -> dict:
216
+ """Converts an annotation into the COCO format.
217
+
218
+ Args:
219
+ idx: Index of the annotation.
220
+ annotation: The annotation object containing relevant data.
221
+ image_packet: The ImagePacket object containing image metadata.
222
+
223
+ Returns:
224
+ dict: The converted annotation in COCO format with an optional oriented_bbox key.
225
+ """
226
+ bbox = self._get_bbox(annotation)
227
+ oriented_bbox = self._get_oriented_bbox(annotation)
228
+ keypoints, num_keypoints = self._get_keypoints(annotation)
229
+ segmentation = self._get_segmentation(annotation)
230
+ category_id = self._get_category_id(annotation.label_str or "")
231
+
232
+ # Constructing the COCO annotation directly
233
+ coco_annotation = {
234
+ CocoAnnotationsKeys.ANNOTATION_ID: idx + 1,
235
+ CocoAnnotationsKeys.IMAGE_ID: str(image_packet.id),
236
+ CocoAnnotationsKeys.BBOX: bbox,
237
+ CocoAnnotationsKeys.ORIENTED_BBOX: oriented_bbox,
238
+ CocoAnnotationsKeys.SEGMENTATIONS: segmentation,
239
+ CocoAnnotationsKeys.AREA: (max(0.0, annotation.area) if annotation.area else max(0.0, bbox[2] * bbox[3])),
240
+ CocoAnnotationsKeys.IS_CROWD: (annotation.is_crowd if annotation.is_crowd is not None else 0),
241
+ CocoAnnotationsKeys.KEYPOINTS: keypoints,
242
+ CocoAnnotationsKeys.NUM_KEYPOINTS: num_keypoints,
243
+ CocoAnnotationsKeys.CATEGORY_ID: (category_id if category_id is not None else -1),
244
+ }
245
+
246
+ return coco_annotation
247
+
248
+ def _annotations_to_format(self, image_packet: ImagePacket) -> FORMATTED_ANNOTATIONS:
249
+ """Converts image annotations to COCO format."""
250
+ return [
251
+ self._convert_annotation(idx, annotation, image_packet)
252
+ for idx, annotation in enumerate(image_packet.annotations)
253
+ if annotation.bbox
254
+ ]
255
+
256
+ def _get_category_definitions(self) -> list[dict]:
257
+ """Creates category definitions or COCO format."""
258
+ categories = [
259
+ {
260
+ CocoAnnotationsKeys.ANNOTATION_ID: category_id,
261
+ "name": label_str,
262
+ "supercategory": "object",
263
+ }
264
+ for label_str, category_id in self.attributes.category_mapping.items()
265
+ ]
266
+ if any(
267
+ annotation[CocoAnnotationsKeys.CATEGORY_ID] == -1
268
+ for folder_data in self.folder_annotations.values()
269
+ for annotation in folder_data[CocoJsonKeys.ANNOTATIONS]
270
+ ):
271
+ categories.append(
272
+ {
273
+ CocoAnnotationsKeys.ANNOTATION_ID: -1,
274
+ "name": "generic",
275
+ "supercategory": "object",
276
+ }
277
+ )
278
+ return categories
279
+
280
+ def save_annotations(self, all_annotations: dict[str, Any], folder_name: str) -> None:
281
+ """
282
+ Saves annotations including category definitions.
283
+
284
+ Args:
285
+ all_annotations (dict[str, Any]): full dictionary of
286
+ annotations to be saved in the file
287
+ folder_name (str): name of the folder where annotations are saved.
288
+ """
289
+ all_annotations["categories"] = self._get_category_definitions()
290
+ super().save_annotations(all_annotations, folder_name)
@@ -0,0 +1,121 @@
1
+ # -*- coding: utf-8 -*-
2
+ import os
3
+ from typing import Literal
4
+
5
+ import numpy as np
6
+ import soundfile
7
+ from sinapsis_core.data_containers.data_packet import AudioPacket, DataContainer
8
+ from sinapsis_core.template_base import Template, TemplateAttributes
9
+ from sinapsis_core.utils.env_var_keys import SINAPSIS_CACHE_DIR
10
+
11
+
12
+ class AudioWriterSoundfile(Template):
13
+ """
14
+ Template for saving audio packets to local environment
15
+ using the SoundFile library. The template includes methods
16
+ to extract the content of the Audio packet, and saving the audio
17
+ with a given extension using Soundfile
18
+
19
+ Usage example:
20
+
21
+ agent:
22
+ name: my_test_agent
23
+ templates:
24
+ - template_name: InputTemplate
25
+ class_name: InputTemplate
26
+ attributes: {}
27
+ - template_name: AudioWriterSoundfile
28
+ class_name: AudioWriterSoundfile
29
+ template_input: InputTemplate
30
+ attributes:
31
+ save_dir: '/path/to/desired/destination'
32
+ root_dir: $SINAPSIS_CACHE_DIR
33
+ extension: wav
34
+
35
+
36
+ """
37
+
38
+ class AttributesBaseModel(TemplateAttributes):
39
+ """
40
+ Attributes for configuring the audio saving process.
41
+
42
+ - save_dir (str): Directory where audio files will be saved.
43
+ - root_dir (str): Root directory for saving audio files.
44
+ Defaults to SINAPSIS_CACHE_DIR.
45
+ - extension (Literal["wav", "flac", "aif", "raw"]):
46
+ File format for saving audio files.
47
+ """
48
+
49
+ save_dir: str
50
+ root_dir: str = SINAPSIS_CACHE_DIR
51
+ extension: Literal["wav", "flac", "aif", "raw"] = "wav"
52
+
53
+ @staticmethod
54
+ def _process_audio_packet(audio_packet: AudioPacket) -> np.ndarray:
55
+ """
56
+ Flattens the audio content from an AudioPacket for saving.
57
+
58
+ Args:
59
+ audio_packet (AudioPacket): The audio packet to process.
60
+
61
+ Returns:
62
+ np.ndarray: Flattened audio data.
63
+ """
64
+ return audio_packet.content.flatten()
65
+
66
+ def _get_destination_dir(self) -> str:
67
+ """
68
+ Constructs the destination directory path and ensures it exists.
69
+
70
+ Returns:
71
+ str: Path to the destination directory.
72
+ """
73
+ destination_dir = os.path.join(self.attributes.root_dir, self.attributes.save_dir)
74
+ os.makedirs(destination_dir, exist_ok=True)
75
+ return destination_dir
76
+
77
+ def _get_full_path(self, audio_packet: AudioPacket, destination_dir: str) -> str:
78
+ """
79
+ Generates the full file path for an audio file based on its metadata.
80
+
81
+ Args:
82
+ audio_packet (AudioPacket): The audio packet to save.
83
+ destination_dir (str): Directory where the file will be saved.
84
+
85
+ Returns:
86
+ str: Full path to the destination file.
87
+ """
88
+ filename = f"{audio_packet.source}-{audio_packet.id.split('-')[0]}"
89
+ destination_path = os.path.join(destination_dir, f"{filename}.{self.attributes.extension}")
90
+ return destination_path
91
+
92
+ def _save_audio(self, audio_packet: AudioPacket, destination_path: str) -> None:
93
+ """
94
+ Saves audio data to a file using the SoundFile library.
95
+
96
+ Args:
97
+ audio_packet (AudioPacket): The audio packet containing the data to save.
98
+ destination_path (str): Full path to the destination file.
99
+ """
100
+ soundfile.write(
101
+ destination_path,
102
+ self._process_audio_packet(audio_packet),
103
+ audio_packet.sample_rate,
104
+ format=self.attributes.extension,
105
+ )
106
+
107
+ def execute(self, container: DataContainer) -> DataContainer:
108
+ """
109
+ Processes and saves all audio packets from a DataContainer.
110
+
111
+ Args:
112
+ container (DataContainer): Container holding the audio packets.
113
+
114
+ Returns:
115
+ DataContainer: The same container, unmodified, for chaining.
116
+ """
117
+ destination_dir = self._get_destination_dir()
118
+ for audio_packet in container.audios:
119
+ full_path = self._get_full_path(audio_packet, destination_dir)
120
+ self._save_audio(audio_packet, full_path)
121
+ return container