开发者社区> 问答> 正文

在Flask的网络中看不到图像

我尝试了所有可能性,但是当我单击按钮时,它没有显示我从本地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

展开
收起
is大龙 2020-03-24 12:26:10 645 0
1 条回答
写回答
取消 提交回答
  • 指定到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

    2020-03-24 12:26:18
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Session:更加安全、可靠的数据中心网络产品更新 立即下载
Session:极简易用的全球化网络产品更新 立即下载
Session:弹性、高可用、可观测的应用交付网络产品更新 立即下载