Sri3010 commited on
Commit
3002c85
·
verified ·
1 Parent(s): 4d44b8e

Upload dataset.py

Browse files
Files changed (1) hide show
  1. dataset.py +55 -0
dataset.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import datasets
3
+ import soundfile as sf
4
+
5
+ _CITATION = ""
6
+ _DESCRIPTION = "Audio classification dataset with labels from folder names"
7
+ _HOMEPAGE = ""
8
+ _LICENSE = ""
9
+
10
+ class AudioDatasetConfig(datasets.BuilderConfig):
11
+ def __init__(self, **kwargs):
12
+ super(AudioDatasetConfig, self).__init__(**kwargs)
13
+
14
+ class AudioDataset(datasets.GeneratorBasedBuilder):
15
+ BUILDER_CONFIGS = [AudioDatasetConfig(name="default", version=datasets.Version("1.0.0"))]
16
+
17
+ def _info(self):
18
+ labels = sorted(os.listdir(os.path.join(self.config.data_dir, "Final data")))
19
+ return datasets.DatasetInfo(
20
+ description=_DESCRIPTION,
21
+ features=datasets.Features({
22
+ "audio": datasets.Audio(sampling_rate=16000),
23
+ "label": datasets.ClassLabel(names=labels)
24
+ }),
25
+ supervised_keys=None,
26
+ homepage=_HOMEPAGE,
27
+ license=_LICENSE,
28
+ citation=_CITATION,
29
+ )
30
+
31
+ def _split_generators(self, dl_manager):
32
+ data_dir = self.config.data_dir
33
+ return [
34
+ datasets.SplitGenerator(
35
+ name=datasets.Split.TEST,
36
+ gen_kwargs={"data_dir": data_dir}
37
+ )
38
+ ]
39
+
40
+ def _generate_examples(self, data_dir):
41
+ audio_path = os.path.join(data_dir, "Final data")
42
+ label_list = sorted(os.listdir(audio_path))
43
+ id_ = 0
44
+ for label in label_list:
45
+ label_dir = os.path.join(audio_path, label)
46
+ if not os.path.isdir(label_dir):
47
+ continue
48
+ for fname in os.listdir(label_dir):
49
+ if fname.endswith(".wav"):
50
+ path = os.path.join(label_dir, fname)
51
+ yield id_, {
52
+ "audio": path,
53
+ "label": label
54
+ }
55
+ id_ += 1