File size: 2,004 Bytes
5ab1cc0
 
 
d148b96
 
 
 
135cb72
d148b96
 
135cb72
 
 
d148b96
 
135cb72
d148b96
 
135cb72
 
d148b96
 
135cb72
 
7f7fd60
135cb72
 
 
 
 
 
 
 
 
 
 
 
9eb28c5
 
 
 
 
559d1f3
9eb28c5
 
 
 
 
 
d148b96
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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