【问题标题】:'Method not allowed' while making webpage in flask在烧瓶中制作网页时“方法不允许”
【发布时间】:2020-02-20 04:14:14
【问题描述】:

我正在使用 keras 编写烧瓶 api。但是我得到了很多错误。其中之一是错误 405 - 不允许的方法。 发布http://0.0.0.0:5000/static/predict 405(方法不允许)jquery-3.3.1.min.js 我正在尝试将预测写在页面上,但它们甚至在错误 405 之前都没有显示。 我不知道哪个地方会导致这个错误。

代码如下: 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)

错误显示在 google-chrome 中。

【问题讨论】:

  • 只检查正确的路线是/static/predict 而不仅仅是/predict
  • 我将其更改为 /static/predict 并得到:jquery-3.3.1.min.js:2 POST 0.0.0.0:5000/static/predict 400 (BAD REQUEST)

标签: python google-chrome flask


【解决方案1】:

你的 JS 代码有

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

但你的 Flask 路线是

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

因为您发布的 sn-p 并未显示您将 /static/ 附加到所有路由,所以看起来这是一个错误。

您正确指定了methods=['POST'],因此访问127.0.0.1:5000/predict 应该会产生预期的结果。

如果您想查看0.0.0.0:5000/predict,您需要添加app.run(host='0.0.0.0')(参见:Configure Flask dev server to be visible across the network)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-09
    • 2014-07-28
    • 2014-03-08
    • 2020-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多