예제 #1
0
def apicall():
    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)
    else:
        return (jsonify({
            "prediction": "No Prediction",
            "filename": "No 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).
    all_numbers = extract_number(
        test_image_path,
        os.path.join(app.config['UPLOAD_FOLDER'],
                     filename.split('/')[-1] + "_"), "PROD")

    digits = all_numbers[0]
    X_coord = all_numbers[1]

    predictions = []

    for idx in np.argsort(X_coord):
        # Recognizing digits using the trained model on the local machine itself
        test_X = np.uint8(np.asarray(digits[idx]).reshape(1, -1))
        pred_Y = predict_value(test_X)[0]
        predictions.append(str(pred_Y))

    # Return result as response to the application
    return (jsonify({
        "prediction": ''.join(predictions),
        "filename": filename
    }))
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}))