首页 > 解决方案 > Flask 无法读取从 html 上传的图像

问题描述

我正在尝试制作一个简单的 html 网页,用户可以在其中上传图片。该图像通过一种形式传递给flask API,其中图像被传递给图像分类模型。但是,上传文件后,我无法在 Flask API 中接收图像。我收到一条错误消息, werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand. KeyError: 'uploaded_img' 有人可以指导我哪里出错了吗?

我的html代码:

<body class="center-screen">
    <div id="content">
        <form id="url-form" action="/classify_image" method="post" enctype="multipart/form-data">
            <h1>Upload Image</h1>
            <div class="formcontainer">
                <hr/>
                <div class="container">
                    <label for="img">Upload image:</label>
                    <input type="file" id="img" name="uploaded_img" accept="image/*">
                </div>
                <button type="submit">
                    <strong>Classify Image</strong>
                </button>
            </div>
        </form>
    </div>
</body>

我的烧瓶端点:

@app.route('/classify_image', methods=['POST'])
def classify_image():
    if request.method == 'POST':
        print("Running")
        f = request.files['uploaded_img']
        model.predict(f)

标签: pythonhtmlflask

解决方案


推荐阅读