Python ';方法不允许';在烧瓶中制作网页时
我正在用keras编写flaskapi。然而,我得到了很多错误。其中之一是错误405-不允许使用方法。 POST 405(不允许使用方法)jquery-3.3.1.min.js 我试着把预测写在页面上,但在错误405之前它们都没有显示出来。 我不知道哪个地方会导致那个错误 以下是代码: predict.htmlPython ';方法不允许';在烧瓶中制作网页时,python,google-chrome,flask,Python,Google Chrome,Flask,我正在用keras编写flaskapi。然而,我得到了很多错误。其中之一是错误405-不允许使用方法。 POST 405(不允许使用方法)jquery-3.3.1.min.js 我试着把预测写在页面上,但在错误405之前它们都没有显示出来。 我不知道哪个地方会导致那个错误 以下是代码: predict.html <body> <input id="image-selector" type="file"> <button id="predict-but
<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>
错误显示在google chrome中。您的JS代码
$.post(“http://0.0.0.0:5000/static/predict")
但你的路线是
@app.route(“/predict”,methods=[“POST”])
def predict():
因为您发布的代码片段没有显示您正在为所有路由预先添加/static/
,所以看起来这是一个错误
您正确地指定了methods=['POST']
,因此访问127.0.0.1:5000/predict
应该会得到预期的结果
如果要检查
0.0.0.0:5000/预测
,则需要添加app.run(host='0.0.0.0')
(请参阅:) 只需检查正确的路径是/static/predict
,而不仅仅是/predict
,我将其更改为/static/predict并获得:jquery-3.3.1.min.js:2 POST 400(错误请求)
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)