Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

fsdd.py 2.1 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  1. from __future__ import print_function
  2. import os
  3. from collections import defaultdict
  4. import scipy.io.wavfile
  5. import scipy.ndimage
  6. class FSDD:
  7. """Summary
  8. Attributes:
  9. file_paths (TYPE): Description
  10. recording_paths (TYPE): Description
  11. """
  12. def __init__(self, data_dir):
  13. """Initializes the FSDD data helper which is used for fetching FSDD data.
  14. :param data_dir: The directory where the audiodata is located.
  15. :return: None
  16. Args:
  17. data_dir (TYPE): Description
  18. """
  19. # A dict containing lists of file paths, where keys are the label and vals.
  20. self.recording_paths = defaultdict(list)
  21. file_paths = [f for f in os.listdir(data_dir) if os.path.isfile(os.path.join(data_dir, f))]
  22. self.file_paths = file_paths
  23. for digit in range(0, 10):
  24. # fetch all the file paths that start with this digit
  25. digit_paths = [os.path.join(data_dir, f) for f in file_paths if f[0] == str(digit)]
  26. self.recording_paths[digit] = digit_paths
  27. @staticmethod
  28. def get_spectrograms(data_dir=None):
  29. """
  30. Args:
  31. data_dir (string): Path to the directory containing the spectrograms.
  32. Returns:
  33. (spectrograms, labels): a tuple of containing lists of spectrograms images(as numpy arrays) and their corresponding labels as strings
  34. """
  35. spectrograms = []
  36. labels = []
  37. if data_dir is None:
  38. data_dir = os.path.dirname(__file__) + '/../spectrograms'
  39. print(data_dir)
  40. file_paths = [f for f in os.listdir(data_dir) if os.path.isfile(os.path.join(data_dir, f)) and '.png' in f]
  41. if len(file_paths) == 0:
  42. raise Exception('There are no files in the spectrogram directory. Make sure to run the spectrogram.py before calling this function.')
  43. for file_name in file_paths:
  44. label = file_name[0]
  45. spectrogram = scipy.ndimage.imread(data_dir + '/' + file_name, flatten=True).flatten()
  46. spectrograms.append(spectrogram)
  47. labels.append(label)
  48. return spectrograms, labels
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...