def cmd1():

    paser = argparse.ArgumentParser(description='Speech Emotion Recognition')
    # python3 cmd.py -o p - a 'Test/fear.wav'

    # paser.add_argument(
    #     '-o',
    #     '--option',
    #     type = str,
    #     dest = 'option',
    #     help = "Use 'p' to predict directly or use 't' to train a model.")
    #
    # paser.add_argument(
    #     '-a',
    #     '--audio',
    #     type = str,
    #     dest = 'audio',
    #     help = "The path of audio which you want to predict.")
    #

    # args = paser.parse_args()

    # option = args.option.lower() # p / t
    option = 'p'
    model_name = "lstm"
    model_type = "lstm"
    feature = "l"
    load = True
    # audio = args.audio if args.audio else 'default.wav'
    audio = "output.wav"
    # predict
    if option == 'p':
        model = load_model(load_model_name=model_name, model_name=model_type)
        emotion = Predict(model, file_path=audio)
    return emotion
    # train
    # elif option == 't':
    #     Train(model_name = model_type, save_model_name = model_name, if_load = load, feature_method = feature)

    # else:
    #     print("Wrong option. 'p' for predicting, 't' for training")
    #     return


# if __name__ == '__main__':
#     cmd1()
def upload_file():
    if request.method == 'POST' or request.method == 'GET':
        print(request.method)
        # check if the post request has the file part
        if 'file' not in request.files:
            print('No file part')
            return

        file = request.files['file']
        print(file.filename)
        # if user does not select file, browser also
        # submit a empty part without filename
        if file.filename == '':
            print('No selected file')
            return redirect(request.url)
        if file:  # and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            inputloc = os.path.join(app.config['UPLOAD_FOLDER'], filename)
            model = load_model(load_model_name="LSTM_LIBROSA",
                               model_name="lstm")
            emotion, prob = Predict(model,
                                    model_name="lstm",
                                    file_path=inputloc,
                                    feature_method='l')
            keras.backend.clear_session()
            result = {
                "emotion": emotion,
                "prob": {
                    "angry": prob[0].tolist(),
                    "fear": prob[1].tolist(),
                    "happy": prob[2].tolist(),
                    "neutral": prob[3].tolist(),
                    "sad": prob[4].tolist(),
                    "surprise": prob[5].tolist()
                }
            }
            return jsonify(json.dumps(result))
Beispiel #3
0
def cmd():
    paser = argparse.ArgumentParser(description='Speech Emotion Recognition')

    paser.add_argument(
        '-o',
        '--option',
        type=str,
        dest='option',
        help="Use 'p' to predict directly or use 't' to train a model.")

    paser.add_argument('-mt',
                       '--model_type',
                       type=str,
                       dest='model_type',
                       help="The type of model.")

    paser.add_argument('-mn',
                       '--model_name',
                       type=str,
                       dest='model_name',
                       help="The name of saved model file.")

    paser.add_argument('-l',
                       '--load',
                       type=bool,
                       dest='load',
                       help="Whether to load exist features.")

    paser.add_argument(
        '-f',
        '--feature',
        type=str,
        dest='feature',
        help=
        "The method for features extracting: use 'o' to use opensmile or use 'l' to use librosa."
    )

    paser.add_argument('-a',
                       '--audio',
                       type=str,
                       dest='audio',
                       help="The path of audio which you want to predict.")

    args = paser.parse_args()

    option = args.option.lower()  # p / t
    model_type = args.model_type if args.model_type else 'svm'  # svm / mlp / lstm
    model_name = args.model_name if args.model_name else 'default'
    load = args.load if args.load else True  # True / False
    feature = args.feature if args.feature else 'o'  # o / l
    audio = args.audio if args.audio else 'default.wav'

    # 预测
    if option == 'p':
        model = load_model(load_model_name=model_name, model_name=model_type)
        Predict(model,
                model_name=model_type,
                file_path=audio,
                feature_method=feature)

    # 训练
    elif option == 't':
        Train(model_name=model_type,
              save_model_name=model_name,
              if_load=load,
              feature_method=feature)

    else:
        print("Wrong option. 'p' for predicting, 't' for training")
        return
Beispiel #4
0
def get_emotion(path):
    print(Predict(model, model_name='svm', file_path=path, feature_method='l'))