phdkhanh2507 commited on
Commit
474c7c7
·
verified ·
1 Parent(s): 569d9f2

Upload vietnamese-speaker-lip-clip-v1.py

Browse files
Files changed (1) hide show
  1. vietnamese-speaker-lip-clip-v1.py +134 -0
vietnamese-speaker-lip-clip-v1.py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import datasets
3
+ from huggingface_hub import HfFileSystem
4
+ from typing import List, Tuple
5
+
6
+
7
+ logger = datasets.logging.get_logger(__name__)
8
+ fs = HfFileSystem()
9
+
10
+
11
+ _CITATION = """
12
+
13
+ """
14
+ _DESCRIPTION = """
15
+ This dataset extracts the mouth region from short clips of Vietnamese speakers.
16
+ """
17
+ _HOMEPAGE = "https://github.com/tanthinhdt/vietnamese-av-asr"
18
+ _MAIN_REPO_PATH = "datasets/phdkhanh2507/vietnamese-speaker-lip-clip-v1"
19
+ _REPO_URL = "https://huggingface.co/{}/resolve/main"
20
+ _URLS = {
21
+ "meta": f"{_REPO_URL}/metadata/".format(_MAIN_REPO_PATH) + "{channel}.parquet",
22
+ "visual": f"{_REPO_URL}/visual/".format(_MAIN_REPO_PATH) + "{channel}.zip",
23
+ }
24
+ _CONFIGS = ["all"]
25
+ if fs.exists(_MAIN_REPO_PATH + "/metadata"):
26
+ _CONFIGS.extend([
27
+ os.path.basename(file_name)[:-8]
28
+ for file_name in fs.listdir(_MAIN_REPO_PATH + "/metadata", detail=False)
29
+ if file_name.endswith(".parquet")
30
+ ])
31
+
32
+
33
+ class VietnameseSpeakerLipClipConfig(datasets.BuilderConfig):
34
+ """Vietnamese Speaker Clip configuration."""
35
+
36
+ def __init__(self, name, **kwargs):
37
+ """
38
+ :param name: Name of subset.
39
+ :param kwargs: Arguments.
40
+ """
41
+ super(VietnameseSpeakerLipClipConfig, self).__init__(
42
+ name=name,
43
+ version=datasets.Version("1.0.0"),
44
+ description=_DESCRIPTION,
45
+ **kwargs,
46
+ )
47
+
48
+
49
+ class VietnameseSpeakerLipClip(datasets.GeneratorBasedBuilder):
50
+ """Vietnamese Speaker Clip dataset."""
51
+
52
+ BUILDER_CONFIGS = [VietnameseSpeakerLipClipConfig(name) for name in _CONFIGS]
53
+ DEFAULT_CONFIG_NAME = "all"
54
+
55
+ def _info(self) -> datasets.DatasetInfo:
56
+ features = datasets.Features({
57
+ "id": datasets.Value("string"),
58
+ "channel": datasets.Value("string"),
59
+ "visual": datasets.Value("string"),
60
+ "duration": datasets.Value("float64"),
61
+ "fps": datasets.Value("int8"),
62
+ "audio": datasets.Value("string"),
63
+ "sampling_rate": datasets.Value("int64"),
64
+ })
65
+
66
+ return datasets.DatasetInfo(
67
+ description=_DESCRIPTION,
68
+ features=features,
69
+ homepage=_HOMEPAGE,
70
+ citation=_CITATION,
71
+ )
72
+
73
+ def _split_generators(
74
+ self, dl_manager: datasets.DownloadManager
75
+ ) -> List[datasets.SplitGenerator]:
76
+ """
77
+ Get splits.
78
+ :param dl_manager: Download manager.
79
+ :return: Splits.
80
+ """
81
+ config_names = _CONFIGS[1:] if self.config.name == "all" else [self.config.name]
82
+
83
+ metadata_paths = dl_manager.download(
84
+ [_URLS["meta"].format(channel=channel) for channel in config_names]
85
+ )
86
+ visual_dirs = dl_manager.download_and_extract(
87
+ [_URLS["visual"].format(channel=channel) for channel in config_names]
88
+ )
89
+
90
+ visual_dict = {
91
+ channel: visual_dir for channel, visual_dir in zip(config_names, visual_dirs)
92
+ }
93
+
94
+ return [
95
+ datasets.SplitGenerator(
96
+ name=datasets.Split.TRAIN,
97
+ gen_kwargs={
98
+ "metadata_paths": metadata_paths,
99
+ "visual_dict": visual_dict,
100
+ },
101
+ ),
102
+ ]
103
+
104
+ def _generate_examples(
105
+ self, metadata_paths: List[str],
106
+ visual_dict: dict,
107
+ audio_dict: dict,
108
+ ) -> Tuple[int, dict]:
109
+ """
110
+ Generate examples from metadata.
111
+ :param metadata_paths: Paths to metadata.
112
+ :param visual_dict: Paths to directory containing videos.
113
+ :param audio_dict: Paths to directory containing audios.
114
+ :yield: Example.
115
+ """
116
+ dataset = datasets.load_dataset(
117
+ "parquet",
118
+ data_files=metadata_paths,
119
+ split="train",
120
+ )
121
+ for i, sample in enumerate(dataset):
122
+ channel = sample["channel"]
123
+ visual_path = os.path.join(
124
+ visual_dict[channel], channel, sample["id"] + ".mp4"
125
+ )
126
+
127
+ yield i, {
128
+ "id": sample["id"],
129
+ "channel": channel,
130
+ "visual": visual_path,
131
+ "duration": sample["duration"],
132
+ "fps": sample["fps"],
133
+ "sampling_rate": sample["sampling_rate"],
134
+ }