Flask错误400“浏览器(或代理)发送了该服务器无法理解的请求。”

时间:2018-11-22 06:45:57

标签: javascript python html flask port

因此,我正在尝试遵循本教程https://www.youtube.com/watch?v=eCz_DTtUBfo 关于我的ML模型的烧瓶使用情况。加载模型的部分有效,但是当我尝试对其进行初始化时,它只是不起作用。也许我的写作有些错误。 我希望有人能帮助我:c

这是我的Flask代码:

from keras.preprocessing.image import img_to_array
from flask import request
from flask import jsonify
from flask import Flask

app = Flask(__name__)

def get_model():
    global model
    model = load_model('pecuscope_model.h5')
    print(" * Model loaded!")

def preprocess_image(image, target_size):
    if image.mode != "RGB":
        image = image.convert("RGB")
    image = image.resize(target_size)
    image = img_to_array(image)
    image = np.expand_dims(image, axis=0)

    return image

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

@app.route("/predict", methods=["GET","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=(229, 229))

    prediction = model.predict(processed_image).tolist()

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

和我的html:

<!DOCTYPE html>
<html>
<head>
    <title>PecuScope Prediction</title>
    <style>
        * {
            font-size:30px;
        }
    </style>
</head>
<body>
    <input id="image-selector" type="file">
    <button id="predict-button">Predict</button>
    <p style="font-weight:bold">Predictions</p>
    <p>Mosquito: <span id="mosquito-prediction"></span></p>
    <p>Abeja: <span id=abeja-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]);
        $("#mosquito-prediction").text("");
        $("#abeja-prediction").text("");
        });

        $("#predict-button").click(function(event){
            let message = {
                image: base64Image
            }
            console.log(message);
            $.post("http://10.142.0.2:5000/predict", JSON.stringify(message), 

function(response){
                    $("#mosquito-prediction").text(response.prediction.mosquito.toFixed(6));
                    $("#abeja-prediction").text(response.prediction.abeja.toFixed(6));
                    console.log(response);
                });
            });
        </script>
    <body>
    <html>

我认为缩进或空格可能是个问题,我不知道。我对自己感到非常失望。我不能按照教程进行:c

1 个答案:

答案 0 :(得分:0)

不知道是不是原因,但是至少您在html文件正文中忘记了“: 阿贝贾:span id = abeja-prediction“