Fruits-30 / fruits30.py
VinayHajare's picture
Update fruits30.py
559d1f3
import os
import datasets
from datasets.tasks import ImageClassification
from .classes import FRUITS30_CLASSES
class fruits30(datasets.GeneratorBasedBuilder):
def _info(self):
assert len(FRUITS30_CLASSES) == 30
return datasets.DatasetInfo(
description="The fruits-30 is an image dataset for fruit image classification task."
" It contains high-quality images of 30 types of fruit with annotation using segmentation.",
features=datasets.Features(
{
"image": datasets.Image(),
"label": datasets.ClassLabel(names=list(FRUITS30_CLASSES.values())),
}
),
homepage="https://github.com/VinayHajare/Fruit-Image-Dataset",
task_templates=[ImageClassification(image_column="image", label_column="label")],
)
def _split_generators(self, dl_manager):
"""Returns SplitGenerators."""
data_dir = dl_manager.download("./FruitImageDataset")
# Only return the "train" split
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"data_dir": data_dir,
"split": "train",
},
)
]
def _generate_examples(self, data_dir, split):
"""Yields examples."""
idx = 0
for root, dirs, files in os.walk(data_dir):
for file in files:
if file.endswith(".jpg"):
# Image file path format: <IMAGE_FILENAME>_<SYNSET_ID>.JPEG
_, synset_id = os.path.splitext(file)[0].rsplit("_", 1)
label = FRUITS30_CLASSES[synset_id]
image_path = os.path.join(root, file)
with open(image_path, "rb") as image_file:
ex = {"image": {"path": image_path, "bytes": image_file.read()}, "label": label}
yield idx, ex
idx += 1