File size: 2,621 Bytes
eb4d631
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-
"""PlantsDataset

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/13QeVLQP2QGaxkGcaoMi25geoBxHQ_3rQ
"""

from datasets import Dataset, DatasetInfo, Features, Value, Split, DownloadManager, GeneratorBasedBuilder
from datasets import SplitGenerator
# URL for the datasethttps://drive.google.com/file/d/1fXgVwhdU5YGj0SPIcHxSpxkhvRh54oEH/view?usp=share_link
import os
from datasets import Dataset, DatasetInfo, Features, Value, Split, GeneratorBasedBuilder

# Google Drive ID for your ZIP file
_DRIVE_ID = "1fXgVwhdU5YGj0SPIcHxSpxkhvRh54oEH"
_URL = f"https://drive.google.com/uc?export=download&id={_DRIVE_ID}"

# Custom Dataset Class
class PlantsDataset(GeneratorBasedBuilder):
    VERSION = "1.0.0"

    def _info(self):
        features = Features({
            "image": Value("string"),  # Replace with the actual datatype for your image data
            "label": Value("int64"),   # Replace with the actual datatype for your label data
        })
        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):
      for label, subfolder in enumerate(["gigantic", "small"]):
          subfolder_path = os.path.join(data_folder, subfolder)
          for root, _, files in os.walk(subfolder_path):
              for file_name in files:
                  file_path = os.path.join(root, file_name)

                  # Check if file_path is a file, not a directory
                  if os.path.isfile(file_path):
                      # Yield the example
                      yield {
                          "image": file_path,
                          "label": label,
                      }


# Create an instance of the PlantsDataset class
plants_dataset = PlantsDataset()

# Build and upload the dataset
plants_dataset.download_and_prepare()