繁体   English   中英

Flask 应用程序 - 错误 404,在服务器上找不到请求的 URL。 如果您手动输入了 URL,请检查您的拼写并重试

[英]Flask app - Error 404, The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again

我收到“错误 404 - 在服务器上找不到请求的 URL。如果您手动输入了 URL,请检查您的拼写并重试。” 运行以下代码时:

import base64
import numpy as np
import io
from PIL import Image
import keras
from keras import backend as K
from keras.models import Sequential
from keras.models import load_model
from keras.preprocessing.image import ImageDataGenerator
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('xrayV2.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 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))

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

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

代码或html、javascript代码有什么问题吗?

<!DOCTYPE html>
<html>
<head>
    <title>image app</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>Normal: <span id="normal-prediction"></span></p>
    <p>Pneumonia: <span id="pneumonia-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/jpeg;base64,","");
          console.log(base64Image);
        }
        reader.readAsDataURL($("image-selector")[0].files[0]);
        $("#normal-prediction").text("");
        $("#pneumonia-prediction").text("");
      });

      $("#predict-button").click(function(event){
        let message = {
          image: base64Image
        }
        console.log(message);
        $.post("http://10.0.0.4:5000/predict", JSON.stringify(message), function(response){
          $("#noraml-prediction").text(response.prediction.normal.toFixed(6));
          $("#pneumonia-prediction").text(response.prediction.pneumonia.toFixed(6));
          console.log(response);
        });
      });
      </script>
  </body>
</html>

代码有什么问题吗? 还是使用 tensorflow 版本? 我正在运行 Python 3.7.4 和 tensorflow 2.1.0

你没有办法处理返回你的 HTML 页面,所以用 method = 'GET' 创建一个,然后返回你的 HTML 文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM