【问题标题】:Image is not seen in web in Flask在 Flask 的网络中看不到图像
【发布时间】: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)),看看文件是否真的被保存了。还有,你的 func prepare 是做什么的?
  • 我已经编辑了这个问题。函数 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


【解决方案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 的内置类型。

【讨论】:

  • 它显示同样的问题。图像存储在“静态”文件夹中,但在我们单击提交按钮后它不显示图像。
  • @SijinJohn页面右键查看html源码,获取&lt;img&gt;标签的src路径。为其添加主机前缀并将其放入您的浏览器,例如。 http://localhost:8080/static/abc.png。走着瞧吧。 404 还是别的什么?
  • @SijinJohn 你可能已经知道了,但是,某些类型的图像在网页中无法正确显示。比如CYMK模式jpg等。我们消除所有可能的原因然后我们得到答案。
  • 但它适用于单个模块(例如:用于 body_segmentation)。如果我们添加 3 到 4 个模块,则无法正常工作。并且 itz 给出错误 无法访问此站点 localhost 拒绝连接。尝试:检查连接检查代理和防火墙ERR_CONNECTION_REFUSED
  • @SijinJohn 这是一个烧瓶 wsgi 应用程序,对吧?添加模块意味着什么? python模块或烧瓶蓝图?你会提供一些你的应用程序设置代码,围绕app = Flask(...) 行吗?
【解决方案2】:

在渲染“bs.html”的模板时,传入图像文件名;那应该是文件路径,即abc吗?

【讨论】:

  • 试过了...但是当我们点击提交按钮时它没有显示图像。我已经编辑了我的问题。
猜你喜欢
  • 2012-05-26
  • 1970-01-01
  • 1970-01-01
  • 2013-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多