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

metadata_extractor.py 1.5 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
  1. import csv
  2. import os
  3. # maps allowed classes and the corresponding one-hot encoding
  4. ALLOWED_CLASSES = {
  5. 'horn': 0,
  6. 'siren': 1,
  7. 'noise': 2
  8. }
  9. def translate_label(translation_map, input_label):
  10. return translation_map[input_label]
  11. def get_labels(translation_map):
  12. return [k for k, v in sorted(translation_map.items(), key=lambda item: item[1])]
  13. class UrbanSound8kExtractor:
  14. def __init__(self, metadata_path) -> None:
  15. self.metadata_path = metadata_path
  16. self.samples_names = []
  17. self.samples_labels = []
  18. def class_translation_map(self):
  19. return {
  20. 'air_conditioner': 'noise',
  21. 'car_horn': 'horn',
  22. 'children_playing': 'noise',
  23. 'dog_bark': 'noise',
  24. 'drilling': 'noise',
  25. 'engine_idling': 'noise',
  26. 'gun_shot': 'noise',
  27. 'jackhammer': 'noise',
  28. 'siren': 'siren',
  29. 'street_music': 'noise',
  30. }
  31. def load_metadata(self):
  32. with open(self.metadata_path) as f:
  33. reader = csv.reader(f, delimiter=',')
  34. for line in reader:
  35. self.samples_names.append(line[0])
  36. self.samples_labels.append(line[-1])
  37. def get_class(self, input_sample):
  38. '''
  39. Returns the class associated with given input sample
  40. '''
  41. input_sample_name = os.path.basename(input_sample)
  42. try:
  43. index = self.samples_names.index(input_sample_name)
  44. return self.samples_labels[index]
  45. except:
  46. return None
Tip!

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

Comments

Loading...