File size: 4,484 Bytes
0b972ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6d51e2
0b972ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6d51e2
 
 
 
 
0b972ef
 
 
 
 
 
 
 
 
 
 
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# Copyright 2023 Thinh T. Duong
import os
import datasets
from glob import glob


logger = datasets.logging.get_logger(__name__)


_CITATION = """
"""
_DESCRIPTION = """
"""
_HOMEPAGE = "https://how2sign.github.io/index.html"
_REPO_URL = "https://huggingface.co/datasets/VieSignLang/how2sign-clips/resolve/main"
_URLS = {
    "meta": os.path.join(_REPO_URL, "how2sign_realigned_{split}.csv"),
    "videos": os.path.join(_REPO_URL, "{split}_{subset}/*.zip"),
}


class How2SignConfig(datasets.BuilderConfig):
    """How2Sign configuration."""

    def __init__(self, name, **kwargs):
        """
        :param name:    Name of subset.
        :param kwargs:  Arguments.
        """
        super(How2SignConfig, self).__init__(
            name=name,
            version=datasets.Version("1.0.0"),
            description=_DESCRIPTION,
            **kwargs,
        )


class How2Sign(datasets.GeneratorBasedBuilder):
    """How2Sign dataset."""
    BUILDER_CONFIGS = [
        How2SignConfig(name="raw_videos"),
        How2SignConfig(name="rgb_side_raw_videos"),
        How2SignConfig(name="rgb_front_clips"),
        How2SignConfig(name="rgb_side_clips"),
        How2SignConfig(name="2D_keypoints"),
    ]
    DEFAULT_CONFIG_NAME = "rgb_front_clips"

    def _info(self) -> datasets.DatasetInfo:
        features = datasets.Features({
            "VIDEO_ID": datasets.Value("string"),
            "VIDEO_NAME": datasets.Value("string"),
            "SENTENCE_ID": datasets.Value("string"),
            "SENTENCE_NAME": datasets.Value("string"),
            "START_REALIGNED": datasets.Value("float64"),
            "END_REALIGNED": datasets.Value("float64"),
            "SENTENCE": datasets.Value("string"),
            "VIDEO": datasets.Value("string"),
        })

        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            citation=_CITATION,
        )

    def _split_generators(
        self, dl_manager: datasets.DownloadManager
    ) -> list[datasets.SplitGenerator]:
        """
        Get splits.
        :param dl_manager:  Download manager.
        :return:            Splits.
        """
        split_dict = {
            "train": datasets.Split.TRAIN,
            "test": datasets.Split.TEST,
            "val": datasets.Split.VALIDATION,
        }

        return [
            datasets.SplitGenerator(
                name=name,
                gen_kwargs={
                    "metadata_path": dl_manager.download(
                        _URLS["meta"].format(split=split)
                    ),
                    "video_dirs": dl_manager.download_and_extract(
                        glob(
                            _URLS["videos"].format(
                                split=split,
                                subset=self.config.name,
                            )
                        )
                    ),
                },
            )
            for split, name in split_dict.items()
        ]

    def _generate_examples(
        self, metadata_path: str,
        video_dirs: list[str],
    ) -> tuple[int, dict]:
        """
        Generate examples from metadata.
        :param metadata_path:       Path to metadata.
        :param visual_dirs:         Directories of videos.
        :yield:                     Example.
        """
        split = datasets.load_dataset(
            "csv",
            data_files=metadata_path,
            split="train",
            delimeter="\t",
        )
        for i, sample in enumerate(split):
            for video_dir in video_dirs:
                if self.config.name in ["raw_videos", "rgb_side_raw_videos"]:
                    video_path = os.path.join(video_dir, sample["VIDEO_NAME"] + ".mp4")
                else:
                    video_path = os.path.join(video_dir, sample["SENTENCE_NAME"] + ".mp4")

                if os.path.exists(video_path):
                    yield i, {
                        "VIDEO_ID": sample["VIDEO_ID"],
                        "VIDEO_NAME": sample["VIDEO_NAME"],
                        "SENTENCE_ID": sample["SENTENCE_ID"],
                        "SENTENCE_NAME": sample["SENTENCE_NAME"],
                        "START_REALIGNED": sample["START_REALIGNED"],
                        "END_REALIGNED": sample["END_REALIGNED"],
                        "SENTENCE": sample["SENTENCE"],
                        "VIDEO": video_path,
                    }