|
|
|
"""PlantsDataset |
|
|
|
Automatically generated by Colaboratory. |
|
|
|
Original file is located at |
|
https://colab.research.google.com/drive/1nkvgrtbJQaIBdnxYHl8WTpKVL_AzAzux |
|
""" |
|
|
|
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 |
|
import pandas as pd |
|
import geopandas as gpd |
|
from datasets import ( |
|
GeneratorBasedBuilder, Version, DownloadManager, SplitGenerator, Split, |
|
Features, Value, BuilderConfig, DatasetInfo |
|
) |
|
import matplotlib.pyplot as plt |
|
import seaborn as sns |
|
import csv |
|
import json |
|
from shapely.geometry import Point |
|
import base64 |
|
import matplotlib.pyplot as plt |
|
import matplotlib.image as mpimg |
|
import io |
|
import os |
|
from PIL import Image |
|
import numpy as np |
|
from datasets import DatasetInfo, Features, Value, ClassLabel, Split, SplitGenerator, GeneratorBasedBuilder, BuilderConfig |
|
from datasets import NamedSplit, Split, SplitGenerator |
|
import gdown |
|
_DRIVE_ID = "1QOFLMFCQn_RAjWf2Hbc80NZtxS2lOq3h" |
|
_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": Value("string"), |
|
"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) |
|
if not os.path.exists(subfolder_path): |
|
print(f"Expected subfolder does not exist: {subfolder_path}") |
|
continue |
|
label = label_names.index(label_name) |
|
for root, _, files in os.walk(subfolder_path): |
|
if not files: |
|
print(f"No image files found in: {root}") |
|
continue |
|
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')): |
|
image_id = os.path.splitext(file_name)[0] |
|
yield image_id, { |
|
"image": file_path, |
|
"label": label, |
|
} |
|
else: |
|
print(f"Skipped file {file_path}, since it is not an image.") |
|
|
|
plants_dataset = PlantsDataset() |
|
|
|
|
|
plants_dataset.download_and_prepare() |
|
|
|
|
|
dataset_dict = plants_dataset.as_dataset() |
|
|
|
|
|
train_dataset = dataset_dict['train'] |
|
test_dataset = dataset_dict['test'] |
|
|
|
|
|
|
|
for example in train_dataset: |
|
print(example['image'], example['label']) |