-
Notifications
You must be signed in to change notification settings - Fork 13
/
birdCLEF_spec.py
235 lines (172 loc) · 6.77 KB
/
birdCLEF_spec.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import os
import traceback
import operator
import numpy as np
import cv2
import scipy.io.wavfile as wave
import scipy.ndimage as ndimage
import scipy.stats as stats
from scipy import interpolate
import python_speech_features as psf
from pydub import AudioSegment
######################################################
#Specify the source folder containing subfolders named after genus, species and class id
#Use birdCLEF_sort_data.py in order to sort wav files accordingly
src_dir = 'dataset/train/src/'
#Specify the target folder for spectrograms
#Will also contain subfolders named after genera, species and class ids of the processed wav files
#Will also contain "noise" folder with rejected spectrograms for further inspection
spec_dir = 'dataset/train/spec_44.1/'
#Specify maximum number of spectrograms per species (-1 = No limit)
MAX_SPECS = -1
#Limit number of species? (None = No limit)
MAX_SPECIES = None
######################################################
#Change sample rate if not 44.1 kHz
def changeSampleRate(sig, rate):
duration = sig.shape[0] / rate
time_old = np.linspace(0, duration, sig.shape[0])
time_new = np.linspace(0, duration, int(sig.shape[0] * 44100 / rate))
interpolator = interpolate.interp1d(time_old, sig.T)
new_audio = interpolator(time_new).T
sig = np.round(new_audio).astype(sig.dtype)
return sig, 44100
#Get magnitude spec from signal split
def getMagSpec(sig, rate, winlen, winstep, NFFT):
#get frames
winfunc = lambda x:np.ones((x,))
frames = psf.sigproc.framesig(sig, winlen*rate, winstep*rate, winfunc)
#Magnitude Spectrogram
magspec = np.rot90(psf.sigproc.magspec(frames, NFFT))
return magspec
#Split signal into five-second chunks with overlap of 4 and minimum length of 3 seconds
#Use these settings for other chunk lengths:
#winlen, winstep, seconds
#0.05, 0.0097, 5s
#0.05, 0.0195, 10s
#0.05, 0.0585, 30s
def getMultiSpec(path, seconds=5, overlap=4, minlen=3, winlen=0.05, winstep=0.0097, NFFT=840):
#open wav file
(rate,sig) = wave.read(path)
print "SampleRate:", rate,
#adjust to different sample rates
if rate != 44100:
sig, rate = changeSampleRate(sig, rate)
#split signal with ovelap
sig_splits = []
for i in xrange(0, len(sig), int((seconds - overlap) * rate)):
split = sig[i:i + seconds * rate]
if len(split) >= minlen * rate:
sig_splits.append(split)
#is signal too short for segmentation?
#append it anyway
if len(sig_splits) == 0:
sig_splits.append(sig)
#calculate spectrogram for every split
for sig in sig_splits:
#preemphasis
sig = psf.sigproc.preemphasis(sig, coeff=0.95)
#get spec
magspec = getMagSpec(sig, rate, winlen, winstep, NFFT)
#get rid of high frequencies
h, w = magspec.shape[:2]
magspec = magspec[h - 256:, :]
#normalize in [0, 1]
magspec -= magspec.min(axis=None)
magspec /= magspec.max(axis=None)
#fix shape to 512x256 pixels without distortion
magspec = magspec[:256, :512]
temp = np.zeros((256, 512), dtype="float32")
temp[:magspec.shape[0], :magspec.shape[1]] = magspec
magspec = temp.copy()
magspec = cv2.resize(magspec, (512, 256))
#DEBUG: show spec
#cv2.imshow('SPEC', magspec)
#cv2.waitKey(-1)
yield magspec
#Remove single spots from an image
def filter_isolated_cells(array, struct):
filtered_array = np.copy(array)
id_regions, num_ids = ndimage.label(filtered_array, structure=struct)
id_sizes = np.array(ndimage.sum(array, id_regions, range(num_ids + 1)))
area_mask = (id_sizes == 1)
filtered_array[area_mask[id_regions]] = 0
return filtered_array
#Decide if given spectrum shows bird sounds or noise only
def hasBird(spec, threshold=16):
#working copy
img = spec.copy()
#STEP 1: Median blur
img = cv2.medianBlur(img,5)
#STEP 2: Median threshold
col_median = np.median(img, axis=0, keepdims=True)
row_median = np.median(img, axis=1, keepdims=True)
img[img < row_median * 3] = 0
img[img < col_median * 4] = 0
img[img > 0] = 1
#STEP 3: Remove singles
img = filter_isolated_cells(img, struct=np.ones((3,3)))
#STEP 4: Morph Closing
img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, np.ones((5,5), np.float32))
#STEP 5: Frequency crop
img = img[128:-16, :]
#STEP 6: Count columns and rows with signal
#(Note: We only use rows with signal as threshold, but columns might come in handy in other scenarios)
#column has signal?
col_max = np.max(img, axis=0)
col_max = ndimage.morphology.binary_dilation(col_max, iterations=2).astype(col_max.dtype)
cthresh = col_max.sum()
#row has signal?
row_max = np.max(img, axis=1)
row_max = ndimage.morphology.binary_dilation(row_max, iterations=2).astype(row_max.dtype)
rthresh = row_max.sum()
#final threshold
thresh = rthresh
#DBUGB: show?
#print thresh
#cv2.imshow('BIRD?', img)
#cv2.waitKey(-1)
#STEP 7: Apply threshold (Default = 16)
bird = True
if thresh < threshold:
bird = False
return bird, thresh
######################################################
#elist all bird species
birds = [src_dir + bird + '/' for bird in sorted(os.listdir(src_dir))][:MAX_SPECIES]
print "BIRDS:", len(birds)
#parse bird species
for bird in birds:
total_specs = 0
#get all wav files
wav_files = [bird + wav for wav in sorted(os.listdir(bird))]
#parse wav files
for wav in wav_files:
spec_cnt = 0
print wav,
try:
#get every spec from each wav file
for spec in getMultiSpec(wav):
#does spec contain bird sounds?
isbird, thresh = hasBird(spec)
#new target path -> rejected specs will be copied to "noise" folder
if isbird:
dst_dir = spec_dir + bird.split("/")[-2] + "/"
else:
dst_dir = spec_dir + "noise/" + bird.split("/")[-2] + "/"
#make target dir
if not os.path.exists(dst_dir):
os.makedirs(dst_dir)
#write spec to target dir
cv2.imwrite(dst_dir + str(thresh) + "_" + wav.split("/")[-1].rsplit(".")[0] + "_" + str(spec_cnt) + ".png", spec * 255.0)
spec_cnt += 1
total_specs += 1
#exceeded spec limit?
if total_specs >= MAX_SPECS and MAX_SPECS > -1:
print " "
break
print "SPECS:", spec_cnt
except:
print spec_cnt, "ERROR"
traceback.print_exc()
pass