簡體   English   中英

Flask 錯誤:無法預測結果

[英]Flask Error: Unable to predict the result

我創建了一個 html 模板來預測值,但無法做到這一點。 我對flask不太了解,但嘗試開始。 你能幫我解決這個問題嗎?

我的代碼的相關部分如下:

我無法預測價值

    from __future__ import division, print_function
    import sys
    import os
    import glob
    import re
    import numpy as np
    from tensorflow.keras.applications.imagenet_utils import preprocess_input, decode_predictions
    from tensorflow.keras.models import load_model
    from tensorflow.keras.preprocessing import image
    
    from flask import Flask, redirect, url_for, request, render_template
    from werkzeug.utils import secure_filename
    app = Flask(__name__)
    MODEL_PATH ='model_resnet50.h5'
    model = load_model(MODEL_PATH)
    
    def model_predict(img_path, model):
        img = image.load_img(img_path, target_size=(224, 224))
        x = image.img_to_array(img)
        x=x/255
        x = np.expand_dims(x, axis=0)
        preds = model.predict(x)
        preds=np.argmax(preds, axis=1)
        if preds==0:
            preds="Dhoni"
        elif preds==1:
            preds="Kohli"
        else:
            preds="Rohith"
        return preds
    
    @app.route('/', methods=['GET'])
    def index():
        # Main page
        return render_template('index.html')
    @app.route('/predict', methods=['GET', 'POST'])
    def upload():
        if request.method == 'POST':
            f = request.files['file']
            basepath = os.path.dirname(__file__)
            file_path = os.path.join(
                basepath, 'uploads', secure_filename(f.filename))
            f.save(file_path)
            preds = model_predict(file_path, model)
            result=preds
            return result
    
    
    if __name__ == '__main__':
        app.run(debug=True)
    

我的index.html

這是我的index.html 請幫我解決問題

    <h2>Predict Image</h2>
    
    <div>
        <form id="upload-file" method="post" enctype="multipart/form-data">
            <label for="imageUpload" class="upload-label">
                Choose...
            </label>
            <input type="file" name="file" id="imageUpload" accept=".png, .jpg, .jpeg">
        </form>
        <div>
            <button type="button" class="btn btn-primary btn-lg " id="btn-predict">Predict!</button>
        </div>
        <h3 id="result">
           <span> </span>
        </h3>
    </div>

我沒有 model 來測試主文件中的代碼,但 HTML 需要更改。

要發送<form>您必須將<button>放入<form> 它應該有type="submit"

您在/中加載index.html ,因此它會自動將<form>發送到/ 您需要action="/predict"<form>發送到/predict

<form action="/predict" id="upload-file" method="post" enctype="multipart/form-data">

        <label for="imageUpload" class="upload-label">
            Choose...
        </label>

        <input type="file" name="file" id="imageUpload" accept=".png, .jpg, .jpeg">

        <div>
            <button type="submit" class="btn btn-primary btn-lg " id="btn-predict">Predict!</button>
        </div>

</form>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM