簡體   English   中英

我正在嘗試部署我的 ml model 來對植物圖像進行分類,但是即使路徑正確,我也找不到文件錯誤

[英]Im trying to deploy my ml model which classifies plant images, but im getting file not found error even though its path is correct

錯誤是cmd 錯誤截圖上圖顯示了我在 cmd 中面臨的錯誤

文件目錄

templates
--->index.html   
uploads  
venv  
app.py  
cnn_model.pkl  
index.py  
main.py

應用程序.py


    from flask import Flask

    UPLOAD_FOLDER = "/uploads"

    app = Flask(__name__)
    app.secret_key = "secret key"
    app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

索引.py


    from flask import Flask, render_template, request, redirect, flash, url_for
    import main
    import urllib.request
    from app import app
    from werkzeug.utils import secure_filename
    from main import getPrediction
    import os


    @app.route('/')
    def index():
        return render_template('index.html')


    @app.route('/', methods=['POST'])
    def submit_file():
        if request.method == 'POST':
            if 'file' not in request.files:
                flash('No file part')
                return redirect(request.url)
            file = request.files['file']
            if file.filename == '':
                flash('No file selected for uploading')
                return redirect(request.url)
            if file:
                filename = secure_filename(file.filename)
                print(filename)
                file.save(os.path.join(app.config['UPLOAD_FOLDER'],filename))
                label = getPrediction(filename)
                flash(label)
                return redirect('/')


    if __name__ == "__main__":
        app.run()

索引.html


    <!doctype html>
    <title>Plant Classifier</title>
    <h2>Select a file to upload</h2>
    <p>
        {% with messages = get_flashed_messages() %}
          {% if messages %}
            Label: {{ messages[0] }}

          {% endif %}
        {% endwith %}
    </p>
    <form method="post" action="/" enctype="multipart/form-data">
        <dl>
        <p>
            <input type="file" name="file" autocomplete="off" required>
        </p>
        </dl>
        <p>
        <input type="submit" value="Submit">
        </p>
    </form>

主文件



    labels=['Pepper__bell___Bacterial_spot','Pepper__bell___healthy',
    'Potato___Early_blight','Potato___Late_blight','Potato___healthy',
    'Tomato_Bacterial_spot','Tomato_Early_blight','Tomato_Late_blight',
    'Tomato_Leaf_Mold','Tomato_Septoria_leaf_spot',
    'Tomato_Spider_mites_Two_spotted_spider_mite','Tomato__Target_Spot',
    'Tomato__Tomato_YellowLeaf__Curl_Virus','Tomato__Tomato_mosaic_virus',
    'Tomato_healthy']

    def convert_image_to_array(image_dir):
        try:
            image = cv2.imread(image_dir)
            if image is not None :
                image = cv2.resize(image, default_image_size)   
                return img_to_array(image)
            else :
                return np.array([])
        except Exception as e:
            print(f"Error : {e}")
            return None

    default_image_size = tuple((256, 256))


    def getPrediction(filename):
        file_object = 'cnn_model.pkl'
        model=pickle.load(open(filename, 'rb'))

        #model = pickle.load(file_object)
        #imgpath='/content/drive/My Drive/Final Project files/TEST.JPG'
        lb = preprocessing.LabelBinarizer()

        imar = convert_image_to_array(filename) 
        npimagelist = np.array([imar], dtype=np.float16)/225.0 
        PREDICTEDCLASSES2 = model.predict_classes(npimagelist) 
        num=np.asscalar(np.array([PREDICTEDCLASSES2]))
        return labels[num]

首先我通過 html 文件上傳一張圖片,這個上傳的文件被傳遞到我保存的 cnn model 將用於預測植物病害,這將顯示為 Z78E6221F6393D1356681DB398F14CE6681DB398F14CE6。

https://medium.com/@arifulislam_ron/flask-web-application-to-classify-image-using-vgg16-d9c46f29c4cd我引用了上面鏈接中的代碼

要么像這樣添加:

import os

basedir = os.path.abspath(os.path.dirname(__file__))
UPLOAD_FOLDER = os.path.join(basedir, '/uploads')

或者

UPLOAD_FOLDER = os.getcwd() + '/uploads'

暫無
暫無

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

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