-
Notifications
You must be signed in to change notification settings - Fork 4
/
classify_from_mic.py
323 lines (216 loc) · 8.71 KB
/
classify_from_mic.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/env python
# coding: utf-8
import sys
yamnet_base = './models/research/audioset/yamnet/'
sys.path.append(yamnet_base)
import os
assert os.path.exists(yamnet_base)
import time
import numpy as np
# audio stuff
import librosa
import soundfile as sf
import resampy
import pyaudio
# yamnet imports
import params
import modified_yamnet as yamnet_model
import features as features_lib
# TF / keras
#from tensorflow.keras import Model, layers
import tensorflow as tf
from tensorflow.keras.models import load_model
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
DESIRED_SR = 16000
# TODO: include my slightly modified yamnet code in this file
# i added the 'dense_net' return
def load_yamnet_model(model_path='yamnet.h5'):
# Set up the YAMNet model.
params.PATCH_HOP_SECONDS = 0.1 # 10 Hz scores frame rate.
yamnet, dense_net = yamnet_model.yamnet_frames_model(params)
yamnet.load_weights(model_path)
return yamnet
def load_top_model(model_path="top_model.h5"):
return load_model(model_path)
def read_wav(fname, output_sr, use_rosa=False):
if use_rosa:
waveform, sr = librosa.load(fname, sr=output_sr)
else:
wav_data, sr = sf.read(fname, dtype=np.int16)
if wav_data.ndim > 1:
# (ns, 2)
wav_data = wav_data.mean(1)
if sr != output_sr:
wav_data = resampy.resample(wav_data, sr, output_sr)
waveform = wav_data / 32768.0
return waveform.astype(np.float64)
def remove_silence(waveform, top_db=15, min_chunk_size=2000, merge_chunks=True):
"""
Loads sample into chunks of non-silence
"""
splits = librosa.effects.split(waveform, top_db=top_db)
waves = []
for start, end in splits:
if (end-start) < min_chunk_size:
continue
waves.append(waveform[start:end])
if merge_chunks and len(waves) > 0:
waves = np.concatenate(waves)
return waves
def run_models(waveform,
yamnet_model,
top_model,
strip_silence=True,
min_samples=11000):
if strip_silence:
waveform = remove_silence(waveform, top_db=10)
if waveform is None:
print('none wav?')
return None
if len(waveform) < min_samples:
#print(" too small after silence: " , len(waveform))
return None
# predictions, spectrogram, net, patches
_scores, _spectro, dense_out, _patches = \
yamnet_model.predict(np.reshape(waveform, [1, -1]), steps=1)
# dense = (N, 1024)
all_scores = []
for patch in dense_out:
scores = top_model.predict( np.expand_dims(patch, 0) ).squeeze()
all_scores.append(scores)
if not all_scores:
# no patches returned
return None
all_scores = np.mean(all_scores, axis=0)
return all_scores
def run_detection_loop(input_device_index = 0):
yamnet = load_yamnet_model()
top_model = load_top_model()
CHUNK = 4096 * 2
FORMAT = pyaudio.paInt16
DTYPE = np.int16 if FORMAT == pyaudio.paInt16 else np.float32
CHANNELS = 1
RATE = DESIRED_SR
min_frames_to_process = int(DESIRED_SR * 2.5)
p = pyaudio.PyAudio()
p.get_device_count()
for i in range(p.get_device_count()):
print(p.get_device_info_by_index(i))
print("_______")
p.terminate()
def dump_wav(arr, fname):
librosa.output.write_wav(fname, arr, DESIRED_SR)
if not os.path.exists("logs"):
os.makedirs("logs")
if not os.path.exists("detections"):
os.makedirs("detections")
if not os.path.exists("train_wavs"):
os.makedirs("train_wavs")
os.makedirs("train_wavs/high")
os.makedirs("train_wavs/mid")
os.makedirs("train_wavs/low")
def log_line(line, type='info'):
timestr = time.strftime('%a, %d %b %Y %H:%M:%S', time.localtime() )
log_file.write("{:<30} [{:<8}] {}\n".format(timestr, type, line))
log_file.flush()
def log_detection(score, wav_file=""):
timestr = time.strftime('%a %d %b %Y %H:%M:%S', time.localtime() )
timestr2 = str(time.time())
score = np.round(score, 3)
csv_file.write("{},{},{},{}\n".format(timestr, timestr2, score, wav_file))
csv_file.flush()
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
input_device_index=input_device_index,
frames_per_buffer=CHUNK)
frames = []
chunks_required = int(np.ceil(min_frames_to_process // CHUNK))
MIN_NOISE = 0.1
NOISE_MEAN_SCALE = 30.0
top_db = 18
MIN_SAMPLES_TO_RUN_NN = 5500
train_scores = []
train_times = []
all_sounds = []
all_sounds_times = []
verbose = 0
timestr = time.strftime('%a_%d_%b_%Y_%H-%M-%S', time.localtime())
log_name = "logs/{}.txt".format(timestr)
log_file = open(log_name, 'w')
csv_name = "detections/{}.csv".format(timestr)
csv_file = open(csv_name, 'w', encoding='utf-8')
last_web_update = time.time()
last_ping_time = time.time()
try:
while True:
try:
data = stream.read(CHUNK, exception_on_overflow=False)
except OSError:
print(" __ overflow")
arr = np.frombuffer(data, dtype=DTYPE)
arr = arr.astype(np.float32)
arr = arr / 32768.0
arr = arr * 1.25
frames.append(arr)
if len(frames) > chunks_required:
frames.pop(0)
if len(frames) >= chunks_required:
wave_arr = np.concatenate(frames)
noise_mean = np.abs(wave_arr).mean() * NOISE_MEAN_SCALE
if noise_mean < MIN_NOISE:
continue
wave_arr = remove_silence(wave_arr, top_db=top_db)
if wave_arr is None:
continue
# hack .. double the wave if too short
if len(wave_arr) > MIN_SAMPLES_TO_RUN_NN//2 and len(wave_arr) < MIN_SAMPLES_TO_RUN_NN:
wave_arr = np.concatenate((wave_arr, wave_arr))
scores = None
noise_mean = np.abs(wave_arr).mean() * NOISE_MEAN_SCALE
if noise_mean < MIN_NOISE:
continue
if wave_arr is not None and len(wave_arr) >= MIN_SAMPLES_TO_RUN_NN:
# not sure what the min size is for yamnet -- somewhere around 5k ?
scores = run_models(wave_arr, yamnet, top_model, strip_silence=False)
scores_text = "None"
if scores is not None:
# little bar for train score
scores_text = "="*int(scores[1]*50)
#log_line("detection: {:<10} {}".format(len(wave_arr), scores_text))
if scores is not None:
train_score = scores[1]
if train_score > 0.05:
print(" detection: ", train_score)
rounded_score = int(train_score * 100)
train_scores.append(train_score)
train_times.append(time.time())
if train_score > 0.7:
folder = "high"
elif train_score > 0.35:
folder = "mid"
else:
folder = "low"
wav_out_path = "train_wavs/{}/{}_{}.wav".format(folder, len(train_scores), rounded_score)
dump_wav(wave_arr, wav_out_path)
except KeyboardInterrupt as e:
print(" ____ interrupt ___")
stream.stop_stream()
stream.close()
p.terminate()
log_file.close()
except Exception as e:
stream.stop_stream()
stream.close()
p.terminate()
log_file.close()
print(" err" , str(e))
raise e
if __name__ == '__main__':
input_device_index = 0
if len(sys.argv) > 1:
input_device_index = int(sys.argv[1])
print(" --- Using input device: ", input_device_index)
run_detection_loop(input_device_index=input_device_index)