tanthinhdt commited on
Commit
5f1e8a2
·
1 Parent(s): b6e0d03

chore: add builder

Browse files
Files changed (1) hide show
  1. how2sign-clips.py +122 -0
how2sign-clips.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2023 Thinh T. Duong
2
+ import os
3
+ import datasets
4
+
5
+
6
+ logger = datasets.logging.get_logger(__name__)
7
+
8
+
9
+ _CITATION = """
10
+
11
+ """
12
+ _DESCRIPTION = """
13
+ This dataset contain short clips of Vietnamese speakers.
14
+ """
15
+ _HOMEPAGE = "https://how2sign.github.io/index.html"
16
+ _REPO_URL = "https://huggingface.co/VieSignLang/how2sign-clips/resolve/main"
17
+ _URLS = {
18
+ "meta": f"{_REPO_URL}/metadata/" + "{split}.parquet",
19
+ "video": f"{_REPO_URL}/videos/" + "{type}/{split}.zip",
20
+ }
21
+
22
+
23
+ class How2SignClipsConfig(datasets.BuilderConfig):
24
+ """Vietnamese Speaker Clip configuration."""
25
+
26
+ def __init__(self, name, **kwargs):
27
+ """
28
+ :param name: Name of subset.
29
+ :param kwargs: Arguments.
30
+ """
31
+ super(How2SignClipsConfig, self).__init__(
32
+ name=name,
33
+ version=datasets.Version("1.0.0"),
34
+ description=_DESCRIPTION,
35
+ **kwargs,
36
+ )
37
+
38
+
39
+ class How2SignClips(datasets.GeneratorBasedBuilder):
40
+ """Vietnamese Speaker Clip dataset."""
41
+ BUILDER_CONFIGS = [
42
+ How2SignClipsConfig(name="rgb"),
43
+ How2SignClipsConfig(name="keypoints"),
44
+ ]
45
+ DEFAULT_CONFIG_NAME = "rgb"
46
+
47
+ def _info(self) -> datasets.DatasetInfo:
48
+ features = datasets.Features({
49
+ "id": datasets.Value("string"),
50
+ "type": datasets.Value("string"),
51
+ "view": datasets.Value("string"),
52
+ "text": datasets.Value("string"),
53
+ "video": datasets.Value("large_binary"),
54
+ })
55
+
56
+ return datasets.DatasetInfo(
57
+ description=_DESCRIPTION,
58
+ features=features,
59
+ homepage=_HOMEPAGE,
60
+ citation=_CITATION,
61
+ )
62
+
63
+ def _split_generators(
64
+ self, dl_manager: datasets.DownloadManager
65
+ ) -> list[datasets.SplitGenerator]:
66
+ """
67
+ Get splits.
68
+ :param dl_manager: Download manager.
69
+ :return: Splits.
70
+ """
71
+ split_dict = {
72
+ "train": datasets.Split.TRAIN,
73
+ "test": datasets.Split.TEST,
74
+ "val": datasets.Split.VALIDATION,
75
+ }
76
+
77
+ return [
78
+ datasets.SplitGenerator(
79
+ name=name,
80
+ gen_kwargs={
81
+ "metadata_path": dl_manager.download(
82
+ _URLS["meta"].format(split=split)
83
+ ),
84
+ "video_dir": dl_manager.download_and_extract(
85
+ _URLS["video"].format(
86
+ type=self.config.name,
87
+ split=split
88
+ )
89
+ ),
90
+ },
91
+ )
92
+ for split, name in split_dict.items()
93
+ ]
94
+
95
+ def _generate_examples(
96
+ self, metadata_paths: str,
97
+ video_dir: str,
98
+ ) -> tuple[int, dict]:
99
+ """
100
+ Generate examples from metadata.
101
+ :param metadata_path: Path to metadata.
102
+ :param visual_dir: Directory of visual data.
103
+ :yield: Example.
104
+ """
105
+ dataset = datasets.load_dataset(
106
+ "parquet",
107
+ data_files=metadata_paths,
108
+ split="train",
109
+ )
110
+ for i, sample in enumerate(dataset):
111
+ video_path = os.path.join(video_dir, sample["id"] + ".mp4")
112
+ yield i, {
113
+ "id": sample["id"],
114
+ "type": sample["type"],
115
+ "view": sample["view"],
116
+ "text": sample["text"],
117
+ "video": self.__get_binary_data(video_path),
118
+ }
119
+
120
+ def __get_binary_data(self, path):
121
+ with open(path, "rb") as f:
122
+ return f.read()