Python 为什么我总是得到';请求';对象没有属性';文件';在烧瓶里,而我';我要上传图片吗?
所以我要做一个关于图像分类的网站,用户输入上传图像,图像将在不同的python文件中处理,不幸的是,我在一周多的时间里一直在解决这个问题Python 为什么我总是得到';请求';对象没有属性';文件';在烧瓶里,而我';我要上传图片吗?,python,flask,Python,Flask,所以我要做一个关于图像分类的网站,用户输入上传图像,图像将在不同的python文件中处理,不幸的是,我在一周多的时间里一直在解决这个问题 AttributeError: 'Request' object has no attribute 'file' 这是烧瓶的完整代码 app.py from flask import Flask, redirect, render_template, request from werkzeug.utils import secure_filename imp
AttributeError: 'Request' object has no attribute 'file'
这是烧瓶的完整代码app.py
from flask import Flask, redirect, render_template, request
from werkzeug.utils import secure_filename
import os
from Testing import predict
app = Flask(__name__)
app.config["ALLOWED_EXTENSIONS"] = ["JPEG", "JPG", "PNG", "GIF"]
app.config['UPLOAD_FOLDER'] = 'uploads'
def allowed_image(filename):
if not "." in filename:
return False
ext = filename.rsplit(".", 1)[1]
if ext.upper() in app.config["ALLOWED_EXTENSIONS"]:
return True
else:
return False
@app.route('/predict_image/', methods=['GET', 'POST'])
def render_message():
# Loading CNN model
if request.method == 'POST':
file = request.file['image_retina']
if 'file' not in request.files:
return render_template('upload.html')
if file.filename == '':
return render_template('upload.html')
if file and allowed_image(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
predict()
return render_template('upload.html')
这是处理图像的代码测试.py
import os
import numpy as np
from keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array
from keras.models import Sequential, load_model
import time
start = time.time()
# Define Path
test_path = 'data/test_image'
# Define image parameters
img_width, img_height = 150, 150
# Prediction Function
def predict():
model_path = './models/model.h5'
model_weights_path = './models/weights.h5'
model = load_model(model_path)
model.load_weights(model_weights_path)
x = load_img(test_path, target_size=(img_width, img_height))
x = img_to_array(x)
x = np.expand_dims(x, axis=0)
array = model.predict(x)
result = array[0]
# print(result)
answer = np.argmax(result)
if answer == 0:
print("Predicted: Drusen")
elif answer == 1:
print("Predicted: Normal")
return answer
# Calculate execution time
end = time.time()
dur = end-start
if dur < 60:
print("Execution Time:", dur, "seconds")
elif dur > 60 and dur < 3600:
dur = dur/60
print("Execution Time:", dur, "minutes")
else:
dur = dur/(60*60)
print("Execution Time:", dur, "hours")
导入操作系统
将numpy作为np导入
从keras.preprocessing.image导入ImageDataGenerator,将\u img、img\u加载到\u数组
从keras.models导入顺序加载模型
导入时间
开始=时间。时间()
#定义路径
测试路径='数据/测试图像'
#定义图像参数
img_宽度,img_高度=150,150
#预测函数
def predict():
模型路径='。/models/model.h5'
模型\权重\路径='。/模型/权重.h5'
模型=加载模型(模型路径)
模型。加载权重(模型权重路径)
x=负载img(测试路径,目标尺寸=(img宽度,img高度))
x=img_至_阵列(x)
x=np。展开尺寸(x,轴=0)
数组=模型。预测(x)
结果=数组[0]
#打印(结果)
答案=np.argmax(结果)
如果答案=0:
印刷品(“预测:德鲁森”)
elif答案==1:
打印(“预测:正常”)
回覆
#计算执行时间
end=time.time()
dur=结束-开始
如果dur<60:
打印(“执行时间:,dur,秒”)
elif dur>60和dur<3600:
dur=dur/60
打印(“执行时间:,dur,分钟”)
其他:
dur=dur/(60*60)
打印(“执行时间:,dur,小时”)
upload.html
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Upload Gambar</title>
</head>
<body>
<h1>Upload an Image</h1><br>
<form method="post" enctype="multipart/form-data">
<input type="file" id="image_retina" name="image_retina">
<label for="image_retina">Select image...</label><br>
<button type="submit">Upload</button><br>
<!-- <a href="/predict_image">bercanda anda</a> -->
</form>
{% if message %}
<p>{{message}}</p><br>
{% if data.shape[0] > 0 %}
<!-- {{ data.reset_index(drop = True).to_html(classes="table table-striped") | safe}} -->
{% endif %}
<p><img src="{{image_retina}}" style='width:500px' /><br>
{% endif %}
</body>
</html>
上传甘巴
上传图像
选择图像…
上传
{%if消息%}
{{message}}
{%if data.shape[0]>0%}
{%endif%}
{%endif%}
非常感谢你 根据API文档,应该是
request.files
哦,所以我应该使用
request.files
而不是request.file
?是的。您收到的错误消息表明没有request.file这样的东西;因此,您可能正在查找request.file。在使用特定库时,最好检查其api文档。