File size: 2,676 Bytes
2ca1e9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
import datasets
import pandas as pd
from datasets import DownloadManager

class SetClassification(datasets.GeneratorBasedBuilder):
    """Set-Classification Images dataset"""

    def __init__(self, data_path, *args, **kwargs):
        super(SetClassification, self).__init__(*args, **kwargs)
        self.data_path = data_path
        self.labels = pd.read_csv(f'{self.data_path}/labels.csv')
        self.train = self.labels[self.labels['split'] == 'train']
        self.test = self.labels[self.labels['split'] == 'test']
        self.dl_manager = DownloadManager()

    def _info(self):
        return datasets.DatasetInfo(
            description='Set Classification Images dataset',
        )
    

    def _split_generators(self, dl_manager):

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    'images': [f"{self.data_path}/images/{image.filename}" for image in self.train.itertuples()],
                    'labels': {
                        'no': [image.no for image in self.train.itertuples()],
                        'shape': [image.shape for image in self.train.itertuples()],
                        'color': [image.color for image in self.train.itertuples()],
                        'shading': [image.shading for image in self.train.itertuples()]
                    }
                }
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    'images': [f"{self.data_path}/images/{image.filename}" for image in self.test.itertuples()],
                    'labels': {
                        'no': [image.no for image in self.test.itertuples()],
                        'shape': [image.shape for image in self.test.itertuples()],
                        'color': [image.color for image in self.test.itertuples()],
                        'shading': [image.shading for image in self.test.itertuples()]
                    }
                }
            )
        ]

    def _generate_examples(self, images, labels):
        for img, label in zip(images, zip(*labels.values())):
            try:
                with open(img, 'rb') as img_obj:
                    no, shape, color, shading = label
                    yield img, {
                        'image': {"path": img, "bytes": img_obj.read()},
                        'no': no,
                        'shape': shape,
                        'color': color,
                        'shading': shading
                    }
            except Exception as e:
                print(f"Error processing image {img}: {e}")