| import os | |
| import datasets | |
| import soundfile as sf | |
| _CITATION = "" | |
| _DESCRIPTION = "Audio classification dataset with labels from folder names" | |
| _HOMEPAGE = "" | |
| _LICENSE = "" | |
| class AudioDatasetConfig(datasets.BuilderConfig): | |
| def __init__(self, **kwargs): | |
| super(AudioDatasetConfig, self).__init__(**kwargs) | |
| class AudioDataset(datasets.GeneratorBasedBuilder): | |
| BUILDER_CONFIGS = [AudioDatasetConfig(name="default", version=datasets.Version("1.0.0"))] | |
| def _info(self): | |
| labels = sorted(os.listdir(os.path.join(self.config.data_dir, "Final data"))) | |
| return datasets.DatasetInfo( | |
| description=_DESCRIPTION, | |
| features=datasets.Features({ | |
| "audio": datasets.Audio(sampling_rate=16000), | |
| "label": datasets.ClassLabel(names=labels) | |
| }), | |
| supervised_keys=None, | |
| homepage=_HOMEPAGE, | |
| license=_LICENSE, | |
| citation=_CITATION, | |
| ) | |
| def _split_generators(self, dl_manager): | |
| data_dir = self.config.data_dir | |
| return [ | |
| datasets.SplitGenerator( | |
| name=datasets.Split.TEST, | |
| gen_kwargs={"data_dir": data_dir} | |
| ) | |
| ] | |
| def _generate_examples(self, data_dir): | |
| audio_path = os.path.join(data_dir, "Final data") | |
| label_list = sorted(os.listdir(audio_path)) | |
| id_ = 0 | |
| for label in label_list: | |
| label_dir = os.path.join(audio_path, label) | |
| if not os.path.isdir(label_dir): | |
| continue | |
| for fname in os.listdir(label_dir): | |
| if fname.endswith(".wav"): | |
| path = os.path.join(label_dir, fname) | |
| yield id_, { | |
| "audio": path, | |
| "label": label | |
| } | |
| id_ += 1 | |