-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrealtime_YAMNET.py
More file actions
53 lines (42 loc) · 1.39 KB
/
realtime_YAMNET.py
File metadata and controls
53 lines (42 loc) · 1.39 KB
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
import pyaudio
import librosa
import numpy as np
import matplotlib.pyplot as plt
import keras
import yamnet.params as params
import yamnet.yamnet as yamnet_model
yamnet = yamnet_model.yamnet_frames_model(params)
yamnet.load_weights('yamnet/yamnet.h5')
yamnet_classes = yamnet_model.class_names('yamnet/yamnet_class_map.csv')
frame_len = int(params.SAMPLE_RATE * 1) # 1sec
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16,
channels=1,
rate=params.SAMPLE_RATE,
input=True,
frames_per_buffer=frame_len)
cnt = 0
plt.ion()
while True:
# data read
data = stream.read(frame_len, exception_on_overflow=False)
# byte --> float
frame_data = librosa.util.buf_to_float(data, n_bytes=2, dtype=np.int16)
# model prediction
scores, melspec = yamnet.predict(np.reshape(frame_data, [1, -1]), steps=1)
prediction = np.mean(scores, axis=0)
# visualize input audio
plt.imshow(melspec.T, cmap='jet', aspect='auto', origin='lower')
plt.pause(0.001)
plt.show()
top5_i = np.argsort(prediction)[::-1][:5]
# print result
print('Current event:\n' +
'\n'.join(' {:12s}: {:.3f}'.format(yamnet_classes[i], prediction[i])
for i in top5_i))
# print idx
print(cnt)
cnt += 1
stream.stop_stream()
stream.close()
p.terminate()