-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathstep5_extract_spectrograms_for_new_dataset.py
68 lines (55 loc) · 2.14 KB
/
step5_extract_spectrograms_for_new_dataset.py
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
65
66
67
68
#
# extract_spectrograms_for_new_dataset.py
#
# Generate spectrograms for a data set on which we want to run the models trained
# in steps 1-4.
#
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
#
#%% Imports
import glob
import os
import wave
import pylab
from matplotlib import pyplot
from joblib import Parallel, delayed ## version 0.12.0
import multiprocessing
import gc
#%% Path configuration
current_dir = "./Whale_Acoustics/"
data_dir = current_dir + "Data/"
audio_dir = data_dir + "Raw_Audio_Full_Analysis/"
output_spectrogram_dir = data_dir + "Extracted_Spectrogram_Full_Analysis/"
if not os.path.exists(output_spectrogram_dir):
os.makedirs(output_spectrogram_dir)
#%% Spectrogram generation
audio_filenames = glob.glob(audio_dir + '/*.wav')
print("Total number of New Audio Files to Score:", len(audio_filenames))
def get_wav_info(audio_filename):
wav = wave.open(audio_filename, 'r')
frames = wav.readframes(-1)
sound_info = pylab.frombuffer(frames, 'int16')
frame_rate = wav.getframerate()
wav.close()
return sound_info, frame_rate
def graph_spectrogram(spectrogram_second_length, audio_filename):
sound_info, frame_rate = get_wav_info(audio_filename)
audio_length_second = int(len(sound_info) / frame_rate)
for j in range(0, audio_length_second, spectrogram_second_length):
pyplot.figure(num=None, figsize=(19, 12))
pyplot.subplot(222)
ax = pyplot.axes()
ax.set_axis_off()
pyplot.specgram(sound_info[frame_rate * j: frame_rate * (j + spectrogram_second_length)], Fs = frame_rate)
pyplot.savefig(output_spectrogram_dir + audio_filename.split('\\')[1][:-4] + '_' + str(j) + '_' + str(j + spectrogram_second_length) + '.png', bbox_inches='tight', transparent=True, pad_inches=0.0)
pyplot.close()
gc.collect()
def generate_spectrograms(i):
audio_filename = audio_filenames[i]
try:
return graph_spectrogram(2, audio_filename)
except:
pass
num_cores = multiprocessing.cpu_count()
spectrograms = Parallel(n_jobs=num_cores)(delayed(generate_spectrograms)(i) for i in range(len(audio_filenames)))