Refactor constants management to use Pydantic BaseModel for configuration

- Replaced module-level path variables in constants.py with a structured Pydantic Config class.
- Updated all relevant modules (train.py, augmentation.py, exports.py, dataset-visualiser.py, manual_run.py) to access paths through the new config structure.
- Fixed bugs related to image processing and model saving.
- Enhanced test infrastructure to accommodate the new configuration approach.

This refactor improves code maintainability and clarity by centralizing configuration management.
This commit is contained in:
Oleksandr Bezdieniezhnykh
2026-03-27 18:18:30 +02:00
parent b68c07b540
commit 142c6c4de8
106 changed files with 5706 additions and 654 deletions
+13 -13
View File
@@ -9,7 +9,7 @@ import albumentations as A
import cv2
import numpy as np
from constants import (data_images_dir, data_labels_dir, processed_images_dir, processed_labels_dir, processed_dir)
import constants
from dto.imageLabel import ImageLabel
@@ -60,8 +60,8 @@ class Augmentator:
results.append(ImageLabel(
image=img_ann.image,
labels=img_ann.labels,
image_path=os.path.join(processed_images_dir, Path(img_ann.image_path).name),
labels_path=os.path.join(processed_labels_dir, Path(img_ann.labels_path).name)
image_path=os.path.join(constants.config.processed_images_dir, Path(img_ann.image_path).name),
labels_path=os.path.join(constants.config.processed_labels_dir, Path(img_ann.labels_path).name)
)
)
for i in range(7):
@@ -72,8 +72,8 @@ class Augmentator:
img = ImageLabel(
image=res['image'],
labels=res['bboxes'],
image_path=os.path.join(processed_images_dir, f'{name}{path.suffix}'),
labels_path=os.path.join(processed_labels_dir, f'{name}.txt')
image_path=os.path.join(constants.config.processed_images_dir, f'{name}{path.suffix}'),
labels_path=os.path.join(constants.config.processed_labels_dir, f'{name}.txt')
)
results.append(img)
except Exception as e:
@@ -95,8 +95,8 @@ class Augmentator:
def augment_annotation(self, image_file):
try:
image_path = os.path.join(data_images_dir, image_file.name)
labels_path = os.path.join(data_labels_dir, f'{Path(str(image_path)).stem}.txt')
image_path = os.path.join(constants.config.data_images_dir, image_file.name)
labels_path = os.path.join(constants.config.data_labels_dir, f'{Path(str(image_path)).stem}.txt')
image = cv2.imdecode(np.fromfile(image_path, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
img_ann = ImageLabel(
@@ -115,7 +115,7 @@ class Augmentator:
f.writelines(lines)
f.close()
print(f'{datetime.now():{"%Y-%m-%d %H:%M:%S"}}: {self.total_files_processed + 1}/{self.total_to_process} : {image_file.name} has augmented')
print(f'{datetime.now():{"%Y-%m-%d %H:%M:%S"}}: {self.total_files_processed + 1}/{self.total_images_to_process} : {image_file.name} has augmented')
except Exception as e:
print(e)
self.total_files_processed += 1
@@ -126,15 +126,15 @@ class Augmentator:
self.total_files_processed = 0
if from_scratch:
shutil.rmtree(processed_dir)
shutil.rmtree(constants.config.processed_dir)
os.makedirs(processed_images_dir, exist_ok=True)
os.makedirs(processed_labels_dir, exist_ok=True)
os.makedirs(constants.config.processed_images_dir, exist_ok=True)
os.makedirs(constants.config.processed_labels_dir, exist_ok=True)
processed_images = set(f.name for f in os.scandir(processed_images_dir))
processed_images = set(f.name for f in os.scandir(constants.config.processed_images_dir))
images = []
with os.scandir(data_images_dir) as imd:
with os.scandir(constants.config.data_images_dir) as imd:
for image_file in imd:
if image_file.is_file() and image_file.name not in processed_images:
images.append(image_file)