如何在 FastAPI 中返回 NumPy 数组? [重复]

问题描述 投票:0回答:1
@app.post("/predict/")
async def predict(file:UploadFile = File(...)):
    bytes_str = io.BytesIO(await file.read())
    img = Image.open(bytes_str)
    img_array = np.array(img.convert('RGB'))
    img_array = cv2.resize(img_array, (1024,1024))
    img_final = np.expand_dims(img_array, axis = 0)
    pred = model.predict(img_final)

此代码返回一个 NumPy 数组,我需要在端点上的 FastAPI 上返回它,但策略是什么?

python arrays numpy image-processing fastapi
1个回答
0
投票

尝试使用简单的 pred.tolist() 返回 fastapi 可以理解的普通列表。

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