在FastAPI发布请求中上传图像导致400错误请求

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

我想通过邮寄请求获取图像,然后阅读。我正在尝试这样做:

import numpy as np
from PIL import Image
from fastapi import FastAPI, File, UploadFile, HTTPException, Depends
app = FastAPI()
@app.post("/predict_image")
    @logger.catch
    def predict_image(predict_image: UploadFile = File(...)):
        logger.info('predict_image POST request performed')
        try:
            pil_image = np.array(Image.open(predict_image.file))
        except:
            raise HTTPException(
                status_code=HTTP_422_UNPROCESSABLE_ENTITY, detail="Unable to process file"
            )
        pred = pil_image.shape
        logger.info('predict_image POST request performed, shape {}'.format(pred))
    return {'input_shape': pred}

回叫请求返回INFO: 127.0.0.1:59364 - "POST /predict_image HTTP/1.1" 400 Bad Request

enter image description here

如何解决?

UPD:

官方教程中的示例返回相同的例外:

@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile = File(...)):
    return {"filename": file.filename}
image post file-upload fastapi
1个回答
0
投票

您可能需要安装python-multipart

仅:

pip install python-multipart
© www.soinside.com 2019 - 2024. All rights reserved.