Пример #1
0
def main():
    theta1 = loadmat('ml.mat')['theta1']
    theta2 = loadmat('ml.mat')['theta2']
    """Xtest = [];
  ytest = [];

  nspeakers = theta2.shape[0];
  folders = os.listdir("wav")
  for i in range(5):
    folder = folders[i];
    print(folder)
    files = [f for f in glob.glob("wav/"+folder + "/" + "**/*.wav", recursive=True)]
    sztraining = int(len(files)*0.6);
    for fid in range(sztraining, len(files)):
      sample_rate, signal = wav.read(files[fid])
      signal = signal[0:int(2 * sample_rate)]
      mfcc = MFCC.main(signal, sample_rate)
      Xtest.append(mfcc)
      ytest.append(i)
      
  ytest = np.array(ytest)
  Xtest = np.array(Xtest)

  pred = [];
  for i in range(len(Xtest)):
    pred.append(ml.predictWAV(theta1, theta2, Xtest[i])[0])
  print(np.mean(pred == ytest.flatten()) * 100)"""

    signal = []
    sample_rate = 16000

    #th = threading.Thread(target=audio.plot_audio, args=(1,));
    #th.start()
    while True:
        cmd = input("Digite um comando")
        print("CMDZAO = " + str(cmd))
        if cmd == "record":
            seconds = 7
            print("recording...")
            signal = sd.rec(int(seconds * sample_rate),
                            samplerate=sample_rate,
                            channels=1)
            sd.wait()
        elif cmd == "who":
            if not len(signal):
                print("no signal")
                continue
            sd.play(signal, sample_rate)

            signal = signal[0:int(2 * sample_rate)]
            mfcc = MFCC.main(signal, sample_rate)

            mlres = ml.predictWAV(theta1, theta2, mfcc)

            print("user id: {}".format(mlres[0]))
        elif cmd == "exit":
            break
        else:
            print("not found.")
    return 0
Пример #2
0
 def getPerson(self):
     if self.file[0] == '':
         print('empty file!')
         return
     sample_rate, signal = read(self.file[0])
     theta1 = loadmat('ml.mat')['theta1']
     theta2 = loadmat('ml.mat')['theta2']
     mfcc = MFCC.main(signal, sample_rate)
     mlres = ml.predictWAV(theta1, theta2, mfcc)
     self.whoAmIWindow = WhoAmIWindowClass(mlres)
     self.whoAmIWindow.setWindowTitle('Result')
     self.whoAmIWindow.show()
Пример #3
0
def main():
    folders = os.listdir("wav")
    X = []
    y = []
    Xtest = []
    ytest = []
    nspeakers = 5
    #feature extraction

    for i in range(nspeakers):
        folder = folders[i]
        files = [
            f for f in glob.glob("wav/" + folder + "/" + "**/*.wav",
                                 recursive=True)
        ]

        sztraining = int(len(files) * 0.6)
        for fid in range(sztraining):
            sample_rate, signal = wav.read(files[fid])
            mfcc = MFCC.main(signal, sample_rate)
            for j in range(len(mfcc)):
                X.append([])
                for k in range(len(mfcc[j])):
                    X[-1].append(mfcc[j][k])
                y.append(i)
        for fid in range(sztraining, len(files)):
            sample_rate, signal = wav.read(files[fid])
            mfcc = MFCC.main(signal, sample_rate)
            for j in range(len(mfcc)):
                Xtest.append([])
                for k in range(len(mfcc[j])):
                    Xtest[-1].append(mfcc[j][k])
                ytest.append(i)

    y = np.array(y)
    X = np.array(X)
    ytest = np.array(ytest)
    Xtest = np.array(Xtest)
    input_layer_size = 390
    hidden_layer_size = 200
    num_labels = nspeakers

    lmbda = 1
    initial_theta1 = ml.randInitializeWeights(input_layer_size,
                                              hidden_layer_size)
    initial_theta2 = ml.randInitializeWeights(hidden_layer_size, num_labels)
    nn_initial_params = np.hstack(
        (initial_theta1.ravel(order='F'), initial_theta2.ravel(order='F')))

    print(
        ml.nnCostFunc(nn_initial_params, input_layer_size, hidden_layer_size,
                      num_labels, X, y, lmbda))
    theta_opt = opt.fmin_cg(maxiter=50,
                            f=ml.nnCostFunc,
                            x0=nn_initial_params,
                            fprime=ml.nnGrad,
                            args=(input_layer_size, hidden_layer_size,
                                  num_labels, X, y.flatten(), lmbda))

    theta1_opt = np.reshape(
        theta_opt[:hidden_layer_size * (input_layer_size + 1)],
        (hidden_layer_size, input_layer_size + 1), 'F')
    theta2_opt = np.reshape(
        theta_opt[hidden_layer_size * (input_layer_size + 1):],
        (num_labels, hidden_layer_size + 1), 'F')

    pred = ml.predict(theta1_opt, theta2_opt, Xtest, ytest)
    print(np.mean(pred == ytest.flatten()) * 100)
    savemat('ml.mat', {
        'theta1': theta1_opt,
        'theta2': theta2_opt
    })