File size: 2,389 Bytes
9dc041f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
352b162
9dc041f
 
 
 
 
 
 
 
4f1d9ee
37d9243
9dc041f
4f1d9ee
 
 
9dc041f
 
 
 
 
 
 
4f1d9ee
 
 
 
 
 
9dc041f
 
 
 
 
 
 
352b162
 
 
 
9dc041f
 
 
352b162
9dc041f
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
65
66
67
68
69
70
71
72
73
74
import datasets

_CITATION = """\
@InProceedings{huggingface:dataset,
title = {Shrek Detection Dataset},
author={Aurelio AI},
year={2024}
}
"""

_DESCRIPTION = """\
Demo dataset for testing Shrek detection capabilities in images.
"""
_HOMEPAGE = "https://huggingface.co/datasets/aurelio-ai/shrek-detection"

_LICENSE = ""

_REPO = "https://huggingface.co/datasets/aurelio-ai/shrek-detection"

class ImageSet(datasets.GeneratorBasedBuilder):
    """Small sample of image-text pairs"""

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    'text': datasets.Value("string"),
                    'image': datasets.Image(),
                    'is_shrek': datasets.Value("bool")
                }
            ),
            supervised_keys=None,
            homepage=_HOMEPAGE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        # iterator for train set
        images_archive = dl_manager.download(f"{_REPO}/resolve/main/images.tgz")
        image_iters = dl_manager.iter_archive(images_archive)
        # iterator for test set
        test_images_archive = dl_manager.download(f"{_REPO}/resolve/main/test_images.tgz")
        test_image_iters = dl_manager.iter_archive(test_images_archive)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "images": image_iters
                }
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "images": test_image_iters
                }
            ),
        ]

    def _generate_examples(self, images):
        """ This function returns the examples in the raw (text) form."""
        
        for idx, (filepath, image) in enumerate(images):
            filename = filepath.split('/')[-1][:-4]
            cls = filename[0] if filename[0] in ["0", "1"] else None
            if cls:
                filename = filename[1:]
            description = filename.replace('_', ' ').replace('-', ' ')
            yield idx, {
                "image": {"path": filepath, "bytes": image.read()},
                "text": description,
                "is_shrek": True if cls == "1" else False
            }