图像未使用烧瓶上传(当我单击上传时显示未选择文件)。 IDE显示302

问题描述 投票:0回答:2

我正在学习flask,尝试编写图像中边缘检测的代码,并让用户上传图像,使用flask。我相信我无法正确上传图像,当我单击上传时,它显示“未选择文件”。这是我的代码。

import os.path
from io import BytesIO
from tempfile import mkdtemp

from flask import Flask, render_template, flash, request
from flask import send_from_directory, redirect, send_file
from werkzeug.utils import secure_filename

from edge_new import edge_detector
from flask_app import UPLOAD_FOLDER

app = Flask(__name__)
app.secret_key = 'sib'

temp_dir = mkdtemp()
app.config[UPLOAD_FOLDER] = 'temp_dir'


@app.route('/')
def hello():
    return "The app is up and running. Go to /edge for functionality"


@app.route('/edge', methods=['POST', 'GET'])
def process_img():
    """
    Function takes an image,
    finds edges in the image,
    returns processed image for download.
    """
    if request.method == 'POST':

        # Check if image exists
        if 'img' not in request.files:
            flash("Image not found"), 400
            return redirect('/edge')

        image = request.files['img']

        # Check if image selected

        if image.filename == "":
            flash("No selected image"), 400
            return redirect('/edge')

        # Save the image

        image_path = secure_filename(image.filename)
        path = os.path.join(temp_dir, image_path)
        image.save(path)

        # Find edges in the image using edge_detector function

        processed_image = edge_detector(path)  # processed_img is ndarray

        # Save the processed image

        
        send_file(BytesIO(processed_image), download_name='processed.jpg', mimetype='image/jpg', as_attachment=True)
        
    return render_template('index.html')




if __name__ == '__main__':
    app.run(host='0.0.0.0', port=3000, debug=True)

我尝试过不同的事情。但错误仍然存在。我尝试过 send_file()、send_from_directory()、cv2.imwrite() 但我不确定什么有效。这是我的index.html代码。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>File Upload Example</title>
    <style>
    .ok{

        font-size: 20px;
    }
    .op{
        font-size: 20px;
        margin-left: -70px;
        font-weight: bold;
        background-color: yellow;
        border-radius: 5px;
        cursor: pointer;
    }


    </style>
</head>
<body>
    <div class="center">
        <h1> Uploading and Returning Files With a Database in Flask </h1>
        <form method="POST" action="/edge" enctype="multipart/form-data">
            <input class="ok" type="file" name="file">
            <button class="op">Submit</button>
        </form>
    </div>

</body>
</html>

python opencv flask edge-detection
2个回答
0
投票

可以使用 name 属性查询输入字段的值。不幸的是,您已经为输入字段设置了属性

file
,并且正在从
img
请求
request.files
。您可以将 html 代码中的属性更改为
img
或在服务器端请求
file


0
投票

我通过运行检查了你的代码。 您的端点 /edge 正在尝试在 if 条件下获取 img 并阻止:

if 'img' not in request.files:
    flash("Image not found"), 400
    return redirect('/edge')

image = request.files['img']

当您以 name="file" 的形式发送文件时

<input class="ok" type="file" name="file">

您可以通过更新index.html输入代码行来纠正它: (参见 name="img" 而不是 name="file")

<input class="ok" type="file" name="img">

如果我解决了你的问题,请点赞

© www.soinside.com 2019 - 2024. All rights reserved.