pauldarvin commited on
Commit
bbab825
·
1 Parent(s): 210a1a2

Delete example.py

Browse files
Files changed (1) hide show
  1. example.py +0 -74
example.py DELETED
@@ -1,74 +0,0 @@
1
- import os
2
- import matplotlib.pyplot as plt
3
- import numpy as np
4
- import librosa
5
- import panns_inference
6
- from panns_inference import AudioTagging, SoundEventDetection, labels
7
-
8
- def print_audio_tagging_result(clipwise_output):
9
- """Visualization of audio tagging result.
10
-
11
- Args:
12
- clipwise_output: (classes_num,)
13
- """
14
- sorted_indexes = np.argsort(clipwise_output)[::-1]
15
-
16
- # Print audio tagging top probabilities
17
- for k in range(10):
18
- print('{}: {:.3f}'.format(np.array(labels)[sorted_indexes[k]],
19
- clipwise_output[sorted_indexes[k]]))
20
-
21
-
22
- def plot_sound_event_detection_result(framewise_output):
23
- """Visualization of sound event detection result.
24
-
25
- Args:
26
- framewise_output: (time_steps, classes_num)
27
- """
28
- out_fig_path = 'results/sed_result.png'
29
- os.makedirs(os.path.dirname(out_fig_path), exist_ok=True)
30
-
31
- classwise_output = np.max(framewise_output, axis=0) # (classes_num,)
32
-
33
- idxes = np.argsort(classwise_output)[::-1]
34
- idxes = idxes[0:5]
35
-
36
- ix_to_lb = {i : label for i, label in enumerate(labels)}
37
- lines = []
38
- for idx in idxes:
39
- line, = plt.plot(framewise_output[:, idx], label=ix_to_lb[idx])
40
- lines.append(line)
41
-
42
- plt.legend(handles=lines)
43
- plt.xlabel('Frames')
44
- plt.ylabel('Probability')
45
- plt.ylim(0, 1.)
46
- plt.savefig(out_fig_path)
47
- print('Save fig to {}'.format(out_fig_path))
48
-
49
-
50
- if __name__ == '__main__':
51
- """Example of using panns_inferece for audio tagging and sound evetn detection.
52
- """
53
- device = 'cpu' # 'cuda' | 'cpu'
54
- audio_path = 'resources/R9_ZSCveAHg_7s.wav'
55
- (audio, _) = librosa.core.load(audio_path, sr=32000, mono=True)
56
- #print(audio)
57
- plt.plot(audio)
58
- plt.savefig('sample.png')
59
- audio = audio[None, :] # (batch_size, segment_samples)
60
- #print(audio)
61
-
62
- print('------ Audio tagging ------')
63
- at = AudioTagging(checkpoint_path=None, device=device)
64
- (clipwise_output, embedding) = at.inference(audio)
65
- """clipwise_output: (batch_size, classes_num), embedding: (batch_size, embedding_size)"""
66
-
67
- print_audio_tagging_result(clipwise_output[0])
68
-
69
- print('------ Sound event detection ------')
70
- sed = SoundEventDetection(checkpoint_path=None, device=device)
71
- framewise_output = sed.inference(audio)
72
- """(batch_size, time_steps, classes_num)"""
73
-
74
- plot_sound_event_detection_result(framewise_output[0])