我想通过邮寄请求获取图像,然后阅读。我正在尝试这样做:
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
如何解决?
UPD:
官方教程中的示例返回相同的例外:
@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile = File(...)):
return {"filename": file.filename}