phdkhanh2507 commited on
Commit
6109566
·
verified ·
1 Parent(s): 781d61a

Upload vietnamese-speaker-video.py

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