vietnamceleb / vietnamceleb.py
yangwang825's picture
Create vietnamceleb.py
48f626a verified
# coding=utf-8
"""VietnamCeleb dataset."""
import os
from typing import List
from pathlib import Path
import librosa
import datasets
from rich import print
DATA_DIR_STRUCTURE = """
root/
└── data
β”œβ”€β”€ id00000
β”œβ”€β”€ 00001.wav
...
...
"""
MANUAL_DOWNLOAD_INSTRUCTION = f"""
To use VietnamCeleb you have to download it manually.
The tree structure of the downloaded data looks like:
{DATA_DIR_STRUCTURE}
"""
SAMPLING_RATE = 16_000
class VietnamCelebConfig(datasets.BuilderConfig):
"""BuilderConfig for VietnamCeleb."""
def __init__(self, features, **kwargs):
super(VietnamCelebConfig, self).__init__(version=datasets.Version("0.0.1", ""), **kwargs)
self.features = features
class VietnamCeleb(datasets.GeneratorBasedBuilder):
BUILDER_CONFIGS = [
VietnamCelebConfig(
features=datasets.Features(
{
"audio": datasets.Audio(sampling_rate=SAMPLING_RATE),
"speaker": datasets.Value("string"),
# "duration": datasets.Value("int32"),
}
),
name="verification",
description="",
),
]
DEFAULT_CONFIG_NAME = "verification"
def _info(self):
return datasets.DatasetInfo(
description="VietnamCeleb for verification",
features=self.config.features,
)
@property
def manual_download_instructions(self):
return MANUAL_DOWNLOAD_INSTRUCTION
def _split_generators(self, dl_manager):
data_dir = os.path.abspath(os.path.expanduser(dl_manager.manual_dir))
if not os.path.exists(data_dir):
raise self.manual_download_instructions
if not os.path.isdir(os.path.join(data_dir, 'data')):
raise FileExistsError(f"{data_dir} does not exist. Make sure you have unzipped the dataset.")
return [
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"split": "train", "data_dir": data_dir}),
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"split": "test", "data_dir": data_dir}),
]
def _generate_examples(self, split, data_dir):
"""Generate examples from VietnamCeleb"""
train_txt = os.path.join(data_dir, 'vietnam-celeb-t.txt')
archive_path = os.path.join(data_dir, 'data')
with open(train_txt, "r") as f:
train_speakers = [line.strip().split()[0] for line in f]
train_speakers = list(set(train_speakers))
test_speakers = list(set(os.listdir(archive_path)) - set(train_speakers))
# Iterating the contents of the data to extract the relevant information
extensions = ['.wav']
if split == 'train':
speakers = train_speakers
elif split == 'test':
speakers = test_speakers
guid = 0
for speaker in speakers:
_, wav_paths = fast_scandir(os.path.join(archive_path, speaker), extensions)
for wav_path in wav_paths:
yield guid, {
"id": str(guid),
"audio": wav_path,
"speaker": speaker
}
guid += 1
def fast_scandir(path: str, extensions: List[str], recursive: bool = False):
# Scan files recursively faster than glob
# From github.com/drscotthawley/aeiou/blob/main/aeiou/core.py
subfolders, files = [], []
try: # hope to avoid 'permission denied' by this try
for f in os.scandir(path):
try: # 'hope to avoid too many levels of symbolic links' error
if f.is_dir():
subfolders.append(f.path)
elif f.is_file():
if os.path.splitext(f.name)[1].lower() in extensions:
files.append(f.path)
except Exception:
pass
except Exception:
pass
if recursive:
for path in list(subfolders):
sf, f = fast_scandir(path, extensions, recursive=recursive)
subfolders.extend(sf)
files.extend(f) # type: ignore
return subfolders, files