我尝试了所有可能性,但是当我单击按钮时,它没有显示我从本地PC动态拍摄的图像。具有3至4个应用程序路由,其中包含3至4个不同的HTML。(其中一个是bs.html)。在本文中,我有两个模块:一个用于检测身体分割,另一个用于脑肿瘤检测。app.py
model = tf.keras.models.load_model("CNN1.model")
the_model = torch.load('cnn.pt')
app = Flask(__name__,instance_relative_config=True, static_url_path = "/static", static_folder = "static")
UPLOAD_FOLDER = './static'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def prepare(file): #image-processing for body segmentation
---------
---------
return sample_array
def transform(file): #image-processing for brain tumor
----------
----------
return img_t
@app.route('/')
def index():
# Main page
return render_template('RCnn.html')
@app.route('/body_seg')
def body_seg():
return render_template('bs1.html')
@app.route('/brain_t')
def brain_t():
return render_template('brain1.html')
@app.route('/body', methods=['POST','GET']) #to detect body segmentation parts
def body():
if request.method =='POST':
file1 = request.files['file']
if file1:
filename = secure_filename(file1.filename)
# task 1. let's get a clear path
path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
path = os.path.abspath(path)
# task 2. make sure the folder exists
folder = os.path.dirname(path)
if not os.path.isdir(folder):
raise IOError('no such folder: %s' % folder)
file1.save(path)
abc=prepare(os.path.join(app.config['UPLOAD_FOLDER'],filename))
uploadimage=file1.filename
prediction = model.predict(abc)
return render_template('bs1.html',image=uploadimage, data=data2, data3=data3)
@app.route('/brain', methods=['POST','GET']) #module to detect brain tumor
def brain():
if request.method =='POST':
file1 = request.files['file']
if file1:
filename = secure_filename(file1.filename)
# task 1. let's get a clear path
path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
path = os.path.abspath(path)
# task 2. make sure the folder exists
folder = os.path.dirname(path)
if not os.path.isdir(folder):
raise IOError('no such folder: %s' % folder)
file1.save(path)
#torch.save(model_conv,'cnn.pt')
the_model = torch.load('cnn.pt')
img_t = transform(os.path.join(app.config['UPLOAD_FOLDER'],filename))
uploadimage1=file1.filename
batch_t = torch.unsqueeze(img_t, 0)
out = the_model(batch_t)
return render_template('brain1.html',image1=uploadimage1,data=data4, data3=data5)
if __name__ == '__main__':
app.run(debug=True, use_reloader=False)
bs.html
</style>
</head>
<body bgcolor = "#ffeee6">
<h1><center><u>Radiological Image Classification</u></center></h1>
<h2><u>Body Part Segment Detection </u></h2>
<p><bold>Upload your Radiological image with different body parts: </bold></p>
<form action = "/body" method ='POST' enctype=multipart/form-data>
<input type="file" name="file" >
<input type="submit" value="upload" >
<h3><u>Results</u></h3>
<img src="{{url_for('static',filename = image)}}" align="middle" style="width:150px"/>
<p>{{data}}</p>
<p>{{data3}}</p>
</form>
问题来源:stackoverflow
指定到app.config ['UPLOAD_FOLDER']`的相对路径不是一个好主意,因为当前目录不是一个可靠的值。您可以使用相对于python脚本的路径。像这样:
app_dir = os.path.dirname(os.path.abspath(__file__))
app.config['UPLOAD_FOLDER'] = os.path.join(app_dir, 'static')
现在,app.config中的路径将始终是脚本文件旁边的静态文件夹。
然后做你的事情:
file = request.files['file']
if file:
filename = secure_filename(file.filename)
# task 1. let's get a clear path
path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
path = os.path.abspath(path)
# task 2. make sure the folder exists
folder = os.path.dirname(path)
if not os.path.isdir(folder):
raise IOError('no such folder: %s' % folder)
file.save(path)
# ... then do template rendering...
return render_template(...)
顺便说一句 使用file
作为变量名不是一个好主意。file
是python中的内置类型。
回答来源:stackoverflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。