File size: 2,282 Bytes
3e346a2
 
 
 
 
b24c04f
 
3e346a2
b24c04f
3e346a2
 
b24c04f
 
3e346a2
5aeda0b
 
 
 
 
b24c04f
3e346a2
b24c04f
3e346a2
 
 
 
 
 
 
b24c04f
 
3e346a2
 
 
b24c04f
3e346a2
 
 
 
 
 
 
b24c04f
3e346a2
 
b24c04f
 
3e346a2
 
 
b24c04f
3e346a2
 
b24c04f
3e346a2
 
 
 
 
 
 
 
b24c04f
3e346a2
 
b24c04f
 
3e346a2
 
 
b24c04f
3e346a2
 
 
b24c04f
 
 
3e346a2
b24c04f
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
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)