Spaces:
Sleeping

File size: 1,894 Bytes
ef7c84a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import argparse
from pathlib import Path

import cv2
import numpy as np

categories = ["daisy", "dandelion", "rose", "sunflower", "tulip", "bellflower", "lotus"]
target_size = (150, 150)

def pixel_converter(dataset_dir):
    resized_pixels = []
    labelled = []

    for category in categories:
        folder = dataset_dir / category
        if not folder.is_dir():
            raise FileNotFoundError(
                f"Missing category folder: {folder}\n"
                "Download/extract the raw flower images and pass their category root "
                "with --dataset-dir. Expected layout: <dataset-dir>/<category>/image.jpg"
            )
        count_in_category = 0

        for file_path in folder.iterdir():
            read_file = cv2.imread(str(file_path))

            if read_file is None:
                print(f"Skipping unreadable file: {file_path}")
                continue

            resized_image = cv2.resize(read_file, target_size)
            resized_pixels.append(resized_image)

            category_index = categories.index(category)
            labelled.append(category_index)

            count_in_category += 1

        print(f"{category}: {count_in_category} images processed")

    return resized_pixels, labelled


parser = argparse.ArgumentParser(description="Convert flower images into an NPZ dataset.")
parser.add_argument(
    "--dataset-dir",
    type=Path,
    default=Path(__file__).resolve().parent / "flowers" / "flowers",
    help="Folder containing one subfolder per flower category.",
)
args = parser.parse_args()

# Run the extraction
resized_pixels, labelled = pixel_converter(args.dataset_dir)

# Convert to numpy arrays
X = np.array(resized_pixels)
Y = np.array(labelled)


# Save both arrays together in the NPZ format used by the training notebook.
np.savez("flowers_data.npz", X=X, Y=Y)
print("Saved flowers_data.npz successfully.")