【发布时间】:2020-03-17 08:45:01
【问题描述】:
我已经尝试了所有可能性,但是当我单击按钮时,它没有显示我从本地电脑动态获取的图像。 Is 有 3 到 4 个应用程序路由和 3 到 4 个不同的 Html。(一个是 bs.html)。在此我有两个模块:一个用于检测身体分割,另一个用于脑肿瘤检测。 应用程序.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>
【问题讨论】:
-
UPLOAD_FOLDER也是静态文件夹吗? -
是的。它在静态文件夹中。
UPLOAD_FOLDER = './static' -
所以主机中的本地路径是
os.path.join(app.config['UPLOAD_FOLDER'],filename)。试试path = os.path.join(app.config['UPLOAD_FOLDER'],filename)和print(path)和print(os.path.isfile(path)),看看文件是否真的被保存了。还有,你的 funcprepare是做什么的? -
我已经编辑了这个问题。函数 prepare 用于该图像预处理。 ```def prepare(file): IMG_SIZE = 100 img_array = cv2.imread(file, cv2.IMREAD_GRAYSCALE) img_array = img_array/255.0 new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE)) sample_array = new_array.reshape(- 1、IMG_SIZE、IMG_SIZE、1)返回sample_array
标签: python html python-3.x web flask