flowerfy / src /utils /file_utils.py
Toy
Fix pre-commit configuration and resolve all linting issues
5aeda0b
"""
File and directory utilities.
"""
import glob
import os
try:
from ..core.constants import IMAGES_DIR, MODELS_DIR, SUPPORTED_IMAGE_EXTENSIONS
except ImportError:
# Handle direct execution
import sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from core.constants import ( # type: ignore
IMAGES_DIR,
MODELS_DIR,
SUPPORTED_IMAGE_EXTENSIONS,
)
def get_image_files(directory: str) -> list[str]:
"""Get all image files from a directory."""
image_files = []
for ext in SUPPORTED_IMAGE_EXTENSIONS:
pattern = os.path.join(directory, f"*{ext}")
image_files.extend(glob.glob(pattern))
return image_files
def get_flower_types_from_directory(image_dir: str = IMAGES_DIR) -> list[str]:
"""Auto-detect flower types from directory structure."""
if not os.path.exists(image_dir):
return []
detected_types = []
for item in os.listdir(image_dir):
item_path = os.path.join(image_dir, item)
if os.path.isdir(item_path):
image_files = get_image_files(item_path)
if image_files: # Only add if there are images
detected_types.append(item)
return sorted(detected_types)
def count_training_images() -> tuple[int, dict]:
"""Count training images by flower type."""
if not os.path.exists(IMAGES_DIR):
return 0, {}
total_images = 0
flower_counts = {}
for flower_type in os.listdir(IMAGES_DIR):
flower_path = os.path.join(IMAGES_DIR, flower_type)
if os.path.isdir(flower_path):
image_files = get_image_files(flower_path)
count = len(image_files)
if count > 0:
flower_counts[flower_type] = count
total_images += count
return total_images, flower_counts
def get_available_trained_models() -> list[str]:
"""Get list of available trained models."""
if not os.path.exists(MODELS_DIR):
return []
models = []
for item in os.listdir(MODELS_DIR):
model_path = os.path.join(MODELS_DIR, item)
if os.path.isdir(model_path) and os.path.exists(
os.path.join(model_path, "config.json")
):
models.append(item)
return sorted(models)