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

the error is cmd error snip the above picture shows the error im facing in cmd错误是cmd 错误截图上图显示了我在 cmd 中面临的错误

the file directory文件目录

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

app.py应用程序.py


    from flask import Flask

    UPLOAD_FOLDER = "/uploads"

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

index.py索引.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()

index.html索引.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>

main.py主文件



    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]

firstly i upload a pic through html file and this uploaded file is passed down to my saved cnn model which will be used to predict the plant disease and this will be displayed as output.首先我通过 html 文件上传一张图片,这个上传的文件被传递到我保存的 cnn model 将用于预测植物病害,这将显示为 Z78E6221F6393D1356681DB398F14CE6681DB398F14CE6。

https://medium.com/@arifulislam_ron/flask-web-application-to-classify-image-using-vgg16-d9c46f29c4cd im referencing the code from above link https://medium.com/@arifulislam_ron/flask-web-application-to-classify-image-using-vgg16-d9c46f29c4cd我引用了上面链接中的代码

Either add like this:要么像这样添加:

import os

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

or或者

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

暂无
暂无

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

相关问题 我试图读取在我的 Python 程序中以文本形式写入的日志文件,但它返回“没有这样的文件或目录” - Im trying to read a log file which is written in text in my Python program but its returning an “No such file or directory” 我试图计算 model object 但它不起作用 - im trying to count model object but its not working 我试图为我的网站运行 url 文件,但我在 powershell 中不断收到错误 - im trying to run a url file for my website, but it i keep getting an error in powershell 我试图通过电子邮件发送随机数,但我不断收到错误消息 - Im trying to Send a random number though a email but i keep getting a error 尝试为 python 安装 Kivy,但我收到此错误 - Trying to install Kivy for python but im getting this error 在运行YOLO以测试自定义对象cfg文件路径错误时,路径是正确的,即使显示此错误 - while running YOLO for test custom object cfg file path error the path is correct but even though its showing this error 我试图在heroku上托管我的flask api,但是在将路径传递给我的应用程序时,出现导入错误 - im trying to host my flask api on heroku, but when passing the path to my app, i get an import error 我试图制作一个代码,一个人会在一个随机生成的 b/w 1, 6 数字上下注,但即使我猜对了,它也说我输了 - Im trying to make a code where a person will bet on a number which is randomly generated b/w 1, 6 but even if i get it correct the it says that I lost 我得到一个索引错误,我不知道为什么 - Im getting an index error and im not sure why 我试图将 python 文件转换为 exe 文件,但它不起作用 - Im trying to turn a python file into an exe file and its not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM