0

I'm writing flask api using keras. However I get a lot of errors. One of them is Error 405 - method not allowed. POST http://0.0.0.0:5000/static/predict 405 (METHOD NOT ALLOWED) jquery-3.3.1.min.js I'm trying to get predictions written on the page, but they didn't show even before that error 405. I don't know which place can lead to that error.

Here is code: predict.html

<body>
    <input id="image-selector" type="file">
    <button id="predict-button"> Predict</button>

    <p style="font-weight:bold">Predictions</p>
    <p> Jablko <span id="apple-prediction"></span></p>
    <p> Banan <span id="banana-prediction"></span></p>

    <img id="selected-image" src=""/>

    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    <script>
        let base64Image;
        $("#image-selector").change(function(){
        let reader = new FileReader();
        reader.onload = function(e){
            let dataURL = reader.result;
            $('#selected-image').attr("src", dataURL);
            base64Image = dataURL.replace("data:image/jpg;base64,", "");
            //console.log(base64Image);
            }
            reader.readAsDataURL($("#image-selector")[0].files[0]);
            $("#apple-prediction").text("");
            $("#banana-prediction").text("");
            });

            $("#predict-button").click(function(event){
                let message = {
                    image:base64Image
                }
                //console.log(message);
                $.post("http://0.0.0.0:5000/static/predict", function(response){
                    $("#apple-prediction").text(response.prediction.apple.toFixed(6));
                    $("#banana-prediction").text(response.prediction.banana.toFixed(6));
                    console.log(response);
                });
            });
    </script>
</body>

predict.py

app = Flask(__name__)

def get_model():
    global model
    model=load_model('fc_model1.h5')
    #model.load_weights('model_grocery.h5')
    #graph = tf.get_default_graph
    print("** Model loaded!")

def preprocess_image(image, target_size):
    image = image.resize(target_size)
    image = image.img_to_array(image)
    image = np.expand_dims(image, axis=0)
    return image

print("**Loading model**")
get_model()

@app.route("/predict", methods=["POST"])
def predict():
    message = request.get_json(force=True)
    encoded = message['image']
    decoded = base64.b64decode(encoded)
    image = Image.open(io.BytesIO(decoded))

    processed_image = preprocess_image(image, target_size=(224, 224))
    #bt_prediction = vgg16.predict(processed_image)
    prediction = model.predict(processed_image).tolist()

    response = {
        'prediction': {
            'apple': prediction[0][0],
            'banana': prediction[0][1]
        }
    }
    return jsonify(response)

The error shows in google-chrome.

Patrieszka
  • 63
  • 1
  • 9
  • Just checking the correct route is `/static/predict` and not just `/predict` – Phix Oct 23 '19 at 21:09
  • I changed it to /static/predict and got: jquery-3.3.1.min.js:2 POST http://0.0.0.0:5000/static/predict 400 (BAD REQUEST) – Patrieszka Oct 23 '19 at 21:19

1 Answers1

0

Your JS code has

$.post("http://0.0.0.0:5000/static/predict")

but your Flask route is

@app.route("/predict", methods=["POST"])
def predict():

Because the snippet you posted doesn't show that you're prepending /static/ to all routes, it looks like that's a mistake.

You specified methods=['POST'] correctly, so visiting 127.0.0.1:5000/predict should yield the expected result.

If you wanted to check 0.0.0.0:5000/predict, you need to add app.run(host='0.0.0.0')(See: Configure Flask dev server to be visible across the network).

Ahmed
  • 138
  • 7