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 = "./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, archives, split): """Yields examples.""" idx = 0 for archive in archives: for path, file in archive: if path.endswith(".JPG"): # image filepath format: _.JPEG root, _ = os.path.splitext(path) _, synset_id = os.path.basename(root).rsplit("_", 1) label = FRUITS30_CLASSES[synset_id] ex = {"image": {"path": path, "bytes": file.read()}, "label": label} yield idx, ex idx += 1