Пример #1
0
def main():
    userWantNotToQuit = True
    models = f.loadModels()
    while userWantNotToQuit:
        print("Currently stored models:")
        for i in range(len(models)):
            print("Nr. " +
                 str(i +
                     1) +
                 ": " +
                 str(models[i].name) +
                 " | Frames:\t" +
                 str(len(models[i].features)) +
                 " | Matches:\t" +
                 str(models[i].matches) +
                 " | Influenced by:\t" +
                 str(models[i].influencedBy) +
                 " | Threshold:\t" +
                 str(models[i].threshold) +
                 " | Score:\t" +
                 str(models[i].score) +
                 " | Loaded:\t" +
                 str(models[i].loaded))
        print()
        printCommands()
        isUserInputWrong = True
        while isUserInputWrong:
            userInput = raw_input("What do you want to do? ")
            if userInput == "a":
                userInput = raw_input("Which model do you want to activate? ")
                try:
                    selectedModel = int(userInput) - 1
                    if selectedModel < len(models):
                        if selectedModel >= 0:
                            models[selectedModel].activate()
                            f.storeSameModel(models[selectedModel])
                            isUserInputWrong = False
                        else:
                            print("There are no models with a number smaller or equal 0.")
                    else:
                        print("There is no model with such a high number.")
                except ValueError:
                    print("That was not a number")
            elif userInput == "d":
                userInput = raw_input("Which model do you want to deactivate? ")
                try:
                    selectedModel = int(userInput) - 1
                    if selectedModel < len(models):
                        if selectedModel >= 0:
                            models[selectedModel].deactivate()
                            f.storeSameModel(models[selectedModel])
                            isUserInputWrong = False
                        else:
                            print("There are no models with a number smaller or equal 0.")
                    else:
                        print("There is no model with such a high number.")
                except ValueError:
                    print("That was not a number")
            elif userInput == "del":
                userInput = raw_input("Which model do you want to delete? ")
                try:
                    selectedModel = int(userInput) - 1
                    if selectedModel < len(models):
                        if selectedModel >= 0:
                            os.remove(conf.MODELS_DIR + "/" + models[selectedModel].name)
                            models = f.loadModels()
                            isUserInputWrong = False
                        else:
                            print("There are no models with a number smaller or equal 0.")
                    else:
                        print("There is no model with such a high number.")
                except ValueError:
                    print("That was not a number")
            elif userInput == "r":
                userInput = raw_input("Which model do you want to rename? ")
                try:
                    selectedModel = int(userInput) - 1
                    if selectedModel < len(models):
                        if selectedModel >= 0:
                            oldModelName = models[selectedModel].name
                            newName = raw_input("Which name should this model have?")
                            models[selectedModel].name = newName
                            f.storeModel(models[selectedModel])
                            os.remove(conf.MODELS_DIR + "/" + oldModelName)
                            isUserInputWrong = False
                        else:
                            print("There are no models with a number smaller or equal 0.")
                    else:
                        print("There is no model with such a high number.")
                except ValueError:
                    print("That was not a number")
            elif userInput == "c":
                userInput = raw_input("For which model do you want to change the path to the script? ")
                try:
                    selectedModel = int(userInput) - 1
                    if selectedModel < len(models):
                        if selectedModel >= 0:
                            oldScriptPath = models[selectedModel].script
                            newPath = raw_input("Where is the script? (Current script is at " + oldScriptPath + ". ")
                            while os.path.isfile(newPath) == False:
                                print("It seems that there is no script at " + newPath)
                                newPath = raw_input("Where is the script? (Current script is at " + oldScriptPath + ". ")
                            models[selectedModel].script = newPath
                            f.storeSameModel(models[selectedModel])
                            isUserInputWrong = False
                        else:
                            print("There are no models with a number smaller or equal 0.")
                    else:
                        print("There is no model with such a high number.")
                except ValueError:
                    print("That was not a number")
            elif userInput == "h":
                isUserInputWrong = False
                printCommands()
            elif userInput == "q":
                isUserInputWrong = False
                userWantNotToQuit = False
            else:
                print("That was not a valid input.")
Пример #2
0
def main():
    global models
    global modelThreshold
    global modelScore

    print("")
    print("This script can only process conf.CHUNK (currently: " + str(conf.CHUNK) +
          ") frames per loop so if the file contains a number of frames which is not divisible by conf.CHUNK the last few frames are dropped")

    model = []
    wavenumber = 1
    fileName, modelName, optimalFrames, scriptpath = interactions.getTrainParameters()
    beginning = time.time()
    # do it while there are wave files
    wf = []
    while os.path.isfile(str(fileName) + "/" + str(wavenumber) + ".wav"):
        wf.append((wave.open(str(fileName) + "/" + str(wavenumber) + ".wav"), wavenumber))
        print("File " + str(fileName) + "/" + str(wavenumber) +
                ".wav found.")
        wavenumber += 1
    for i in wf:
        model.append(preprocess(i))
    for i in wf:
        i[0].close()
    wavenumber -= 1
    print("Processed " + str(wavenumber) + " files in " + str(time.time() - beginning) + " seconds, minimalize them.")

    if model != []:
        data = []
        for i in range(wavenumber):
            data.append(
                (model[i],
                 optimalFrames))
        beginning = time.time()
        f.clearTmpFolder()
        pool = multiprocessing.Pool(processes=4)
        result = pool.map(f.minimalizeAndCalcThreshold, data)
        minimalizedRecords = []
        calculatedThresholds = []
        for i in result:
            minimalizedRecords.append(i[0])
            calculatedThresholds.append(i[1])

        zeroFrame = np.zeros(conf.FEATURES_PER_FRAME, dtype=np.float64)
        models = []
        for i in range(len(minimalizedRecords)):
            features = copy.deepcopy(minimalizedRecords[i])
            tmpFeatures = [copy.deepcopy(zeroFrame) for number in range(optimalFrames)]
            tmpCounter = [0 for number in range(optimalFrames)]
            counter = 0.
            posCounter = [0 for number in range(len(minimalizedRecords))]
            # for every frame in this record try if we find mergable frames
            for h in range(optimalFrames):    
                # we try all recordings
                for j in range(len(minimalizedRecords)):
                    if f.compare(minimalizedRecords[j][h], features[h]) < calculatedThresholds[i][h]:
                        tmpFeatures[h] += minimalizedRecords[j][h]
                        tmpCounter[h] += 1
            for h in range(optimalFrames):
                tmpFeatures[h] = np.divide(tmpFeatures[h], tmpCounter[h])
                counter += tmpCounter[h]
            counter /= optimalFrames
            models.append(modelImport.Model(tmpFeatures, calculatedThresholds[i], modelName, tmpCounter, scriptpath))


        print()
        print("Computed the models in " + str(time.time() - beginning) + " seconds. Compute their score.")
        print()
        beginning = time.time()
        data = []
        for i in range(len(models)):
            data.append((models[i], fileName, i))
        pool = multiprocessing.Pool(processes=4)
        pool.map(q.qualityCheck, data)
        models = f.loadModels(tmp=True)
        print("Computed the scores in " + str(time.time() - beginning) + " seconds.")
        print()
        for i in range(len(models)):
            print("Model Nr:\t" +
                  str(i +
                      1) +
                  " | Frames:\t" +
                  str(len(models[i].features)) +
                  " | Matches:\t" +
                  str(models[i].matches) +
                  " | Influenced by:\t" +
                  str(models[i].influencedBy) +
                  " | Threshold:\t" +
                  str(models[i].threshold) +
                  " | Score:\t" +
                  str(models[i].score))

        # get the model number and substract 1 because list indexing starts
        # with 0
        modelNumber = interactions.getModelNumber(len(models)+1) - 1
        print("You selected Model " + str(modelNumber) + " with " + str(models[modelNumber].matches) + " Matches and a Score of: " + str(models[modelNumber].score))
        f.storeModel(models[modelNumber])