繁体   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