File size: 2,743 Bytes
06c63b4
 
 
 
 
 
 
 
 
 
 
 
 
 
dea2ab5
06c63b4
 
2f474c6
 
06c63b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dea2ab5
 
5c084cc
06c63b4
 
 
dea2ab5
06c63b4
 
dea2ab5
06c63b4
 
 
dea2ab5
 
06c63b4
dea2ab5
06c63b4
 
 
 
 
 
6f870be
dea2ab5
06c63b4
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
import csv
import os

import datasets

logger = datasets.logging.get_logger(__name__)

_CITATION = """"""

_DESCRIPTION = """Flickr30k filtered and translated to Persian made by Sajjad Ayoubi https://www.kaggle.com/datasets/sajjadayobi360/flickrfa"""

_DOWNLOAD_URLS = {
    "train": "https://huggingface.co./datasets/hezarai/flickr30k-fa/resolve/main/annotations_train.csv",
    "test": "https://huggingface.co./datasets/hezarai/flickr30k-fa/resolve/main/annotations_test.csv",
    "data": "https://huggingface.co./datasets/hezarai/flickr30k-fa/resolve/main/images.zip",
}

ZIP_IMAGES_DIR = "images"


class Flickr30kFaConfig(datasets.BuilderConfig):
    def __init__(self, **kwargs):
        super(Flickr30kFaConfig, self).__init__(**kwargs)


class Flickr30kFa(datasets.GeneratorBasedBuilder):
    BUILDER_CONFIGS = [
        Flickr30kFaConfig(
            name="Persian flickr30k",
            version=datasets.Version("1.0.0"),
            description=_DESCRIPTION,
        ),
    ]

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "image_path": datasets.Value("string"),
                    "label": datasets.Value("string"),
                }
            ),
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        """
        Return SplitGenerators.
        """

        train_path = dl_manager.download_and_extract(_DOWNLOAD_URLS["train"])
        test_path = dl_manager.download_and_extract(_DOWNLOAD_URLS["test"])
        archive_path = dl_manager.download(_DOWNLOAD_URLS["data"])
        images_dir = dl_manager.extract(archive_path) if not dl_manager.is_streaming else ""
        images_dir = os.path.join(images_dir, ZIP_IMAGES_DIR)

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN, gen_kwargs={"annotations_file": train_path, "images_dir": images_dir}
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST, gen_kwargs={"annotations_file": test_path, "images_dir": images_dir}
            ),
        ]

    def _generate_examples(self, annotations_file, images_dir):
        logger.info("⏳ Generating examples from = %s", annotations_file)

        with open(annotations_file, encoding="utf-8") as csv_file:
            csv_reader = csv.reader(csv_file, quotechar='"', skipinitialspace=True)

            # Skip header
            next(csv_reader, None)

            for id_, row in enumerate(csv_reader):
                filename, label = row
                image_path = os.path.join(images_dir, filename)
                yield id_, {"image_path": image_path, "label": label}