yangwang825 commited on
Commit
48f626a
Β·
verified Β·
1 Parent(s): 9d8ffa8

Create vietnamceleb.py

Browse files
Files changed (1) hide show
  1. vietnamceleb.py +137 -0
vietnamceleb.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+
3
+ """VietnamCeleb dataset."""
4
+
5
+
6
+ import os
7
+ from typing import List
8
+ from pathlib import Path
9
+
10
+ import librosa
11
+ import datasets
12
+ from rich import print
13
+
14
+
15
+ DATA_DIR_STRUCTURE = """
16
+ root/
17
+ └── data
18
+ β”œβ”€β”€ id00000
19
+ β”œβ”€β”€ 00001.wav
20
+ ...
21
+ ...
22
+ """
23
+ MANUAL_DOWNLOAD_INSTRUCTION = f"""
24
+ To use VietnamCeleb you have to download it manually.
25
+ The tree structure of the downloaded data looks like:
26
+ {DATA_DIR_STRUCTURE}
27
+ """
28
+
29
+ SAMPLING_RATE = 16_000
30
+
31
+
32
+ class VietnamCelebConfig(datasets.BuilderConfig):
33
+ """BuilderConfig for VietnamCeleb."""
34
+
35
+ def __init__(self, features, **kwargs):
36
+ super(VietnamCelebConfig, self).__init__(version=datasets.Version("0.0.1", ""), **kwargs)
37
+ self.features = features
38
+
39
+
40
+ class VietnamCeleb(datasets.GeneratorBasedBuilder):
41
+
42
+ BUILDER_CONFIGS = [
43
+ VietnamCelebConfig(
44
+ features=datasets.Features(
45
+ {
46
+ "audio": datasets.Audio(sampling_rate=SAMPLING_RATE),
47
+ "speaker": datasets.Value("string"),
48
+ # "duration": datasets.Value("int32"),
49
+ }
50
+ ),
51
+ name="verification",
52
+ description="",
53
+ ),
54
+ ]
55
+
56
+ DEFAULT_CONFIG_NAME = "verification"
57
+
58
+ def _info(self):
59
+ return datasets.DatasetInfo(
60
+ description="VietnamCeleb for verification",
61
+ features=self.config.features,
62
+ )
63
+
64
+ @property
65
+ def manual_download_instructions(self):
66
+ return MANUAL_DOWNLOAD_INSTRUCTION
67
+
68
+ def _split_generators(self, dl_manager):
69
+
70
+ data_dir = os.path.abspath(os.path.expanduser(dl_manager.manual_dir))
71
+
72
+ if not os.path.exists(data_dir):
73
+ raise self.manual_download_instructions
74
+
75
+ if not os.path.isdir(os.path.join(data_dir, 'data')):
76
+ raise FileExistsError(f"{data_dir} does not exist. Make sure you have unzipped the dataset.")
77
+
78
+ return [
79
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"split": "train", "data_dir": data_dir}),
80
+ datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"split": "test", "data_dir": data_dir}),
81
+ ]
82
+
83
+ def _generate_examples(self, split, data_dir):
84
+ """Generate examples from VietnamCeleb"""
85
+ train_txt = os.path.join(data_dir, 'vietnam-celeb-t.txt')
86
+
87
+ archive_path = os.path.join(data_dir, 'data')
88
+ with open(train_txt, "r") as f:
89
+ train_speakers = [line.strip().split()[0] for line in f]
90
+ train_speakers = list(set(train_speakers))
91
+ test_speakers = list(set(os.listdir(archive_path)) - set(train_speakers))
92
+
93
+ # Iterating the contents of the data to extract the relevant information
94
+ extensions = ['.wav']
95
+
96
+ if split == 'train':
97
+ speakers = train_speakers
98
+ elif split == 'test':
99
+ speakers = test_speakers
100
+
101
+ guid = 0
102
+ for speaker in speakers:
103
+ _, wav_paths = fast_scandir(os.path.join(archive_path, speaker), extensions)
104
+ for wav_path in wav_paths:
105
+ yield guid, {
106
+ "id": str(guid),
107
+ "audio": wav_path,
108
+ "speaker": speaker
109
+ }
110
+ guid += 1
111
+
112
+
113
+ def fast_scandir(path: str, extensions: List[str], recursive: bool = False):
114
+ # Scan files recursively faster than glob
115
+ # From github.com/drscotthawley/aeiou/blob/main/aeiou/core.py
116
+ subfolders, files = [], []
117
+
118
+ try: # hope to avoid 'permission denied' by this try
119
+ for f in os.scandir(path):
120
+ try: # 'hope to avoid too many levels of symbolic links' error
121
+ if f.is_dir():
122
+ subfolders.append(f.path)
123
+ elif f.is_file():
124
+ if os.path.splitext(f.name)[1].lower() in extensions:
125
+ files.append(f.path)
126
+ except Exception:
127
+ pass
128
+ except Exception:
129
+ pass
130
+
131
+ if recursive:
132
+ for path in list(subfolders):
133
+ sf, f = fast_scandir(path, extensions, recursive=recursive)
134
+ subfolders.extend(sf)
135
+ files.extend(f) # type: ignore
136
+
137
+ return subfolders, files