stouputils 1.14.0__py3-none-any.whl → 1.14.2__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.
- stouputils/__init__.pyi +15 -0
- stouputils/_deprecated.pyi +12 -0
- stouputils/all_doctests.pyi +46 -0
- stouputils/applications/__init__.pyi +2 -0
- stouputils/applications/automatic_docs.py +3 -0
- stouputils/applications/automatic_docs.pyi +106 -0
- stouputils/applications/upscaler/__init__.pyi +3 -0
- stouputils/applications/upscaler/config.pyi +18 -0
- stouputils/applications/upscaler/image.pyi +109 -0
- stouputils/applications/upscaler/video.pyi +60 -0
- stouputils/archive.pyi +67 -0
- stouputils/backup.pyi +109 -0
- stouputils/collections.pyi +86 -0
- stouputils/continuous_delivery/__init__.pyi +5 -0
- stouputils/continuous_delivery/cd_utils.pyi +129 -0
- stouputils/continuous_delivery/github.pyi +162 -0
- stouputils/continuous_delivery/pypi.pyi +52 -0
- stouputils/continuous_delivery/pyproject.pyi +67 -0
- stouputils/continuous_delivery/stubs.pyi +39 -0
- stouputils/ctx.pyi +211 -0
- stouputils/data_science/config/get.py +51 -51
- stouputils/data_science/data_processing/image/__init__.py +66 -66
- stouputils/data_science/data_processing/image/auto_contrast.py +79 -79
- stouputils/data_science/data_processing/image/axis_flip.py +58 -58
- stouputils/data_science/data_processing/image/bias_field_correction.py +74 -74
- stouputils/data_science/data_processing/image/binary_threshold.py +73 -73
- stouputils/data_science/data_processing/image/blur.py +59 -59
- stouputils/data_science/data_processing/image/brightness.py +54 -54
- stouputils/data_science/data_processing/image/canny.py +110 -110
- stouputils/data_science/data_processing/image/clahe.py +92 -92
- stouputils/data_science/data_processing/image/common.py +30 -30
- stouputils/data_science/data_processing/image/contrast.py +53 -53
- stouputils/data_science/data_processing/image/curvature_flow_filter.py +74 -74
- stouputils/data_science/data_processing/image/denoise.py +378 -378
- stouputils/data_science/data_processing/image/histogram_equalization.py +123 -123
- stouputils/data_science/data_processing/image/invert.py +64 -64
- stouputils/data_science/data_processing/image/laplacian.py +60 -60
- stouputils/data_science/data_processing/image/median_blur.py +52 -52
- stouputils/data_science/data_processing/image/noise.py +59 -59
- stouputils/data_science/data_processing/image/normalize.py +65 -65
- stouputils/data_science/data_processing/image/random_erase.py +66 -66
- stouputils/data_science/data_processing/image/resize.py +69 -69
- stouputils/data_science/data_processing/image/rotation.py +80 -80
- stouputils/data_science/data_processing/image/salt_pepper.py +68 -68
- stouputils/data_science/data_processing/image/sharpening.py +55 -55
- stouputils/data_science/data_processing/image/shearing.py +64 -64
- stouputils/data_science/data_processing/image/threshold.py +64 -64
- stouputils/data_science/data_processing/image/translation.py +71 -71
- stouputils/data_science/data_processing/image/zoom.py +83 -83
- stouputils/data_science/data_processing/image_augmentation.py +118 -118
- stouputils/data_science/data_processing/image_preprocess.py +183 -183
- stouputils/data_science/data_processing/prosthesis_detection.py +359 -359
- stouputils/data_science/data_processing/technique.py +481 -481
- stouputils/data_science/dataset/__init__.py +45 -45
- stouputils/data_science/dataset/dataset.py +292 -292
- stouputils/data_science/dataset/dataset_loader.py +135 -135
- stouputils/data_science/dataset/grouping_strategy.py +296 -296
- stouputils/data_science/dataset/image_loader.py +100 -100
- stouputils/data_science/dataset/xy_tuple.py +696 -696
- stouputils/data_science/metric_dictionnary.py +106 -106
- stouputils/data_science/mlflow_utils.py +206 -206
- stouputils/data_science/models/abstract_model.py +149 -149
- stouputils/data_science/models/all.py +85 -85
- stouputils/data_science/models/keras/all.py +38 -38
- stouputils/data_science/models/keras/convnext.py +62 -62
- stouputils/data_science/models/keras/densenet.py +50 -50
- stouputils/data_science/models/keras/efficientnet.py +60 -60
- stouputils/data_science/models/keras/mobilenet.py +56 -56
- stouputils/data_science/models/keras/resnet.py +52 -52
- stouputils/data_science/models/keras/squeezenet.py +233 -233
- stouputils/data_science/models/keras/vgg.py +42 -42
- stouputils/data_science/models/keras/xception.py +38 -38
- stouputils/data_science/models/keras_utils/callbacks/__init__.py +20 -20
- stouputils/data_science/models/keras_utils/callbacks/colored_progress_bar.py +219 -219
- stouputils/data_science/models/keras_utils/callbacks/learning_rate_finder.py +148 -148
- stouputils/data_science/models/keras_utils/callbacks/model_checkpoint_v2.py +31 -31
- stouputils/data_science/models/keras_utils/callbacks/progressive_unfreezing.py +249 -249
- stouputils/data_science/models/keras_utils/callbacks/warmup_scheduler.py +66 -66
- stouputils/data_science/models/keras_utils/losses/__init__.py +12 -12
- stouputils/data_science/models/keras_utils/losses/next_generation_loss.py +56 -56
- stouputils/data_science/models/keras_utils/visualizations.py +416 -416
- stouputils/data_science/models/sandbox.py +116 -116
- stouputils/data_science/range_tuple.py +234 -234
- stouputils/data_science/utils.py +285 -285
- stouputils/decorators.pyi +242 -0
- stouputils/image.pyi +172 -0
- stouputils/installer/__init__.py +18 -18
- stouputils/installer/__init__.pyi +5 -0
- stouputils/installer/common.pyi +39 -0
- stouputils/installer/downloader.pyi +24 -0
- stouputils/installer/linux.py +144 -144
- stouputils/installer/linux.pyi +39 -0
- stouputils/installer/main.py +223 -223
- stouputils/installer/main.pyi +57 -0
- stouputils/installer/windows.py +136 -136
- stouputils/installer/windows.pyi +31 -0
- stouputils/io.pyi +213 -0
- stouputils/parallel.py +12 -10
- stouputils/parallel.pyi +211 -0
- stouputils/print.pyi +136 -0
- stouputils/py.typed +1 -1
- stouputils/stouputils/parallel.pyi +4 -4
- stouputils/version_pkg.pyi +15 -0
- {stouputils-1.14.0.dist-info → stouputils-1.14.2.dist-info}/METADATA +1 -1
- stouputils-1.14.2.dist-info/RECORD +171 -0
- stouputils-1.14.0.dist-info/RECORD +0 -140
- {stouputils-1.14.0.dist-info → stouputils-1.14.2.dist-info}/WHEEL +0 -0
- {stouputils-1.14.0.dist-info → stouputils-1.14.2.dist-info}/entry_points.txt +0 -0
|
@@ -1,100 +1,100 @@
|
|
|
1
|
-
"""
|
|
2
|
-
This module contains utility functions for loading image data from directories.
|
|
3
|
-
|
|
4
|
-
It provides alternatives to Keras image loading functions, focused on
|
|
5
|
-
efficient image loading, resizing, and preprocessing using PIL.
|
|
6
|
-
The main functionality allows loading images from directories into
|
|
7
|
-
numpy arrays suitable for machine learning model input.
|
|
8
|
-
"""
|
|
9
|
-
# pyright: reportUnknownMemberType=false
|
|
10
|
-
|
|
11
|
-
# Imports
|
|
12
|
-
from __future__ import annotations
|
|
13
|
-
|
|
14
|
-
import os
|
|
15
|
-
from typing import Any
|
|
16
|
-
|
|
17
|
-
import numpy as np
|
|
18
|
-
from ...decorators import handle_error, LogLevels
|
|
19
|
-
from ...parallel import multithreading
|
|
20
|
-
from ...print import warning
|
|
21
|
-
from ...io import clean_path
|
|
22
|
-
from numpy.typing import NDArray
|
|
23
|
-
from PIL import Image
|
|
24
|
-
|
|
25
|
-
# Constants
|
|
26
|
-
ALLOWLIST_FORMATS: tuple[str, ...] = tuple(ex for ex, f in Image.registered_extensions().items() if f in Image.OPEN)
|
|
27
|
-
""" List of image formats supported by PIL """
|
|
28
|
-
|
|
29
|
-
# Functions
|
|
30
|
-
def load_images_from_directory(
|
|
31
|
-
directory_path: str,
|
|
32
|
-
image_size: tuple[int, int] = (224, 224),
|
|
33
|
-
color_mode: str | None = "RGB",
|
|
34
|
-
resample: Image.Resampling = Image.Resampling.LANCZOS,
|
|
35
|
-
to_float32: bool = True,
|
|
36
|
-
**kwargs: Any
|
|
37
|
-
) -> list[tuple[NDArray[Any], str]]:
|
|
38
|
-
""" Load images from a directory using PIL instead of Keras.
|
|
39
|
-
|
|
40
|
-
This function loads all images from a directory and its subdirectories, resizes them to the specified size,
|
|
41
|
-
converts them to the specified color mode, and returns them as a list of numpy arrays.
|
|
42
|
-
Unlike Keras' image_dataset_from_directory, this function doesn't create batches or labels.
|
|
43
|
-
If directory_path is a file path, it will load that single image.
|
|
44
|
-
|
|
45
|
-
Args:
|
|
46
|
-
directory_path (str): Path to the directory containing images or a single image file
|
|
47
|
-
image_size (tuple[int, int]): Size to which images should be resized
|
|
48
|
-
color_mode (str | None): Color mode to use ("RGB" or "grayscale")
|
|
49
|
-
resample (Image.Resampling): Resampling filter to use when resizing
|
|
50
|
-
to_float32 (bool): Whether to convert the image to float32 (between 0 and 1)
|
|
51
|
-
**kwargs (Any): Additional arguments (ignored, for compatibility)
|
|
52
|
-
|
|
53
|
-
Returns:
|
|
54
|
-
list[tuple[NDArray[Any], str]]: List of tuples containing images
|
|
55
|
-
with shape (height, width, channels) and their file paths
|
|
56
|
-
"""
|
|
57
|
-
# Function to load images from a directory
|
|
58
|
-
def _load_image(img_path: str) -> tuple[NDArray[Any], str]:
|
|
59
|
-
# Open image using PIL and decorate with error handling the Image.open function
|
|
60
|
-
img: Image.Image = handle_error(
|
|
61
|
-
message=f"Failed to open image: '{img_path}'",
|
|
62
|
-
error_log=LogLevels.WARNING_TRACEBACK
|
|
63
|
-
)(Image.open)(img_path)
|
|
64
|
-
|
|
65
|
-
# Resize image with proper resampling
|
|
66
|
-
img = img.resize(image_size, resample=resample)
|
|
67
|
-
|
|
68
|
-
# If grayscale, convert to grayscale, else convert to correct color mode
|
|
69
|
-
is_grayscale: bool = color_mode is not None and color_mode.lower() == "grayscale"
|
|
70
|
-
img = img.convert("L" if is_grayscale else color_mode)
|
|
71
|
-
|
|
72
|
-
# Convert to numpy array to float32 without normalizing (not this function's job)
|
|
73
|
-
img_array: NDArray[Any] = np.array(img, dtype=np.float32) if to_float32 else np.array(img)
|
|
74
|
-
|
|
75
|
-
# Add channel dimension if grayscale
|
|
76
|
-
if is_grayscale:
|
|
77
|
-
img_array = np.expand_dims(img_array, axis=-1) # Add channel dimension, e.g. (224, 224, 1)
|
|
78
|
-
|
|
79
|
-
return img_array, img_path
|
|
80
|
-
|
|
81
|
-
# If directory_path is a file, return the image
|
|
82
|
-
if os.path.isfile(directory_path):
|
|
83
|
-
|
|
84
|
-
# Check if the file is an image
|
|
85
|
-
if any(directory_path.endswith(ext) for ext in ALLOWLIST_FORMATS):
|
|
86
|
-
return [_load_image(directory_path)]
|
|
87
|
-
|
|
88
|
-
# If the file is not an image, warn the user
|
|
89
|
-
else:
|
|
90
|
-
warning(f"File '{directory_path}' is not a supported image format")
|
|
91
|
-
return []
|
|
92
|
-
|
|
93
|
-
# Find all image files
|
|
94
|
-
image_files: list[str] = []
|
|
95
|
-
for root, _, files in os.walk(directory_path):
|
|
96
|
-
image_files.extend(clean_path(f"{root}/{f}") for f in files if f.endswith(ALLOWLIST_FORMATS))
|
|
97
|
-
|
|
98
|
-
# Load and process images in parallel
|
|
99
|
-
return multithreading(_load_image, image_files)
|
|
100
|
-
|
|
1
|
+
"""
|
|
2
|
+
This module contains utility functions for loading image data from directories.
|
|
3
|
+
|
|
4
|
+
It provides alternatives to Keras image loading functions, focused on
|
|
5
|
+
efficient image loading, resizing, and preprocessing using PIL.
|
|
6
|
+
The main functionality allows loading images from directories into
|
|
7
|
+
numpy arrays suitable for machine learning model input.
|
|
8
|
+
"""
|
|
9
|
+
# pyright: reportUnknownMemberType=false
|
|
10
|
+
|
|
11
|
+
# Imports
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import os
|
|
15
|
+
from typing import Any
|
|
16
|
+
|
|
17
|
+
import numpy as np
|
|
18
|
+
from ...decorators import handle_error, LogLevels
|
|
19
|
+
from ...parallel import multithreading
|
|
20
|
+
from ...print import warning
|
|
21
|
+
from ...io import clean_path
|
|
22
|
+
from numpy.typing import NDArray
|
|
23
|
+
from PIL import Image
|
|
24
|
+
|
|
25
|
+
# Constants
|
|
26
|
+
ALLOWLIST_FORMATS: tuple[str, ...] = tuple(ex for ex, f in Image.registered_extensions().items() if f in Image.OPEN)
|
|
27
|
+
""" List of image formats supported by PIL """
|
|
28
|
+
|
|
29
|
+
# Functions
|
|
30
|
+
def load_images_from_directory(
|
|
31
|
+
directory_path: str,
|
|
32
|
+
image_size: tuple[int, int] = (224, 224),
|
|
33
|
+
color_mode: str | None = "RGB",
|
|
34
|
+
resample: Image.Resampling = Image.Resampling.LANCZOS,
|
|
35
|
+
to_float32: bool = True,
|
|
36
|
+
**kwargs: Any
|
|
37
|
+
) -> list[tuple[NDArray[Any], str]]:
|
|
38
|
+
""" Load images from a directory using PIL instead of Keras.
|
|
39
|
+
|
|
40
|
+
This function loads all images from a directory and its subdirectories, resizes them to the specified size,
|
|
41
|
+
converts them to the specified color mode, and returns them as a list of numpy arrays.
|
|
42
|
+
Unlike Keras' image_dataset_from_directory, this function doesn't create batches or labels.
|
|
43
|
+
If directory_path is a file path, it will load that single image.
|
|
44
|
+
|
|
45
|
+
Args:
|
|
46
|
+
directory_path (str): Path to the directory containing images or a single image file
|
|
47
|
+
image_size (tuple[int, int]): Size to which images should be resized
|
|
48
|
+
color_mode (str | None): Color mode to use ("RGB" or "grayscale")
|
|
49
|
+
resample (Image.Resampling): Resampling filter to use when resizing
|
|
50
|
+
to_float32 (bool): Whether to convert the image to float32 (between 0 and 1)
|
|
51
|
+
**kwargs (Any): Additional arguments (ignored, for compatibility)
|
|
52
|
+
|
|
53
|
+
Returns:
|
|
54
|
+
list[tuple[NDArray[Any], str]]: List of tuples containing images
|
|
55
|
+
with shape (height, width, channels) and their file paths
|
|
56
|
+
"""
|
|
57
|
+
# Function to load images from a directory
|
|
58
|
+
def _load_image(img_path: str) -> tuple[NDArray[Any], str]:
|
|
59
|
+
# Open image using PIL and decorate with error handling the Image.open function
|
|
60
|
+
img: Image.Image = handle_error(
|
|
61
|
+
message=f"Failed to open image: '{img_path}'",
|
|
62
|
+
error_log=LogLevels.WARNING_TRACEBACK
|
|
63
|
+
)(Image.open)(img_path)
|
|
64
|
+
|
|
65
|
+
# Resize image with proper resampling
|
|
66
|
+
img = img.resize(image_size, resample=resample)
|
|
67
|
+
|
|
68
|
+
# If grayscale, convert to grayscale, else convert to correct color mode
|
|
69
|
+
is_grayscale: bool = color_mode is not None and color_mode.lower() == "grayscale"
|
|
70
|
+
img = img.convert("L" if is_grayscale else color_mode)
|
|
71
|
+
|
|
72
|
+
# Convert to numpy array to float32 without normalizing (not this function's job)
|
|
73
|
+
img_array: NDArray[Any] = np.array(img, dtype=np.float32) if to_float32 else np.array(img)
|
|
74
|
+
|
|
75
|
+
# Add channel dimension if grayscale
|
|
76
|
+
if is_grayscale:
|
|
77
|
+
img_array = np.expand_dims(img_array, axis=-1) # Add channel dimension, e.g. (224, 224, 1)
|
|
78
|
+
|
|
79
|
+
return img_array, img_path
|
|
80
|
+
|
|
81
|
+
# If directory_path is a file, return the image
|
|
82
|
+
if os.path.isfile(directory_path):
|
|
83
|
+
|
|
84
|
+
# Check if the file is an image
|
|
85
|
+
if any(directory_path.endswith(ext) for ext in ALLOWLIST_FORMATS):
|
|
86
|
+
return [_load_image(directory_path)]
|
|
87
|
+
|
|
88
|
+
# If the file is not an image, warn the user
|
|
89
|
+
else:
|
|
90
|
+
warning(f"File '{directory_path}' is not a supported image format")
|
|
91
|
+
return []
|
|
92
|
+
|
|
93
|
+
# Find all image files
|
|
94
|
+
image_files: list[str] = []
|
|
95
|
+
for root, _, files in os.walk(directory_path):
|
|
96
|
+
image_files.extend(clean_path(f"{root}/{f}") for f in files if f.endswith(ALLOWLIST_FORMATS))
|
|
97
|
+
|
|
98
|
+
# Load and process images in parallel
|
|
99
|
+
return multithreading(_load_image, image_files)
|
|
100
|
+
|