コード例 #1
0
def execute(silences,
            inputFolder='./raw/',
            outputFolder='./train/',
            segment=500,
            verbose=True,
            defaultSilenceLen=defaultLen):

    import random as ran

    silences_list_len = len(silences)  # number of silences sample

    for file in listFile(os.walk(inputFolder), ['*.wav']):
        org = rd.importSignal(inputFolder + file)
        wav, __len = trim(org, outOrgLen=True)

        for i in range(
                1
        ):  # range (max ((defaultSilenceLen - __len) // segment-2, 1)):
            idx = ran.randrange(0, silences_list_len)
            choice = silences[idx]
            seg = mix(wav, choice, i * segment)
            sname = outputFolder + 'mix_%d_%d_%s' % (idx, i, file)
            rd.save(seg, sname)
            if verbose:
                print("Saved file:%-40s with sample size:%5d" %
                      (sname, len(seg)))
コード例 #2
0
def main():

    # print (X_train)
    if os.path.isfile('model.keras'):
        model = load_lobe()
    else:
        model = network_1()

    # for train purpose only
    # follow this standard
    X_train, Y_train = loadData()
    X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], 1)
    #

    model.fit(X_train, Y_train, batch_size=32, nb_epoch=50)
    save_lobe(model)

    for file in listFile(os.walk('./test/'), ['*nhu*.wav']):
        mfcc = loadMfccFromFile('./test/' + file)
        mfcc = mfcc.reshape(1, mfcc.shape[0], 1)

        pred = model.predict(mfcc)
        print('Predict for %-40s:' % file, pred, pred[0][0] > 0.5)

    pass
コード例 #3
0
def loadSilences(dir='./raw/silent/'):
    silences = []
    for silent in listFile(os.walk(dir), ['*.wav']):

        silence = rd.importSignal('%s%s' % (dir, silent))[0:defaultLen]
        silences.append(silence)
        # print (len (silence))
    return silences
コード例 #4
0
def loadFolder(nhu, label):
    import json
    if os.path.isfile(nhu + 'cache'):
        a = np.loadtxt(nhu + 'cache')
        return np.array(a), np.array([label] * len(a))
    else:
        X_train = []
        Y_train = []

        flist = listFile(os.walk(nhu), ['*.wav'])
        i = 0
        for file in flist:
            i += 1
            print('%4d/%4d. Loading %-40s for label %5d' %
                  (i, len(flist), nhu + file, label))
            mfcc = loadMfccFromFile(nhu + file)
            X_train.append(mfcc)
            Y_train.append(label)
        # print (X_train)
        np.savetxt(nhu + 'cache', X_train)
        return np.array(X_train), np.array(Y_train)
コード例 #5
0
def main():
    # ask ()
    # return;
    # os.environ["CUDA_VISIBLE_DEVICES"]="-1"
    # from play import listFile
    # print (listFile (pattern=['nhu*']))
    # return
    from play import listFile
    # print (listFile (pattern=['khong*']))
    lobe = None
    if not os.path.isfile('model.keras'):
        lobe = voice_lobe_5th()
        print(lobe.summary())
        # return;
        yes, no = collect()
        print('Importing Data set ...')
        X_train, Y_train = importData(yes, no)
        # Y_train = Y_train.reshape(Y_train.shape[0], 1, 1)
        # print (Y_train.shape)
        # Y_train = Y_train.reshape (1, Y_train.shape[0], 1)
        # X_train = X_train.reshape (len(X_train), defaultLen // particle, particle)
        # Y_train = Y_train.reshape (len(Y_train), 1)
        # print (X_train)
        # return;
        # print (X_train)
        lobe.fit(X_train, Y_train, batch_size=len(X_train), nb_epoch=300)
        save_lobe(lobe)
    else:
        lobe = load_lobe()
    # return;
    # from ann_visualizer.visualize import ann_viz;
    # ann_viz(lobe, title="My second neural network")

    def check(i):
        from time import time
        wav = rd.importSignal(i)
        mfcc = transRaw(wav, trim=True)  # have to trim a word
        #from time import sleep
        mfcc = mfcc.reshape(1, mfcc.shape[0], mfcc.shape[1])
        # mfcc = mfcc.reshape (1, defaultLen // particle, particle)

        mark = time()
        pred = lobe.predict(mfcc)
        print('%-40s' % str(pred),
              'for %10s estimate time in second: %5.4f' % (i, time() - mark),
              end=' ')
        chosen = 0
        mx = 0
        for i in range(len(pred[0])):
            if mx < pred[0][i]:
                chosen = i
                mx = pred[0][i]
        print('label:', chosen)

        return (pred[0][0] >= 0.5)

    # for i in listFile (pattern=['nhu*']):
    # check (i)
    # return;
    cnt = 0
    _len = 0
    for i in listFile(pattern=['nhu*', 'khong*', 'toi*']):
        if check(i):
            cnt += 1
        _len += 1