Upload plantsdataset.py
Browse files- plantsdataset.py +110 -0
plantsdataset.py
ADDED
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""PlantsDataset
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1nkvgrtbJQaIBdnxYHl8WTpKVL_AzAzux
|
8 |
+
"""
|
9 |
+
|
10 |
+
import datasets
|
11 |
+
from datasets import load_dataset, DatasetInfo, Features, Value, ClassLabel, Split, SplitGenerator, GeneratorBasedBuilder, BuilderConfig, Array3D, Version
|
12 |
+
import os
|
13 |
+
from PIL import Image
|
14 |
+
import matplotlib.pyplot as plt
|
15 |
+
import numpy as np
|
16 |
+
import pandas as pd
|
17 |
+
import geopandas as gpd
|
18 |
+
from datasets import (
|
19 |
+
GeneratorBasedBuilder, Version, DownloadManager, SplitGenerator, Split,
|
20 |
+
Features, Value, BuilderConfig, DatasetInfo
|
21 |
+
)
|
22 |
+
import matplotlib.pyplot as plt
|
23 |
+
import seaborn as sns
|
24 |
+
import csv
|
25 |
+
import json
|
26 |
+
from shapely.geometry import Point
|
27 |
+
import base64
|
28 |
+
import matplotlib.pyplot as plt
|
29 |
+
import matplotlib.image as mpimg
|
30 |
+
import io
|
31 |
+
import os
|
32 |
+
from PIL import Image
|
33 |
+
import numpy as np
|
34 |
+
from datasets import DatasetInfo, Features, Value, ClassLabel, Split, SplitGenerator, GeneratorBasedBuilder, BuilderConfig
|
35 |
+
from datasets import NamedSplit, Split, SplitGenerator
|
36 |
+
import gdown
|
37 |
+
_DRIVE_ID = "1fXgVwhdU5YGj0SPIcHxSpxkhvRh54oEH"
|
38 |
+
_URL = f"https://drive.google.com/uc?export=download&id={_DRIVE_ID}"
|
39 |
+
|
40 |
+
class PlantsDataset(GeneratorBasedBuilder):
|
41 |
+
VERSION = datasets.Version("1.0.0")
|
42 |
+
BUILDER_CONFIGS = [
|
43 |
+
BuilderConfig(name="default", version=VERSION, description="Default configuration for PlantsDataset"),
|
44 |
+
]
|
45 |
+
|
46 |
+
def _info(self):
|
47 |
+
features = Features({
|
48 |
+
"image": Value("string"), # Change to Array3D to store image arrays
|
49 |
+
"label": ClassLabel(names=["aleo vera", "calotropis gigantea"]),
|
50 |
+
})
|
51 |
+
return DatasetInfo(
|
52 |
+
description="Your dataset description",
|
53 |
+
features=features,
|
54 |
+
supervised_keys=("image", "label"),
|
55 |
+
homepage="Your dataset homepage",
|
56 |
+
citation="Citation for your dataset",
|
57 |
+
)
|
58 |
+
|
59 |
+
def _split_generators(self, dl_manager):
|
60 |
+
downloaded_file = dl_manager.download_and_extract(_URL)
|
61 |
+
|
62 |
+
return [
|
63 |
+
SplitGenerator(
|
64 |
+
name=Split.TRAIN,
|
65 |
+
gen_kwargs={
|
66 |
+
"data_folder": os.path.join(downloaded_file, "train"),
|
67 |
+
},
|
68 |
+
),
|
69 |
+
SplitGenerator(
|
70 |
+
name=Split.TEST,
|
71 |
+
gen_kwargs={
|
72 |
+
"data_folder": os.path.join(downloaded_file, "test"),
|
73 |
+
},
|
74 |
+
),
|
75 |
+
]
|
76 |
+
|
77 |
+
def _generate_examples(self, data_folder):
|
78 |
+
label_names = self.info.features['label'].names
|
79 |
+
for label_name in label_names:
|
80 |
+
subfolder_path = os.path.join(data_folder, label_name)
|
81 |
+
label = label_names.index(label_name)
|
82 |
+
for root, _, files in os.walk(subfolder_path):
|
83 |
+
for file_name in files:
|
84 |
+
file_path = os.path.join(root, file_name)
|
85 |
+
if os.path.isfile(file_path) and file_name.lower().endswith(('.png', '.jpg', '.jpeg')):
|
86 |
+
# Image ID should be unique, use filename for simplicity
|
87 |
+
image_id = os.path.splitext(file_name)[0]
|
88 |
+
yield image_id, {
|
89 |
+
"image": file_path, # Store file path as a string
|
90 |
+
"label": label,
|
91 |
+
}
|
92 |
+
else:
|
93 |
+
print(f"Skipped file {file_path}, since it is not an image.")
|
94 |
+
# Instantiate the dataset builder for PlantsDataset
|
95 |
+
plants_dataset = PlantsDataset()
|
96 |
+
|
97 |
+
# Download the data and prepare the dataset
|
98 |
+
plants_dataset.download_and_prepare()
|
99 |
+
|
100 |
+
# Access the dataset as a `DatasetDict`
|
101 |
+
dataset_dict = plants_dataset.as_dataset()
|
102 |
+
|
103 |
+
# Access the train and test splits
|
104 |
+
train_dataset = dataset_dict['train']
|
105 |
+
test_dataset = dataset_dict['test']
|
106 |
+
|
107 |
+
# Now you can use `train_dataset` and `test_dataset` as needed
|
108 |
+
# For example, you can iterate over the dataset and access the file paths and labels
|
109 |
+
for example in train_dataset:
|
110 |
+
print(example['image'], example['label'])
|