File size: 2,701 Bytes
f7d5758
2e6963f
ec658a9
 
 
 
 
 
 
 
7799d1d
ec658a9
7799d1d
ec658a9
 
 
 
7799d1d
ec658a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb0a703
7799d1d
 
 
eb0a703
 
 
7799d1d
 
 
 
 
 
 
 
 
 
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
import datasets
from datasets import load_dataset, DatasetInfo, Features, Value, ClassLabel, Split, SplitGenerator, GeneratorBasedBuilder, BuilderConfig, Array3D, Version
import os
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
_DRIVE_ID = "1fXgVwhdU5YGj0SPIcHxSpxkhvRh54oEH"
_URL = f"https://drive.google.com/uc?export=download&id={_DRIVE_ID}"

class PlantsDataset(GeneratorBasedBuilder):
    VERSION = datasets.Version("1.0.0")
    BUILDER_CONFIGS = [
        BuilderConfig(name="default", version=VERSION, description="Default configuration for PlantsDataset"),
    ]

    def _info(self):
        features = Features({
            "image": Array3D(dtype="uint8", shape=(None, None, 3)),  # Change to Array3D to store image arrays
            "label": ClassLabel(names=["aleo vera", "calotropis gigantea"]),
        })
        return DatasetInfo(
            description="Your dataset description",
            features=features,
            supervised_keys=("image", "label"),
            homepage="Your dataset homepage",
            citation="Citation for your dataset",
        )

    def _split_generators(self, dl_manager):
        downloaded_file = dl_manager.download_and_extract(_URL)

        return [
            SplitGenerator(
                name=Split.TRAIN,
                gen_kwargs={
                    "data_folder": os.path.join(downloaded_file, "train"),
                },
            ),
            SplitGenerator(
                name=Split.TEST,
                gen_kwargs={
                    "data_folder": os.path.join(downloaded_file, "test"),
                },
            ),
        ]

    def _generate_examples(self, data_folder):
        label_names = self.info.features['label'].names
        for label_name in label_names:
            subfolder_path = os.path.join(data_folder, label_name)
            label = label_names.index(label_name)
            for root, _, files in os.walk(subfolder_path):
                for file_name in files:
                    file_path = os.path.join(root, file_name)
                    if os.path.isfile(file_path) and file_name.lower().endswith(('.png', '.jpg', '.jpeg')):
                        try:
                            with Image.open(file_path) as image:
                                image_array = np.array(image)
                                yield file_path, {
                                    "image": image_array,  # Keep as numpy array
                                    "label": label,
                                }
                        except (IOError, OSError):
                            print(f"Skipped file {file_path}, due to an error opening the image.")