Exemplo n.º 1
0
def apply_model_on_test_set():
    pred_Y = []
    test_Y = []
    base_path = os.path.join(ROOT_DIR, "static/Data/Test Images")
    for f in os.listdir(base_path):
        test_Y.append(int(f.split('(')[0].strip()))
        extracted_numbers = extract_number(
            os.path.join(base_path, f),
            os.path.join(ROOT_DIR, "Model/intermediate/"))
        digits = extracted_numbers
        for idx in range(len(digits)):
            pixels = imageToPixel(digits[idx]).tolist()
            predicted_value = int(predict_value(pixels)[0])
            pred_Y.append(predicted_value)

    # Getting to know how our model performed on data from our android app. This is the data the model
    # will actually work with once it goes in production. That's why these metrics are much more important.
    print(
        "Metrics of trained model on Images from Android Application (Our actual production data): "
    )
    print("Accuracy of trained model is: ",
          accuracy_score(test_Y, pred_Y) * 100, "%")
    print("Confusion_matrix: ", confusion_matrix(test_Y, pred_Y))
    print("Recall: ", recall_score(test_Y, pred_Y, average='weighted'))
    print("Precision: ", precision_score(test_Y, pred_Y, average='weighted'))
def apicall():
    # Basepath - You need to changes this if you are not following the exact same folder structure.
    # basepath = "/var/www/FlaskApplications/SampleApp/api"

    filename = ""

    # Check if the 'test_image' key is present in the files section of the request.
    # If it is that means that the android application (or whatever client sent this post request)
    # has successfully sent an image file (of the sketch of the digit)
    if 'test_image' in request.files:
        # Get that image file and store it in file variable
        file = request.files['test_image']

        # Also save the filename
        filename = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)

        # Now save this file on EC2 instance storage
        file.save(filename)

    test_image_path = filename

    # Pass the path of this file which you just saved on EC2 Instance to your image processing function extract_number()
    # It will process it and return the a list of file paths (these filepaths are for images of digits extracted from
    # the original image).
    digits = extract_number(
        test_image_path,
        app.config['UPLOAD_FOLDER'] + filename.split('/')[-1] + "_")

    predictions = []

    for digitPath in digits:
        # Convert the image of digit present on the filepath (digitPath) to pixel values
        test_X = imageToPixel(digitPath).tolist()

        # Recognizing digit using the trained model
        pred_Y = predict_value(test_X)
        predictions.append(pred_Y)

    # Return result as response to the application
    return (jsonify({"prediction": predictions[0][0], "filename": filename}))