File size: 4,316 Bytes
0f4f76e
 
ae2c7cf
 
 
 
 
120a49b
ae2c7cf
 
 
 
 
 
3bd8540
 
 
ae2c7cf
120a49b
ae2c7cf
 
 
 
 
 
 
 
52525b4
ae2c7cf
 
 
 
 
 
120a49b
f1c2810
 
116c2ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120a49b
 
 
 
ae2c7cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120a49b
 
 
 
 
ae2c7cf
120a49b
 
 
 
 
 
 
 
65e20bd
116c2ad
ae2c7cf
120a49b
 
 
 
 
 
 
 
 
 
 
 
3bd8540
120a49b
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import io

import datasets
import pandas as pd

_CITATION = """\
@InProceedings{huggingface:dataset,
title = {portrait_and_26_photos},
author = {TrainingDataPro},
year = {2023}
}
"""

_DESCRIPTION = """\
Each set includes 27 photos of people. Each person provided
two types of photos: one photo in profile (portrait_1),
and 26 photos from their life (photo_1, photo_2, …, photo_26).
"""
_NAME = 'portrait_and_26_photos'

_HOMEPAGE = f"https://huggingface.co/datasets/TrainingDataPro/{_NAME}"

_LICENSE = ""

_DATA = f"https://huggingface.co/datasets/TrainingDataPro/{_NAME}/resolve/main/data/"


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

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features({
                'portrait_1': datasets.Image(),
                'photo_1': datasets.Image(),
                'photo_2': datasets.Image(),
                'photo_3': datasets.Image(),
                'photo_4': datasets.Image(),
                'photo_5': datasets.Image(),
                'photo_6': datasets.Image(),
                'photo_7': datasets.Image(),
                'photo_8': datasets.Image(),
                'photo_9': datasets.Image(),
                'photo_10': datasets.Image(),
                'photo_11': datasets.Image(),
                'photo_12': datasets.Image(),
                'photo_13': datasets.Image(),
                'photo_14': datasets.Image(),
                'photo_15': datasets.Image(),
                'photo_16': datasets.Image(),
                'photo_17': datasets.Image(),
                'photo_18': datasets.Image(),
                'photo_19': datasets.Image(),
                'photo_20': datasets.Image(),
                'photo_21': datasets.Image(),
                'photo_22': datasets.Image(),
                'photo_23': datasets.Image(),
                'photo_24': datasets.Image(),
                'photo_25': datasets.Image(),
                'photo_26': datasets.Image(),
                'worker_id': datasets.Value('string'),
                'age': datasets.Value('int8'),
                'country': datasets.Value('string'),
                'gender': datasets.Value('string')
            }),
            supervised_keys=None,
            homepage=_HOMEPAGE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        images = dl_manager.download(f"{_DATA}images.tar.gz")
        annotations = dl_manager.download(f"{_DATA}{_NAME}.csv")
        images = dl_manager.iter_archive(images)
        return [
            datasets.SplitGenerator(name=datasets.Split.TRAIN,
                                    gen_kwargs={
                                        "images": images,
                                        'annotations': annotations
                                    }),
        ]

    def _generate_examples(self, images, annotations):
        annotations_df = pd.read_csv(annotations, sep=',')
        images_data = pd.DataFrame(columns=['Link', 'Bytes'])
        for idx, (image_path, image) in enumerate(images):
            images_data.loc[idx] = {'Link': image_path, 'Bytes': image.read()}

        annotations_df = pd.merge(annotations_df, images_data)
        for idx, worker_id in enumerate(pd.unique(annotations_df['WorkerId'])):
            annotation = annotations_df.loc[annotations_df['WorkerId'] ==
                                            worker_id]
            annotation = annotation.sort_values(['Type'])
            data = {
                row[5]: {
                    'path': row[6],
                    'bytes': row[7]
                } for row in annotation.itertuples()
            }

            age = annotation.loc[annotation['Type'] ==
                                 'portrait_1']['Age'].values[0]
            country = annotation.loc[annotation['Type'] ==
                                     'portrait_1']['Country'].values[0]
            gender = annotation.loc[annotation['Type'] ==
                                    'portrait_1']['Gender'].values[0]

            data['worker_id'] = worker_id
            data['age'] = age
            data['country'] = country
            data['gender'] = gender

            yield idx, data