DurhamTrees / plantsdataset.py
Ziyuan111's picture
Update plantsdataset.py
afc93ac verified
raw
history blame
2.93 kB
from datasets import Dataset, DatasetInfo, Features, Value, Split, GeneratorBasedBuilder
import matplotlib.pyplot as plt
import random
import os
import cv2
# Google Drive ID for your ZIP file
_DRIVE_ID = "1fXgVwhdU5YGj0SPIcHxSpxkhvRh54oEH"
_URL = f"https://drive.google.com/uc?export=download&id={_DRIVE_ID}"
class PlantsDataset(GeneratorBasedBuilder):
VERSION = "1.0.0"
def _info(self):
features = Features({
"image": Value("string"),
"label": Value("int64"),
})
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):
displayed_index = -1
for label, subfolder in enumerate(["aleo vera", "calotropis gigantea"]):
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)
if os.path.isfile(file_path):
# Display the image
displayed_index = (displayed_index + 1) % (len(self._datasets['train']) + len(self._datasets['test']))
if displayed_index < len(self._datasets['train']):
subset_name = 'train'
example = self._datasets[subset_name][displayed_index]
else:
subset_name = 'test'
example = self._datasets[subset_name][displayed_index - len(self._datasets['train'])]
image = cv2.imread(example['image'])
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.title(f"Subset: {subset_name}, Label: {example['label']}")
plt.show()
# 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()