You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Flask部署深度学习模型时遇TypeError错误求助

Flask部署植物病害识别模型时TypeError问题排查与解决

错误原因分析

报错TypeError: __init__() got an unexpected keyword argument 'method'的核心原因是Flask路由装饰器的参数拼写错误,同时代码里还有几处逻辑错误需要一并修复,才能正常运行。

具体错误点及修复方案

  1. 路由参数拼写错误
    原代码中@app.route('/predict', method=['POST ', 'GET'])里的参数名是单数method,但Flask要求必须用复数methods;另外POST后面多了空格,需要删除。
    修复后:@app.route('/predict', methods=['POST', 'GET'])

  2. 请求方法判断逻辑错误
    原代码用if __name__ == 'POST':判断请求方法完全错误,__name__是当前模块的名称,和HTTP请求方法无关,正确写法是通过request.method判断。
    修复后:if request.method == 'POST':

  3. 上传文件获取方式错误
    request.files是字典类型,需要用方括号[]通过键获取文件,原代码用圆括号()会报错。
    修复后:img = request.files['imge_file']

  4. 未处理GET请求场景
    当用户通过GET访问/predict时,pred和img_path变量未定义,会导致模板渲染失败,需要添加默认值或者重定向到首页。

  5. 图像归一化数值错误
    图像归一化应该除以255(像素值范围0-255),原代码写的是225,会导致输入数据异常,影响预测结果。
    修复后:x = x/255

修复后的完整代码

from flask import Flask , redirect , url_for , render_template, request
import tensorflow as tf
import numpy as np
from tensorflow import keras
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import load_img , img_to_array

app = Flask(__name__)

model = load_model('9366_9660model.h5')

key_list = ['Apple___Apple_scab', 'Apple___Black_rot', 'Apple___Cedar_apple_rust', 'Apple___healthy', 'Blueberry___healthy', 'Cherry_(including_sour)___Powdery_mildew', 'Cherry_(including_sour)___healthy', 'Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot', 'Corn_(maize)___Common_rust_', 'Corn(maize)___Northern_Leaf_Blight', 'Corn_(maize)_healthy', 'Grape___Black_rot', 'Grape___Esca_(Black_Measles)', 'Grape___Leaf_blight(Isariopsis_Leaf_Spot)', 'Grape___healthy', 'Orange___Haunglongbing(Citrus_greening)', 'Peach___Bacterial_spot', 'Peach___healthy', 'Pepper,_bell___Bacterial_spot', 'Pepper,_bell___healthy', 'Potato___Early_blight', 'Potato___Late_blight', 'Potato___healthy', 'Raspberry___healthy', 'Soybean___healthy', 'Squash___Powdery_mildew', 'Strawberry___Leaf_scorch', 'Strawberry___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_Yellow_Leaf_Curl_Virus', 'Tomato___Tomato_mosaic_virus', 'Tomato___healthy']

val_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37]

def predict_model(path):
    image = load_img(path , target_size=(299,299))
    x = img_to_array(image)
    x = x/255  # 修复归一化数值
    x = np.expand_dims(x , axis=0)

    images = np.vstack([x])
    pred_res = model.predict(images , batch_size=10)
    pred_res = np.argmax(pred_res[0])
    posi = val_list.index(pred_res)
    value = key_list[posi]
    return value

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

@app.route('/predict' , methods=['POST', 'GET']  )  # 修复参数名和空格问题
def predict():
    pred = None
    img_path = None
    if request.method == 'POST':  # 修复请求方法判断逻辑
        img = request.files['imge_file']  # 修复文件获取方式
        img_path = "static/" + img.filename
        img.save(img_path)
        pred = predict_model(img_path)
    
    return render_template('index.html' , prediction = pred , image_path = img_path)

if __name__ == '__main__':
    app.run(debug=True)

内容的提问来源于stack exchange,提问作者Mohit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 19:02:53