
import glob
import os
import librosa
import numpy as np
import tensorflow as tf
import sounddevice
import time

duration = 3  # seconds
sample_rate=44100


'''0 = air_conditioner
1 = car_horn
2 = children_playing
3 = dog_bark
4 = drilling
5 = engine_idling
6 = gun_shot
7 = jackhammer
8 = siren
9 = street_music
10 = truck'''



def extract_feature():
    X = sounddevice.rec(int(duration * sample_rate), samplerate=sample_rate, channels=1)
    sounddevice.wait()
    X= np.squeeze(X)
    stft = np.abs(librosa.stft(X))
    mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T,axis=0)
    chroma = np.mean(librosa.feature.chroma_stft(S=stft, sr=sample_rate).T,axis=0)
    mel = np.mean(librosa.feature.melspectrogram(X, sr=sample_rate).T,axis=0)
    contrast = np.mean(librosa.feature.spectral_contrast(S=stft, sr=sample_rate).T,axis=0)
    tonnetz = np.mean(librosa.feature.tonnetz(y=librosa.effects.harmonic(X), sr=sample_rate).T,axis=0)
    ext_features = np.hstack([mfccs,chroma,mel,contrast,tonnetz])
    features = np.vstack([features,ext_features])
    return features




model_path = "model/model"



n_dim = 193
n_classes = 10
n_hidden_units_one = 256
n_hidden_units_two = 256
sd = 1 / np.sqrt(n_dim)
learning_rate = 0.01

X = tf.placeholder(tf.float32,[None,n_dim])
Y = tf.placeholder(tf.float32,[None,n_classes])

W_1 = tf.Variable(tf.random_normal([n_dim,n_hidden_units_one], mean = 0, stddev=sd))
b_1 = tf.Variable(tf.random_normal([n_hidden_units_one], mean = 0, stddev=sd))
h_1 = tf.nn.tanh(tf.matmul(X,W_1) + b_1)

W_2 = tf.Variable(tf.random_normal([n_hidden_units_one,n_hidden_units_two], mean = 0, stddev=sd))
b_2 = tf.Variable(tf.random_normal([n_hidden_units_two], mean = 0, stddev=sd))
h_2 = tf.nn.sigmoid(tf.matmul(h_1,W_2) + b_2)

W = tf.Variable(tf.random_normal([n_hidden_units_two,n_classes], mean = 0, stddev=sd))
b = tf.Variable(tf.random_normal([n_classes], mean = 0, stddev=sd))
y_ = tf.nn.softmax(tf.matmul(h_2,W) + b)


init = tf.global_variables_initializer()
saver = tf.train.Saver()

y_true, y_pred = None, None
with tf.Session() as sess:
    saver.restore(sess, model_path)
    print "Model loaded"
    
    sess.run(tf.global_variables())
    while 1:
        feat = extract_feature()
        y_pred = sess.run(tf.argmax(y_, 1), feed_dict={X: feat})
        
        print y_pred




@@ -117,17 +117,19 @@
 ation = 
-3
+0.1
   # seco
@@ -527,28 +527,29 @@
  mfccs = np.
-mean
+array
 (librosa.fea
@@ -590,20 +590,13 @@
 fcc=
-40
+13
 ).T
-,axis=0
 )%0A  
@@ -605,28 +605,29 @@
 chroma = np.
-mean
+array
 (librosa.fea
@@ -664,31 +664,24 @@
 mple_rate).T
-,axis=0
 )%0A    mel = 
@@ -679,28 +679,29 @@
    mel = np.
-mean
+array
 (librosa.fea
@@ -736,31 +736,24 @@
 mple_rate).T
-,axis=0
 )%0A    contra
@@ -756,28 +756,29 @@
 ntrast = np.
-mean
+array
 (librosa.fea
@@ -821,31 +821,24 @@
 mple_rate).T
-,axis=0
 )%0A    tonnet
@@ -844,20 +844,21 @@
 tz = np.
-mean
+array
 (librosa
@@ -926,15 +926,8 @@
 e).T
-,axis=0
 )%0A  
@@ -1061,19 +1061,16 @@
 atures%0A%0A
-%0A%0A%0A
 model_pa
@@ -1089,18 +1089,16 @@
 model%22%0A%0A
-%0A%0A
 n_dim = 
@@ -1102,10 +1102,10 @@
  = 1
-93
+66
 %0An_c

Update classiPy.py